简体   繁体   中英

How to reuse the model and weights in Keras

I created a model and fitted as shown below. I also followed the Keras official docs to save and load the 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')

history = model.fit(
    train_generator,
    steps_per_epoch=train_steps,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=valid_steps)

I know that the saved model and weights can be loaded as below:

model.load_weights('my_model_weights.h5')
model.load_weights('my_model_weights.h5', by_name=True)

If I want to do transfer learning and apply the saved model and weights to same architecture but with different data, what should be done?


error:


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'

You partly answered your own question.
Once you've saved the weights, if you've saved the model you first need to load the model, then the weights. If you only saved the weights, you need to create a model with the exact same architecture, then load the weights

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(...)

However, this isn't transfer learning. That's just training a model, and reusing it somewhere else. Transfer learning is using a pre-trained model, and replacing the last layers to fit your needs. When training, only the modified layers get trained, which is much faster than training the whole model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM