How to save keras custom model with dynamic input shape in SaveModel format?

南楼画角 提交于 2021-01-29 18:11:31

问题


I have a custom model with dynamic input shape (flexible second dimension).

I need to save it in SaveModel format. But it saves only one signature (the first used).

When I try to use different signature after loading - I am getting an error:

Python inputs incompatible with input_signature

My code is as follows:

seq_len = 2
batch_size = 3
import tensorflow as tf

class CustomModule(tf.keras.Model):

  def __init__(self):
    super(CustomModule, self).__init__()
    self.v = tf.Variable(1.)

  #@tf.function
  def call(self, x):
    return x * self.v

module_output = CustomModule()
input = tf.random.uniform([batch_size, seq_len], dtype=tf.float32)
input2 = tf.random.uniform([batch_size, seq_len+1], dtype=tf.float32)
output = tf.random.uniform([batch_size, seq_len], dtype=tf.float32)
output2 = tf.random.uniform([batch_size, seq_len+1], dtype=tf.float32)

optimizer = tf.keras.optimizers.SGD()
training_loss = tf.keras.losses.MeanSquaredError()
module_output.compile(optimizer=optimizer, loss=training_loss)
#hist = module_output.fit(input, output, epochs=1, steps_per_epoch=1, verbose=0)
#hist = module_output.fit(input2, output2, epochs=1, steps_per_epoch=1, verbose=0)

a = module_output(input)               # the first signature
a = module_output(input2)              # the second signature
module_output.save('savedModel/', True, False)
module_output = tf.keras.models.load_model('savedModel/')
a = module_output(input)               # <= it works
a = module_output(input2)              # <= the error is here

How can I make it work ?

Edit: It is a toy example. I can not compose model using functional API because the real model is too complicated.


回答1:


Try creating the model using different Input shape, and using functional API:

def create_model(batch_size, seq_len):
   inputs = tf.keras.Input(shape=(batch_size, seq_len)) #input layer
   x = tf.keras.layers...(inputs) # next layer
   x = tf.keras.layers...(x)
   ...
   outputs = tf.keras.layers...(x) # output layer
   model = tf.keras.Model(inputs = inputs, outputs = outputs)
   model.compile(...)
   return model

Since you inherit from Model, it should work if you replace the model declaration line.



来源:https://stackoverflow.com/questions/63953163/how-to-save-keras-custom-model-with-dynamic-input-shape-in-savemodel-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!