Hi alban, I did not understand “If your original convolution was dense, I would says you can create a anew group convolution, copy the weights in it and replace the dense convolution by the group one in your model”.
I will try to give an example of what i am trying to do, Let’s say I am training the Dense Convolution MobileNet Model
“”"
class mobilenet(nn.Module):
def init(self, num_classes=10, _groups=1):
super(mobilenet, self).init()
    def conv_bn(inp, oup, stride):
        return nn.Sequential(
            nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
            nn.BatchNorm2d(oup),
            nn.ReLU(inplace=True)
        )

    def conv_dw(inp, oup, stride, _groups=2):
        return nn.Sequential(
            nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
            nn.BatchNorm2d(inp),
            nn.ReLU(inplace=True),

            nn.Conv2d(inp, oup, 1, 1, 0, groups=_groups, bias=False),
            nn.BatchNorm2d(oup),
            nn.ReLU(inplace=True),
        )

    self.model = nn.Sequential(
        conv_bn(  3,  32, 1), 
        conv_dw( 32,  64, 1, _groups),
        conv_dw( 64, 128, 1, _groups),
        conv_dw(128, 128, 1, _groups),
        conv_dw(128, 256, 2, _groups),
        conv_dw(256, 256, 1, _groups),
        conv_dw(256, 512, 2, _groups),
        conv_dw(512, 512, 1, _groups),
        conv_dw(512, 512, 1, _groups),
        conv_dw(512, 512, 1, _groups),
        conv_dw(512, 512, 1, _groups),
        conv_dw(512, 512, 1, _groups),
        conv_dw(512, 1024, 2, _groups),
        conv_dw(1024, 1024, 1, _groups),
        nn.AvgPool2d(4),
    )
    self.fc = nn.Linear(1024, num_classes)

def forward(self, x):
    x = self.model(x)
    #print("The shape of the intermediate feature is:", x.shape)
    x = x.view(-1, 1024)
    x = self.fc(x)
    #print("Returning the output size:", x.size())
    return x

“”"
I have done group pruning like condensenet and store all the convolutional weights. Let’s say groups=4, So due to pruning my initial convolutional weights [[0.5, 0.7, 0.8, 0.3], […], […], […]] has become  [[0.8, 0, 0, 0], [0, 0.9, 0, 0], [0, 0, 0.3, 0], [0, 0, 0, 0.5]]
let’s say I have function which takes input as cnn.weight.data and groups and returns output as cnn_group.weight.data.
[[0.8, 0, 0, 0], [0, 0.9, 0, 0], [0, 0, 0.3, 0], [0, 0, 0, 0.5]] --> [0.8, 0.9, 0.3, 0.5]
“”"
I have to run the function across all convolutional layers and store weights in each layer and upload those weights to the mobilenet model with groups=2
I stored weights like this
for i, j in enumerate(model):
if isinstance(i, nn.Conv2d):
_dict[i] = convert_wt(i, groups)
How to upload this dict to each convolutional layer.