Change input shape for a subclassed Keras model loaded from SavedModel format

Viewed 83

is it possible to change the input shape of a tf.keras.Model that has been loaded from SavedModel format?

import tensorflow as tf


class DummyModel(tf.keras.Model):
    def __init__(self):
        super().__init__()
        self.conv = tf.keras.layers.Conv2D(filters=1, kernel_size=3)

    def call(self, inputs, training=None, mask=None):
        return self.conv(inputs)


if __name__ == "__main__":

    model = DummyModel()
    big_image = tf.random.uniform((1, 9, 9, 1))
    small_image = tf.random.uniform((1, 7, 7, 1))

    model(big_image)  # Works
    model(small_image)  # Works

    model.save("saved_model/my_model")
    del model

    model = tf.keras.models.load_model("saved_model/my_model")
    model(big_image)  # Works
    model(small_image)  # Does not work

I also tried this:

model = tf.saved_model.load('saved_model/my_model')
concrete_func = model.signatures["serving_default"]
concrete_func.inputs[0].set_shape(small_image.shape)

but it won't work either because of ValueError: Dimension 1 in both shapes must be equal, but are 9 and 7. Shapes are [?,9,9,1] and [1,7,7,1].

0 Answers
Related