[Target detection] Model information analysis / YOLOv5 detection results displayed in Chinese

foreword

I wrote a blog post before [Target Detection] YOLOv5: Label Chinese display/custom color , mainly to solve the problem of target Chinese display from the display side.
This paper focuses on solving the target Chinese display problem from the model point of view.

Model Information Analysis

Under normal circumstances, the model printing information can be directly loaded, but the printed model information is not complete.

import torch
if __name__ == '__main__':
    model = torch.load('weights/best.pt')
    print(model)

At this point, you can view the model information through the method of breakpoint debugging.
insert image description here
The normal print out is also the same content, but the information of the three dictionaries , model, and are not displayed completely.optgit

In the model information, in addition to the structural parameters of the model, it also includes other information of the model. The name of the category information ismodel/names

insert image description here

The test result is displayed in Chinese

Therefore, if you need to change the category to Chinese information on the model side, you only need to modify model/namesthe content inside
. For example, here I small-vehiclechange the label to Chinese label 汽车, and then save it as a new model.

import torch
if __name__ == '__main__':
    model = torch.load('weights/best.pt')
    # print(model)
    model['model'].names[1] = '汽车'
    torch.save(model, "weights/new.pt")

Load a new model for detection, and you can see that the Chinese labels are perfectly displayed:

insert image description here

Model Volume Compression

With the above experience, it is not difficult to find that because the model needs to be used for loading training, it carries information such as git, , optetc. For pure reasoning tasks, this information does not work and is redundant information.

Therefore, the redundant information of the model can be further eliminated to reduce the volume of the model so that it can be deployed in lightweight devices.
Here we take the removal of git, optas an example:

if __name__ == '__main__':
    model = torch.load('weights/best.pt')
    model['git'] = None
    model['opt'] = None
    torch.save(model, "weights/new.pt")

insert image description here
It can be seen that after the model is eliminated, the size becomes smaller without affecting the operation of the detection task.

Guess you like

Origin blog.csdn.net/qq1198768105/article/details/130531095