Tag Archives: ModuleNotFoundError: No module named ‘models’

[Solved] Pytorch load pre-training model Error: modulenotfounderror: no module named ‘models’

Problem Description:

When using pytorch to load the pre training model, an error occurs: modulenotfounderror: no module named ‘models’. The pre training model is yolov5s.pt downloaded from the official

Solution:

Build the directory structure of yolov5 under the wrong file directory. That is, you need to exist yolov5 models and utils, and copy these two folders to the file directory. For example, at this time, the location where I report an error is yolov5/train.py. Because I use yolov5 for transplantation, the yolov5 directory is not a real yolov5, so I will report missing models. The deeper reason is that the following method is used to save weights (there is another way to save and load models, which can be viewed in the official document of pytorch. That method will not cause this problem and is more conducive to migration):

The weights saved in this way will also save the relative position of the source code of your model by default (here refers to the models folder and utils folder in yolov5).Yolo5s.pt downloaded from the official must be trained under this directory structure, so we can build this directory structure.