def learn_deblurring_model(num_res_blocks=5, 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 deblurring model """ paths = sol5_utils.images_for_deblurring() deb_model = build_nn_model(parm.HEIGHT_BLUR_MODEL, parm.WIDTH_BLUR_MODEL, parm.NUM_CHANNELS, num_res_blocks) corrption_func = lambda image: random_motion_blur(image, [7]) if quick_mode: train_model(deb_model, paths, corrption_func, parm.BATCH_SIZE_SMALL, parm.SAMPLES_PER_EPOCH_SMALL, parm.SMALL_EPOCH, parm.VALID_SAMPLES_SMALL) else: train_model(deb_model, paths, corrption_func, parm.BATCH_SIZE, parm.SAMPLES_PER_EPOCH_BIG, parm.BIG_EPOCH, parm.VALID_SAMPLES_BIG) return deb_model
def learn_deblurring_model(num_res_blocks=5, quick_mode=False): """ Trains a neural network for deblurring motion blurred images. :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 random_motion_blur(im, [LDB_KERNEL_SIZE]) images = sol5_utils.images_for_deblurring() model = build_nn_model(LDB_PATCH_SIZE, LDB_PATCH_SIZE, LDB_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, LDB_EPOCHS, VALID_SAMPLES) return model
def learn_deblurring_model(quick_mode: bool = False) -> tuple: """ train a network to fix blurring images :param quick_mode: if true, train with test-parameters to save time :return: tuple contains a trained deblurring model, and the number of channels used in its construction """ im_list = sol5_utils.images_for_deblurring() num_channels = DEBLUR_CHANNELS model = build_nn_model(height=DEBLUR_PATCH_SIZE, width=DEBLUR_PATCH_SIZE, num_channels=num_channels) train_model( model=model, images=im_list, corruption_func=lambda x: random_motion_blur(x, [DEBLUR_KERNEL_SIZE]), batch_size=DEBLUR_BATCH_SIZE if not quick_mode else DEBLUR_TEST_BATCH_SIZE, samples_per_epoch=DEBLUR_EPOCH_SIZE if not quick_mode else DEBLUR_TEST_EPOCH_SIZE, num_epochs=DEBLUR_EPOCH_NB if not quick_mode else DEBLUR_TEST_EPOCH_NB, num_valid_samples=DEBLUR_VALID_SIZE if not quick_mode else DEBLUR_TEST_VALID_SIZE) return model, num_channels
def learn_deblurring_model(quick_mode=False): """ The above method trains a network which expect patches of size 16×16, and have 32 channels in all layers except the last. :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_deblurring() patch_size = (16, 16) num_channels = 32 kernel_sizes = [7] im_per_batch = 100 if not quick_mode else 10 samples_per_epoch = 10000 if not quick_mode else 30 total_epochs = 10 if not quick_mode else 2 samples_for_validation = 1000 if not quick_mode else 30 corrupt_im = lambda im: random_motion_blur(im, kernel_sizes) 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_deblurring_model(num_res_blocks=5, quick_mode=False): ''' Training for motion deblurring on blurred images :param num_res_blocks: The number of res blocks in the network :param quick_mode: Bool values representing if trainning is done in the fast mode :return: A trained model ''' def corruption_func(im): return random_motion_blur(im, [DEBLUR_KERNEL_SIZE]) images = sol5_utils.images_for_deblurring() model = build_nn_model(DEBLUR_PATCH_SIZE, DEBLUR_PATCH_SIZE, DEBLUR_CHANNELS, num_res_blocks) 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, DEBLUR_EPOCHS, VALIDATION_SET) return model
def learn_deblurring_model(num_res_blocks=5, quick_mode=False): """ function which will return a trained deblurring model. :param num_res_blocks: :param quick_mode: :return: a trained deblurring model. """ images_for_denoising = sol5_utils.images_for_deblurring() corruption_func = lambda x: random_motion_blur(x, [7]) model = build_nn_model(16, 16, 32, 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 = 10 num_valid_samples = 1000 train_model(model, images_for_denoising, corruption_func, batch_size, steps_per_epoch, num_epochs, num_valid_samples) return model
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') plt.show() ################ DEBLURRING TEST ################# im_path = util.images_for_deblurring()[23] model = sol5.learn_deblurring_model(quick)[0] model.save_weights("deblurring_weights_full.weights") im = sol5.read_image(im_path, 1) cor_im = sol5.random_motion_blur(sol5.read_image(im_path, 1), [7]) res_im = sol5.restore_image(cor_im, model, 32) ax1 = plt.subplot(221) ax1.set_title("cor_im") plt.imshow(cor_im, cmap='gray') ax2 = plt.subplot(222)
def learn_deblurring_model(num_res_blocks=5, quick_mode=False): images = sol5_utils.images_for_deblurring() corruption_func = lambda image: random_motion_blur(image, [7]) model = build_nn_model(16, 16, 32, num_res_blocks) batch_size = 100 steps_per_epoch = 100 num_epochs = 10 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 check_network_denoising(): # # model_noise = learn_denoising_model(quick_mode=False) # im1_corrupt = read_image("examples/163004_2_corrupted_0.10.png", 1) # im1_fixed = read_image("examples/163004_3_fixed_0.10_5.png", 1) # im1_out = restore_image(im1_corrupt, model_noise) # plt.imshow(np.hstack((im1_corrupt, im1_out, im1_fixed)), cmap="gray") # plt.axis('off') # plt.show() # # # def check_network_deblurring(): # model_blur = learn_deblurring_model(quick_mode=False) # im2_corrupt = read_image("examples/0000018_2_corrupted.png", 1) # im2_fixed = read_image("examples/0000018_3_fixed.png", 1) # im2_out = restore_image(im2_corrupt, model_blur) # plt.imshow(np.hstack((im2_corrupt, im2_out, im2_fixed)), cmap="gray") # plt.axis('off') # plt.show() # # if __name__ == "__main__" : # # # check_network_deblurring() # check_network_denoising() # validation_error_denoise = [] # validation_error_deblur = [] # for i in range(1, 6): # denoise_model = learn_denoising_model(i) # deblur_model = learn_deblurring_model(i) # validation_error_denoise.append(denoise_model.history.history['val_loss'][-1]) # validation_error_deblur.append(deblur_model.history.history['val_loss'][-1]) # # arr = np.arange(1, 6) # # plt.plot(arr, validation_error_denoise) # plt.title('validation error - denoise') # plt.xlabel('number res blocks') # plt.ylabel('validation loss denoise') # plt.savefig('denoise.png') # plt.show() # # plt.plot(arr, validation_error_deblur) # plt.title('validation error - deblur') # plt.xlabel('number res blocks') # plt.ylabel('validation loss deblur') # plt.savefig('deblur.png') # plt.show()