RuntimeError: std::bad_cast trying to fine tune resnet18

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 https://gist.github.com/panovr/2977d9f26866b05583b0c40d88a315bf
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!

1 Like

I figured it out… turns out it is the incompatibility of the data type. since I defined the model on cuda, I need to copy the data (images and labels) onto cuda as well. And apparently “target” expects long tensor

@lliu25 Hey , I am also facing a similar issue , can you put up your modified code , so it becomes for me how can I copy the data on cuda . Thanks :slight_smile:

@rishabh135 if you load your images as a 4D numpy array and labels as 2D numpy array, you can do:

inputs = Variable(torch.from_numpy(images)).float().cuda()
targets = Variable(torch.from_numpy(labels)).long().cuda()

Be careful that some loss functions requires float for target, so you might want to change it to:

targets = Variable(torch.from_numpy(labels)).float().cuda()
2 Likes

I just wrote a short tutorial on fine tuning resnet in pytorch. Here - https://github.com/Spandan-Madan/Pytorch_fine_tuning_Tutorial

Hope this helps!