-
Notifications
You must be signed in to change notification settings - Fork 21
/
demo.py
211 lines (182 loc) · 7.73 KB
/
demo.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import argparse
import time
import yaml
from shutil import copy2
import sys
import json
import importlib
import http.server
import socketserver
import threading
from functools import partial
import cv2
import base64
# x: N by 4: r x y z
def attrswapaxis(x):
y = x * torch.tensor([1, 1,-1,-1], dtype=x.dtype, device=x.device)
return y
class TestHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, trainer, *args, **kwargs):
self.trainer = trainer
self.start_path = os.path.abspath('pyjs3d')
super().__init__(*args, **kwargs)
def send_head(self):
path = self.translate_path(self.path)
f = None
try:
if not path.startswith(self.start_path):
raise IOError
# Always read in binary mode. Opening files in text mode may cause
# newline translations, making the actual size of the content
# transmitted *less* than the content-length!
f = open(path, 'rb')
except IOError:
#self.send_error(404, "File not found")
self.send_response(301)
self.send_header("Location", "/pyjs3d/html/webgl_dualsdf_editor.html")
self.end_headers()
return None
ctype = self.guess_type(path)
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
return f
def do_POST(self):
print('********HEADER********')
#print(self.headers)
length = int(self.headers.get_all('content-length')[0])
print('********content-length********')
print(self.headers.get_all('content-length'))
print('********data_string********')
data_string = self.rfile.read(length)
#print(data_string)
data = json.loads(data_string)
#print(data)
if self.path == '/get_attributes':
exist_feature = None
if 'feature' in data.keys():
exist_feature = np.array(data['feature'], dtype=np.float32)
attrs, feature = self.handle_get_new_shape(exist_feature)
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.flush_headers()
self.wfile.write(json.dumps({'attrs': attrs.tolist(), 'feature': feature.tolist(), 'kld': self.trainer.stats_loss_kld}).encode())
elif self.path == '/update_attributes':
attrs = np.array(data['attrs'], dtype=np.float32)
attrs_mod = np.array(data['modified_attrs'], dtype=np.float32)
feature = np.array(data['feature'], dtype=np.float32)
gamma = data['gamma']
beta = data['beta']
attrs_update, feature_update = self.handle_update_shape(attrs, attrs_mod, feature, gamma, beta)
# Process the new primitives
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.flush_headers()
self.wfile.write(json.dumps({'attrs': attrs_update.tolist(), 'feature': feature_update.tolist(), 'kld': self.trainer.stats_loss_kld}).encode())
elif self.path == '/get_highres':
print('get_highres')
feature = np.array(data['feature'], dtype=np.float32)
feature = torch.from_numpy(feature)
rendered_img = self.trainer.render_express(feature)
rendered_img_png = cv2.imencode('.png', rendered_img, [cv2.IMWRITE_PNG_COMPRESSION,3])[1]
rendered_img_png_b64 = "data:image/png;base64, " + base64.b64encode(rendered_img_png).decode('ascii')
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.flush_headers()
print(len(rendered_img_png_b64))
self.wfile.write(json.dumps({'highres_png': rendered_img_png_b64}).encode())
else:
print('Unknow POST path')
def handle_update_shape(self, attrs, attrs_mod, feature, gamma, beta):
attrs_mod = torch.from_numpy(attrs_mod)
attrs_mod[:,0] = torch.log(attrs_mod[:,0])
attrs = torch.from_numpy(attrs)
attrs[:,0] = torch.log(attrs[:,0])
# Convert manipulation to objective function
mask = (torch.abs(attrs_mod-attrs) > 0.001).float()
def lossfun(x, feat):
loss_kld = torch.mean(0.5 * torch.mean(feat**2, dim=-1))
x = attrswapaxis(x)
gt = attrs_mod
mask_ = mask
gt = gt.to(x.device)
mask_ = mask_.to(x.device)
loss = torch.mean(torch.abs(x - gt)*mask_) + gamma * (torch.clamp(loss_kld, beta, None) - beta)
return loss
feature, attrs = self.trainer.step_manip(torch.from_numpy(feature), lossfun)
attrs[:,0] = torch.exp(attrs[:,0])
attrs = attrswapaxis(attrs)
return attrs.detach().cpu().numpy(), feature.detach().cpu().numpy()
def handle_get_new_shape(self, existing_feature=None):
if existing_feature is None:
num_features = self.trainer.get_known_latent(None)
feature = self.trainer.get_known_latent(np.random.choice(num_features)) # 1, 128
else:
feature = torch.from_numpy(existing_feature).to(self.trainer.device)
attrs = self.trainer.prim_attr_net(feature)
attrs = attrs.reshape( -1, 4) # 64, 256, 4, (r x y z)
attrs[:,0] = torch.exp(attrs[:,0])
attrs = attrswapaxis(attrs)
return attrs.detach().cpu().numpy(), feature.detach().cpu().numpy()
class ThreadedHTTPServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
pass
def run(server_class=http.server.HTTPServer, handler_class=http.server.BaseHTTPRequestHandler, trainer=None, port=1234):
handler = partial(handler_class, trainer)
server_address = ('0.0.0.0', port)
httpd = server_class(server_address, handler)
httpd.trainer = trainer
httpd.serve_forever()
def get_args():
# command line args
parser = argparse.ArgumentParser(
description='DualSDF Web Demo')
parser.add_argument('config', type=str,
help='The configuration file.')
# Resume:
parser.add_argument('--pretrained', default=None, type=str,
help='pretrained model checkpoint')
parser.add_argument('--port', default=1234, type=int)
args = parser.parse_args()
def dict2namespace(config):
namespace = argparse.Namespace()
for key, value in config.items():
if isinstance(value, dict):
new_value = dict2namespace(value)
else:
new_value = value
setattr(namespace, key, new_value)
return namespace
# parse config file
with open(args.config, 'r') as f:
config = yaml.load(f)
config = dict2namespace(config)
return args, config
def main(args, cfg):
torch.backends.cudnn.benchmark = True
device = torch.device('cuda:0')
trainer_lib = importlib.import_module(cfg.trainer.type)
trainer = trainer_lib.Trainer(cfg, args, device)
if args.pretrained is not None:
trainer.resume_demo(args.pretrained)
else:
trainer.resume_demo(cfg.resume.dir)
run(http.server.HTTPServer, TestHandler, trainer, port=args.port)
if __name__ == "__main__":
# command line args
args, cfg = get_args()
print("Arguments:")
print(args)
print("Configuration:")
print(cfg)
main(args, cfg)