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)
def build_input_image(image, width=0, height=0, channels=1, scale=1, alignment=0, convert_ycbcr=True): """ build input image from file. crop, adjust the image alignment for the scale factor, resize, convert color space. """ if width != 0 and height != 0: if image.shape[0] != height or image.shape[1] != width: x = (image.shape[1] - width) // 2 y = (image.shape[0] - height) // 2 image = image[y:y + height, x:x + width, :] if alignment > 1: image = util.set_image_alignment(image, alignment) if channels == 1 and image.shape[2] == 3: if convert_ycbcr: image = util.convert_rgb_to_y(image) else: if convert_ycbcr: image = util.convert_rgb_to_ycbcr(image) if scale != 1: image = util.resize_image_by_pil(image, 1.0 / scale) return image
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) input_image = util.resize_image_by_pil(true_image, 1.0/ self.scale, resampling_method=self.resampling_method) input_bicubic_image = util.resize_image_by_pil(input_image, self.scale, resampling_method=self.resampling_method) util.save_image(output_directory + filename + "_input_bicubic" + extension, input_bicubic_image) 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) output_y_image = self.do(input_y_image, input_bicubic_y_image) 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]) 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_y" + 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) output_image = self.do(input_image, input_bicubic_y_image) psnr, ssim = util.compute_psnr_and_ssim(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 + "_result" + extension, output_image) else: return None, None if print_console: print("[%s] PSNR:%f, SSIM:%f" % (filename, psnr, ssim)) return psnr, ssim
def doframe(self, org_image): 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) output_y_image = self.do(input_y_image) scaled_ycbcr_image = util.convert_rgb_to_ycbcr( util.resize_image_by_pil(org_image, self.scale, self.resampling_method)) return util.convert_y_and_cbcr_to_rgb(output_y_image, scaled_ycbcr_image[:, :, 1:3])
def predict_im(self, 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) output_y_image = self.do(input_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: image = self.do(org_image) return image
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 do_for_evaluate(self, file_path, output_directory="output", output=True, print_console=True): filename, extension = os.path.splitext(file_path) output_directory += "/" true_image = util.set_image_alignment(util.load_image(file_path), 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) 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=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) output_y_image = self.do(input_y_image) mse = util.compute_mse(true_y_image, output_y_image, border_size=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) output_image = self.do(input_image) mse = util.compute_mse(true_image, output_image, border_size=self.scale) if output: util.save_image(output_directory + file_path, true_image) util.save_image(output_directory + filename + "_result" + extension, output_image) if print_console: print("MSE:%f PSNR:%f" % (mse, util.get_psnr(mse))) return mse
def upscale(self, org_image): assert 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) big_blurry_input_image = util.resize_image_by_pil( org_image, self.scale) big_blurry_input_ycbcr_image = util.convert_rgb_to_ycbcr( big_blurry_input_image) bbi_input_y_image, bbi_input_cbcr_image = util.convert_ycbcr_to_y_cbcr( big_blurry_input_ycbcr_image) output_y_image = self.do(input_y_image, bbi_input_y_image) output_image = util.convert_y_and_cbcr_to_rgb(output_y_image, bbi_input_cbcr_image) return output_image
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)
def do_for_file(self, file_path, output_folder="output"): org_image = cv2.imread(file_path) assert 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) big_blurry_input_image = util.resize_image_by_pil( org_image, self.scale) big_blurry_input_ycbcr_image = util.convert_rgb_to_ycbcr( big_blurry_input_image) bbi_input_y_image, bbi_input_cbcr_image = util.convert_ycbcr_to_y_cbcr( big_blurry_input_ycbcr_image) output_y_image = self.do(input_y_image, bbi_input_y_image) output_image = util.convert_y_and_cbcr_to_rgb(output_y_image, bbi_input_cbcr_image) target_path = os.path.basename(file_path) cv2.imwrite(os.path.join(output_folder, target_path), output_image)
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)
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))
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