Hi, I just started digging into pytorch. I want to fine-tune a resnet18 model, and I modified the last layer. But an error occurred when I tried to call the original resnet18 model.
Traceback (most recent call last):
File “pt_resnet.py”, line 85, in
outputs = new_mod(inputs)
File “/usr/lib64/python2.7/site-packages/torch/nn/modules/module.py”, line 206, in call
result = self.forward(*input, **kwargs)
File “/usr/lib64/python2.7/site-packages/torch/nn/parallel/data_parallel.py”, line 59, in forward
return self.module(*inputs[0], **kwargs[0])
File “/usr/lib64/python2.7/site-packages/torch/nn/modules/module.py”, line 206, in call
result = self.forward(*input, **kwargs)
File “pt_resnet.py”, line 54, in forward
f = self.features(x)
File “/usr/lib64/python2.7/site-packages/torch/nn/modules/module.py”, line 206, in call
result = self.forward(*input, **kwargs)
File “/usr/lib64/python2.7/site-packages/torch/nn/modules/container.py”, line 64, in forward
input = module(input)
File “/usr/lib64/python2.7/site-packages/torch/nn/modules/module.py”, line 206, in call
result = self.forward(*input, **kwargs)
File “/usr/lib64/python2.7/site-packages/torch/nn/modules/batchnorm.py”, line 43, in forward
self.training, self.momentum, self.eps)
File “/usr/lib64/python2.7/site-packages/torch/nn/functional.py”, line 439, in batch_norm
return f(input, weight, bias)
RuntimeError: std::bad_cast
I followed the code provided in Fine-tuning pre-trained models with PyTorch · GitHub
The code that involves the definition is
class my_model(nn.Module):
def init(self, original_model, num_cls):
super(my_model, self).init()
self.features = nn.Sequential(*list(original_model.children())[:-1])
…
new_mod is created by
resn18 = models.resnet18(pretrained=True)
new_mod = my_model(resn18, label_dim)
new_mod = torch.nn.DataParallel(new_mod).cuda()
And the error occurred when it is called in the training
Any suggestion would be helpful. Thanks!