• 基于图像识别的迁移学习之二


    1.导库

    Keras里面提供了许多在ImageNet上的预训练模型,VGG-16ResNet50就是其中的预训练模型,可以通过from tensorflow.keras.applications import VGG16from tensorflow.keras.applications.resnet50 import ResNet50引入VGG-16模型和RseNet50模型。调用VGG-16模型和RseNet50模型分别使用VGG16(include_top=True, weights=‘imagenet’, input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation=‘softmax’)ResNet50(include_top=True, weights=‘imagenet’, input_tensor=None, input_shape=None, pooling=None, classes=1000)函数来加载预训练模型,其中两个模型中使用参数基本一致

    include_top:是否保留顶层的3个全连接网络;

    weightsNone代表随机初始化,即选择不加载预训练权重。‘imagenet’代表加载预训练权重;

    input_tensor:可填入Keras tensor作为模型的图像输出tensor

    input_shape:可选,仅当include_top=False有效,应为长为3tuple,指明输入图片的shape,图片的宽高必须大于48,如(200,200,3)

    pooling:当include_top=False时,该参数指定了池化方式。None代表不池化,最后一个卷积层的输出为4D张量。‘avg’代表全局平均池化,‘max’代表全局最大值池化;

    classes:可选,图片分类的类别数,仅当include_top=True并且不加载预训练权重时可用;

    返回值:Keras 模型对象。

    2.加载数据

    (train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

    3.使用预训练的VGG-16模型或者ResNet50模型

    #使用VGG-16模型

    base_model = VGG16(include_top=False, pooling ='avg',input_shape=(32, 32, 3))

    #使用RseNet50模型

    #base_model = ResNet50(include_top=False, pooling ='avg',input_shape=(32, 32, 3))

    for layer in base_model.layers:

        layer.trainable = False

    4.编译并训练

    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy’])

    5.评估模型

    test_loss, test_acc = model.evaluate(test_imagestest_labels, verbose=2)

     

  • 相关阅读:
    Zotero(1)---文献管理软件Zotero安装教程
    C++中constexpr的用法
    Leetcode20.有效的括号
    C语言指针操作(七)*指针数组和多重指针
    C# 模拟button按钮批量锁住与打开
    Vue底层术语解析以及存在关系
    Java中枚举类(enum)的实用小技巧
    TongWeb8下应用忙碌线程监控
    光线追踪与全域光渲染keyshot中文
    docker 挂载宿主机文件到容器中
  • 原文地址:https://blog.csdn.net/qq_20660115/article/details/133799741