Keras可以使用的现有模型

这篇具有很好参考价值的文章主要介绍了Keras可以使用的现有模型。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

官网:https://keras.io/api/applications/

Keras可以使用的现有模型,【Keras】,【计算机视觉】,keras,人工智能,深度学习Keras可以使用的现有模型,【Keras】,【计算机视觉】,keras,人工智能,深度学习

一些使用的列子:

 ResNet50:分类预测

import keras
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(weights='imagenet')

img_path = 'elephant.jpg'
img = keras.utils.load_img(img_path, target_size=(224, 224))
x = keras.utils.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:用作特征提取器时,不需要最后的全连接层,所以实例化模型时参数 include_top=False

import keras
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np

model = VGG16(weights='imagenet', include_top=False)

img_path = 'elephant.jpg'
img = keras.utils.load_img(img_path, target_size=(224, 224))
x = keras.utils.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

features = model.predict(x)

网上例子解释:文章来源地址https://www.toymoban.com/news/detail-825599.html

from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import decode_predictions
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
from keras.utils.vis_utils import plot_model
import numpy as np

#加载VGG16模型,这里指定weights='imagenet'来自动下载并加载预训练的模型参数
model = VGG16(weights='imagenet', include_top=True);
image_path = 'E:/dog.jpg';
#加载图片,并且接企鹅为224*224
img = image.load_img(image_path, target_size=(224, 224));
#图片转为array格式
x = image.img_to_array(img)
#假设有一张灰度图,读取之后的shape是( 360, 480),而模型的输入要求是( 1, 360 , 480 ) 或者是 ( 360 , 480 ,1 ),
# 那么可以通过np.expand_dims(a,axis=0)或者np.expand_dims(a,axis=-1)将形状改变为满足模型的输入
x = np.expand_dims(x, axis=0)
#对数据进行归一化处理x/255
x = preprocess_input(x)

#放入模型预测
features = model.predict(x)
#decode_predictions() 用于解码ImageNet模型的预测结果,一般带两个参数:features表示输入批处理的预测,top默认是5,设置3表示返回3个概率最大的值
result = decode_predictions(features, top=3)

#展示结果,并且保存图片
plot_model(model, 'E:/model.png', show_shapes=True)
print(result)

VGG19:

from keras.applications.vgg19 import VGG19
from keras.applications.vgg19 import preprocess_input
from keras.models import Model
import numpy as np

base_model = VGG19(weights='imagenet')
model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)

img_path = 'elephant.jpg'
img = keras.utils.load_img(img_path, target_size=(224, 224))
x = keras.utils.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.models import Model
from keras.layers import Dense, GlobalAveragePooling2D

# 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(...)

# 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(...)

Build InceptionV3:自定义tensor,输入V3

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))

model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)

到了这里,关于Keras可以使用的现有模型的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 【计算机视觉】DINOv2(视觉大模型)代码使用和测试(完整的源代码)

    【计算机视觉】DINOv2(视觉大模型)代码使用和测试(完整的源代码)

    输出为: 命令是一个Git命令,用于克隆(Clone)名为\\\"dinov2\\\"的存储库。它使用了一个名为\\\"ghproxy.com\\\"的代理,用于加速GitHub的克隆操作。 我们需要切换为output的路径: 以下是代码的逐行中文解读: 这段代码的功能是对给定的图像进行一系列处理和特征提取,并使用PCA对特征进

    2024年02月16日
    浏览(12)
  • Azure 机器学习 - 使用自动化机器学习训练计算机视觉模型的数据架构

    Azure 机器学习 - 使用自动化机器学习训练计算机视觉模型的数据架构

    了解如何设置Azure Machine Learning JSONL 文件格式,以便在训练和推理期间在计算机视觉任务的自动化 ML 实验中使用数据。 关注TechLead,分享AI全维度知识。作者拥有10+年互联网服务架构、AI产品研发经验、团队管理经验,同济本复旦硕,复旦机器人智能实验室成员,阿里云认证的

    2024年02月05日
    浏览(14)
  • Azure 机器学习 - 使用 ONNX 对来自 AutoML 的计算机视觉模型进行预测

    Azure 机器学习 - 使用 ONNX 对来自 AutoML 的计算机视觉模型进行预测

    本文介绍如何使用 Open Neural Network Exchange (ONNX) 对从 Azure 机器学习中的自动机器学习 (AutoML) 生成的计算机视觉模型进行预测。 关注TechLead,分享AI全维度知识。作者拥有10+年互联网服务架构、AI产品研发经验、团队管理经验,同济本复旦硕,复旦机器人智能实验室成员,阿里云

    2024年02月05日
    浏览(10)
  • 计算机视觉:卷积核的参数可以通过反向传播学习到吗?

    计算机视觉:卷积核的参数可以通过反向传播学习到吗?

    在深度学习中,卷积神经网络(Convolutional Neural Networks, CNN)是一种常用的神经网络结构,其中卷积核是CNN的核心组件之一。卷积核是一个小矩阵,用于对输入数据进行卷积操作。卷积操作可以提取输入数据的特征,通过不同的卷积核可以提取不同的特征。   在前面课程中我

    2024年02月16日
    浏览(11)
  • Part1:使用 TensorFlow 和 Keras 的 NeRF计算机图形学和深度学习——计算机图形学世界中相机的工作原理

    Part1:使用 TensorFlow 和 Keras 的 NeRF计算机图形学和深度学习——计算机图形学世界中相机的工作原理

    是否有一种方法可以仅从一个场景多张不同视角的照片中捕获整个3D场景? 有。 NeRF:将场景表示为用于视图合成的神经辐射场中(NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis),Mildenhall等人(2020)的论文解答了这个问题。NeRF的更简单实现赢得了 TensorFlow社区聚光

    2024年02月07日
    浏览(16)
  • 计算机视觉基础知识(八)--点云模型

    计算机视觉基础知识(八)--点云模型

    三维图像 一种特殊的信息表达形式; 特征是表达的空间中有三个维度的数据; 是对一类信息的统称; 信息的表现形式: 深度图:以灰度表达物体与相机的距离 几何模型:由cad软件建立 点云模型:所有逆向工程设备都将物体采样为点云 和二维图像相比; 三维图像借助第三

    2024年01月25日
    浏览(12)
  • 【学习笔记】计算机视觉深度学习网络模型

    【学习笔记】计算机视觉深度学习网络模型

    这是本人学习计算机视觉CV领域深度学习模型的学习的一点点学习笔记,很多片子没有完成,可以作为学习的参考~

    2024年04月10日
    浏览(29)
  • 计算机视觉领域经典模型汇总(2023.09.08

    计算机视觉领域经典模型汇总(2023.09.08

    一、RCNN系列 1、RCNN RCNN是用于目标检测的经典方法,其核心思想是将目标检测任务分解为两个主要步骤:候选区域生成和目标分类。 候选区域生成:RCNN的第一步是生成可能包含目标的候选区域,RCNN使用传统的计算机视觉技术,特别是 选择性搜索(Selective Search)算法 ,这是一

    2024年02月09日
    浏览(15)
  • 数据增强:让计算机视觉模型更加智能和有效

    作者:禅与计算机程序设计艺术 引言 1.1. 背景介绍 随着计算机视觉技术的快速发展,各种数据增强技术也应运而生。数据增强技术可以有效地提高计算机视觉模型的智能和有效性,从而在众多应用场景中取得更好的表现。 1.2. 文章目的 本文旨在阐述数据增强技术在计算机视

    2024年02月08日
    浏览(10)
  • 计算机视觉:比SAM快50倍的分割一切视觉模型FastSAM

    计算机视觉:比SAM快50倍的分割一切视觉模型FastSAM

    目录 引言 1 FastSAM介绍 1.1 FastSAM诞生 1.2 模型算法 1.3 实验结果 2 FastSAM运行环境构建 2.1 conda环境构建 2.2 运行环境安装 2.3 模型下载 3 FastSAM运行 3.1 命令行运行 3.1.1 Everything mode  3.1.2 Text prompt 3.1.3 Box prompt (xywh) 3.1.4 Points prompt  3.2 通过代码调用 4 总结 MetaAI提出的能够“分割一切

    2024年02月11日
    浏览(12)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包