11-pytorch-使用自己的数据集测试

b站小土堆pytorch教程学习笔记

在这里插入图片描述

import torch
import torchvision
from PIL import Image
from torch import nn

img_path= '../imgs/dog.png'
image=Image.open(img_path)
print(image)
# image=image.convert('RGB')

transform=torchvision.transforms.Compose([torchvision.transforms.Resize((32,32)),
                                          torchvision.transforms.ToTensor()])
image=transform(image)
print(image.shape)

#加载模型
class Han(nn.Module):
    def __init__(self):
        super(Han, self).__init__()
        self.model = nn.Sequential(
            nn.Conv2d(3, 32, kernel_size=5, stride=1, padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, kernel_size=5, stride=1, padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, kernel_size=5, stride=1, padding=2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(64 * 4 * 4, 64),
            nn.Linear(64, 10)
        )

    def forward(self, x):
        x = self.model(x)
        return x

model=torch.load('../han_9.pth',map_location=torch.device('cpu'))#将GPU上运行的模型转移到CPU
print(model)

#对图片进行reshap
image=torch.reshape(image,(-1,3,32,32))

#将模型转化为测试类型
model.eval()
with torch.no_grad():#节约内存
    output=model(image)
print(output)


print(output.argmax(1))

<PIL.PngImagePlugin.PngImageFile image mode=RGB size=306x283 at 0x250B0006EE0>
torch.Size([3, 32, 32])
Han(
(model): Sequential(
(0): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(4): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Flatten(start_dim=1, end_dim=-1)
(7): Linear(in_features=1024, out_features=64, bias=True)
(8): Linear(in_features=64, out_features=10, bias=True)
)
)
tensor([[-2.0302, -0.6256, 0.7483, 1.5765, 0.2651, 2.2243, -0.7037, -0.5262,
-1.4401, -0.6563]])
tensor([5])
Process finished with exit code 0

预测正确!
在这里插入图片描述

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DGCNN(Dynamic Graph Convolutional Neural Networks)是一种用于图分类的神经网络模型,通过动态图卷积实现对图结构的特征提取和分类。下面是使用DGCNN模型的PyTorch库的方法。 首先,安装所需的Python库和PyTorch。然后,从GitHub上下载并安装DGCNN-PyTorch库。可以使用以下命令: ``` git clone https://github.com/muhanzhang/pytorch_DGCNN.git cd pytorch_DGCNN pip install -r requirements.txt ``` 下载并准备数据集,将数据集存储在合适的路径下。接下来,根据实际的数据集和训练需要调整hyperparameters文件中的参数。 运行`main.py`文件进行训练和测试。可以使用以下命令: ``` python main.py ``` 在命令行中,可以设置一些参数,如数据集路径、模型保存路径、训练时的批次大小、迭代次数等。训练过程将根据设置的参数进行训练,并在测试集上评估模型性能。 另外,如果需要使用预训练的模型进行图分类任务,可以通过以下命令加载预训练模型并进行预测: ``` python from model import DGCNN import torch device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = DGCNN().to(device) model.load_state_dict(torch.load('saved_model.pth')) model.eval() # 假设有一个图样本data output = model(data) # 进行预测 ``` 以上是使用DGCNN-PyTorch库的基本方法。根据实际任务和数据集,您可能需要进行适当的调整和修改。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值