-
Notifications
You must be signed in to change notification settings - Fork 11
/
infer.py
338 lines (285 loc) · 10.9 KB
/
infer.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# -----------------------------------------------------------------------
# Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
#
# This work is made available
# under the Nvidia Source Code License (1-way Commercial).
#
# Official Implementation of the CVPR2020 Paper
# Two-shot Spatially-varying BRDF and Shape Estimation
# Mark Boss, Varun Jampani, Kihwan Kim, Hendrik P. A. Lensch, Jan Kautz
# -----------------------------------------------------------------------
import argparse
import os
import sys
import time
import traceback
from typing import List, Tuple
import numpy as np
import tensorflow as tf
from tensorpack.dataflow import BatchData, PrintData
from tensorpack.utils.gpu import change_gpu
from tqdm import tqdm
from config import get_render_config
from dataflow.dataflow import InferenceDataflow, InferenceStage
from models.base import BaseNetwork
from models.brdf_network import InferenceModel as BrdfNet
from models.illumination_network import InferenceModel as IllumNet
from models.joint_network import InferenceModel as JointNet
from models.shape_network import InferenceModel as ShapeNet
from utils.config import ParameterNames, Stages
from utils.dataflow_utils import uncompressDepth, save
import utils.sg_utils as sg
from utils.common_layers import apply_mask
from utils.rendering_layer import RenderingLayer
class Predictor:
def __init__(self, compact_graph: str, model: BaseNetwork, step: InferenceStage):
self.compact_graph = compact_graph
self.model = model
self.step = step
self.inputs = [i.name for i in model.inputs()]
# list of tuples with output names and output image names
assert len(model.outputs()) == len(model.output_image_names())
self.outputs: List[Tuple[str, str]] = list(
zip(model.outputs(), model.output_image_names())
)
def __enter__(self):
self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
# Note, we just load the graph and do *not* need to initialize anything.
with tf.gfile.GFile(self.compact_graph, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def)
self.network_inputs = [
self.sess.graph.get_tensor_by_name("import/" + n + ":0")
for n in self.inputs
]
self.network_outputs = [
self.sess.graph.get_tensor_by_name("import/" + n[0] + ":0")
for n in self.outputs
]
return self
def predict_all(self, data_path: str, isSrgb: bool, isLdr: bool):
t0 = time.time()
df = InferenceDataflow(data_path, self.step, isSrgb, isLdr)
df = PrintData(df, max_list=7, max_depth=7)
df.reset_state() # Needed to setup dataflow
for dp in tqdm(df):
path, dp = dp
self.predict(dp, path)
t1 = time.time()
print("Prediction finished in: {}".format(t1 - t0))
def predict(self, dp, save_path: str):
dp = [np.expand_dims(e, 0) for e in dp]
inputDict = {name: dp[i] for i, name in enumerate(self.network_inputs)}
outputs = self.sess.run(self.network_outputs, inputDict)
if self.step == InferenceStage.JOINT:
runIdx = Stages.REFINEMENT.value
else:
runIdx = Stages.INITIAL.value
for i, o in enumerate(self.outputs):
_, fileName = o
try:
fname = fileName % runIdx
except TypeError: # String does not contain formatting
fname = fileName
filePath = os.path.join(save_path, fname)
res = outputs[i][0]
if fileName == ParameterNames.DEPTH_PRED.value:
res = uncompressDepth(res)
print(
f"Saving {res.shape} as {filePath} - Range {res.min()} to {res.max()}"
)
save(res, filePath)
def __exit__(self, exception_type, exception_value, tb):
if tb is not None:
print("__exit__", exception_type, exception_value)
print(sys.exc_info()[0])
traceback.print_tb(tb)
print("Session closing...")
self.sess.close()
tf.reset_default_graph()
def stepInference(data_path, model, step, weight_path, isSrgb: bool, isLdr: bool):
with Predictor(weight_path, model, step) as p:
p.predict_all(data_path, isSrgb, isLdr)
class Renderer:
def __init__(self, step: InferenceStage):
self.render_config = get_render_config()
assert (
step == InferenceStage.INITIAL_RENDERING
or step == InferenceStage.FINAL_RENDERING
)
self.step = step
self.stage = (
Stages.INITIAL
if step == InferenceStage.INITIAL_RENDERING
else Stages.REFINEMENT
)
def __enter__(self):
self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
return self
def render_all(self, data_path: str):
t0 = time.time()
df = InferenceDataflow(data_path, self.step)
df = PrintData(df)
df.reset_state() # Needed to setup dataflow
for dp in tqdm(df):
path, dp = dp
self.render(dp, path)
t1 = time.time()
print("Rendering finished in: {}".format(t1 - t0))
def render(self, dp, save_path: str):
# Batch everything
dp = [np.expand_dims(e, 0) for e in dp]
# Extract corresponding maps
diffuse, specular, roughness, normal, depth, mask, sgs = dp
# Setup everything for rendering
imgSize = diffuse.shape[1]
num_sgs = sgs.shape[1] # Get the number of sgs
intensity = self.render_config["light_intensity_lumen"] / (4.0 * np.pi)
light_color = np.asarray(self.render_config["light_color"])
light_color_intensity = light_color * intensity
with self.sess.as_default():
camera_pos = tf.expand_dims(
tf.convert_to_tensor(
self.render_config["camera_position"], dtype=tf.float32
),
0,
)
light_pos = tf.expand_dims(
tf.convert_to_tensor(
self.render_config["light_position"], dtype=tf.float32
),
0,
)
repeat = [1 for _ in range(len(mask.shape))]
repeat[-1] = 3
mask3 = tf.tile(mask, repeat)
axis_sharpness = tf.expand_dims(
tf.convert_to_tensor(
sg.setup_axis_sharpness(num_sgs), dtype=tf.float32
),
0,
)
# Add a batch dim
light_col = tf.convert_to_tensor(
light_color_intensity.reshape([1, 3]), dtype=tf.float32
)
sgs_joined = tf.concat([sgs, axis_sharpness], -1)
renderer = RenderingLayer(
self.render_config["field_of_view"],
self.render_config["distance_to_zero"],
tf.TensorShape([None, imgSize, imgSize, 3]),
)
rerendered = renderer.call(
diffuse,
specular,
roughness,
normal,
depth,
mask3[:, :, :, 0:1],
camera_pos,
light_pos,
light_col,
sgs_joined,
)
rerendered = apply_mask(rerendered, mask3, "masked_rerender")
result = rerendered.eval()[0]
fname = ParameterNames.RERENDER.value % self.stage.value
filePath = os.path.join(save_path, fname)
save(result, filePath)
def __exit__(self, exception_type, exception_value, tb):
if tb is not None:
print("__exit__", exception_type, exception_value)
print(sys.exc_info()[0])
traceback.print_tb(tb)
print("Session closing...")
self.sess.close()
tf.reset_default_graph()
def stepRender(data_path, step):
with Renderer(step) as r:
r.render_all(data_path)
def shapeInference(data_path, weights, isSrgb: bool, isLdr: bool):
print("\n\tPerforming Shape Inference...\n")
stepInference(data_path, ShapeNet(), InferenceStage.SHAPE, weights, isSrgb, isLdr)
def illuminationInference(data_path, weights, isSrgb: bool, isLdr: bool):
print("\n\tPerforming Illumination Inference...\n")
stepInference(
data_path, IllumNet(), InferenceStage.ILLUMINATION, weights, isSrgb, isLdr
)
def brdfInference(data_path, weights, isSrgb: bool, isLdr: bool):
print("\n\tPerforming BRDF Inference...\n")
stepInference(data_path, BrdfNet(), InferenceStage.BRDF, weights, isSrgb, isLdr)
stepRender(data_path, InferenceStage.INITIAL_RENDERING)
def jointInference(data_path, weights, isSrgb: bool, isLdr: bool):
print("\n\tPerforming Refinement Inference...\n")
stepInference(data_path, JointNet(), InferenceStage.JOINT, weights, isSrgb, isLdr)
stepRender(data_path, InferenceStage.FINAL_RENDERING)
def fullInference(
data_path,
shape_weights,
illumination_weights,
brdf_weights,
joint_weights,
isSrgb: bool,
isLdr: bool,
):
shapeInference(data_path, shape_weights, isSrgb, isLdr)
illuminationInference(data_path, illumination_weights, isSrgb, isLdr)
brdfInference(data_path, brdf_weights, isSrgb, isLdr)
jointInference(data_path, joint_weights, isSrgb, isLdr)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--data", required=True, help="Path to the inference folder.")
parser.add_argument(
"-s",
"--shape_weights",
required=True,
help="Path to the shape network weights.",
)
parser.add_argument(
"-i",
"--illumination_weights",
required=True,
help="Path to the illumination network weights.",
)
parser.add_argument(
"-b",
"--brdf_weights",
required=True,
help="Path to the brdf network weights.",
)
parser.add_argument(
"-j",
"--joint_weights",
required=True,
help="Path to the joint refinement network weights.",
)
parser.add_argument(
"--gpu",
help="Comma separated list of GPU(s) to use. -1 Runs training/inference on CPU.",
default="-1",
type=str,
)
parser.add_argument(
"--linear",
help="The images are in a linear color space. Otherwise sRGB is assumed",
action="store_true",
)
parser.add_argument(
"--hdr",
help="The images are in a HDR format such as .exr or .hdr. Otherwise LDR PNG images are assumed",
action="store_true",
)
args = parser.parse_args()
with change_gpu(args.gpu):
fullInference(
args.data,
args.shape_weights,
args.illumination_weights,
args.brdf_weights,
args.joint_weights,
not args.linear,
not args.hdr,
)
if __name__ == "__main__":
main()