# swinv2


Swin_transformerv2 神经网络

函数库: TyDeepLearning

# 语法

net = swinv2()

# 说明

net = swinv2() 创建一个 swin_transformerv2 神经网络。

swin_transformerv2 模型基于论文《Swin Transformer V2: Scaling Up Capacity and Resolution》提出.示例

TIP

# 示例

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

加载预训练网络 swinv2。

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

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

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

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

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

swinv2 对图像进行分类预测。

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 - swinv2
网络对象

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

# 另请参阅

predict | softmaxLayer