Tensorflow TimeDistributed 包裹模型加载/保存

问题描述 投票:0回答:1

按照 Tensorflow 教程此处, 一旦训练了以下模型:

张量流版本:2.17.0

输入形状:

(None, 16, 224, 224, 3)

哪个是

(BATCH_SIZE, SEQUENCE, 224, 224,3)

net = tf.keras.applications.EfficientNetB0(include_top = False)
net.trainable = False

model = tf.keras.Sequential([
    tf.keras.layers.Rescaling(scale=255),
    tf.keras.layers.TimeDistributed(net),
    tf.keras.layers.Dense(10),
    tf.keras.layers.GlobalAveragePooling3D()
])

有什么特殊的保存方法吗?我尝试过,例如。

model.save('model.keras')
,它保存了模型,但当我尝试时:
loaded_model = tf.keras.models.load_model('model.keras') 

我收到以下错误:

ValueError: Exception encountered when calling TimeDistributed.call().

Cannot convert '16' to a shape.

Arguments received by TimeDistributed.call():
  • args=('<KerasTensor shape=(None, 16, 224, 224, 3), dtype=float32, sparse=False, name=keras_tensor_2462>',)
  • kwargs={'mask': 'None'}

还尝试了保存/加载模型的不同方法:

# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.weights.h5")
print("Saved model to disk")
# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = tf.keras.models.model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.weights.h5")
print("Loaded model from disk")
tensorflow keras conv-neural-network
1个回答
0
投票

更新:

设法通过使用以下方法使其工作:

model.export('model')

然后使用以下命令导入它:

loaded_model = tf.saved_model.load('model')

然后使用

y = loaded_model .serve(np.expand_dims(sample_video, axis = 0))

© www.soinside.com 2019 - 2024. All rights reserved.