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

Error Loading Model #145

Open
Robin2091 opened this issue Jan 6, 2020 · 8 comments
Open

Error Loading Model #145

Robin2091 opened this issue Jan 6, 2020 · 8 comments

Comments

@Robin2091
Copy link

Robin2091 commented Jan 6, 2020

I have saved the yolov3 tiny model into HDF5 format. However, when I try to load the model using keras load_model function I get this error.
Traceback (most recent call last):

File "Save_Model.py", line 40, in
new_model = keras.models.load_model('./savedmodel2/YOLO.h5')#tf.saved_model.load_model
File "C:\Users\sudes\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\saving\save.py", line 146, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "C:\Users\sudes\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py", line 168, in load_model_from_hdf5
custom_objects=custom_objects)
File "C:\Users\sudes\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\saving\model_config.py", line 55, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:\Users\sudes\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\layers\serialization.py", line 102, in deserialize
printable_module_name='layer')
File "C:\Users\sudes\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py", line 191, in deserialize_keras_object
list(custom_objects.items())))
File "C:\Users\sudes\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 906, in from_config
config, custom_objects)
File "C:\Users\sudes\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1852, in reconstruct_from_config
process_node(layer, node_data)
File "C:\Users\sudes\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1799, in process_node
output_tensors = layer(input_tensors, kwargs)
File "C:\Users\sudes\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 842, in call
outputs = call_fn(cast_inputs, *args, kwargs)
File "C:\Users\sudes\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\layers\core.py", line 795, in call
return self.function(inputs, arguments)
File "C:/Users/sudes/Documents/yolov3-tf2-master/yolov3-tf2-master/yolov3_tf2/models.py", line 252, in
boxes_0 = Lambda(lambda x: yolo_boxes(x, anchors[masks[0]], classes),
TypeError: list indices must be integers or slices, not list

How can I solve this problem? Is it something with the model itself or is something going wrong when saving in this specific format? I was able to get it to work in the TensorFlow saved model format. But I need it in an hdf5 file.

I also need to know what the shape of the input layer is to the yolov3 tiny model.

Thank you.

@zzh8829
Copy link
Owner

zzh8829 commented Jan 7, 2020

i did use hdf5 format initially and they don't work for some reason.
tensorflow/tensorflow#27769
keras-team/keras#11847

it look like the old problem was fixed and this one is a new problem with hdf5.
I don't have to much faith in fixing hdf5 because they are not native to tensorflow. Is there a reason you can't use tf format.

For your last question, you can print out model summary using yolo.summary() like convert.py.

@Robin2091
Copy link
Author

@zzh8829 Hi, thanks for your help. I was able to find the input dimensions of the model. I am trying to integrate this model into unity and I need either a frozen graph as a .pb file or an h5 file

@zzh8829
Copy link
Owner

zzh8829 commented Jan 8, 2020

I believe either .tf or .h5 are for model (training) checkpoints. For your purpose SavedModel aka .pb format is more suitable. Please check out tools/export_tfserving.py on how to export model to .pb format

@stanleyyylau
Copy link

I believe either .tf or .h5 are for model (training) checkpoints. For your purpose SavedModel aka .pb format is more suitable. Please check out tools/export_tfserving.py on how to export model to .pb format

Hi, I would like to use the model with tfjs, should I convert to .h5 format first than use tfconverter to convert to browser format? which way do you recommend?

@ptamas88
Copy link

any progress with this? trying to do the same (load the model in h5 format) to add one more input and output to the model

@edurenye
Copy link
Contributor

I don't get the error when I export the model as .pb, but I get the error when I import the model:

File "/projects/yolov3_tf2/models.py", line 250, in <lambda>
TypeError: list indices must be integers or slices, not list

@edurenye
Copy link
Contributor

We have:

anchors = np.array([(10, 14), (23, 27), (37, 58),
                              (81, 82), (135, 169),  (344, 319)],
                             np.float32) / 416
masks = np.array([[3, 4, 5], [0, 1, 2]])

And we map it like this:

anchors[masks[0]]

If instead I replace anchors[masks[0]] by its values, like:

boxes_0 = Lambda(lambda x: yolo_boxes(x,
                 np.array([(81, 82), (135, 169),  (344, 319)], np.float32) / 416, classes),
                 name='yolo_boxes_0')(output_0)
boxes_1 = Lambda(lambda x: yolo_boxes(x,
                 np.array([(10, 14), (23, 27), (37, 58)], np.float32) / 416, classes),
                 name='yolo_boxes_1')(output_1)

Then it just works but I understand this is not supposed to be like this, works for me as a workaround, but I would like to have a proper solution.

Is somewhere the np.array transformed into a list?

@edurenye
Copy link
Contributor

edurenye commented May 18, 2020

With that workaround I been able to deploy the model to my Raspberry pi, it loads the model fine, but when I try to to call boxes, scores, classes, nums = self.model(img) it throws the following error:

ValueError: not enough values to unpack (expected 4, got 2)

Looks like are 2 tensos:
First: <tf.Tensor: shape=(1, 13, 13, 3, 8), dtype=float32, numpy=array(....), dtype=float32)>
Second: <tf.Tensor: shape=(1, 26, 26, 3, 8), dtype=float32, numpy=array(....), dtype=float32)>

The 3 in there is probably because I'm using 3 custom classes.

Any idea why this happens?

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

5 participants