Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I make predictions with the .pth model, I get 1000-sized logits arrays #2

Closed
systemosc opened this issue Jun 25, 2020 · 1 comment

Comments

@systemosc
Copy link

systemosc commented Jun 25, 2020

Hi, hope you're fine

I ran your code and it worked quite well : first run.sh then ResNet18_224.ipynb and finally ResNet18_FineTune.ipynb
I get the path files
The problem I have is that when I load the model weights, and I do y = model_ft(x) on an image from Tiny ImageNet, I get a probabilities/logits array of size 1000, and not 200 as expected.
Here is my code (strongly inspired by yours) :

model_ft = models.resnet18()
model_ft.conv1 = nn.Conv2d(3,64, kernel_size=(3,3), stride=(1,1), padding=(1,1), bias=False)
model_ft.maxpool = nn.Sequential()
model_ft.avgpool = nn.AdaptiveAvgPool2d(1)
model_ft.fc.out_features = 200

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model_ft = model_ft.to(device)

model_ft.load_state_dict(torch.load('./models/finetuned2/model_18_epoch.pt',
                                    map_location=torch.device('cpu')))

path = 'tiny-imagenet-200/val/images/val_2342.JPEG'
x = plt.imread(path)
plt.imshow(x)

x = np.transpose(x, (2, 0, 1))
x.reshape((1, 3, 64, 64))

x = Variable(torch.from_numpy(x))
x = x.float()
x = x.view(1, 3, 64, 64)

model_ft.eval()

y = model_ft(x)
z = y.detach().numpy()

Any help is welcome, thanks a lot.

@tjmoon0104
Copy link
Owner

I updated the code to re-initialize the final layer instead of overriding out_features. New one should be working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants