Tensorflow2.0学习(17):利用resnet50进行迁移学习

利用预训练好的resnet50模型进行迁移训练

  • 在下载resnet50模型时出现了域名解析错误,查了好多都不知道怎么解决。
  • 总的来看,网络分为了两层:第一层是resnet50的模型,其中包含了很多卷积层与池化层等,并且已经预训练好了权重偏置等。第二层是自定义的全连接层。在训练模型时,设置的是仅训练全连接层,及resnet50_fine_tune.layer[0].trainable = False

实战

# 做卷积的时候所有图片的尺寸应该是一样的
# resnet处理图像都是224*224的
height = 224
width = 224
channels = 3 
batch_size = 24
num_classes = 10

# 读取训练数据并作数据增强
# 确定一些读取格式要求
train_datagen = keras.preprocessing.image.ImageDataGenerator(
    # 给resnet50预处理图像的函数
    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,
    # label的编码格式:这里为one-hot编码
    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,
    # label的编码格式:这里为one-hot编码
    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最后一层有1000类,所以在本例中把最后一层去掉(include_top = False)
# resnet的倒数第二层是一个三维矩阵,所以无法与全连接层连接,故要pooling
# weights = 'imagenet': 下载一个模型,然后在这个模型的基础上进行训练
# weights = 'None':从头开始训练
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 
# 数据是generator出来的,所以不能直接用fit
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)

发布了35 篇原创文章 · 获赞 3 · 访问量 2494

猜你喜欢

转载自blog.csdn.net/Smile_mingm/article/details/104608050
今日推荐