No description has been provided for this image

[K3AE3] - Playing with our denoiser model¶

Episode 2 : Using the previously trained autoencoder to denoise data

Objectives :¶

  • Retrieve and use our denoiser model

What we're going to do :¶

  • Reload our dataset and saved best model
  • Encode/decode some test images (neved used, never seen by the model)

Data Terminology :¶

  • clean_train, clean_test for noiseless images
  • noisy_train, noisy_test for noisy images
  • denoised_test for denoised images at the output of the model

Step 1 - Init python stuff¶

1.1 - Init¶

InĀ [1]:
import os
os.environ['KERAS_BACKEND'] = 'torch'

import keras

import numpy as np
import matplotlib.pyplot as plt
import random

from modules.MNIST import MNIST

import fidle

# Init Fidle environment
run_id, run_dir, datasets_dir = fidle.init('K3AE3')


FIDLE - Environment initialization

Version              : 2.3.2
Run id               : K3AE3
Run dir              : ./run/K3AE3
Datasets dir         : /lustre/fswork/projects/rech/mlh/uja62cb/fidle-project/datasets-fidle
Start time           : 22/12/24 21:26:22
Hostname             : r3i6n0 (Linux)
Tensorflow log level : Info + Warning + Error  (=0)
Update keras cache   : False
Update torch cache   : False
Save figs            : ./run/K3AE3/figs (True)
keras                : 3.7.0
numpy                : 2.1.2
sklearn              : 1.5.2
yaml                 : 6.0.2
matplotlib           : 3.9.2
pandas               : 2.2.3
torch                : 2.5.0

1.2 - Parameters¶

These parameters must be identical to those used during the training in order to have the same dataset.
prepared_dataset : Filename of the prepared dataset (Need 400 Mo, but can be in ./data)
dataset_seed : Random seed for shuffling dataset
scale : % of the dataset to use (1. for 100%)
train_prop : Percentage for train (the rest being for the test)

InĀ [2]:
prepared_dataset = './data/mnist-noisy.h5'
saved_models     = './run/K3AE2/models'
dataset_seed     = 123
scale            = 1
train_prop       = .8

Override parameters (batch mode) - Just forget this cell

InĀ [3]:
fidle.override('prepared_dataset', 'dataset_seed', 'scale', 'train_prop')

Step 2 - Retrieve dataset¶

With our MNIST class, in one call, we can reload, rescale, shuffle and split our previously saved dataset :-)
Important : Make sure that the digest is identical to the one used during the training !
See : AE2 / Step 2 - Retrieve dataset

InĀ [4]:
clean_train,clean_test, noisy_train,noisy_test, _,_ = MNIST.reload_prepared_dataset(scale      = scale, 
                                                                                    train_prop = train_prop,
                                                                                    seed       = dataset_seed,
                                                                                    shuffle    = True,
                                                                                    filename=prepared_dataset )
Loaded.
rescaled (1).
Seeded (123)
Shuffled.
splited (0.8).
clean_train shape is :  (56000, 28, 28, 1)
clean_test  shape is :  (14000, 28, 28, 1)
noisy_train shape is :  (56000, 28, 28, 1)
noisy_test  shape is :  (14000, 28, 28, 1)
class_train shape is :  (56000,)
class_test  shape is :  (14000,)
Blake2b digest is    :  3e97fec95d853b5a2615

Step 3 - Evaluation¶

Note : We will use the following data:
clean_train, clean_test for noiseless images
noisy_train, noisy_test for noisy images
denoised_test for denoised images at the output of the model

3.1 - Reload our best model¶

InĀ [5]:
# model = keras.models.load_model(f'{saved_models}/model.keras')

encoder = keras.models.load_model(f'{saved_models}/encoder.keras')
decoder = keras.models.load_model(f'{saved_models}/decoder.keras')

inputs    = keras.Input(shape=(28, 28, 1))

latents   = encoder(inputs)
outputs   = decoder(latents)

model = keras.Model(inputs,outputs, name="ae")

3.2 - Let's make a prediction¶

InĀ [6]:
from tabnanny import verbose


denoised_test = model.predict(noisy_test,verbose=0)

print('Denoised images   (denoised_test) shape : ',denoised_test.shape)
/lustre/fswork/projects/rech/mlh/uja62cb/local/fidle-k3/lib/python3.12/site-packages/keras/src/backend/common/backend_utils.py:91: UserWarning: You might experience inconsistencies across backends when calling conv transpose with kernel_size=3, stride=2, dilation_rate=1, padding=same, output_padding=1.
  warnings.warn(
Denoised images   (denoised_test) shape :  (14000, 28, 28, 1)

3.3 - Denoised images¶

InĀ [7]:
i=random.randint(0,len(denoised_test)-8)
j=i+8

fidle.utils.subtitle('Noisy test images (input):')
fidle.scrawler.images(noisy_test[i:j], None, indices='all', columns=8, x_size=2,y_size=2, interpolation=None, save_as='05-test-noisy')

fidle.utils.subtitle('Denoised images (output):')
fidle.scrawler.images(denoised_test[i:j], None, indices='all', columns=8, x_size=2,y_size=2, interpolation=None, save_as='06-test-predict')

fidle.utils.subtitle('Real test images :')
fidle.scrawler.images(clean_test[i:j], None, indices='all', columns=8, x_size=2,y_size=2, interpolation=None, save_as='07-test-real')


Noisy test images (input):

Saved: ./run/K3AE3/figs/05-test-noisy
No description has been provided for this image


Denoised images (output):

Saved: ./run/K3AE3/figs/06-test-predict
No description has been provided for this image


Real test images :

Saved: ./run/K3AE3/figs/07-test-real
No description has been provided for this image

Step 4 - Looking at the latent space¶

4.1 - Getting clean data and class¶

InĀ [8]:
clean_data,_, _,_, class_data,_ = MNIST.reload_prepared_dataset(scale      = 1, 
                                                                train_prop = 1,
                                                                seed       = dataset_seed,
                                                                shuffle    = False,
                                                                filename   = prepared_dataset )
Loaded.
rescaled (1).
Seeded (123)
splited (1).
clean_train shape is :  (70000, 28, 28, 1)
clean_test  shape is :  (0, 28, 28, 1)
noisy_train shape is :  (70000, 28, 28, 1)
noisy_test  shape is :  (0, 28, 28, 1)
class_train shape is :  (70000,)
class_test  shape is :  (0,)
Blake2b digest is    :  459c527f88adba139c17

4.2 - Retrieve encoder¶

InĀ [9]:
encoder=model.get_layer('encoder')

4.3 Showing latent space¶

Here is the digit distribution in the latent space

InĀ [10]:
n_show = min( 20000, len(clean_data) )

# ---- Select images

x_show, y_show = fidle.utils.pick_dataset(clean_data, class_data, n=n_show)

# ---- Get latent points

z = encoder.predict(x_show)

# ---- Show them

fig = plt.figure(figsize=(14, 10))
plt.scatter(z[:, 0] , z[:, 1], c=y_show, cmap= 'tab10', alpha=0.5, s=30)
plt.colorbar()
fidle.scrawler.save_fig('08-Latent-space')
plt.show()
  1/625 ━━━━━━━━━━━━━━━━━━━━ 2s 4ms/step

 21/625 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step

 42/625 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step

 63/625 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step

 84/625 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step

105/625 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step

126/625 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step

147/625 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step

168/625 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step

189/625 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step

210/625 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step

231/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

252/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

273/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

294/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

315/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

336/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

357/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

378/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

399/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

420/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

441/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

462/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

483/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

504/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

525/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

546/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

567/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

588/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

609/625 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step

625/625 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step
Saved: ./run/K3AE3/figs/08-Latent-space
No description has been provided for this image
InĀ [11]:
fidle.end()

End time : 22/12/24 21:26:33
Duration : 00:00:11 481ms
This notebook ends here :-)
https://fidle.cnrs.fr



No description has been provided for this image