zhanghang1989 / PyTorch-Encoding

A CV toolkit for my papers.
https://hangzhang.org/PyTorch-Encoding/
MIT License
2.04k stars 450 forks source link

ResNet 101 pretrained model equivalent to old version #420

Closed fabriziojpiva closed 1 year ago

fabriziojpiva commented 1 year ago

Hi, I am currently using an old version of your repository, and the download link of the pretrained weights of ResNet 101 is not available anymore. The old link is https://hangzh.s3.amazonaws.com/encoding/models/resnet101-5be5422a.zip.

If I use the download link of the current repository version, either resnest101 or resnets101 do not work, because both pretrained models were trained using the deep version of the model with inPlanes=128. The pretrained model that I am asking is ResNet101 with the first layer depth of 64.

Could you please provide me the link of the same weights?

Thanks!

zhanghang1989 commented 1 year ago

That is renamed to resnet101s at

https://github.com/zhanghang1989/PyTorch-Encoding/blob/c959dab8312b637fcc7edce83607acb4b0f82645/encoding/models/model_zoo.py#L21

Please use get_model("resnet101s")

fabriziojpiva commented 1 year ago

Hi, thanks for replying. It does not work, because the one that you are mentioning is a model with self.inplanes=128. When I try that model I get the following error:

RuntimeError: Error(s) in loading state_dict for ResNet: size mismatch for bn1.weight: copying a param with shape torch.Size([128]) from checkpoint, the shape in current model is torch.Size([64]). size mismatch for bn1.bias: copying a param with shape torch.Size([128]) from checkpoint, the shape in current model is torch.Size([64]). size mismatch for bn1.running_mean: copying a param with shape torch.Size([128]) from checkpoint, the shape in current model is torch.Size([64]). size mismatch for bn1.running_var: copying a param with shape torch.Size([128]) from checkpoint, the shape in current model is torch.Size([64]). size mismatch for layer1.0.conv1.weight: copying a param with shape torch.Size([64, 128, 1, 1]) from checkpoint, the shape in current model is torch.Size([64, 64, 1, 1]). size mismatch for layer1.0.downsample.0.weight: copying a param with shape torch.Size([256, 128, 1, 1]) from checkpoint, the shape in current model is torch.Size([256, 64, 1, 1]).

To help you find the proper pretrained weights, this is the model that I am referring to:

def __init__(self, block, layers, num_classes=1000, dilated=True, multi_grid=False,
                 deep_base=True, norm_layer=nn.BatchNorm2d):
        self.inplanes = 64
        super(ResNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=1, padding=3,
                               bias=False)
        self.bn1 = norm_layer(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(
            block, 64, layers[0], stride=1, dilation=2, norm_layer=norm_layer)
        self.layer2 = self._make_layer(
            block, 128, layers[1], stride=2, dilation=2, norm_layer=norm_layer)
        self.layer3 = self._make_layer(
            block, 256, layers[2], stride=2, dilation=2, norm_layer=norm_layer)
        self.layer4 = self._make_layer(
            block, 512, layers[3], stride=1, dilation=4, norm_layer=norm_layer)

        self.avgpool = nn.AvgPool2d(7, stride=1)
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, norm_layer):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def _make_layer(self, block, planes, blocks, stride=1, dilation=1, norm_layer=None, multi_grid=False, multi_dilation=None):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,
                          kernel_size=1, stride=stride, bias=False),
                norm_layer(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, dilation=dilation,
                            downsample=downsample, previous_dilation=dilation, norm_layer=norm_layer))

        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes, dilation=dilation, previous_dilation=dilation,
                                norm_layer=norm_layer))

        return nn.Sequential(*layers)
zhanghang1989 commented 1 year ago

Can you try this one https://github.com/zhanghang1989/PyTorch-Encoding/blob/c959dab8312b637fcc7edce83607acb4b0f82645/encoding/models/model_zoo.py#L12

fabriziojpiva commented 1 year ago

Hi, thanks for the quick answer. I am pretty sure that is the right model definition, however, there is no pretrained weights for that specific model in model_store.py:

https://github.com/zhanghang1989/PyTorch-Encoding/blob/c959dab8312b637fcc7edce83607acb4b0f82645/encoding/models/model_store.py#L10

There is no resnet101 there, therefore no pretrained weights. There is resnest101 (it does not work) and resnet101s (also does not work).

fabriziojpiva commented 1 year ago

Hey after reading model_store.py again I figured out that you are downloading the weights of resnet101 directly from the official link of pytorch. Now it works!