1

我正在尝试使用连体神经网络。这里我想比较 2 种类型的图像并得到分数的结果,这是测试模型产生分数的代码

在这种情况下,我使用 pytorch

model = Siamese()

# Load state_dict
model.load_state_dict(torch.load('/Users/tania/Desktop/TA/model/model-batch-1001.pth'))

# Create the preprocessing transformation
from torchvision import transforms
transforms = transforms.ToTensor()

# load image(s)
from PIL import Image
x1 = Image.open('table.PNG')
x2 = Image.open('table.PNG')
# Transform

x1 = transforms(x1)
x2 = transforms(x2)

x1 = torch.stack([x1])
x2 = torch.stack([x2])

model.eval()

# Get prediction
output = model(x1,x2)
print (output)

所以我得到了这样的分数,

在此处输入图像描述

分数是-14.1640

基本上是连体的,如果图像相同,则产生值 1,如果不同,则产生值 0

如何获得 0 或 1 的值,以便知道图像是否相同?

请帮助我,我是神经网络的新手

4

1 回答 1

2

要获得介于 0 和 1 之间的输出,您需要使用激活函数转换值,以便将它们映射到概率。这可以通过 Sigmoid 函数来完成,定义为: 在此处输入图像描述

它返回范围为 (0,1)(不包括)的概率,其中值 0 < y < 0.5 可以解释为负标签,而值 0.5 <= y < 1 可以解释为正标签。在 PyTorch 中,这可以实现:

output = model(x1,x2)
output = torch.sigmoid(output)

希望这可以帮助!

于 2020-03-14T04:24:55.143 回答