experiencor / keras-yolo2

Easy training on custom dataset. Various backends (MobileNet and SqueezeNet) supported. A YOLO demo to detect raccoon run entirely in brower is accessible at https://git.io/vF7vI (not on Windows).
MIT License
1.73k stars 785 forks source link

ValueError: axes don't match array #358

Closed visshvesh closed 5 years ago

visshvesh commented 5 years ago

After training model with inception weights when I try to run predict.py, I am getting the following error. What am I doing wrong or missing?

Traceback (most recent call last): File "predict.py", line 96, in main(args) File "predict.py", line 56, in main yolo.load_weights(weights_path) File "C:\Users\A638054\PycharmProjects\YOLO\YOLO_TRY1\keras-yolo2-master\front end.py", line 243, in load_weights self.model.load_weights(weight_path) File "C:\Users\A638054\AppData\Local\Programs\Python\Python36\lib\site-package s\keras\engine\network.py", line 1180, in load_weights f, self.layers, reshape=reshape) File "C:\Users\A638054\AppData\Local\Programs\Python\Python36\lib\site-package s\keras\engine\saving.py", line 916, in load_weights_from_hdf5_group reshape=reshape) File "C:\Users\A638054\AppData\Local\Programs\Python\Python36\lib\site-package s\keras\engine\saving.py", line 557, in preprocess_weights_for_loading weights = convert_nested_model(weights) File "C:\Users\A638054\AppData\Local\Programs\Python\Python36\lib\site-package s\keras\engine\saving.py", line 545, in convert_nested_model original_backend=original_backend)) File "C:\Users\A638054\AppData\Local\Programs\Python\Python36\lib\site-package s\keras\engine\saving.py", line 557, in preprocess_weights_for_loading weights = convert_nested_model(weights) File "C:\Users\A638054\AppData\Local\Programs\Python\Python36\lib\site-package s\keras\engine\saving.py", line 533, in convert_nested_model original_backend=original_backend)) File "C:\Users\A638054\AppData\Local\Programs\Python\Python36\lib\site-package s\keras\engine\saving.py", line 675, in preprocess_weights_for_loading weights[0] = np.transpose(weights[0], (3, 2, 0, 1)) File "C:\Users\A638054\AppData\Local\Programs\Python\Python36\lib\site-package s\numpy\core\fromnumeric.py", line 575, in transpose return _wrapfunc(a, 'transpose', axes) File "C:\Users\A638054\AppData\Local\Programs\Python\Python36\lib\site-package s\numpy\core\fromnumeric.py", line 52, in _wrapfunc return getattr(obj, method)(*args, **kwds) ValueError: axes don't match array

letilessa commented 5 years ago

Try changing the versions of keras and tensorflow. Keras=2.1.5 and Tensorflow=1.8.0 worked for me.

visshvesh commented 5 years ago

Thanks a lot!! @letilessa . Worked just fine after changing versions.

SteveIb commented 5 years ago

I tried your solution @letilessa , it didnt work. I already trained on Full Yolo, it worked smoothly, when I train with MobileNet backend it gives the previous error above. Since it worked with Full yolo but not with MobileNet backend I dont think the problem is related with Tensorflow or keras.

jamesvrt commented 5 years ago

I had the same problem when using the inception backend. The problem arises because there is a model within the YOLO model. I couldn't roll back my version of Keras (I can't alter my environment where I work) but I fixed the problem by defining the entire model (inception body and YOLO head) in a function.

dlsaavedra commented 5 years ago

For join the models in one, only you need change the line 60 in frontend.py (features = self.feature_extractor.extract(input_image) to
features = self.feature_extractor.feature_extractor.output) and the line 71 (self.model = Model([input_image, self.true_boxes], output) to self.model = Model([self.feature_extractor.feature_extractor.input , self.true_boxes], output))

vade commented 5 years ago

Hi. Apologies for bouncing across threads, im looking to understand how to successfully load weights using this model (trained in TF 1.14 using TF.Keras, I need to load it into Keras properly). Im aware this isn't a pure Keras project, but ive asked there as well. Considering folks appear to solve it, I hope you dont mind the question:

IMG_SIZE = 224

base_model = keras.applications.nasnet.NASNetMobile(input_shape=(IMG_SIZE, IMG_SIZE, 3), include_top=False, weights=None)
base_model.trainable = False
num_labels = 229
output = keras.layers.Dense(num_labels, activation = 'sigmoid', name="cinemanet_output")
model = keras.models.Sequential( [base_model, keras.layers.GlobalAveragePooling2D(), output] )
model.summary()

#load weights from tf 1.14 / tf.keras version of the model:
model.load_weights("/tmp/" + weights_name)

results in:

     54 def _wrapfunc(obj, method, *args, **kwds):
     55     try:
---> 56         return getattr(obj, method)(*args, **kwds)
     57 
     58     # An AttributeError occurs if the object does not have

ValueError: axes don't match array

Im running on Google Collab, and have also attempted @letilessa fix of rolling back Tensorflow and Keras to the versions mentioned, however the resulting model has fewer layers I believe due to lack of some layer types NASNet uses, and thus weight loading fails.

Any pointers? Thank you in advance.

chaudharyachint08 commented 5 years ago

Recursive saving & loading worked for me, so i never attempted to save weights of entire abstract large model at once, save & load individual components