利用预训练好的resnet50模型进行迁移训练
- 在下载resnet50模型时出现了域名解析错误,查了好多都不知道怎么解决。
- 总的来看,网络分为了两层:第一层是resnet50的模型,其中包含了很多卷积层与池化层等,并且已经预训练好了权重偏置等。第二层是自定义的全连接层。在训练模型时,设置的是仅训练全连接层,及resnet50_fine_tune.layer[0].trainable = False
实战
height = 224
width = 224
channels = 3
batch_size = 24
num_classes = 10
train_datagen = keras.preprocessing.image.ImageDataGenerator(
preprocessing_function = keras.applications.resnet50.preprocess_input,
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = "nearest"
)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size = (height, width),
batch_size = batch_size,
seed = 7,
shuffle = True,
class_mode = 'categorical')
valid_datagen = keras.preprocessing.image.ImageDataGenerator(
preprocessing_function = keras.applications.resnet50.preprocess_input)
valid_generator = valid_datagen.flow_from_directory(
valid_dir,
target_size = (height, width),
batch_size = batch_size,
seed = 7,
shuffle = False,
class_mode = 'categorical')
train_num = train_generator.samples
valid_num = valid_generator.samples
print(train_num, valid_num)
for i in range(2):
x, y = train_generator.next()
print(x.shape, y.shape)
print(y)
from keras.applications.resnet50 import ResNet50
resnet50_fine_tune = keras.models.Sequential()
resnet50_fine_tune.add(ResNet50(include_top = False,
pooling = 'avg',
weights = 'imagenet'))
resnet50_fine_tune.add(keras.layers.Dense(num_classes,activation = 'softmax'))
resnet50_fine_tune.layer[0].trainable = False
resnet50_fine_tune.compile(loss="categorical_crossentropy",
optimizer="sgd",
metrics = ["accuracy"])
resnet50_fine_tune.summary()
epochs = 10
history = resnet50_fine_tune.fit_generator(train_generator,
steps_per_epoch = train_num // batch_size,
epochs=epochs,
validation_data = valid_generator,
validation_steps = valid_num // batch_size
)
print(history.history.keys())
def plot_learning_curves(history, label, epochs, min_value, max_value):
data = {}
data[label]=history.history[label]
data['val_'+label]=history.history['val_'+label]
pd.DataFrame(data).plot(figsize=(8, 5))
plt.grid(True)
plt.axis([0, epochs, min_value, max_value])
plt.show()
plot_learning_curves(history, 'accuracy', epochs, 0, 1)
plot_learning_curves(history, 'loss', epochs, 1.5, 2.5)