卷积神经网络案例:使用pre_trained模型进行VGG预测

Posted ZSYL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了卷积神经网络案例:使用pre_trained模型进行VGG预测相关的知识,希望对你有一定的参考价值。

案例:使用pre_trained模型进行VGG预测

Google 在提供VGG行预测的时候效果会更好一些,所以选择VGG来进行测试

1. VGG模型使用

tensorflow.keras.applications中已经存在很多现有模型,

from tensorflow._api.v1.keras.applications import densenet
from tensorflow._api.v1.keras.applications import inception_resnet_v2
from tensorflow._api.v1.keras.applications import inception_v3
from tensorflow._api.v1.keras.applications import mobilenet
from tensorflow._api.v1.keras.applications import mobilenet_v2
from tensorflow._api.v1.keras.applications import nasnet
from tensorflow._api.v1.keras.applications import resnet50
from tensorflow._api.v1.keras.applications import vgg16
from tensorflow._api.v1.keras.applications import vgg19
from tensorflow._api.v1.keras.applications import xception
from tensorflow.python.keras.applications import DenseNet121
from tensorflow.python.keras.applications import DenseNet169
from tensorflow.python.keras.applications import DenseNet201
from tensorflow.python.keras.applications import InceptionResNetV2
from tensorflow.python.keras.applications import InceptionV3
from tensorflow.python.keras.applications import MobileNet
from tensorflow.python.keras.applications import MobileNetV2
from tensorflow.python.keras.applications import NASNetLarge
from tensorflow.python.keras.applications import NASNetMobile
from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.applications import VGG16
from tensorflow.python.keras.applications import VGG19
from tensorflow.python.keras.applications import Xception

我们来使用其中一个VGG16的模型进行预测,这个模型源码文件中提供了相关的预处理图片的接口以及预测结果概率的处理API.

from tensorflow.python.keras.applications import VGG16
from tensorflow.python.keras.applications.vgg16 import decode_predictions
from tensorflow.python.keras.applications.vgg16 import preprocess_input
  • preprocess_input:处理输入图片
  • decode_predictions:对预测结果进行处理

2. 步骤以及代码

  • 模型获取以及已训练好的参数加载
    • 注意:参数总计超过500M,因此当你首次使用下面的命令时,Keras需要从网上先下载这些参数模型数据到本地~/.keras/models,这可能需要耗用一些时间。
itcast:~/.keras/models$ ls -alh
total 1198296
drwxr-xr-x  5 root  staff   160B Jan 16 23:25 .
drwxr-xr-x  5 root  staff   160B Feb 19 13:11 ..
-rw-r--r--  1 root  staff    35K Jan 16 14:19 imagenet_class_index.json
-rw-r--r--  1 root  staff   528M Jan 16 14:15 vgg16_weights_tf_dim_ordering_tf_kernels.h5
itcast:~/.keras/models$

模型获取代码

model = VGG16()
print(model.summary())

模型打印为:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 224, 224, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
_________________________________________________________________
block3_conv1 (Conv2D)        (None, 56, 56, 256)       295168    
_________________________________________________________________
block3_conv2 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_conv3 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, 28, 28, 256)       0         
_________________________________________________________________
block4_conv1 (Conv2D)        (None, 28, 28, 512)       1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, 14, 14, 512)       0         
_________________________________________________________________
block5_conv1 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, 7, 7, 512)         0         
_________________________________________________________________
flatten (Flatten)            (None, 25088)             0         
_________________________________________________________________
fc1 (Dense)                  (None, 4096)              102764544 
_________________________________________________________________
fc2 (Dense)                  (None, 4096)              16781312  
_________________________________________________________________
predictions (Dense)          (None, 1000)              4097000   
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
  • 图片的输入以及格式转换

我们将会用到两个API,但是使用这个API需要PIL工具,3.7兆左右大小

# 在虚拟环境中下载
pip install PIL
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array

进行本地图片的加载,

# 加载一个图片到VGG指定输入大小
image = load_img('./tiger.png', target_size=(224, 224))

# 进行数据转换到numpy数组形式,以便于VGG能够进行使用
image = img_to_array(image)

# 形状修改
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
  • 使用模型对数据进行处理和预测
    • from tensorflow.python.keras.applications.vgg16 import decode_predictions
      from tensorflow.python.keras.applications.vgg16 import preprocess_input
    • 对模型输入进行处理,对预测结果进行解码
# 输入数据进行预测,进行图片的归一化处理
image = preprocess_input(image)
y_predict = model.predict(image)

# 进行结果解码
label = decode_predictions(y_predict)
# 进行lable获取
res = label[0][0]
# 预测的结果输出
print('预测的类别为:%s 概率为:(%.2f%%)' % (res[1], res[2]*100))

输出结果为:

Downloading data from https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json
8192/35363 [=====>........................] - ETA: 0s
24576/35363 [===================>..........] - ETA: 0s
40960/35363 [==================================] - 0s 6us/step

预测的类别为:tiger 概率为:(80.30%)

3. 完整代码

"""pre_trainedVGG网络预测案例"""
from tensorflow.python.keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
from tensorflow.python.keras.preprocessing.image import load_img, img_to_array
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def predict():
	# 实例化VGG16模型
    model = VGG16()
    print(model.summary())

    # 预测一张图片类别
    # 加载图片并输入到模型当中
    # (224, 224)是VGG输入的要求
    image = load_img('./tiger.png', target_size=(224, 224))
    image = img_to_array(image)
    print(image.shape)  # (224, 224, 3)

    # 输入卷积中,需要思维结构
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    print(image.shape)  # (1, 224, 224, 3)

    # 预测之前做图片的数据处理,归一化处理等
    image = preprocess_input(image)
    y_predictions = model.predict(image)

    print(y_predictions)

    # 对结果进行阶码,按照排序
    label = decode_predictions(y_predictions)
    print(('预测的类别是:%s,并且预测概率为:%f') % (label[0][0][1], label[0][0][2]))


if __name__ == '__main__':
    predict()

加油!

感谢!

努力!

以上是关于卷积神经网络案例:使用pre_trained模型进行VGG预测的主要内容,如果未能解决你的问题,请参考以下文章

相关名词

几种经典的卷积神经网络模型(必须收藏)

技术推文几种经典的卷积神经网络模型(必须收藏)

热文 | 卷积神经网络入门案例,轻松实现花朵分类

利用Python实现卷积神经网络的可视化

卷积神经网络