def upscale(self, img_path, save_intermediate=False, return_image=False, suffix="scaled", patch_size=8, mode="patch", verbose=True): """ Standard method to upscale an image. :param img_path: path to the image :param save_intermediate: saves the intermediate upscaled image (bilinear upscale) :param return_image: returns a image of shape (height, width, channels). :param suffix: suffix of upscaled image :param patch_size: size of each patch grid :param verbose: whether to print messages :param mode: mode of upscaling. Can be "patch" or "fast" """ import os from scipy.misc import imread, imresize, imsave # Destination path path = os.path.splitext(img_path) filename = path[0] + "_" + suffix + "(%dx)" % (self.scale_factor) + path[1] # Read image scale_factor = int(self.scale_factor) true_img = imread(img_path, mode='RGB') init_width, init_height = true_img.shape[0], true_img.shape[1] if verbose: print("Old Size : ", true_img.shape) if verbose: print("New Size : (%d, %d, 3)" % (init_height * scale_factor, init_width * scale_factor)) img_height, img_width = 0, 0 if mode == "patch" and self.type_true_upscaling: # Overriding mode for True Upscaling models mode = 'fast' print("Patch mode does not work with True Upscaling models yet. Defaulting to mode='fast'") if mode == 'patch': # Create patches if self.type_requires_divisible_shape: if patch_size % 4 != 0: print("Deep Denoise requires patch size which is multiple of 4.\nSetting patch_size = 8.") patch_size = 8 images = img_utils.make_patches(true_img, scale_factor, patch_size, verbose) nb_images = images.shape[0] img_width, img_height = images.shape[1], images.shape[2] print("Number of patches = %d, Patch Shape = (%d, %d)" % (nb_images, img_height, img_width)) else: # Use full image for super resolution img_width, img_height = self.__match_autoencoder_size(img_height, img_width, init_height, init_width, scale_factor) images = imresize(true_img, (img_width, img_height)) images = np.expand_dims(images, axis=0) print("Image is reshaped to : (%d, %d, %d)" % (images.shape[1], images.shape[2], images.shape[3])) # Save intermediate bilinear scaled image is needed for comparison. intermediate_img = None if save_intermediate: if verbose: print("Saving intermediate image.") fn = path[0] + "_intermediate_" + path[1] intermediate_img = imresize(true_img, (init_width * scale_factor, init_height * scale_factor)) imsave(fn, intermediate_img) # Transpose and Process images if K.image_dim_ordering() == "th": img_conv = images.transpose((0, 3, 1, 2)).astype(np.float32) / 255. else: img_conv = images.astype(np.float32) / 255. model = self.create_model(img_height, img_width, load_weights=True) if verbose: print("Model loaded.") # Create prediction for image patches result = model.predict(img_conv, batch_size=128, verbose=verbose) if verbose: print("De-processing images.") # Deprocess patches if K.image_dim_ordering() == "th": result = result.transpose((0, 2, 3, 1)).astype(np.float32) * 255. else: result = result.astype(np.float32) * 255. # Output shape is (original_width * scale, original_height * scale, nb_channels) if mode == 'patch': out_shape = (init_width * scale_factor, init_height * scale_factor, 3) result = img_utils.combine_patches(result, out_shape, scale_factor) else: result = result[0, :, :, :] # Access the 3 Dimensional image vector result = np.clip(result, 0, 255).astype('uint8') result = cv2.pyrUp(result) result = cv2.medianBlur(result, 3) result = cv2.pyrDown(result) if verbose: print("\nCompleted De-processing image.") if return_image: # Return the image without saving. Useful for testing images. return result if verbose: print("Saving image.") imsave(filename, result)
imsave(filename, result) #%% img_path = "/opt/EuroSAT/Test/output/Highway_47.jpg" #img_path = "/opt/EuroSAT/Test/output/Highway_58.jpg" #img_path = "/opt/EuroSAT/Test/output/Highway_63.jpg" path = os.path.splitext(img_path) filename = path[0] + "_" + suffix + "(%dx)" % (scaling_factor) + path[1] true_img = imread(img_path, mode='RGB') init_dim_1, init_dim_2 = true_img.shape[0], true_img.shape[1] if verbose: print("Old Size : ", true_img.shape) print("New Size : (%d, %d, 3)" % (init_dim_1 * scaling_factor, init_dim_2 * scaling_factor)) images = make_patches(true_img, scaling_factor, patch_size, verbose) nb_images = images.shape[0] img_dim_1, img_dim_2 = images.shape[1], images.shape[2] print("Number of patches = %d, Patch Shape = (%d, %d)" % (nb_images, img_dim_2, img_dim_1)) if verbose: print("Saving intermediate image...") fn = path[0] + "_intermediate_" + path[1] intermediate_img = imresize(true_img, (init_dim_1 * scaling_factor, init_dim_2 * scaling_factor)) imsave(fn, intermediate_img) img_conv = images.astype(np.float32) / 255. result = model.predict(img_conv, batch_size=64, verbose=verbose) if verbose: print("De-processing images...") result = result.astype(np.float32) * 255.
def upscale(self, img_path, scale_factor=2, save_intermediate=False, return_image=False, suffix="scaled", patch_size=8, mode="patch", verbose=True, evaluate=True): """ Standard method to upscale an image. :param img_path: path to the image :param scale_factor: scale factor can be any value, usually is an integer :param save_intermediate: saves the intermediate upscaled image (bilinear upscale) :param return_image: returns a image of shape (height, width, channels). :param suffix: suffix of upscaled image :param patch_size: size of each patch grid :param verbose: whether to print messages :param evaluate: evaluate the upscaled image on the original image. """ import os from scipy.misc import imread, imresize, imsave # Destination path path = os.path.splitext(img_path) filename = path[0] + "_" + suffix + "(%dx)" % (scale_factor) + path[1] # Read image scale_factor = int(scale_factor) true_img = imread(img_path, mode='RGB') init_width, init_height = true_img.shape[0], true_img.shape[1] if verbose: print("Old Size : ", true_img.shape) if verbose: print("New Size : (%d, %d, 3)" % (init_height * scale_factor, init_width * scale_factor)) img_height, img_width = 0, 0 if mode == 'patch': # Create patches if self.model_name in self.denoise_models: if patch_size % 4 != 0: print( "Deep Denoise requires patch size which is multiple of 4.\nSetting patch_size = 8." ) patch_size = 8 images = img_utils.make_patches(true_img, scale_factor, patch_size, verbose) nb_images = images.shape[0] img_width, img_height = images.shape[1], images.shape[2] print("Number of patches = %d, Patch Shape = (%d, %d)" % (nb_images, img_height, img_width)) else: # Use full image for super resolution img_width, img_height = self.__match_denoise_size( denoise_models, img_height, img_width, init_height, init_width, scale_factor) images = imresize(true_img, (img_width, img_height)) images = np.expand_dims(images, axis=0) print("Image is reshaped to : (%d, %d, %d)" % (images.shape[1], images.shape[2], images.shape[3])) # Save intermediate bilinear scaled image is needed for comparison. intermediate_img = None if save_intermediate: if verbose: print("Saving intermediate image.") fn = path[0] + "_intermediate_" + path[1] intermediate_img = imresize( true_img, (init_width * scale_factor, init_height * scale_factor)) imsave(fn, intermediate_img) # Transpose and Process images if K.image_dim_ordering() == "th": img_conv = images.transpose((0, 3, 1, 2)).astype(np.float32) / 255. else: img_conv = images.astype(np.float32) / 255. model = self.create_model(img_height, img_width, load_weights=True) if verbose: print("Model loaded.") # Create prediction for image patches result = model.predict(img_conv, batch_size=128, verbose=verbose) if verbose: print("De-processing images.") # Deprocess patches if K.image_dim_ordering() == "th": result = result.transpose((0, 2, 3, 1)).astype(np.float32) * 255. else: result = result.astype(np.float32) * 255. # Output shape is (original_width * scale, original_height * scale, nb_channels) if mode == 'patch': out_shape = (init_width * scale_factor, init_height * scale_factor, 3) result = img_utils.combine_patches(result, out_shape, scale_factor) else: result = result[ 0, :, :, :] # Access the 3 Dimensional image vector result = np.clip(result, 0, 255).astype('uint8') if verbose: print("\nCompleted De-processing image.") if return_image: # Return the image without saving. Useful for testing images. return result if verbose: print("Saving image.") imsave(filename, result) if evaluate: if verbose: print("Evaluating results.") # Convert initial image into correct format if intermediate_img is None: intermediate_img = imresize( true_img, (init_width * scale_factor, init_height * scale_factor)) if mode == 'patch': intermediate_img = img_utils.make_patches(intermediate_img, scale_factor, patch_size, upscale=False) else: img_width, img_height = self.__match_denoise_size( denoise_models, img_height, img_width, init_height, init_width, scale_factor) intermediate_img = imresize(true_img, (img_width, img_height)) intermediate_img = np.expand_dims(intermediate_img, axis=0) if K.image_dim_ordering() == "th": intermediate_img = intermediate_img.transpose( (0, 3, 1, 2)).astype(np.float32) / 255. else: intermediate_img = intermediate_img.astype(np.float32) / 255. eval_model = self.create_model(img_height, img_width, load_weights=True) # Evaluating the initial image patches, which gets transformed to the output image, to the input image error = eval_model.evaluate(img_conv, intermediate_img, batch_size=128) print("\nMean Squared Error of %s : " % (self.model_name), error[0]) print("Peak Signal to Noise Ratio of %s : " % (self.model_name), error[1])
def upscale(self, img_path, scale_factor=2, save_intermediate=False, return_image=False, suffix="scaled", patch_size=8, mode="patch", verbose=True, evaluate=True): """ Standard method to upscale an image. :param img_path: path to the image :param scale_factor: scale factor can be any value, usually is an integer :param save_intermediate: saves the intermediate upscaled image (bilinear upscale) :param return_image: returns a image of shape (height, width, channels). :param suffix: suffix of upscaled image :param patch_size: size of each patch grid :param verbose: whether to print messages :param evaluate: evaluate the upscaled image on the original image. """ import os from scipy.misc import imread, imresize, imsave # Destination path path = os.path.splitext(img_path) filename = path[0] + "_" + suffix + "(%dx)" % (scale_factor) + path[1] # Read image scale_factor = int(scale_factor) true_img = imread(img_path, mode='RGB') init_width, init_height = true_img.shape[0], true_img.shape[1] if verbose: print("Old Size : ", true_img.shape) if verbose: print("New Size : (%d, %d, 3)" % (init_height * scale_factor, init_width * scale_factor)) img_height, img_width = 0, 0 if mode == 'patch': # Create patches if self.model_name in self.denoise_models: if patch_size % 4 != 0: print("Deep Denoise requires patch size which is multiple of 4.\nSetting patch_size = 8.") patch_size = 8 images = img_utils.make_patches(true_img, scale_factor, patch_size, verbose) nb_images = images.shape[0] img_width, img_height = images.shape[1], images.shape[2] print("Number of patches = %d, Patch Shape = (%d, %d)" % (nb_images, img_height, img_width)) else: # Use full image for super resolution img_width, img_height = self.__match_denoise_size(denoise_models, img_height, img_width, init_height, init_width, scale_factor) images = imresize(true_img, (img_width, img_height)) images = np.expand_dims(images, axis=0) print("Image is reshaped to : (%d, %d, %d)" % (images.shape[1], images.shape[2], images.shape[3])) # Save intermediate bilinear scaled image is needed for comparison. intermediate_img = None if save_intermediate: if verbose: print("Saving intermediate image.") fn = path[0] + "_intermediate_" + path[1] intermediate_img = imresize(true_img, (init_width * scale_factor, init_height * scale_factor)) imsave(fn, intermediate_img) # Transpose and Process images if K.image_dim_ordering() == "th": img_conv = images.transpose((0, 3, 1, 2)).astype(np.float32) / 255. else: img_conv = images.astype(np.float32) / 255. model = self.create_model(img_height, img_width, load_weights=True) if verbose: print("Model loaded.") # Create prediction for image patches result = model.predict(img_conv, batch_size=128, verbose=verbose) if verbose: print("De-processing images.") # Deprocess patches if K.image_dim_ordering() == "th": result = result.transpose((0, 2, 3, 1)).astype(np.float32) * 255. else: result = result.astype(np.float32) * 255. # Output shape is (original_width * scale, original_height * scale, nb_channels) if mode == 'patch': out_shape = (init_width * scale_factor, init_height * scale_factor, 3) result = img_utils.combine_patches(result, out_shape, scale_factor) else: result = result[0, :, :, :] # Access the 3 Dimensional image vector result = np.clip(result, 0, 255).astype('uint8') if verbose: print("\nCompleted De-processing image.") if return_image: # Return the image without saving. Useful for testing images. return result if verbose: print("Saving image.") imsave(filename, result) if evaluate: if verbose: print("Evaluating results.") # Convert initial image into correct format if intermediate_img is None: intermediate_img = imresize(true_img, (init_width * scale_factor, init_height * scale_factor)) if mode == 'patch': intermediate_img = img_utils.make_patches(intermediate_img, scale_factor, patch_size, upscale=False) else: img_width, img_height = self.__match_denoise_size(denoise_models, img_height, img_width, init_height, init_width, scale_factor) intermediate_img = imresize(true_img, (img_width, img_height)) intermediate_img = np.expand_dims(intermediate_img, axis=0) if K.image_dim_ordering() == "th": intermediate_img = intermediate_img.transpose((0, 3, 1, 2)).astype(np.float32) / 255. else: intermediate_img = intermediate_img.astype(np.float32) / 255. eval_model = self.create_model(img_height, img_width, load_weights=True) # Evaluating the initial image patches, which gets transformed to the output image, to the input image error = eval_model.evaluate(img_conv, intermediate_img, batch_size=128) print("\nMean Squared Error of %s : " % (self.model_name), error[0]) print("Peak Signal to Noise Ratio of %s : " % (self.model_name), error[1])