In [1]:
from __future__ import division, print_function
from keras.layers import Input
from keras.layers.core import Dense, Flatten, Reshape, Lambda
from keras.layers.convolutional import Conv2D, Conv2DTranspose, UpSampling2D
from keras.layers.pooling import MaxPooling2D, GlobalAveragePooling2D
from keras.models import Model
from sklearn.manifold import TSNE
import numpy as np
import matplotlib.pyplot as plt
import os
import tensorflow as tf
%matplotlib inline
In [2]:
DATA_DIR = "../../data"
TRAIN_FILE = os.path.join(DATA_DIR, "mnist_train.csv")
TEST_FILE = os.path.join(DATA_DIR, "mnist_test.csv")
BATCH_SIZE = 128
IMG_SIZE = 28
NUM_CLASSES = 10
NUM_EPOCHS = 10
In [3]:
def parse_file(filename):
xdata, ydata = [], []
fin = open(filename, "rb")
i = 0
for line in fin:
if i % 10000 == 0:
print("{:s}: {:d} lines read".format(
os.path.basename(filename), i))
cols = line.strip().split(",")
ydata.append(int(cols[0]))
xdata.append([float(x) / 255. for x in cols[1:]])
i += 1
fin.close()
print("{:s}: {:d} lines read".format(os.path.basename(filename), i))
y = np.array(ydata)
X = np.array(xdata).reshape((-1, IMG_SIZE, IMG_SIZE, 1))
return X, y
Xtrain, ytrain = parse_file(TRAIN_FILE)
Xtest, ytest = parse_file(TEST_FILE)
print(Xtrain.shape, ytrain.shape, Xtest.shape, ytest.shape)
In [4]:
xin = Input(batch_shape=(None, IMG_SIZE, IMG_SIZE, 1))
# encoder
conv1 = Conv2D(1, kernel_size=(2, 2), padding="same",
activation="relu")(xin)
pool1 = MaxPooling2D(pool_size=(2, 2), padding="same")(conv1)
conv2 = Conv2D(64, kernel_size=(2, 2), padding="same",
activation="relu")(pool1)
pool2 = MaxPooling2D(pool_size=(2, 2), padding="same")(conv2)
# hidden
hidden = GlobalAveragePooling2D()(pool2)
hidden_r = Reshape((8, 8, 1))(hidden)
# decoder
deconv2 = Conv2DTranspose(64, kernel_size=(2, 2), padding="same",
activation="relu")(hidden_r)
upsamp2 = UpSampling2D(size=(2, 2))(deconv2)
deconv1 = Conv2DTranspose(1, kernel_size=(2, 2), padding="same",
activation="relu")(upsamp2)
upsamp1 = UpSampling2D(size=(2, 2))(deconv1)
xout = Lambda(lambda x: tf.image.resize_image_with_crop_or_pad(
x, IMG_SIZE, IMG_SIZE))(upsamp1)
autoencoder = Model(inputs=xin, outputs=xout)
In [5]:
for layer in autoencoder.layers:
print(layer.name, layer.input_shape, layer.output_shape)
In [6]:
autoencoder.compile(optimizer="adam", loss="mse")
In [7]:
history = autoencoder.fit(Xtrain, Xtrain, batch_size=BATCH_SIZE,
epochs=NUM_EPOCHS,
validation_data=(Xtest, Xtest))
In [8]:
plt.plot(np.arange(NUM_EPOCHS), history.history["loss"],
color="r", label="train")
plt.plot(np.arange(NUM_EPOCHS), history.history["val_loss"],
color="b", label="val")
plt.xlabel("epochs")
plt.ylabel("MSE loss")
plt.legend(loc="best")
Out[8]:
In [9]:
Xviz = Xtest[0:5]
Xviz_ = autoencoder.predict(Xviz)
j = 1
for i in range(5):
img_orig = Xviz[i].reshape(28, 28)
img_ae = Xviz_[i].reshape(28, 28)
plt.subplot(5, 2, j)
plt.imshow(img_orig)
plt.xticks([])
plt.yticks([])
j += 1
plt.subplot(5, 2, j)
plt.imshow(img_ae)
plt.xticks([])
plt.yticks([])
j += 1
plt.show()
In [10]:
encoder = Model(inputs=xin, outputs=hidden)
xout_ = encoder.predict(Xtest)
print(Xtest.shape, xout_.shape)
In [11]:
tsne = TSNE(n_components=2)
Z = tsne.fit_transform(xout_)
print(Z.shape)
In [12]:
colors = ["r", "g", "b", "c", "m", "y", "orange", "brown", "gray", "k"]
for i in range(NUM_CLASSES):
yi_indices = np.where(ytest == i)[0]
xi = Z[yi_indices][:, 0].tolist()
yi = Z[yi_indices][:, 1].tolist()
plt.scatter(xi, yi, color=colors[i], alpha=0.5, label=str(i))
plt.legend(loc="best")
plt.xlabel("Z1")
plt.ylabel("Z2")
plt.show()
In [ ]: