pytorch-YOLOv4 icon indicating copy to clipboard operation
pytorch-YOLOv4 copied to clipboard

convert .pth file to .weights file

Open Sean-hku opened this issue 3 years ago • 1 comments

Hi.I want to convert my pth file to weights file, and I run this script model = Darknet(cfgfile='/media/hkuit164/WD20EJRX/yolov4.cfg') model.load_state_dict(torch.load("/media/hkuit164/WD20EJRX/pytorch-YOLOv4/yolov4.pth", map_location=torch.device('cpu'))) model.save_weights(outfile='newYolov4.weights', cutoff=-1) But there is an error: /home/hkuit164/anaconda3/envs/v4/bin/python3.7 /media/hkuit164/WD20EJRX/pytorch-YOLOv4/tool/darknet2pytorch.py convalution havn't activate linear convalution havn't activate linear convalution havn't activate linear Traceback (most recent call last): File "/media/hkuit164/WD20EJRX/pytorch-YOLOv4/tool/darknet2pytorch.py", line 518, in <module> model.load_state_dict(torch.load("/media/hkuit164/WD20EJRX/pytorch-YOLOv4/yolov4.pth", map_location=torch.device('cpu'))) File "/home/hkuit164/anaconda3/envs/v4/lib/python3.7/site-packages/torch/nn/modules/module.py", line 830, in load_state_dict self.__class__.__name__, "\n\t".join(error_msgs))) RuntimeError: Error(s) in loading state_dict for Darknet: Missing key(s) in state_dict: "models.0.conv1.weight", "models.0.bn1.weight", "models.0.bn1.bias", "models.0.bn1.running_mean", "models.0.bn1.running_var", "models.1.conv2.weight", "models.1.bn2.weight", "models.1.bn2.bias", "models.1.bn2.running_mean", "models.1.bn2.running_var", "models.2.conv3.weight", "models.2.bn3.weight", "models.2.bn3.bias", "models.2.bn3.running_mean", "models.2.bn3.running_var", "models.4.conv4.weight", "models.4.bn4.weight", "models.4.bn4.bias", "models.4.bn4.running_mean", "models.4.bn4.running_var", "models.5.conv5.weight", "models.5.bn5.weight", "models.5.bn5.bias", "models.5.bn5.running_mean", "models.5.bn5.running_var", "models.6.conv6.weight", "models.6.bn6.weight", "models.6.bn6.bias", "models.6.bn6.running_mean", "models.6.bn6.running_var", "models.8.conv7.weight", "models.8.bn7.weight", "models.8.bn7.bias", "models.8.bn7.running_mean", "models.8 I wonder how to convert pth file to weights

Sean-hku avatar Nov 30 '20 03:11 Sean-hku

Hey, I found a way to properly load the model (don't ask me why it works):

model = Yolov4(yolov4conv137weight=None, n_classes=1, inference=True)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if torch.cuda.device_count() > 1:
    model = torch.nn.DataParallel(model)
model.to(device=device)
pretrained_dict = torch.load(model_path, map_location=torch.device('cuda'))
model.load_state_dict(pretrained_dict)
model.cuda()

adrien-jacquot avatar Oct 01 '21 15:10 adrien-jacquot