Reputation: 123
I use Tensorflow 2.0 and I have a model that was defined in Imperative API. In call method I use something like this:
b, h, w, c = images.shape
k_h, k_w = kernels.shape[2], kernels.shape[3]
images = tf.transpose(images, [1, 2, 0, 3]) # (h, w, b, c)
new_shape = tf.TensorShape([1, h, w, b * c])
images = tf.reshape(images, new_shape)
When I train my model with custom loop -- no problem. But I want to porting my model to SavedModel format. I use the following function:
tf.keras.experimental.export_saved_model(
model, file_path,
serving_only=True,
input_signature=[
tf.TensorSpec(shape=[None, None, None, 3], dtype=tf.float32),
]
)
And i got error:
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
Moreover, I can't do it even If I specify shape=[1, None, None, 3], because I got:
ValueError: Tried to convert 'shape' to a tensor and failed. Error: Cannot convert a partially known TensorShape to a Tensor: (1, None, None, 3)
It means that I can't do reshape at all. But I need it. How can I do it?
Upvotes: 1
Views: 682
Reputation: 2632
When running in graph mode use tf.shape. tf.Tensor.shape fails automatic shape inference when runnning in graph mode. Here is the code with necessary changes made.
image_shape = tf.shape(images)
kernel_shape = tf.shape(kernels)
b, h, w, c = image_shape[0], image_shape[1], image_shape[2], image_shape[3],
k_h, k_w = kernel_shape[2], kernel_shape[3]
images = tf.transpose(images, [1, 2, 0, 3]) # (h, w, b, c)
new_shape = tf.TensorShape([1, h, w, b * c])
images = tf.reshape(images, new_shape)
Upvotes: 1