Beispiel #1
0
def main(not_parsed_args):
    if len(not_parsed_args) > 1:
        print("Unknown args:%s" % not_parsed_args)
        exit()
    training_filenames = util.get_files_in_directory(
        "/media/data1/ww/sr_data/DIV2K_aug2/DIV2K_train_HR")
    # target_dir_x2 = "/media/data1/ww/sr_data/DIV2K_aug2/291_LR_bicubic_X2/291_LR_bicubic/X2"
    target_dir_x3 = "/media/data1/ww/sr_data/DIV2K_aug2/DIV2K_train_LR_bicubic_X3/DIV2K_train_LR_bicubic/X3"
    target_dir_x4 = "/media/data1/ww/sr_data/DIV2K_aug2/DIV2K_train_LR_bicubic_X4/DIV2K_train_LR_bicubic/X4"
    # util.make_dir(target_dir_x2)
    util.make_dir(target_dir_x3)
    util.make_dir(target_dir_x4)
    for file_path in training_filenames:
        org_image = util.load_image(file_path)
        filename = os.path.basename(file_path)
        filename, extension = os.path.splitext(filename)
        # new_filename_x2 = target_dir_x2 + '/' +filename + 'x{}'.format(2)
        new_filename_x3 = target_dir_x3 + '/' + filename + 'x{}'.format(3)
        new_filename_x4 = target_dir_x4 + '/' + filename + 'x{}'.format(4)

        # bicubic_image_x2 = util.resize_image_by_pil(org_image, 1 / 2)
        bicubic_image_x3 = util.resize_image_by_pil(org_image, 1 / 3)
        bicubic_image_x4 = util.resize_image_by_pil(org_image, 1 / 4)
        # util.save_image(new_filename_x2 + extension, bicubic_image_x2)
        util.save_image(new_filename_x3 + extension, bicubic_image_x3)
        util.save_image(new_filename_x4 + extension, bicubic_image_x4)
def do_util(src, dest):
    with Timer('util: load'):
        inp = util.load_image(src)
    with Timer('util: resize'):
        resized = util.resize_image_by_pil(inp, 2)
    with Timer('util: extract Y'):
        only_y = util.convert_rgb_to_y(inp)
    only_y = util.convert_rgb_to_y(resized)  # simulate upscale
    with Timer('util: rgb => YCbCr'):
        scaled_ycbcr_image = util.convert_rgb_to_ycbcr(resized)
    with Timer('util: Y + YCbCr -> rgb'):
        image = util.convert_y_and_cbcr_to_rgb(only_y, scaled_ycbcr_image[:, :,
                                                                          1:3])
    with Timer('util: save'):
        util.save_image(dest, image)
    def make_input_image(self, file_path, true_image, scale=1, print_console=True):
        if true_image is not None:
            assert len(true_image.shape) == 3 and true_image.shape[2] == 3

        fname, fext = os.path.splitext(file_path)
        input_image = None
        if fname.lower().endswith(TRUE_SUFFIX):
            target = fname[:-len(TRUE_SUFFIX)] + INPUT_SUFFIX + fext
            head, tail = os.path.split(target)
            target = os.path.join(head, 'input', tail)
            if os.path.exists(target):
                input_image = util.set_image_alignment(util.load_image(target, print_console=print_console), scale)

        if input_image is None and true_image is not None:
            input_image = util.resize_image_by_pil(true_image, 1.0 / scale, resampling_method=self.resampling_method)

        hblur_radius = random.randrange(0, self.hblur_max) / 100.
        vblur_radius = random.randrange(0, self.vblur_max) / 100.
        qua = random.randrange(self.jpegify[0], self.jpegify[1])

#        if vblur_radius > 0:
#            input_image = gaussian_filter1d(input_image, sigma=vblur_radius, axis=0)
#        if hblur_radius > 0:
#            input_image = gaussian_filter1d(input_image, sigma=hblur_radius, axis=1)

        kx = int((hblur_radius + 0.5) * 2)
        if kx % 2 == 0:
            kx += 1

        ky = int((vblur_radius + 0.5) * 2)
        if ky % 2 == 0:
            ky += 1

        cv2.GaussianBlur(src=input_image, ksize=(kx, ky), sigmaX=hblur_radius, dst=input_image, sigmaY=vblur_radius)

        if qua < 100:
            tmpname = tempfile.mktemp(suffix='.jpg')
            util.save_image(tmpname, input_image, qua, print_console=False)
            input_image = util.load_image(tmpname, print_console=False)
            os.unlink(tmpname)

        return input_image
Beispiel #4
0
    def do_for_file(self, file_path, output_folder="output"):

        filename, extension = os.path.splitext(file_path)
        output_folder += "/"
        org_image = util.load_image(file_path)
        util.save_image(output_folder + file_path, org_image)

        if len(org_image.shape
               ) >= 3 and org_image.shape[2] == 3 and self.channels == 1:
            input_y_image = util.convert_rgb_to_y(org_image,
                                                  jpeg_mode=self.jpeg_mode)
            scaled_image = util.resize_image_by_pil(
                input_y_image,
                self.scale,
                resampling_method=self.resampling_method)
            util.save_image(
                output_folder + filename + "_bicubic_y" + extension,
                scaled_image)
            output_y_image = self.do(input_y_image)
            util.save_image(output_folder + filename + "_result_y" + extension,
                            output_y_image)

            scaled_ycbcr_image = util.convert_rgb_to_ycbcr(
                util.resize_image_by_pil(org_image, self.scale,
                                         self.resampling_method),
                jpeg_mode=self.jpeg_mode)
            image = util.convert_y_and_cbcr_to_rgb(output_y_image,
                                                   scaled_ycbcr_image[:, :,
                                                                      1:3],
                                                   jpeg_mode=self.jpeg_mode)
        else:
            scaled_image = util.resize_image_by_pil(
                org_image,
                self.scale,
                resampling_method=self.resampling_method)
            util.save_image(
                output_folder + filename + "_bicubic_y" + extension,
                scaled_image)
            image = self.do(org_image)

        util.save_image(output_folder + filename + "_result" + extension,
                        image)
Beispiel #5
0
def main(not_parsed_args):
    if len(not_parsed_args) > 1:
        print("Unknown args:%s" % not_parsed_args)
        exit()

    print("Building Y channel data...")

    training_filenames = util.get_files_in_directory(FLAGS.data_dir + "/" +
                                                     FLAGS.dataset + "/")
    target_dir = FLAGS.data_dir + "/" + FLAGS.dataset + "_y/"
    util.make_dir(target_dir)

    for file_path in training_filenames:
        org_image = util.load_image(file_path)
        if org_image.shape[2] == 3:
            org_image = util.convert_rgb_to_y(org_image)

        filename = os.path.basename(file_path)
        filename, extension = os.path.splitext(filename)

        new_filename = target_dir + filename
        util.save_image(new_filename + ".bmp", org_image)
Beispiel #6
0
    def do_for_file(self, file_path, output_folder="output"):

        org_image = util.load_image(file_path)

        filename, extension = os.path.splitext(os.path.basename(file_path))
        output_folder += "/" + self.name + "/"
        # util.save_image(output_folder + filename + extension, org_image)

        if os.path.exists(output_folder + filename + extension):
            print("File already exists in the target directory")
            return

        if org_image.shape[0] + org_image.shape[1] >= 1024:
            print("Image is too big: ", org_image.shape)
            image = org_image
        elif len(org_image.shape
                 ) >= 3 and org_image.shape[2] == 3 and self.channels == 1:
            input_y_image = util.convert_rgb_to_y(org_image)
            # scaled_image = util.resize_image_by_pil(input_y_image, self.scale, resampling_method=self.resampling_method)
            # util.save_image(output_folder + filename + "_bicubic_y" + extension, scaled_image)
            output_y_image = self.do(input_y_image)
            # util.save_image(output_folder + filename + "_result_y" + extension, output_y_image)

            scaled_ycbcr_image = util.convert_rgb_to_ycbcr(
                util.resize_image_by_pil(org_image, self.scale,
                                         self.resampling_method))
            image = util.convert_y_and_cbcr_to_rgb(
                output_y_image, scaled_ycbcr_image[:, :, 1:3])
        else:
            # scaled_image = util.resize_image_by_pil(org_image, self.scale, resampling_method=self.resampling_method)
            # util.save_image(output_folder + filename + "_bicubic_y" + extension, scaled_image)
            image = self.do(org_image)

        # util.save_image(output_folder + filename + "_result" + extension, image)
        file_path = output_folder + filename + extension
        util.save_image(file_path, image)
        print("Saved at ", file_path)
Beispiel #7
0
    def do_for_evaluate(self,
                        file_path,
                        output_directory=None,
                        print_console=False,
                        save_output_images=True):
        if save_output_images:
            filename, extension = os.path.splitext(file_path)
            output_directory += "/" + self.name + "/"
            util.make_dir(output_directory)

        true_image = util.set_image_alignment(
            util.load_image(file_path, print_console=False), self.scale)

        if true_image.shape[2] == 3 and self.channels == 1:

            # for color images
            input_y_image = loader.build_input_image(true_image,
                                                     channels=self.channels,
                                                     scale=self.scale,
                                                     alignment=self.scale,
                                                     convert_ycbcr=True)
            input_bicubic_y_image = util.resize_image_by_pil(
                input_y_image,
                self.scale,
                resampling_method=self.resampling_method)

            true_ycbcr_image = util.convert_rgb_to_ycbcr(true_image)

            start = time.time()
            output_y_image = self.do(input_y_image, input_bicubic_y_image)
            end = time.time()
            psnr, ssim = util.compute_psnr_and_ssim(
                true_ycbcr_image[:, :, 0:1],
                output_y_image,
                border_size=self.psnr_calc_border_size)
            loss_image = util.get_loss_image(
                true_ycbcr_image[:, :, 0:1],
                output_y_image,
                border_size=self.psnr_calc_border_size)

            output_color_image = util.convert_y_and_cbcr_to_rgb(
                output_y_image, true_ycbcr_image[:, :, 1:3])

            if save_output_images:
                util.save_image(output_directory + file_path, true_image)
                util.save_image(
                    output_directory + filename + "_input" + extension,
                    input_y_image)
                util.save_image(
                    output_directory + filename + "_input_bicubic" + extension,
                    input_bicubic_y_image)
                util.save_image(
                    output_directory + filename + "_true_y" + extension,
                    true_ycbcr_image[:, :, 0:1])
                util.save_image(
                    output_directory + filename + "_result" + extension,
                    output_y_image)
                util.save_image(
                    output_directory + filename + "_result_c" + extension,
                    output_color_image)
                util.save_image(
                    output_directory + filename + "_loss" + extension,
                    loss_image)

        elif true_image.shape[2] == 1 and self.channels == 1:

            # for monochrome images
            input_image = loader.build_input_image(true_image,
                                                   channels=self.channels,
                                                   scale=self.scale,
                                                   alignment=self.scale)
            input_bicubic_y_image = util.resize_image_by_pil(
                input_image,
                self.scale,
                resampling_method=self.resampling_method)
            start = time.time()
            output_image = self.do(input_image, input_bicubic_y_image)
            end = time.time()
            psnr, ssim = util.compute_psnr_and_ssim(
                true_image,
                output_image,
                border_size=self.psnr_calc_border_size)
            if save_output_images:
                util.save_image(output_directory + file_path, true_image)
                util.save_image(
                    output_directory + filename + "_result" + extension,
                    output_image)
        else:
            return None, None
        if print_console:
            print("[%s] PSNR:%f, SSIM:%f" % (filename, psnr, ssim))
        elapsed_time = end - start
        return psnr, ssim, elapsed_time
Beispiel #8
0
def main(not_parsed_args):
    if len(not_parsed_args) > 1:
        print("Unknown args:%s" % not_parsed_args)
        exit()

    print("Building x%d augmented data." % FLAGS.augment_level)

    training_filenames = util.get_files_in_directory(FLAGS.data_dir + "/" +
                                                     FLAGS.dataset + "/")
    target_dir = FLAGS.data_dir + "/" + FLAGS.dataset + ("_%d/" %
                                                         FLAGS.augment_level)
    util.make_dir(target_dir)

    for file_path in training_filenames:
        org_image = util.load_image(file_path)

        filename = os.path.basename(file_path)
        filename, extension = os.path.splitext(filename)

        new_filename = target_dir + filename
        util.save_image(new_filename + extension, org_image)

        if FLAGS.augment_level >= 2:
            ud_image = np.flipud(org_image)
            util.save_image(new_filename + "_v" + extension, ud_image)
        if FLAGS.augment_level >= 3:
            lr_image = np.fliplr(org_image)
            util.save_image(new_filename + "_h" + extension, lr_image)
        if FLAGS.augment_level >= 4:
            lr_image = np.fliplr(org_image)
            lrud_image = np.flipud(lr_image)
            util.save_image(new_filename + "_hv" + extension, lrud_image)

        if FLAGS.augment_level >= 5:
            rotated_image1 = np.rot90(org_image)
            util.save_image(new_filename + "_r1" + extension, rotated_image1)
        if FLAGS.augment_level >= 6:
            rotated_image2 = np.rot90(org_image, -1)
            util.save_image(new_filename + "_r2" + extension, rotated_image2)

        if FLAGS.augment_level >= 7:
            rotated_image1 = np.rot90(org_image)
            ud_image = np.flipud(rotated_image1)
            util.save_image(new_filename + "_r1_v" + extension, ud_image)
        if FLAGS.augment_level >= 8:
            rotated_image2 = np.rot90(org_image, -1)
            ud_image = np.flipud(rotated_image2)
            util.save_image(new_filename + "_r2_v" + extension, ud_image)
Beispiel #9
0
 def save_true_batch_image(self, image_number, image):
     return util.save_image(
         self.batch_dir + "/" + TRUE_IMAGE_DIR + "/%06d.bmp" % image_number,
         image)
Beispiel #10
0
 def save_interpolated_batch_image(self, image_number, image):
     return util.save_image(
         self.batch_dir + "/" + INTERPOLATED_IMAGE_DIR +
         "/%06d.bmp" % image_number, image)
Beispiel #11
0
 def save_input_batch_image(self, image_number, image):
     return util.save_image(
         self.batch_dir + "/" + INPUT_IMAGE_DIR +
         "/%06d.bmp" % image_number, image)
Beispiel #12
0
    def build_batch(self, data_dir, batch_dir):
        """ load from input files. Then save batch images on file to reduce memory consumption. """

        print("Building batch images for %s..." % batch_dir)
        filenames = util.get_files_in_directory(data_dir)
        images_count = 0

        util.make_dir(batch_dir)
        util.clean_dir(batch_dir)
        util.make_dir(batch_dir + "/" + INPUT_IMAGE_DIR)
        util.make_dir(batch_dir + "/" + INTERPOLATED_IMAGE_DIR)
        util.make_dir(batch_dir + "/" + TRUE_IMAGE_DIR)

        for filename in filenames:
            output_window_size = self.batch_image_size * self.scale
            output_window_stride = self.stride * self.scale
            input_image, input_bicubic_image = self.input.load_input_image(
                filename,
                rescale=True,
                resampling_method=self.resampling_method)
            test_image = self.true.load_test_image(filename)

            # split into batch images
            input_batch_images = util.get_split_images(input_image,
                                                       self.batch_image_size,
                                                       stride=self.stride)
            input_bicubic_batch_images = util.get_split_images(
                input_bicubic_image,
                output_window_size,
                stride=output_window_stride)
            if input_batch_images is None or input_bicubic_batch_images is None:
                continue

            input_count = input_batch_images.shape[0]

            test_batch_images = util.get_split_images(
                test_image, output_window_size, stride=output_window_stride)

            for i in range(input_count):
                # util.save_image_data(batch_dir + "/" + INPUT_IMAGE_DIR + "/%06d.npy" % images_count, input_batch_images[i])
                # util.save_image_data(batch_dir + "/" + INTERPOLATED_IMAGE_DIR + "/%06d.npy" % images_count,
                #                 input_bicubic_batch_images[i])
                # util.save_image_data(batch_dir + "/" + TRUE_IMAGE_DIR + "/%06d.npy" % images_count, test_batch_images[i])
                util.save_image(
                    batch_dir + "/" + INPUT_IMAGE_DIR +
                    "/%06d.bmp" % images_count, input_batch_images[i])
                util.save_image(
                    batch_dir + "/" + INTERPOLATED_IMAGE_DIR +
                    "/%06d.bmp" % images_count, input_bicubic_batch_images[i])
                util.save_image(
                    batch_dir + "/" + TRUE_IMAGE_DIR +
                    "/%06d.bmp" % images_count, test_batch_images[i])

                images_count += 1

        print("%d mini-batch images are built(saved)." % images_count)

        config = configparser.ConfigParser()
        config.add_section("batch")
        config.set("batch", "count", str(images_count))
        config.set("batch", "scale", str(self.scale))
        config.set("batch", "batch_image_size", str(self.batch_image_size))
        config.set("batch", "stride", str(self.stride))
        config.set("batch", "channels", str(self.channels))
        config.set("batch", "jpeg_mode", str(self.jpeg_mode))
        config.set("batch", "max_value", str(self.max_value))

        with open(batch_dir + "/batch_images.ini", "w") as configfile:
            config.write(configfile)
Beispiel #13
0
def load_and_evaluate_tflite_graph(
    output_dir,
    data_dir,
    test_data,
    model_path=os.path.join(os.getcwd(),
                            'model_to_freeze/converted_model.tflite')):
    # https://stackoverflow.com/questions/50443411/how-to-load-a-tflite-model-in-script
    # https://www.tensorflow.org/lite/convert/python_api#tensorflow_lite_python_interpreter_
    output_directory = output_dir
    output_directory += "/" + "tflite" + "/"
    util.make_dir(output_directory)

    test_filepaths = util.get_files_in_directory(data_dir + "/" + test_data)
    total_psnr = total_ssim = total_time = 0

    # Load TFLite model and allocate tensors.
    interpreter = tf.lite.Interpreter(model_path=model_path)
    # interpreter = tf.contrib.lite.Interpreter(model_path=model_path)
    interpreter.allocate_tensors()

    # Get input and output tensors.
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    for file_path in test_filepaths:
        # split filename from extension
        filename, extension = os.path.splitext(file_path)

        # prepare true image
        true_image = util.set_image_alignment(
            util.load_image(file_path, print_console=False), FLAGS.scale)

        # start the timer
        if true_image.shape[2] == 3 and FLAGS.channels == 1:
            # prepare input and ground truth images
            input_y_image = loader.build_input_image(true_image,
                                                     channels=FLAGS.channels,
                                                     scale=FLAGS.scale,
                                                     alignment=FLAGS.scale,
                                                     convert_ycbcr=True)
            input_bicubic_y_image = util.resize_image_by_pil(
                input_y_image,
                FLAGS.scale,
                resampling_method=FLAGS.resampling_method)
            true_ycbcr_image = util.convert_rgb_to_ycbcr(true_image)

            # pass inputs through the model (need to recast and reshape inputs)
            input_y_image_reshaped = input_y_image.astype('float32')
            input_y_image_reshaped = input_y_image_reshaped.reshape(
                1, input_y_image.shape[0], input_y_image.shape[1],
                FLAGS.channels)

            input_bicubic_y_image_reshaped = input_bicubic_y_image.astype(
                'float32')
            input_bicubic_y_image_reshaped = input_bicubic_y_image_reshaped.reshape(
                1, input_bicubic_y_image.shape[0],
                input_bicubic_y_image.shape[1], FLAGS.channels)

            interpreter.set_tensor(input_details[0]['index'],
                                   input_y_image_reshaped)  # pass x
            interpreter.set_tensor(input_details[1]['index'],
                                   input_bicubic_y_image_reshaped)  # pass x2

            start = time.time()
            interpreter.invoke()
            end = time.time()

            output_y_image = interpreter.get_tensor(
                output_details[0]['index'])  # get y
            # resize the output into an image
            output_y_image = output_y_image.reshape(output_y_image.shape[1],
                                                    output_y_image.shape[2],
                                                    FLAGS.channels)

            # calculate psnr and ssim for the output
            psnr, ssim = util.compute_psnr_and_ssim(
                true_ycbcr_image[:, :, 0:1],
                output_y_image,
                border_size=FLAGS.psnr_calc_border_size)

            # get the loss image
            loss_image = util.get_loss_image(
                true_ycbcr_image[:, :, 0:1],
                output_y_image,
                border_size=FLAGS.psnr_calc_border_size)

            # get output color image
            output_color_image = util.convert_y_and_cbcr_to_rgb(
                output_y_image, true_ycbcr_image[:, :, 1:3])

            # save all images
            util.save_image(output_directory + file_path, true_image)
            util.save_image(output_directory + filename + "_input" + extension,
                            input_y_image)
            util.save_image(
                output_directory + filename + "_input_bicubic" + extension,
                input_bicubic_y_image)
            util.save_image(
                output_directory + filename + "_true_y" + extension,
                true_ycbcr_image[:, :, 0:1])
            util.save_image(
                output_directory + filename + "_result" + extension,
                output_y_image)
            util.save_image(
                output_directory + filename + "_result_c" + extension,
                output_color_image)
            util.save_image(output_directory + filename + "_loss" + extension,
                            loss_image)
        elapsed_time = end - start
        total_psnr += psnr
        total_ssim += ssim
        total_time += elapsed_time
    testSize = len(test_filepaths)
    print("Model Average [%s] PSNR:%f, SSIM:%f, Elapsed Time:%f" %
          (test_data, total_psnr / testSize, total_ssim / testSize,
           total_time / testSize))
Beispiel #14
0
    def do_for_evaluate_with_output(self,
                                    file_path,
                                    output_directory,
                                    print_console=False):

        filename, extension = os.path.splitext(file_path)
        output_directory += "/" + self.name + "/"
        util.make_dir(output_directory)

        true_image = util.set_image_alignment(
            util.load_image(file_path, print_console=False), self.scale)

        if true_image.shape[2] == 3 and self.channels == 3:

            # for color images
            input_image = util.build_input_image(true_image,
                                                 scale=self.scale,
                                                 alignment=self.scale)
            input_bicubic_image = util.resize_image_by_pil(
                input_image,
                self.scale,
                resampling_method=self.resampling_method)

            output_image, spend_time = self.do(input_image)  # SR

            SR_y = eva.convert_rgb_to_y(output_image)
            HR_y = eva.convert_rgb_to_y(true_image)
            psnr_predicted = eva.PSNR(np.uint8(HR_y),
                                      np.uint8(SR_y),
                                      shave_border=self.psnr_calc_border_size)
            ssim_predicted = eva.compute_ssim(np.squeeze(HR_y),
                                              np.squeeze(SR_y))

            mse = util.compute_mse(HR_y,
                                   SR_y,
                                   border_size=self.psnr_calc_border_size)
            loss_image = util.get_loss_image(
                HR_y, SR_y, border_size=self.psnr_calc_border_size)

            util.save_image(output_directory + file_path[29:], true_image)
            util.save_image(
                output_directory + filename[28:] + "_input" + extension,
                input_image)
            util.save_image(
                output_directory + filename[28:] + "_input_bicubic" +
                extension, input_bicubic_image)
            util.save_image(
                output_directory + filename[28:] + "_sr" + extension,
                output_image)
            util.save_image(
                output_directory + filename[28:] + "_loss" + extension,
                loss_image)

        elif true_image.shape[2] == 1 and self.channels == 1:
            # for monochrome images
            input_image = util.build_input_image(true_image,
                                                 scale=self.scale,
                                                 alignment=self.scale)
            output_image, spend_time = self.do(input_image)

            psnr_predicted = eva.PSNR(np.uint8(true_image),
                                      np.uint8(output_image),
                                      shave_border=self.psnr_calc_border_size)
            ssim_predicted = eva.compute_ssim(np.squeeze(true_image),
                                              np.squeeze(output_image))

            mse = util.compute_mse(true_image,
                                   output_image,
                                   border_size=self.psnr_calc_border_size)
            util.save_image(output_directory + file_path, true_image)
            util.save_image(output_directory + filename + "_sr" + extension,
                            output_image)
        else:
            psnr_predicted = 0.0
            ssim_predicted = 0.0
            mse = 0.0
            spend_time = 0.0

        if print_console:
            print("[%s] psnr:%f, ssim:%f, time:%f" %
                  (filename, psnr_predicted, ssim_predicted, spend_time))

        return mse, psnr_predicted, ssim_predicted, spend_time
Beispiel #15
0
    def do_for_file(self, file_path, output_folder="output"):
        org_image = util.load_image(file_path)

        filename, extension = os.path.splitext(os.path.basename(file_path))
        output_folder += "/" + self.name + "/"
        util.save_image(output_folder + filename + extension, org_image)

        #Esta linea para solo blanco y negro
        org_image = util.convert_rgb_to_y(org_image)
        util.save_image(output_folder + filename + "_y" + extension, org_image)

        if len(org_image.shape) >= 3 and org_image.shape[2] == 3 and self.channels == 1:
            input_y_image = util.convert_rgb_to_y(org_image)
            scaled_image = util.resize_image_by_pil(input_y_image, self.scale, resampling_method=self.resampling_method)
            util.save_image(output_folder + filename + "_bicubic_y" + extension, scaled_image)
            output_y_image = self.do(input_y_image)
            util.save_image(output_folder + filename + "_result_y" + extension, output_y_image)

            scaled_ycbcr_image = util.convert_rgb_to_ycbcr(
                util.resize_image_by_pil(org_image, self.scale, self.resampling_method))
            image = util.convert_y_and_cbcr_to_rgb(output_y_image, scaled_ycbcr_image[:, :, 1:3])

        else:
            scaled_image = util.resize_image_by_pil(org_image, self.scale, resampling_method=self.resampling_method)
            util.save_image(output_folder + filename + "_bicubic_y" + extension, scaled_image)
            image = self.do(org_image)
            util.save_image(output_folder + filename + "_residual_y" + extension, image)
            final_image = scaled_image + image



        util.save_image(output_folder + filename + "_result" + extension, final_image)
Beispiel #16
0
    def do_for_evaluate(self,
                        file_path,
                        output_directory="output",
                        output=True,
                        print_console=False):

        filename, extension = os.path.splitext(file_path)
        output_directory += "/" + self.name + "/"
        util.make_dir(output_directory)
        true_image = util.set_image_alignment(
            util.load_image(file_path, print_console=False), self.scale)

        if true_image.shape[2] == 3 and self.channels == 1:
            input_y_image = loader.build_input_image(true_image,
                                                     channels=self.channels,
                                                     scale=self.scale,
                                                     alignment=self.scale,
                                                     convert_ycbcr=True,
                                                     jpeg_mode=self.jpeg_mode)
            # for color images
            if output:
                input_bicubic_y_image = util.resize_image_by_pil(
                    input_y_image,
                    self.scale,
                    resampling_method=self.resampling_method)

                true_ycbcr_image = util.convert_rgb_to_ycbcr(
                    true_image, jpeg_mode=self.jpeg_mode)

                output_y_image = self.do(input_y_image, input_bicubic_y_image)
                mse = util.compute_mse(true_ycbcr_image[:, :, 0:1],
                                       output_y_image,
                                       border_size=6 + self.scale)
                loss_image = util.get_loss_image(true_ycbcr_image[:, :, 0:1],
                                                 output_y_image,
                                                 border_size=self.scale)

                output_color_image = util.convert_y_and_cbcr_to_rgb(
                    output_y_image,
                    true_ycbcr_image[:, :, 1:3],
                    jpeg_mode=self.jpeg_mode)

                util.save_image(output_directory + file_path, true_image)
                util.save_image(
                    output_directory + filename + "_input" + extension,
                    input_y_image)
                util.save_image(
                    output_directory + filename + "_input_bicubic" + extension,
                    input_bicubic_y_image)
                util.save_image(
                    output_directory + filename + "_true_y" + extension,
                    true_ycbcr_image[:, :, 0:1])
                util.save_image(
                    output_directory + filename + "_result" + extension,
                    output_y_image)
                util.save_image(
                    output_directory + filename + "_result_c" + extension,
                    output_color_image)
                util.save_image(
                    output_directory + filename + "_loss" + extension,
                    loss_image)
            else:
                true_y_image = util.convert_rgb_to_y(true_image,
                                                     jpeg_mode=self.jpeg_mode)
                input_bicubic_y_image = util.resize_image_by_pil(
                    input_y_image,
                    self.scale,
                    resampling_method=self.resampling_method)
                output_y_image = self.do(input_y_image, input_bicubic_y_image)
                mse = util.compute_mse(true_y_image,
                                       output_y_image,
                                       border_size=6 + self.scale)

        elif true_image.shape[2] == 1 and self.channels == 1:

            # for monochrome images
            input_image = loader.build_input_image(true_image,
                                                   channels=self.channels,
                                                   scale=self.scale,
                                                   alignment=self.scale)
            input_bicubic_y_image = util.resize_image_by_pil(
                input_image,
                self.scale,
                resampling_method=self.resampling_method)
            output_image = self.do(input_image, input_bicubic_y_image)
            mse = util.compute_mse(true_image,
                                   output_image,
                                   border_size=6 + self.scale)
            if output:
                util.save_image(output_directory + file_path, true_image)
                util.save_image(
                    output_directory + filename + "_result" + extension,
                    output_image)
        else:
            mse = 0

        if print_console:
            print("MSE:%f, PSNR:%f" % (mse, util.get_psnr(mse)))

        return mse
Beispiel #17
0
    def do_for_evaluate_with_output(self,
                                    file_path,
                                    output_directory=None,
                                    print_console=False):
        true_image = util.set_image_alignment(
            util.load_image(file_path, print_console=False), self.scale)

        # Assuming the image is color
        assert true_image.shape[
            2] == 3 and self.channels == 1, "Only 3-channel images are supported"

        input_image = loader.build_input_image(true_image,
                                               scale=self.scale,
                                               alignment=self.scale)

        input_y_image = util.convert_rgb_to_y(input_image)
        true_y_image = util.convert_rgb_to_y(true_image)
        input_bicubic_y_image = util.resize_image_by_pil(
            input_y_image,
            self.scale,
            resampling_method=self.resampling_method)

        output_y_image = self.do(input_y_image, input_bicubic_y_image)

        psnr, ssim = util.compute_psnr_and_ssim(
            true_y_image,
            output_y_image,
            border_size=self.psnr_calc_border_size)

        if output_directory:
            true_ycbcr_image = util.convert_rgb_to_ycbcr(true_image)
            _, true_cbcr = util.convert_ycbcr_to_y_cbcr(true_ycbcr_image)
            output_color_image = util.convert_y_and_cbcr_to_rgb(
                output_y_image, true_cbcr)

            loss_image = util.get_loss_image(
                true_y_image,
                output_y_image,
                border_size=self.psnr_calc_border_size)

            filename, extension = os.path.splitext(file_path)
            output_directory += "/" + self.name + "/"
            util.make_dir(output_directory)

            util.save_image(output_directory + file_path, true_image)
            util.save_image(output_directory + filename + "_input" + extension,
                            input_y_image)
            util.save_image(
                output_directory + filename + "_input_bicubic" + extension,
                input_bicubic_y_image)
            util.save_image(
                output_directory + filename + "_true_y" + extension,
                true_ycbcr_image[:, :, 0:1])
            util.save_image(
                output_directory + filename + "_result" + extension,
                output_y_image)
            util.save_image(
                output_directory + filename + "_result_c" + extension,
                output_color_image)
            util.save_image(output_directory + filename + "_loss" + extension,
                            loss_image)

        if print_console:
            print("[%s] PSNR:%f, SSIM:%f" % (filename, psnr, ssim))

        return psnr, ssim