Exemple #1
0
def _evaluate_denoise(sr_model : BaseSuperResolutionModel, validation_dir, scale_pred=False):
    print("Validating %s model" % sr_model.model_name)
    predict_path = "val_predict/"
    if not os.path.exists(predict_path):
        os.makedirs(predict_path)

    validation_path_set5 = validation_dir + "set5/"
    validation_path_set14 = validation_dir + "set14/"

    validation_dirs = [validation_path_set5, validation_path_set14]
    for val_dir in validation_dirs:
        image_fns = [name for name in os.listdir(val_dir)]
        nb_images = len(image_fns)
        print("Validating %d images from path %s" % (nb_images, val_dir))

        total_psnr = 0.0

        for impath in os.listdir(val_dir):
            t1 = time.time()

            # Input image
            y = img_utils.imread(val_dir + impath, mode='RGB')
            width, height, _ = y.shape

            if ((width // sr_model.scale_factor) % 4 != 0) or ((height // sr_model.scale_factor) % 4 != 0) \
                    or (width % 2 != 0) or (height % 2 != 0):
                width = ((width // sr_model.scale_factor) // 4) * 4 * sr_model.scale_factor
                height = ((height // sr_model.scale_factor) // 4) * 4 * sr_model.scale_factor

                print("Model %s require the image size to be divisible by 4. New image size = (%d, %d)" % \
                      (sr_model.model_name, width, height))

                y = img_utils.imresize(y, (width, height), interp='bicubic')

            y = y.astype('float32')
            y = np.expand_dims(y, axis=0)

            x_temp = y.copy()

            if sr_model.type_scale_type == "tanh":
                x_temp = (x_temp - 127.5) / 127.5
                y = (y - 127.5) / 127.5
            else:
                x_temp /= 255.
                y /= 255.

            img = img_utils.imresize(x_temp[0], (width // sr_model.scale_factor, height // sr_model.scale_factor),
                                     interp='bicubic', mode='RGB')

            if not sr_model.type_true_upscaling:
                img = img_utils.imresize(img, (width, height), interp='bicubic')

            x = np.expand_dims(img, axis=0)

            if K.image_dim_ordering() == "th":
                x = x.transpose((0, 3, 1, 2))
                y = y.transpose((0, 3, 1, 2))

            sr_model.model = sr_model.create_model(height, width, load_weights=True)

            if sr_model.evaluation_func is None:
                if sr_model.uses_learning_phase:
                    sr_model.evaluation_func = K.function([sr_model.model.layers[0].input, K.learning_phase()],
                                                          [sr_model.model.layers[-1].output])
                else:
                    sr_model.evaluation_func = K.function([sr_model.model.layers[0].input],
                                                          [sr_model.model.layers[-1].output])

            if sr_model.uses_learning_phase:
                y_pred = sr_model.evaluation_func([x, 0])[0][0]
            else:
                y_pred = sr_model.evaluation_func([x])[0][0]

            if scale_pred:
                if sr_model.type_scale_type == "tanh":
                    y_pred = (y_pred + 1) * 127.5
                else:
                    y_pred *= 255.

            if sr_model.type_scale_type == 'tanh':
                y = (y + 1) / 2

            psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255)
            total_psnr += psnr_val

            t2 = time.time()
            print("Validated image : %s, Time required : %0.2f, PSNR value : %0.4f" % (impath, t2 - t1, psnr_val))

            generated_path = predict_path + "%s_%s_generated.png" % (sr_model.model_name, os.path.splitext(impath)[0])

            if K.image_dim_ordering() == "th":
                y_pred = y_pred.transpose((1, 2, 0))

            y_pred = np.clip(y_pred, 0, 255).astype('uint8')
            img_utils.imsave(generated_path, y_pred)

        print("Average PRNS value of validation images = %00.4f \n" % (total_psnr / nb_images))
Exemple #2
0
    def evaluate(self, validation_dir, scale_factor, target_size=256, small_train_images=False):
        """
        Evaluates the model on the Set5 Validation images
        """
        if self.model == None: self.create_model(load_weights=True, small_train_images=small_train_images)

        if self.evaluation_func is None:
            self.evaluation_func = K.function([self.model.layers[0].input],
                                              [self.model.layers[-1].output])

        predict_path = "val_predict/"
        if not os.path.exists(predict_path):
            os.makedirs(predict_path)

        image_fns = [name for name in os.listdir(validation_dir)]
        nb_images = len(image_fns)
        print("Validating %d images" % (nb_images))

        total_psnr = 0.0

        for impath in os.listdir(validation_dir):
            t1 = time.time()

            # Input image
            y = img_utils.imread(validation_dir + impath, mode='RGB')
            width, height, _ = y.shape

            if self.model_name in self.denoise_models:
                # Denoise models require precise width and height, divisible by 4

                if ((width // scale_factor) % 4 != 0) or ((height // scale_factor) % 4 != 0):
                    width = ((width // scale_factor) // 4) * 4 * scale_factor
                    height = ((height // scale_factor) // 4) * 4 * scale_factor

                    print("Model %s require the image size to be divisible by 4. New image size = (%d, %d)" % \
                                                                                (self.model_name, width, height))

                    y = img_utils.imresize(y, (width, height), interp='bicubic')

            y = y.astype('float32') / 255.
            y = np.expand_dims(y, axis=0)

            x_width = width if not small_train_images else width // scale_factor
            x_height = height if not small_train_images else height // scale_factor

            x_temp = y.copy()
            img = img_utils.gaussian_filter(x_temp[0], sigma=0.01)
            img = img_utils.imresize(img, (x_width // scale_factor, x_height // scale_factor), interp='bicubic')

            if not small_train_images:
                img = img_utils.imresize(img, (x_width, x_height), interp='bicubic')

            x = np.expand_dims(img, axis=0)

            if K.image_dim_ordering() == "th":
                x = x.transpose((0, 3, 1, 2))
                y = y.transpose((0, 3, 1, 2))

            y_pred = self.evaluation_func([x])[0][0]

            psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255)
            total_psnr += psnr_val

            t2 = time.time()
            print("Validated image : %s, Time required : %0.2f, PSNR value : %0.4f" % (impath, t2 - t1, psnr_val))

            generated_path = predict_path + "%s_generated.png" % (os.path.splitext(impath)[0])

            if K.image_dim_ordering() == "th":
                y_pred = y_pred.transpose((1, 2, 0))

            y_pred = np.clip(y_pred, 0, 255).astype('uint8')
            img_utils.imsave(generated_path, y_pred)

        print("Average PRNS value of validation images = %00.4f" % (total_psnr / nb_images))
    def evaluate(self,
                 validation_dir,
                 scale_factor,
                 target_size=256,
                 small_train_images=False):
        """
        Evaluates the model on the Set5 Validation images
        """
        if self.model == None:
            self.create_model(load_weights=True,
                              small_train_images=small_train_images)

        if self.evaluation_func is None:
            self.evaluation_func = K.function([self.model.layers[0].input],
                                              [self.model.layers[-1].output])

        predict_path = "val_predict/"
        if not os.path.exists(predict_path):
            os.makedirs(predict_path)

        image_fns = [name for name in os.listdir(validation_dir)]
        nb_images = len(image_fns)
        print("Validating %d images" % (nb_images))

        total_psnr = 0.0

        for impath in os.listdir(validation_dir):
            t1 = time.time()

            # Input image
            y = img_utils.imread(validation_dir + impath, mode='RGB')
            width, height, _ = y.shape

            if self.model_name in self.denoise_models:
                # Denoise models require precise width and height, divisible by 4

                if ((width // scale_factor) % 4 != 0) or (
                    (height // scale_factor) % 4 != 0):
                    width = ((width // scale_factor) // 4) * 4 * scale_factor
                    height = ((height // scale_factor) // 4) * 4 * scale_factor

                    print("Model %s require the image size to be divisible by 4. New image size = (%d, %d)" % \
                                                                                (self.model_name, width, height))

                    y = img_utils.imresize(y, (width, height),
                                           interp='bicubic')

            y = y.astype('float32') / 255.
            y = np.expand_dims(y, axis=0)

            x_width = width if not small_train_images else width // scale_factor
            x_height = height if not small_train_images else height // scale_factor

            x_temp = y.copy()
            img = img_utils.gaussian_filter(x_temp[0], sigma=0.01)
            img = img_utils.imresize(
                img, (x_width // scale_factor, x_height // scale_factor),
                interp='bicubic')

            if not small_train_images:
                img = img_utils.imresize(img, (x_width, x_height),
                                         interp='bicubic')

            x = np.expand_dims(img, axis=0)

            if K.image_dim_ordering() == "th":
                x = x.transpose((0, 3, 1, 2))
                y = y.transpose((0, 3, 1, 2))

            y_pred = self.evaluation_func([x])[0][0]

            psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255)
            total_psnr += psnr_val

            t2 = time.time()
            print(
                "Validated image : %s, Time required : %0.2f, PSNR value : %0.4f"
                % (impath, t2 - t1, psnr_val))

            generated_path = predict_path + "%s_generated.png" % (
                os.path.splitext(impath)[0])

            if K.image_dim_ordering() == "th":
                y_pred = y_pred.transpose((1, 2, 0))

            y_pred = np.clip(y_pred, 0, 255).astype('uint8')
            img_utils.imsave(generated_path, y_pred)

        print("Average PRNS value of validation images = %00.4f" %
              (total_psnr / nb_images))
Exemple #4
0
    def evaluate(self, validation_dir, small_train_images=False):
        print("Validating %s model" % self.model_name)

        predict_path = "val_predict/"
        if not os.path.exists(predict_path):
            os.makedirs(predict_path)

        validation_path_set5 = validation_dir + "set5/"
        validation_path_set14 = validation_dir + "set14/"

        validation_dirs = [validation_path_set5, validation_path_set14]

        for val_dir in validation_dirs:
            image_fns = [name for name in os.listdir(val_dir)]
            nb_images = len(image_fns)
            print("Validating %d images from path %s" % (nb_images, val_dir))

            total_psnr = 0.0

            for impath in os.listdir(val_dir):
                t1 = time.time()

                # Input image
                y = img_utils.imread(val_dir + impath, mode='RGB')
                width, height, _ = y.shape

                y = y.astype('float32') / 255.
                y = np.expand_dims(y, axis=0)

                x_temp = y.copy()
                img = img_utils.imresize(
                    x_temp[0],
                    (width // self.scale_factor, height // self.scale_factor),
                    interp='bicubic')

                if not small_train_images:
                    img = img_utils.imresize(img, (width, height),
                                             interp='bicubic')

                x = np.expand_dims(img, axis=0)

                if K.image_dim_ordering() == "th":
                    x = x.transpose((0, 3, 1, 2))
                    y = y.transpose((0, 3, 1, 2))

                self.model = self.create_model(height,
                                               width,
                                               load_weights=True)

                self.evaluation_func = K.function(
                    [self.model.layers[0].input],
                    [self.model.layers[-1].output])

                y_pred = self.evaluation_func([x])[0][0]

                psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255)
                total_psnr += psnr_val

                t2 = time.time()
                print(
                    "Validated image : %s, Time required : %0.2f, PSNR value : %0.4f"
                    % (impath, t2 - t1, psnr_val))

                generated_path = predict_path + "%s_%s_generated.png" % (
                    self.model_name, os.path.splitext(impath)[0])

                if K.image_dim_ordering() == "th":
                    y_pred = y_pred.transpose((1, 2, 0))

                y_pred = np.clip(y_pred, 0, 255).astype('uint8')
                img_utils.imsave(generated_path, y_pred)

            print("Average PRNS value of validation images = %00.4f \n" %
                  (total_psnr / nb_images))