Tìm hiểu về auto encoder

Posted by Hao Do on September 1, 2022

Tìm hiểu về auto encoder

Tìm hiểu autoencoder

Kiến trúc cơ bản của AutoEncoder

img

Ví dụ cơ bản

Bài toán Mnist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# hiding warning tensorflow
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 

# import cac thu vien
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import cv2
import numpy as np

# Load du lieu mnist tu keras
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

# Hien thi anh trong dataset voi cmap = 'gray'
plt.imshow(x_train[1234], cmap='gray')

Model autoencoder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# data processing (chuan hoa du lieu [0, 1])
x_train = x_train / 255.0
x_test = x_test / 255.0

# autoencoder

# su dung ham toi uu la Adam voi lr=1e-3, decay = 1e-6
opt = keras.optimizers.Adam(learning_rate=0.001, decay = 1e-6)

# dau vao encoder (28, 28, 1)
# processing cua encoder: tu dau vao --> flatten --> dense
encoder_input = keras.Input((28, 28, 1), name = 'img')
x = keras.layers.Flatten()(encoder_input)
encoder_output = keras.layers.Dense(64, activation='relu') (x)

# encoder (day la mot mo hinh voi 2 tham so (encoder_input, encoder_output))
encoder = keras.Model(encoder_input, encoder_output, name = 'encoder')

# processing decoder: dau vao la encoder_output --> qua lop Dense --> reshape thanh (28, 28, 1)
decoder_input = keras.layers.Dense(784, activation='relu') (encoder_output)
decoder_output = keras.layers.Reshape((28, 28, 1)) (decoder_input)

# autoencoder no la mot model voi 2 so (encoder_input vs decoder_output)
autoencoder = keras.Model(encoder_input, decoder_output, name = 'autoencoder')
autoencoder.summary()

# compilie vs fit data
autoencoder.compile(opt, loss='mse')
autoencoder.fit(x_train, x_train, epochs=10, batch_size = 32, validation_split=0.1)
# luu y, cho nay minh dung ca (x, y) = (x_train, x_train)

1
2
3
4
5
6
7
8
9
10
example = encoder.predict([x_test[3].reshape(-1, 28, 28, 1)]) [0]
print(example)
print(example.shape)
print(64/784)

plt.imshow(example.reshape((8, 8)), cmap = 'gray')
plt.imshow(x_test[3], cmap='gray')

ae_out = autoencoder.predict([x_test[3].reshape(-1, 28, 28, 1)])[0]
plt.imshow(ae_out, cmap='gray')

Bổ sung nhiễu (noise)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import random
def add_noise(img, random_chance=5):
    noisy = []
    for row in img:
        new_row = []
        for pix in row:
            if random.choice(range(100)) <= random_chance:
                new_val = random.uniform(0, 1)
                new_row.append(new_val)
            else:
                new_row.append(pix)
        noisy.append(new_row)
    return np.array(noisy)

noisy = add_noise(x_test[3])
plt.imshow(noisy, cmap = 'gray')

noisy = add_noise(x_test[3])
plt.imshow(noisy, cmap = 'gray')
	

Một mô hình autoencoder khác

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# mot mo hinh auto encoder khac.
import tensorflow as tf
from tensorflow import keras

opt = keras.optimizers.Adam(learning_rate = 0.001, decay = 1e-6)

#1. 
encoder_input = keras.Input((64, 64, 3), name = 'img_in')
#2. 
x = keras.layers.Conv2D(64, (3, 3), activation = 'relu', padding='same') (encoder_input)
#3. 
x = keras.layers.MaxPooling2D((2, 2), padding = 'same')(x)
#4. 
x = keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same') (x)
#5. 
x= keras.layers.MaxPooling2D((2, 2), padding = 'same') (x)
#6. 
x = keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same') (x)
#7. 
x = keras.layers.MaxPooling2D((2, 2), padding='same') (x)
#8. 
x = keras.layers.Flatten() (x)
#9. 
encoder_output = keras.layers.Dense(512, activation='relu') (x)


encoder = keras.Model(encoder_input, encoder_output, name = 'encoder')


#9. 
decoder_input = keras.layers.Dense(2048, activation='relu') (encoder_output)
#8. 
x = keras.layers.Reshape((8, 8, 32)) (decoder_input)
#7. 
x = keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same') (x)
#6. 
x = keras.layers.UpSampling2D((2, 2)) (x)
#5. 
x = keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same') (x)
#4. 
x = keras.layers.UpSampling2D((2, 2)) (x)
#3. 
x = keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
#2. 
x = keras.layers.UpSampling2D((2, 2)) (x)
#1. 
decoder_output = keras.layers.Conv2D(3, (3, 3), activation='relu', padding='same') (x)

autoencoder = keras.Model(encoder_input, decoder_output, name = 'ae')
autoencoder.summary()

img

Full ipynb

Tài liệu tham khảo

Machine learning cơ bản

Hết.