Pytorch의 컨볼루션 및 디컨볼루션(conv2d 및 convTranspose2d)

회선

컨볼루션은 특징 추출을 위한 일반적인 작업으로, 컨볼루션은 이미지의 채널과 크기를 변경할 수 있으며, 완전 연결 작업에 비해 계산량을 줄이고 이미지의 로컬 특징을 완전히 통합할 수 있습니다.

영상

import torch
import torch.nn as nn

x = torch.randn(1,1,4,4)
model = nn.Conv2d(in_channels=1,out_channels=1,kernel_size=3,stride=1,padding=0)
output = model(x)
print('output shape',output.shape)

 

 

import torch
import torch.nn as nn

x = torch.randn(1,1,5,5)
model = nn.Conv2d(in_channels=1,out_channels=1,kernel_size=4,stride=1,padding=2)
output = model(x)
print('output shape',output.shape)

 

 디컨볼루션(ConvTranspose2d)

Deconvolution은 딥 러닝에서 일반적인 업샘플링 작업입니다. 또한 업샘플링에는 업풀링 및 바이큐빅 보간이 포함됩니다.

Deconvolution은 새로운 기능 맵을 얻기 위해 먼저 기능 맵을 보간/패딩한 다음 기존의 컨볼루션 작업을 수행하는 것으로 간주할 수 있습니다.

Deconvolution은 Convolution 연산의 역 과정이 아닙니다. 디콘볼루션을 통해 원래 행렬의 크기만 복원할 수 있지만 원래 행렬의 값을 완전히 복원할 수는 없습니다 .

deconvolution의 매개변수와 convolution 매개변수의 의미가 변경되었습니다. Pandding은 컨볼루션 커널이 중앙으로 이동하는 단계 수로 이해할 수 있습니다. 동시에 stride는 더 이상 커널이 이동하는 단계 수가 아니라 입력 단위가 서로 떨어져 퍼지는 단계 수가 됩니다.

Deconvolution 패딩 매개변수

패딩=0,kernel_size=3,스트라이드=1

J66KbV.gif

 패딩=2,kernel_size=3,스트라이드=1

여기에 이미지 설명 삽입

 padding=2일 때 커널이 2단위만큼 중앙으로 이동하는 것을 볼 수 있다.

deconvolution의 보폭 매개변수

패딩=0,kernel_size=3,스트라이드=1

패딩=0,kernel_size=3,스트라이드=2 

여기에 이미지 설명 삽입 

stride는 원본 그림에서 0을 채우는 것과 같고 채우기 형식은 stride-1임을 알 수 있습니다. 

 deconvolution의 output_padding 매개변수

stride > 1이면 보간은 기능 맵의 오른쪽 아래에서 수행됩니다.

import torch
import torch.nn as nn

x = torch.randn(1,1,3,3)
#这里的padding 与卷积的padding相反,卷积中的padding是在周围填充0,而这里的padding是已经在周围插值的kernel_size个0,padding表示的向中心移动的距离
#output_padding,表示当stride大于1的时候,需要在特征图周围进行填充
model = nn.ConvTranspose2d(in_channels=1,out_channels=1,kernel_size=3,stride=2,padding=1,output_padding=1)
output = model(x)
print('output shape',output.shape)

 

추천

출처blog.csdn.net/qq_40107571/article/details/128362203