From 7322b54e5f27cbb4f778fb25aba57cbb8de80502 Mon Sep 17 00:00:00 2001 From: thinh Date: Sun, 7 Oct 2018 17:51:42 +0900 Subject: [PATCH] update --- LSTMAEncoderTest.py | 2 +- LSTMAutoencoder.py | 5 +++ data_gen.py | 14 ++++++++ display_images.py | 18 ++++++++++ image_preprocessing.py | 19 +++++++---- kerasLSTMAudoencoderTrain.py | 66 ++++++++++++++++++++++++++++++++++++ kerasLSTMAutoencoder.py | 24 +++++++++++++ 7 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 display_images.py create mode 100644 kerasLSTMAudoencoderTrain.py create mode 100644 kerasLSTMAutoencoder.py diff --git a/LSTMAEncoderTest.py b/LSTMAEncoderTest.py index 3439c5e..6010b0e 100644 --- a/LSTMAEncoderTest.py +++ b/LSTMAEncoderTest.py @@ -37,7 +37,7 @@ iteration = 1 for i in range(iteration): - pinput = get_next_batch(XTest, i+5, batch_size) + pinput = get_next_batch(XTest, i+15, batch_size) (input_, output_) = sess.run([ae.input_, ae.output_], {p_input: pinput}) diff --git a/LSTMAutoencoder.py b/LSTMAutoencoder.py index 6afcc08..8bdc47b 100644 --- a/LSTMAutoencoder.py +++ b/LSTMAutoencoder.py @@ -49,6 +49,7 @@ def __init__( with tf.variable_scope('encoder'): (self.z_codes, self.enc_state) = tf.contrib.rnn.static_rnn(self._enc_cell, inputs, dtype=tf.float32) + with tf.variable_scope('decoder') as vs: dec_weight_ = tf.Variable(tf.truncated_normal([hidden_num, self.elem_num], dtype=tf.float32), name='dec_weight' @@ -67,9 +68,13 @@ def __init__( dec_outputs = dec_outputs[::-1] dec_output_ = tf.transpose(tf.stack(dec_outputs), [1, 0, 2]) + dec_output_ = tf.Print(dec_output_, [dec_output_], message="dec_output_:") dec_weight_ = tf.tile(tf.expand_dims(dec_weight_, 0), [self.batch_num, 1, 1]) + dec_bias_ = tf.Print(dec_bias_, [dec_bias_], message="dec_bias_:") + dec_weight_ = tf.Print(dec_weight_, [dec_weight_], message="dec_weight_:") self.output_ = tf.matmul(dec_output_, dec_weight_) + dec_bias_ + self.output_ = tf.Print(self.output_, [self.output_], message="self.output_:") else: dec_state = self.enc_state diff --git a/data_gen.py b/data_gen.py index 2d52249..f6c3c8a 100644 --- a/data_gen.py +++ b/data_gen.py @@ -45,6 +45,20 @@ def get_train_data(training_dir, step_num): return np.array(X_data).reshape(int(len(X_data)/step_num), step_num) +def get_data_full(training_dir, step_num): + X_data = [] + for r, dirs, files in os.walk(training_dir): + for dir in dirs: + for file in sorted(os.listdir(os.path.join(r, dir))): + file_path = os.path.join(r, dir, file) + try: + img = mpimg.imread(str(file_path)).reshape(-1) + X_data.append(img) + except Exception as ex: + print("Exception while reading ", file_path, ". Skipping it") + return np.array(X_data).reshape(int(len(X_data) / step_num), step_num, len(img)) + + def get_test_data(): return [], [] diff --git a/display_images.py b/display_images.py new file mode 100644 index 0000000..f23220a --- /dev/null +++ b/display_images.py @@ -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() diff --git a/image_preprocessing.py b/image_preprocessing.py index c10d308..de175c6 100644 --- a/image_preprocessing.py +++ b/image_preprocessing.py @@ -2,17 +2,22 @@ import os base_width = 115 -target_folder = 'data/UCSDped1/Train' -out_folder = 'data/scaled_data/UCSDped1/Train' +target_folder = 'data/UCSDped1/Test' +out_folder = 'data/scaled_data/UCSDped1/Test' for d in os.listdir(target_folder): image_folder_path = os.path.join(target_folder, d) + if not os.path.isdir(image_folder_path): + continue os.mkdir(os.path.join(out_folder, d)) for image_file_name in os.listdir(image_folder_path): image_file_path = os.path.join(image_folder_path, image_file_name) print("Processing ", image_file_path) - img = Image.open(image_file_path) - percent = base_width/float(img.size[0]) - hsize = int(float(img.size[1])*percent) - img = img.resize((base_width, hsize), Image.ANTIALIAS) - img.save(os.path.join(out_folder, d, image_file_name)) + try: + img = Image.open(image_file_path) + percent = base_width/float(img.size[0]) + hsize = int(float(img.size[1])*percent) + img = img.resize((base_width, hsize), Image.ANTIALIAS) + img.save(os.path.join(out_folder, d, image_file_name)) + except Exception as e: + print("skip ", image_file_path) diff --git a/kerasLSTMAudoencoderTrain.py b/kerasLSTMAudoencoderTrain.py new file mode 100644 index 0000000..4845243 --- /dev/null +++ b/kerasLSTMAudoencoderTrain.py @@ -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() \ No newline at end of file diff --git a/kerasLSTMAutoencoder.py b/kerasLSTMAutoencoder.py new file mode 100644 index 0000000..5dc8992 --- /dev/null +++ b/kerasLSTMAutoencoder.py @@ -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)) \ No newline at end of file