# Question about group convolution

**URL:** <https://discuss.pytorch.org/t/question-about-group-convolution/17042>\
**Category:** Uncategorized\
**Created:** [April 26, 2018, 10:05am UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042 "2018-04-26T10:05:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alpha](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alpha/32/25271_2.png) [@Alpha](https://discuss.pytorch.org/u/Alpha)\
**Post date:** [April 26, 2018, 10:05am UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/1 "2018-04-26T10:05:43Z")

</div>

Hi,

I read the doc about **group** of the Conv2d().

e.g. If I use **group=10** , does it mean that 10 convolution layers side by side and the 10 layers share the same parameters?

If so, is there an elegant way to use 10 layers of different parameters ?  
i.e:  
I have a tensor whose size is [batch\_size, channel=100, H, W]  
and I want to have 5 Conv layers, each looks at only 20 of the 100 channel, how can I do?  
Must I use slice operation? Is there a better way?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 26, 2018, 4:35pm UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/2 "2018-04-26T16:35:06Z")

</div>

I think for your use case you can just use `groups=5`:

```auto
conv = nn.Conv2d(
    in_channels=100,
    out_channels=5,
    kernel_size=3,
    stride=1,
    padding=1,
    groups=5)
print(conv.weight.shape)
> torch.Size([5, 20, 3, 3])

```

Each kernel of the 5 filters will just use 20 input channels and create an output.  
Leaving `groups=1` gives you `torch.Size([5, 100, 3, 3])`, which means each filter will use all 100 input channels.

---

<div class="post-metadata">

**Author:** ![Alpha](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alpha/32/25271_2.png) [@Alpha](https://discuss.pytorch.org/u/Alpha)\
**Post date:** [April 27, 2018, 2:40am UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/3 "2018-04-27T02:40:01Z")

</div>

Thank you very much.

When I use group = 5, I understand each kernel of the 5 filters just use 20 input channels.

But as the **conv.weight.shape** is [5, 20, 3, 3], does it mean that the 5 filters will share weight ?

---

<div class="post-metadata">

**Author:** ![DanielLCH](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@DanielLCH](https://discuss.pytorch.org/u/DanielLCH)\
**Post date:** [September 21, 2018, 6:26am UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/4 "2018-09-21T06:26:17Z")

</div>

You can look for result in the source code.

---

<div class="post-metadata">

**Author:** ![Guodong\_Zhang](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/guodong_zhang/32/7275_2.png) [@Guodong\_Zhang](https://discuss.pytorch.org/u/Guodong_Zhang)\
**Post date:** [October 28, 2018, 3:53pm UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/5 "2018-10-28T15:53:55Z")

</div>

Have you figured it out?

---

<div class="post-metadata">

**Author:** ![Jayan-K-Duggal](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jayan-k-duggal/32/10191_2.png) [@Jayan-K-Duggal](https://discuss.pytorch.org/u/Jayan-K-Duggal)\
**Post date:** [March 20, 2019, 5:04am UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/6 "2019-03-20T05:04:33Z")

</div>

@ptrblck\_de I want to know group convolution is different from simple convolution. What is the effect of it on mocel size, speed and accuracy?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 20, 2019, 3:18pm UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/7 "2019-03-20T15:18:56Z")

</div>

The number of parameters in a grouped convolution will most likely differ, e.g. in the example posted above you see that each kernel has 20 input channels due to the 5 groups instead of 100 as in a vanilla convolution.

I’m not sure you can generalize the speed, since depending on e.g. the kernel size different algorithms might be used by cuDNN, so that even model with more operations (on paper) might run faster than other optimized models.

Basically the same applies to the accuracy, as the model architecture is problem dependent, so you would just have to try out different approaches or stick to a good paper claiming good results using grouped/non-grouped convolutions.

---

<div class="post-metadata">

**Author:** ![lan2720](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lan2720/32/6181_2.png) [@lan2720](https://discuss.pytorch.org/u/lan2720)\
**Post date:** [June 30, 2019, 1:08am UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/8 "2019-06-30T01:08:22Z")

</div>

Nope.  
For example, if in\_c = 10, out\_c = 20, in\_w = 64, in\_h = 64, group=10, each input channel (whose shape is (64,64,in\_c//group=1)) will have independently filter (whose shape for example is (5,5,1)), the two will be convoluted to (60,60,1). so here group=10 will generate (60,60,group). If out\_c = group=10, this will just be the resulting output. But if out\_c is the multiple of group like 20, 30, etc, there will be out\_c//group feature map concatenating to (60,60,out\_c), the total filter parameter = group \* (5, 5, in\_c//group) \* (out\_c//group) = 5 \* 5 \* in\_c \* (out\_c//group). All of weights are different instead of sharing. This op is used in depthwise seperable convolution like MobileNet. When we want to seperate depth, group=in\_c is what we want.

---

<div class="post-metadata">

**Author:** ![Sanjayvarma11](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sanjayvarma11/32/20339_2.png) [@Sanjayvarma11](https://discuss.pytorch.org/u/Sanjayvarma11)\
**Post date:** [March 2, 2020, 7:14am UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/9 "2020-03-02T07:14:12Z")

</div>

Hi sir,Actually in depthwise seperable convolution we will follow operation by 3x3 followed by 1x1.In nn.conv2d we specify the no of groups in the convolution where the process of seperating the channels took place.can you tell me where the combining of channels i.e(using 1x1) took place in nn.conv2d??

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 2, 2020, 7:19am UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/10 "2020-03-02T07:19:46Z")

</div>

You would have to use a separate layer with a kernel size of 1 to combine the features.

---

<div class="post-metadata">

**Author:** ![Sanjayvarma11](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sanjayvarma11/32/20339_2.png) [@Sanjayvarma11](https://discuss.pytorch.org/u/Sanjayvarma11)\
**Post date:** [March 2, 2020, 8:11am UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/11 "2020-03-02T08:11:04Z")

</div>

So suppose we need to obtain 512 features from 256 features using depthwise seperable convolution then  
plz give one example of code doing depth wise seperable convolution.Thank you for answering

---

<div class="post-metadata">

**Author:** ![malcolmyanguci](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@malcolmyanguci](https://discuss.pytorch.org/u/malcolmyanguci)\
**Post date:** [May 16, 2020, 10:28am UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/12 "2020-05-16T10:28:44Z")

</div>

Hi. How about if I want to construct the filters with different size of channels? For example, instead of 5 filters all pass 20 channels, i want to do something like[1,10,3,3],[1,20,3,3],[1,30,3,3],[1,30,3,3],[1,10,3,3]. Do you think is possible?

Besides, I wish my question might get your concern. Very appreciated  
[https://discuss.pytorch.org/t/how-to-train-the-paralleled-layers/81317](https://discuss.pytorch.org/t/how-to-train-the-paralleled-layers/81317)

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 16, 2020, 10:42am UTC](https://discuss.pytorch.org/t/question-about-group-convolution/17042/13 "2020-05-16T10:42:29Z")

</div>

I don’t think this approach would work out of the box with the current implementations without splitting the operations or alternatively zeropadding the filter kernels in the unwanted input channel dimensions.
