예제 #1
0
def handle_data():
    args = common_args.get_args()
    device = torch.device(1)

    # Receive data from front-end
    print("received")
    if 'file1' not in request.files:
        # flash('No file part')
        # print("test point 1")
        # return redirect(request.url)
        csvFile = open("store_name.csv", "r")
        reader = csv.reader(csvFile)
        for item in reader:
            print(item)
        filename1 = item[0]
        filename2 = item[1]
        csvFile.close()
    else:
        file1 = request.files['file1']
        file2 = request.files['file2']
        if file1.filename == '' or file2.filename == '':
            flash('No selected file')
            print("test point 2")
            return redirect(request.url)
        # saved_file1 = file1.filename
        # saved_file2 = file2.filename
        filename1 = secure_filename(file1.filename)
        filename2 = secure_filename(file2.filename)
        csvFile = open("store_name.csv", "w")
        writer = csv.writer(csvFile)
        writer.writerow([filename1, filename2])
        csvFile.close()
    print(filename1)
    print(filename2)
    # image_text = "Image 1: " + filename1 + "; Image 2: " + filename2
    image_text = filename1 + "; " + filename2
    method = int(request.values.get("method"))
    resolution1 = int(request.values.get("resolution1"))
    resolution2 = int(request.values.get("resolution2"))
    # Deal with received data, including images, model and resolution
    p = filename1.rsplit('_', 1)
    file_path1 = p[0] + '/' + p[0] + '_' + p[1]
    p = filename2.rsplit('_', 1)
    file_path2 = p[0] + '/' + p[0] + '_' + p[1]
    file_path1 = os.path.join(app.config['UPLOAD_FOLDER'], file_path1)
    file_path2 = os.path.join(app.config['UPLOAD_FOLDER'], file_path2)
    print(file_path1)
    print(filename1)
    resized_face1 = cv2.imread(file_path1)
    resized_face2 = cv2.imread(file_path2)
    print("check")
    width1, height1, channel1 = resized_face1.shape
    width2, height2, channel2 = resized_face2.shape
    print("check")
    # cv2.resize(face, (int(96 / self.N), int(112 / self.N)))
    resized_face1 = cv2.resize(
        resized_face1, (int(width1 / resolution1), int(height1 / resolution1)))
    print("check")
    resized_face1 = cv2.resize(resized_face1, (width1, height1))
    print("check")
    resized_face2 = cv2.resize(
        resized_face2, (int(width2 / resolution2), int(height2 / resolution2)))
    resized_face2 = cv2.resize(resized_face2, (width2, height2))
    resized_face_path1 = os.path.join(app.config['UPLOAD_FOLDER'],
                                      'resized_face1.jpg')
    resized_face_path2 = os.path.join(app.config['UPLOAD_FOLDER'],
                                      'resized_face2.jpg')
    cv2.imwrite(resized_face_path1, resized_face1)
    cv2.imwrite(resized_face_path2, resized_face2)
    aligned_face1 = get_alignedface(file_path1, filename1)
    aligned_face2 = get_alignedface(file_path2, filename2)
    aligned_face1 = cv2.resize(aligned_face1,
                               (int(96 / resolution1), int(112 / resolution1)))
    aligned_face2 = cv2.resize(aligned_face2,
                               (int(96 / resolution2), int(112 / resolution2)))
    aligned_face1 = cv2.resize(aligned_face1, (96, 112))
    aligned_face2 = cv2.resize(aligned_face2, (96, 112))
    aligned_face_path1 = os.path.join(app.config['UPLOAD_FOLDER'],
                                      'aligned_face1.jpg')
    aligned_face_path2 = os.path.join(app.config['UPLOAD_FOLDER'],
                                      'aligned_face2.jpg')
    cv2.imwrite(aligned_face_path1, aligned_face1)
    cv2.imwrite(aligned_face_path2, aligned_face2)

    # setup model
    if method == 1:
        model_root1 = '/home/tangjiawei/project/fyp/saved/30000_net_backbone.pth'
        model_root2 = '/home/tangjiawei/PycharmProjects/FYP_Face_Verification/saved/1000_lr_spherenet_df7.pth'
        net1 = nerual_net.spherenet(20, args.feature_dim, args.use_pool,
                                    args.use_dropout)
        net1.load_state_dict(torch.load(model_root1))
        net1.to(device)
        net1.eval()
        net2 = nerual_net.spherenet(20, args.feature_dim, args.use_pool,
                                    args.use_dropout)
        net2.load_state_dict(torch.load(model_root2))
        net2.to(device)
        net2.eval()
        feature1 = net1(
            face_ToTensor(aligned_face1).to(device).view([1, 3, 112, 96]))
        feature2 = net1(
            face_ToTensor(aligned_face2).to(device).view([1, 3, 112, 96]))
        feature3 = net2(
            face_ToTensor(aligned_face1).to(device).view([1, 3, 112, 96]))
        feature4 = net2(
            face_ToTensor(aligned_face2).to(device).view([1, 3, 112, 96]))
        model = "Resnet"
        thd1 = 0.29
        thd2 = 0.29
    if method == 2:
        model_root1 = '/home/tangjiawei/project/fyp/saved/ALEX_NET_HR_11_April.pkl'
        model_root2 = '/home/tangjiawei/PycharmProjects/RadimoicDeepFeatureExtraction/model/1000_lr_alexnet_df9.pth'
        net1 = models.alexnet(pretrained=True)
        net1.classifier[6] = torch.nn.Linear(4096, 10559)
        net1.load_state_dict(torch.load(model_root1))
        net1.to(device)
        net1.eval()
        net2 = models.alexnet(pretrained=True)
        net2.classifier[6] = torch.nn.Linear(4096, 10559)
        net2.load_state_dict(torch.load(model_root2))
        net2.to(device)
        aligned_face1 = cv2.resize(aligned_face1, (224, 224))
        aligned_face2 = cv2.resize(aligned_face2, (224, 224))
        net2.eval()
        feature1 = net1(
            face_ToTensor(aligned_face1).to(device).view([1, 3, 224, 224]))
        feature2 = net1(
            face_ToTensor(aligned_face2).to(device).view([1, 3, 224, 224]))
        feature3 = net2(
            face_ToTensor(aligned_face1).to(device).view([1, 3, 224, 224]))
        feature4 = net2(
            face_ToTensor(aligned_face2).to(device).view([1, 3, 224, 224]))
        model = "Alexnet"
        thd1 = 0.68
        thd2 = 0.68
    # get features from received images, comparing two images --> same or different
    # feature1 = net1(face_ToTensor(aligned_face1).to(device).view([1, 3, 112, 96]))
    # feature2 = net1(face_ToTensor(aligned_face2).to(device).view([1, 3, 112, 96]))
    print(feature1.shape)
    print(feature2.shape)
    score1 = torch.nn.CosineSimilarity()(feature1, feature2)
    score1 = score1.cpu().detach().numpy().reshape(-1, 1)
    score1 = score1.item()
    score2 = torch.nn.CosineSimilarity()(feature3, feature4)
    score2 = score2.cpu().detach().numpy().reshape(-1, 1)
    score2 = score2.item()
    print(score1)
    print(score2)
    if score1 >= thd1:
        text_to_html1 = model + " verification result: Same Person"
        confindence_text1 = "Score is {:.2f}".format(score1 * 100)
    else:
        text_to_html1 = model + " verification result: Different Persons"
        confindence_text1 = "Score is {:.2f}".format(score1 * 100)
    if score2 >= thd2:
        text_to_html2 = "LR-" + model + " verification result: Same Person"
        confindence_text2 = "Score is {:.2f}".format(score2 * 100)
    else:
        text_to_html2 = "LR-" + model + " verification result: Different Persons"
        confindence_text2 = "Score is {:.2f}".format(score2 * 100)



    return render_template('demo.html', input_face1=resized_face_path1, input_face2=resized_face_path2, aligned_face1=aligned_face_path1,\
                           aligned_face2=aligned_face_path2,Result_test1=text_to_html1, Confidence1=confindence_text1,\
                           Result_test2=text_to_html2, Confidence2=confindence_text2, image_text=image_text)
logging.basicConfig(filename='FYP_Resnet.log', level=logging.INFO)
hr_model_root = '/home/tangjiawei/PycharmProjects/FYP_Face_Verification/30000_net_backbone_asoftmax_64.pth'
lr_model_root = '/home/tangjiawei/project/fyp/saved/30000_net_backbone.pth'
if __name__ == '__main__':
    args = common_args.get_args()

    # setup device and dataloader
    device = torch.device(1)

    print(device)
    dataloader = get_loader('webface',
                            batch_size=128,
                            N=args.downsampling_factor)

    # Setup network Spherenet10_HR
    hr_spherenet = nerual_net.spherenet(64, args.feature_dim, args.use_pool,
                                        args.use_dropout)
    hr_spherenet.load_state_dict(torch.load(hr_model_root))
    hr_spherenet.to(device)

    # # Setup network Spherenet20_HR
    # num_layers = 20
    # hr_spherenet = nerual_net.spherenet(num_layers, args.feature_dim, args.use_pool, args.use_dropout)
    # hr_spherenet.load_state_dict(torch.load(model_root))
    # hr_spherenet.to(device)

    # Setup network Spherenet20_lR
    lr_spherenet = nerual_net.spherenet(20, args.feature_dim, args.use_pool,
                                        args.use_dropout)
    lr_spherenet.load_state_dict(torch.load(lr_model_root))
    lr_spherenet.to(device)
예제 #3
0
    downsampling_factor = 7
    args = common_args.get_args()
    device = torch.device(1)
    print(device)

    # # Setup network Spherenet20_lR
    # num_layers = 20
    # lr_spherenet_test = nerual_net.spherenet(num_layers, args.feature_dim, args.use_pool, args.use_dropout)
    # lr_spherenet_test.load_state_dict(
    #     torch.load('/home/tangjiawei/PycharmProjects/FYP_Face_Verification/saved/2000_lr_spherenet_df9.pth'))
    # lr_spherenet_test.to(device)
    #
    # for i in range(16):
    #     lfw_verification_for_validate.run(lr_spherenet_test, feature_dim=512, device=device, N=(i + 1))

    spherenet_64 = nerual_net.spherenet(20, args.feature_dim, args.use_pool,
                                        args.use_dropout)
    spherenet_64.load_state_dict(torch.load(model_root))
    spherenet_64.to(device)
    logging.info('test sphereface backbone img1_lr img2_hr')
    for i in range(16):
        lfw_verification.run(spherenet_64,
                             feature_dim=512,
                             device=device,
                             N=(i + 1))

# import logging
# import nerual_net
# import pandas as pd
# import cv2
# import numpy as np
# from matlab_cp2tform import get_similarity_transform_for_cv2
예제 #4
0
import lfw_verification
import common_args

logging.basicConfig(filename='FYP_Resnet.log', level=logging.INFO)
model_root = '/home/tangjiawei/project/fyp/saved/30000_net_backbone.pth'
if __name__ == '__main__':
    args = common_args.get_args()

    # setup device and dataloader
    device = torch.device(1)
    print(device)
    dataloader = get_loader('webface', batch_size=256, N=args.downsampling_factor)

    # Setup network Spherenet20_HR
    num_layers = 20
    hr_spherenet = nerual_net.spherenet(num_layers, args.feature_dim, args.use_pool, args.use_dropout)
    hr_spherenet.load_state_dict(torch.load(model_root))
    hr_spherenet.to(device)

    # Setup network Spherenet20_lR
    lr_spherenet = nerual_net.spherenet(num_layers, args.feature_dim, args.use_pool, args.use_dropout)
    lr_spherenet.load_state_dict(torch.load(model_root))
    lr_spherenet.to(device)

    # setup criterion and optimizer
    criterion_lr = torch.nn.MSELoss()
    optimizer = optim.Adam(lr_spherenet.parameters(),
                           lr=0.0003,
                           weight_decay=0.0005,
                           betas=(0.9, 0.999),
                           amsgrad=True)