如何在keras中微调已加载的模型?

时间:2019-08-05 10:22:55

标签: keras neural-network deep-learning finetunning

我已经使用预训练网络作为特征提取器训练了模型。我想微调我的模型以解冻最后一个卷积块。问题在于,当我加载训练后的模型时,整个网络被视为一层,因此我无法访问其特定的层/块。

如何解冻已加载的预训练网络的特定层?


我已经弄清楚了。加载的模型的第一层也是模型对象,因此我们可以按以下方式访问其层:loadel_model.layers [0] .layers。

1 个答案:

答案 0 :(得分:1)

这是基本模型

 import tensorflow as tf
 from tensorflow.keras.datasets import imdb
 import numpy as np
 import matplotlib.pyplot as plt
 from tensorflow.keras import models, optimizers, losses, metrics, layers



def one_hot(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.
    return results


(train_data,train_labels),(test_data,test_labels) = imdb.load_data(num_words=10000)
x_train = one_hot(train_data)
x_test = one_hot(test_data)

y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')

inputs = layers.Input(shape=(10000,))
x = layers.Dense(16)(inputs)
x = layers.Activation(tf.nn.relu)(x)

x = layers.Dense(16, activation='relu')(x)
predictions = layers.Dense(1, kernel_regularizer=tf.keras.regularizers.l2(1e-3),
                           activation='sigmoid')(x)

model = tf.keras.Model(inputs=inputs, outputs=predictions)

print('There are {} layers in this model.'.format(len(model.layers)))
# Notice that if the dense(or conv2d) and the activation are in one layer,
# it is regarded as one layer. if they are written separately, they are regarded as two layers


model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
              loss=tf.keras.losses.binary_crossentropy,
              metrics=['acc'])

x_val = x_train[:10000]
partial_x_train = x_train[10000:]
y_val = y_train[:10000]
partial_y_train = y_train[10000:]

tb_dir = ".\\base_model\Graph"
tb_callback = tf.keras.callbacks.TensorBoard(log_dir=tb_dir,
                                             histogram_freq=1,
                                             write_graph=True,
                                             write_grads=True)

history = model.fit(partial_x_train,
                    partial_y_train,
                    batch_size=512,
                    epochs=20,
                    verbose=2,
                    validation_data=(x_val, y_val),
                    callbacks=[tb_callback])

model.summary()
model.save('.\\base_model.h5')

这是已转移的模型(微调):

import tensorflow as tf
from tensorflow.keras.datasets import imdb
import numpy as np
from tensorflow.keras import models, layers, optimizers, losses, metrics
from tensorflow.keras import backend as K


def one_hot(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.
    return results


(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
x_train = one_hot(train_data)
x_test = one_hot(test_data)
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')

x_val = x_train[:10000]
partial_x_train = x_train[10000:]
y_val = y_train[:10000]
partial_y_train = y_train[10000:]

base_model = models.load_model('base_model.h5')

print("Number of layers in the base model: ", len(base_model.layers))

input_tensor = base_model.layers[1].output     # choose how many layers you want to keep
h1 = layers.Dense(10, name='dense_new_1')(input_tensor)
h2 = layers.Dense(1, name='dense_new_2')(h1)
out = layers.Activation('sigmoid')(h2)

new_model = models.Model(base_model.input, outputs=out)

for i in range(len(base_model.layers)): 
    layers.trainable = True   # True--> fine tine, False-->frozen

print("Number of layers in the new model: ", len(new_model.layers))

new_model.compile(optimizer=optimizers.RMSprop(lr=0.001),
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

tb_dir = ".\\transfer\Graph"
tb_callback = tf.keras.callbacks.TensorBoard(log_dir=tb_dir,
                                             write_graph=True)

history = new_model.fit(partial_x_train,      # input your new training data and labels
                        partial_y_train,
                        batch_size=512,
                        epochs=20,
                        verbose=2,
                        validation_data=(x_val, y_val),
                        callbacks=[tb_callback])
new_model.summary()