-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
57 lines (44 loc) · 1.86 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import numpy as np
import tensorflow as tf
import tensorflow.contrib.slim as slim
from PIL import Image
from renderer import Renderer
from net import AdversarialNet
from config import cfg
def main():
texture = Image.open(cfg.texture)
height, width = texture.size
renderer = Renderer((299, 299))
renderer.load_obj(cfg.obj)
renderer.set_parameters(
camera_distance=(cfg.camera_distance_min, cfg.camera_distance_max),
x_translation=(cfg.x_translation_min, cfg.x_translation_max),
y_translation=(cfg.y_translation_min, cfg.y_translation_max)
)
texture = np.asarray(texture).astype(np.float32)[..., :3] / 255.0
model = AdversarialNet(texture)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver(slim.get_model_variables(scope='InceptionV3'))
saver.restore(sess, os.path.join(cfg.model_dir, cfg.model_name))
writer = tf.summary.FileWriter(cfg.logdir)
for i in range(cfg.iterations):
uv = renderer.render(cfg.batch_size) * \
np.asarray([width - 1, height - 1], dtype=np.float32)
summary, loss, indices, diff, _ = sess.run(
[model.train_summary, model.loss, model.indices, model.diff, model.update],
feed_dict={model.uv_mapping: uv}
)
print('Loss: {}'.format(loss))
print('Diff: {}'.format(diff.sum()))
print('Prediction:\n{}'.format(indices))
writer.add_summary(summary, i)
if i % 10 == 0:
adv_texture = np.rint(sess.run([model.adv_texture])[0] * 255)
adv_texture = Image.fromarray(adv_texture.astype(np.uint8))
adv_texture.save('{}/adv_{}.jpg'.format(cfg.image_dir, i))
if __name__ == '__main__':
main()