deep_sort_pytorch icon indicating copy to clipboard operation
deep_sort_pytorch copied to clipboard

size mismatch for classifier.4.weight: copying a param with shape torch.Size([751, 256]) from checkpoint, the shape in current model is torch.Size([1, 256]).

Open alexnguyen2201 opened this issue 4 years ago • 11 comments

I still do not understand why we need 751 classes for just extractor feature of persons?

~/Desktop/deep_sort_pytorch/deep_sort/deep$ python train.py --resume
Loading from checkpoint/ckpt.t7
Traceback (most recent call last):
  File "train.py", line 61, in <module>
    net.load_state_dict(net_dict)
  File "/home/alex/.pyenv/versions/3.7.0/lib/python3.7/site-packages/torch/nn/modules/module.py", line 847, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for Net:
	size mismatch for classifier.4.weight: copying a param with shape torch.Size([751, 256]) from checkpoint, the shape in current model is torch.Size([1, 256]).
	size mismatch for classifier.4.bias: copying a param with shape torch.Size([751]) from checkpoint, the shape in current model is torch.Size([1]).

alexnguyen2201 avatar May 01 '20 12:05 alexnguyen2201

Same here. Also didnt understand the whole "classifier" block too.

ghost avatar May 02 '20 00:05 ghost

Me too

pvti avatar May 02 '20 08:05 pvti

751 classes is because model was trained using Market1501 which has 751 classes. When training model, classifier block is used to classify images in training batch into the 751 different people in Market1501. When conducting reid, no classifying is done. Instead you'd just get the feature embeddings of the images in the form of an array, then conduct cosine distancing between images you want to compare.

To get array of features, you got to initialize the model with reid=True

khayliang avatar May 03 '20 05:05 khayliang

@khayliang thank you, khayliang

Could you tell me how I can get the data for another object like 'Car' with 751 classes to train for 'Car' objects.

alexnguyen2201 avatar May 03 '20 07:05 alexnguyen2201

You'd first want to train your model on some car reid dataset like VeRi, with 776 different vehicles, or classes. Once you got your weights you can get the feature embeddings of a car by just calling FeatureExtractor class with your image.

khayliang avatar May 03 '20 07:05 khayliang

751 classes is because model was trained using Market1501 which has 751 classes. When training model, classifier block is used to classify images in training batch into the 751 different people in Market1501. When conducting reid, no classifying is done. Instead you'd just get the feature embeddings of the images in the form of an array, then conduct cosine distancing between images you want to compare.

To get array of features, you got to initialize the model with reid=True

I changed the Net class to force reid=True, but still not working. Im trying to train my custom model, with my custom dataset at Market1501 format. def __init__(self, num_classes, reid=True):

Error: size mismatch for classifier.4.weight: copying a param with shape torch.Size([751, 256]) from checkpoint, the shape in current model is torch.Size([128, 256]). size mismatch for classifier.4.bias: copying a param with shape torch.Size([751]) from checkpoint, the shape in current model is torch.Size([128]).

emannuell avatar Aug 12 '21 17:08 emannuell

751 classes is because model was trained using Market1501 which has 751 classes. When training model, classifier block is used to classify images in training batch into the 751 different people in Market1501. When conducting reid, no classifying is done. Instead you'd just get the feature embeddings of the images in the form of an array, then conduct cosine distancing between images you want to compare. To get array of features, you got to initialize the model with reid=True

I changed the Net class to force reid=True, but still not working. Im trying to train my custom model, with my custom dataset at Market1501 format. def __init__(self, num_classes, reid=True):

Error: size mismatch for classifier.4.weight: copying a param with shape torch.Size([751, 256]) from checkpoint, the shape in current model is torch.Size([128, 256]). size mismatch for classifier.4.bias: copying a param with shape torch.Size([751]) from checkpoint, the shape in current model is torch.Size([128]).

did you find a solution to the problem?

ProBuro avatar Oct 14 '21 11:10 ProBuro

Make sure your training model saves the ckpt.t7 file, then Try to modify this file

# deep_sort/deep/model.py

def __init__(self, num_classes=1, reid=False)

WeiYangBin avatar Oct 28 '21 03:10 WeiYangBin

I have encountered the same problem. Has anyone solved it?

hejinrong0602 avatar Dec 01 '22 01:12 hejinrong0602

This solved for me: in deep_sort/deep/feature_extractor.py, inialize the model with your network's number of classes

def __init__(self, model_path, use_cuda=True):
    self.net = Net(num_classes=NUM_CLASSES, reid=True)

junyuli avatar Jan 13 '23 12:01 junyuli

I have changed the class_num, but it doesn't work

licpp avatar Feb 01 '24 10:02 licpp