본문 바로가기
Artificial Intelligence/Neural Networks

[ CNN ] 2. Grouped convolution - PyTorch Code

by SuperMemi 2021. 8. 11.
반응형

 

Grouped convolution - PyTorch Code

 


 

Grouped Convolution

 

Standard vs. grouped convolutions: (a) In a standard convolution S, each filter is convolved with all of the input's channels; (b) In a grouped convolution with two groups G(2), half of the filters are applied to each half of the input for a 2× reduction in parameters used. More generally, a grouped convolution with g groups uses g× fewer parameters.

 

  • Input Channel(C_in) 을 g개의 group으로 분리한다.
  • 각 group 을 독립적으로 Convolution 연산을 수행한다.

 

  • 장점 : 병렬 처리에 유리, 구현 간단, 기존 2D convolution에 비해 낮은 parameter 수
    • nn.Conv2d() 함수에서 groups(default : 1)의 값을 바꿔주면 된다.
    • parameter : (C_in * Kernel_szie^2 * C_out) / g

 

  • 단점 : 직접 group 개수를 설정해야 함. 많은 그룹으로 분할시 오히려 성능하락.

 

import torch.nn as nn

class ConvBNReLU(nn.Module):
    def __init__(self, C_in, C_out, kernel_size, stride, padding, g, affine=True):
        super(ConvBNReLU, self).__init__()

        self.op     = nn.Sequential(
            nn.Conv2d(C_in, C_out, kernel_size, stride=stride, padding=padding, bias=False, groups=g),
            nn.BatchNorm2d(C_out, affine=affine),
            nn.ReLU(inplace=False)   
        )

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


2021.08.12 - [AI/ML & DL] - [ CNN ] 3. Pointwise Convolution - PyTorch Code

 

[ CNN ] 3. Pointwise Convolution - PyTorch Code

보통의 convolution은 3x3 또는 5x5 크기의 필터를 사용해서 spatial feature 들을 뽑아낸다. 그런데 pointwise convolution 1x1 크기의 필터를 사용한다. 1x1 필터는 단순히 생각하면 똑같은 결과를 만드는 것이..

supermemi.tistory.com

 

 


[ 참고자료 ]

https://www.researchgate.net/profile/Jose-Cano-6/publication/342242494/figure/fig1/AS:903428242948107@1592405486871/Standard-vs-grouped-convolutions-a-In-a-standard-convolution-S-each-filter-is.ppm

https://eehoeskrap.tistory.com/431

https://hichoe95.tistory.com/48

https://www.slideshare.net/ssuser6135a1/ss-106656779


 

반응형