Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: cannot reshape array of size 2048 into shape (18,1024,1,1) #147

Open
dsbyprateekg opened this issue Jul 15, 2020 · 25 comments
Open

Comments

@dsbyprateekg
Copy link

Hi,

I am trying to convert my custom YOLOv4 weights to TF using below command-
python save_model.py --weights ./data/yolo-obj_best.weights --output ./checkpoints/yolov4-704 --input_size 704 --model yolov4

But getting below error-
File "save_model.py", line 58, in <module> app.run(main) File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 299, in run _run_main(main, args) File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 250, in _run_main sys.exit(main(argv)) File "save_model.py", line 54, in main save_tf() File "save_model.py", line 49, in save_tf utils.load_weights(model, FLAGS.weights, FLAGS.model, FLAGS.tiny) File "/content/drive/My Drive/kaggle/tensorflow-yolov4-tflite/core/utils.py", line 63, in load_weights conv_weights = conv_weights.reshape(conv_shape).transpose([2, 3, 1, 0]) ValueError: cannot reshape array of size 2048 into shape (18,1024,1,1)

I have only one class in my data/classes/obj.names file-
obj.txt

Please suggest how to solve this error.

@vladimirhorvat
Copy link

Me as well, but with yolov4-tiny!

@dsbyprateekg
Copy link
Author

@vladimirhorvat I am able to successfully do the conversion but using below repository-
https://github.com/david8862/keras-YOLOv3-model-set

@vladimirhorvat
Copy link

Thank you. It's a good lead, although it doesn't work for tiny.

@tobymcclean
Copy link

Any resolutions to this. My change from the default instructions at https://github.com/AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects is to also include the changes recommended in "for training for small objects" which is part of https://github.com/AlexeyAB/darknet#how-to-improve-object-detection

@dsbyprateekg
Copy link
Author

@tobymcclean I recommend you to use following repo-
https://github.com/david8862/keras-YOLOv3-model-set

The author of above repo is more active and replies asap. With this repo, I am able to convert my custom YOLOv4 into keras model.

@AlexanderSoroka
Copy link

I've just got exactly the same issue trying to convert tiny weights file trained with custom dataset following the instructions at https://github.com/AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects and to convert weights to checkpoint I defined class list in the file data/classes/custom.names (one class per line) and pointed the code to use this classes here: https://github.com/hunglc007/tensorflow-yolov4-tflite/blob/master/core/config.py#L14
After this change convert succeeded

@cpshaheen
Copy link

I've just got exactly the same issue trying to convert tiny weights file trained with custom dataset following the instructions at https://github.com/AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects and to convert weights to checkpoint I defined class list in the file data/classes/custom.names (one class per line) and pointed the code to use this classes here: https://github.com/hunglc007/tensorflow-yolov4-tflite/blob/master/core/config.py#L14
After this change convert succeeded

I can confirm this is the solution - @hunglc007 please add command line argument support inorder to provide classnames to avoid future issues

@Farjad3253
Copy link

Getting the same issue but not still resolved by me. Any suggestions?

@victoriest
Copy link

Getting the same issue but not still resolved by me. Any suggestions?

me to #193

@hamhochoi
Copy link

I've just got exactly the same issue trying to convert tiny weights file trained with custom dataset following the instructions at https://github.com/AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects and to convert weights to checkpoint I defined class list in the file data/classes/custom.names (one class per line) and pointed the code to use this classes here: https://github.com/hunglc007/tensorflow-yolov4-tflite/blob/master/core/config.py#L14
After this change convert succeeded

Thanks, this work for me!

@albertoisorna
Copy link

I've just got exactly the same issue trying to convert tiny weights file trained with custom dataset following the instructions at https://github.com/AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects and to convert weights to checkpoint I defined class list in the file data/classes/custom.names (one class per line) and pointed the code to use this classes here: https://github.com/hunglc007/tensorflow-yolov4-tflite/blob/master/core/config.py#L14
After this change convert succeeded

Please could you detail a bit more this? Is just to change the default coco.names with your own one?

@hamhochoi
Copy link

Maybe it because of the absolute path makes things work.

@quickgrid
Copy link

I've just got exactly the same issue trying to convert tiny weights file trained with custom dataset following the instructions at https://github.com/AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects and to convert weights to checkpoint I defined class list in the file data/classes/custom.names (one class per line) and pointed the code to use this classes here: https://github.com/hunglc007/tensorflow-yolov4-tflite/blob/master/core/config.py#L14
After this change convert succeeded

Thank you. Conversion and inference worked for me after correcting this.

@zoltanszalontay
Copy link

I can confirm this is the solution - @hunglc007 please add command line argument support inorder to provide classnames to avoid future issues

Supposing you're using Colab this command modifies the file name
!sed -i "s/coco.names/custom.names/g" /content/tensorflow-yolov4-tflite/core/config.py

@wsora
Copy link

wsora commented Nov 9, 2020

Getting the same issue but not still resolved by me. Any suggestions?

When train darknet,did you edit your custom *.cfg based on yolov4-custom.cfg instead of yolov4.cfg? There are some different in these two cfg data. You can edit your custom cfg based on yolov4.cfg and train again,that is how I solve the issue.

@Ehabur
Copy link

Ehabur commented Nov 16, 2020

try to check class names defines properly

@JeremyDemers
Copy link

More updated code hosted by theAIGuysCode https://github.com/theAIGuysCode/tensorflow-yolov4-tflite. Has the instructions with images on what to change for the names when custom training.

@joeng03
Copy link

joeng03 commented May 25, 2021

I still don't understand how to solve this problem, can anyone explain it to me? Thanks

@ssimon0327tw
Copy link

I had the same question like this,I tried all the suggestion above this coment,but still not got solved....
Had checked config.py "__C.YOLO.CLASSES = "./data/classes/*.names""
and *.names is just one class per line.(My calss id only one class lol)
I also train my weights again followed Alexey's git .
but it don't work......
anyone can help me ? Thanks.

@bhaecker
Copy link

I used black and white images to train, which means that I had to change the channel number to 1 in input_layer, line 19 in save_model.py:

input_layer = tf.keras.layers.Input([FLAGS.input_size, FLAGS.input_size, 1])

@shafaattahir
Copy link

Simple solution
Add the bellow code into /content/yolov4-deepsort/core/config.py
'''
from easydict import EasyDict as edict

__C = edict()

Consumers can get config by: from config import cfg

cfg = __C

YOLO options

__C.YOLO = edict()

__C.YOLO.CLASSES = "./data/classes/coco.names"
__C.YOLO.ANCHORS = [12,16, 19,36, 40,28, 36,75, 76,55, 72,146, 142,110, 192,243, 459,401]
__C.YOLO.ANCHORS_V3 = [10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326]
__C.YOLO.ANCHORS_TINY = [23,27, 37,58, 81,82, 81,82, 135,169, 344,319]
__C.YOLO.STRIDES = [8, 16, 32]
__C.YOLO.STRIDES_TINY = [16, 32]
__C.YOLO.XYSCALE = [1.2, 1.1, 1.05]
__C.YOLO.XYSCALE_TINY = [1.05, 1.05]
__C.YOLO.ANCHOR_PER_SCALE = 3
__C.YOLO.IOU_LOSS_THRESH = 0.5

Train options

__C.TRAIN = edict()

__C.TRAIN.ANNOT_PATH = "./data/dataset/val2017.txt"
__C.TRAIN.BATCH_SIZE = 2

__C.TRAIN.INPUT_SIZE = [320, 352, 384, 416, 448, 480, 512, 544, 576, 608]

__C.TRAIN.INPUT_SIZE = 416
__C.TRAIN.DATA_AUG = True
__C.TRAIN.LR_INIT = 1e-3
__C.TRAIN.LR_END = 1e-6
__C.TRAIN.WARMUP_EPOCHS = 2
__C.TRAIN.FISRT_STAGE_EPOCHS = 20
__C.TRAIN.SECOND_STAGE_EPOCHS = 30

TEST options

__C.TEST = edict()

__C.TEST.ANNOT_PATH = "./data/dataset/val2017.txt"
__C.TEST.BATCH_SIZE = 2
__C.TEST.INPUT_SIZE = 416
__C.TEST.DATA_AUG = False
__C.TEST.DECTECTED_IMAGE_PATH = "./data/detection/"
__C.TEST.SCORE_THRESHOLD = 0.25
__C.TEST.IOU_THRESHOLD = 0.5
'''

@ssimon0327tw
Copy link

ssimon0327tw commented Mar 30, 2022 via email

@eduardomoraes
Copy link

Simple solution Add the bellow code into /content/yolov4-deepsort/core/config.py ''' from easydict import EasyDict as edict

__C = edict()

Consumers can get config by: from config import cfg

cfg = __C

YOLO options

__C.YOLO = edict()

__C.YOLO.CLASSES = "./data/classes/coco.names" __C.YOLO.ANCHORS = [12,16, 19,36, 40,28, 36,75, 76,55, 72,146, 142,110, 192,243, 459,401] __C.YOLO.ANCHORS_V3 = [10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326] __C.YOLO.ANCHORS_TINY = [23,27, 37,58, 81,82, 81,82, 135,169, 344,319] __C.YOLO.STRIDES = [8, 16, 32] __C.YOLO.STRIDES_TINY = [16, 32] __C.YOLO.XYSCALE = [1.2, 1.1, 1.05] __C.YOLO.XYSCALE_TINY = [1.05, 1.05] __C.YOLO.ANCHOR_PER_SCALE = 3 __C.YOLO.IOU_LOSS_THRESH = 0.5

Train options

__C.TRAIN = edict()

__C.TRAIN.ANNOT_PATH = "./data/dataset/val2017.txt" __C.TRAIN.BATCH_SIZE = 2

__C.TRAIN.INPUT_SIZE = [320, 352, 384, 416, 448, 480, 512, 544, 576, 608]

__C.TRAIN.INPUT_SIZE = 416 __C.TRAIN.DATA_AUG = True __C.TRAIN.LR_INIT = 1e-3 __C.TRAIN.LR_END = 1e-6 __C.TRAIN.WARMUP_EPOCHS = 2 __C.TRAIN.FISRT_STAGE_EPOCHS = 20 __C.TRAIN.SECOND_STAGE_EPOCHS = 30

TEST options

__C.TEST = edict()

__C.TEST.ANNOT_PATH = "./data/dataset/val2017.txt" __C.TEST.BATCH_SIZE = 2 __C.TEST.INPUT_SIZE = 416 __C.TEST.DATA_AUG = False __C.TEST.DECTECTED_IMAGE_PATH = "./data/detection/" __C.TEST.SCORE_THRESHOLD = 0.25 __C.TEST.IOU_THRESHOLD = 0.5 '''

Hi, thanks for the solution purposed.
Still getting an error here. After applying this, the model shows the same error with different numbers. Now I have:
cannot reshape array of size 28407 into shape (33,1024,1,1). I don't know what to do from here. Any help would be appreciated.
Thanks

@eduardomoraes
Copy link

Hi, thanks for the solution purposed.
Still getting an error here. After applying this, the model shows the same error with different numbers. Now I have:
cannot reshape array of size 28407 into shape (33,1024,1,1). I don't know what to do from here. Any help would be appreciated.
Thanks

@lamegaton
Copy link

Hi, thanks for the solution purposed. Still getting an error here. After applying this, the model shows the same error with different numbers. Now I have: cannot reshape array of size 28407 into shape (33,1024,1,1). I don't know what to do from here. Any help would be appreciated. Thanks

In tensorflow-yolov4-tflite, you can make a copy of data/classes/coco.names for later use. Then, overwrite your custom classes into coco.names

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests