Share weight in Resnet model


I have a fine-tuned ResNet model, but I have no idea how I can do to share a weight for this architecture!

Basically, the idea is to share the weight only for the base_network.
Thank you<

class ResNetModel(nn.Module):

    def __init__(self, class_num):
        super(ResNetModel, self).__init__()
        # avg pooling to global pooling
        model_ft.avgpool = nn.AdaptiveAvgPool2d((1, 1))

        num_ftrs = model_ft.fc.in_features  # extract feature parameters of fully collected layers
        add_block = []
        num_bottleneck = 512
        add_block += [nn.Linear(num_ftrs,
                                num_bottleneck)]  # add a linear layer, batchnorm layer, leakyrelu layer and dropout layer
        add_block += [nn.BatchNorm1d(num_bottleneck)]
        add_block += [nn.LeakyReLU(0.1)]
        add_block += [nn.Dropout(p=0.5)]  # default dropout rate 0.5
        # transforms.CenterCrop(224),
        add_block = nn.Sequential(*add_block)
        model_ft.fc = add_block
        self.model = model_ft

        classifier = []
        classifier += [nn.Linear(num_bottleneck, class_num)]  # class_num classification
        classifier = nn.Sequential(*classifier)
        self.classifier = classifier

    def forward(self, x):
        x = self.model(x)
        x = self.classifier(x)
        return x

Based on the image you’ve posted, it looks like you should just reuse the layer where “share weight” is pointed to.
I’m not sure how your code snippet relates to the image, since it seems your need two inputs (image sequence and a single image?).

I have updated the image. It’s a single image input. But how can I make the output of the first network to be the input of the second network while sharing the resnet base parameters?

I’m not sure if you will train the share weight twice in one forward action, if it is , I think the forward code should be something like following.

def forward(self, x):
    input_image = x
    x = self.share_weight_layer(x)
    pool = x = self.avg_pool(x)
    product = pool * input_image
    x = self.some_layer(product)
    x = self.share_weight_layer(x) # this is the same layer as previous one
    return x