コード例 #1
0
ファイル: base_model.py プロジェクト: tamwaiban/CISR_PSI
    def comput_PSNR_SSIM(self, pred, gt, shave_border=0):

        if isinstance(pred, torch.Tensor):
            pred = tensor2np(pred, self.opt.rgb_range)
            pred = pred.astype(np.float32)

        if isinstance(gt, torch.Tensor):
            gt = tensor2np(gt, self.opt.rgb_range)
            gt = gt.astype(np.float32)

        height, width = pred.shape[:2]
        pred = pred[shave_border:height - shave_border,
                    shave_border:width - shave_border]
        gt = gt[shave_border:height - shave_border,
                shave_border:width - shave_border]

        if pred.shape[2] == 3 and gt.shape[2] == 3:
            pred_y = rgb2ycbcr(pred)[:, :, 0]
            gt_y = rgb2ycbcr(gt)[:, :, 0]
        elif pred.shape[2] == 1 and gt.shape[2] == 1:
            pred_y = pred[:, :, 0]
            gt_y = gt[:, :, 0]
        else:
            raise ValueError('Input or output channel is not 1 or 3!')

        psnr_ = calc_PSNR(pred_y, gt_y)
        ssim_ = calc_ssim(pred_y, gt_y)

        return psnr_, ssim_
コード例 #2
0
ファイル: train.py プロジェクト: v4lkyri3/PPON
def test():
    avg_psnr = 0

    for batch in testing_data_loader:
        input, target = batch[0].detach(), batch[1].detach()
        model.feed_data([input], need_HR=False)
        model.test()
        pre = model.get_current_visuals(need_HR=False)
        sr_img = utils.tensor2np(pre['SR'].data)
        gt_img = utils.tensor2np(target.data[0])
        crop_size = args.scale
        cropped_sr_img = utils.shave(sr_img, crop_size)
        cropped_gt_img = utils.shave(gt_img, crop_size)
        if is_y is True:
            im_label = utils.quantize(sc.rgb2ycbcr(cropped_gt_img)[:, :, 0])
            im_pre = utils.quantize(sc.rgb2ycbcr(cropped_sr_img)[:, :, 0])
        else:
            im_label = cropped_gt_img
            im_pre = cropped_sr_img
        avg_psnr += utils.compute_psnr(im_pre, im_label)

    print("===> Valid. psnr: {:.4f}".format(avg_psnr /
                                            len(testing_data_loader)))
コード例 #3
0
ファイル: test_IMDN_AS.py プロジェクト: xiaotongtt/IMDN
    with torch.no_grad():

        if h % 4 == 0 and w % 4 == 0:
            start.record()
            out = model(im_input)
            end.record()
            torch.cuda.synchronize()
            time_list[i] = start.elapsed_time(end)  # milliseconds
        else:
            start.record()
            out = crop_forward(im_input, model)
            end.record()
            torch.cuda.synchronize()
            time_list[i] = start.elapsed_time(end)  # milliseconds

    sr_img = utils.tensor2np(out.detach()[0])
    if opt.is_y is True:
        im_label = utils.quantize(sc.rgb2ycbcr(im_gt)[:, :, 0])
        im_pre = utils.quantize(sc.rgb2ycbcr(sr_img)[:, :, 0])
    else:
        im_label = im_gt
        im_pre = sr_img
    psnr_list[i] = utils.compute_psnr(im_pre, im_label)
    ssim_list[i] = utils.compute_ssim(im_pre, im_label)

    output_folder = os.path.join(opt.output_folder, imname.split('/')[-1])

    if not os.path.exists(opt.output_folder):
        os.makedirs(opt.output_folder)

    sio.imsave(output_folder, sr_img)
コード例 #4
0
    img_path_l = sorted(glob.glob(os.path.join(opt.test_lr_folder, sub_foler) + '/*.png'))

    if not os.path.isdir(save_sub_folder):
        utils.mkdirs(save_sub_folder)

    ### read LR images
    imgs = utils.read_seq_imgs(os.path.join(opt.test_lr_folder, sub_foler))

    # process each image
    for img_idx, img_path in enumerate(img_path_l):
        # get input images
        img_in = imgs[img_idx].unsqueeze(0).to(device)

        with torch.no_grad():

            output = model(img_in)
            output_f = output.data.float().cpu().squeeze(0)

            if opt.flip_test:
                # flip W
                output = model(torch.flip(img_in, (-1,)))
                output = torch.flip(output, (-1,))
                output = output.data.float().cpu().squeeze(0)
                output_f += output

                output_f /= 2

        output = utils.tensor2np(output_f)[:, :, [2, 1, 0]]  # rgb --> bgr
        cv2.imwrite(os.path.join(save_sub_folder, '{:04d}.png'.format(img_idx + 1)), output)
        print("Saved {}-th image".format(img_idx + 1))
コード例 #5
0
sub_folder_name_list = []

# for each sub-folder
for sub_foler in sub_folder_list:
    sub_folder_name_list.append(sub_foler)
    save_sub_folder = os.path.join(opt.output_folder, sub_foler)

    img_path_l = sorted(
        glob.glob(os.path.join(opt.test_lr_folder, sub_foler) + '/*.png'))

    if not os.path.isdir(save_sub_folder):
        utils.mkdirs(save_sub_folder)

    ### read LR images
    imgs = utils.read_seq_imgs(os.path.join(opt.test_lr_folder, sub_foler))

    # process each image
    for img_idx, img_path in enumerate(img_path_l):
        # get input images
        img_in = imgs[img_idx].unsqueeze(0).to(device)

        with torch.no_grad():
            output = model(img_in)
            output_f = output.data.float().cpu().squeeze(0)

        output = utils.tensor2np(output_f)[:, :, [2, 1, 0]]
        cv2.imwrite(
            os.path.join(save_sub_folder, '{:04d}.png'.format(img_idx + 1)),
            output)
        print("Saved {}-th image".format(img_idx + 1))
コード例 #6
0
ファイル: base_model.py プロジェクト: tamwaiban/CISR_PSI
 def save_image(self, img_tensor, save_name):
     img_np = tensor2np(img_tensor, self.opt.rgb_range)
     imageio.imsave(save_name, img_np)
コード例 #7
0
    def paintEvent(self, e) -> None:
        if self.inter == cv2.INTER_NEAREST:
            inter_method = 'Nearest'
        elif self.inter == cv2.INTER_LINEAR:
            inter_method = 'Bilinear'
        elif self.inter == cv2.INTER_CUBIC:
            inter_method = 'Bicubic'

        self.setWindowTitle('Interpolation: {} / backend: {}'.format(
            inter_method, self.backend))

        qimg = QImage(
            self.img,
            self.img_w,
            self.img_h,
            3 * self.img_w,
            QImage.Format_RGB888,
        )
        qpix = QPixmap(qimg)

        qp = QPainter()
        qp.begin(self)
        qp.drawPixmap(self.margin, self.margin, self.img_w, self.img_h, qpix)

        m = self.get_matrix()
        y_min, x_min, h_new, w_new = self.get_dimension(m)
        mc = np.array([[1, 0, -x_min], [0, 1, -y_min], [0, 0, 1]])
        m = np.matmul(mc, m)
        if self.backend == 'opencv':
            warp = cv2.warpPerspective(
                self.img,
                m,
                (w_new, h_new),
                flags=self.inter,
            )
        elif self.backend == 'core':
            warp = core_warp.warp(self.img_tensor,
                                  torch.Tensor(m),
                                  sizes=(h_new, w_new),
                                  kernel=inter_method.lower(),
                                  fill_value=0.5)
            warp = utils.tensor2np(warp)

        qimg_warp = QImage(warp, w_new, h_new, 3 * w_new, QImage.Format_RGB888)
        qpix_warp = QPixmap(qimg_warp)
        qp.drawPixmap(
            self.offset_w + x_min,
            self.offset_h + y_min,
            w_new,
            h_new,
            qpix_warp,
        )
        '''
        for i, pos in enumerate(self.line_order):
            j = (i + 1) % 4
            y, x = self.cps[pos]
            y = y + self.offset_h
            x = x + self.offset_w
            y_next, x_next = self.cps[self.line_order[j]]
            y_next = y_next + self.offset_h
            x_next = x_next + self.offset_w
            qp.drawLine(x, y, x_next, y_next)
        '''
        center_y = self.offset_h + self.img_h // 2
        center_x = self.offset_w + self.img_w // 2

        pen_blue = QPen(Qt.blue, 5)
        pen_white = QPen(Qt.white, 10)
        text_size = 20
        #brush = QBrush(Qt.red, Qt.SolidPattern)
        #qp.setBrush(brush)
        for key, val in self.cps.items():
            y, x = val
            y = y + self.offset_h
            x = x + self.offset_w
            qp.setPen(pen_blue)
            #qp.drawEllipse(x, y, 3, 3)
            qp.drawPoint(x, y)
            qp.setPen(pen_white)
            dy = y - center_y
            dx = x - center_x
            dl = math.sqrt(dy**2 + dx**2) / 10
            qp.drawText(
                x + (dx / dl) - text_size // 2,
                y + (dy / dl) - text_size // 2,
                text_size,
                text_size,
                int(Qt.AlignCenter),
                key,
            )

        qp.end()
        return