如何在暹罗神经网络中获得值0或1?

时间:2020-03-14 03:12:49

标签: model pytorch prediction siamese-network

我正在尝试使用暹罗神经网络。 在这里,我想比较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)

所以我得到了这样的分数

enter image description here

得分是-14.1640

基本上是暹罗语,如果图像相同,则产生的值为1,如果图像不同,则产生的值为0

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

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

1 个答案:

答案 0 :(得分:2)

要获得0到1之间的输出,您需要使用激活函数来转换您的值,以便将它们映射到概率。可以使用Sigmoid函数定义为: enter image description here

它返回范围(0,1)(不包括)的概率,其中值0

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

希望这会有所帮助!