简体   繁体   中英

Can we alter the trained neural network structure (.json or .h5 file) and its weights(.h5 file) in keras?

After training a neural network and saving it in a specific format(say in .h5 or .json format). Suppose I want to do inference using the network but without a layer(say batch normalization) , can i remove only this layer from the model file(.h5 or json) and do inference?

Is this possible to alter the network and do inference ? If yes how?

Yes, and it is actually straightforward :) You need to name all your layers with parameters and use model.save_weights to save the weights and build another model with the desired architecture sharing some of the layers. Then you can use new_model.load_weights(..., by_name=True) to load only the shared layers, documentation . Here is an example:

input = Input(..., name='image_in')
conv1 = Conv2D(..., name='conv2d')(input)
normed = BatchNormalization(...)(conv1)
out = Flatten()(normed)
out = Dense(num_classes, activation='softmax', name='final_dense')(out)
model = Model(input, out)
# ... train etc
model.save_weights(model_file)

Then you can create another model and just use the same name for the layers you want to be shared:

input = Input(..., name='image_in')
conv1 = Conv2D(..., name='conv2d')(input) # reuse conv2d
out = Flatten()(conv1) # we got rid of batch
out = Dense(num_classes, activation='softmax', name='final_dense')(out) # reuse final_dense
new_model = Model(input, out)
# ... now load
new_model.load_weights(model_file, by_name=True)

Setting by_name=True only loads the layers with matching name into any architecture you want.

One method, which gives lots of freedom for different kind of model editing, but unfortunately quite cumbersome:

  1. Get the config and weights of your keras model by calling

    temp_config = my_model.get_config()

    temp_weights = my_model.get_weights()

  2. analyse what layer in temp_config you want to change or remove.

  3. make the change to the temp_config and the corresponding changes to temp_weights. Can unfortunately be quite confusing finding the right weights for each layer as the weights is just appended to each other (often ending up in a stream of weights, biases, weights, biases, weights, biases, etc...). In you case you are removing an layer, and remove the corresponding weights to that layer.

  4. then build the model using the new config and weights

    new_model = Model.from_config(new_config)

    new_model.set_weights(new_weights)

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