def main(): device = torch.device('cuda:9' if torch.cuda.is_available else 'cpu') train_dataset_dir = tdatasets.ImageFolder('images/all') train_dataset = SiameseNetworkDataset(imageFolderDataset = train_dataset_dir, transform = transforms.Compose([transforms.Resize((100,100)), transforms.ToTensor()])) vis_dataloader = DataLoader(train_dataset, shuffle=False, num_workers=0, batch_size=10) # 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()) # # 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()) net = SiameseNetwork() criterion = ContrastiveLoss() optimizer = optim.Adam(net.parameters(),lr = 0.0005 ) loss_vals = [] ''' Training Starts ''' print('Training started') for epoch in range(10): loss_epoch = 0 for i, data in enumerate(vis_dataloader,0): img_0, img_1, label = data print(i, label) # img_0, img_1, label = img_0.to(device), img_1.to(device), label.to(device) optimizer.zero_grad() out_0, out_1 = net(img_0, img_1) loss = criterion(out_0, out_1, label) loss_epoch += loss.item() loss.backward() optimizer.step() loss_vals.append(loss_epoch) print('Epoch',str(epoch+1), str(loss_epoch)) print('Epoch done') torch.save(net.state_dict(), 'siamese.pt') print('Training completed') plt.plot(loss_vals) plt.savefig('loss_siamese.png') # ****************************** Training ends *************************************** '''
), should_invert=False ) train_dataloader = DataLoader(siamese_dataset, shuffle=True, num_workers=8, batch_size=Config.train_batch_size) net = SiameseNetwork().cuda() print(net) criterion = ContrastiveLoss() optimizer = optim.Adam(net.parameters(),lr = 0.0005 ) 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()) torch.save(net.state_dict(), "./model.pth") show_plot(counter,loss_history)
def main(): device = torch.device('cuda:9' if torch.cuda.is_available else 'cpu') train_dataset_dir = tdatasets.ImageFolder('images/train') train_dataset = SiameseNetworkDataset(imageFolderDataset = train_dataset_dir, transform = transforms.Compose([transforms.Resize((100,100)), transforms.ToTensor()])) vis_dataloader = DataLoader(train_dataset, shuffle=False, num_workers=0, batch_size=1) # 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()) # # 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()) net = SiameseNetwork() criterion = ContrastiveLoss() optimizer = optim.Adam(net.parameters(),lr = 0.0005 ) loss_vals = [] net.to(device) ''' Training Starts ''' print('Training started') for epoch in range(1000): loss_epoch = 0 for i, data in enumerate(vis_dataloader,0): img_0, img_1, label = data img_0, img_1, label = img_0.to(device), img_1.to(device), label.to(device) optimizer.zero_grad() out_0, out_1 = net(img_0, img_1) loss = criterion(out_0, out_1, label) loss_epoch += loss.item() loss.backward() optimizer.step() loss_vals.append(loss_epoch) print('Epoch',str(epoch+1), str(loss_epoch)) print('Training completed') plt.plot(loss_vals) plt.savefig('loss_siamese.png') # ****************************** Training ends *************************************** ''' Testing starts ''' test_dataset_dir = tdatasets.ImageFolder('images/test') net.load_state_dict(torch.load('siamese.pt')) test_dataset = SiameseNetworkDataset(imageFolderDataset = test_dataset_dir, transform = transforms.Compose([transforms.Resize((100,100)), transforms.ToTensor()])) test_dataloader = DataLoader(test_dataset, shuffle=True, num_workers=2, batch_size=1) print('Testing starts') correct = 0 total = 0 test_img_sub = None for i, data in enumerate(test_dataloader, 0): img_0, img_1, label = data if test_img_sub is None: test_img_sub = img_0 #concat = torch.cat((test_img_sub, img_1), 0) concat = torch.cat((img_0, img_1), 0) test_img_sub, img_1, label = test_img_sub.to(device), img_1.to(device), label.to(device) img_0, img_1, label = img_0.to(device), img_1.to(device), label.to(device) out_0, out_1 = net(img_0, img_1) dist = F.pairwise_distance(out_0, out_1) if dist <= 0.5 and label == 0: correct = correct + 1 elif label == 1: correct = correct + 1 else: pass total = total + 1 imshow(torchvision.utils.make_grid(concat),i,'Dissimilarity: {:.2f}'.format(dist.item()), True) test_img_sub = test_img_sub.cpu() # dist = dist.cpu().detach() # print(dist.numpy()) # dist = torch.sigmoid(dist) # print(dist.numpy()) print(correct/total) print('Testing complete') torch.save(net.state_dict(), 'siamese_blog.pt')
time_ = datetime.datetime.now() for iter_, data in enumerate(validate_dataloader, 0): iter1_, img0, iter2_, img1, label = data img0, img1, label = img0.cuda(), img1.cuda(), label.cuda() with torch.no_grad(): output1, output2 = model(img0, img1) loss_contrastive = criterion(output1, output2, label) eval_loss += loss_contrastive.item() nb_eval_steps += 1 eval_loss = eval_loss / nb_eval_steps elapsed = datetime.datetime.now() - time_ print('evaluation done^^') print('[epoch {}] elapsed: {}'.format(epoch + 1, elapsed)) if best_loss > eval_loss: best_loss = eval_loss # save the best model!!!! print('achieve best loss!') best_dir = os.path.join('./model_save/', 'best_model/') os.makedirs(best_dir, exist_ok=True) tmp_state = { 'model': model.state_dict(), 'optimizer': optimizer.state_dict() } torch.save(tmp_state, os.path.join(best_dir, str(epoch + 1) + '.pth'))