mercredi 2 décembre 2020

How to change code from random.choice to normal. It shouldn't consider randomly (Siamese Neural Network)

There are two folders, in which one folder contains one image and another folder contains one image. I'm trying to compare the distances of the two images (Siamese Neural Network). But due to random.choices code sometimes it's comparing with the same image present in one folder (It's not comparing with another image from the 2nd folder). I'm attaching the code below

def __getitem__(self,index):
        img0_tuple = random.choice(self.imageFolderDataset.imgs)
        img1_tuple = random.choice(self.imageFolderDataset.imgs)
        #we need to make sure approx 50% of images are in the same class
        should_get_same_class = random.randint(0,1) 
        if should_get_same_class:
            while True:
                #keep looping till the same class image is found
                img1_tuple = random.choice(self.imageFolderDataset.imgs) 
                if img0_tuple[1]==img1_tuple[1]:
                    break
        else:
            while True:
                #keep looping till a different class image is found
                
                img1_tuple = random.choice(self.imageFolderDataset.imgs) 
                if img0_tuple[1] !=img1_tuple[1]:
                    break

        img0 = Image.open(img0_tuple[0])
        img1 = Image.open(img1_tuple[0])
        img0 = img0.convert("L")
        img1 = img1.convert("L")
        
        if self.should_invert:
            img0 = PIL.ImageOps.invert(img0)
            img1 = PIL.ImageOps.invert(img1)

        if self.transform is not None:
            img0 = self.transform(img0)
            img1 = self.transform(img1)
        
        return img0, img1 , torch.from_numpy(np.array([int(img1_tuple[1]!=img0_tuple[1])],dtype=np.float32))
    
    def __len__(self):
        return len(self.imageFolderDataset.imgs)

Followed by code:

testing_dir1 = '/content/drive/My Drive/Signature Dissimilarity/Forged_Signature_Verification/processed_dataset/training1/'
folder_dataset_test = dset.ImageFolder(root=testing_dir1)
siamese_dataset = InferenceSiameseNetworkDataset(imageFolderDataset=folder_dataset_test,
                                        transform=transforms.Compose([transforms.Resize((100,100)),
                                                                      transforms.ToTensor()
                                                                      ])
                                       ,should_invert=False)

test_dataloader = DataLoader(siamese_dataset,num_workers=6,batch_size=1,shuffle=False)
dataiter = iter(test_dataloader)
x0,_,_ = next(dataiter)

for i in range(2):
  _,x1,label2 = next(dataiter)
  concatenated = torch.cat((x0,x1),0)
  
  output1,output2 = net(Variable(x0).cuda(),Variable(x1).cuda())
  euclidean_distance = F.pairwise_distance(output1, output2)
  imshow(torchvision.utils.make_grid(concatenated),'Dissimilarity: {:.2f}'.format(euclidean_distance.item()))
  dis = 'Dissimilarity: {:.2f}'.format(euclidean_distance.item())
  dis1 = dis
  dis1 = dis1.replace("Dissimilarity:", "").replace(" ", "")
  print(dis)
  if float(dis1) < 0.5:
    print("It's Same Signature")
  else:
    print("It's Forged Signature")

Where testing_dir1 is the path which contains two folders with one image. The problem is that the image from the first folder is sometimes comparing with itself and sometimes it's comparing with the image present in the second folder. Due to random.choice it's happening. The output should be the image in the first folder should compare with the image in the second folder.




Aucun commentaire:

Enregistrer un commentaire