-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_1ball.py
68 lines (53 loc) · 1.76 KB
/
test_1ball.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
import os
import numpy as np
import theano.tensor as T
import theano
from scipy import misc
from autoencoder_1obj import Autoencoder_1obj
from transform import *
from scene import *
from shader import *
from optimize import *
if not os.path.exists('output'):
os.makedirs('output')
train_data = np.array([misc.imread('example.png').flatten()], dtype='float32')/255.0
N,D = train_data.shape
img_sz = int(np.sqrt(D))
def scene(center1, scale1):
material1 = Material((0.2, 0.9, 0.4), 0.3, 0.7, 0.5, 50.)
t1 = translate(center1) * scale(scale1)
shapes = [
Sphere(t1, material1)
]
light = Light((-1., -1., 2.), (0.961, 1., 0.87))
camera = Camera(img_sz, img_sz)
shader = DepthMapShader(6.1)
scene = Scene(shapes, [light], camera, shader)
return scene.build()
ae = Autoencoder_1obj(scene, D, 300, 30, 10)
opt = MGDAutoOptimizer(ae)
#import pdb; pdb.set_trace()
recon = ae.get_reconstruct(train_data[0])[:,:,0].eval()
imsave('output/test0.png', recon)
epsilon = 0.0001
num_epoch = 100
train_ae = opt.optimize(train_data)
get_recon = theano.function([], ae.get_reconstruct(train_data[0])[:,:,0])
get_rvars= theano.function([], ae.encoder(train_data[0]))
rvars = get_rvars()
print '...Initial center1 (%g,%g,%g) scale1 (%g,%g,%g)' % (
rvars[0], rvars[1], rvars[2], rvars[3], rvars[4], rvars[5])
print recon.sum()
n=0;
while (n<num_epoch):
n+=1
#ggg =get_grad()
#gbb =get_gradb()
eps = get_epsilon(epsilon, num_epoch, n)
train_loss = train_ae(eps)
rvars = get_rvars()
print 'Epoch %d Train loss %g, Center (%g, %g, %g) Scale (%g, %g, %g)' \
% (n, train_loss, rvars[0], rvars[1], rvars[2],
rvars[3], rvars[4], rvars[5])
image = get_recon()
imsave('output/test%d.png' % (n,), image)