TNTWEN / OpenVINO-YOLOV4

This is implementation of YOLOv4,YOLOv4-relu,YOLOv4-tiny,YOLOv4-tiny-3l,Scaled-YOLOv4 and INT8 Quantization in OpenVINO2021.3
MIT License
239 stars 66 forks source link

ValueError: cannot reshape array of size 251802 into shape (512,512,3,3) #19

Closed zhubinsheng closed 3 years ago

zhubinsheng commented 3 years ago

python3 convert_weights_pb.py --class_names coco_pf.names --weights_file yolov4-tiny-_final.weights --data_format NHWC --tiny Hello, when I use the above command, I give the following error report ValueError: cannot reshape array of size 251802 into shape (512,512,3,3) How to solve this problem, thank you very much

TNTWEN commented 3 years ago

你好! 可能会是以下问题,你排查一下 1:训练时的.weights 模型的输入图片大小是多少 需要与convert_weights_pb.py里的输入匹配 https://github.com/TNTWEN/OpenVINO-YOLOV4/issues/10 第三和第四点 2:确保.names文件类别数量和训练模型一致

sparkie2640 commented 3 years ago

Hello there! It may be the following problems, please check 1: What is the input image size of the .weights model during training? It needs to match the input in convert_weights_pb.py #10 The third and fourth points 2: Ensure that the number of .names file categories is consistent with the training model

I get the same error when trying to convert a custom yolov4-tiny model. Size is 416x416 and number of classes is 6, with the cfg file edited to fix the filters to be 33.

TNTWEN commented 3 years ago

@zhubinsheng @sparkie2640 Have you solved the problem? You can share your. cfg and. names files here

TNTWEN commented 3 years ago

And how you train your model is also important. Many repos don't build models according to darknet . cfg, which also causes the. weights file to conflict with the this repos. You could use https://github.com/AlexeyAB/darknet to train your model!

sparkie2640 commented 3 years ago

Names File 20201101_v4.names: 20201101_v4.names.txt

CFG File 20201101_v4.cfg: 20201101_v4-train.cfg.txt

Trained on the latest pull of AlexeyAB's fork of darknet darknet detector train 20201101_v4.data 20201101_v4-train.cfg yolov4-tiny.conv.29 -dont_show -mjpeg_port 8090

I've compared the cfg file to the stock yolov4-tiny from Alexey's repo. With the exception of burn-in, batches, steps, classes, and filters the cfg file is identical, and correct. I am able to do yolov3 no problem.

I can also provide the weights file if you'd like.

sparkie2640 commented 3 years ago

I believe I found the issue in the cfg file during training. One of the convolutional layers was set to 33 when it should be 128. Retraining now.

This has indeed corrected the error.

zhubinsheng commented 3 years ago

我使用你提供的yolov4-tiny.weights文件可以转化成功,而且可以在任何条件转化成功,比如我修改成我自己的coco.name 或者是我自己的cfg文件或者是在这里面修改成inputs = tf.placeholder(tf.float32, [None, 1920, 1080, 3], "inputs"),都是可以转化成功. 这很奇怪,我只是一个新手,但是实验让我认为修改参数不是关键的原因,根本的决定因素是训练模型使用的参数

TNTWEN commented 3 years ago

因为没有全连接层,Yolo模型是不限制输入大小的,并且支持多尺度训练,此项目中输入大小是否和训练时大小匹配只会影响你的检测效果。在使用你自己的数据集训练时,模型结构只需要也只能修改yolo层的类别数量和yolo层前的那个卷积通道。darknet中训练模型即使改错了cfg文件也能训练,但tensorflow是静态图,如果训练错误就会导致你遇到的情况。在本项目中根据names文件自动识别你的类别数,所以一定要使用正确的names文件。

TNTWEN commented 3 years ago

类别数量影响yolo层前的卷积层通道数量,通道数量决定了模型参数量,我没有尝试过在转换为pb模型时候用错误的类别数,我觉得如果你设大于80个类别,weights文件中的参数就会不够用,就会报错。如果你是把类别改少了,模型参数够用,也不会报错,只不过错误地转换了模型而已。你可以尝试一下

sparkie2640 commented 3 years ago

Yes, you are correct on all counts. The training was done improperly, with one of the convolutional layers changed from 128 to 33, so when it was converted it was expecting more data in the array which was not there. Hence the shape error.

TNTWEN commented 3 years ago

Yes, you are correct on all counts. The training was done improperly, with one of the convolutional layers changed from 128 to 33, so when it was converted it was expecting more data in the array which was not there. Hence the shape error.

Yes!For yolov3 and yolov4 (not tiny version) I also made a tool that automatically parses the .cfg file to generate the conversion code, also welcome to try https://github.com/TNTWEN/OpenVINO-YOLO-Automatic-Generation This month I will share how to optimize the full version of the YOLOv3/v4 model in OpenVINO for deployment at the edge