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

article/2025/8/17 4:17:04

Keras 入门教程

  • 1.线性回归建模(快速入门)
  • 2.线性模型的优化
  • 3.波士顿房价回归 (MPL)
  • 4.卷积神经网络(CNN)
  • 5.使用LSTM RNN 进行时间序列预测
  • 6.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, mobilenetimport 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)]]


http://chatgpt.dhexx.cn/article/dW9hySvV.shtml

相关文章

keras教程_Keras教程

keras教程 Keras教程 (Keras Tutorial) PDF Version Quick Guide Resources Job Search Discussion PDF版本 快速指南 资源资源 求职 讨论区 Keras is an open source deep learning framework for python. It has been developed by an artificial intelligence researcher at…

深度学习——keras教程系列基础知识

大家好,本期我们将开始一个新的专题的写作,因为有一些小伙伴想了解一下深度学习框架Keras的知识,恰好本人也会一点这个知识,因此就开始尝试着写一写吧。本着和大家一起学习的态度,有什么写的不是很好的地方还请大家多多…

Keras实例教程(4)之迁移学习

迁移学习(Transfer Learning)是机器学习中的一个重要研究话题,也是在实践中具有重要价值的一类技术。Transfer learning focuses on storing knowledge gained while solving one problem and applying it to a different but related problem. 举例来说,在之前的文章中,我…

保姆级教程:手把手教你使用 Keras 搭建神经网络

大家好,本文从0到1详细讲解两种基于Keras的建模方法: 基于Sequential的建模;快速方便,易上手 基于函数式API的建模;易于扩展,灵活性强 文章目录 你会学到什么?技术提升导入内置数据集数据缩放…

keras教程-静态图编程框架keras-学习心得以及知识点总结

在过去的四个月里(2020三月开始的),和朋友一起对着官网的keras教程学习了一遍,学习的过程中发现有一些解释的不清楚的地方(官网的教程实在是~~~),我们自己做了实验,探索了问题的各个…

keras入门教程

线性回归非线性模型MNIST手写数字识别 改进欠拟合,过拟合 early stopDropout正则化梯度下降 批量梯度下降随机梯度下降小批量梯度下降其他找寻最低点的方法卷积神经网络RNN模型的保存和载入 保存模型载入模型绘制神经网络的结构 几个keras学习的网址: 1.…

超快速!10分钟入门Keras指南

点击上方“小白学视觉”,选择加"星标"或“置顶” 重磅干货,第一时间送达 本文转自|机器学习算法工程师 1 Keras框架介绍在用了一段时间的Keras后感觉真的很爽,所以特意祭出此文与我们公众号的粉丝分享。 Keras是一个非常方便的深度…

LinuxReader —— 在windows下查看linux系统文件

怕忘了有这个一个方法,记录一下 我是用everything查看文件夹位置,然后点击: 直接可以查看Linux下的文件: 还有饼状图:

如何查看Linux系统的硬件配置

1、查看Linux系统的cpu的个数 cat /proc/cpuinfo | grep "model name"2、查看Linux系统的内存大小 cat /proc/meminfo | grep "MemTotal"3、查看Linux系统文件系统磁盘挂在情况 df -h也可以使用fdisk -l查看磁盘总概况

查看linux系统的glibc版本

查看linux系统的glibc版本 getconf GNU_LIBC_VERSION # 或者 ldd --version

查看Linux系统的初始安装时间

偶然在网上冲浪的时候看到有人问如何查看自己的linux系统的最初的安装时间,然后研究了一下,大致总结出了几个方法。 1.先查看系统盘挂到哪个分区上,然后用 dumpe2fs 查看这个磁盘分区 创建的时间 2.查看 lostfound 目录的时间 3.使用uptime(前…

查看linux系统CPU内存

文章目录 1 查看linux系统的CPU型号、类型以及大小2 查看linux系统内存总内存,剩余内存、可使用内存等信息3 查看linux系统各分区的使用情况4 查看linux系统内存使用量和交换区使用量5查看系统版本 1 查看linux系统的CPU型号、类型以及大小 cat /proc/cpuinfo | mo…

linux 怎么查看内核日志,怎样查看Linux系统日志?

原标题:怎样查看Linux系统日志? 很多企业都会使用Linux系统,审计Linux系统日志可以提供有关网络事件的重要信息。高效查看Linux系统日志对工作而言十分重要,以下是常用命令 # uname -a # 查看内核/操作系统/CPU信息 # cat /etc/is…

怎么查看linux系统防火墙,如何查看linux系统中防火墙的状态

如何查看linux系统中防火墙的状态 发布时间:2020-04-23 13:52:39 来源:亿速云 阅读:253 作者:小新 这篇文章主要为大家详细介绍了如何查看linux系统中防火墙的状态,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。 linux防火墙状态查看的…

查看Linux系统负载命令

查看Linux系统负载的命令一般常用的有5种: 1.uptime 该命令可以显示的信息依次为:现在时间、系统已运行了多长时间、目前有多少登录用户、系统过去1分钟,5分钟,15分钟内的平均负载。 18:36:16 //系统当前时间 up 0 m…

查看linux系统是centos还是ubuntu的方法

查看系统架构信息: 1、uname -a 2、cat /proc/version 查看Linux系统是ubuntu还是CentOS 1、cat /etc/redhat-release 适用于:CentOS,RedHat 若回显中包含CentOS ,则为CentOS系统。ubuntu系统不支持该命令 2.cat /etc/issu…

查看Linux系统是UBUNTU还是CentOS的方法

使用以下命令查看Linux系统是UBUNTU还是CentOS,在命令行下输入: 1、cat /etc/redhat-release 适用于:CentOS,RedHat 如图,出现CentOS ,则为CentOS系统。 2.cat /etc/issue 显示如下图,则为Ce…

查看linux系统信息的常用命令

1. 查看linux系统内核版本 uname -a uname -r cat /proc/version 2. 查看linux系统版本 lsb_release -a cat /etc/redhat-release 3. 查看linux系统的架构是amd还是arm arch 返回x86_64就是amd的 4. 查看linux系统是32还是64 getconf LONG_BIT 5. 查看系统是实体机还是…

【nginx】nginx的使用

接上一篇的部署,之后就是使用它 cd /usr/local/nginx/conf vi nginx.conf 只改了两个地方 到本地改 hosts文件 C:\Windows\System32\drivers\etc\hosts 增加这一行,其中前面的ip是nginx所在服务器的地址。 此时不需要dns,直接本地访问这个…

Nginx的介绍与使用

想必大家一定听说过 Nginx,若没听说过它,那么一定听过它的"同行"Apache 吧! Nginx 的产生 Nginx(engine x) 同 Apache 一样都是一种 Web 服务器。基于 REST 架构风格,以统一资源描述符(Uniform Resources …