Example #1
0
    def evaluate(self,
                 data_loaders=None,
                 logger=None,
                 gen_images=False,
                 score_acc=None):
        assert isinstance(score_acc, ScoreAccumulator)
        for loader in data_loaders:
            img_obj = loader.dataset.image_objects[0]
            x, y = img_obj.working_arr.shape[0], img_obj.working_arr.shape[1]
            predicted_img = torch.FloatTensor(x, y).fill_(0).to(self.device)

            for i, data in enumerate(loader, 1):
                inputs, labels = data['inputs'].to(
                    self.device).float(), data['labels'].to(
                        self.device).float()
                clip_ix = data['clip_ix'].to(self.device).int()

                outputs = F.softmax(self.model(inputs), 1)
                _, predicted = torch.max(outputs, 1)
                predicted_map = outputs[:, 1, :, :]

                for j in range(predicted_map.shape[0]):
                    p, q, r, s = clip_ix[j]
                    predicted_img[p:q, r:s] = predicted[j]

                print('Batch: ', i, end='\r')

            img_score = ScoreAccumulator()
            if gen_images:  #### Test mode
                predicted_img = predicted_img.cpu().numpy() * 255
                predicted_img[img_obj.extra['fill_in'] == 1] = 255

                img_score.reset().add_array(predicted_img,
                                            img_obj.ground_truth)
                ### Only save scores for test images############################

                self.conf['acc'].accumulate(img_score)  # Global score
                prf1a = img_score.get_prfa()
                print(img_obj.file_name, ' PRF1A', prf1a)
                self.flush(
                    logger,
                    ','.join(str(x) for x in [img_obj.file_name] + prf1a))
                #################################################################

                IMG.fromarray(np.array(predicted_img, dtype=np.uint8)).save(
                    os.path.join(self.log_dir,
                                 img_obj.file_name.split('.')[0] + '.png'))
            else:  #### Validation mode
                img_score.reset().add_tensor(
                    predicted_img,
                    torch.FloatTensor(img_obj.extra['gt_mid']).to(self.device))
                score_acc.accumulate(img_score)
                prf1a = img_score.get_prfa()
                print(img_obj.file_name, ' PRF1A', prf1a)
                self.flush(
                    logger,
                    ','.join(str(x) for x in [img_obj.file_name] + prf1a))
Example #2
0
 def validation(self, epoch=None, validation_loader=None, epoch_run=None):
     score_acc = ScoreAccumulator()
     self.evaluate(data_loaders=validation_loader, logger=self.val_logger, gen_images=False, score_acc=score_acc)
     # epoch_run(epoch=epoch, data_loader=validation_loader, logger=self.val_logger, score_acc=score_acc)
     p, r, f1, a = score_acc.get_prfa()
     print('>>> PRF1: ', [p, r, f1, a])
     self._save_if_better(score=f1)
Example #3
0
    def _eval(self,
              data_loaders=None,
              logger=None,
              gen_images=False,
              score_acc=None):
        assert isinstance(score_acc, ScoreAccumulator)
        with torch.no_grad():
            for loader in data_loaders:
                img_obj = loader.dataset.image_objects[0]
                x, y = img_obj.working_arr.shape[0], img_obj.working_arr.shape[
                    1]
                predicted_img = torch.FloatTensor(x,
                                                  y).fill_(0).to(self.device)
                map_img = torch.FloatTensor(x, y).fill_(0).to(self.device)

                gt = torch.FloatTensor(img_obj.ground_truth).to(self.device)

                for i, data in enumerate(loader, 1):
                    inputs, labels = data['inputs'].to(
                        self.device).float(), data['labels'].to(
                            self.device).float()
                    clip_ix = data['clip_ix'].to(self.device).int()

                    outputs = self.model(inputs)
                    _, predicted = torch.max(outputs, 1)
                    predicted_map = outputs[:, 1, :, :]

                    for j in range(predicted_map.shape[0]):
                        p, q, r, s = clip_ix[j]
                        predicted_img[p:q, r:s] = predicted[j]
                        map_img[p:q, r:s] = predicted_map[j]
                    print('Batch: ', i, end='\r')

                img_score = ScoreAccumulator()
                map_img = torch.exp(map_img) * 255
                predicted_img = predicted_img * 255

                if gen_images:
                    map_img = map_img.cpu().numpy()
                    predicted_img = predicted_img.cpu().numpy()
                    img_score.add_array(predicted_img, img_obj.ground_truth)
                    self.conf['acc'].accumulate(img_score)  # Global score

                    IMG.fromarray(np.array(
                        predicted_img, dtype=np.uint8)).save(
                            os.path.join(
                                self.log_dir, 'pred_' +
                                img_obj.file_name.split('.')[0] + '.png'))
                    IMG.fromarray(np.array(map_img, dtype=np.uint8)).save(
                        os.path.join(self.log_dir,
                                     img_obj.file_name.split('.')[0] + '.png'))
                else:
                    img_score.add_tensor(predicted_img, gt)
                    score_acc.accumulate(img_score)

                prf1a = img_score.get_prfa()
                print(img_obj.file_name, ' PRF1A', prf1a)
                self.flush(
                    logger,
                    ','.join(str(x) for x in [img_obj.file_name] + prf1a))
        self._save_if_better(score=score_acc.get_prfa()[2])