ScaledYOLOv4 icon indicating copy to clipboard operation
ScaledYOLOv4 copied to clipboard

how to convert the pt to weights??

Open SpongeBab opened this issue 3 years ago • 26 comments

SpongeBab avatar Mar 22 '21 03:03 SpongeBab

Were you able to do it? There was a function in models.py in the models directory called "convert". Use that function to convert pt to weights. I wrote a simple python script to convert pt to weights using that function. ` import argparse import os import platform import shutil import time from pathlib import Path

import cv2 import torch import torch.backends.cudnn as cudnn from numpy import random

from models.experimental import attempt_load from utils.datasets import LoadStreams, LoadImages from utils.general import ( check_img_size, non_max_suppression, apply_classifier, scale_coords, xyxy2xywh, plot_one_box, strip_optimizer) from utils.torch_utils import select_device, load_classifier, time_synchronized

from models.models import * from models.experimental import * from utils.datasets import * from utils.general import *

def convert_weights(): convert(cfg=opt.cfg, weights=opt.weights, saveto=opt.output) print(opt.cfg) print(opt.weights) print(opt.output)

if name=="main": parser = argparse.ArgumentParser() parser.add_argument('--weights', type=str, default='yolov4.pt') parser.add_argument('--cfg', type=str, default='models/yolov4-csp.cfg', help='config file') parser.add_argument('--output', type=str, default='yolov4.weights', help='output file path') opt = parser.parse_args() #print(opt) with torch.no_grad(): #else: convert_weights() ` I tried doing that, unfortunately, NMS is not working properly. @xiaoxiaopeng1998, Please share if you are successful in accomplishing this.

bobbilichandu avatar Mar 31 '21 07:03 bobbilichandu

你好,朋友!非常乐于与你交流,我最近打算修改这个代码作为我的毕业论文。 Hi,guy! I am very happy to communicate with you. I recently plan to modify this code as my graduation thesis.

I have successed convert the .pt to weights. 一开始没仔细看,后来我在代码中也找到这个函数了。他已经写好了这个convert函数。 I didn't look closely at the beginning, but later I also found this function in the code. He has already written the convert function.

def convert(cfg='../models/yolov4-csp.cfg', weights='../runs/exp26_yolov4-csp-/weights/best_yolov4-csp-.pt', saveto='yolov4-csp-desk-800-100.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')

因为是python,所以我懒得写main函数了。在最后一行直接写: Because it is python, I am too lazy to write the main function. Write directly on the last line: convert() 另外我想说点别的,虽然精度确实高。 我用Alexey的darknet训练完,使用map命令测试,虽然ap只有3%。这个训练完ap有40%。但是检测效果比3%差多了。他会在同一个位置生成多个重复检测框。我怀疑他用了这种手段去提高他的ap。 In addition, I want to say something else. Although the accuracy is indeed high. But this is meaningless.

I finished training with Alexey's darknet and tested with the map command, although the ap is only 3%. After this training with Scaled-yolo, ap has 40%. But the detection effect is much worse than 3% . It will generate multiple duplicate detection Bbox at the same location. I suspect he used this method to improve his ap. anything can @me。

SpongeBab avatar Mar 31 '21 08:03 SpongeBab

@xiaoxiaopeng1998 Then is there any other way to convert pt files to weight files? Detections are really bad as far as I observed too. bus_ person_ zidane_ I thought this was an issue with NMS. Is it not? If detections are bad, there is no way I want to convert pt to weights using this method.

bobbilichandu avatar Mar 31 '21 08:03 bobbilichandu

@xiaoxiaopeng1998

image

The decode parts are different in yolov3, yolov4 and scaled-yolov4, if you want to use darknet for inference, cfg file needs some modification. The reason why you get duplicate bounding boxes at same location is due to decoder is mismatch between your training and inference, so that they can not recognized as same object by NMS. Using wrong way to do the things will always get wrong results.

WongKinYiu avatar Mar 31 '21 09:03 WongKinYiu

Oh! 你的问题和我一模一样!这是你用转换后的weights测试的结果吗? Your question is exactly the same as mine!Is this the result of your test with the converted weights? you can see this https://github.com/WongKinYiu/ScaledYOLOv4/issues/205 原谅我学的不好,我不能确定是不是NMS的问题。我觉得与NMS没有关系。我正在拿darknet训练yolo-csp。我想看看是否有不一样的结果。 Forgive me for not learning well, I am not sure if it is the NMS problem. I don't think it has anything to do with NMS. I am training yolo-csp with darknet. I want to see if there are any different results.

SpongeBab avatar Mar 31 '21 09:03 SpongeBab

cfg in pytorch version:

[convolutional]
size=1
stride=1
pad=1
filters=255
activation=linear


[yolo]
mask = 6,7,8
anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401
classes=80
num=9
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1
scale_x_y = 1.05
iou_thresh=0.213
cls_normalizer=1.0
iou_normalizer=0.07
iou_loss=ciou
nms_kind=greedynms
beta_nms=0.6

cfg in darknet version:

[convolutional]
size=1
stride=1
pad=1
filters=255
activation=logistic


[yolo]
mask = 6,7,8
anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401
classes=80
num=9
jitter=.1
scale_x_y = 2.0
objectness_smooth=1
ignore_thresh = .7
truth_thresh = 1
#random=1
resize=1.5
iou_thresh=0.2
iou_normalizer=0.05
cls_normalizer=0.5
obj_normalizer=0.4
iou_loss=ciou
nms_kind=diounms
beta_nms=0.6
new_coords=1
max_delta=2

main difference should be modified:

  1. activation of convolutional layer before yolo layer will be logistic.
  2. add new_coords=1 in yolo layer
  3. change scale_x_y = 2.0 in yolo layer

WongKinYiu avatar Mar 31 '21 09:03 WongKinYiu

If I make these changes in cfg file and convert pt to weights, will those work for detect.py in yolov4-csp branch in this current repo?

bobbilichandu avatar Mar 31 '21 09:03 bobbilichandu

@WongKinYiu 谢谢您的解答,我还是太菜了。。 意思这个代码中的convert是只能转成在pytorch中的weights,不能在darknet中有效运行,是这个意思吗? Thank you for your answer, I'm still too good at it. . It means that the convert in this code can only be converted into weights in pytorch, and cannot run effectively in darknet. Does that mean?

我还有一个疑问。 这是我运行test的图片。 图片

第一张是正常的。第二张后面的是不正常的。 The first one is normal. The one behind the second one is abnormal.

SpongeBab avatar Mar 31 '21 09:03 SpongeBab

@chandu1263

I think yes, but I do not suggest to do that, it may make your code/cfg confused. logistic activation of pytorch code is in the decoder, while of darknet code is in the architecture. you can compare them https://github.com/AlexeyAB/darknet/blob/master/cfg/yolov4-csp.cfg https://github.com/WongKinYiu/ScaledYOLOv4/blob/yolov4-csp/models/yolov4-csp.cfg

WongKinYiu avatar Mar 31 '21 09:03 WongKinYiu

Then if I want to convert the yolov4-csp model to onnx/trt, how do I do it(with the current cfg and pt files)?

bobbilichandu avatar Mar 31 '21 09:03 bobbilichandu

@WongKinYiu Ok this is what happened. I converted the pt file to weights file using the convert function and tried detect.py using the weights and cfg file in this repo, I still got duplicate bounding boxes. So if I want to have to use the .weights file I have to connvert the cfg to darknet format, is that correct?

bobbilichandu avatar Apr 01 '21 12:04 bobbilichandu

cfg in pytorch version:

[convolutional]
size=1
stride=1
pad=1
filters=255
activation=linear


[yolo]
mask = 6,7,8
anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401
classes=80
num=9
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1
scale_x_y = 1.05
iou_thresh=0.213
cls_normalizer=1.0
iou_normalizer=0.07
iou_loss=ciou
nms_kind=greedynms
beta_nms=0.6

cfg in darknet version:

[convolutional]
size=1
stride=1
pad=1
filters=255
activation=logistic


[yolo]
mask = 6,7,8
anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401
classes=80
num=9
jitter=.1
scale_x_y = 2.0
objectness_smooth=1
ignore_thresh = .7
truth_thresh = 1
#random=1
resize=1.5
iou_thresh=0.2
iou_normalizer=0.05
cls_normalizer=0.5
obj_normalizer=0.4
iou_loss=ciou
nms_kind=diounms
beta_nms=0.6
new_coords=1
max_delta=2

main difference should be modified:

  1. activation of convolutional layer before yolo layer will be logistic.
  2. add new_coords=1 in yolo layer
  3. change scale_x_y = 2.0 in yolo layer

When I am trying to add new_coords=1 in yolo layer I am facing this issue " assert not any(u), "Unsupported fields %s in %s. See https://github.com/ultralytics/yolov3/issues/631" % (u, path) AssertionError: Unsupported fields ['new_coords'] in models/yolov4-csp.cfg."

bobbilichandu avatar Apr 02 '21 07:04 bobbilichandu

@WongKinYiu Ok this is what happened. I converted the pt file to weights file using the convert function and tried detect.py using the weights and cfg file in this repo, I still got duplicate bounding boxes. So if I want to have to use the .weights file I have to connvert the cfg to darknet format, is that correct?

hi,I had a cold yesterday... I haven’t meet this problem.I converted the pt file to weights file, and then run it in detect.py. It can detect correctly, no duplication. And It has better performance than darknet.

SpongeBab avatar Apr 02 '21 09:04 SpongeBab

You tried yolov4-csp only right? Can you share the cfg if so. Thanks!

bobbilichandu avatar Apr 02 '21 09:04 bobbilichandu

I just used the yolov4-csp.cfg in the repo.I didn’t make any change.Try again.I will share the cfg later.

SpongeBab avatar Apr 02 '21 09:04 SpongeBab

@chandu1263 Here it is. https://github.com/xiaoxiaopeng1998/ScaledYOLOv4/blob/xiaoPeng/models/yolov4-csp.cfg I didn't use the command.I changed the code in the detect.py:

    opt = parser.parse_args(['--device', '0',
                             '--weights', 'models/yolov4-csp-desk-800-1000.weights',
                             '--names', 'data/voc.names',
                             '--cfg', 'models/yolov4-csp.cfg',
                             '--view-img',
                             '--source', 'inference/desk.mp4',
                             '--output', 'inference/output'])

And the result :

vlcsnap-2021-04-03-11h18m59s874

This is in darknet:

图片

There isn't duplicate detection.

SpongeBab avatar Apr 03 '21 03:04 SpongeBab

Thanks

bobbilichandu avatar Apr 03 '21 06:04 bobbilichandu

代码写的很好,我学到了很多。从Scaled yolov4到yolov4,但是我看到了太多了版本,我感觉非常苦恼。经常在pt和weights之间切换,经常在yaml和cfg之间切换,Darknet和YAML解析,太复杂了。整个代码库糅合太多东西了,心太累了。

tangning9495 avatar May 07 '21 03:05 tangning9495

现在一个代码是YAML,一个是CFG,找不到可用的pt文件。感觉好难受啊

tangning9495 avatar May 07 '21 03:05 tangning9495

darknet刚刚看懂,发现weights需要pt权重。结果给的权重是weights。又想着去看yaml的解析了

tangning9495 avatar May 07 '21 03:05 tangning9495

Then if I want to convert the yolov4-csp model to onnx/trt, how do I do it(with the current cfg and pt files)?

I think this will help you,https://github.com/ultralytics/yolov5/issues/251

SpongeBab avatar May 11 '21 06:05 SpongeBab

Hello @chandu1263 @SpongeBab I am trying to convert weight file .pt to .weights. I have changed the cfg and weights in the convert function. Later how to run the piece of code to convert the weights? Could you please help me with this?

TIA BR Ravi

rak7045 avatar Nov 19 '21 22:11 rak7045

@WongKinYiu Is this issue solved? I am using the exact cfg files for pytorch and darknet, but I get duplicate bounding boxes when I convert the pt file to weights

hadign20 avatar Apr 14 '22 16:04 hadign20

@SpongeBab I haven't managed to conver the weights either, can you give us a little help? @rak7045 any luck?

callmesora avatar May 12 '22 16:05 callmesora

@callmesora Yeah I could get that. Initially I thought of converting using convert function. Then later, when I gave weight file path into the arguments it worked well for me. I could give detail explanation tomorrow. If it worked just let me know.

Thanks

rak7045 avatar May 12 '22 16:05 rak7045

I still can't manage to get it to work. Can you try to run it with my weights and see if the problem is my file? I trained with the default yolov4-csp yaml (but I think the cfg is the same) https://drive.google.com/file/d/1xAr7TGpXpgCTPFRMrymTIALEm_1vgvTZ/view?usp=sharing

Help is really apreciated , thanks for everything so far 🥇

callmesora avatar May 12 '22 16:05 callmesora