示例#1
0
 def test(self, num, use_crf=False):
     if use_crf: from tools.crf_process import crf
     avg_mae, img_num = 0.0, len(self.test_dataset)
     avg_prec, avg_recall = torch.zeros(num), torch.zeros(num)
     with torch.no_grad():
         for i, (img, labels) in enumerate(self.test_dataset):
             images = self.transform(img).unsqueeze(0)
             labels = labels.unsqueeze(0)
             shape = labels.size()[2:]
             images = images.to(self.device)
             prob_pred = self.net(images)
             prob_pred = torch.mean(torch.cat([prob_pred[i] for i in self.select], dim=1), dim=1, keepdim=True)
             prob_pred = F.interpolate(prob_pred, size=shape, mode='bilinear', align_corners=True).cpu().data
             if use_crf:
                 prob_pred = crf(img, prob_pred.numpy(), to_tensor=True)
             mae = self.eval_mae(prob_pred, labels)
             prec, recall = self.eval_pr(prob_pred, labels, num)
             print("[%d] mae: %.4f" % (i, mae))
             print("[%d] mae: %.4f" % (i, mae), file=self.test_output)
             avg_mae += mae
             avg_prec, avg_recall = avg_prec + prec, avg_recall + recall
     avg_mae, avg_prec, avg_recall = avg_mae / img_num, avg_prec / img_num, avg_recall / img_num
     score = (1 + self.beta ** 2) * avg_prec * avg_recall / (self.beta ** 2 * avg_prec + avg_recall)
     score[score != score] = 0  # delete the nan
     print('average mae: %.4f, max fmeasure: %.4f' % (avg_mae, score.max()))
     print('average mae: %.4f, max fmeasure: %.4f' % (avg_mae, score.max()), file=self.test_output)
示例#2
0
    def test(self, num, use_crf=False):
        if use_crf: from tools.crf_process import crf
        avg_mae, img_num = 0.0, len(self.test_dataset)
        avg_prec, avg_recall = torch.zeros(num), torch.zeros(num)
        with torch.no_grad():
            for i, (img, labels) in enumerate(self.test_dataset):
                #img.show()
                images = self.transform(img).unsqueeze(0)

                #print(images.shape)
                if (images.shape != torch.Size([1, 3, 256, 256])):
                    continue
                labels = labels.unsqueeze(0)
                shape = labels.size()[2:]
                images = images.to(self.device)
                prob_pred = self.net(images)
                if (self.mode == 1):
                    prob_pred = torch.mean(torch.cat(
                        [prob_pred[i] for i in self.select], dim=1),
                                           dim=1,
                                           keepdim=True)
                    prob_pred = F.interpolate(prob_pred,
                                              size=shape,
                                              mode='bilinear',
                                              align_corners=True).cpu().data

                else:
                    #prob_pred = torch.mean(torch.cat([prob_pred[i] for i in [1,2,3,6,7,7]],dim=1), dim=1, keepdim=True)
                    prob_pred = prob_pred[-1]
                    prob_pred = F.interpolate(prob_pred,
                                              size=shape,
                                              mode='bilinear',
                                              align_corners=True).cpu().data
                if use_crf:
                    prob_pred = crf(img, prob_pred.numpy(), to_tensor=True)
                mae = self.eval_mae(prob_pred, labels)
                prec, recall = self.eval_pr(prob_pred, labels, num)
                print("[%d] mae: %.4f" % (i, mae))
                print("[%d] mae: %.4f" % (i, mae), file=self.test_output)

                #********To present the hard cases*******************************8
                """
                if (mae>0.2):
                    img.show()
                    ss = prob_pred[0][0].cpu().numpy()
                    ss = 256 * ss
                    ims1 = Image.fromarray(ss)
                    ims1.show()
                """
                avg_mae += mae
                avg_prec, avg_recall = avg_prec + prec, avg_recall + recall
        avg_mae, avg_prec, avg_recall = avg_mae / img_num, avg_prec / img_num, avg_recall / img_num
        score = (1 + self.beta**2) * avg_prec * avg_recall / (
            self.beta**2 * avg_prec + avg_recall)
        score[score != score] = 0
        print('average mae: %.4f, max fmeasure: %.4f' % (avg_mae, score.max()))
        print('average mae: %.4f, max fmeasure: %.4f' % (avg_mae, score.max()),
              file=self.test_output)
示例#3
0
 def test(self, num, use_crf=False, epoch=None):
     if use_crf: from tools.crf_process import crf
     avg_mae, img_num = 0.0, 0.0
     avg_prec, avg_recall = torch.zeros(num), torch.zeros(num)
     with torch.no_grad():
         for i, (img, labels, bg, fg, name) in enumerate(self.test_dataset):
             images = self.transform(img).unsqueeze(0)
             labels = self.t_transform(labels).unsqueeze(0)
             shape = labels.size()[2:]
             images = images.cuda()
             prob_pred = self.net(images, mode='test')
             bg_pred = torch.mean(torch.cat([prob_pred[i+7] for i in self.select], dim=1), dim=1, keepdim=True)
             bg_pred = (bg_pred > 0.5).float()
             prob_pred = torch.mean(torch.cat([prob_pred[i] for i in self.select], dim=1), dim=1, keepdim=True)
             
             prob_pred = F.interpolate(prob_pred, size=shape, mode='bilinear', align_corners=True).cpu().data
             bg_pred = F.interpolate(bg_pred, size=shape, mode='nearest').cpu().data.numpy()
             fork_bg, fork_fg = Bwdist(bg_pred)
             if use_crf:
                 prob_pred = crf(img, prob_pred.numpy(), to_tensor=True)
             if not os.path.exists('{}/visualize_pred{}/'.format(self.visual_save_fold, epoch)):
                 os.mkdir('{}/visualize_pred{}/'.format(self.visual_save_fold, epoch))
             img_save = prob_pred.numpy()
             img_save = img_save.reshape(-1, img_save.shape[2], img_save.shape[3]).transpose(1,2,0) * 255
             cv2.imwrite('{}/visualize_pred{}/{}'.format(self.visual_save_fold, epoch, name), img_save.astype(np.uint8))
             # print('save visualize_pred{}/{} done.'.format(name, epoch))
             if not os.path.exists('{}/visualize_bg{}/'.format(self.visual_save_fold, epoch)):
                 os.mkdir('{}/visualize_bg{}/'.format(self.visual_save_fold, epoch))
             img_save = fork_bg
             img_save = img_save.reshape(-1, img_save.shape[2], img_save.shape[3]).transpose(1,2,0) * 255
             cv2.imwrite('{}/visualize_bg{}/{}'.format(self.visual_save_fold, epoch, name), img_save.astype(np.uint8))
             # print('save visualize_bg{}/{} done.'.format(name, epoch))
             if not os.path.exists('{}/visualize_fg{}/'.format(self.visual_save_fold, epoch)):
                 os.mkdir('{}/visualize_fg{}/'.format(self.visual_save_fold, epoch))
             img_save = fork_fg
             img_save = img_save.reshape(-1, img_save.shape[2], img_save.shape[3]).transpose(1,2,0) * 255
             cv2.imwrite('{}/visualize_fg{}/{}'.format(self.visual_save_fold, epoch, name), img_save.astype(np.uint8))
             # print('save visualize_bg{}/{} done.'.format(name, epoch))
             mae = self.eval_mae(prob_pred, labels)
             if mae == mae:
                 avg_mae += mae
                 img_num += 1.0
                 # prec, recall = self.eval_pr(prob_pred, labels, num)
                 # avg_prec, avg_recall = avg_prec + prec, avg_recall + recall
     avg_mae = avg_mae / img_num
     # avg_mae, avg_prec, avg_recall = avg_mae / img_num, avg_prec / img_num, avg_recall / img_num
     # score = (1 + self.beta ** 2) * avg_prec * avg_recall / (self.beta ** 2 * avg_prec + avg_recall)
     # score[score != score] = 0  # delete the nan
     # print('average mae: %.4f, max fmeasure: %.4f' % (avg_mae, score.max()))
     print('average mae: %.4f' % (avg_mae))
     # print('average mae: %.4f, max fmeasure: %.4f' % (avg_mae, score.max()), file=self.test_output)
     return avg_mae, 1.0 #score.max()
示例#4
0
    def test(self, num, use_crf=False):
        if use_crf: from tools.crf_process import crf
        avg_mae, img_num = 0.0, len(self.test_dataset)
        avg_prec, avg_recall = torch.zeros(num), torch.zeros(num)
        with torch.no_grad():
            for i, data in enumerate(
                    self.test_dataset
            ):  #(img, labels) in enumerate(self.test_dataset):
                images, labels = data['image'], data['label']
                images = images.type(torch.cuda.FloatTensor)
                labels = labels.type(torch.cuda.FloatTensor)
                #images = self.transform(img).unsqueeze(0)
                #labels = labels.unsqueeze(0)
                shape = labels.size()[2:]
                #print(shape)
                images = images.to(self.device)
                labels = labels.to(self.device)
                prob_pred = self.net(images)

                prob_pred = torch.mean(torch.cat(
                    [prob_pred[i] for i in self.select], dim=1),
                                       dim=1,
                                       keepdim=True)
                prob_pred = F.interpolate(prob_pred,
                                          size=shape,
                                          mode='bilinear',
                                          align_corners=True).cpu().data
                print(prob_pred[0].size())
                result_dir = 'C:/Users/Paul Vincent Nonat/Documents/Graduate Student Files/results/'
                save_image(prob_pred[0],
                           result_dir + 'result' + str(i) + '.png')
                if use_crf:
                    prob_pred = crf(img, prob_pred.numpy(), to_tensor=True)
                mae = self.eval_mae(prob_pred, labels)
                prec, recall = self.eval_pr(prob_pred, labels, num)
                print(num)
                print("[%d] mae: %.4f" % (i, mae))
                print("[%d] mae: %.4f" % (i, mae), file=self.test_output)
                avg_mae += mae
                avg_prec, avg_recall = avg_prec + prec, avg_recall + recall
        avg_mae, avg_prec, avg_recall = avg_mae / img_num, avg_prec / img_num, avg_recall / img_num
        score = (1 + self.beta**2) * avg_prec * avg_recall / (
            self.beta**2 * avg_prec + avg_recall)
        score[score != score] = 0  # delete the nan
        print('average mae: %.4f, max fmeasure: %.4f' % (avg_mae, score.max()))
        print('average mae: %.4f, max fmeasure: %.4f' % (avg_mae, score.max()),
              file=self.test_output)
示例#5
0
    def test(self, num, output_path, use_crf=False):
        if use_crf: from tools.crf_process import crf
        avg_mae, img_num = 0.0, len(self.test_dataset)
        avg_prec, avg_recall = torch.zeros(num), torch.zeros(num)
        with torch.no_grad():
            counter = 0
            for i, data in enumerate(
                    self.test_dataset
            ):  #(img, labels) in enumerate(self.test_dataset):
                images, labels = data['image'], data['label']
                images = images.type(torch.cuda.FloatTensor)
                labels = labels.type(torch.cuda.FloatTensor)
                #images = self.transform(img).unsqueeze(0)
                #labels = labels.unsqueeze(0)
                shape = labels.size()[2:]
                #print(shape)
                images = images.to(self.device)
                labels = labels.to(self.device)
                prob_pred = self.net(images)

                prob_pred = torch.mean(torch.cat(
                    [prob_pred[i] for i in self.select], dim=1),
                                       dim=1,
                                       keepdim=True)

                if use_crf:
                    prob_pred = crf(img, prob_pred.numpy(), to_tensor=True)
                mae = self.eval_mae(prob_pred, labels).item()

                prob_pred = F.interpolate(prob_pred,
                                          size=shape,
                                          mode='bilinear',
                                          align_corners=True)
                ratio = 160 / 224 * 7
                #plot_result.append(images[0])
                #plot_result.append(labels[0])
                result_dir = output_path
                #plot_image(prob_pred[0], (224/60, 224/60), 'Predicted Map')
                print(images.size()[0])
                for j in range(images.size()[0]):
                    print(counter)
                    #    plot_image(images[j], (224/120, 224/120), 'Input Image',True)
                    #    plot_image(labels[j], (224/120, 224/120), 'Ground Truth')
                    #    plot_image(prob_pred[j], (224/120, 224/120), 'Predicted Map')
                    save_image(images[j],
                               result_dir + 'input' + str(counter) + '.jpg')
                    save_image(make_grid([labels[j], prob_pred[j]]),
                               result_dir + 'result' + str(counter) + '.png')
                    counter = counter + 1
                prec, recall = self.prec_recall(prob_pred, labels, num)

                #print(num)
                print("[%d] mae: %.4f" % (i, mae))
                print("[%d] mae: %.4f" % (i, mae), file=self.test_output)
                avg_mae += mae
                avg_prec, avg_recall = avg_prec + prec, avg_recall + recall

        avg_mae, avg_prec, avg_recall = avg_mae / img_num, avg_prec / img_num, avg_recall / img_num

        score = (1 + self.beta**2) * avg_prec * avg_recall / (
            self.beta**2 * avg_prec + avg_recall)
        score[score != score] = 0  # delete the nan
        print('average mae: %.4f, max fmeasure: %.4f' % (avg_mae, score.max()))
        print('average mae: %.4f, max fmeasure: %.4f' % (avg_mae, score.max()),
              file=self.test_output)