使用 PyTorch 模型预测新样本

Predict new samples with PyTorch model

我是神经网络的新手,我已经教过我的模型,现在我想测试它。我在 google 的帮助下写了一段代码,但它不起作用。 问题是我不明白我从哪里得到第 4 个维度。

代码如下:

import matplotlib.pyplot as plt
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

transform = transforms.Compose([
    transforms.Resize(32),
    transforms.CenterCrop(32),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

main_path = 'D:/RTU/dataset/ready_dataset_2classes'
train_data_path = main_path + '/train'
#test_data_path = main_path + '/test'
weigths_path = 'D:/RTU/dataset/weights_done/weights_noise_original037-97%.pt'

train_data = torchvision.datasets.ImageFolder(root=train_data_path, transform=transform)

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # convolutional layer (sees 32x32x3 image tensor)
        self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
        # convolutional layer (sees 16x16x16 tensor)
        self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
        # convolutional layer (sees 8x8x32 tensor)
        self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
        # max pooling layer
        self.pool = nn.MaxPool2d(2, 2)
        # linear layer (64 * 4 * 4 -> 500)
        self.fc1 = nn.Linear(64 * 4 * 4, 500)
        # linear layer (500 -> 10)
        self.fc2 = nn.Linear(500, 250)
        self.fc3 = nn.Linear(250, 2)  
        # dropout layer (p=0.25)
        self.dropout = nn.Dropout(0.25) #0.25

    def forward(self, x):
        # add sequence of convolutional and max pooling layers
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        # flatten image input
        x = x.view(-1, 64 * 4 * 4)
        # add dropout layer
        x = self.dropout(x)
        # add 1st hidden layer, with relu activation function
        x = F.relu(self.fc1(x))
        # add dropout layer
        x = self.dropout(x)
        # add 2nd hidden layer, with relu activation function
        x = F.relu(self.fc2(x))
        x = self.dropout(x)
        x = self.fc3(x)       
        return x

# Disable grad
with torch.no_grad():
    
    # Retrieve item
    index = 1
    item = train_data[index]
    image = item[0]
    true_target = item[1]
    
    # Loading the saved model
    mlp = Net()
    optimizer = optim.SGD(mlp.parameters(), lr=0.01)
    epoch=5
    valid_loss_min = np.Inf
    checkpoint = torch.load(weigths_path , map_location=torch.device('cpu'))
    mlp.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    epoch = checkpoint['epoch']
    valid_loss_min = checkpoint['valid_loss_min']
    mlp.eval()
    # Generate prediction
    prediction = mlp(image)
    
    # Predicted class value using argmax
    predicted_class = np.argmax(prediction)
    
    # Reshape image
    #image = image.reshape(28, 28, 1)
    
    # Show result
    plt.imshow(image, cmap='gray')
    plt.title(f'Prediction: {predicted_class} - Actual target: {true_target}')
    plt.show()

代码似乎一直工作到“mlp.eval()”,然后我收到一个错误 4 维权重的预期 4 维输入 [16, 3, 3, 3 ],却得到了大小为 [3, 32, 32] 的 3 维输入,而不是 。 我做错了什么? Error

训练神经网络时,您是在向模型提供小批量的输入数据。事实上,在 Pytorch 中编写 Layers 时甚至没有明确指定,如果您查看文档,here 可以看到 Layers 接收 4D 数组

with N corresponding to batch size and C to number of channels, here 3 because you are using RGB images

因此,在训练模型后测试时,测试数据的构建方式应与输入网络的方式相同。

因此,如果您想将 1 张图像提供给您的网络,您必须适当地对其进行整形

myimage.reshape(-1,3,32,32)
print(myimage.shape)
#(1,3,32,33)