forked from aminshabani/house_diffusion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_sample.py
378 lines (360 loc) · 16.2 KB
/
image_sample.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""
Generate a large batch of image samples from a model and save them as a large
numpy array. This can be used to produce samples for FID evaluation.
"""
import argparse
import os
import numpy as np
import torch as th
import io
import PIL.Image as Image
import drawSvg as drawsvg
import cairosvg
import imageio
from tqdm import tqdm
import matplotlib.pyplot as plt
from pytorch_fid.fid_score import calculate_fid_given_paths
from house_diffusion.rplanhg_datasets import load_rplanhg_data
from house_diffusion import dist_util, logger
from house_diffusion.script_util import (
model_and_diffusion_defaults,
create_model_and_diffusion,
add_dict_to_argparser,
args_to_dict,
update_arg_parser,
)
import webcolors
import networkx as nx
from collections import defaultdict
from shapely.geometry import Polygon
from shapely.geometry.base import geom_factory
from shapely.geos import lgeos
# import random
# th.manual_seed(0)
# random.seed(0)
# np.random.seed(0)
bin_to_int = lambda x: int("".join([str(int(i.cpu().data)) for i in x]), 2)
def bin_to_int_sample(sample, resolution=256):
sample_new = th.zeros([sample.shape[0], sample.shape[1], sample.shape[2], 2])
sample[sample<0] = 0
sample[sample>0] = 1
for i in range(sample.shape[0]):
for j in range(sample.shape[1]):
for k in range(sample.shape[2]):
sample_new[i, j, k, 0] = bin_to_int(sample[i, j, k, :8])
sample_new[i, j, k, 1] = bin_to_int(sample[i, j, k, 8:])
sample = sample_new
sample = sample/(resolution/2) - 1
return sample
def get_graph(indx, g_true, ID_COLOR, draw_graph, save_svg):
# build true graph
G_true = nx.Graph()
colors_H = []
node_size = []
edge_color = []
linewidths = []
edgecolors = []
# add nodes
for k, label in enumerate(g_true[0]):
_type = label
if _type >= 0 and _type not in [11, 12]:
G_true.add_nodes_from([(k, {'label':k})])
colors_H.append(ID_COLOR[_type])
node_size.append(1000)
edgecolors.append('blue')
linewidths.append(0.0)
# add outside node
G_true.add_nodes_from([(-1, {'label':-1})])
colors_H.append("white")
node_size.append(750)
edgecolors.append('black')
linewidths.append(3.0)
# add edges
for k, m, l in g_true[1]:
k = int(k)
l = int(l)
_type_k = g_true[0][k]
_type_l = g_true[0][l]
if m > 0 and (_type_k not in [11, 12] and _type_l not in [11, 12]):
G_true.add_edges_from([(k, l)])
edge_color.append('#D3A2C7')
elif m > 0 and (_type_k==11 or _type_l==11):
if _type_k==11:
G_true.add_edges_from([(l, -1)])
else:
G_true.add_edges_from([(k, -1)])
edge_color.append('#727171')
if draw_graph:
plt.figure()
pos = nx.nx_agraph.graphviz_layout(G_true, prog='neato')
nx.draw(G_true, pos, node_size=node_size, linewidths=linewidths, node_color=colors_H, font_size=14, font_color='white',\
font_weight='bold', edgecolors=edgecolors, width=4.0, with_labels=False)
if save_svg:
plt.savefig(f'outputs/graphs_gt/{indx}.svg')
else:
plt.savefig(f'outputs/graphs_gt/{indx}.jpg')
plt.close('all')
return G_true
def estimate_graph(indx, polys, nodes, G_gt, ID_COLOR, draw_graph, save_svg):
nodes = np.array(nodes)
G_gt = G_gt[1-th.where((G_gt == th.tensor([0,0,0], device='cuda')).all(dim=1))[0]]
G_gt = get_graph(indx, [nodes, G_gt], ID_COLOR, draw_graph, save_svg)
G_estimated = nx.Graph()
colors_H = []
node_size = []
edge_color = []
linewidths = []
edgecolors = []
edge_labels = {}
# add nodes
for k, label in enumerate(nodes):
_type = label
if _type >= 0 and _type not in [11, 12]:
G_estimated.add_nodes_from([(k, {'label':k})])
colors_H.append(ID_COLOR[_type])
node_size.append(1000)
linewidths.append(0.0)
# add outside node
G_estimated.add_nodes_from([(-1, {'label':-1})])
colors_H.append("white")
node_size.append(750)
edgecolors.append('black')
linewidths.append(3.0)
# add node-to-door connections
doors_inds = np.where((nodes == 11) | (nodes == 12))[0]
rooms_inds = np.where((nodes != 11) & (nodes != 12))[0]
doors_rooms_map = defaultdict(list)
for k in doors_inds:
for l in rooms_inds:
if k > l:
p1, p2 = polys[k], polys[l]
p1, p2 = Polygon(p1), Polygon(p2)
if not p1.is_valid:
p1 = geom_factory(lgeos.GEOSMakeValid(p1._geom))
if not p2.is_valid:
p2 = geom_factory(lgeos.GEOSMakeValid(p2._geom))
iou = p1.intersection(p2).area/ p1.union(p2).area
if iou > 0 and iou < 0.2:
doors_rooms_map[k].append((l, iou))
# draw connections
for k in doors_rooms_map.keys():
_conn = doors_rooms_map[k]
_conn = sorted(_conn, key=lambda tup: tup[1], reverse=True)
_conn_top2 = _conn[:2]
if nodes[k] != 11:
if len(_conn_top2) > 1:
l1, l2 = _conn_top2[0][0], _conn_top2[1][0]
edge_labels[(l1, l2)] = k
G_estimated.add_edges_from([(l1, l2)])
else:
if len(_conn) > 0:
l1 = _conn[0][0]
edge_labels[(-1, l1)] = k
G_estimated.add_edges_from([(-1, l1)])
# add missed edges
G_estimated_complete = G_estimated.copy()
for k, l in G_gt.edges():
if not G_estimated.has_edge(k, l):
G_estimated_complete.add_edges_from([(k, l)])
# add edges colors
colors = []
mistakes = 0
for k, l in G_estimated_complete.edges():
if G_gt.has_edge(k, l) and not G_estimated.has_edge(k, l):
colors.append('yellow')
mistakes += 1
elif G_estimated.has_edge(k, l) and not G_gt.has_edge(k, l):
colors.append('red')
mistakes += 1
elif G_estimated.has_edge(k, l) and G_gt.has_edge(k, l):
colors.append('green')
else:
print('ERR')
if draw_graph:
plt.figure()
pos = nx.nx_agraph.graphviz_layout(G_estimated_complete, prog='neato')
weights = [4 for u, v in G_estimated_complete.edges()]
nx.draw(G_estimated_complete, pos, edge_color=colors, linewidths=linewidths, edgecolors=edgecolors, node_size=node_size, node_color=colors_H, font_size=14, font_weight='bold', font_color='white', width=weights, with_labels=False)
if save_svg:
plt.savefig(f'outputs/graphs_pred/{indx}.svg')
else:
plt.savefig(f'outputs/graphs_pred/{indx}.jpg')
plt.close('all')
return mistakes
def save_samples(
sample, ext, model_kwargs,
tmp_count, num_room_types,
save_gif=False, save_edges=False,
door_indices = [11, 12, 13], ID_COLOR=None,
is_syn=False, draw_graph=False, save_svg=False):
prefix = 'syn_' if is_syn else ''
graph_errors = []
if not save_gif:
sample = sample[-1:]
for i in tqdm(range(sample.shape[1])):
resolution = 256
images = []
images2 = []
images3 = []
for k in range(sample.shape[0]):
draw = drawsvg.Drawing(resolution, resolution, displayInline=False)
draw.append(drawsvg.Rectangle(0,0,resolution,resolution, fill='black'))
draw2 = drawsvg.Drawing(resolution, resolution, displayInline=False)
draw2.append(drawsvg.Rectangle(0,0,resolution,resolution, fill='black'))
draw3 = drawsvg.Drawing(resolution, resolution, displayInline=False)
draw3.append(drawsvg.Rectangle(0,0,resolution,resolution, fill='black'))
draw_color = drawsvg.Drawing(resolution, resolution, displayInline=False)
draw_color.append(drawsvg.Rectangle(0,0,resolution,resolution, fill='white'))
polys = []
types = []
for j, point in (enumerate(sample[k][i])):
if model_kwargs[f'{prefix}src_key_padding_mask'][i][j]==1:
continue
point = point.cpu().data.numpy()
if j==0:
poly = []
if j>0 and (model_kwargs[f'{prefix}room_indices'][i, j]!=model_kwargs[f'{prefix}room_indices'][i, j-1]).any():
polys.append(poly)
types.append(c)
poly = []
pred_center = False
if pred_center:
point = point/2 + 1
point = point * resolution//2
else:
point = point/2 + 0.5
point = point * resolution
poly.append((point[0], point[1]))
c = np.argmax(model_kwargs[f'{prefix}room_types'][i][j-1].cpu().numpy())
polys.append(poly)
types.append(c)
for poly, c in zip(polys, types):
if c in door_indices or c==0:
continue
room_type = c
c = webcolors.hex_to_rgb(ID_COLOR[c])
draw_color.append(drawsvg.Lines(*np.array(poly).flatten().tolist(), close=True, fill=ID_COLOR[room_type], fill_opacity=1.0, stroke='black', stroke_width=1))
draw.append(drawsvg.Lines(*np.array(poly).flatten().tolist(), close=True, fill='black', fill_opacity=0.0, stroke=webcolors.rgb_to_hex([int(x/2) for x in c]), stroke_width=0.5*(resolution/256)))
draw2.append(drawsvg.Lines(*np.array(poly).flatten().tolist(), close=True, fill=ID_COLOR[room_type], fill_opacity=1.0, stroke=webcolors.rgb_to_hex([int(x/2) for x in c]), stroke_width=0.5*(resolution/256)))
for corner in poly:
draw.append(drawsvg.Circle(corner[0], corner[1], 2*(resolution/256), fill=ID_COLOR[room_type], fill_opacity=1.0, stroke='gray', stroke_width=0.25))
draw3.append(drawsvg.Circle(corner[0], corner[1], 2*(resolution/256), fill=ID_COLOR[room_type], fill_opacity=1.0, stroke='gray', stroke_width=0.25))
for poly, c in zip(polys, types):
if c not in door_indices:
continue
room_type = c
c = webcolors.hex_to_rgb(ID_COLOR[c])
draw_color.append(drawsvg.Lines(*np.array(poly).flatten().tolist(), close=True, fill=ID_COLOR[room_type], fill_opacity=1.0, stroke='black', stroke_width=1))
draw.append(drawsvg.Lines(*np.array(poly).flatten().tolist(), close=True, fill='black', fill_opacity=0.0, stroke=webcolors.rgb_to_hex([int(x/2) for x in c]), stroke_width=0.5*(resolution/256)))
draw2.append(drawsvg.Lines(*np.array(poly).flatten().tolist(), close=True, fill=ID_COLOR[room_type], fill_opacity=1.0, stroke=webcolors.rgb_to_hex([int(x/2) for x in c]), stroke_width=0.5*(resolution/256)))
for corner in poly:
draw.append(drawsvg.Circle(corner[0], corner[1], 2*(resolution/256), fill=ID_COLOR[room_type], fill_opacity=1.0, stroke='gray', stroke_width=0.25))
draw3.append(drawsvg.Circle(corner[0], corner[1], 2*(resolution/256), fill=ID_COLOR[room_type], fill_opacity=1.0, stroke='gray', stroke_width=0.25))
images.append(Image.open(io.BytesIO(cairosvg.svg2png(draw.asSvg()))))
images2.append(Image.open(io.BytesIO(cairosvg.svg2png(draw2.asSvg()))))
images3.append(Image.open(io.BytesIO(cairosvg.svg2png(draw3.asSvg()))))
if k==sample.shape[0]-1 or True:
if save_edges:
draw.saveSvg(f'outputs/{ext}/{tmp_count+i}_{k}_{ext}.svg')
if save_svg:
draw_color.saveSvg(f'outputs/{ext}/{tmp_count+i}c_{k}_{ext}.svg')
else:
Image.open(io.BytesIO(cairosvg.svg2png(draw_color.asSvg()))).save(f'outputs/{ext}/{tmp_count+i}c_{ext}.png')
if k==sample.shape[0]-1:
if 'graph' in model_kwargs:
graph_errors.append(estimate_graph(tmp_count+i, polys, types, model_kwargs[f'{prefix}graph'][i], ID_COLOR=ID_COLOR, draw_graph=draw_graph, save_svg=save_svg))
else:
graph_errors.append(0)
if save_gif:
imageio.mimwrite(f'outputs/gif/{tmp_count+i}.gif', images, fps=10, loop=1)
imageio.mimwrite(f'outputs/gif/{tmp_count+i}_v2.gif', images2, fps=10, loop=1)
imageio.mimwrite(f'outputs/gif/{tmp_count+i}_v3.gif', images3, fps=10, loop=1)
return graph_errors
def main():
args = create_argparser().parse_args()
update_arg_parser(args)
dist_util.setup_dist()
logger.configure()
logger.log("creating model and diffusion...")
model, diffusion = create_model_and_diffusion(
**args_to_dict(args, model_and_diffusion_defaults().keys())
)
model.load_state_dict(
dist_util.load_state_dict(args.model_path, map_location="cpu")
)
model.to(dist_util.dev())
model.eval()
errors = []
for _ in range(5):
logger.log("sampling...")
tmp_count = 0
os.makedirs('outputs/pred', exist_ok=True)
os.makedirs('outputs/gt', exist_ok=True)
os.makedirs('outputs/gif', exist_ok=True)
os.makedirs('outputs/graphs_gt', exist_ok=True)
os.makedirs('outputs/graphs_pred', exist_ok=True)
if args.dataset=='rplan':
ID_COLOR = {1: '#EE4D4D', 2: '#C67C7B', 3: '#FFD274', 4: '#BEBEBE', 5: '#BFE3E8',
6: '#7BA779', 7: '#E87A90', 8: '#FF8C69', 10: '#1F849B', 11: '#727171',
13: '#785A67', 12: '#D3A2C7'}
num_room_types = 14
data = load_rplanhg_data(
batch_size=args.batch_size,
analog_bit=args.analog_bit,
set_name=args.set_name,
target_set=args.target_set,
)
else:
print("dataset does not exist!")
assert False
graph_errors = []
while tmp_count < args.num_samples:
model_kwargs = {}
sample_fn = (
diffusion.p_sample_loop if not args.use_ddim else diffusion.ddim_sample_loop
)
data_sample, model_kwargs = next(data)
for key in model_kwargs:
model_kwargs[key] = model_kwargs[key].cuda()
sample = sample_fn(
model,
data_sample.shape,
clip_denoised=args.clip_denoised,
model_kwargs=model_kwargs,
analog_bit=args.analog_bit,
)
sample_gt = data_sample.cuda().unsqueeze(0)
sample = sample.permute([0, 1, 3, 2])
sample_gt = sample_gt.permute([0, 1, 3, 2])
if args.analog_bit:
sample_gt = bin_to_int_sample(sample_gt)
sample = bin_to_int_sample(sample)
graph_error = save_samples(sample_gt, 'gt', model_kwargs, tmp_count, num_room_types, ID_COLOR=ID_COLOR, draw_graph=args.draw_graph, save_svg=args.save_svg)
graph_error = save_samples(sample, 'pred', model_kwargs, tmp_count, num_room_types, ID_COLOR=ID_COLOR, is_syn=True, draw_graph=args.draw_graph, save_svg=args.save_svg)
graph_errors.extend(graph_error)
tmp_count+=sample_gt.shape[1]
logger.log("sampling complete")
fid_score = calculate_fid_given_paths(['outputs/gt', 'outputs/pred'], 64, 'cuda', 2048)
print(f'FID: {fid_score}')
print(f'Compatibility: {np.mean(graph_errors)}')
errors.append([fid_score, np.mean(graph_errors)])
errors = np.array(errors)
print(f'Diversity mean: {errors[:, 0].mean()} \t Diversity std: {errors[:, 0].std()}')
print(f'Compatibility mean: {errors[:, 1].mean()} \t Compatibility std: {errors[:, 1].std()}')
def create_argparser():
defaults = dict(
dataset='',
clip_denoised=True,
num_samples=10000,
batch_size=16,
use_ddim=False,
model_path="",
draw_graph=True,
save_svg=True,
)
defaults.update(model_and_diffusion_defaults())
parser = argparse.ArgumentParser()
add_dict_to_argparser(parser, defaults)
return parser
if __name__ == "__main__":
main()