我试图遵循使用下面的代码从教科书迁移学习的指南,并得到上面的错误信息。我认为input_shape
与IMAGE_SHAPE
不匹配,但我无法计算出正确的尺寸。
代码:
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import matplotlib.pyplot as plt
module_url = "https://tfhub.dev/google/imagenet/mobilenet_v2_100_160/feature_vector/4"
my_model = hub.KerasLayer(module_url)
classifier_url = "https://tfhub.dev/google/imagenet/mobilenet_v2_100_160/feature_vector/4"
IMAGE_SHAPE = (224,224)
classifier = tf.keras.Sequential([hub.KerasLayer(classifier_url, input_shape = IMAGE_SHAPE+(3,))])
错误信息:
graph_function = self._create_graph_function(args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py:3289 _create_graph_function
capture_by_value=self._capture_by_value),
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py:999 func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py:672 wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/saved_model/function_deserialization.py:291 restored_function_body
"nn".join(signature_descriptions)))
ValueError: Could not find matching function to call loaded from the SavedModel. Got:
Positional arguments (4 total):
* Tensor("inputs:0", shape=(None, 224, 224, 3), dtype=float32)
* False
* False
* 0.99
Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1:
Positional arguments (4 total):
* TensorSpec(shape=(None, 160, 160, 3), dtype=tf.float32, name='inputs')
* False
* False
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 2:
Positional arguments (4 total):
* TensorSpec(shape=(None, 160, 160, 3), dtype=tf.float32, name='inputs')
* False
* True
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 3:
Positional arguments (4 total):
* TensorSpec(shape=(None, 160, 160, 3), dtype=tf.float32, name='inputs')
* True
* True
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 4:
Positional arguments (4 total):
* TensorSpec(shape=(None, 160, 160, 3), dtype=tf.float32, name='inputs')
* True
* False
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
您可以在https://tfhub.dev/google/imagenet/mobilenet_v2_100_160/feature_vector/4找到您想要使用的模型的文档。在错误信息中,它说输入图像需要形状为(160, 160, 3)
:
classifier_url = "https://tfhub.dev/google/imagenet/mobilenet_v2_100_160/feature_vector/4"
IMAGE_SHAPE = (160, 160)
classifier = tf.keras.Sequential([hub.KerasLayer(classifier_url, input_shape = IMAGE_SHAPE+(3,))])