autoencoder experiment – studio-31

여기서는 Autoencoder를 구현한다.

1
2
3
4
5
from sklearn.datasets import fetch_olivetti_faces

olivetti = fetch_olivetti_faces()

imgs = olivetti.images
1
2
3
4
5
6
7
8
9
10
11
12
13
x = 26
j = 1
for i in range(x, x+8):

plt.subplot(2, 4, j)
plt.imshow(imgs[i], cmap='gray')
plt.title(i)
plt.axis('off')

j += 1

plt.tight_layout()
plt.show()

png

1
2
3
4
5
6
7
8
9
10
11
12
13
j = 1

for i in np.random.randint(len(imgs), size=8):

plt.subplot(2, 4, j)
plt.imshow(imgs[i], cmap='gray')
plt.title(i)
plt.axis('off')

j += 1

plt.tight_layout()
plt.show()

png

1
2
X_train = imgs[:, :, :, np.newaxis]
X_train.shape
(400, 64, 64, 1)
1
2
input_shape = X_train.shape[1:]
latent_dim = 64
1
2
3
4
from keras.layers import Input, Dropout, concatenate, Activation, Reshape, Flatten, Dense
from keras.models import Sequential, Model
from keras.optimizers import Adam
import keras.backend as K
Using TensorFlow backend.
1
2
3
4
5
6
7
8
9
10
11
12
encoder = Sequential()
encoder.add(Flatten(input_shape=input_shape))
encoder.add(Dense(1024, activation="relu"))
encoder.add(Dense(latent_dim, activation="relu"))
encoder.summary()


decoder = Sequential()
decoder.add(Dense(1024, activation="relu", input_shape=(latent_dim,)))
decoder.add(Dense(4096, activation="sigmoid"))
decoder.add(Reshape(input_shape))
decoder.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_1 (Flatten)          (None, 4096)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 1024)              4195328   
_________________________________________________________________
dense_2 (Dense)              (None, 64)                65600     
=================================================================
Total params: 4,260,928
Trainable params: 4,260,928
Non-trainable params: 0
_________________________________________________________________
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_3 (Dense)              (None, 1024)              66560     
_________________________________________________________________
dense_4 (Dense)              (None, 4096)              4198400   
_________________________________________________________________
reshape_1 (Reshape)          (None, 64, 64, 1)         0         
=================================================================
Total params: 4,264,960
Trainable params: 4,264,960
Non-trainable params: 0
_________________________________________________________________

학습 모델

1
2
3
4
5
inputs = Input(input_shape)
encoded = encoder(inputs)
decoded = decoder(encoded)
model = Model(inputs, decoded)
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 64, 64, 1)         0         
_________________________________________________________________
sequential_1 (Sequential)    (None, 64)                4260928   
_________________________________________________________________
sequential_2 (Sequential)    (None, 64, 64, 1)         4264960   
=================================================================
Total params: 8,525,888
Trainable params: 8,525,888
Non-trainable params: 0
_________________________________________________________________
1
2
op = Adam(lr=0.001, amsgrad=True)
model.compile(optimizer=op, loss='binary_crossentropy')
1
history = model.fit(X_train, X_train, batch_size=64, epochs=100, verbose=0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
plt.subplot(2, 2, 1)
plt.imshow(X_train[127].reshape(64, 64), cmap="gray")
plt.title("원본이미지")
plt.axis("off")
plt.subplot(2, 2, 2)
plt.imshow(model.predict(
X_train[127][np.newaxis, :, :, :]).reshape(64, 64), cmap="gray")
plt.title("디코딩된 이미지")
plt.axis("off")
plt.subplot(2, 2, 3)
plt.imshow(X_train[91].reshape(64, 64), cmap="gray")
plt.axis("off")
plt.subplot(2, 2, 4)
plt.imshow(model.predict(
X_train[91][np.newaxis, :, :, :]).reshape(64, 64), cmap="gray")
plt.axis("off")
plt.tight_layout()
plt.show()

png

Vector 연산

265 - 376 + 77 ?

1
X_train[[31, 33, 77]].shape
(3, 64, 64, 1)
1
[a, b, c] = encoder.predict(X_train[[31, 33, 77]])
1
2
re = (a - b) /10 + c
re.shape
(64,)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
plt.subplot(221)
plt.imshow(X_train[30].reshape(64,64), cmap='gray')
plt.axis('off')
plt.subplot(222)
plt.imshow(X_train[33].reshape(64,64), cmap='gray')
plt.axis('off')
plt.subplot(223)
plt.imshow(X_train[77].reshape(64,64), cmap='gray')
plt.axis('off')
plt.subplot(224)
plt.imshow(decoder.predict(re[np.newaxis, :]).reshape(64,64), cmap='gray')
plt.axis('off')
plt.tight_layout()
plt.show()

png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
plt.subplot(1,3,1)
re_ = (b - a)/5 + c
plt.imshow(decoder.predict(re_[np.newaxis, :]).reshape(64,64), cmap='gray')
plt.title("5 devided")
plt.axis('off')

plt.subplot(1,3,2)
re_ = (b - a)/2 + c
plt.imshow(decoder.predict(re_[np.newaxis, :]).reshape(64,64), cmap='gray')
plt.title("2 devided")
plt.axis('off')

plt.subplot(1,3,3)
re_ = (b - a) + c
plt.imshow(decoder.predict(re_[np.newaxis, :]).reshape(64,64), cmap='gray')
plt.title("no devided")
plt.axis('off')

plt.tight_layout()
plt.show()

png

1
2
3
4
5
6
from sklearn.manifold import TSNE

tsne = TSNE()

code = encoder.predict(X_train)
code_manifold = tsne.fit_transform(code)

encoded image Visualization

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from matplotlib.offsetbox import OffsetImage, AnnotationBbox


def (x, y, image, zoom=0.3, ax=None):
im = OffsetImage(image, zoom=zoom, cmap='gray')
artists = []
for x0, y0 in zip(x, y):
ab = AnnotationBbox(im, (x0, y0), xycoords='data', frameon=False)
artists.append(ax.add_artist(ab))
ax.update_datalim(np.column_stack([x, y]))
ax.autoscale()
return artists


fig, ax = plt.subplots()
for i in np.random.randint(X_train.shape[0], size=20):

encoded = code_manifold[i]

imscatter([encoded[0]], [encoded[1]],
X_train[i].reshape(64, 64), zoom=0.5, ax=ax)
plt.title("2차원으로 차원축소한 이미지")
plt.show()

png