-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
83 lines (71 loc) · 3.37 KB
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
import tensorflow as tf
import evaluater
TARGET_CLASS = 1
OUTPUT_DIR = 'images'
#
# FLAGS = tf.app.flags.FLAGS
# tf.app.flags.DEFINE_integer('target_class', 1,
# """target class index.""")
# tf.app.flags.DEFINE_string('images_dir', 'images',
# """Directory where to write generated images.""")
def main(argv=None):
imagenet = evaluater.ImageNet()
# input variable
with tf.variable_scope('input') as scope:
v = tf.get_variable('input', shape=(96, 96, 3),
initializer=tf.random_uniform_initializer(0.0, 1.0))
# per_image_whitening without relu
image = tf.mul(tf.clip_by_value(v, 0.0, 1.0), 255.5)
mean, variance = tf.nn.moments(image, [0, 1, 2])
pixels = tf.reduce_prod(tf.shape(image))
stddev = tf.sqrt(tf.maximum(variance, 0))
image = tf.sub(image, mean)
image = tf.div(image, tf.maximum(
stddev, tf.inv(tf.sqrt(tf.cast(pixels, tf.float32)))))
# loss and train
inputs = tf.expand_dims(image, 0)
filename = 'generated-%03d.jpg' % TARGET_CLASS
output_image = tf.image.convert_image_dtype(v, tf.uint8, saturate=True)
eval_image_path = os.path.join(OUTPUT_DIR, filename)
logits = imagenet.inference(eval_image_path)
logits_v = tf.Variable(logits)
#logits = r.inference(inputs, FLAGS.num_classes)
softmax = tf.nn.softmax(logits_v)
losses = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits_v, [TARGET_CLASS])
train_op = tf.train.AdamOptimizer().minimize(losses, var_list=[v])
# variable_averages = tf.train.ExponentialMovingAverage(
# r.MOVING_AVERAGE_DECAY)
# variables_to_restore = {}
# for key, value in variable_averages.variables_to_restore().items():
# if not key.startswith('input'):
# variables_to_restore[key] = value
# saver = tf.train.Saver(variables_to_restore)
# checkpoint = tf.train.latest_checkpoint(FLAGS.train_dir)
with tf.Session() as sess:
sess.run(tf.initialize_local_variables())
#saver.restore(sess, checkpoint)
for step in range(1000):
print(step)
with open(eval_image_path, 'wb') as f:
f.write(sess.run(tf.image.encode_jpeg(
output_image, quality=100, chroma_downsampling=False)))
_, loss_value, softmax_value = sess.run(
[train_op, losses, softmax])
print('%04d - loss: %f (%f)' % (step,
loss_value[0], softmax_value.flatten().tolist()[TARGET_CLASS]))
# write image to file
# output_image = tf.image.convert_image_dtype(v, tf.uint8, saturate=True)
# filename = 'target-%03d.png' % FLAGS.target_class
# with open(os.path.join(os.path.dirname(__file__), '..', '..', FLAGS.images_dir, filename), 'wb') as f:
# f.write(sess.run(tf.image.encode_png(output_image)))
# filename = 'target-%03d.jpg' % FLAGS.target_class
# with open(os.path.join(os.path.dirname(__file__), '..', '..', FLAGS.images_dir, filename), 'wb') as f:
# f.write(sess.run(tf.image.encode_jpeg(
# output_image, quality=100, chroma_downsampling=False)))
if __name__ == '__main__':
tf.app.run()