WongKinYiu / PyTorch_YOLOv4

PyTorch implementation of YOLOv4
1.88k stars 587 forks source link

why i cannot test the pretrained weights? #320

Closed Zigars closed 3 years ago

Zigars commented 3 years ago

No matter yolov4.weights or converted weights yolov4.pt, both weights I can not test in this test.py, my torch version is 1.7.1, this is my Error message: Traceback (most recent call last): File "D:/VSCodeProject/PyTorch_YOLOv4-master/test.py", line 288, in test(opt.data, File "D:/VSCodeProject/PyTorchYOLOv4-master/test.py", line 92, in test = model(img.half() if half else img) if device.type != 'cpu' else None # run once File "D:\Users\ZHT\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl result = self.forward(*input, kwargs) File "D:\VSCodeProject\PyTorch_YOLOv4-master\models\models.py", line 293, in forward return self.forward_once(x) File "D:\VSCodeProject\PyTorch_YOLOv4-master\models\models.py", line 347, in forward_once x = module(x) File "D:\Users\ZHT\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl result = self.forward(*input, *kwargs) File "D:\Users\ZHT\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\container.py", line 117, in forward input = module(input) File "D:\Users\ZHT\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl result = self.forward(input, kwargs) File "D:\Users\ZHT\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 175, in _forward_unimplemented raise NotImplementedError NotImplementedError

also , i can not convert yolov4-tiny.weights to pt, because the python darknet didn't have the 'groups-id' and 'resize' module,

rak7045 commented 2 years ago

Hello @Zigars I have trained my custom dataset using Tiny-YOLOv4 with pretrained weights. I have now the weight_file.pt. I would like to convert this to .weights file. How can I do that? I have seen a piece of code in models.py line 647. `def convert(cfg='cfg/yolov3-spp.cfg', weights='weights/yolov3-spp.weights', saveto='converted.weights'):

Converts between PyTorch and Darknet format per extension (i.e. .weights convert to .pt and vice versa)

# from models import *; convert('cfg/yolov3-spp.cfg', 'weights/yolov3-spp.weights')`

# Initialize model
model = Darknet(cfg)
ckpt = torch.load(weights)  # load checkpoint
try:
    ckpt['model'] = {k: v for k, v in ckpt['model'].items() if model.state_dict()[k].numel() == v.numel()}
    model.load_state_dict(ckpt['model'], strict=False)
    save_weights(model, path=saveto, cutoff=-1)
except KeyError as e:
    print(e)` 

where I have given my cfg file and weights file.

My question what line of code I have to run so that I get my best.pt or last.pt converted to best.weights or last.weights file? And also do I need to uncomment # from models import *; convert('cfg/yolov3-spp.cfg', 'weights/yolov3-spp.weights') in the function?

I am trying to detect or test but a Runtime error is raising. I think this is because of the weights file. Traceback (most recent call last): File "test.py", line 330, in <module> save_conf=opt.save_conf, File "test.py", line 71, in test load_darknet_weights(model, weights[0]) File "/content/drive/MyDrive/PyTorch_YOLOv4-wong/PyTorch_YOLOv4-master/models/models.py", line 617, in load_darknet_weights conv.weight.data.copy_(torch.from_numpy(weights[ptr:ptr + nw]).view_as(conv.weight)) RuntimeError: shape '[512, 256, 3, 3]' is invalid for input of size [1017227]

Could you please help me with this?

TIA

qutyyds commented 2 years ago

遇到了同样的问题,请问解决了吗?