-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
140 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
def show_img(img): | ||
plt.figure(figsize=(20, 4)) | ||
plt.imshow(img) | ||
plt.show() | ||
|
||
def show_img_arr(img_arr, n_image_per_row): | ||
n_image = len(img_arr) | ||
plt.figure(figsize=(20, 4)) | ||
for idx, img in enumerate(img_arr): | ||
ax = plt.subplot(int(n_image/n_image_per_row+1), n_image_per_row, idx + 1) | ||
plt.imshow(img) | ||
plt.gray() | ||
ax.get_xaxis().set_visible(False) | ||
ax.get_yaxis().set_visible(False) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from kerasLSTMAutoencoder import * | ||
from keras.datasets import mnist | ||
import numpy as np | ||
import os | ||
from data_gen import get_data_full | ||
|
||
# x_train = get_data_full("data/scaled_data/UCSDped1/Train", timesteps) | ||
# x_test = get_data_full("data/scaled_data/UCSDped1/Test", timesteps) | ||
|
||
# from display_images import show_img, show_img_arr | ||
# show_img_arr(x_train[2, 0:10, :].reshape(10, 76, 115), 5) | ||
|
||
(x_train, _), (x_test, _) = mnist.load_data() | ||
x_train = x_train.astype('float32') / 255. | ||
x_test = x_test.astype('float32') / 255. | ||
x_train = x_train.reshape((int(len(x_train)/timesteps), timesteps, np.prod(x_train.shape[1:]))) | ||
x_test = x_test.reshape((int(len(x_test)/timesteps), timesteps, np.prod(x_test.shape[1:]))) | ||
print(x_train.shape) | ||
print(x_test.shape) | ||
|
||
model_path = "keras_model/lstmAutoencoder.h5" #_ucsdped1 | ||
if(os.path.isfile(model_path)): | ||
print("Load saved model at ", model_path) | ||
LSTMautoencoder.load_weights(model_path) | ||
|
||
LSTMautoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') # (optimizer='rmsprop', loss=vae_loss) | ||
hist = LSTMautoencoder.fit(x_train, x_train, | ||
epochs=10, | ||
batch_size=200, | ||
shuffle=True, | ||
validation_data=(x_test, x_test)) | ||
|
||
if(hist.history['loss'][-1] < hist.history['loss'][0]): | ||
print("Model improved from ", hist.history['loss'][0], " to ", hist.history['loss'][-1]) | ||
LSTMautoencoder.save_weights(model_path) | ||
print("Saved model into ", model_path) | ||
|
||
|
||
encoded_imgs = encoder.predict(x_test) | ||
encoded_imgs = encoded_imgs.reshape((int(len(x_test)/timesteps), timesteps, latent_dim)) | ||
decoded_imgs = decoder.predict(encoded_imgs) | ||
|
||
|
||
import matplotlib.pyplot as plt | ||
|
||
n = 3 # how many batch of sequence we will display | ||
plt.figure(figsize=(20, 4)) | ||
for i in range(n): | ||
# display original | ||
img_arr = x_test[i].reshape(timesteps, imgWidth, imgHeight) | ||
for idx, img in enumerate(img_arr): | ||
ax = plt.subplot(2*n, timesteps, i*timesteps*2 + idx + 1) | ||
plt.imshow(img) | ||
plt.gray() | ||
ax.get_xaxis().set_visible(False) | ||
ax.get_yaxis().set_visible(False) | ||
|
||
# display reconstruction | ||
img_arr = decoded_imgs[i].reshape(timesteps, imgWidth, imgHeight) | ||
for idx, img in enumerate(img_arr): | ||
ax = plt.subplot(2*n, timesteps, i*timesteps*2 + idx + 1 + timesteps) | ||
plt.imshow(img) | ||
plt.gray() | ||
ax.get_xaxis().set_visible(False) | ||
ax.get_yaxis().set_visible(False) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from keras.layers import Input, LSTM, RepeatVector | ||
from keras.models import Model | ||
|
||
timesteps = 10 | ||
imgWidth = 28 | ||
imgHeight = 28 | ||
input_dim = imgWidth*imgHeight #115*76 | ||
latent_dim = 32 | ||
|
||
inputs = Input(shape=(timesteps, input_dim)) | ||
encoded = LSTM(latent_dim)(inputs) | ||
|
||
decoded = RepeatVector(timesteps)(encoded) | ||
decoded = LSTM(input_dim, return_sequences=True)(decoded) | ||
|
||
# Autoencoder | ||
LSTMautoencoder = Model(inputs, decoded) | ||
|
||
# Encoder model | ||
encoder = Model(inputs, encoded) | ||
# # Decoder model | ||
encoder_output = Input(shape=(timesteps, latent_dim,)) | ||
decoder_layer = LSTMautoencoder.layers[-1] | ||
decoder = Model(encoder_output, decoder_layer(encoder_output)) |