Error in extracting features

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 "", line 58, in visualize_picture
  File "/home/liupu/anaconda3/lib/python3.6/site-packages/torch/nn/modules/", line 491, in __call__
    result = self.forward(*input, **kwargs)
  File "", line 45, in forward
    x = module(x)
  File "/home/liupu/anaconda3/lib/python3.6/site-packages/torch/nn/modules/", line 491, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/liupu/anaconda3/lib/python3.6/site-packages/torch/nn/modules/", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/liupu/anaconda3/lib/python3.6/site-packages/torch/nn/", 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/

What should I do? Can you help me?

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.

Thank you very much, I posted my code below:

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.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),
                  for x in ['val']}
dataloaders = {x:[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):
        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:
        return outputs

# Iterate over data.
def visualize_picture(model):
    for i, (inputs, labels) in enumerate(dataloaders['val']):
        inputs =
        labels =         


        for i in range(4):
            ax = plt.subplot(2, 2, i + 1)
            #ax.set_title('Sample #{}'.format(i))

myresnet =torch.load("./checkpoint/model.pkl")
myresnet =

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

you can follow the procedure shown here to save and load models.