심드렁하게 저장

Convolution - Depthwise Convolution & Pointwise Convolution 본문

Artificial intelligence/Deep Learning

Convolution - Depthwise Convolution & Pointwise Convolution

Ggoosae 2025. 4. 23. 00:27

1. Depthwise Convolution

Depthwise Convolution의 추상적 그림 출처:https://paperswithcode.com/method/depthwise-convolution

 

Depthwise Convolution은 일반적인 Conv2D에서 채널 간 연산을 제거하고, 각 채널 별로 독립적 커널을 적용하는 방식이다. 이것은 MobileNet 계열에서 파라미터 수와 연산량을 줄이는데 효과적으로 사용되고있다. 이때 Spatial filtering만 수행하고 채널간 결합은 Pointwise Conv(1x1)로 따로 처리하는 것이 일반적이다.

Depthwise Conv 수식

  • $Y_{c}(i,j)$: 출력 feature map의 c 채널, 위치$(i,j)$의 값
  • $X_{c}$ : 입력의 c 채널
  • $W_{c}(m,n)$: 해당 채널에만 적용되는 커널(필터) $k_{n}\times k_{n}$ 크기
  • $(i+m,j+n)$: 필터를 적용할 입력 이미지의 위치
  • $c$ : 채널 인덱스, 모든 채널에 대해 독립적인 필터가 존재

Pytorch에서는 다음과 같이 구현할 수 있다.

import torch
import torch.nn as nn
import torch.nn.functional as F

class DepthwiseConv2d(nn.Module):
    def __init__(self, in_channels, kernel_size, stride=1, padding=0):
        super().__init__()
        # groups 인자에 in_channels 부여
        self.conv = nn.Conv2d(in_channels, in_channels, kernel_size, stride, padding, groups=in_channels)

    def forward(self, x):
        return self.conv(x)

 

Depthwise Conv의 활용 모델은 다음과 같다.

활용 모델 설명
MobileNet Depthwise + Pointwise Conv 조합으로 경량화
EfficientNet 일부 block에서 Depthwise 사용
ConvNeXt,MixNet Modern CNN에서 variation으로 사용

 

2. Pointwise Convolution

Pointwise Convolution 출처:https://paperswithcode.com/method/pointwise-convolution

 

Pointwise Convolution은 커널 크기가 1x1인 Conv로 각 위치에서 채널 간 선형 결합만 수행하는 연산이다. 입력의 Spatial 정보(위치)는 유지하면서 채널 정보를 조합하는데 특화되어있기 때문에 Depthwise Conv 이후 채널 정보를 통합할 때 자주 사용된다.

Pointwise Convolution 수식

  • $Y_{c_{out}}(i,j)$: 출력 채널 $c_{out}$의 위치 $(i,j)$ 값
  • $X_{c_{in}}(i,j)$  : 입력 채널 $c_{in}$의 동일 위치 값
  • $W_{c_{out},c_{in}}$: 1x1 위치에서 입력 채널을 출력 채널로 연결하는 가중치
  • $(i,j)$: 공간상 위치 인덱스 (H,W)

Pytorch를 사용하면 다음과 같이 간단히 구현할 수 있다.

class PointwiseConv2d(nn.Module):
    def __init__(self, in_channels, out_channels):
        super().__init__()
        # 커널 사이즈 1
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1)

    def forward(self, x):
        return self.conv(x)

 

Pointwise Conv는 다음 모델들에서 활용된다.

활용 모델 설명
Inception Module 다양한 크기의 filter 결과를 1x1 conv로 통합
ResNet Bottleneck 채널 수 축소와 확장을 위해 1x1 conv 사용
ViT Patch Embedding 이미지 패치를 채널로 인코딩할 때 사용됨