示例#1
0
def main():
    global plotter
    plotter = VisdomLinePlotter(env_name=config.visdom_name)

    # instantiate model and initialize weights
    model = SiameseNetwork()
    if config.cuda:
        model.cuda()

    optimizer = create_optimizer(model, config.lr)

    # optionally resume from a checkpoint
    if config.resume:
        if os.path.isfile(config.resume):
            print('=> loading checkpoint {}'.format(config.resume))
            checkpoint = torch.load(config.resume)
            config.start_epoch = checkpoint['epoch']
            checkpoint = torch.load(config.resume)
            model.load_state_dict(checkpoint['state_dict'])
        else:
            print('=> no checkpoint found at {}'.format(config.resume))

    start = config.start_epoch
    end = start + config.epochs

    for epoch in range(start, end):
        train(train_loader, model, optimizer, epoch)
def get_siamese_model(nclasses):
    device = torch.device("cuda")
    model = SiameseNetwork()
    # model.classifier.classifier = nn.Sequential()
    checkpoint = torch.load('./model/siamese/net_59.pth')
    model.load_state_dict(checkpoint['model_state_dict'])
    model.to(device)
    model.eval()
    return model
from model import SiameseNetwork
from utils import SiameseNetworkDataset, imshow

opt = config.args()
folder_dataset_test = torchvision.datasets.ImageFolder(root=opt.testing_dir)
# 定义图像dataset
transform_test = transforms.Compose([transforms.Resize((100, 100)),
                                     transforms.ToTensor()])
siamese_dataset_test = SiameseNetworkDataset(imageFolderDataset=folder_dataset_test,
                                             transform=transform_test,
                                             should_invert=False)

# 定义图像dataloader
test_dataloader = DataLoader(siamese_dataset_test,
                             shuffle=True,
                             batch_size=1)

net = SiameseNetwork().cuda()
net.load_state_dict(torch.load('checkpoint/siameseNet49.pth'))
# 生成对比图像
dataiter = iter(test_dataloader)
x0, _, _ = next(dataiter)

for i in range(10):
    _, x1, label2 = next(dataiter)
    concatenated = torch.cat((x0, x1), 0)
    output1, output2 = net(x0.cuda(), x1.cuda())
    euclidean_distance = F.pairwise_distance(output1, output2)
    imshow(torchvision.utils.make_grid(concatenated), 'Dissimilarity: {:.2f}'.format(euclidean_distance.item()))
示例#4
0
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')
示例#5
0
from flask import Flask, request, Response, jsonify
from http import HTTPStatus
from model import SiameseNetwork
import json
import torch

from utils import cosine_similarity
import numpy as np
import pandas as pd

device = "cpu"

app = Flask(__name__)

model = SiameseNetwork().to(device)
model.load_state_dict(
    torch.load("./model2.pt", map_location=torch.device(device)))
model.eval()

A = 1


@app.route("/a")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"


@app.route('/b', methods=['POST'])
def map_recomend():
    request_data = json.loads(request.data)["user"]

    latitude_0 = request_data[0]["latitude_0"]
示例#6
0
from torch import optim
import torch.nn.functional as F

from config import Config
from utils import imshow,show_plot
from data_reader import SiameseNetworkDataset
from model import SiameseNetwork
from loss import ContrastiveLoss

folder_dataset_test = dset.ImageFolder(root=Config.testing_dir)
siamese_dataset = SiameseNetworkDataset(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=True)
dataiter = iter(test_dataloader)
x0,_,_ = next(dataiter)
model = SiameseNetwork().cuda()
model.load_state_dict(torch.load("./model.pth"))
model.eval()
for i in range(10):
    _,x1,label2 = next(dataiter)
    concatenated = torch.cat((x0,x1),0)
    
    output1,output2 = model(Variable(x0).cuda(),Variable(x1).cuda())
    euclidean_distance = F.pairwise_distance(output1, output2)
    imshow(torchvision.utils.make_grid(concatenated),"output/"+str(i),'Dissimilarity: {:.2f}'.format(euclidean_distance.item()))

示例#7
0
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=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 = []
    '''
    Training Starts
    '''
    print('Training started')
    # for epoch in range(100):
    #    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')
    #
    # torch.save(net.state_dict(), 'siamese.pt')

    # ****************************** Training ends ***************************************
    '''
    Testing starts
    '''
    net.load_state_dict(torch.load('siamese.pt'))
    test_dataset = SiameseTestDataset(train_dataset_dir, \
                                 transform=transforms.Compose([transforms.Resize((100, 100)), transforms.ToTensor()]))
    test_vis_dataloader = DataLoader(test_dataset,
                                     shuffle=False,
                                     num_workers=0,
                                     batch_size=1)

    train_dataset_dir = tdatasets.ImageFolder('images/all')
    train_dataset = FacesDataset(train_dataset_dir, \
                                 transform=transforms.Compose([transforms.Resize((100, 100)), transforms.ToTensor()]))

    _, test = split_train_val(train_dataset)
    test_dataloader = DataLoader(test,
                                 shuffle=False,
                                 num_workers=0,
                                 batch_size=1)
    correct = 0
    total = 0
    for i, data in enumerate(test_dataloader, 0):
        total += 1
        img_1, labels = data
        min_dist = float("inf")
        pred = -1
        print('Testing begins', i)
        for j, data_test_vis in enumerate(test_vis_dataloader, 0):
            img_0 = data_test_vis
            out_0, out_1 = net(img_0, img_1)
            dist = F.pairwise_distance(out_0, out_1)
            if min_dist > dist:
                min_dist = dist
                pred = j
        if pred == labels.item():
            correct += 1
        print('Testing ends', i, pred)

    print('Accuracy: ', str(correct / total))