Keras入门教程 6.Keras 预训练模型应用

13 篇文章 0 订阅
7 篇文章 0 订阅

Keras 入门教程


Keras 预训练模型应用

Keras 应用模块用于为深度神经网络提供预训练模型。Keras 模型用于预测、特征提取和微调。本节详细介绍了 Keras 应用程序。

预训练模型
训练好的模型由模型架构和模型权重两部分组成。模型权重是大文件,因此我们必须从 ImageNet 数据库下载并提取特征。下面列出了一些流行的预训练模型:

  • ResNet
  • VGG16
  • MobileNet
  • InceptionResNetV2
  • InceptionResNetV3

加载模型

from keras.applications import vgg16, inception_v3, resnet50, mobilenet
#Load the VGG model
vgg_model = vgg16.VGG16(weights = 'imagenet')
#Load the Inception_V3 model
inception_model = inception_v3.InceptionV3(weights = 'imagenet')
#Load the ResNet50 model
resnet_model = resnet50.ResNet50(weights = 'imagenet')
#Load the MobileNet model 
mobilenet_model = mobilenet.MobileNet(weights = 'imagenet')

加载模型后,我们可以立即将其用于预测目的。让我们在接下来的章节中检查每个预训练模型。

ResNet是一个预训练模型。它使用 ImageNet 进行训练。在 ImageNet 上预训练的 ResNet 模型权重。它具有以下语法:

keras.applications.resnet.ResNet50 (
   include_top = True,
   weights = 'imagenet',
   input_tensor = None,
   input_shape = None,
   pooling = None, 
   classes = 1000
)
  • include_top 指的是网络顶部的全连接层。
  • weights 指的是 ImageNet 上的预训练。
  • input_tensor 指用作模型的图像输入的可选的 Keras 张量。
  • input_shape 指可选的形状元组。此模型的默认输入大小为 224x224。
  • clasees 指用于对图像进行分类的可选数量的类。

让我们通过写一个简单的例子来理解模型:

第 1 步:导入模块

加载如下指定的必要模块:

import PIL
from keras.preprocessing.image import load_img, image 
from keras.preprocessing.image import img_to_array 
from keras.applications.imagenet_utils import decode_predictions 
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.applications import vgg16, inception_v3, resnet50, mobilenet

import matplotlib.pyplot as plt
%matplotlib inline

第 2 步:选择一个输入

选择一个输入图像,网上找了一张图片,如下所示:

filename = 'pp8.png'
original = load_img(filename, target_size = (224, 224))
print('PIL image size',original.size)
plt.imshow(original);

在这里,我们加载了一个图像并显示了它。

在这里插入图片描述

第 3 步:将图像转换为 NumPy 数组

将输入的 Banana 转换为 NumPy 数组,以便将其传递到模型中以进行预测。

# 将图 转 numpy 数组
numpy_image = img_to_array(original)
plt.imshow(np.uint8(numpy_image))
print('numpy array size',numpy_image.shape)

# 将图转 图的批量格式
image_batch = np.expand_dims(numpy_image, axis = 0)
print('image batch size', image_batch.shape)

numpy array size (224, 224, 3)
image batch size (1, 224, 224, 3)
在这里插入图片描述

第 4 步:模型预测

将输入输入模型以获得预测

# 为 resnet50 模型 格式
processed_image = resnet50.preprocess_input(image_batch.copy())
# 创建 resnet 模型
resnet_model = resnet50.ResNet50(weights = 'imagenet')
# 获得 预测可能分类
predictions = resnet_model.predict(processed_image)

输出

# 转化为可能分类标签
label = decode_predictions(predictions,top=1)
print(label)

[[(‘n02676566’, ‘acoustic_guitar’, 0.98159516)]]

# 换另张图
filename = 'ba.png'

original1 = load_img(filename, target_size = (224, 224))
print('PIL image size',original1.size)
plt.imshow(original1)

在这里插入图片描述

numpy_image1 = img_to_array(original1)
plt.imshow(np.uint8(numpy_image1))
print('numpy array size',numpy_image1.shape)

image_batch1 = np.expand_dims(numpy_image1, axis = 0)
print('image batch size', image_batch1.shape)

在这里插入图片描述

processed_image1 = resnet50.preprocess_input(image_batch1.copy())
predictions1 = resnet_model.predict(processed_image1)
label = decode_predictions(predictions1,top=1)
print(label)

[[(‘n07753592’, ‘banana’, 0.99918777)]]

换其他模型

VGG16

VGG16 是另一个预训练模型。它还使用 ImageNet 进行训练。加载模型的语法如下:

keras.applications.vgg16.VGG16(
   include_top = True,
   weights = 'imagenet',
   input_tensor = None,
   input_shape = None,
   pooling = None,
   classes = 1000
)

此模型的默认输入大小为 224x224

MobileNetV2

MobileNetV2 是另一个预训练模型。它也是使用ImageNet 进行训练。 加载模型的语法如下:

keras.applications.mobilenet_v2.MobileNetV2 (
   input_shape = None, 
   alpha = 1.0, 
   include_top = True, 
   weights = 'imagenet', 
   input_tensor = None, 
   pooling = None, 
   classes = 1000
)

alpha控制网络的宽度。如果该值低于 1,则减少每层中的过滤器数量。如果该值大于 1,则增加每层中的过滤器数量。如果 alpha = 1,则在每一层使用纸张的默认过滤器数量。

此模型的默认输入大小为224x224

InceptionResNetV2

InceptionResNetV2 是另一个预训练模型。它也是使用ImageNet 进行训练。加载模型的语法如下:

keras.applications.inception_resnet_v2.InceptionResNetV2 (
   include_top = True, 
   weights = 'imagenet',
   input_tensor = None, 
   input_shape = None, 
   pooling = None, 
   classes = 1000)

此模型可以使用“channels_first”数据格式(通道、高度、宽度)或“channels_last”数据格式(高度、宽度、通道)构建。

此模型的默认输入大小为299x299

InceptionV3

InceptionV3 是另一个预训练模型。它也是使用ImageNet 进行训练。加载模型的语法如下:

keras.applications.inception_v3.InceptionV3 (
   include_top = True, 
   weights = 'imagenet', 
   input_tensor = None, 
   input_shape = None, 
   pooling = None, 
   classes = 1000
)

此模型的默认输入大小为299x299

#Load the VGG model
vgg_model = vgg16.VGG16(weights = 'imagenet')

#Load the Inception_V3 model
inception_model = inception_v3.InceptionV3(weights = 'imagenet')

#Load the MobileNet model 
mobilenet_model = mobilenet.MobileNet(weights = 'imagenet')
vgg16_image = vgg16.preprocess_input(image_batch1.copy())
inception_image = inception_v3.preprocess_input(image_batch1.copy())
mobilenet_image = mobilenet.preprocess_input(image_batch1.copy())
pred_vgg16 = vgg_model.predict(vgg16_image)
label = decode_predictions(pred_vgg16,top=1)
print(label)

[[(‘n07753592’, ‘banana’, 0.9616605)]]

pred_mobilenet = mobilenet_model.predict(mobilenet_image)
label = decode_predictions(pred_mobilenet,top=1)
print(label)

[[(‘n07753592’, ‘banana’, 0.9993032)]]

由于inception_v3模型默认的大小是299*299,因此图片重新处理,并归一化,使得分类效果更加明显。

img = image.load_img("ba.png", target_size=(299, 299))
input_image = image.img_to_array(img)
input_image /= 255.
input_image -= 0.5
input_image *= 2.
# Add a 4th dimension for batch size (Keras)
input_image = np.expand_dims(input_image, axis=0)

# Run the image through the NN
predictions = inception_model.predict(input_image)

# Convert the predictions into text
predicted_classes = inception_v3.decode_predictions(predictions, top=1)
predicted_classes

[[(‘n07753592’, ‘banana’, 0.99996054)]]

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值