Example #1
0
def main():
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    model = CDCNpp()
    model.load_state_dict(torch.load(args.model_path))
    model.eval()
    model = model.to(device)

    image = cv2.resize(cv2.imread(args.image_path), (256, 256))
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # normalize into [-1, 1]
    image = (image - 127.5) / 128
    # convert to tensor
    image = transforms.functional.to_tensor(image)
    image.unsqueeze_(0)
    image = image.to(device, dtype=torch.float)
    # Get outputs
    depth_map, embedding, cv_block1, cv_block2, cv_block3, input_image = model(
        image)
    # Save feature maps if you want
    FeatureMap2Heatmap(input_image, cv_block1, cv_block2, cv_block3, depth_map)
    liveness_score = torch.sum(depth_map)
    depth_map = depth_map.cpu().detach().numpy()
    print(f"Liveness Score: {liveness_score:.2f} Threshold = 148.90")
    prediction = "Real" if liveness_score > 148.90 else "Fake"
    print(f"Prediction: {prediction}")
Example #2
0
def train_test():
    # GPU  & log file  -->   if use DataParallel, please comment this command
    #os.environ["CUDA_VISIBLE_DEVICES"] = "%d" % (args.gpu)

    isExists = os.path.exists(args.log)
    if not isExists:
        os.makedirs(args.log)
    log_file = open(args.log + '/' + args.log + f'_log_{args.protocol}.txt',
                    'w')

    echo_batches = args.echo_batches

    print(f"SIW, {args.protocol}:\n ")

    log_file.write(f"SIW, {args.protocol}:\n ")
    log_file.flush()

    # load the network, load the pre-trained model in UCF101?
    finetune = args.finetune
    if finetune == True:
        print('finetune!\n')
        log_file.write('finetune!\n')
        log_file.flush()

        model = CDCNpp()
        #model = model.cuda()
        model = model.to(device[0])
        model = nn.DataParallel(model,
                                device_ids=device,
                                output_device=device[0])
        model.load_state_dict(torch.load('xxx.pkl'))

        lr = args.lr
        optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=0.00005)
        scheduler = optim.lr_scheduler.StepLR(optimizer,
                                              step_size=args.step_size,
                                              gamma=args.gamma)

    else:
        print('train from scratch!\n')
        log_file.write('train from scratch!\n')
        log_file.flush()

        #model = CDCN(basic_conv=Conv2d_cd, theta=0.7)
        model = CDCNpp(basic_conv=Conv2d_cd, theta=0.7)

        model = model.cuda()
        #model = model.to(device[0])
        #model = nn.DataParallel(model, device_ids=device, output_device=device[0])

        lr = args.lr
        optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=0.00005)
        scheduler = optim.lr_scheduler.StepLR(optimizer,
                                              step_size=args.step_size,
                                              gamma=args.gamma)

    print(model)

    criterion_absolute_loss = nn.MSELoss().cuda()
    criterion_contrastive_loss = Contrast_depth_loss().cuda()

    #bandpass_filter_numpy = build_bandpass_filter_numpy(30, 30)  # fs, order  # 61, 64

    ACER_save = 1.0

    for epoch in range(args.epochs):  # loop over the dataset multiple times
        scheduler.step()
        if (epoch + 1) % args.step_size == 0:
            lr *= args.gamma

        loss_absolute = AvgrageMeter()
        loss_contra = AvgrageMeter()
        #top5 = utils.AvgrageMeter()

        ###########################################
        '''                train             '''
        ###########################################
        model.train()

        # load random 16-frame clip data every epoch
        #train_data = Spoofing_train(train_list, train_image_dir, map_dir, transform=transforms.Compose([RandomErasing(), RandomHorizontalFlip(),  ToTensor(), Cutout(), Normaliztion()]))
        train_data = SiwDataset(
            "train",
            dir_path="/storage/alperen/sodecsapp/datasets/SiW/lists",
            protocol=args.protocol,
            transform=transforms.Compose([
                RandomErasing(),
                RandomHorizontalFlip(),
                ToTensor(),
                Cutout(),
                Normaliztion()
            ]))
        #train_data = SodecDataset(dataset_type="train",dir_path="dataset_with_margin",protocol=args.protocol, transform=transforms.Compose([RandomErasing(), RandomHorizontalFlip(),  ToTensor(), Normaliztion()]))
        dataloader_train = DataLoader(train_data,
                                      batch_size=args.batchsize,
                                      shuffle=True,
                                      num_workers=4)

        for i, sample_batched in enumerate(dataloader_train):
            # get the inputs
            inputs, map_label, spoof_label = sample_batched['image_x'].cuda(
            ), sample_batched['map_x'].cuda(
            ), sample_batched['spoofing_label'].cuda()

            optimizer.zero_grad()

            #pdb.set_trace()

            # forward + backward + optimize
            map_x, embedding, x_Block1, x_Block2, x_Block3, x_input = model(
                inputs)

            absolute_loss = criterion_absolute_loss(map_x, map_label)
            contrastive_loss = criterion_contrastive_loss(map_x, map_label)

            loss = absolute_loss + contrastive_loss
            #loss =  absolute_loss

            loss.backward()

            optimizer.step()

            n = inputs.size(0)
            loss_absolute.update(absolute_loss.data, n)
            loss_contra.update(contrastive_loss.data, n)

            if i % echo_batches == echo_batches - 1:  # print every 50 mini-batches

                # visualization
                FeatureMap2Heatmap(x_input, x_Block1, x_Block2, x_Block3,
                                   map_x)

                # log written
                print(
                    'epoch:%d, mini-batch:%3d, lr=%f, Absolute_Depth_loss= %.4f, Contrastive_Depth_loss= %.4f'
                    %
                    (epoch + 1, i + 1, lr, loss_absolute.avg, loss_contra.avg))
                #log_file.write('epoch:%d, mini-batch:%3d, lr=%f, Absolute_Depth_loss= %.4f, Contrastive_Depth_loss= %.4f \n' % (epoch + 1, i + 1, lr, loss_absolute.avg, loss_contra.avg))
                #log_file.flush()

            #break

        # whole epoch average
        print(
            'epoch:%d, Train:  Absolute_Depth_loss= %.4f, Contrastive_Depth_loss= %.4f\n'
            % (epoch + 1, loss_absolute.avg, loss_contra.avg))
        log_file.write(
            'epoch:%d, Train: Absolute_Depth_loss= %.4f, Contrastive_Depth_loss= %.4f \n'
            % (epoch + 1, loss_absolute.avg, loss_contra.avg))
        log_file.flush()

        #### validation/test
        """
        if epoch <300:
             epoch_test = 300   
        else:
            epoch_test = 20   
        """
        epoch_test = 1
        if epoch % epoch_test == epoch_test - 1:  # test every 5 epochs
            model.eval()

            with torch.no_grad():
                ###########################################
                '''                val             '''
                ###########################################
                # val for threshold
                #val_data = Spoofing_valtest(val_list, val_image_dir, val_map_dir, transform=transforms.Compose([Normaliztion_valtest(), ToTensor_valtest()]))
                val_data = SiwDataset(
                    "dev",
                    dir_path="/storage/alperen/sodecsapp/datasets/SiW/lists",
                    protocol=args.protocol,
                    transform=transforms.Compose(
                        [Normaliztion_valtest(),
                         ToTensor_valtest()]))
                #val_data = SodecDataset(dataset_type="test",dir_path="dataset_with_margin",protocol=args.protocol,transform=transforms.Compose([Normaliztion_valtest(), ToTensor_valtest()]))
                dataloader_val = DataLoader(val_data,
                                            batch_size=args.batchsize,
                                            shuffle=False,
                                            num_workers=4)

                map_score_list = []

                for i, sample_batched in enumerate(dataloader_val):
                    # get the inputs
                    inputs, spoof_label = sample_batched['image_x'].cuda(
                    ), sample_batched['spoofing_label'].cuda()
                    val_maps = sample_batched['map_x'].cuda(
                    )  # binary map from PRNet

                    optimizer.zero_grad()

                    #pdb.set_trace()
                    map_score = 0.0
                    map_x, embedding, x_Block1, x_Block2, x_Block3, x_input = model(
                        inputs)
                    score_norm = torch.sum(map_x, (1, 2))

                    for j, score in enumerate(score_norm):
                        map_score_list.append('{} {}\n'.format(
                            score.item(), spoof_label[j].item()))

                    #pdb.set_trace()
                map_score_val_filename = args.log + '/' + args.protocol + '_map_score_val.txt'
                with open(map_score_val_filename, 'w') as file:
                    file.writelines(map_score_list)

                ###########################################
                '''                test             '''
                ##########################################
                # test for ACC
                #test_data = Spoofing_valtest(test_list, test_image_dir, test_map_dir, transform=transforms.Compose([Normaliztion_valtest(), ToTensor_valtest()]))
                test_data = SiwDataset(
                    "eval",
                    dir_path="/storage/alperen/sodecsapp/datasets/SiW/lists",
                    protocol=args.protocol,
                    transform=transforms.Compose(
                        [Normaliztion_valtest(),
                         ToTensor_valtest()]))
                #test_data = SodecDataset(dataset_type="test",dir_path="dataset_with_margin",protocol=args.protocol,transform=transforms.Compose([Normaliztion_valtest(), ToTensor_valtest()]))
                dataloader_test = DataLoader(test_data,
                                             batch_size=args.batchsize,
                                             shuffle=False,
                                             num_workers=4)

                map_score_list = []

                for i, sample_batched in enumerate(dataloader_test):
                    # get the inputs
                    inputs, spoof_label = sample_batched['image_x'].cuda(
                    ), sample_batched['spoofing_label'].cuda()
                    test_maps = sample_batched['map_x'].cuda(
                    )  # binary map from PRNet

                    optimizer.zero_grad()

                    #pdb.set_trace()
                    map_score = 0.0
                    map_x, embedding, x_Block1, x_Block2, x_Block3, x_input = model(
                        inputs)
                    score_norm = torch.sum(map_x, (1, 2))

                    for j, score in enumerate(score_norm):
                        map_score_list.append('{} {}\n'.format(
                            score.item(), spoof_label[j].item()))

                map_score_test_filename = args.log + '/' + args.protocol + '_map_score_test.txt'
                with open(map_score_test_filename, 'w') as file:
                    file.writelines(map_score_list)

                #############################################################
                #       performance measurement both val and test
                #############################################################
                val_threshold, test_threshold, val_ACC, val_ACER, test_ACC, test_APCER, test_BPCER, test_ACER, test_ACER_test_threshold = performances(
                    map_score_val_filename, map_score_test_filename)

                print(
                    'epoch:%d, Val:  val_threshold= %.4f, val_ACC= %.4f, val_ACER= %.4f'
                    % (epoch + 1, val_threshold, val_ACC, val_ACER))
                log_file.write(
                    '\n epoch:%d, Val:  val_threshold= %.4f, val_ACC= %.4f, val_ACER= %.4f \n'
                    % (epoch + 1, val_threshold, val_ACC, val_ACER))

                print(
                    'epoch:%d, Test:  ACC= %.4f, APCER= %.4f, BPCER= %.4f, ACER= %.4f'
                    % (epoch + 1, test_ACC, test_APCER, test_BPCER, test_ACER))
                #print('epoch:%d, Test:  test_threshold= %.4f, test_ACER_test_threshold= %.4f\n' % (epoch + 1, test_threshold, test_ACER_test_threshold))
                log_file.write(
                    'epoch:%d, Test:  ACC= %.4f, APCER= %.4f, BPCER= %.4f, ACER= %.4f \n'
                    % (epoch + 1, test_ACC, test_APCER, test_BPCER, test_ACER))
                #log_file.write('epoch:%d, Test:  test_threshold= %.4f, test_ACER_test_threshold= %.4f \n\n' % (epoch + 1, test_threshold, test_ACER_test_threshold))
                log_file.flush()

        if epoch > 0:
            #save the model until the next improvement
            torch.save(model.state_dict(),
                       args.log + '/' + args.log + '_%d.pkl' % (epoch + 1))

    print('Finished Training')
    log_file.close()