Some problems encountered during YoloV5 deployment, ModuleNotFoundError: No module named 'models', solution

In my case, the model is trained and the accuracy is not bad, but the detect module in YoloV5 needs to be deployed to the software

Moved the Models and Utils of YoloV5, put them in the libs folder that the software specifically depends on, and modified all the reference paths, but still reported an error.

Multi-party verification, found the problem: If you use the torch.save(model, PATH) code to save the model, when you use the saved model for prediction, the same file structure is required between the source file and the prediction program, this method will save the model The structure is also saved in the pt file. When you are deploying the model, if your dependencies and detect are not in the same folder, it will be useless even if you modify the reference method of the code. (ps: I saw someone say that this is because the path is wrong when importing. This is a novice problem. I am really convinced. Most of this person has never deployed yolo. Let me emphasize it again. It is not an import problem, and the import is no problem)

Specifically, someone raised and solved this problem in the source code

ModuleNotFoundError: No module named 'models' Issue #353 Ultrasound/Yorof5 (github.com)

Yolo's model structure is:

Solution 1:

It is consistent with the path of the yolov5 source code, which means that the detect.py you write yourself needs to be under the same module as models and utils.

But if your leader forbids you to do this, and insists that you put the dependencies together and the running files on the other side separately, then there is no way. (No, no one will be stuck for a week by a problem that can be solved in three minutes by an inexplicable demand)

Solution 2:

Train from scratch, change the source code structure to be consistent with your project structure before training, for example, my project is to put models and utils in the libs folder, but my detect is a folder at the same level as libs, then you can modify your source code Structure, separate out models and libs, and place your train, models, and utils files according to the path you deployed

But the problem with this solution is that there is no way to use pretrained weights. You will find that the four pre-trained weights yolos, yolox, yolom, and yolol that come with yolo seem to have saved the original path in the four pt files, which means that you have to train from scratch without using pre-trained weights, which may lead to The training didn't work well, and I just ended up not achieving the same accuracy as before. After four or five rounds of training, the accuracy is not as high as the original round.

Solution 3:

It is said that converting the saved model to onnx can solve this problem, so my detect needs to be rewritten.

Solution 4:

Add the following two lines of code at the beginning of the prune_mynet.py file that uses torch.load() to load the model:

import sys
sys.path.insert(0, './yolov5')

Just put the folders where your utils and models are located into the system path. This is the solution proposed by the person who asked this question on github

However, if there are other files with the same name in the folder where utils and models are located, that is to say, your utils may need to be renamed to utilsforyolo, modelsforyolo, in this case, the problem will come again. (Oh, what a disaster!)

Solution 5

Change the source code, only save the weight file, not the path.

Find all torch.save() functions in train.py during training, there should be two, change to

torch.save(model.state_dict(), PATH)

That is to save the state_dict attribute of the model, which has only weight and no path. perfect solution

Guess you like

Origin blog.csdn.net/m0_50317149/article/details/131774910