Keras-yolo3: Mismatch between model and given anchor and class sizes (tiny yolov3)

Created on 14 Jun 2019  路  3Comments  路  Source: qqwweee/keras-yolo3

SOLUTION:
my weights and anchors files weren't getting loaded correctly. Will open a new issue with new problem.

Trying to convert a tiny yolov3 .cfg and .weights into a Keras .h5 model.

tiny.cfg

[net]
# Testing
batch=1
subdivisions=1
# Training
# batch=64
# subdivisions=8
width=416
height=416
channels=3
momentum=0.9
decay=0.0005
angle=0
saturation = 1.5
exposure = 1.5
hue=.1

learning_rate=0.001
burn_in=1000
max_batches = 40000
policy=steps
steps=32000,36000
scales=.1,.1

[convolutional]
batch_normalize=1
filters=16
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=32
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=64
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=1

[convolutional]
batch_normalize=1
filters=1024
size=3
stride=1
pad=1
activation=leaky

###########

[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=leaky

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



[yolo]
mask = 3,4,5
anchors = 54,75, 100,130, 243,87, 181,182, 332,148, 323,300
classes=20
num=6
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1

[route]
layers = -4

[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=leaky

[upsample]
stride=2

[route]
layers = -1, 8

[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=leaky

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

[yolo]
mask = 0,1,2
anchors = 54,75, 100,130, 243,87, 181,182, 332,148, 323,300
classes=20
num=6
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1

Accordingly, here is my tiny_custom_anchors.txt file:

54,75, 100,130, 243,87, 181,182, 332,148, 323,300

Output from python convert.py be1ca778.cfg be1ca778.weights be1ca778.h5 --plot_model:

Using TensorFlow backend.
Loading weights.
Weights Header:  0 2 5 [2358784]
Parsing Darknet config.
Creating Keras model.
Parsing section net_0
Parsing section convolutional_0
conv2d bn leaky (3, 3, 3, 16)
WARNING:tensorflow:From /Users/ns242e/keras-yolo3/venv/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2019-06-14 12:32:27.547474: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Parsing section maxpool_0
Parsing section convolutional_1
conv2d bn leaky (3, 3, 16, 32)
Parsing section maxpool_1
Parsing section convolutional_2
conv2d bn leaky (3, 3, 32, 64)
Parsing section maxpool_2
Parsing section convolutional_3
conv2d bn leaky (3, 3, 64, 128)
Parsing section maxpool_3
Parsing section convolutional_4
conv2d bn leaky (3, 3, 128, 256)
Parsing section maxpool_4
Parsing section convolutional_5
conv2d bn leaky (3, 3, 256, 512)
Parsing section maxpool_5
Parsing section convolutional_6
conv2d bn leaky (3, 3, 512, 1024)
Parsing section convolutional_7
conv2d bn leaky (1, 1, 1024, 256)
Parsing section convolutional_8
conv2d bn leaky (3, 3, 256, 512)
Parsing section convolutional_9
conv2d    linear (1, 1, 512, 75)
Parsing section yolo_0
Parsing section route_0
Parsing section convolutional_10
conv2d bn leaky (1, 1, 256, 128)
Parsing section upsample_0
Parsing section route_1
Concatenating route layers: [<tf.Tensor 'up_sampling2d_1/ResizeNearestNeighbor:0' shape=(?, 26, 26, 128) dtype=float32>, <tf.Tensor 'leaky_re_lu_5/LeakyRelu:0' shape=(?, 26, 26, 256) dtype=float32>]
Parsing section convolutional_11
conv2d bn leaky (3, 3, 384, 256)
Parsing section convolutional_12
conv2d    linear (1, 1, 256, 75)
Parsing section yolo_1
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 416, 416, 3)  0                                            
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, 416, 416, 16) 432         input_1[0][0]                    
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, 416, 416, 16) 64          conv2d_1[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_1 (LeakyReLU)       (None, 416, 416, 16) 0           batch_normalization_1[0][0]      
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D)  (None, 208, 208, 16) 0           leaky_re_lu_1[0][0]              
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, 208, 208, 32) 4608        max_pooling2d_1[0][0]            
__________________________________________________________________________________________________
batch_normalization_2 (BatchNor (None, 208, 208, 32) 128         conv2d_2[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_2 (LeakyReLU)       (None, 208, 208, 32) 0           batch_normalization_2[0][0]      
__________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D)  (None, 104, 104, 32) 0           leaky_re_lu_2[0][0]              
__________________________________________________________________________________________________
conv2d_3 (Conv2D)               (None, 104, 104, 64) 18432       max_pooling2d_2[0][0]            
__________________________________________________________________________________________________
batch_normalization_3 (BatchNor (None, 104, 104, 64) 256         conv2d_3[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_3 (LeakyReLU)       (None, 104, 104, 64) 0           batch_normalization_3[0][0]      
__________________________________________________________________________________________________
max_pooling2d_3 (MaxPooling2D)  (None, 52, 52, 64)   0           leaky_re_lu_3[0][0]              
__________________________________________________________________________________________________
conv2d_4 (Conv2D)               (None, 52, 52, 128)  73728       max_pooling2d_3[0][0]            
__________________________________________________________________________________________________
batch_normalization_4 (BatchNor (None, 52, 52, 128)  512         conv2d_4[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_4 (LeakyReLU)       (None, 52, 52, 128)  0           batch_normalization_4[0][0]      
__________________________________________________________________________________________________
max_pooling2d_4 (MaxPooling2D)  (None, 26, 26, 128)  0           leaky_re_lu_4[0][0]              
__________________________________________________________________________________________________
conv2d_5 (Conv2D)               (None, 26, 26, 256)  294912      max_pooling2d_4[0][0]            
__________________________________________________________________________________________________
batch_normalization_5 (BatchNor (None, 26, 26, 256)  1024        conv2d_5[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_5 (LeakyReLU)       (None, 26, 26, 256)  0           batch_normalization_5[0][0]      
__________________________________________________________________________________________________
max_pooling2d_5 (MaxPooling2D)  (None, 13, 13, 256)  0           leaky_re_lu_5[0][0]              
__________________________________________________________________________________________________
conv2d_6 (Conv2D)               (None, 13, 13, 512)  1179648     max_pooling2d_5[0][0]            
__________________________________________________________________________________________________
batch_normalization_6 (BatchNor (None, 13, 13, 512)  2048        conv2d_6[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_6 (LeakyReLU)       (None, 13, 13, 512)  0           batch_normalization_6[0][0]      
__________________________________________________________________________________________________
max_pooling2d_6 (MaxPooling2D)  (None, 13, 13, 512)  0           leaky_re_lu_6[0][0]              
__________________________________________________________________________________________________
conv2d_7 (Conv2D)               (None, 13, 13, 1024) 4718592     max_pooling2d_6[0][0]            
__________________________________________________________________________________________________
batch_normalization_7 (BatchNor (None, 13, 13, 1024) 4096        conv2d_7[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_7 (LeakyReLU)       (None, 13, 13, 1024) 0           batch_normalization_7[0][0]      
__________________________________________________________________________________________________
conv2d_8 (Conv2D)               (None, 13, 13, 256)  262144      leaky_re_lu_7[0][0]              
__________________________________________________________________________________________________
batch_normalization_8 (BatchNor (None, 13, 13, 256)  1024        conv2d_8[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_8 (LeakyReLU)       (None, 13, 13, 256)  0           batch_normalization_8[0][0]      
__________________________________________________________________________________________________
conv2d_11 (Conv2D)              (None, 13, 13, 128)  32768       leaky_re_lu_8[0][0]              
__________________________________________________________________________________________________
batch_normalization_10 (BatchNo (None, 13, 13, 128)  512         conv2d_11[0][0]                  
__________________________________________________________________________________________________
leaky_re_lu_10 (LeakyReLU)      (None, 13, 13, 128)  0           batch_normalization_10[0][0]     
__________________________________________________________________________________________________
up_sampling2d_1 (UpSampling2D)  (None, 26, 26, 128)  0           leaky_re_lu_10[0][0]             
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 26, 26, 384)  0           up_sampling2d_1[0][0]            
                                                                 leaky_re_lu_5[0][0]              
__________________________________________________________________________________________________
conv2d_9 (Conv2D)               (None, 13, 13, 512)  1179648     leaky_re_lu_8[0][0]              
__________________________________________________________________________________________________
conv2d_12 (Conv2D)              (None, 26, 26, 256)  884736      concatenate_1[0][0]              
__________________________________________________________________________________________________
batch_normalization_9 (BatchNor (None, 13, 13, 512)  2048        conv2d_9[0][0]                   
__________________________________________________________________________________________________
batch_normalization_11 (BatchNo (None, 26, 26, 256)  1024        conv2d_12[0][0]                  
__________________________________________________________________________________________________
leaky_re_lu_9 (LeakyReLU)       (None, 13, 13, 512)  0           batch_normalization_9[0][0]      
__________________________________________________________________________________________________
leaky_re_lu_11 (LeakyReLU)      (None, 26, 26, 256)  0           batch_normalization_11[0][0]     
__________________________________________________________________________________________________
conv2d_10 (Conv2D)              (None, 13, 13, 75)   38475       leaky_re_lu_9[0][0]              
__________________________________________________________________________________________________
conv2d_13 (Conv2D)              (None, 26, 26, 75)   19275       leaky_re_lu_11[0][0]             
==================================================================================================
Total params: 8,720,134
Trainable params: 8,713,766
Non-trainable params: 6,368
__________________________________________________________________________________________________
None
Saved Keras model to be1ca778.h5
Read 8720134 of 8720134.0 from Darknet weights.
Saved model plot to be1ca778.png

classes.names:

area_rugs
bar_stools
bedding
beds
coffee_and_cocktail_tables
desks
dining_chairs
dining_table_sets
dining_tables
dressers_and_chests
end_tables
kids_beds
nightstands
outdoor_conversation_sets
recliners
sectionals
sofas
tv_stands_and_entertainment_centers
vanities

command:

python yolo_video.py --model be1ca778.h5 --anchors tiny_custom_anchors.txt --classes classes.names --gpu_num 0 --image --input /Users/ns242e/Desktop/6d6ba64bc59b1cf78f520e5d0e326d535984921d.jpg
Using TensorFlow backend.
Image detection mode
 Ignoring remaining command line arguments: /Users/ns242e/Desktop/6d6ba64bc59b1cf78f520e5d0e326d535984921d.jpg,
2019-06-14 12:29:24.014972: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
WARNING:tensorflow:From /Users/ns242e/keras-yolo3/venv/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
Traceback (most recent call last):
  File "yolo_video.py", line 73, in <module>
    detect_img(YOLO(**vars(FLAGS)))
  File "/Users/ns242e/keras-yolo3/yolo.py", line 45, in __init__
    self.boxes, self.scores, self.classes = self.generate()
  File "/Users/ns242e/keras-yolo3/yolo.py", line 80, in generate
    'Mismatch between model and given anchor and class sizes'
AssertionError: Mismatch between model and given anchor and class sizes

Most helpful comment

I am also getting the same issue. Is it resolved ?

All 3 comments

I am also getting the same issue. Is it resolved ?

Anyone got around this? Having the same issue

Not sure, that it's connected to your issue, but I found some problem in YOLO class constructor when I tyed to use tiny model
self.__dict__.update(kwargs) # and update with user overrides
seems wrong, becouse the keys in kwargs and in class attrs are different.
self.__dict__.update({'model_path':kwargs['model'], 'anchors_path':kwargs['anchors']}) # and update with user overrides`
works for me.

Was this page helpful?
0 / 5 - 0 ratings