Skip to content

python 2 to 3 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
__pycache__/
training/
MNIST_data/
8 changes: 4 additions & 4 deletions input_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def maybe_download(filename, work_directory):
os.mkdir(work_directory)
filepath = os.path.join(work_directory, filename)
if not os.path.exists(filepath):
filepath, _ = urllib.urlretrieve(SOURCE_URL + filename, filepath)
filepath, _ = urllib.request.urlretrieve(SOURCE_URL + filename, filepath)
statinfo = os.stat(filepath)
print 'Succesfully downloaded', filename, statinfo.st_size, 'bytes.'
print ('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
return filepath


Expand All @@ -25,7 +25,7 @@ def _read32(bytestream):

def extract_images(filename):
"""Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
print 'Extracting', filename
print ('Extracting', filename)
with gzip.open(filename) as bytestream:
magic = _read32(bytestream)
if magic != 2051:
Expand All @@ -52,7 +52,7 @@ def dense_to_one_hot(labels_dense, num_classes=10):

def extract_labels(filename, one_hot=False):
"""Extract the labels into a 1D uint8 numpy array [index]."""
print 'Extracting', filename
print ('Extracting', filename)
with gzip.open(filename) as bytestream:
magic = _read32(bytestream)
if magic != 2049:
Expand Down
18 changes: 10 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tensorflow as tf
import numpy as np
import input_data
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
import os
from scipy.misc import imsave as ims
Expand Down Expand Up @@ -64,16 +64,18 @@ def train(self):
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for epoch in range(10):
gen_loss = None
lat_loss = None

for idx in range(int(self.n_samples / self.batchsize)):
batch = self.mnist.train.next_batch(self.batchsize)[0]
_, gen_loss, lat_loss = sess.run((self.optimizer, self.generation_loss, self.latent_loss), feed_dict={self.images: batch})
# dumb hack to print cost every epoch
if idx % (self.n_samples - 3) == 0:
print "epoch %d: genloss %f latloss %f" % (epoch, np.mean(gen_loss), np.mean(lat_loss))
saver.save(sess, os.getcwd()+"/training/train",global_step=epoch)
generated_test = sess.run(self.generated_images, feed_dict={self.images: visualization})
generated_test = generated_test.reshape(self.batchsize,28,28)
ims("results/"+str(epoch)+".jpg",merge(generated_test[:64],[8,8]))

print ("epoch {}: genloss {} latloss {}".format(epoch, np.mean(gen_loss), np.mean(lat_loss)))
saver.save(sess, os.getcwd()+"/training/train",global_step=epoch)
generated_test = sess.run(self.generated_images, feed_dict={self.images: visualization})
generated_test = generated_test.reshape(self.batchsize,28,28)
ims("results/"+str(epoch)+".jpg",merge(generated_test[:64],[8,8]))


model = LatentAttention()
Expand Down
2 changes: 1 addition & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def merge(images, size):

for idx, image in enumerate(images):
i = idx % size[1]
j = idx / size[1]
j = int(idx / size[1])
img[j*h:j*h+h, i*w:i*w+w] = image

return img