Keras Upsampling2d -> tflite 转换导致形状推断失败和未定义的输出形状

Keras Upsampling2d -> tflite conversion results in failing shape inference and undefined output shape

Keras Upsampling2d 操作被转换成这个加上额外的操作和未定义的形状

然而,Tensorflow 在没有此操作的情况下以正确的形状进行转换

这会导致未定义的整体模型输出形状并导致设备错误。如何解决?

此行为在此处描述 https://github.com/tensorflow/tensorflow/issues/45090

Keras by default sets dynamic batch size to true. That means that the model input shape is [*,28,28] not [1,28,28]. The old(deprecated) converter used to ignore the dynamic batch and override this to 1 - which is wrong since this is not what the original model has - you can imagine how bad it will be when you try to resize the inputs at runtime.

The current converter instead handles the dynamic batch size correct, and the model generated can be resized at runtime correct. That's why the sequence of "Shape, StridedSlice, Pack" wasn't constant folded, since the shape is dependent on the shape defined at runtime.

对于单输入模型,这可以通过在保存前为 keras 模型设置恒定形状来解决

model.input.set_shape(1 + model.input.shape[1:])