我已经使用this教程成功地训练和测试了我的模型,并且我想使用单个图像来测试我的模型。这是我的代码
import torchvision.datasets as dset
import torchvision.transforms as transforms
from torch.utils.data import DataLoader,Dataset
import matplotlib.pyplot as plt
import torchvision.utils
import numpy as np
import random
from PIL import Image
import torch
from torch.autograd import Variable
import PIL.ImageOps
import torch.nn as nn
from torch import optim
import torch.nn.functional as F
import os
class Config():
training_dir = "./data/faces/training/"
testing_dir = "./data/faces/testing/"
train_batch_size = 80
train_number_epochs = 100
class SiameseNetworkDataset(Dataset):
...
class SiameseNetwork(nn.Module):
...
class ContrastiveLoss(torch.nn.Module):
...
if __name__=='__main__':
net = SiameseNetwork().cuda()
net.load_state_dict(torch.load("model.pt"))
img0 = Image.open(os.path.join('data', 'faces', 'testing', 's5', '2.png'))
img1 = Image.open(os.path.join('data', 'faces', 'testing', 's5', '1.png'))
img0 = img0.convert("L")
img1 = img1.convert("L")
img0 = PIL.ImageOps.invert(img0)
img1 = PIL.ImageOps.invert(img1)
transform=transforms.Compose([transforms.Resize((100,100)),
transforms.ToTensor()
])
img0 = transform(img0)
img1 = transform(img1)
img0 = img0.cuda()
img1 = img1.cuda()
output1,output2 = net(Variable(img0).cuda(),Variable(img1).cuda()) //The error occurred here
euclidean_distance = F.pairwise_distance(output1, output2)
print(euclidean_distance.cpu().data.numpy())
我有以下错误:AssertionError: 3D tensors expect 2 values for padding
。我不明白这是怎么回事,因为我使用了与SiameseNetworkDataset中相同的预处理