我创建了一个模型并拟合如下所示。我还按照Keras官方文档保存和加载模型。
c1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(inputs)
c1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = tf.keras.layers.MaxPooling2D((2, 2))(c1)
c2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)
c2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)
u3 = tf.keras.layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(c2)
u3 = tf.keras.layers.concatenate([u3, c1], axis=3)
c3 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u3)
c3 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)
outputs = tf.keras.layers.Conv2D(1, (1, 1), activation='linear')(c3)
model = tf.keras.Model(inputs=[inputs], outputs=[outputs])
model.compile(optimizer='ADAM', loss='mean_squared_error', metrics=['mae'])
model.save('my_model')
model.save_weights('my_model_weights.h5')
history = model.fit(
train_generator,
steps_per_epoch=train_steps,
epochs=epochs,
validation_data=validation_generator,
validation_steps=valid_steps)
我知道保存的模型和权重可以如下方式加载:
model.load_weights('my_model_weights.h5')
model.load_weights('my_model_weights.h5', by_name=True)
如果我想做迁移学习,并将保存的模型和权重应用于相同的架构,但具有不同的数据,应该做什么?
错误:AttributeError Traceback (most recent call last)
<ipython-input-16-e5ee0aa441fb> in <module>
1 # Loading saved model
----> 2 new_model = tf.keras.load_model('my_model')
3 # New model using the same architecture, but without loading it
4 new_model_bis = tf.keras.Model(inputs=[inputs], outputs=[outputs])
5 new_model_bis.compile(optimizer='ADAM', loss='mean_squared_error', metrics=['mae'])
AttributeError: module 'tensorflow.keras' has no attribute 'load_model'
你部分回答了你自己的问题。
一旦您保存了权重,如果您已经保存了模型,那么您首先需要加载模型,然后是权重。如果您只保存了权重,那么您需要创建一个具有完全相同架构的模型,然后加载权重
c1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(inputs)
c1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = tf.keras.layers.MaxPooling2D((2, 2))(c1)
c2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)
c2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)
u3 = tf.keras.layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(c2)
u3 = tf.keras.layers.concatenate([u3, c1], axis=3)
c3 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u3)
c3 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)
outputs = tf.keras.layers.Conv2D(1, (1, 1), activation='linear')(c3)
model = tf.keras.Model(inputs=[inputs], outputs=[outputs])
model.compile(optimizer='ADAM', loss='mean_squared_error', metrics=['mae'])
model.save('my_model')
model.save_weights('my_model_weights.h5')
# Here's how you load the weight and models
# Loading saved model
new_model = tf.keras.load_model('my_model')
# New model using the same architecture, but without loading it
new_model_bis = tf.keras.Model(inputs=[inputs], outputs=[outputs])
new_model_bis.compile(optimizer='ADAM', loss='mean_squared_error', metrics=['mae'])
new_model.load_weights('my_model_weights.h5')
new_model_bis.load_weights('my_model_weights.h5')
print(new_model.summary())
# Both models would now be ready to use
new_model.predict(...)
然而,这不是迁移学习。这只是训练一个模型,然后在其他地方重用它。迁移学习是使用预先训练的模型,替换最后一层以满足您的需求。训练时,只训练修改过的层,比训练整个模型要快得多