<jupyter_start><jupyter_text>One Shot Learning with Siamese NetworksThis is the jupyter notebook that accompanies  ImportsAll the imports are defined here<jupyter_code>%matplotlib inline
import torchvision
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
from SiameseNetworks import SiameseNetwork,SiameseNetworkDataset,ContrastiveLoss<jupyter_output><empty_output><jupyter_text>Helper functionsSet of helper functions<jupyter_code>def imshow(img,text=None,should_save=False):
    npimg = img.numpy()
    plt.axis("off")
    if text:
        plt.text(75, 8, text, style='italic',fontweight='bold',
            bbox={'facecolor':'white', 'alpha':0.8, 'pad':10})
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()    

def show_plot(iteration,loss):
    plt.plot(iteration,loss)
    plt.show()<jupyter_output><empty_output><jupyter_text>Configuration ClassA simple class to manage configuration<jupyter_code>class Config():
    training_dir = "./data/flower_data/train/"
    testing_dir = "./data/flower_data/valid/"
    train_batch_size = 64
    train_number_epochs = 5<jupyter_output><empty_output><jupyter_text>Using Image Folder Dataset<jupyter_code>folder_dataset = dset.ImageFolder(root=Config.training_dir)
siamese_dataset = SiameseNetworkDataset(imageFolderDataset=folder_dataset,
                                        transform=transforms.Compose([transforms.Resize(100),
                                                               transforms.RandomResizedCrop(100),
                                                               transforms.RandomHorizontalFlip(),
                                                               transforms.RandomRotation(10),
                                                               transforms.ToTensor(),
                                                               transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])
                                       ,should_invert=False)<jupyter_output><empty_output><jupyter_text>Visualising some of the dataThe top row and the bottom row of any column is one pair. The 0s and 1s correspond to the column of the image.1 indiciates dissimilar, and 0 indicates similar.<jupyter_code>vis_dataloader = DataLoader(siamese_dataset,
                        shuffle=True,
                        num_workers=8,
                        batch_size=8)
dataiter = iter(vis_dataloader)


example_batch = next(dataiter)
concatenated = torch.cat((example_batch[0],example_batch[1]),0)
imshow(torchvision.utils.make_grid(concatenated))
print(example_batch[2].numpy())<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).<jupyter_text>Training Time!<jupyter_code>train_dataloader = DataLoader(siamese_dataset,
                        shuffle=True,
                        num_workers=8,
                        batch_size=Config.train_batch_size)
net = SiameseNetwork().cuda()
criterion = ContrastiveLoss()
optimizer = optim.Adam(net.parameters(),lr = 0.001 )
counter = []
loss_history = [] 
iteration_number= 0
for epoch in range(0,Config.train_number_epochs):
    for i, data in enumerate(train_dataloader,0):
        img0, img1 , label = data
        img0, img1 , label = img0.cuda(), img1.cuda() , label.cuda()
        optimizer.zero_grad()
        output1,output2 = net(img0,img1)
        loss_contrastive = criterion(output1,output2,label)
        loss_contrastive.backward()
        optimizer.step()
        if i %10 == 0 :
            print("Epoch number {}\n Current loss {}\n".format(epoch,loss_contrastive.item()))
            iteration_number +=10
            counter.append(iteration_number)
            loss_history.append(loss_contrastive.item())
show_plot(counter,loss_history)
torch.save(net.state_dict(), 'model_augmented.pt')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09502154140737194, 'fraction_numerical': 0.01771182383915749, 'mean_word_length': 2.837465564738292, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '322036', 'license': 'MIT', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1207, 'n_words': 249, 'path': 'Siamese-networks-train.ipynb', 'repo_name': 'IsraelAbebe/Image-Similarity-with-Siamese-Networks-in-Pytorch', 'size': '4178'}