[K3AE3] - Playing with our denoiser modelĀ¶
Episode 2 : Using the previously trained autoencoder to denoise dataObjectives :Ā¶
- 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 imagesnoisy_train
,noisy_test
for noisy imagesdenoised_test
for denoised images at the output of the model
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)
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
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
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
# 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Ā¶
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Ā¶
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):
Denoised images (output):
Real test images :
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Ā¶
encoder=model.get_layer('encoder')
4.3 Showing latent spaceĀ¶
Here is the digit distribution in the latent space
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
fidle.end()
End time : 22/12/24 21:26:33
Duration : 00:00:11 481ms
This notebook ends here :-)
https://fidle.cnrs.fr