简体   繁体   中英

InvalidArgumentError: required broadcastable shapes [Op:Mul]

I am noob in python and tensorflow. And I met a problem when training tensorflow lite model in colab. The model was good before exporting model. However, when I test the tflite file on my images which using following code, I got an error.

model.evaluate_tflite('/content/label-img/model.tflite', validation_data)

error

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-22-6548d36b036c> in <module>()
----> 1 model.evaluate_tflite('/content/label-img/model.tflite', validation_data)

8 frames
/usr/local/lib/python3.7/dist-packages/tensorflow_examples/lite/model_maker/core/task/object_detector.py in evaluate_tflite(self, tflite_filepath, data)
    187     ds = data.gen_dataset(self.model_spec, batch_size=1, is_training=False)
    188     return self.model_spec.evaluate_tflite(tflite_filepath, ds, len(data),
--> 189                                            data.annotations_json_file)
    190 
    191   def _export_saved_model(self, saved_model_dir: str) -> None:

/usr/local/lib/python3.7/dist-packages/tensorflow_examples/lite/model_maker/core/task/model_spec/object_detector_spec.py in evaluate_tflite(self, tflite_filepath, dataset, steps, json_file)
    386       normalize_factor = tf.constant([height, width, height, width],
    387                                      dtype=tf.float32)
--> 388       nms_boxes *= normalize_factor
    389       if labels['image_scales'] is not None:
    390         scales = tf.expand_dims(tf.expand_dims(labels['image_scales'], -1), -1)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py in r_binary_op_wrapper(y, x)
   1398       #   r_binary_op_wrapper use different force_same_dtype values.
   1399       y, x = maybe_promote_tensors(y, x)
-> 1400       return func(x, y, name=name)
   1401 
   1402   # Propagate func.__doc__ to the wrappers

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py in _mul_dispatch(x, y, name)
   1708     return sparse_tensor.SparseTensor(y.indices, new_vals, y.dense_shape)
   1709   else:
-> 1710     return multiply(x, y, name=name)
   1711 
   1712 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    204     """Call target, and fall back on dispatchers if there is a TypeError."""
    205     try:
--> 206       return target(*args, **kwargs)
    207     except (TypeError, ValueError):
    208       # Note: convert_to_eager_tensor currently raises a ValueError, not a

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py in multiply(x, y, name)
    528   """
    529 
--> 530   return gen_math_ops.mul(x, y, name)
    531 
    532 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/gen_math_ops.py in mul(x, y, name)
   6234       return _result
   6235     except _core._NotOkStatusException as e:
-> 6236       _ops.raise_from_not_ok_status(e, name)
   6237     except _core._FallbackException:
   6238       pass

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
   6939   message = e.message + (" name: " + name if name is not None else "")
   6940   # pylint: disable=protected-access
-> 6941   six.raise_from(core._status_to_exception(e.code, message), None)
   6942   # pylint: enable=protected-access
   6943 

/usr/local/lib/python3.7/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: required broadcastable shapes [Op:Mul]

This error should be due to the shapes difference. But can anyone tell me how to do broadcastable in colab? The code and colab are from https://colab.research.google.com/github/googlecodelabs/odml-pathways/blob/main/object-detection/codelab2/python/Train_a_salad_detector_with_TFLite_Model_Maker.ipynb#scrollTo=HD5BvzWe6YKa . The tensorflow official tutorial.

Currently, the above colab has an issue with TensorFlow 2.6 or above. Please stick with TensorFlow 2.5 for the time being.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM