I have a trained model that I've exported the weights and want to partially load into another model. My model is built in Keras using TensorFlow as backend.
Right now I'm doing as follows:
model = Sequential() model.add(Conv2D(32, (3, 3), input_shape=input_shape, trainable=False)) model.add(Activation('relu', trainable=False)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(32, (3, 3), trainable=False)) model.add(Activation('relu', trainable=False)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3), trainable=True)) model.add(Activation('relu', trainable=True)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(64)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) model.load_weights("image_500.h5") model.pop() model.pop() model.pop() model.pop() model.pop() model.pop() model.add(Conv2D(1, (6, 6),strides=(1, 1), trainable=True)) model.add(Activation('relu', trainable=True)) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
I'm sure it's a terrible way to do it, although it works.
How do I load just the first 9 layers?
Save Your Neural Network Model to JSON This can be saved to a file and later loaded via the model_from_json() function that will create a new model from the JSON specification. The weights are saved directly from the model using the save_weights() function and later loaded using the symmetrical load_weights() function.
Use the get_weights() function to get the weights and biases of the layers before training the model. These are the weights and biases with which the layers will be initialized.
Keras recommends to use model. save(). Scikit recommends joblib. After tuning the params with RandomizedSearchCV, you can just use trial_search.
How to save and load model weights in Keras? Create a Model. Let’s train this model, just so it has weight values to save, as well as an optimizer state. Save model weights at the end of epochs. If you like to save the model weights at the end epochs then you need to create... Load weight into the ...
How do I load just the first 9 layers? Seems model.pop () is not valid for modern Keras Model. If your first 9 layers are consistently named between your original trained model and the new model, then you can use model.load_weights () with by_name=True.
A Keras model consists of multiple components: The architecture, or configuration, which specifies what layers the model contain, and how they're connected. A set of weights values (the "state of the model").
To load the weights, you would first need to build your model, and then call load_weights on the model, as in Another saving technique is model.save (filepath). This save function saves: The architecture of the model, allowing to re-create the model.
If your first 9 layers are consistently named between your original trained model and the new model, then you can use model.load_weights()
with by_name=True
. This will update weights only in the layers of your new model that have an identically named layer found in the original trained model.
The name of the layer can be specified with the name
keyword, for example:
model.add(Dense(8, activation='relu',name='dens_1'))
This call:
weights_list = model.get_weights()
will return a list of all weight tensors in the model, as Numpy arrays.
All what you have to do next is to iterate over this list and apply:
for i, weights in enumerate(weights_list[0:9]): model.layers[i].set_weights(weights)
where model.layers
is a flattened list of the layers comprising the model. In this case, you reload the weights of the first 9 layers.
More information is available here:
https://keras.io/layers/about-keras-layers/
https://keras.io/models/about-keras-models/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With