#####好好好####keras之预训练模型Application

2024-05-07 14:58

本文主要是介绍#####好好好####keras之预训练模型Application,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Application应用

Kera的应用模块Application提供了带有预训练权重的Keras模型,这些模型可以用来进行预测、特征提取和finetune

模型的预训练权重将下载到~/.keras/models/并在载入模型时自动载入

可用的模型

应用于图像分类的模型,权重训练自ImageNet: Xception VGG16 VGG19 ResNet50 InceptionV3 InceptionResNetV2* MobileNet

所有的这些模型(除了Xception和MobileNet)都兼容Theano和Tensorflow,并会自动基于~/.keras/keras.json的Keras的图像维度进行自动设置。例如,如果你设置data_format="channel_last",则加载的模型将按照TensorFlow的维度顺序来构造,即“Width-Height-Depth”的顺序

Xception模型仅在TensorFlow下可用,因为它依赖的SeparableConvolution层仅在TensorFlow可用。MobileNet仅在TensorFlow下可用,因为它依赖的DepethwiseConvolution层仅在TF下可用。

以上模型(暂时除了MobileNet)的预训练权重可以在我的百度网盘下载,如果有更新的话会在这里报告


图片分类模型的示例

利用ResNet50网络进行ImageNet分类

from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as npmodel = ResNet50(weights='imagenet')img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted:', decode_predictions(preds, top=3)[0])
# Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]

利用VGG16提取特征

from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as npmodel = VGG16(weights='imagenet', include_top=False)img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)features = model.predict(x)

从VGG19的任意中间层中抽取特征

from keras.applications.vgg19 import VGG19
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input
from keras.models import Model
import numpy as npbase_model = VGG19(weights='imagenet')
model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)block4_pool_features = model.predict(x)

在新类别上fine-tune inceptionV3

from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(200, activation='softmax')(x)# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in base_model.layers:layer.trainable = False# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')# train the model on the new data for a few epochs
model.fit_generator(...)# at this point, the top layers are well trained and we can start fine-tuning
# convolutional layers from inception V3. We will freeze the bottom N layers
# and train the remaining top layers.# let's visualize layer names and layer indices to see how many layers
# we should freeze:
for i, layer in enumerate(base_model.layers):print(i, layer.name)# we chose to train the top 2 inception blocks, i.e. we will freeze
# the first 249 layers and unfreeze the rest:
for layer in model.layers[:249]:layer.trainable = False
for layer in model.layers[249:]:layer.trainable = True# we need to recompile the model for these modifications to take effect
# we use SGD with a low learning rate
from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')# we train our model again (this time fine-tuning the top 2 inception blocks
# alongside the top Dense layers
model.fit_generator(...)

在定制的输入tensor上构建InceptionV3

from keras.applications.inception_v3 import InceptionV3
from keras.layers import Input# this could also be the output a different Keras model or layer
input_tensor = Input(shape=(224, 224, 3))  # this assumes K.image_data_format() == 'channels_last'model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)

模型信息

模型大小Top1准确率Top5准确率参数数目深度
Xception88MB0.7900.94522,910,480126
VGG16528MB0.7150.901138,357,54423
VGG19549MB0.7270.910143,667,24026
ResNet5099MB0.7590.92925,636,712168
InceptionV392MB0.7880.94423,851,784159
IncetionResNetV2215MB0.8040.95355,873,736572
MobileNet17MB0.6650.8714,253,86488

Xception模型

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

Xception V1 模型, 权重由ImageNet训练而言

在ImageNet上,该模型取得了验证集top1 0.790和top5 0.945的正确率

注意,该模型目前仅能以TensorFlow为后端使用,由于它依赖于"SeparableConvolution"层,目前该模型只支持channels_last的维度顺序(width, height, channels)

默认输入图片大小为299x299

参数

  • include_top:是否保留顶层的3个全连接网络
  • weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重
  • input_tensor:可填入Keras tensor作为模型的图像输出tensor
  • input_shape:可选,仅当include_top=False有效,应为长为3的tuple,指明输入图片的shape,图片的宽高必须大于71,如(150,150,3)
  • pooling:当include_top=False时,该参数指定了池化方式。None代表不池化,最后一个卷积层的输出为4D张量。‘avg’代表全局平均池化,‘max’代表全局最大值池化。

  • classes:可选,图片分类的类别数,仅当include_top=True并且不加载预训练权重时可用。

返回值

Keras 模型对象

参考文献

  • Xception: Deep Learning with Depthwise Separable Convolutions

License

预训练权重由我们自己训练而来,基于MIT license发布


VGG16模型

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

VGG16模型,权重由ImageNet训练而来

该模型再Theano和TensorFlow后端均可使用,并接受channels_first和channels_last两种输入维度顺序

模型的默认输入尺寸时224x224

参数

  • include_top:是否保留顶层的3个全连接网络
  • weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重
  • input_tensor:可填入Keras tensor作为模型的图像输出tensor
  • input_shape:可选,仅当include_top=False有效,应为长为3的tuple,指明输入图片的shape,图片的宽高必须大于48,如(200,200,3)

返回值

  • pooling:当include_top=False时,该参数指定了池化方式。None代表不池化,最后一个卷积层的输出为4D张量。‘avg’代表全局平均池化,‘max’代表全局最大值池化。
  • classes:可选,图片分类的类别数,仅当include_top=True并且不加载预训练权重时可用。

Keras 模型对象

参考文献

  • Very Deep Convolutional Networks for Large-Scale Image Recognition:如果在研究中使用了VGG,请引用该文

License

预训练权重由牛津VGG组发布的预训练权重移植而来,基于Creative Commons Attribution License


VGG19模型

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

VGG19模型,权重由ImageNet训练而来

该模型在Theano和TensorFlow后端均可使用,并接受channels_first和channels_last两种输入维度顺序

模型的默认输入尺寸时224x224

参数

  • include_top:是否保留顶层的3个全连接网络
  • weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重
  • input_tensor:可填入Keras tensor作为模型的图像输出tensor
  • input_shape:可选,仅当include_top=False有效,应为长为3的tuple,指明输入图片的shape,图片的宽高必须大于48,如(200,200,3)
  • pooling:当include_top=False时,该参数指定了池化方式。None代表不池化,最后一个卷积层的输出为4D张量。‘avg’代表全局平均池化,‘max’代表全局最大值池化。
  • classes:可选,图片分类的类别数,仅当include_top=True并且不加载预训练权重时可用。

返回值

返回值

Keras 模型对象

参考文献

  • Very Deep Convolutional Networks for Large-Scale Image Recognition:如果在研究中使用了VGG,请引用该文

License

预训练权重由牛津VGG组发布的预训练权重移植而来,基于Creative Commons Attribution License


ResNet50模型

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

50层残差网络模型,权重训练自ImageNet

该模型在Theano和TensorFlow后端均可使用,并接受channels_first和channels_last两种输入维度顺序

模型的默认输入尺寸时224x224

参数

  • include_top:是否保留顶层的全连接网络
  • weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重
  • input_tensor:可填入Keras tensor作为模型的图像输出tensor
  • input_shape:可选,仅当include_top=False有效,应为长为3的tuple,指明输入图片的shape,图片的宽高必须大于197,如(200,200,3)
  • pooling:当include_top=False时,该参数指定了池化方式。None代表不池化,最后一个卷积层的输出为4D张量。‘avg’代表全局平均池化,‘max’代表全局最大值池化。
  • classes:可选,图片分类的类别数,仅当include_top=True并且不加载预训练权重时可用。

返回值

Keras 模型对象

参考文献

  • Deep Residual Learning for Image Recognition:如果在研究中使用了ResNet50,请引用该文

License

预训练权重由Kaiming He发布的预训练权重移植而来,基于MIT License


InceptionV3模型

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

InceptionV3网络,权重训练自ImageNet

该模型在Theano和TensorFlow后端均可使用,并接受channels_first和channels_last两种输入维度顺序

模型的默认输入尺寸时299x299

参数

  • include_top:是否保留顶层的全连接网络
  • weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重
  • input_tensor:可填入Keras tensor作为模型的图像输出tensor
  • input_shape:可选,仅当include_top=False有效,应为长为3的tuple,指明输入图片的shape,图片的宽高必须大于197,如(200,200,3)
  • pooling:当include_top=False时,该参数指定了池化方式。None代表不池化,最后一个卷积层的输出为4D张量。‘avg’代表全局平均池化,‘max’代表全局最大值池化。
  • classes:可选,图片分类的类别数,仅当include_top=True并且不加载预训练权重时可用。

返回值

Keras 模型对象

参考文献

  • Rethinking the Inception Architecture for Computer Vision:如果在研究中使用了InceptionV3,请引用该文

License

预训练权重由我们自己训练而来,基于MIT License


InceptionResNetV2模型

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

InceptionResNetV2网络,权重训练自ImageNet

该模型在Theano、TensorFlow和CNTK后端均可使用,并接受channels_first和channels_last两种输入维度顺序

模型的默认输入尺寸时299x299

参数

  • include_top:是否保留顶层的全连接网络
  • weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重
  • input_tensor:可填入Keras tensor作为模型的图像输出tensor
  • input_shape:可选,仅当include_top=False有效,应为长为3的tuple,指明输入图片的shape,图片的宽高必须大于197,如(200,200,3)
  • pooling:当include_top=False时,该参数指定了池化方式。None代表不池化,最后一个卷积层的输出为4D张量。‘avg’代表全局平均池化,‘max’代表全局最大值池化。
  • classes:可选,图片分类的类别数,仅当include_top=True并且不加载预训练权重时可用。

返回值

Keras 模型对象

参考文献

  • Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning:如果在研究中使用了InceptionV3,请引用该文

License

预训练权重基于the Apache License


MobileNet模型

keras.applications.mobilenet.MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000)

MobileNet网络,权重训练自ImageNet

该模型仅在TensorFlow后端均可使用,因此仅channels_last维度顺序可用。当需要以load_model()加载MobileNet时,需要在custom_object中传入relu6DepthwiseConv2D,即:

model = load_model('mobilenet.h5', custom_objects={'relu6': mobilenet.relu6,'DepthwiseConv2D': mobilenet.DepthwiseConv2D})

模型的默认输入尺寸时224x224

参数

  • include_top:是否保留顶层的全连接网络
  • weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重
  • input_tensor:可填入Keras tensor作为模型的图像输出tensor
  • input_shape:可选,仅当include_top=False有效,应为长为3的tuple,指明输入图片的shape,图片的宽高必须大于197,如(200,200,3)
  • pooling:当include_top=False时,该参数指定了池化方式。None代表不池化,最后一个卷积层的输出为4D张量。‘avg’代表全局平均池化,‘max’代表全局最大值池化。
  • classes:可选,图片分类的类别数,仅当include_top=True并且不加载预训练权重时可用。
  • alpha: 控制网络的宽度:
  • 如果alpha<1,则同比例的减少每层的滤波器个数
  • 如果alpha>1,则同比例增加每层的滤波器个数
  • 如果alpha=1,使用默认的滤波器个数
  • depth_multiplier:depthwise卷积的深度乘子,也称为(分辨率乘子)
  • dropout:dropout比例

返回值

Keras 模型对象

参考文献

  • MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications:如果在研究中使用了MobileNet,请引用该文

这篇关于#####好好好####keras之预训练模型Application的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/967673

相关文章

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

Spring AI Alibaba接入大模型时的依赖问题小结

《SpringAIAlibaba接入大模型时的依赖问题小结》文章介绍了如何在pom.xml文件中配置SpringAIAlibaba依赖,并提供了一个示例pom.xml文件,同时,建议将Maven仓... 目录(一)pom.XML文件:(二)application.yml配置文件(一)pom.xml文件:首

如何在本地部署 DeepSeek Janus Pro 文生图大模型

《如何在本地部署DeepSeekJanusPro文生图大模型》DeepSeekJanusPro模型在本地成功部署,支持图片理解和文生图功能,通过Gradio界面进行交互,展示了其强大的多模态处... 目录什么是 Janus Pro1. 安装 conda2. 创建 python 虚拟环境3. 克隆 janus

本地私有化部署DeepSeek模型的详细教程

《本地私有化部署DeepSeek模型的详细教程》DeepSeek模型是一种强大的语言模型,本地私有化部署可以让用户在自己的环境中安全、高效地使用该模型,避免数据传输到外部带来的安全风险,同时也能根据自... 目录一、引言二、环境准备(一)硬件要求(二)软件要求(三)创建虚拟环境三、安装依赖库四、获取 Dee

DeepSeek模型本地部署的详细教程

《DeepSeek模型本地部署的详细教程》DeepSeek作为一款开源且性能强大的大语言模型,提供了灵活的本地部署方案,让用户能够在本地环境中高效运行模型,同时保护数据隐私,在本地成功部署DeepSe... 目录一、环境准备(一)硬件需求(二)软件依赖二、安装Ollama三、下载并部署DeepSeek模型选

JAVA系统中Spring Boot应用程序的配置文件application.yml使用详解

《JAVA系统中SpringBoot应用程序的配置文件application.yml使用详解》:本文主要介绍JAVA系统中SpringBoot应用程序的配置文件application.yml的... 目录文件路径文件内容解释1. Server 配置2. Spring 配置3. Logging 配置4. Ma

Golang的CSP模型简介(最新推荐)

《Golang的CSP模型简介(最新推荐)》Golang采用了CSP(CommunicatingSequentialProcesses,通信顺序进程)并发模型,通过goroutine和channe... 目录前言一、介绍1. 什么是 CSP 模型2. Goroutine3. Channel4. Channe