def learn_denoising_model(num_res_blocks=5, quick_mode=False): """ returns trained denoised model :param num_res_blocks: number of res block in the network :param quick_mode: true or false value :return: the model. """ model = build_nn_model(24, 24, 48, num_res_blocks) if quick_mode: train_model(model, sol5_utils.images_for_denoising(), _gaussian_for_learn_denosing_model, 10, 3, 2, 30) return model train_model(model, sol5_utils.images_for_denoising(), _gaussian_for_learn_denosing_model, 100, 100, 5, 1000) return model
def test_add_gaussian_noise(): name = sol5_utils.images_for_denoising()[np.random.randint(100)] im = read_image(name, REP_GRAY) show_im(im, True) im = add_gaussian_noise(im, 0, 0.2) show_im(im, True) print(f'add_gaussian_noise to image {name}')
def test_add_motion_blur(): name = sol5_utils.images_for_denoising()[np.random.randint(100)] im = read_image(name, REP_GRAY) show_im(im, True) im = add_motion_blur(im, 7, np.pi / 2) show_im(im, True) print(f'add_motion_blur on image {name}')
def learn_denoising_model(num_res_blocks=5, quick_mode=False): ''' this function creates a trained neural-network model for image denoising :param num_res_blocks: num of residual blocks in the model :param quick_mode: an indicator which allows a quick model training :return: a trained model for image denoising ''' model = build_nn_model(PATCH_DENOISE_DIM, PATCH_DENOISE_DIM, DENOISE_OUT_CHANNELS, num_res_blocks) noisy_images_list = sol5_utils.images_for_denoising() if (quick_mode): batch_size = TRAIN_BATCH_SIZE_QUICK samples_num = VALID_SAMPLES_NUM_QUICK samples_per_epoch = SAMPLES_PER_EPOCH_QUICK epoch_num = EPOCHS_NUM_QUICK else: batch_size = TRAIN_BATCH_SIZE samples_num = VALID_SAMPLES_NUM samples_per_epoch = SAMPLES_PER_EPOCH epoch_num = NOISY_EPOCHS_NUM corruption_func = lambda img: add_gaussian_noise(img, TRAIN_MIN_SIGMA, TRAIN_MAX_SIGMA) train_model(model, noisy_images_list, corruption_func, batch_size, samples_per_epoch, epoch_num, samples_num) return model
def learn_model(learning, num_res_blocks=5, quick_mode=False): if quick_mode: batch_size = 10 steps_per_epoch = 3 epochs = 2 num_valid_samples = 30 else: batch_size = 100 steps_per_epoch = 100 epochs = 5 num_valid_samples = 1000 if learning == "denoising": model = build_nn_model(DENOISING_PATCH, DENOISING_PATCH, DENOSING_OUTPUT_CHANNEL, num_res_blocks) images = sol5_utils.images_for_denoising() corruption_func = lambda image: add_gaussian_noise( image, MIN_SIGMA, MAX_SIGMA) elif learning == "deblurring": model = build_nn_model(DEBLURRING_PATCH, DEBLURRING_PATCH, DEBLURRING_OUTPUT_CHANNEL, num_res_blocks) images = sol5_utils.images_for_deblurring() corruption_func = lambda image: random_motion_blur(image, [7]) else: return train_model(model, images, corruption_func, batch_size, steps_per_epoch, epochs, num_valid_samples) # The model is now trained return model
def learn_denoising_model(num_res_blocks=5, quick_mode=False): """ Learn denoising model :param num_res_blocks: number of residual blocks in the model :param quick_mode: :return: """ images = sol5_utils.images_for_denoising() if quick_mode: batch_size = 10 samples_per_epoch = 30 num_epochs = 2 num_valid_samples = 30 else: batch_size = 100 samples_per_epoch = 10000 num_epochs = 5 num_valid_samples = 1000 model = build_nn_model(24, 24, 48, num_res_blocks) train_model(model, images, lambda image: add_gaussian_noise(image, 0, 0.2), batch_size, samples_per_epoch, num_epochs, num_valid_samples) return model
def learn_denoising_model(num_res_blocks=5, quick_mode=False): """ This function is creating and learning a denoising model. For validity check use quick mode. :param num_res_blocks: The amount of residual blocks in the nn network wanted. default is 5. :param quick_mode: Boolean, set to True, then the process wil be quick (but not necessarily create a good model) :return: Model, Keras neural network trained model. """ file_names = sol5_utils.images_for_denoising() min_sig = 0. max_sig = 0.2 corruption_func = lambda img: add_gaussian_noise( img, min_sigma=min_sig, max_sigma=max_sig) patch_size = PATCH_SIZE_NOISE channels = CHANNELS_DENOISINING batch_size = BATCH_SIZE samples_per_epoch = SAMPLES_PER_EPOCH num_epochs = 5 num_validation = SAMPLES_VALIDATION if quick_mode: batch_size = QUICK_BATCH_SIZE samples_per_epoch = QUICK_SAMPLES_PER_EPOCH num_epochs = QUICK_NUM_EPOCHS num_validation = QUICK_VALIDATION_SAMPLES model = build_nn_model(patch_size, patch_size, channels, num_res_blocks) train_model(model, file_names, corruption_func=corruption_func, batch_size=batch_size, sampels_per_epoch=samples_per_epoch, num_epochs=num_epochs, num_valid_samples=num_validation) return model
def learn_denoising_model(num_res_blocks=5, quick_mode=False): """ This function return a trained denoising model given the function - add_gaussian_noise. :param num_res_blocks: The number of residual blocks. :param quick_mode: quick mode for the above function for testing purposes. :return: A trained model """ images_for_denoising = sol5_utils.images_for_denoising() corruption_func = lambda x: add_gaussian_noise(x, 0, 0.2) model = build_nn_model(24, 24, 48, num_res_blocks) if quick_mode: batch_size = 10 steps_per_epoch = 3 num_epochs = 2 num_valid_samples = 30 else: batch_size = 100 steps_per_epoch = 100 num_epochs = 5 num_valid_samples = 1000 train_model(model, images_for_denoising, corruption_func, batch_size, steps_per_epoch, num_epochs, num_valid_samples) return model
def learn_denoising_model(num_res_blocks=5, quick_mode=False): ''' Used for trainning a neural network to denoise images with iid type gausian blur :param num_res_blocks: The number of residual blocks in the network :param quick_mode: Bool values representing if trainning is done in the fast mode :return: The trained model ''' def corruption_func(im): return add_gaussian_noise(im, DENOISE_MIN_SIGMA, DENOISE_MAX_SIGMA) # Get images to denoise images = sol5_utils.images_for_denoising() # Inits a neural network model model = build_nn_model(DENOISE_PATCH_SIZE, DENOISE_PATCH_SIZE, DENOISE_CHANNELS_NUM, num_res_blocks) # Train The model if quick_mode: train_model(model, images, corruption_func, QUICK_BATCH_NUM, QUICK_STEPS_PER_EPOCH, QUICK_TOTAL_EPOCH, QUICK_VALIDATION_SET) else: train_model(model, images, corruption_func, BATCH_NUM, STEPS_PER_EPOCH, DENOISE_TOTAL_EPOCH, VALIDATION_SET) return model
def __init__(self, json_file="denoising.json", weights="denoising.h5"): self._corrupted_handler = RestoreCorruptedHandler() self._json_file = json_file self._weights = weights self._random_corrupt = lambda x, y: sol.add_gaussian_noise( x, y, self.MAX_SIGMA) self._model = sol.learn_denoising_model self._corrupted_images = sol5_utils.images_for_denoising()
def learn_denoising_model(quick_mode=False): files = sol5_utils.images_for_denoising() num_channels = 48 model = build_nn_model(24, 24, num_channels) if quick_mode: train_model(model, files, lambda x: add_gaussian_noise(x, 0, 0.2), 10, 30, 2, 30) else: train_model(model, files, lambda x: add_gaussian_noise(x, 0, 0.2), 100, 10000, 5, 1000) return model, num_channels
def learn_denoising_model(num_res_blocks=5, quick_mode=False): filenames = sol5_utils.images_for_denoising() model = build_nn_model(DENOISE_PATCH_HEIGHT, DENOISE_PATCH_WIDTH, DENOISE_NUM_CHANNELS, num_res_blocks) corr_func = lambda im: add_gaussian_noise(im, 0, 0.2) if quick_mode: train_model(model, filenames, corr_func, QUICK_BATCH_SIZE, QUICK_STEPS_PER_EPOCH, QUICK_NUM_EPOCH, QUICK_NUM_VALID_SAMPLES) else: train_model(model, filenames, corr_func, BATCH_SIZE, STEPS_PER_EPOCH, DENOISE_NUM_EPOCH, NUM_VALID_SAMPLES) return model
def test_load_dataset(): CROP_SIZE = 300 filenames = sol5_utils.images_for_denoising() batch_size = 1 corruption_func = lambda im: add_gaussian_noise(im, 0, 0.2) crop_size = (CROP_SIZE, CROP_SIZE) generator = load_dataset(filenames, batch_size, corruption_func, crop_size) source_batch, target_batch = next(generator) assert source_batch.shape == (batch_size, CROP_SIZE, CROP_SIZE, 1) assert target_batch.shape == (batch_size, CROP_SIZE, CROP_SIZE, 1) show_im(source_batch[0], True) show_im(target_batch[0], True)
def learn_denoising_model(num_res_blocks=5, quick_mode=False): """ A method for learning a model for denoising pictures :param num_res_blocks: The number of ResNet blocks to be in the trained model :param quick_mode: A boolean to indicate if we're on quick mode :return: A trained model for denoising pictures """ images = sol5_utils.images_for_denoising() model = build_nn_model(24, 24, 48, num_res_blocks) lambda_noise = lambda x: add_gaussian_noise(x, 0, 0.2) if quick_mode: train_model(model, images, lambda_noise, 10, 30, 2, 30) else: train_model(model, images, lambda_noise, 100, 10000, 5, 1000) return model
def learn_denoising_model(quick_mode=False): """ :param quick_mode: :return:denoising_model - Trained nn, DENOISING_NUM_CHANNEL - number of channel we used. """ lam_cor = lambda im: add_gaussian_noise(im, GAUS_NOISE_MIN, GAUS_NOISE_MAX) ims = util.images_for_denoising() ##Build model for desnosing denoising_model = build_nn_model(DENOISING_HEIGHT, DENOISING_WIDTH, DENOISING_NUM_CHANNEL) ##TODO change qucikmode to orig train_model(denoising_model, ims, lam_cor, 10, 30, 2, 30) if quick_mode else \ train_model(denoising_model, ims, lam_cor, 100, 10000, 5, 1000) return denoising_model, DENOISING_NUM_CHANNEL
def learn_denoising_model(num_res_blocks=5, quick_mode=False): images = sol5_utils.images_for_denoising() corruption_func = lambda image: add_gaussian_noise(image, 0.0, 0.2) model = build_nn_model(24, 24, 48, num_res_blocks) batch_size = 100 steps_per_epoch = 100 num_epochs = 5 num_valid_samples = 1000 if quick_mode: batch_size = 10 steps_per_epoch = 3 num_epochs = 2 num_valid_samples = 30 train_model(model, images, corruption_func, batch_size, steps_per_epoch, num_epochs, num_valid_samples) return model
def learn_denoising_model(num_res_blocks=5, quick_mode=False): """ create and train a model of denoising images :param num_res_blocks: number of blocks in the model :param quick_mode: helper argument for less work :return: a trained denoising model """ images_for_denoising = sol5_utils.images_for_denoising() model = build_nn_model(24, 24, 48, num_res_blocks) batch_size, steps_per_epoch, num_epochs, num_valid_samples = 100, 100, 5, 1000 if quick_mode: batch_size, steps_per_epoch, num_epochs, num_valid_samples = 10, 3, 2, 30 train_model(model, images_for_denoising, lambda im: add_gaussian_noise(im, 0, 0.2), batch_size, steps_per_epoch, num_epochs, num_valid_samples) return model
def test_denoise(): num_ims = 10 model_path = "model_q.h5" print("Start denoising test") # if os.path.exists(model_path): model, num_channels = learn_denoising_model(True) model.save_weights(model_path) for i,im_path in enumerate(np.random.choice(sol5_utils.images_for_denoising(), size=num_ims)): print("Start denoising im num: {0}/{1}".format(i+1, num_ims)) im = read_image(im_path, 1) corrupt_im = add_gaussian_noise(im, 0, 0.2) restored_im = restore_image(corrupt_im, model, num_channels) show(im, corrupt_im, restored_im) plt.show() print("Done denoising test")
def test_learn_denoising_model(num_res_block=5, quick_mode=False): model = build_nn_model(24, 24, 48, num_res_block) model.load_weights("datasets/model_weights_denoising") name = sol5_utils.images_for_denoising()[np.random.randint(100)] image = read_image(name, REP_GRAY) cur_image = add_gaussian_noise(image, 0, 0.2) fixed_im = restore_image(cur_image, model) difference = cur_image - fixed_im show_im(image, True, 'original') show_im(cur_image, True, 'currpted') show_im(fixed_im, True, 'fixed') print(f'avarage change is {np.average(difference)}') print(f'Standard deviation of change is {np.std(difference)}')
def test_denoise_wo_train(): num_ims = 10 model_path = "model.h5" print("Start denoising w/o train - test") # if os.path.exists(model_path): patch_size = (24, 24) num_channels = 48 model = build_nn_model(*patch_size, num_channels) model.load_weights(model_path) for i, im_path in enumerate(np.random.choice(sol5_utils.images_for_denoising(), size=num_ims)): print("Start denoising im num: {0}/{1}".format(i + 1, num_ims)) im = read_image(im_path, 1) corrupt_im = add_gaussian_noise(im, 0, 0.2) restored_im = restore_image(corrupt_im, model, num_channels) show(im, corrupt_im, restored_im) plt.show() print("Done denoising test")
def test_super_res(): num_ims = 10 model_path = "model_super.h5" print("Start superres test") # if os.path.exists(model_path): model, num_channels = learn_super_resolution_model() model.save_weights(model_path) for i,im_path in enumerate(np.random.choice(sol5_utils.images_for_denoising(), size=num_ims)): print("Start superres im num: {0}/{1}".format(i+1, num_ims)) im = read_image(im_path, 1) corrupt_im = expand(im, np.array([[0.25,0.5,0.25]])) restored_im = restore_image(corrupt_im, model, num_channels) plt.figure() plt.subplot(1,2,1) plt.imshow(corrupt_im) plt.subplot(1,2,2) plt.imshow(restored_im) plt.show() print("Done superres test")
def learn_denoising_model(num_res_blocks=5, quick_mode=False): """ train a denoising model :param num_res_blocks: The number of residual blocks in the net :param quick_mode: bool. set other params in order to train the model faster :return: trained denoising model """ image_paths_list = sol5_utils.images_for_denoising() model = build_nn_model(height=24, width=24, num_channels=48, num_res_blocks=num_res_blocks) def inner(image): return add_gaussian_noise(image, min_sigma=0, max_sigma=0.2) if not quick_mode: train_model(model, image_paths_list, inner, batch_size=100, steps_per_epoch=100, num_epochs=5, num_valid_samples=1000) else: train_model(model, image_paths_list, inner, batch_size=10, steps_per_epoch=3, num_epochs=2, num_valid_samples=30) return model
def learn_denoising_model(num_res_blocks=5, quick_mode=False): """ Trains a neural network for denoising images with iid gaussian blur. :param num_res_blocks: number of residual blocks in the network :param quick_mode: True trains the network faster but with lower parameters :return: a trained model """ def corruption_func(im): return add_gaussian_noise(im, LDN_MIN_SIG, LDN_MAX_SIG) images = sol5_utils.images_for_denoising() model = build_nn_model(LDN_PATCH_SIZE, LDN_PATCH_SIZE, LDN_CHANNELS, num_res_blocks) if quick_mode: train_model_quick_mode(model, images, corruption_func) else: train_model(model, images, corruption_func, BATCH_SIZE, SAMPLES_PER_EPOCH, LDN_EPOCHS, VALID_SAMPLES) return model
def test_suprres_wo_train(): num_ims = 3 model_path = "model_super.h5" print("Start superres w/o train - test") # if os.path.exists(model_path): patch_size = (32, 32) num_channels = 32 model = build_nn_model(*patch_size, num_channels) model.load_weights(model_path) for i, im_path in enumerate(np.random.choice(sol5_utils.images_for_denoising(), size=num_ims)): print("Start superres im num: {0}/{1}".format(i + 1, num_ims)) im = read_image(im_path, 1) imc = read_image(im_path, 2) corrupt_imc = np.zeros((imc.shape[0]*2, imc.shape[1]*2, 3)) restored_imc = np.zeros(corrupt_imc.shape) corrupt_im = expand(im, np.array([[0.25, 0.5, 0.25]])) restored_im = restore_image(corrupt_im, model, num_channels) for c in range(3): corrupt_imc[:,:,c] = expand(imc[:,:,c], np.array([[0.25, 0.5, 0.25]])) restored_imc[:,:,c] = restore_image(corrupt_imc[:,:,c], model, num_channels) plt.figure() plt.subplot(2, 3, 1) plt.imshow(im, cmap=plt.cm.gray) plt.subplot(2, 3, 2) plt.imshow(corrupt_im, cmap=plt.cm.gray) plt.subplot(2, 3, 3) plt.imshow(restored_im, cmap=plt.cm.gray) plt.subplot(2, 3, 4) plt.imshow(imc) plt.subplot(2, 3, 5) plt.imshow(corrupt_imc) plt.subplot(2, 3, 6) plt.imshow(restored_imc) plt.show() print("Done debluring test")
def learn_denoising_model(num_res_blocks=parm.NUM_RES_BLOCKS, quick_mode=True): """ Train a network which expect patches of size 16×16, and have 32 channels. :param num_res_blocks: the number of the residual blocks. :param quick_mode: use only 10 images in a batch, 30 samples per epoch, just 2 epochs and only 30 samples for the validation set. :return: a trained denoised model. """ num_channels = parm.NUM_CHANNELS * 2 paths = sol5_utils.images_for_denoising() den_model = build_nn_model(parm.MODEL_HEIGHT_NOISE, parm.MODEL_WIDTH_NOISE, num_channels, num_res_blocks) corrption_func = lambda img: add_gaussian_noise(img, 0, parm.NOISE_FACTOR) if quick_mode: train_model(den_model, paths, corrption_func, parm.BATCH_SIZE_SMALL, parm.SAMPLES_PER_EPOCH_SMALL, parm.SMALL_EPOCH, parm.VALID_SAMPLES_SMALL) else: train_model(den_model, paths, corrption_func, parm.BATCH_SIZE, parm.SAMPLES_PER_EPOCH_BIG, parm.BIG_EPOCH, parm.VALID_SAMPLES_BIG) return den_model
def learn_denoising_model(quick_mode=False): """ The above method trains a network which expect patches of size 24×24, using 48 channels for all but the last layer. The corruption used is a gaussian noise with sigma in the range [0, 0.2]. :param quick_mode: a single argument used solely for the presubmission phase :return: model, num_channels - The train models and the number fo channels used in it """ images = sol5_utils.images_for_denoising() patch_size = (24, 24) num_channels = 48 sigma_range = (0, 0.2) im_per_batch = 100 if not quick_mode else 10 samples_per_epoch = 10000 if not quick_mode else 30 total_epochs = 5 if not quick_mode else 2 samples_for_validation = 1000 if not quick_mode else 30 corrupt_im = lambda im: add_gaussian_noise(im, *sigma_range) model = build_nn_model(*patch_size, num_channels) train_model(model, images, corrupt_im, im_per_batch, samples_per_epoch, total_epochs, samples_for_validation) return model, num_channels
def learn_denoising_model(quick_mode: bool = False) -> tuple: """ train a network to fix noising images :param quick_mode: if true, train with test-parameters to save time :return: tuple contains a trained denoising model, and the number of channels used in its construction """ im_list = sol5_utils.images_for_denoising() num_channels = DENOIS_CHANNELS model = build_nn_model(height=DENOIS_PATCH_SIZE, width=DENOIS_PATCH_SIZE, num_channels=num_channels) train_model( model=model, images=im_list, corruption_func=lambda x: add_gaussian_noise(x, DENOIS_MIN_SIG, DENOIS_MAX_SIG), batch_size=DENOIS_BATCH_SIZE if not quick_mode else DENOIS_TEST_BATCH_SIZE, samples_per_epoch=DENOIS_EPOCH_SIZE if not quick_mode else DENOIS_TEST_EPOCH_SIZE, num_epochs=DENOIS_EPOCH_NB if not quick_mode else DENOIS_TEST_EPOCH_NB, num_valid_samples=DENOIS_VALID_SIZE if not quick_mode else DENOIS_TEST_VALID_SIZE) return model, num_channels
def main(test): if test == 1: im = read_image("image_dataset/train/3096.jpg", 1) noised_im = add_gaussian_noise(im, 0, 0.2) plt.imshow(noised_im, cmap=plt.cm.gray) plt.show() if test == 5: im = read_image("text_dataset/train/0000005_orig.png", 1) noised_im = random_motion_blur(im, [7]) model, channels = learn_deblurring_model(True) restored = restore_image(noised_im, model, channels) fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1, label="noise") ax1.title.set_text("noise") plt.imshow(noised_im, cmap=plt.cm.gray) ax2 = fig.add_subplot(1, 2, 2) ax2.title.set_text("restored") plt.imshow(restored, cmap=plt.cm.gray) plt.show() if test == 6: im = read_image("text_dataset/train/0000021_orig.png", 1) noised = corrupted_image = random_motion_blur(im, [7]) untrained_model = build_nn_model(corrupted_image.shape[0], corrupted_image.shape[1], 32) untrained_model.load_weights("blur_weight.txt") corrupted_image = corrupted_image.reshape( (1, corrupted_image.shape[0], corrupted_image.shape[1])) - 0.5 restored_im = untrained_model.predict( corrupted_image[np.newaxis, ...])[0] + 0.5 # todo should i add 0,5? restored_im = np.clip(restored_im, 0, 1) restored_im = restored_im.reshape( (restored_im.shape[1], restored_im.shape[2])) fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) ax1.title.set_text("blur") plt.imshow(noised, cmap=plt.cm.gray) ax2 = fig.add_subplot(1, 2, 2) ax2.title.set_text("restored") plt.imshow(restored_im, cmap=plt.cm.gray) plt.show() if test == 2: im = read_image("image_dataset/train/2092.jpg", 1) noised_im = add_gaussian_noise(im, 0, 0.2) model, channels = learn_denoising_model(True) restored = restore_image(noised_im, model, channels) fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1, label="noise") ax1.title.set_text("noise") plt.imshow(noised_im, cmap=plt.cm.gray) ax2 = fig.add_subplot(1, 2, 2) ax2.title.set_text("restored") plt.imshow(restored, cmap=plt.cm.gray) plt.show() if test == 3: im = read_image("image_dataset/train/2092.jpg", 1) noised = corrupted_image = add_gaussian_noise(im, 0, 0.2) untrained_model = build_nn_model(corrupted_image.shape[0], corrupted_image.shape[1], 48) untrained_model.load_weights("denoise_weight.txt") corrupted_image = corrupted_image.reshape( (1, corrupted_image.shape[0], corrupted_image.shape[1])) - 0.5 restored_im = untrained_model.predict( corrupted_image[np.newaxis, ...])[0] + 0.5 # todo should i add 0,5? restored_im = np.clip(restored_im, 0, 1) restored_im = restored_im.reshape( (restored_im.shape[1], restored_im.shape[2])) fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1, label="noise") ax1.title.set_text("noise") plt.imshow(noised, cmap=plt.cm.gray) ax2 = fig.add_subplot(1, 2, 2) ax2.title.set_text("restored") plt.imshow(restored_im, cmap=plt.cm.gray) plt.show() if test == 4: images = sut.images_for_denoising() train_images = images[:int(len(images) * 0.8)] valid_images = images[int(len(images) * 0.8):] batch_size = 30 for im in load_dataset(train_images, batch_size, corrupt_im, (1, 16, 16)): fig = plt.figure() ori = im[0].reshape(batch_size, im[0].shape[1], im[0].shape[2]) + 0.5 ori = im[1].reshape(batch_size, im[1].shape[1], im[1].shape[2]) + 0.5 ax1 = fig.add_subplot(1, 2, 1, label="noise") plt.imshow(ori, cmap=plt.cm.gray) ax2 = fig.add_subplot(1, 2, 2) ax2.title.set_text("restored") plt.imshow(restored, cmap=plt.cm.gray) plt.show()
from sol5 import * import matplotlib.pyplot as plt import sol5_utils # model1, channels1 = learn_deblurring_model() # model1.save_weights('blur_weights.h5') # model1.save('blur_model.h5') # model, channels = learn_denoising_model() # model.save('denois_model.h5') # model.save_weights('denois_weights.h5') im1 = read_image(sol5_utils.images_for_denoising()[20], 1) cim = add_gaussian_noise(im1, 0.0, 0.2) mdl = build_nn_model(DENOIS_PATCH_SIZE, DENOIS_PATCH_SIZE, DENOIS_CHANNELS) mdl.load_weights('denois_weights.h5') fix = restore_image(cim, mdl, DENOIS_CHANNELS) f = plt.figure() f.add_subplot(121) plt.imshow(cim, cmap=plt.cm.gray) f.add_subplot(122) plt.imshow(fix, cmap=plt.cm.gray) plt.show()
import sol5_utils as util import sol5 import matplotlib.pyplot as plt #quick = False quick = False ################# DENOISING TEST ################# im_path = util.images_for_denoising()[22] model = sol5.learn_denoising_model(quick)[0] model.save_weights("denoising_weights_full.weights") im = sol5.read_image(im_path, 1) cor_im = sol5.add_gaussian_noise(sol5.read_image(im_path, 1), 0, 0.2) res_im = sol5.restore_image(cor_im, model, 48) ax1 = plt.subplot(221) ax1.set_title("cor_im") plt.imshow(cor_im, cmap='gray') ax2 = plt.subplot(222) ax2.set_title("restored Image") plt.imshow(res_im, cmap='gray') ax3 = plt.subplot(223) ax3.set_title("original") plt.imshow(im, cmap='gray')