您当前的位置:首页 > IT编程 > 深度学习
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:keras: 用预训练的模型提取特征

51自学网 2020-11-06 11:43:53
  深度学习
这篇教程keras: 用预训练的模型提取特征写得很实用,希望能帮到您。

keras: 用预训练的模型提取特征

keras提供了VGG19在ImageNet上的预训练权重模型文件,其他可用的模型还有VGG16、Xception、ResNet50、InceptionV3 4个。

VGG19在keras中的定义:


 
  1.  
    def VGG19(include_top=True, weights='imagenet',
  2.  
    input_tensor=None, input_shape=None,
  3.  
    pooling=None,
  4.  
    classes=1000)
  5.  
     

参数介绍:

include_top: 是否包含最后的3个全连接层

weights: 定义为‘imagenet’,表示加载在imagenet数据库上训练的预训练权重,定义为None则不加载权重,参数随机初始化

1)使用VGG19预训练模型分类图片的例子


 
  1.  
    # coding: utf-8
  2.  
    from keras.applications.vgg19 import VGG19
  3.  
    from keras.preprocessing import image
  4.  
    from keras.applications.vgg19 import preprocess_input
  5.  
    from keras.models import Model
  6.  
    import numpy as np
  7.  
     
  8.  
    base_model = VGG19(weights='imagenet', include_top=True)
  9.  
    img_path = 'cat.jpg'
  10.  
    img = image.load_img(img_path, target_size=(224, 224)) # 加载图像,归一化大小
  11.  
    x = image.img_to_array(img) # 序列化
  12.  
    x = np.expand_dims(x, axis=0) # 扩展维度[batch_size,H,W,Channel]
  13.  
    x = preprocess_input(x) # 预处理,默认mode="caffe"
  14.  
    """
  15.  
    def preprocess_input(x, data_format=None, mode='caffe'):
  16.  
    Preprocesses a tensor or Numpy array encoding a batch of images.
  17.  
     
  18.  
    # Arguments
  19.  
    x: Input Numpy or symbolic tensor, 3D or 4D.
  20.  
    data_format: Data format of the image tensor/array.
  21.  
    mode: One of "caffe", "tf".
  22.  
    - caffe: will convert the images from RGB to BGR,
  23.  
    then will zero-center each color channel with
  24.  
    respect to the ImageNet dataset,
  25.  
    without scaling.
  26.  
    - tf: will scale pixels between -1 and 1,
  27.  
    sample-wise.
  28.  
     
  29.  
    # Returns
  30.  
    Preprocessed tensor or Numpy array.
  31.  
     
  32.  
    # Raises
  33.  
    ValueError: In case of unknown `data_format` argument.
  34.  
    """
  35.  
    out = base_model.predict(x) # 预测结果,1000维的向量
  36.  
    print(out.shape) # 预测结果out是一个1000维的向量,代表了预测结果分别属于10000个分类的概率,形状是(1,1000)

2) 使用VGG19预训练模型提取VGG19网络中任意层的输出特征的例子

上个例子可以看到keras对VGG网络的封装异常好,简单几行代码就可以分类图片。keras中VGG预训练参数模型另一个更常用的应用是可以提取VGG网络中任意一层的特征。
以下例子提取的是VGG19网络中第5个卷积层的输出特征(也是第1个全连接层的输入特征)


 
  1.  
    # coding: utf-8
  2.  
    from keras.applications.vgg19 import VGG19
  3.  
    from keras.preprocessing import image
  4.  
    from keras.applications.vgg19 import preprocess_input
  5.  
    from keras.models import Model
  6.  
    import numpy as np
  7.  
     
  8.  
    base_model = VGG19(weights='imagenet', include_top=False)
  9.  
    model = Model(inputs=base_model.input, outputs=base_model.get_layer('block5_pool').output)
  10.  
    img_path = 'cat.jpg'
  11.  
    img = image.load_img(img_path, target_size=(224, 224))
  12.  
    x = image.img_to_array(img)
  13.  
    x = np.expand_dims(x, axis=0)
  14.  
    x = preprocess_input(x)
  15.  
    block5_pool_features = model.predict(x)
  16.  
    print(block5_pool_features.shape) #(1, 7, 7, 512)

也可以设置为加载最后3个全连接层的VGG19网络,就可以获取最后3个全连接层的输出了:


 
  1.  
    # coding: utf-8
  2.  
    from keras.applications.vgg19 import VGG19
  3.  
    from keras.preprocessing import image
  4.  
    from keras.applications.vgg19 import preprocess_input
  5.  
    from keras.models import Model
  6.  
    import numpy as np
  7.  
    base_model = VGG19(weights='imagenet', include_top=True)
  8.  
    model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc2').output)
  9.  
    img_path = 'cat.jpg'
  10.  
    img = image.load_img(img_path, target_size=(224, 224))
  11.  
    x = image.img_to_array(img)
  12.  
    x = np.expand_dims(x, axis=0)
  13.  
    x = preprocess_input(x)
  14.  
    fc2 = model.predict(x)
  15.  
    print(fc2.shape) #(1, 4096)

加了全连接层,所以base_model.get_layer('fc2') 里参数也可以是 flatten、fc1、fc2和predictions 。

VGG19各个模块在keras中定义的名称可以通过以下代码获得,然后根据名称可以轻松获取该层特征::


 
  1.  
    from keras.applications.vgg19 import VGG19
  2.  
    from keras.preprocessing import image
  3.  
    from keras.applications.vgg19 import preprocess_input
  4.  
    from keras.models import Model
  5.  
    import numpy as np
  6.  
    import os
  7.  
    import tensorflow as tf
  8.  
    os.environ["CUDA_VISIBLE_DEVICES"] = "0"
  9.  
    gpu_options = tf.GPUOptions(allow_growth=True)
  10.  
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
  11.  
    from keras.utils import plot_model
  12.  
    from matplotlib import pyplot as plt
  13.  
     
  14.  
    #【0】VGG19模型,加载预训练权重
  15.  
    base_model = VGG19(weights='imagenet')
  16.  
     
  17.  
    #【1】创建一个新model, 使得它的输出(outputs)是 VGG19 中任意层的输出(output)
  18.  
    model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)
  19.  
    print(model.summary()) # 打印模型概况,如下所示
  20.  
    plot_model(model,to_file = 'a simple convnet.png') # 画出模型结构图,并保存成图片,如下图所示
  21.  
    """
  22.  
    _________________________________________________________________
  23.  
    Layer (type) Output Shape Param #
  24.  
    =================================================================
  25.  
    input_1 (InputLayer) (None, 224, 224, 3) 0
  26.  
    _________________________________________________________________
  27.  
    block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
  28.  
    _________________________________________________________________
  29.  
    block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
  30.  
    _________________________________________________________________
  31.  
    block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
  32.  
    _________________________________________________________________
  33.  
    block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
  34.  
    _________________________________________________________________
  35.  
    block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
  36.  
    _________________________________________________________________
  37.  
    block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
  38.  
    _________________________________________________________________
  39.  
    block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
  40.  
    _________________________________________________________________
  41.  
    block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
  42.  
    _________________________________________________________________
  43.  
    block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
  44.  
    _________________________________________________________________
  45.  
    block3_conv4 (Conv2D) (None, 56, 56, 256) 590080
  46.  
    _________________________________________________________________
  47.  
    block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
  48.  
    _________________________________________________________________
  49.  
    block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
  50.  
    _________________________________________________________________
  51.  
    block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
  52.  
    _________________________________________________________________
  53.  
    block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
  54.  
    _________________________________________________________________
  55.  
    block4_conv4 (Conv2D) (None, 28, 28, 512) 2359808
  56.  
    _________________________________________________________________
  57.  
    block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
  58.  
    =================================================================
  59.  
    Total params: 10,585,152
  60.  
    Trainable params: 10,585,152
  61.  
    Non-trainable params: 0
  62.  
    _________________________________________________________________
  63.  
     
  64.  
    """

 

3)最终的特征提取代码


 
  1.  
    # -*- coding: UTF-8 -*-
  2.  
    #-------------------------------------------
  3.  
    #任 务:利用VGG19提取任意中间层特征
  4.  
    #数 据:网上下载的测试图片‘elephant.jpg’
  5.  
    #-------------------------------------------
  6.  
     
  7.  
    from keras.applications.vgg19 import VGG19
  8.  
    from keras.preprocessing import image
  9.  
    from keras.applications.vgg19 import preprocess_input
  10.  
    from keras.models import Model
  11.  
    import numpy as np
  12.  
    import os
  13.  
    import tensorflow as tf
  14.  
    os.environ["CUDA_VISIBLE_DEVICES"] = "6"
  15.  
    gpu_options = tf.GPUOptions(allow_growth=True)
  16.  
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
  17.  
    from keras.utils import plot_model
  18.  
    from matplotlib import pyplot as plt
  19.  
     
  20.  
    #【0】VGG19模型,加载预训练权重
  21.  
    base_model = VGG19(weights='imagenet')
  22.  
     
  23.  
    #【1】创建一个新model, 使得它的输出(outputs)是 VGG19 中任意层的输出(output)
  24.  
    model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)
  25.  
    print(model.summary()) # 打印模型概况
  26.  
    plot_model(model,to_file = 'a simple convnet.png') # 画出模型结构图,并保存成图片
  27.  
     
  28.  
    '''
  29.  
    _________________________________________________________________
  30.  
    Layer (type) Output Shape Param #
  31.  
    =================================================================
  32.  
    input_1 (InputLayer) (None, 224, 224, 3) 0
  33.  
    _________________________________________________________________
  34.  
    block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
  35.  
    _________________________________________________________________
  36.  
    block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
  37.  
    _________________________________________________________________
  38.  
    block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
  39.  
    _________________________________________________________________
  40.  
    block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
  41.  
    _________________________________________________________________
  42.  
    block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
  43.  
    _________________________________________________________________
  44.  
    block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
  45.  
    _________________________________________________________________
  46.  
    block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
  47.  
    _________________________________________________________________
  48.  
    block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
  49.  
    _________________________________________________________________
  50.  
    block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
  51.  
    _________________________________________________________________
  52.  
    block3_conv4 (Conv2D) (None, 56, 56, 256) 590080
  53.  
    _________________________________________________________________
  54.  
    block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
  55.  
    _________________________________________________________________
  56.  
    block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
  57.  
    _________________________________________________________________
  58.  
    block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
  59.  
    _________________________________________________________________
  60.  
    block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
  61.  
    _________________________________________________________________
  62.  
    block4_conv4 (Conv2D) (None, 28, 28, 512) 2359808
  63.  
    _________________________________________________________________
  64.  
    block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
  65.  
    =================================================================
  66.  
    Total params: 10,585,152
  67.  
    Trainable params: 10,585,152
  68.  
    Non-trainable params: 0
  69.  
    _________________________________________________________________
  70.  
     
  71.  
    '''
  72.  
     
  73.  
    #【2】从网上下载一张图片,保存在当前路径下
  74.  
    img_path = 'elephant.jpg'
  75.  
    img = image.load_img(img_path, target_size=(224, 224)) # 加载图片并resize成224x224
  76.  
     
  77.  
    #【3】将图片转化为4d tensor形式
  78.  
    x = image.img_to_array(img)
  79.  
    x = np.expand_dims(x, axis=0)
  80.  
     
  81.  
    #【4】数据预处理
  82.  
    x = preprocess_input(x) #去均值中心化,preprocess_input函数详细功能见注释
  83.  
    """
  84.  
    def preprocess_input(x, data_format=None, mode='caffe'):
  85.  
    Preprocesses a tensor or Numpy array encoding a batch of images.
  86.  
     
  87.  
    # Arguments
  88.  
    x: Input Numpy or symbolic tensor, 3D or 4D.
  89.  
    data_format: Data format of the image tensor/array.
  90.  
    mode: One of "caffe", "tf".
  91.  
    - caffe: will convert the images from RGB to BGR,
  92.  
    then will zero-center each color channel with
  93.  
    respect to the ImageNet dataset,
  94.  
    without scaling.
  95.  
    - tf: will scale pixels between -1 and 1,
  96.  
    sample-wise.
  97.  
     
  98.  
    # Returns
  99.  
    Preprocessed tensor or Numpy array.
  100.  
     
  101.  
    # Raises
  102.  
    ValueError: In case of unknown `data_format` argument.
  103.  
    """
  104.  
    #【5】提取特征
  105.  
    block4_pool_features = model.predict(x)
  106.  
    print(block4_pool_features.shape) #(1, 14, 14, 512)

示例图片: 

 


在keras中合奏resnet50和densenet121
利用vgg预训练模型提取任一层图像特征
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。