Tianxiaomo / pytorch-YOLOv4

PyTorch ,ONNX and TensorRT implementation of YOLOv4
Apache License 2.0
4.47k stars 1.49k forks source link

how to convert darknet .weight to pytorch .pt file #281

Open songzhifei opened 4 years ago

songzhifei commented 4 years ago

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'.

Linchunhui commented 3 years ago

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

ajsanjoaquin commented 3 years ago

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.

ryj0902 commented 3 years ago

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'))
ajsanjoaquin commented 3 years ago

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.

vishnuvardhan58 commented 3 years ago

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
ajsanjoaquin commented 3 years ago

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.

vishnuvardhan58 commented 3 years ago

Hello @ajsanjoaquin , thankyou for your reply.

laoshaw commented 3 years ago

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

bobbilichandu commented 3 years ago

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

laoshaw commented 3 years ago

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

gauravgund commented 3 years ago

@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?
ryj0902 commented 3 years ago

@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.

gauravgund commented 3 years ago

@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'

ryj0902 commented 3 years ago

@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]?

gauravgund commented 3 years ago

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 .

ryj0902 commented 3 years ago

@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.

thatITfox commented 2 years ago

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?

ryj0902 commented 2 years ago

@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.)

thatITfox commented 2 years ago

thanks dude 👍

journeytosilius commented 1 year ago

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

Thanks

MrMaynard commented 1 year ago

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'
mathur01 commented 7 months ago

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

ryj0902 commented 7 months ago

@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.