Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
drsagitn committed Oct 7, 2018
1 parent 8595266 commit 7322b54
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 8 deletions.
2 changes: 1 addition & 1 deletion LSTMAEncoderTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
5 changes: 5 additions & 0 deletions LSTMAutoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions data_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [], []

Expand Down
18 changes: 18 additions & 0 deletions display_images.py
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()
19 changes: 12 additions & 7 deletions image_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
66 changes: 66 additions & 0 deletions kerasLSTMAudoencoderTrain.py
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()
24 changes: 24 additions & 0 deletions kerasLSTMAutoencoder.py
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))

0 comments on commit 7322b54

Please sign in to comment.