Skip to content

Commit

Permalink
Added requirements.txt, use tf==1.7 because this repo is using old TF…
Browse files Browse the repository at this point in the history
… api and added a script to get the datasets required, taken from tdrussell#1 (comment)
  • Loading branch information
hbina committed Oct 11, 2018
1 parent aecb729 commit ce3f6ab
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 86 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
__pycache__/
datasets/
venv/
52 changes: 31 additions & 21 deletions custom_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def leaky_rectify(x, leakiness=0.1):
# import ipdb; ipdb.set_trace()
return ret


'''
@pt.Register
class custom_conv2d(pt.VarStoreMethod):
Expand All @@ -78,17 +79,18 @@ def __call__(self, input_layer, output_dim,
return input_layer.with_tensor(tf.nn.bias_add(conv, biases), parameters=self.vars)
'''


@pt.Register
class custom_deconv2d(pt.VarStoreMethod):
def __call__(self, input_layer, output_dim,
k_h=7, k_w=7, d_h=2, d_w=2, stddev=0.02,
name="deconv2d"):
#output_shape[0] = input_layer.shape[0]
#ts_output_shape = tf.pack(output_shape)
# output_shape[0] = input_layer.shape[0]
# ts_output_shape = tf.pack(output_shape)
batch_size = input_layer.shape[0]
h = input_layer.shape[1]
w = input_layer.shape[2]
output_shape = [batch_size, h*2, w*2, output_dim]
output_shape = [batch_size, h * 2, w * 2, output_dim]
with tf.variable_scope(name):
# filter : [height, width, output_channels, in_channels]
w = self.variable('w', [k_h, k_w, output_dim, input_layer.shape[-1]],
Expand All @@ -109,18 +111,19 @@ def __call__(self, input_layer, output_dim,

return deconv


@pt.Register
class minibatch_discrimination(pt.VarStoreMethod):
def __call__(self, input_layer, num_kernels, dim_per_kernel=5, name='minibatch_discrim'):
batch_size = input_layer.shape[0]
num_features = input_layer.shape[1]
W = self.variable('W', [num_features, num_kernels*dim_per_kernel],
W = self.variable('W', [num_features, num_kernels * dim_per_kernel],
init=tf.contrib.layers.xavier_initializer())
b = self.variable('b', [num_kernels], init=tf.constant_initializer(0.0))
activation = tf.matmul(input_layer, W)
activation = tf.reshape(activation, [batch_size, num_kernels, dim_per_kernel])
tmp1 = tf.expand_dims(activation, 3)
tmp2 = tf.transpose(activation, perm=[1,2,0])
tmp2 = tf.transpose(activation, perm=[1, 2, 0])
tmp2 = tf.expand_dims(tmp2, 0)
abs_diff = tf.reduce_sum(tf.abs(tmp1 - tmp2), reduction_indices=[2])
f = tf.reduce_sum(tf.exp(-abs_diff), reduction_indices=[2])
Expand Down Expand Up @@ -159,28 +162,31 @@ def __call__(self, input_layer, output_size, scope=None, in_dim=None, stddev=0.0
# import ipdb; ipdb.set_trace()
'''


# http://stackoverflow.com/a/17201686
def matlab_style_gauss2D(shape=(3,3),sigma=0.5):
def matlab_style_gauss2D(shape=(3, 3), sigma=0.5):
"""
2D gaussian mask - should give the same result as MATLAB's
fspecial('gaussian',[shape],[sigma])
"""
m,n = [(ss-1.)/2. for ss in shape]
y,x = np.ogrid[-m:m+1,-n:n+1]
h = np.exp( -(x*x + y*y) / (2.*sigma*sigma) )
h[ h < np.finfo(h.dtype).eps*h.max() ] = 0
m, n = [(ss - 1.) / 2. for ss in shape]
y, x = np.ogrid[-m:m + 1, -n:n + 1]
h = np.exp(-(x * x + y * y) / (2. * sigma * sigma))
h[h < np.finfo(h.dtype).eps * h.max()] = 0
sumh = h.sum()
if sumh != 0:
h /= sumh
return h


def gaussian_blur(x):
f = matlab_style_gauss2D(shape=(5,5), sigma=1)
f = matlab_style_gauss2D(shape=(5, 5), sigma=1)
f = np.expand_dims(f, 2)
f = np.expand_dims(f, 3)
f = np.tile(f, (1, 1, 3, 1))
f = f.astype(np.float32)
return tf.nn.depthwise_conv2d(x, f, [1,1,1,1], padding='SAME')
return tf.nn.depthwise_conv2d(x, f, [1, 1, 1, 1], padding='SAME')


def depthwise_conv2d_transpose(value, filter, output_shape, strides, padding='SAME', name=None):
output_shape_ = ops.convert_to_tensor(output_shape, name="output_shape")
Expand All @@ -194,20 +200,22 @@ def depthwise_conv2d_transpose(value, filter, output_shape, strides, padding='SA
padding=padding,
name=name)


@ops.RegisterGradient('DepthwiseConv2dNativeBackpropInput')
def _DepthwiseConv2dNativeBackpropInput(op, grad):
return [None,
nn_ops.depthwise_conv2d_native_backprop_filter(grad, array_ops.shape(op.inputs[1]),
op.inputs[2], op.get_attr("strides"),
op.get_attr("padding")),
op.inputs[2], op.get_attr("strides"),
op.get_attr("padding")),
nn_ops.depthwise_conv2d_native(grad, op.inputs[1], op.get_attr("strides"),
op.get_attr("padding"))]
op.get_attr("padding"))]


def upsample_bilinear_2x(input):
output_shape = input.get_shape().as_list()
output_shape[1] = output_shape[1]*2
output_shape[2] = output_shape[2]*2
output_shape[1] = output_shape[1] * 2
output_shape[2] = output_shape[2] * 2

f = [[0.25, 0.5, 0.25],
[0.5, 1, 0.5],
[0.25, 0.5, 0.25]]
Expand All @@ -216,7 +224,8 @@ def upsample_bilinear_2x(input):
f = np.expand_dims(f, 3)
f = np.tile(f, (1, 1, output_shape[3], 1))
f = f.astype(np.float32)
return depthwise_conv2d_transpose(input, f, output_shape, [1,2,2,1])
return depthwise_conv2d_transpose(input, f, output_shape, [1, 2, 2, 1])


@pt.Register
class upsample_conv(pt.VarStoreMethod):
Expand All @@ -230,15 +239,16 @@ def __call__(self, input_layer, kernel, depth, padding='SAME', name="upsample_co
biases = self.variable('biases', [depth], init=tf.constant_initializer(0.0))
return input_layer.with_tensor(tf.nn.bias_add(conv, biases), parameters=self.vars)


def k_sparsify(x, k):
values, _ = tf.nn.top_k(x, k=k, sorted=True)
min_value = tf.slice(values, [0,k-1], [-1, 1])
min_value = tf.slice(values, [0, k - 1], [-1, 1])
bool_mask = tf.greater_equal(x, min_value)
float_mask = tf.cast(bool_mask, tf.float32)
sparse_x = tf.mul(x, float_mask)
return sparse_x


def histogram_summary(x, name):
tf.histogram_summary(name, x)
return x

22 changes: 10 additions & 12 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
from __future__ import division
from __future__ import print_function

import sys
import os.path

import matplotlib.pyplot as plt
import numpy as np
#import cv2
import tensorflow as tf
import prettytensor as pt

import matplotlib.pyplot as plt
# import cv2
import tensorflow as tf
from mpl_toolkits.axes_grid1 import ImageGrid

import model
import input
import model

FLAGS = tf.app.flags.FLAGS

Expand All @@ -23,6 +19,7 @@

GRID = (8, 8)


def init_variables(sess):
init_op = tf.initialize_all_variables()
sess.run(init_op)
Expand All @@ -37,14 +34,15 @@ def init_variables(sess):
raise Exception('No checkpoint file found')
return


def main(argv=None):
input.init_dataset_constants()
num_images = GRID[0] * GRID[1]
FLAGS.batch_size = num_images
with tf.Graph().as_default():
g_template = model.generator_template()
z = tf.placeholder(tf.float32, shape=[FLAGS.batch_size, FLAGS.z_size])
#np.random.seed(1337) # generate same random numbers each time
# np.random.seed(1337) # generate same random numbers each time
noise = np.random.normal(size=(FLAGS.batch_size, FLAGS.z_size))
with pt.defaults_scope(phase=pt.Phase.test):
gen_images_op, _ = pt.construct_all(g_template, input=z)
Expand All @@ -55,12 +53,12 @@ def main(argv=None):
gen_images = (gen_images + 1) / 2

sess.close()

fig = plt.figure(1)
grid = ImageGrid(fig, 111,
nrows_ncols=GRID,
axes_pad=0.1)
for i in xrange(num_images):
for i in range(num_images):
im = gen_images[i]
axis = grid[i]
axis.axis('off')
Expand All @@ -69,6 +67,6 @@ def main(argv=None):
plt.show()
fig.savefig('montage.png', dpi=100, bbox_inches='tight')


if __name__ == '__main__':
tf.app.run()

7 changes: 7 additions & 0 deletions get_datasets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
mkdir datasets
cd datasets
mkdir anime_faces
cd anime_faces
curl "https://media.kitsu.io/characters/images/[1-100000]/original.jpg" -o "#1.jpg"
curl "http://www.anime-planet.com/images/characters/i-[1-100000].jpg" -o "#1.jpg"
24 changes: 13 additions & 11 deletions input.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
from __future__ import division
from __future__ import print_function

import sys
import os.path
import time

import numpy as np
import tensorflow as tf

FLAGS = tf.app.flags.FLAGS
Expand All @@ -16,6 +13,7 @@
tf.app.flags.DEFINE_string('dataset', 'custom',
'One of: custom, cifar')


def read_and_decode_cifar(filename_queue):
label_bytes = 1
height = 32
Expand All @@ -35,6 +33,7 @@ def read_and_decode_cifar(filename_queue):

return image


def read_and_decode1(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
Expand All @@ -59,9 +58,9 @@ def read_and_decode1(filename_queue):

shape = tf.cast(tf.shape(image), tf.float32)
height_pad = tf.maximum(tf.ceil((96 - shape[0]) / 2), 0)
height_pad = tf.reshape(height_pad, [1,1])
height_pad = tf.reshape(height_pad, [1, 1])
width_pad = tf.maximum(tf.ceil((96 - shape[1]) / 2), 0)
width_pad = tf.reshape(width_pad, [1,1])
width_pad = tf.reshape(width_pad, [1, 1])
height_pad = tf.tile(height_pad, [1, 2])
width_pad = tf.tile(width_pad, [1, 2])
paddings = tf.concat(0, [height_pad, width_pad, tf.zeros([1, 2])])
Expand All @@ -73,16 +72,18 @@ def read_and_decode1(filename_queue):

# downsample
image = tf.image.resize_images(image, IMAGE_SIZE, IMAGE_SIZE, method=tf.image.ResizeMethod.AREA)
#image = tf.image.resize_images(image, IMAGE_SIZE, IMAGE_SIZE, method=tf.image.ResizeMethod.BICUBIC)

# image = tf.image.resize_images(image, IMAGE_SIZE, IMAGE_SIZE, method=tf.image.ResizeMethod.BICUBIC)

# randomly flip the image horizontally
image = tf.image.random_flip_left_right(image)

label = features['label']
label = tf.slice(label, [0], [NUM_TAGS_TO_USE])

return image, label


def read_and_decode2(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
Expand All @@ -109,10 +110,11 @@ def read_and_decode2(filename_queue):

return image


def inputs():
if FLAGS.dataset == 'cifar':
filenames = [os.path.join(FLAGS.data_dir, 'cifar', 'data_batch_%d.bin' % i)
for i in xrange(1, 6)]
for i in range(1, 6)]
filename_queue = tf.train.string_input_producer(filenames)
image = read_and_decode_cifar(filename_queue)
elif FLAGS.dataset == 'custom':
Expand All @@ -137,14 +139,15 @@ def inputs():
[image],
batch_size=FLAGS.batch_size,
num_threads=num_preprocess_threads,
capacity=3*min_queue_examples,
capacity=3 * min_queue_examples,
min_after_dequeue=min_queue_examples)

# display training images in visualizer
tf.image_summary('images', images, max_images=FLAGS.batch_size, name='images_summary')
tf.summary.image(name='images_summary', tensor=images, max_outputs=FLAGS.batch_size)

return images


def init_dataset_constants():
global IMAGE_SIZE
global NUM_LEVELS
Expand All @@ -159,4 +162,3 @@ def init_dataset_constants():
CHANNELS = 3
else:
raise NotImplemented()

Loading

1 comment on commit ce3f6ab

@hbina
Copy link
Owner Author

@hbina hbina commented on ce3f6ab Oct 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main repo prolly used python2.7

Please sign in to comment.