我成功地在google colab
上运行TensorFlow Hub Object Detection Colab的推理
我加载的模型
model_display_name = 'CenterNet HourGlass104 Keypoints 512x512'
model_handle = 'https://tfhub.dev/tensorflow/centernet/hourglass_512x512/1'
尝试在TensorFlow Hub中使用迁移学习
IMAGE_SHAPE = (None, None)
classifier = tf.keras.Sequential([
hub.KerasLayer(model_handle, input_shape=IMAGE_SHAPE+(3,))
])
我得到这个错误
ValueError Traceback (most recent call last)
<ipython-input-23-081693dbe40a> in <module>()
4
5 object_detector = tf.keras.Sequential([
----> 6 hub.KerasLayer(object_detector_model, input_shape=Object_Detector_IMAGE_SHAPE+(3,))
7 ])
2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
690 except Exception as e: # pylint:disable=broad-except
691 if hasattr(e, 'ag_error_metadata'):
--> 692 raise e.ag_error_metadata.to_exception(e)
693 else:
694 raise
ValueError: Exception encountered when calling layer "keras_layer" (type KerasLayer).
in user code:
File "/usr/local/lib/python3.7/dist-packages/tensorflow_hub/keras_layer.py", line 229, in call *
result = f()
ValueError: Python inputs incompatible with input_signature:
inputs: (
Tensor("Placeholder:0", shape=(None, 1, None, None, 3), dtype=float32))
input_signature: (
TensorSpec(shape=(1, None, None, 3), dtype=tf.uint8, name=None)).
Call arguments received:
• inputs=tf.Tensor(shape=(None, 1, None, None, 3), dtype=float32)
• training=None
我如何改变IMAGE_SHAPE
为此,我很困惑?
需要帮助,谢谢你
有两个问题,该模型在指定input_shape
时期望输入类型为tf.uint8
,并根据源返回输出字典。由于Sequential
API不能与多个输出一起工作,您将不得不使用Functional
API。
简单的例子:
import tensorflow as tf
import tensorflow_hub as hub
model_display_name = 'CenterNet HourGlass104 Keypoints 512x512'
model_handle = 'https://tfhub.dev/tensorflow/centernet/hourglass_512x512/1'
k_layer = hub.KerasLayer(model_handle, input_shape=(None, None, 3), dtype=tf.uint8)
和Functional
API:
import tensorflow as tf
import tensorflow_hub as hub
model_display_name = 'CenterNet HourGlass104 Keypoints 512x512'
model_handle = 'https://tfhub.dev/tensorflow/centernet/hourglass_512x512/1'
k_layer = hub.KerasLayer(model_handle, input_shape=(None, None, 3))
inputs = tf.keras.layers.Input(shape=(None, None, 3), dtype=tf.uint8)
outputs = k_layer(inputs)
classifier = tf.keras.Model(inputs, outputs)
print(classifier.summary())
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) [(None, None, None, 3)] 0
keras_layer_9 (KerasLayer) {'num_detections': (1,), 0
'detection_boxes': (1,
100, 4),
'detection_classes': (1
, 100),
'detection_scores': (1,
100)}
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
None