Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
drsagitn committed Oct 1, 2018
1 parent 47a1831 commit 8595266
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 24 deletions.
57 changes: 57 additions & 0 deletions LSTMAEncoderTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Basic libraries
import numpy as np
import tensorflow as tf
from data_gen import get_train_data, get_next_batch, save_images
import os

tf.reset_default_graph()
tf.set_random_seed(2016)
np.random.seed(2016)

# LSTM-autoencoder
from LSTMAutoencoder import *

# Constants
batch_size = 2
hidden_num = 1024
step_num = 10 # number of frames per time
elem_num = 115*76 # frame size h x w
n_epoch = 100

# placeholder list
p_input = tf.placeholder(tf.float32, shape=(batch_size, step_num, elem_num))
p_inputs = [tf.squeeze(t, [1]) for t in tf.split(p_input, step_num, 1)]

cell = tf.nn.rnn_cell.LSTMCell(hidden_num, use_peepholes=True)
ae = LSTMAutoencoder(hidden_num, p_inputs, cell=cell)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
save_model = "models/test.ckpt"
if os.path.exists(save_model + ".meta"):
print("load save model")
saver.restore(sess, save_model)
XTest = get_train_data("data/scaled_data/UCSDped1/Train", step_num)

iteration = 1

for i in range(iteration):
pinput = get_next_batch(XTest, i+5, batch_size)

(input_, output_) = sess.run([ae.input_, ae.output_], {p_input: pinput})

for j in range(batch_size):
save_images(output_[j, :, :], "output_" + str(i) + "_" + str(j))
save_images(input_[j, :, :], "input_" + str(i) + "_" + str(j))

print('train result :')
print('input :', input_[0, :, :].flatten())
print('input size: ',input_[0, :, :].flatten().shape)
print('output :', output_[0, :, :].flatten())
print('output2 :', output_[1, :, :].flatten())
print('diff value :', np.sum(np.absolute(input_[0, :, :].flatten() - output_[0, :, :].flatten())))
else:
print("No saved model found!")


5 changes: 4 additions & 1 deletion LSTMAutoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(
cell=None,
optimizer=None,
reverse=True,
decode_without_input=False,
decode_without_input=True,
):
"""
Args:
Expand Down Expand Up @@ -92,6 +92,9 @@ def __init__(
self.input_ = tf.transpose(tf.stack(inputs), [1, 0, 2])
self.loss = tf.reduce_mean(tf.square(self.input_
- self.output_))
tf.summary.histogram("lstmae_loss_historgram", self.loss)
tf.summary.scalar("lstmae_loss_scalar", self.loss)


if optimizer is None:
self.train = tf.train.AdamOptimizer().minimize(self.loss)
Expand Down
27 changes: 16 additions & 11 deletions data_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
import os


def get_next_batch(data_list, index, batch_size):
def get_next_batch(data_list, iter, batch_size):
X_data = []

for i in enumerate(batch_size):
index = (iter * batch_size) % len(data_list)
print(index)
for i in range(batch_size):
single_batch = read_one_batch_data(data_list[index + i])
X_data.append(single_batch)

# for idx, d in enumerate(os.listdir(data_dir)):
# if idx >= index and idx < index + batch_size:
# X_data.append(read_video(os.path.join(data_dir, d)))
return np.array(X_data, dtype=np.float32)


def read_one_batch_data(file_name_list):
ret = []
for name in file_name_list: # loop through step num:
for name in np.nditer(file_name_list): # loop through step num:
try:
img = mpimg.imread(os.path.join(name, img)).reshape(-1)
img = mpimg.imread(str(name)).reshape(-1)
ret.append(img)
except Exception as ex:
print("Exception while reading ", img, ". Skipping it")
Expand All @@ -42,11 +39,19 @@ def get_train_data(training_dir, step_num):
X_data = []
for r, dirs, files in os.walk(training_dir):
for dir in dirs:
for file in os.listdir(os.path.join(r, dir)):
for file in sorted(os.listdir(os.path.join(r, dir))):
file_path = os.path.join(r, dir, file)
X_data.append(file_path)
return np.array(X_data).reshape(int(len(X_data)/step_num), step_num)


def get_test_data():
return [], []
return [], []


def save_images(arr, name):
folder = os.path.join("data/lstmAE/test", name)
if not os.path.isdir(folder):
os.mkdir(folder)
for idx, img in enumerate(arr):
mpimg.imsave(folder + "/" + str(idx)+".tif", img.reshape(76, 115))
30 changes: 18 additions & 12 deletions lstmAEncoderTrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,40 @@
hidden_num = 1024
step_num = 10 # number of frames per time
elem_num = 115*76 # frame size h x w
iteration = 100
n_epoch = 1

# placeholder list
p_input = tf.placeholder(tf.float32, shape=(batch_num, step_num, elem_num))
p_inputs = [tf.squeeze(t, [1]) for t in tf.split(p_input, step_num, 1)]

with tf.name_scope("variables_scope"):
p_input = tf.placeholder(tf.float32, shape=(batch_num, step_num, elem_num))
p_inputs = [tf.squeeze(t, [1]) for t in tf.split(p_input, step_num, 1)]
cell = tf.nn.rnn_cell.LSTMCell(hidden_num, use_peepholes=True)
ae = LSTMAutoencoder(hidden_num, p_inputs, cell=cell, decode_without_input=True)
ae = LSTMAutoencoder(hidden_num, p_inputs, cell=cell)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter("train_logs/lstmae", sess.graph)
saver = tf.train.Saver()
save_model = "models/test.ckpt"
if os.path.exists(save_model):
if os.path.exists(save_model + ".meta"):
print("load save model")
saver.restore(sess, save_model)
XTrain = get_train_data("data/scaled_data/UCSDped1/Train", step_num)

for i in range(iteration):
pinput = get_next_batch(XTrain, i, batch_num)
iteration_per_epoch = int(len(XTrain)/batch_num)

for e in range(n_epoch):
for i in range(iteration_per_epoch):
pinput = get_next_batch(XTrain, i, batch_num)

(loss_val, _) = sess.run([ae.loss, ae.train], {p_input: pinput})

(loss_val, _) = sess.run([ae.loss, ae.train], {p_input: pinput})
print('iter %d:' % (i + 1), loss_val)
print('Epoch %d ' % e, 'iter %d:' % (i + 1), loss_val)

(input_, output_) = sess.run([ae.input_, ae.output_], {p_input: pinput})
print('train result :')
print('input :', input_[0, :, :].flatten())
print(input_[0, :, :].flatten().shape)
print('input size: ',input_[0, :, :].flatten().shape)
print('output :', output_[0, :, :].flatten())
print('diff value :', np.sum(input_[0, :, :].flatten() - output_[0, :, :].flatten()))
print('diff value :', np.sum(np.absolute(input_[0, :, :].flatten() - output_[0, :, :].flatten())))
save_path = saver.save(sess, "models/test.ckpt")
print("Model saved in path: %s" % save_path)

0 comments on commit 8595266

Please sign in to comment.