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

how to convert darknet .weight to pytorch .pt file

Open songzhifei opened this issue 3 years ago • 23 comments

I have trained based on darknet-yolov4 and get .weight file. how can i convert it to pytorch .pth file. i have tried to use darknet2pytorch.py. i use the .pth and it report an erro like this:

File "models.py", line 476, in pretrained_dict = torch.load(weightfile) File "D:\ProgramData\Anaconda3\lib\site-packages\torch\serialization.py", line 585, in load return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args) File "D:\ProgramData\Anaconda3\lib\site-packages\torch\serialization.py", line 755, in _legacy_load magic_number = pickle_module.load(f, **pickle_load_args) _pickle.UnpicklingError: invalid load key, '\x00'.

songzhifei avatar Sep 29 '20 03:09 songzhifei

darknet2pytorch.py 里用Darknet构建模型后,load_weights,最后torch.save就好了

Linchunhui avatar Oct 13 '20 11:10 Linchunhui

darknet2pytorch.py 里用Darknet构建模型后,load_weights,最后torch.save就好了

How exactly do you use torch.save on the .weights file you just loaded? I'm having the exact problem as OP.

ajsanjoaquin avatar Dec 14 '20 10:12 ajsanjoaquin

Does the same problem occur after running the code below?

from tool import darknet2pytorch
import torch

# load weights from darknet format
model = darknet2pytorch.Darknet('path/to/cfg/yolov4-416.cfg', inference=True)
model.load_weights('path/to/weights/yolov4-416.weights')

# save weights to pytorch format
torch.save(model.state_dict(), 'path/to/save/yolov4-pytorch.pth')

# reload weights from pytorch format
model_pt = darknet2pytorch.Darknet('path/to/cfg/yolov4-416.cfg', inference=True)
model_pt.load_state_dict(torch.load('path/to/save/yolov4-pytorch.pth'))

ryj0902 avatar Jan 11 '21 08:01 ryj0902

Thank you this solves it. However, I am getting a false warning from output that the linear activation for the convolutional blocks aren't initialized when loading the yolov4.cfg. The code runs fine on the demo images regardless. I will fix this with a PR.

ajsanjoaquin avatar Jan 12 '21 10:01 ajsanjoaquin

Hello , i have used the above script to convert yolov4 darknet weights to pytorch weights , I am getting the following as shown below , error

and when I am running the prediction on video using this weights I am getting the following error

error

vishnuvardhan58 avatar Jan 12 '21 22:01 vishnuvardhan58

Hello , i have used the above script to convert yolov4 darknet weights to pytorch weights , I am getting the following as shown below , error

It's a false warning and it should not impact your model's performance. If you want to get rid of it, you can copy the code I wrote in the PR I opened for this issue.

and when I am running the prediction on video using this weights I am getting the following error

error

"yolov4demo.py" isn't a file in this repo. From the traceback, it seems you have to call on a method on your model object before loading the state_dict.

ajsanjoaquin avatar Jan 13 '21 07:01 ajsanjoaquin

Hello @ajsanjoaquin , thankyou for your reply.

vishnuvardhan58 avatar Jan 14 '21 14:01 vishnuvardhan58

a related question, how to convert pytorch pt file to darknet .weight file?

laoshaw avatar Mar 17 '21 01:03 laoshaw

@laoshaw were you able to convert pt file to weight file?

bobbilichandu avatar Mar 24 '21 10:03 bobbilichandu

looks like only for yolo3, but I actually did not test it eventually, the answer is no

laoshaw avatar Mar 24 '21 13:03 laoshaw

@ryj0902 I am getting error while importing tool. The error is as follows:

ModuleNotFoundError Traceback (most recent call last) in () ----> 1 from tool.darknet2pytorch import * 2 WEIGHTS = Darknet(cfgfile) 3 WEIGHTS.load_weights(weightfile)

/usr/local/lib/python3.7/dist-packages/tool/init.py in () 9 # Software Foundation. See the file README for copying conditions. 10 # ---> 11 from context_locals import app, local 12 from application import Application, WebApplication #, current_app as app 13

ModuleNotFoundError: No module named 'context_locals'

I did 'pip install tool'. Could you help?

gauravgund avatar May 09 '21 07:05 gauravgund

@gauravgund
Code should run on the top-level folder of this repository.
So the module imported here should be /pytorch-YOLOv4/tool, not /usr/local/lib/python3.7/dist-packages/tool.

ryj0902 avatar May 09 '21 10:05 ryj0902

@ryj0902 : I am able to convert it into .pth. But when i am trying to run TTA using https://github.com/kentaroy47/ODA-Object-Detection-ttA

I am getting an error pertaining to pytorch-YOLOv4 which means the conversion has not happened correctly. I think it is not generating the boxes correctly

/content/pytorch-YOLOv4/tool/darknet2pytorch.py in forward(self, x) 224 return out_boxes 225 else: --> 226 return get_region_boxes(out_boxes) 227 228 def print_network(self):

/content/pytorch-YOLOv4/tool/torch_utils.py in get_region_boxes(boxes_and_confs) 55 for item in boxes_and_confs: 56 boxes_list.append(item[0]) ---> 57 confs_list.append(item[1]) 58 59 # boxes: [batch, num1 + num2 + num3, 1, 4]

IndexError: index 1 is out of bounds for dimension 0 with size 1'

gauravgund avatar May 09 '21 14:05 gauravgund

@gauravgund I've never seen such an error...
What pre-trained model did you use? Could you check the cfg file? Is the last layer is [yolo]?

ryj0902 avatar May 10 '21 12:05 ryj0902

yes, my yolo model is working fine using open-cv. Just that i wanted to try tta and weight-box fusion.

On Mon, 10 May 2021 at 5:51 PM, Giha Ryu @.***> wrote:

@gauravgund https://github.com/gauravgund I've never seen such an error... What pre-trained model did you use? Could you check the cfg file? Is the last layer is [yolo]?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Tianxiaomo/pytorch-YOLOv4/issues/281#issuecomment-836630230, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALBKX6VQZJQ3CUSVC2YEKVTTM7FWLANCNFSM4R5ICLWA .

gauravgund avatar May 10 '21 12:05 gauravgund

@gauravgund well..., I am not sure what is causing this problem. It seems good to open up to other issues and ask others for help.

ryj0902 avatar May 11 '21 13:05 ryj0902

hey there, i was wondering on how do i input the images and how do i output them from the pytorch model, is there a script i can use? or what should i do?

thatITfox avatar Feb 28 '22 07:02 thatITfox

@thatITfox I think demo.py can help you. code below shows you how to load a model from pytorch model. https://github.com/Tianxiaomo/pytorch-YOLOv4/blob/a65d219f9066bae4e12003bd7cdc04531860c672/demo.py#L65-L67 although code from another function, you can process image as follows. https://github.com/Tianxiaomo/pytorch-YOLOv4/blob/a65d219f9066bae4e12003bd7cdc04531860c672/demo.py#L46-L57 If you curious about the preprocessing and postprocessing(a.k.a. NMS) see do_detect function. (actually, excluding pre/post-processing, the model execution code is only one line.)

ryj0902 avatar Feb 28 '22 11:02 ryj0902

thanks dude 👍

thatITfox avatar Feb 28 '22 12:02 thatITfox

hi, can I use this tool to convert Yolo9000 weights to .pt usable with Yolo 5 or 7 ?

Thanks

JourneyToSilius avatar Oct 30 '22 14:10 JourneyToSilius

This is not usable for YOLO 8:

    args = {**DEFAULT_CFG_DICT, **ckpt['train_args']}  # combine model and default args, preferring model args
KeyError: 'train_args'

MrMaynard avatar Apr 03 '23 09:04 MrMaynard

Hi @ryj0902 , I am getting error while running above mentioned code.

from context_locals import app, local
ModuleNotFoundError: No module named 'context_locals'

code is as follows:

import torch

from tool.darknet2pytorch import Darknet
WEIGHTS = Darknet('xyz.cfg',inference=True)
WEIGHTS.load_weights('xyz.weights')

looks like tool package is giving error. Also tool is installed in my conda environment. tool 0.8.0

mathur01 avatar Feb 15 '24 03:02 mathur01

@mathur01 I think the issue seems to be the same cause as mentioned above.

Code should run on the top-level folder of this repository. So the module imported here should be /pytorch-YOLOv4/tool, not /usr/local/lib/python3.7/dist-packages/tool.

The tool package mentioned in the code refers to Tianxiaomo/pytorch-YOLOv4/tool, not the tool 0.8.0 provided by pypi(as you mentioned).

(Though I don't know exactly,) based on the priority of importing libraries in Python, if you import a package with the same name, the folder in the directory where you run the code is referenced first.

So, don't run that code in arbitrary path, but try to run it exactly under the Tianxiaomo/pytorch-YOLOv4 directory.

ryj0902 avatar Feb 15 '24 12:02 ryj0902