<jupyter_start><jupyter_text>Denoising with the deep decoderThe code below demonstrates the denoising performance on an example image.<jupyter_code>from google.colab import drive
drive.mount('/content/drive')

%cd /content/drive/MyDrive/supplement_deep_decoder-master/supplement_deep_decoder-master
! ls
from __future__ import print_function
import matplotlib.pyplot as plt
#%matplotlib notebook

import os

import warnings
warnings.filterwarnings('ignore')

from include import *
from PIL import Image
import PIL

import numpy as np
import torch
import torch.optim
from torch.autograd import Variable

GPU = True
if GPU == True:
    torch.backends.cudnn.enabled = True
    torch.backends.cudnn.benchmark = True
    dtype = torch.cuda.FloatTensor
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'
    print("num GPUs",torch.cuda.device_count())
else:
    dtype = torch.FloatTensor<jupyter_output>num GPUs 1<jupyter_text>Load image<jupyter_code>path = './test_data/'
img_name = "astronaut"
#img_name = "phantom256"

img_path = path + img_name + ".png"
img_pil = Image.open(img_path)
img_np = pil_to_np(img_pil)
img_clean_var = np_to_var(img_np).type(dtype)
print("img_clean_var", img_clean_var)<jupyter_output><empty_output><jupyter_text>Generate noisy image<jupyter_code>def get_noisy_img(sig=30,noise_same = False):
    sigma = sig/255.
    if noise_same: # add the same noise in each channel
        noise = np.random.normal(scale=sigma, size=img_np.shape[1:])
        noise = np.array( [noise]*img_np.shape[0] )
    else: # add independent noise in each channel
        noise = np.random.normal(scale=sigma, size=img_np.shape)

    img_noisy_np = np.clip( img_np + noise , 0, 1).astype(np.float32)
    img_noisy_var = np_to_var(img_noisy_np).type(dtype)
    print("img_noisy_var", img_noisy_var)
    return img_noisy_np,img_noisy_var
    
img_noisy_np,img_noisy_var = get_noisy_img()  
output_depth = img_np.shape[0] 
print("Image size: ", img_np.shape)<jupyter_output><empty_output><jupyter_text>Denoise noisy image<jupyter_code>def denoise(img_noisy_var,k=8,numit = 1900,rn = 0.0,find_best=True,upsample_first = True):
    num_channels = [k]*5
    print ("num_channels ",num_channels)
    #define the nueral net arch
    net = decodernw(output_depth,num_channels_up=num_channels,upsample_first=upsample_first).type(dtype)
    #fit on the nueral net
    mse_n, ni, net = fit( num_channels=num_channels,
                        reg_noise_std=rn,
                        num_iter=numit,
                        img_noisy_var=img_noisy_var,
                        net=net,
                        img_clean_var=img_clean_var,
                        find_best=find_best
                        )
    out_img_np = net( ni.type(dtype) ).data.cpu().numpy()[0]
    return out_img_np 
def myimgshow(plt,img):
    plt.imshow(np.clip(img.transpose(1, 2, 0),0,1))

def plot_results(out_img_np,img_np,img_noisy_np):
    fig = plt.figure(figsize = (15,15)) # create a 5 x 5 figure 
    
    ax1 = fig.add_subplot(131)
    myimgshow(ax1,img_np) 
    ax1.set_title('Original image')
    ax1.axis('off')
    
    ax2 = fig.add_subplot(132)
    myimgshow(ax2,img_noisy_np)
    ax2.set_title( "Noisy observation, PSNR: %.2f" % psnr(img_np,img_noisy_np) )
    ax2.axis('off')

    ax3 = fig.add_subplot(133)
    myimgshow(ax3,out_img_np)
    ax3.set_title( "Deep-Decoder denoised image, SNR: %.2f" % psnr(img_np,out_img_np) ) 
    ax3.axis('off')    

    plt.show()
     
img_noisy_np,img_noisy_var = get_noisy_img(sig=30,noise_same = False)
out_img_np = denoise(img_noisy_var,k=128,numit = 1900,rn = 0.0)

plot_results(out_img_np,img_np,img_noisy_np)
img_noisy_np,img_noisy_var = get_noisy_img(sig=30,noise_same = False)
out_img_np, mse_t = denoise(img_noisy_var,k=128,numit = 10000,rn = 0.015,upsample_first = False)

plot_results(out_img_np,img_np,img_noisy_np)<jupyter_output><empty_output><jupyter_text>Choice of number of layersThe number of layers $k$ is a hyperparameter that enables trading off amount of noise that is removed versus the representation error by the model. Smaller $k$ remove more of the noise, but also increase the error of approximating an image with the deep decoder. The optimal choise of $k$ depends on the noise level. To illustrate this consider the experiment below.The following series of plots show the PSNR of the output of the deep decoder during training as a function of iteration number.  The blue curves correspond to k=32, the orange corresponds to k = 64, and the green corresponds to k = 128.<jupyter_code>def best_k(ks,sig=70,numit=400,noise_same = False,find_best=True):
    img_noisy_np,img_noisy_var = get_noisy_img(sig=sig,noise_same = noise_same)
    print("Noisy observation, PSNR: %.2f" % psnr(img_np,img_noisy_np))
    mses = []
    psnrs = []
    for k in ks:
        out_img_np, mse_t = denoise(img_noisy_var,k=k,numit = numit,rn = 0.0)
        psnrs += [psnr(img_np,out_img_np)]
        mses  += [mse_t]
    plt.yscale('log')
    plt.xscale('log')
    for mse_t in mses:
        plt.plot(mse_t)
    plt.show()
    print(psnrs)<jupyter_output><empty_output><jupyter_text>If we regularize with the model alone, and run close to convergence, $64$ performs best:<jupyter_code>ks = [32,64,128]
best_k(ks,sig=25,numit=10000,noise_same=True)<jupyter_output><empty_output><jupyter_text>If we additionally stop early to regularize, then $k=128$ performs best:<jupyter_code>ks = [32,64,128]
best_k(ks,sig=25,numit=1900,noise_same=True)<jupyter_output><empty_output><jupyter_text>More noise requires more regularization, either by using a smaller $k$:<jupyter_code>ks = [32,64,128]
best_k(ks,sig=60,numit=5000,noise_same=True)<jupyter_output><empty_output><jupyter_text>... or by stopping even earlier:<jupyter_code>ks = [32,64,128]
best_k(ks,sig=60,numit=800,noise_same=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10901611244429209, 'fraction_numerical': 0.026911210147411723, 'mean_word_length': 4.499528746465598, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '235540', 'license': 'Apache-1.1', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2163, 'n_words': 435, 'path': 'denoising_medha.ipynb', 'repo_name': 'MedhaKalra95/supplement_deep_decoder', 'size': '5834'}