I'm sure there's a better answer than this, but if I just wanted to hack around this quickly...
If I knew the image size already, I'd try
# 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')
If I didn't know the image size, I'd consider:
Cycling through common sizes like 29x29, 32x32, 224x224, 256x256 and 299x299.
Brute force searching over all 2 ** 20 image size combinations up to 1024x1024
Calling m(None)
and parse the str in the ValueError with a regex. It prints out the TensorSpec of the input shape:
>>> 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: {}