How to compare one picture to all data test in siamese neural network?
I've been build siamese neural network using pytorch. But I've just test it by inserting 2 pictures and calculate the similarity score, where 0 says that picture is different and 1 says picture is same. 

import numpy as np
import os, sys
from PIL import Image
dir_name = "/Users/tania/Desktop/Aksara/Compare" #this should contain 26 images only
X = []
for i in os.listdir(dir_name):
    if ".PNG" in i:
        X.append(torch.from_numpy(np.array(Image.open("./Compare/" + i))))

x1 = np.array(Image.open("/Users/tania/Desktop/Aksara/TEST/Ba/B/B.PNG"))
x1 = transforms(x1)
x1 = torch.from_numpy(x1)

#x1 = torch.stack([x1])

closest = 0.0  #highest similarity
closest_letter_idx = 0  #index of closest letter 0=A, 1=B, ...
cnt = 0

for i in X:
    output = model(x1,i) #assuming x1 is your input image
    output = torch.sigmoid(output)
    if output &gt; closest:
        closest_letter_idx = cnt
        closest = output
    cnt += 1



Both pictures are different, so the output

  File "test.py", line 83, in &lt;module&gt;
    X.append(torch.from_numpy(Image.open("./Compare/" + i)))
TypeError: expected np.ndarray (got PngImageFile)


this is the directory


Yes there is a way, you could use the softmax function:

output = torch.softmax(output)


This returns a tensor of 26 values, each corresponding to the probability that the image corresponds to each of the 26 classes. Hence, the tensor sums to 1 (100%). 

However, this method is suitable for classification tasks, as opposed to Siamese Networks. Siamese networks compare between inputs, instead of sorting inputs into classes. From your question, it seems like you're trying to compare 1 picture with 26 others. You could loop over all the 26 samples to compare with, compute &amp; save the similarity score for each, and output the maximum value (that is if you don't want to modify your model):

dir_name = '/Aksara/Compare' #this should contain 26 images only
X = []
for i in os.listdir(dir_name):
    if ".PNG" in i:
        X.append(torch.from_numpy(np.array(Image.open("./Compare/" + i))))

x1 = np.array(Image.open("test.PNG"))
#do your transformations on x1
x1 = torch.from_numpy(x1)
closest = 0.0  #highest similarity
closest_letter_idx = 0  #index of closest letter 0=A, 1=B, ...
cnt = 0

for i in X:
    output = model(x1,i) #assuming x1 is your input image
    output = torch.sigmoid(output)
    if output &gt; closest:
        closest_letter_idx = cnt
        closest = output
    cnt += 1
print(closest_letter_idx)

