Where should I look to solve "running_mean" error in ResNet transfer learning?

Hi! I’m trying to implement transfer learning on binary classification using ResNet model. I have tried the solution showed in this discussion but I’m stuck in this error:

RuntimeError: running_mean should contain 128 elements not 64

This is the way I define and call the pretrained model:

class ResnetPretrained(models.resnet.ResNet):
    def __init__(self, block, layers, num_classes=2):
        self.inplanes = 128
        super(ResnetPretrained, self).__init__(block, layers)
        self.conv1 = nn.Conv2d(1, 128, kernel_size=(7, 7), bias=False)
model = ResnetPretrained(models.resnet.Bottleneck, [3, 8, 36, 3]).to(device)

My input is 128x128, 1 channel and I’m normalizing the datasets with its own mean and std parameters.

This line calls the error:

for epoch in range(self.num_epochs):
    i = 0
    for images, labels in self.train_set_loader:
        images = images.to(device)
        labels = labels.to(device))
        outputs = model(images)       <----------- this line calls the error
        loss = criterion(outputs, labels.long())  

The error reference points to this batch_norm nn.functional function

I don’t understand what this error means, where should I look to solve it?


The original resnet’s first convolution out channel is 64, but you are using 128. Thus it does not work with the next batch norm as well as following layers.

Please use self.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), bias=False); self.inplanes = 128 or you have to change the entire network.

Thanks! Now it’s working :smiley: