我确信有比这更好的答案,但如果我只是想快速解决这个问题...
如果我已经知道图像大小,我会尝试
# Arrange.
tf.saved_model.save(tf.keras.applications.MobileNet(),'/path/to/dir')
m=tf.saved_model.load('/path/to/dir')
image_size = (224,224) #
# Act
try:
m(tf.zeros((1,) + image_size + (3,))
format='channels_last'
except ValueError:
try:
m(tf.zeros((1,3) + image_size)
format='channels_first'
except ValueError:
raise ValueError('input shape is neither None,224,224,3 nor None,3,224,224')
如果我不知道图片大小,我会考虑:
-
循环使用常见尺寸,例如 29x29、32x32、224x224、256x256 和 299x299。
-
暴力搜索所有 2 ** 20 种图像尺寸组合,最大为 1024x1024
-
调用m(None) 并使用正则表达式解析ValueError 中的str。它打印出输入形状的 TensorSpec:
>>> m(tf.zeros((1,1,1,3)))
Traceback (most recent call last):
[...]
ValueError: Could not find matching function to call loaded from the SavedModel. Got:
Positional arguments (3 total):
* Tensor("inputs:0", shape=(1, 1, 1, 3), dtype=float32)
* False
* None
Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1:
Positional arguments (3 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='input_1')
* True
* None
Keyword arguments: {}
Option 2:
Positional arguments (3 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='input_1')
* False
* None
Keyword arguments: {}
Option 3:
Positional arguments (3 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
* True
* None
Keyword arguments: {}
Option 4:
Positional arguments (3 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
* False
* None
Keyword arguments: {}