I used the pre-training model and fine-tuned it, then I saved the model and then restored the model, then extracted the features, and I got this error:
File "feature-map.py", line 58, in visualize_picture
y=myexactor(inputs)
File "/home/liupu/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
File "feature-map.py", line 45, in forward
x = module(x)
File "/home/liupu/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
File "/home/liupu/anaconda3/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 55, in forward
return F.linear(input, self.weight, self.bias)
File "/home/liupu/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 994, in linear
output = input.matmul(weight.t())
RuntimeError: size mismatch, m1: [4096 x 1], m2: [2048 x 11] at /opt/conda/conda-bld/pytorch_1525909934016/work/aten/src/THC/generic/THCTensorMathBlas.cu:249
Are you sure you are passing the images of correct size during evaluation? It seems there could be a mistake in that part. Can you please post your code to have a look? It’s difficult to hypothesize without code.
from __future__ import print_function, division
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.autograd import Variable
import torchvision
from torchvision import datasets, models, transforms
import time
import os
from PIL import Image
import matplotlib.pyplot as plt
import torch.nn.functional as F
plt.ion() # interactive mode
data_transforms = {
'val': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])
]),
}
data_dir = 'feature_pic/'
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
data_transforms[x])
for x in ['val']}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=2,
shuffle=False, num_workers=4)
for x in ['val']}
dataset_sizes = {x: len(image_datasets[x]) for x in ['val']}
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class FeatureExtractor(nn.Module):
def __init__(self, submodule, extracted_layers):
super(FeatureExtractor,self).__init__()
self.submodule = submodule
self.extracted_layers= extracted_layers
def forward(self, x):
outputs = []
for name, module in self.submodule._modules.items():
if name is "fc": x = x.view(x.size(0), -1)
x = module(x)
if name in self.extracted_layers:
outputs.append(x)
return outputs
# Iterate over data.
def visualize_picture(model):
model.eval()
for i, (inputs, labels) in enumerate(dataloaders['val']):
inputs = inputs.to(device)
labels = labels.to(device)
exact_list=["conv2","layer2","avgpool"]
myexactor=FeatureExtractor(model,exact_list)
y=myexactor(inputs)
#特征输出可视化
for i in range(4):
ax = plt.subplot(2, 2, i + 1)
#ax.set_title('Sample #{}'.format(i))
ax.axis('off')
plt.imshow(y[0].data.cpu().numpy()[0,i,:,:],cmap='jet')
plt.show()
plt.savefig('feature.png')
myresnet =torch.load("./checkpoint/model.pkl")
myresnet = myresnet.to(device)
visualize_picture(myresnet)
plt.ioff()
plt.show()
I think there may be a problem with the model I loaded, because I saved the entire network. If I only load the parameters, I don’t know how to do it.Thanks