当我们要使用神经网络解决某一问题时,有时我们并不需要从0开始训练一个网络,因为训练一个较为复杂的网络,不仅会消耗很多时间也需要较大的数据集。
我们可以使用别人在解决与我们类似的问题时,已经训练好的网络和相应的参数,将网络结构进行“微调”,使得它适合我们的问题,最后在我们的数据集上进行再次的训练。
例如,在解决图像分类问题时
我们可以使用已经在大型的数据集上训练好的InceptionV3网络。
由于卷积层的主要作用是对图像特征的提取,并且在前几层卷积层,提取的方法(权重)基本相同。都是通过寻找图片中的 边,线,轮廓,然后组合。所以在使用InceptionV3时,我们可以锁住前面的卷积层的权重(训练时不进行更新),仅仅对后面的全连接层的参数进行训练。不仅可以保证准确率还能提高训练所需的时间。
实现:
(1)预定义InceptionV3网络,加载参数,锁住卷积层,或者卷积层的输出
import os
from tensorflow.keras import layers
from tensorflow.keras import Model
#下载网络参数,放置在 /tmp/
!wget --no-check-certificate \
https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5 \
-O /tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5
from tensorflow.keras.applications.inception_v3 import InceptionV3
local_weights_file = '/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'
#预定义InceptionV3网络
pre_trained_model = InceptionV3(input_shape = (150, 150, 3), #输入150x150的彩色图片
include_top = False, #去除网络卷积层前面的全连接层
weights = None) #无参数,稍后加载
pre_trained_model.load_weights(local_weights_file) #加载预训练参数
for layer in pre_trained_model.layers:
layer.trainable = False #锁住所有的InceptionV3网络层权重
# pre_trained_model.summary()
last_layer = pre_trained_model.get_layer('mixed7') #获得InceptionV3网络mixed7卷积层
print('last layer output shape: ', last_layer.output_shape)
last_output = last_layer.output #获得卷积层的输出
from tensorflow.keras.optimizers import RMSprop
# 展开卷积层的输出
x = layers.Flatten()(last_output)
# 加入一个有1024个神经元的隐藏层,使用relu激活函数
x = layers.Dense(1024, activation='relu')(x)
# 防止过拟合,使用Dropout随机丢弃20%的节点
x = layers.Dropout(0.2)(x)
# 输出层
x = layers.Dense (1, activation='sigmoid')(x)
#使用预训练模型的输入和我们自己定义的输出,生成最终的网络模型
model = Model( pre_trained_model.input, x)
#设置优化器,损失函数等
model.compile(optimizer = RMSprop(lr=0.0001),
loss = 'binary_crossentropy',
metrics = ['acc'])
(3)网络结构定义好后,接下来就只需进行,下载数据集进行网络的训练了。
!wget --no-check-certificate \
https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip \
-O /tmp/cats_and_dogs_filtered.zip
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import os
import zipfile
local_zip = '//tmp/cats_and_dogs_filtered.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp')
zip_ref.close()
# Define our example directories and files
base_dir = '/tmp/cats_and_dogs_filtered'
train_dir = os.path.join( base_dir, 'train')
validation_dir = os.path.join( base_dir, 'validation')
train_cats_dir = os.path.join(train_dir, 'cats') # Directory with our training cat pictures
train_dogs_dir = os.path.join(train_dir, 'dogs') # Directory with our training dog pictures
validation_cats_dir = os.path.join(validation_dir, 'cats') # Directory with our validation cat pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs')# Directory with our validation dog pictures
train_cat_fnames = os.listdir(train_cats_dir)
train_dog_fnames = os.listdir(train_dogs_dir)
# Add our data-augmentation parameters to ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255.,
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator( rescale = 1.0/255. )
# Flow training images in batches of 20 using train_datagen generator
train_generator = train_datagen.flow_from_directory(train_dir,
batch_size = 20,
class_mode = 'binary',
target_size = (150, 150))
# Flow validation images in batches of 20 using test_datagen generator
validation_generator = test_datagen.flow_from_directory( validation_dir,
batch_size = 20,
class_mode = 'binary',
target_size = (150, 150))
history = model.fit_generator(
train_generator,
validation_data = validation_generator,
steps_per_epoch = 100,
epochs = 20,
validation_steps = 50,
verbose = 2)
import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
plt.show()