def validate(self, epoch):
        self.network.eval()
        self.val_loss_metric.reset(epoch)
        self.val_acc_metric.reset(epoch)

        seed = randint(0, len(self.testloader) - 1)

        for i, (img, mask, label) in enumerate(self.testloader):
            img, mask, label = img.to(self.device), mask.to(
                self.device), label.to(self.device)
            net_mask, net_label = self.network(img)
            loss = self.loss(net_mask, net_label, mask, label)

            # Calculate predictions
            preds = predict(net_mask,
                            net_label,
                            score_type=self.cfg['test']['score_type'])
            targets = predict(mask,
                              label,
                              score_type=self.cfg['test']['score_type'])
            acc = calc_acc(preds, targets)
            # Update metrics
            self.val_loss_metric.update(loss.item())
            self.val_acc_metric.update(acc)

            if i == seed:
                add_images_tb(self.cfg, epoch, img, preds, targets,
                              self.writer)

        return self.val_acc_metric.avg
示例#2
0
    def test(self):
        self.load_model()

        self.network.eval()
        self.test_loss_metric.reset(0)
        self.test_acc_metric.reset(0)

        y_pred = np.array([])
        y_true = np.array([])

        with torch.no_grad():
            for i, (img, depth_map, label) in enumerate(self.valloader):
                # if i % 99 == 0:
                #     plt.imshow(img.numpy()[0, 0, :, :])
                img, depth_map, label = img.to(self.device), depth_map.to(
                    self.device), label.to(self.device)
                net_depth_map, _, _, _, _, _ = self.network(img)
                loss = self.criterion(net_depth_map, depth_map)

                preds, score = predict(net_depth_map)
                targets, _ = predict(depth_map)
                y_pred = np.append(y_pred, preds.to('cpu').numpy())
                y_true = np.append(y_true, label.to('cpu').numpy())
                accuracy = calc_accuracy(preds, targets)

                # Update metrics
                self.test_loss_metric.update(loss.item())
                self.test_acc_metric.update(accuracy)

            # print('loss: {}, acc: {}'.format(self.test_loss_metric.avg, self.test_acc_metric.avg))
        utils.print_metrics(y_true, y_pred)

        return y_pred
    def train_one_epoch(self, epoch):

        self.network.train()
        self.train_loss_metric.reset(epoch)
        self.train_acc_metric.reset(epoch)

        for i, (img, mask, label) in enumerate(self.trainloader):
            img, mask, label = img.to(self.device), mask.to(
                self.device), label.to(self.device)
            net_mask, net_label = self.network(img)
            self.optimizer.zero_grad()
            loss = self.loss(net_mask, net_label, mask, label)
            loss.backward()
            self.optimizer.step()

            # Calculate predictions
            preds = predict(net_mask,
                            net_label,
                            score_type=self.cfg['test']['score_type'])
            targets = predict(mask,
                              label,
                              score_type=self.cfg['test']['score_type'])
            acc = calc_acc(preds, targets)
            # Update metrics
            self.train_loss_metric.update(loss.item())
            self.train_acc_metric.update(acc)

            print('Epoch: {}, iter: {}, loss: {}, acc: {}'.format(
                epoch,
                epoch * len(self.trainloader) + i, self.train_loss_metric.avg,
                self.train_acc_metric.avg))
示例#4
0
    def validate(self, epoch):
        self.network.eval()
        self.val_loss_metric.reset(epoch)
        self.val_acc_metric.reset(epoch)

        seed = randint(0, len(self.valloader) - 1)
        with torch.no_grad():
            for i, (img, depth_map, label) in enumerate(self.valloader):
                img, depth_map, label = img.to(self.device), depth_map.to(
                    self.device), label.to(self.device)
                net_depth_map, _, _, _, _, _ = self.network(img)
                loss = self.criterion(net_depth_map, depth_map)

                preds, score = predict(net_depth_map)
                targets, _ = predict(depth_map)

                accuracy = calc_accuracy(preds, targets)

                # Update metrics
                self.val_loss_metric.update(loss.item())
                self.val_acc_metric.update(accuracy)

                if i == seed:
                    add_visualization_to_tensorboard(self.cfg, epoch, img,
                                                     preds, targets, score,
                                                     self.writer)

            return self.val_acc_metric.avg
示例#5
0
文件: trainer.py 项目: vjayd/fasmple
    def train_one_epoch(self, epoch):

        self.network.train()
        self.train_loss_metric.reset(epoch)
        self.train_acc_metric.reset(epoch)
        saved_name = os.path.join(self.cfg['output_dir'], '{}_{}.pth'.format(self.cfg['model']['base'], self.cfg['dataset']['name']))

        if os.path.exists(saved_name):
            checkpoint = torch.load(saved_name)
            self.network.load_state_dict(checkpoint['state_dict'])
            self.optimizer.load_state_dict(checkpoint['optimizer'])
            epoch_stored = checkpoint['epoch']
            print('Recent epoch ',epoch_stored)
            
        for i, (img, mask, label) in enumerate(self.trainloader):
            img, mask, label = img.to(self.device), mask.to(self.device), label.to(self.device)
            net_mask, net_label = self.network(img)
            self.optimizer.zero_grad()
            loss = self.loss(net_mask, net_label, mask, label)
            loss.backward()
            self.optimizer.step()

            # Calculate predictions
            preds, _ = predict(net_mask, net_label, score_type=self.cfg['test']['score_type'])
            targets, _ = predict(mask, label, score_type=self.cfg['test']['score_type'])
            acc = calc_acc(preds, targets)
            # Update metrics
            self.train_loss_metric.update(loss.item())
            self.train_acc_metric.update(acc)

        print('Epoch: {}, iter: {}, loss: {}, acc: {}'.format(epoch, 0, self.train_loss_metric.avg, self.train_acc_metric.avg))
示例#6
0
    def train_one_epoch(self, epoch):

        print(f'Epoch: {epoch}')

        self.network.train()
        self.train_loss_metric.reset(epoch)
        self.train_acc_metric.reset(epoch)

        y_pred = np.array([])
        y_true = np.array([])

        for i, (img, depth_map, label) in enumerate(self.trainloader):
            # image = img.cpu().detach().numpy()[0]
            # print(np.unique(image))
            # image = np.transpose(image, (1, 2, 0))
            # plt.imshow(image)
            # plt.title(str(label.cpu()[0]))
            # plt.show()

            img, depth_map, label = img.to(self.device), depth_map.to(
                self.device), label.to(self.device)
            net_depth_map, _, _, _, _, _ = self.network(img)

            # image = net_depth_map.cpu().detach().numpy()[0]
            # plt.imshow(image)
            # plt.title(str(label.cpu()[0]))
            # plt.colorbar()
            # plt.show()

            # print("label: ", label.cpu())

            self.optimizer.zero_grad()
            loss = self.criterion(net_depth_map, depth_map)
            loss.backward()
            self.optimizer.step()

            preds, _ = predict(net_depth_map)
            targets, _ = predict(depth_map)

            y_pred = np.append(y_pred, preds.to('cpu').numpy())
            y_true = np.append(y_true, label.to('cpu').numpy())

            accuracy = calc_accuracy(preds, targets)

            # Update metrics
            self.train_loss_metric.update(loss.item())
            self.train_acc_metric.update(accuracy)

            # print('Epoch: {}, iter: {}, loss: {}, acc: {}'.format(epoch, epoch * len(self.trainloader) + i,
            #                                                       self.train_loss_metric.avg,
            #                                                       self.train_acc_metric.avg))
        utils.print_metrics(y_true, y_pred)
        return self.train_acc_metric.avg, self.train_loss_metric.avg
    def train_one_epoch(self, epoch):

        self.network.train()
        self.train_loss_metric.reset(epoch)
        self.train_acc_metric.reset(epoch)

        for i, (img, depth_map, label) in enumerate(self.trainloader):
            img, depth_map, label = img.to(self.device), depth_map.to(self.device), label.to(self.device)
            net_depth_map, _, _, _, _, _ = self.network(img)
            self.optimizer.zero_grad()
            loss = self.criterion(net_depth_map, depth_map)
            loss.backward()
            self.optimizer.step()

            preds, _ = predict(net_depth_map)

            targets, _ = predict(depth_map)
            accuracy = calc_accuracy(preds, targets)

            # Update metrics
            self.train_loss_metric.update(loss.item())
            self.train_acc_metric.update(accuracy)

            print('Epoch: {}, iter: {}, loss: {}, acc: {}'.format(epoch, epoch * len(self.trainloader) + i, self.train_loss_metric.avg, self.train_acc_metric.avg))
示例#8
0
文件: trainer.py 项目: vjayd/fasmple
    def validate(self, epoch):
        self.network.eval()
        self.val_loss_metric.reset(epoch)
        self.val_acc_metric.reset(epoch)

        correct = 0
        total = 0
        tp , fp, tn, fn = 0, 0, 0, 0
        spoof = 0
        seed = randint(0, len(self.testloader)-1)
        
        for i, (img, mask, label) in enumerate(self.testloader):
            img, mask, label = img.to(self.device), mask.to(self.device), label.to(self.device)
            net_mask, net_label = self.network(img)
            loss = self.loss(net_mask, net_label, mask, label)

            # Calculate predictions
            preds, score = predict(net_mask, net_label, score_type=self.cfg['test']['score_type'])
            targets, _ = predict(mask, label, score_type=self.cfg['test']['score_type'])
            
            #####################################################################
            true=(targets==preds)
            false=(~true)
            pos=(preds==1)
            neg=(~pos)
            keep=(targets==0)
            tp+=(true*pos).sum().item()
            fp+=(false*pos*keep).sum().item()
            fn+=(false*neg*keep).sum().item()
            tn+=(true*neg).sum().item()
            
            # Calculate predictions
            #preds, _ = predict(net_mask, net_label, score_type=self.cfg['test']['score_type'])
            #targets, _ = predict(mask, label, score_type=self.cfg['test']['score_type'])
            #acc = calc_acc(predicted, label)
            
            
            
            
            
            
            acc = calc_acc(preds, targets)
            # Update metrics
            self.val_loss_metric.update(loss.item())
            self.val_acc_metric.update(acc)

            
#            if i == seed:
#                add_images_tb(self.cfg, epoch, img, preds, targets, score, self.writer)



        n_live = total-spoof
        n_spoof = spoof
        fn = n_spoof - tp
        fp = n_live - tn
        apcer = fn/n_spoof   #attack presentation classification error rates
        bpcer = fp/n_live 
        acer = (apcer+ bpcer) /2   #average classification error rate
        precision =  tp/(tp+fp) 
        recall =  tp/(tp+fn)
        
        
        print('Total live images : {},  Total spoof images : {}'.format(n_live, spoof))
        print('True positive :',tp, ' False positive :', fp, 'False Negative :', fn, 'True negative :', tn)
        print('APCER : {}, BPCER : {}, ACER :{}, Precision :{}, Recall :{} '.format(apcer, bpcer, acer, precision, recall))
        print('Accuracy of the network on the test images: %d %%' % (
            100 * correct / total))


        return self.val_acc_metric.avg