背景
好了,现在开尝试预测新的图片,并且让vgg16模型判断是狗还是猫吧。
声明:整个数据和代码来自于b站,链接:使用pytorch框架手把手教你利用VGG16网络编写猫狗分类程序_哔哩哔哩_bilibili
预测
1、导包
from torchvision import transforms from PIL import Image import matplotlib.pyplot as plt import torch import torch.nn.functional as F from net import vgg16
2、设置新照片的路径
test_pth=r'.\img.png'#设置可以检测的图像 test=Image.open(test_pth)
3、处理图片:图片变成tensor
transform=transforms.Compose([transforms.Resize((224,224)),transforms.ToTensor()]) image=transform(test)
-
transforms.Compose:这是一个类,可以将多个变换操作组合在一起。当你需要对数据执行一系列变换时,就会用到它。它接受一个变换函数列表作为参数。
4、设置设备
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")#CPU与GPU的选择
5、加载网络(vgg16net)
net =vgg16()#输入网络
6、加载模型(权重模型)
model=torch.load(r".\DogandCat5.pth",map_location=device)#已训练完成的结果权重输入 net.load_state_dict(mod

最低0.47元/天 解锁文章
294

被折叠的 条评论
为什么被折叠?



