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.
The normal print out is also the same content, but the information of the three dictionaries , model
, and are not displayed completely.opt
git
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
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/names
the content inside
. For example, here I small-vehicle
change 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:
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
, , opt
etc. 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
, opt
as an example:
if __name__ == '__main__':
model = torch.load('weights/best.pt')
model['git'] = None
model['opt'] = None
torch.save(model, "weights/new.pt")
It can be seen that after the model is eliminated, the size becomes smaller without affecting the operation of the detection task.