xuexingyu24 / License_Plate_Detection_Pytorch

A two stage lightweight and high performance license plate recognition in MTCNN and LPRNet
Other
641 stars 171 forks source link

LPRNet model weights cannot be used from checkpoints #15

Closed mochechan closed 4 years ago

mochechan commented 4 years ago

In my evaluation, the LPRNet weights are fine from pre-trained weights.

lprnet.load_state_dict(torch.load('LPRNet/weights/Final_LPRNet_model.pth', map_location=lambda storage, loc: storage))

STN.load_state_dict(torch.load('LPRNet/weights/Final_STN_model.pth', map_location=lambda storage, loc: storage)

However, the trained weights in checkpoint folder cannot be used.

lprnet.load_state_dict(torch.load(os.getcwd() + '/saving_ckpt/lprnet_Iter_001800_model.ckpt', map_location=lambda storage, loc: storage))

STN.load_state_dict(torch.load(os.getcwd() + '/saving_ckpt/stn_Iter_001800_model.ckpt', map_location=lambda storage, loc: storage))

The following main.py is modified for my environment, so the line numbers in the following error messages are not identical to main.py.

# ls saving_ckpt/
lprnet_Iter_000900_model.ckpt  stn_Iter_000900_model.ckpt
lprnet_Iter_001800_model.ckpt  stn_Iter_001800_model.ckpt

# python3 main.py -image /data/pics/
Traceback (most recent call last):
  File "main.py", line 29, in <module>
    lprnet.load_state_dict(torch.load(os.getcwd() + '/saving_ckpt/lprnet_Iter_001800_model.ckpt', map_location=lambda storage, loc: storage))
  File "/root/anaconda3/envs/eval/lib/python3.7/site-packages/torch/nn/modules/module.py", line 769, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for LPRNet:
    Missing key(s) in state_dict: "backbone.0.weight", "backbone.0.bias", "backbone.1.weight", "backbone.1.bias", "backbone.1.running_mean", "backbone.1.running_var", "backbone.4.block.0.weight", "backbone.4.block.0.bias", "backbone.4.block.2.weight", "backbone.4.block.2.bias", "backbone.4.block.4.weight", "backbone.4.block.4.bias", "backbone.4.block.6.weight", "backbone.4.block.6.bias", "backbone.5.weight", "backbone.5.bias", "backbone.5.running_mean", "backbone.5.running_var", "backbone.8.block.0.weight", "backbone.8.block.0.bias", "backbone.8.block.2.weight", "backbone.8.block.2.bias", "backbone.8.block.4.weight", "backbone.8.block.4.bias", "backbone.8.block.6.weight", "backbone.8.block.6.bias", "backbone.9.weight", "backbone.9.bias", "backbone.9.running_mean", "backbone.9.running_var", "backbone.11.block.0.weight", "backbone.11.block.0.bias", "backbone.11.block.2.weight", "backbone.11.block.2.bias", "backbone.11.block.4.weight", "backbone.11.block.4.bias", "backbone.11.block.6.weight", "backbone.11.block.6.bias", "backbone.12.weight", "backbone.12.bias", "backbone.12.running_mean", "backbone.12.running_var", "backbone.16.weight", "backbone.16.bias", "backbone.17.weight", "backbone.17.bias", "backbone.17.running_mean", "backbone.17.running_var", "backbone.20.weight", "backbone.20.bias", "backbone.21.weight", "backbone.21.bias", "backbone.21.running_mean", "backbone.21.running_var", "container.0.weight", "container.0.bias". 
    Unexpected key(s) in state_dict: "iters", "net_state_dict".

How to use my trained weights?

mochechan commented 4 years ago

A solution was found. Add the following code to generate weight files, and use the weights to predict/test.

torch.save(lprnet.state_dict(),  os.path.join(save_dir, 'lprnet' + str(total_iters) + '.model'))            
torch.save(STN.state_dict(),  os.path.join(save_dir, 'stn' + str(total_iters) + '.moodel'))