2026a

# vgg16


Vgg16 卷积神经网络

函数库: TyDeepLearning

# 语法

net = vgg16()

# 说明

net = vgg16() 创建一个 vgg16 卷积神经网络。

vgg16 模型基于论文《Very Deep Convolutional Networks for Large-Scale Image Recognition》提出.示例

TIP

# 示例

利用 vgg16 对图像进行分类预测

加载预训练网络 vgg16。

using TyDeepLearning
using TyImages
using TyPlot
set_backend(:torch)
network = vgg16()

导入一张图像用于分类预测。

pkg_dir = pkgdir(TyDeepLearning)
source_path = pkg_dir * "/examples/Images/PreTrained/peppers.png"
image = imread(source_path)
figure(1)
imshow(image)

对图像进行格式处理,从而适用于 vgg16 进行分类预测。

I = TyImages.imresize(image, [224 224])
I = reshape(I, (1, size(I)...))
I = permutedims(I, (1, 4, 2, 3))
I = I ./ 255

vgg16 对图像进行分类预测。

label = TyDeepLearning.predict(network, I)
layer = softmaxLayer()
label = TyDeepLearning.predict(layer, label)
index = sortperm(label[1, :]; rev=true)

将图像类别索引与图像对应的类别名称进行一一对应。

file = pkg_dir * "/examples/Images/PreTrained/imagenet_classes.txt"
f = open(file)
class_name = readlines(f)
close(f)
top_index = index[1:5]
classname_top = class_name[top_index]
score_top = label[1, top_index]

绘制图像。

I = permutedims(I, (1, 3, 4, 2))
I = I[1, :, :, :]
figure(2)
imshow(I)
topprob = string(100 * score_top[1])
title(string(classname_top[1], ",", topprob, "%"))
绘制前五个预测情况的直方图。
figure(3)
barh(score_top)
xlim = [0, 1]
title("Top 5 Predictions")
xlabel("Probability")
yticklabels(classname_top)

# 输出参数

net - vgg16
网络对象

输出为一个 vgg16 预训练神经网络。

# 另请参阅

predict | softmaxLayer