Open songzhifei opened 4 years ago
darknet2pytorch.py 里用Darknet构建模型后,load_weights,最后torch.save就好了
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.
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'))
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.
Hello , i have used the above script to convert yolov4 darknet weights to pytorch weights , I am getting the following as shown below ,
and when I am running the prediction on video using this weights I am getting the following error
Hello , i have used the above script to convert yolov4 darknet weights to pytorch weights , I am getting the following as shown below ,
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
"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.
Hello @ajsanjoaquin , thankyou for your reply.
a related question, how to convert pytorch pt file to darknet .weight file?
@laoshaw were you able to convert pt file to weight file?
looks like only for yolo3, but I actually did not test it eventually, the answer is no
@ryj0902 I am getting error while importing tool. The error is as follows:
ModuleNotFoundError Traceback (most recent call last)
@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 : 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 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]?
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 well..., I am not sure what is causing this problem. It seems good to open up to other issues and ask others for help.
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 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.)
thanks dude 👍
hi, can I use this tool to convert Yolo9000 weights to .pt usable with Yolo 5 or 7 ?
Thanks
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'
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 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.
Is the converting script compatible for all darknet models? I got a model which is not yolov4, yoloface-500k from another repo, when i tried to run the script, errors occur when reshaping.
Or is it just a mistake that the model designer made? By the way, there are some route and shortcut components in this yoloface-500k model and I think maybe that's the problem.
Is the converting script compatible for all darknet models? I got a model which is not yolov4, yoloface-500k from another repo, when i tried to run the script, errors occur when reshaping. Or is it just a mistake that the model designer made? By the way, there are some route and shortcut components in this yoloface-500k model and I think maybe that's the problem.
It seems that the buf array here is not big enough to store too much data at every single moment.
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'.