[K3AE4] - Denoiser and classifier modelĀ¶
Episode 4 : Construction of a denoiser and classifier modelObjectives :Ā¶
- Building a multiple output model, able to denoise and classify
- Understanding a more advanced programming model
The calculation needs being important, it is preferable to use a very simple dataset such as MNIST.
The use of a GPU is often indispensable.
What we're going to do :Ā¶
- Defining a multiple output model using Keras procedural programing model
- Build the model
- Train it
- Follow the learning process
Data Terminology :Ā¶
clean_train
,clean_test
for noiseless imagesnoisy_train
,noisy_test
for noisy imagesclass_train
,class_test
for the classes to which the images belongdenoised_test
for denoised images at the output of the modelclasscat_test
for class prediction in model output (is a softmax)classid_test
class prediction (ie: argmax of classcat_test)
import os
os.environ['KERAS_BACKEND'] = 'torch'
import keras
import numpy as np
from skimage import io
import random
from modules.AE4_builder import AE4_builder
from modules.MNIST import MNIST
from modules.ImagesCallback import ImagesCallback
import fidle
# Init Fidle environment
run_id, run_dir, datasets_dir = fidle.init('K3AE4')
FIDLE - Environment initialization
Version : 2.3.2 Run id : K3AE4 Run dir : ./run/K3AE4 Datasets dir : /lustre/fswork/projects/rech/mlh/uja62cb/fidle-project/datasets-fidle Start time : 22/12/24 21:26:45 Hostname : r3i6n0 (Linux) Tensorflow log level : Info + Warning + Error (=0) Update keras cache : False Update torch cache : False Save figs : ./run/K3AE4/figs (True) keras : 3.7.0 numpy : 2.1.2 sklearn : 1.5.2 yaml : 6.0.2 skimage : 0.24.0 matplotlib : 3.9.2 pandas : 2.2.3 torch : 2.5.0
1.2 - ParametersĀ¶
prepared_dataset
: Filename of the prepared dataset (Need 400 Mo, but can be in ./data)
dataset_seed
: Random seed for shuffling dataset. 'None' mean using /dev/urandom
scale
: % of the dataset to use (1. for 100%)
latent_dim
: Dimension of the latent space
train_prop
: Percentage for train (the rest being for the test)
batch_size
: Batch size
epochs
: Nb of epochs for training
fit_verbosity
is the verbosity during training : 0 = silent, 1 = progress bar, 2 = one line per epoch
scale=0.1, epochs=20 => 2' on a laptop
prepared_dataset = './data/mnist-noisy.h5'
dataset_seed = None
scale = .1
train_prop = .8
batch_size = 128
epochs = 10
fit_verbosity = 1
Override parameters (batch mode) - Just forget this cell
fidle.override('prepared_dataset', 'dataset_seed', 'scale')
fidle.override('train_prop', 'batch_size', 'epochs', 'fit_verbosity')
** Overrided parameters : ** scale : 1 ** Overrided parameters : ** epochs : 20 fit_verbosity : 2
Step 2 - Retrieve datasetĀ¶
With our MNIST class, in one call, we can reload, rescale, shuffle and split our previously saved dataset :-)
clean_train,clean_test, noisy_train,noisy_test, class_train,class_test = MNIST.reload_prepared_dataset(
scale = scale,
train_prop = train_prop,
seed = dataset_seed,
shuffle = True,
filename = prepared_dataset )
Loaded. rescaled (1). Seeded (None)
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 : b8d58e240863aa03b731
Step 3 - Build modelsĀ¶
builder = AE4_builder( ae={ 'latent_dim':10 }, cnn = { 'lc1':8, 'lc2':16, 'ld':100 } )
model = builder.create_model()
model.compile(optimizer='rmsprop',
loss={'ae':'binary_crossentropy', 'classifier':'sparse_categorical_crossentropy'},
loss_weights={'ae':1., 'classifier':1.},
metrics={'classifier':'accuracy'} )
# keras.utils.plot_model(model, "multi_input_and_output_model.png", show_shapes=True)
Step 4 - TrainĀ¶
20' on a CPU
1'12 on a GPU (V100, IDRIS)
# ---- Callback : Images
#
fidle.utils.mkdir( run_dir + '/images')
filename = run_dir + '/images/image-{epoch:03d}-{i:02d}.jpg'
encoder = model.get_layer('ae').get_layer('encoder')
decoder = model.get_layer('ae').get_layer('decoder')
callback_images = ImagesCallback(filename, x=clean_test[:5], encoder=encoder,decoder=decoder)
chrono = fidle.Chrono()
chrono.start()
history = model.fit(noisy_train, [clean_train, class_train],
batch_size = batch_size,
epochs = epochs,
verbose = fit_verbosity,
validation_data = (noisy_test, [clean_test, class_test]),
callbacks = [ callback_images ] )
chrono.show()
Epoch 1/20
/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(
1/1 āāāāāāāāāāāāāāāāāāāā 0s 10ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 11ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 5ms/step
438/438 - 9s - 21ms/step - ae_loss: 0.2794 - classifier_accuracy: 0.4499 - classifier_loss: 1.5767 - loss: 1.8562 - val_ae_loss: 0.2263 - val_classifier_accuracy: 0.7361 - val_classifier_loss: 0.8732 - val_loss: 1.0995
Epoch 2/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.2001 - classifier_accuracy: 0.6701 - classifier_loss: 0.9882 - loss: 1.1884 - val_ae_loss: 0.1843 - val_classifier_accuracy: 0.7973 - val_classifier_loss: 0.6603 - val_loss: 0.8446
Epoch 3/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1784 - classifier_accuracy: 0.7299 - classifier_loss: 0.8202 - loss: 0.9986 - val_ae_loss: 0.1707 - val_classifier_accuracy: 0.8274 - val_classifier_loss: 0.5536 - val_loss: 0.7243
Epoch 4/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1682 - classifier_accuracy: 0.7608 - classifier_loss: 0.7299 - loss: 0.8981 - val_ae_loss: 0.1644 - val_classifier_accuracy: 0.8385 - val_classifier_loss: 0.5071 - val_loss: 0.6715
Epoch 5/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1631 - classifier_accuracy: 0.7785 - classifier_loss: 0.6772 - loss: 0.8403 - val_ae_loss: 0.1662 - val_classifier_accuracy: 0.8459 - val_classifier_loss: 0.4809 - val_loss: 0.6471
Epoch 6/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1599 - classifier_accuracy: 0.7856 - classifier_loss: 0.6585 - loss: 0.8184 - val_ae_loss: 0.1612 - val_classifier_accuracy: 0.8522 - val_classifier_loss: 0.4625 - val_loss: 0.6237
Epoch 7/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1575 - classifier_accuracy: 0.7960 - classifier_loss: 0.6291 - loss: 0.7867 - val_ae_loss: 0.1607 - val_classifier_accuracy: 0.8576 - val_classifier_loss: 0.4520 - val_loss: 0.6128
Epoch 8/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1557 - classifier_accuracy: 0.8006 - classifier_loss: 0.6088 - loss: 0.7645 - val_ae_loss: 0.1582 - val_classifier_accuracy: 0.8601 - val_classifier_loss: 0.4376 - val_loss: 0.5958
Epoch 9/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1542 - classifier_accuracy: 0.8061 - classifier_loss: 0.6018 - loss: 0.7560 - val_ae_loss: 0.1555 - val_classifier_accuracy: 0.8619 - val_classifier_loss: 0.4348 - val_loss: 0.5903
Epoch 10/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1530 - classifier_accuracy: 0.8071 - classifier_loss: 0.5935 - loss: 0.7465 - val_ae_loss: 0.1553 - val_classifier_accuracy: 0.8644 - val_classifier_loss: 0.4225 - val_loss: 0.5778
Epoch 11/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1520 - classifier_accuracy: 0.8091 - classifier_loss: 0.5860 - loss: 0.7380 - val_ae_loss: 0.1529 - val_classifier_accuracy: 0.8622 - val_classifier_loss: 0.4276 - val_loss: 0.5805
Epoch 12/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1510 - classifier_accuracy: 0.8115 - classifier_loss: 0.5833 - loss: 0.7343 - val_ae_loss: 0.1527 - val_classifier_accuracy: 0.8604 - val_classifier_loss: 0.4303 - val_loss: 0.5830
Epoch 13/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1502 - classifier_accuracy: 0.8120 - classifier_loss: 0.5784 - loss: 0.7286 - val_ae_loss: 0.1539 - val_classifier_accuracy: 0.8658 - val_classifier_loss: 0.4162 - val_loss: 0.5700
Epoch 14/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1494 - classifier_accuracy: 0.8155 - classifier_loss: 0.5710 - loss: 0.7205 - val_ae_loss: 0.1509 - val_classifier_accuracy: 0.8594 - val_classifier_loss: 0.4334 - val_loss: 0.5842
Epoch 15/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1487 - classifier_accuracy: 0.8169 - classifier_loss: 0.5647 - loss: 0.7134 - val_ae_loss: 0.1521 - val_classifier_accuracy: 0.8685 - val_classifier_loss: 0.4118 - val_loss: 0.5640
Epoch 16/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1482 - classifier_accuracy: 0.8186 - classifier_loss: 0.5645 - loss: 0.7128 - val_ae_loss: 0.1503 - val_classifier_accuracy: 0.8695 - val_classifier_loss: 0.4094 - val_loss: 0.5598
Epoch 17/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1475 - classifier_accuracy: 0.8185 - classifier_loss: 0.5602 - loss: 0.7077 - val_ae_loss: 0.1502 - val_classifier_accuracy: 0.8694 - val_classifier_loss: 0.4100 - val_loss: 0.5601
Epoch 18/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1470 - classifier_accuracy: 0.8182 - classifier_loss: 0.5618 - loss: 0.7088 - val_ae_loss: 0.1499 - val_classifier_accuracy: 0.8641 - val_classifier_loss: 0.4320 - val_loss: 0.5819
Epoch 19/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1466 - classifier_accuracy: 0.8158 - classifier_loss: 0.5590 - loss: 0.7056 - val_ae_loss: 0.1503 - val_classifier_accuracy: 0.8679 - val_classifier_loss: 0.4138 - val_loss: 0.5641
Epoch 20/20
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 3ms/step
1/1 āāāāāāāāāāāāāāāāāāāā 0s 4ms/step
438/438 - 9s - 20ms/step - ae_loss: 0.1460 - classifier_accuracy: 0.8207 - classifier_loss: 0.5565 - loss: 0.7025 - val_ae_loss: 0.1506 - val_classifier_accuracy: 0.8665 - val_classifier_loss: 0.4117 - val_loss: 0.5624
Duration : 173.08 seconds
Save model weights
os.makedirs(f'{run_dir}/models', exist_ok=True)
model.save_weights(f'{run_dir}/models/model.weights.h5')
Step 5 - HistoryĀ¶
fidle.scrawler.history(history, plot={'Loss':['loss', 'val_loss'],
'Accuracy':['classifier_accuracy','val_classifier_accuracy']}, save_as='01-history')
Step 6 - Denoising progressĀ¶
imgs=[]
for epoch in range(0,epochs,4):
for i in range(5):
filename = run_dir + '/images/image-{epoch:03d}-{i:02d}.jpg'.format(epoch=epoch, i=i)
img = io.imread(filename)
imgs.append(img)
fidle.utils.subtitle('Real images (clean_test) :')
fidle.scrawler.images(clean_test[:5], None, indices='all', columns=5, x_size=2,y_size=2, interpolation=None, save_as='02-original-real')
fidle.utils.subtitle('Noisy images (noisy_test) :')
fidle.scrawler.images(noisy_test[:5], None, indices='all', columns=5, x_size=2,y_size=2, interpolation=None, save_as='03-original-noisy')
fidle.utils.subtitle('Evolution during the training period (denoised_test) :')
fidle.scrawler.images(imgs, None, indices='all', columns=5, x_size=2,y_size=2, interpolation=None, y_padding=0.1, save_as='04-learning')
fidle.utils.subtitle('Noisy images (noisy_test) :')
fidle.scrawler.images(noisy_test[:5], None, indices='all', columns=5, x_size=2,y_size=2, interpolation=None, save_as=None)
fidle.utils.subtitle('Real images (clean_test) :')
fidle.scrawler.images(clean_test[:5], None, indices='all', columns=5, x_size=2,y_size=2, interpolation=None, save_as=None)
Real images (clean_test) :
Noisy images (noisy_test) :
Evolution during the training period (denoised_test) :
Noisy images (noisy_test) :
Real images (clean_test) :
Step 7 - EvaluationĀ¶
Note : We will use the following data:
clean_train
, clean_test
for noiseless images
noisy_train
, noisy_test
for noisy images
class_train
, class_test
for the classes to which the images belong
denoised_test
for denoised images at the output of the model
classcat_test
for class prediction in model output (is a softmax)
classid_test
class prediction (ie: argmax of classcat_test)
7.1 - Reload our model (weights)Ā¶
builder = AE4_builder( ae={ 'latent_dim':10 }, cnn = { 'lc1':8, 'lc2':16, 'ld':100 } )
model = builder.create_model()
model.load_weights(f'{run_dir}/models/model.weights.h5')
7.2 - Let's make a predictionĀ¶
Note that our model will returns 2 outputs : denoised images from output 1 and class prediction from output 2
outputs = model.predict(noisy_test, verbose=0)
denoised = outputs['ae']
classcat = outputs['classifier']
print('Denoised images (denoised_test) shape : ', denoised.shape)
print('Predicted classes (classcat_test) shape : ', classcat.shape)
Denoised images (denoised_test) shape : (14000, 28, 28, 1) Predicted classes (classcat_test) shape : (14000, 10)
7.3 - Denoised imagesĀ¶
i=random.randint(0,len(denoised)-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[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 :
7.4 - Class predictionĀ¶
Note: The evaluation requires the noisy images as input (noisy_test) and the 2 expected outputs:
- the images without noise (clean_test)
- the classes (class_test)
# We need to (re)compile our resurrected model (to specify loss and metrics)
#
model.compile(optimizer='rmsprop',
loss={'ae':'binary_crossentropy', 'classifier':'sparse_categorical_crossentropy'},
loss_weights={'ae':1., 'classifier':1.},
metrics={'classifier':'accuracy'} )
# Get an evaluation
#
score = model.evaluate(noisy_test, [clean_test, class_test], verbose=0)
# And show results
#
fidle.utils.subtitle("Accuracy :")
print(f'Classification accuracy : {score[1]:4.4f}')
fidle.utils.subtitle("Few examples :")
classid_test = np.argmax(classcat, axis=-1)
fidle.scrawler.images(noisy_test, class_test, range(0,200), columns=12, x_size=1, y_size=1, y_pred=classid_test, save_as='04-predictions')
Accuracy :
Classification accuracy : 0.1506
Few examples :
fidle.end()
End time : 22/12/24 21:30:11
Duration : 00:03:27 533ms
This notebook ends here :-)
https://fidle.cnrs.fr