文章目录

    • 1.VGG19网络结构:
    • 2.主文件:main.py:

1.VGG19网络结构:


注意:我们输出第四层的block4_pool层的特征:(读者可以根据这个输出其他层的特征也可以)


2.主文件:main.py:

import os
import numpy as np
from tensorflow.keras.models import Model
import tensorflow.keras.applications.vgg19
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg19 import preprocess_input,decode_predictions

def load_VGG19():
    model_VGG19=tensorflow.keras.applications.vgg19.VGG19(weights='imagenet')
    return model_VGG19

def ExtractVGG19():
    model_vgg19=load_VGG19()
    block1_conv1_model=Model(inputs=model_vgg19.input,outputs=model_vgg19.get_layer('block4_pool').output)
    img_path='images/train/dog/1.jpg'
    #导入图片并将图像剪裁为299*299
    img=image.load_img(img_path,target_size=(224,224))
    #将加载的图像转换为向量
    img=image.img_to_array(img)
    #对图像进行升维
    img=np.expand_dims(img,axis=0)
    #对图像进行处理
    img_out=preprocess_input(img)
    #输出卷积层的第一个卷积模块
    block1_conv1_out=block1_conv1_model.predict(img_out)
    #打印网络结构
    model_vgg19.summary()
    print('block1_conv1: {}'.format(block1_conv1_out))
    print('block1_conv1_shape: {}'.format(np.shape(block1_conv1_out)))



if __name__ == '__main__':
    print('Pycharm')
    ExtractVGG19()

更多推荐

从VGG19中任意层提取图像识别的特征