File size: 11,730 Bytes
ce34030
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
import json
import math
import numpy as np
import lightning.pytorch as pl
from metrics.iou_cdist import IoU_cDist
from my_utils.savermixins import SaverMixin
from my_utils.refs import sem_ref, joint_ref
from dataset.utils import convert_data_range, parse_tree
from my_utils.plot import viz_graph, make_grid, add_text
from my_utils.render import draw_boxes_axiss_anim, prepare_meshes
from PIL import Image

class BaseSystem(pl.LightningModule, SaverMixin):
    def __init__(self, hparams):
        super().__init__()
        self.hparams.update(hparams)

    def setup(self, stage: str):
        # config the logger dir for images
        self.hparams.save_dir = os.path.join(self.hparams.exp_dir, 'output', stage) 
        os.makedirs(self.hparams.save_dir, exist_ok=True)
        

    # --------------------------------- visualization ---------------------------------
        
    def convert_json(self, x, c, idx, prefix=''):
        out = {"meta": {}, "diffuse_tree": []}
        
        n_nodes = c[f"{prefix}n_nodes"][idx].item()
        par = c[f"{prefix}parents"][idx].cpu().numpy().tolist()
        adj = c[f"{prefix}adj"][idx].cpu().numpy()
        np.fill_diagonal(adj, 0) # remove self-loop for the root node
        if f"{prefix}obj_cat" in c:
            out["meta"]["obj_cat"] = c[f"{prefix}obj_cat"][idx]

        # convert the data to original range
        data = convert_data_range(x.cpu().numpy())
        # parse the tree
        out["diffuse_tree"] = parse_tree(data, n_nodes, par, adj)
        return out

    # def save_val_img(self, pred, gt, cond):
    #     B = pred.shape[0]
    #     pred_imgs, gt_imgs, gt_graphs_view = [], [], []
    #     for b in range(B):
    #         print(b)
    #         # convert to humnan readable format json
    #         pred_json = self.convert_json(pred[b], cond, b)
    #         gt_json = self.convert_json(gt[b], cond, b)
    #         # visualize bbox and axis
    #         pred_meshes = prepare_meshes(pred_json)
    #         bbox_0, bbox_1, axiss = (
    #             pred_meshes["bbox_0"],
    #             pred_meshes["bbox_1"],
    #             pred_meshes["axiss"],
    #         )
    #         pred_img = draw_boxes_axiss_anim(
    #             bbox_0, bbox_1, axiss, mode="graph", resolution=128
    #         )
    #         gt_meshes = prepare_meshes(gt_json)
    #         bbox_0, bbox_1, axiss = (
    #             gt_meshes["bbox_0"],
    #             gt_meshes["bbox_1"],
    #             gt_meshes["axiss"],
    #         )
    #         gt_img = draw_boxes_axiss_anim(
    #             bbox_0, bbox_1, axiss, mode="graph", resolution=128
    #         )
    #         # visualize graph
    #         # gt_graph = viz_graph(gt_json, res=128)
    #         # gt_graph = add_text(cond["name"][b], gt_graph)
    #         # GT views
    #         rgb_view = cond["img"][b].cpu().numpy()

    #         pred_imgs.append(pred_img)
    #         gt_imgs.append(gt_img)
    #         gt_graphs_view.append(rgb_view)
    #         # gt_graphs_view.append(gt_graph)

    #     # save images for generated results
    #     epoch = str(self.current_epoch).zfill(5)
    #     # pred_thumbnails = np.concatenate(pred_imgs, axis=1)  # concat batch in width

    #     import ipdb
    #     ipdb.set_trace()
    #     # save images for ground truth
    #     for i in range(math.ceil(len(gt_graphs_view) / 8)):
    #         start = i * 8
    #         end = min((i + 1) * 8, len(gt_graphs_view))
    #         pred_thumbnails = np.concatenate(pred_imgs[start:end], axis=1)
    #         gt_graph_imgs = np.concatenate(gt_graphs_view[start:end], axis=1)
    #         gt_thumbnails = np.concatenate(gt_imgs[start:end], axis=1)  # concat batch in width
    #         grid = np.concatenate([gt_graph_imgs, gt_thumbnails, pred_thumbnails], axis=0)
            # self.save_rgb_image(f"new_out_valid_{i}.png", grid)

    def save_test_step(self, pred, gt, cond, batch_idx, res=128):
        exp_name = self._get_exp_name()
        model_name = cond["name"][0].replace("/", '@')
        save_dir = f"{exp_name}/{str(batch_idx)}@{model_name}"

        # input image
        input_img = cond["img"][0].cpu().numpy()
        # GT recordings
        if not self.hparams.get('test_no_GT', False):
            gt_json = self.convert_json(gt[0], cond, 0)
            # gt_graph = viz_graph(gt_json, res=256)
            gt_meshes = prepare_meshes(gt_json)
            bbox_0, bbox_1, axiss = (
                gt_meshes["bbox_0"],
                gt_meshes["bbox_1"],
                gt_meshes["axiss"],
            )
            gt_img = draw_boxes_axiss_anim(bbox_0, bbox_1, axiss, mode="graph", resolution=res)
        else:
            # gt_graph = 255 * np.ones((res, res, 3), dtype=np.uint8)
            gt_img = 255 * np.ones((res, 2 * res, 3), dtype=np.uint8)
        gt_block = np.concatenate([input_img, gt_img], axis=1)

        # recordings for generated results
        img_blocks = []
        for b in range(pred.shape[0]):
            pred_json = self.convert_json(pred[b], cond, 0)
            # visualize bbox and axis
            pred_meshes = prepare_meshes(pred_json)
            bbox_0, bbox_1, axiss = (
                pred_meshes["bbox_0"],
                pred_meshes["bbox_1"],
                pred_meshes["axiss"],
            )
            pred_img = draw_boxes_axiss_anim(
                bbox_0, bbox_1, axiss, mode="graph", resolution=res
            )
            img_blocks.append(pred_img)
            self.save_json(f"{save_dir}/{b}/object.json", pred_json)
        # save images for generated results
        img_grid = make_grid(img_blocks, cols=5)
        # visualize the input graph
        # input_graph = viz_graph(pred_json, res=256)

        # save images
        # self.save_rgb_image(f"{save_dir}/gt_graph.png", gt_graph)
        self.save_rgb_image(f"{save_dir}/output.png", img_grid)
        self.save_rgb_image(f"{save_dir}/gt.png", gt_block)
        # self.save_rgb_image(f"{save_dir}/input_graph.png", input_graph)

    def _save_html_end(self):
        exp_name = self._get_exp_name()
        save_dir = self.get_save_path(exp_name)
        cases = sorted(os.listdir(save_dir), key=lambda x: int(x.split("@")[0]))
        html_head = """
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Test Image Results</title>
            <style>
                table {
                    width: 100%;
                    border-collapse: collapse;
                }
                th, td {
                    border: 1px solid black;
                    padding: 8px;
                    text-align: left;
                }
                .separator {
                    border-top: 2px solid black;
                }
            </style>
        </head>
        <body>
            <table>

        """
        total = len(cases)
        each = 200
        n_pages = total // each + 1
        for p in range(n_pages):
            html_content = html_head
            for i in range(p * each, min((p + 1) * each, total)):
                case = cases[i]
                if self.hparams.get("test_no_GT", False):
                    aid_iou = rid_iou = aid_cdist = rid_cdist = aid_cd = rid_cd = aor = "N/A"              
                else:
                    with open(os.path.join(save_dir, case, "metrics.json"), "r") as f:
                        metrics = json.load(f)["avg"]
                    aid_iou = round(metrics["AS-IoU"], 4)
                    rid_iou = round(metrics["RS-IoU"], 4)
                    aid_cdist = round(metrics["AS-cDist"], 4)
                    rid_cdist = round(metrics["RS-cDist"], 4)
                    aid_cd = round(metrics["AS-CD"], 4)
                    rid_cd = round(metrics["RS-CD"], 4)
                    aor = metrics["AOR"]
                    if aor is not None:
                        aor = round(aor, 4)
                html_content += f"""
                    <tr>
                        <th>Object ID</th>
                        <th>Metrics (avg) </th>
                        <th>Input image + GT object + GT graph</th>
                        <th>Input graph </th>
                    </tr>
                    <tr>
                        <td rowspan="3">{case}</td>
                        <td> 
                        [AS-cDist] {aid_cdist}<br>
                        [RS-cDist] {rid_cdist}<br>
                        -----------------------<br>
                        [AS-IoU]  {aid_iou}<br>
                        [RS-IoU]  {rid_iou}<br>
                        -----------------------<br>
                        [RS-CD]   {rid_cd}<br> 
                        [AS-CD]   {aid_cd}<br>
                        -----------------------<br>
                        [AOR]     {aor}<br>
                        </td>
                        <td>
                            <img src="{exp_name}/{case}/gt.png" alt="GT Image" style="height: 128px; width: 3*128px;">
                            <img src="{exp_name}/{case}/gt_graph.png" alt="Graph Image" style="height: 128px; width: 3*128px;">
                        </td>
                        <td>
                            <img src="{exp_name}/{case}/input_graph.png" alt="Graph Image" style="height: 128px; width: 3*128px;">
                        </td>
                    </tr>
                    <tr><th colspan="3">Generated samples</th></tr>
                    <tr>
                        <td colspan="3"><img src="{exp_name}/{case}/output.png" alt="Generated Image" style="height: 3*128px; width: 10*128px;"></td>
                    </tr>
                    <tr class="separator"><td colspan="4"></td></tr>
                """
            html_content += """</table></body></html>"""
            outfile = self.get_save_path(f"{exp_name}_page_{p+1}.html")
            with open(outfile, "w") as file:
                file.write(html_content)

    def val_compute_metrics(self, pred, gt, cond):
        loss_dict = {}
        B = pred.shape[0]
        as_ious = 0.0
        rs_ious = 0.0
        as_cdists = 0.0
        rs_cdists = 0.0
        for b in range(B):
            gt_json = self.convert_json(gt[b], cond, b)
            pred_json = self.convert_json(pred[b], cond, b)
            scores = IoU_cDist(
                pred_json,
                gt_json,
                num_states=5,
                compare_handles=True,
                iou_include_base=True,
            )
            as_ious += scores['AS-IoU']
            rs_ious += scores['RS-IoU']
            as_cdists += scores['AS-cDist']
            rs_cdists += scores['RS-cDist']

        as_ious /= B
        rs_ious /= B
        as_cdists /= B
        rs_cdists /= B

        loss_dict['val/AS-IoU'] = as_ious
        loss_dict['val/RS-IoU'] = rs_ious
        loss_dict['val/AS-cDist'] = as_cdists
        loss_dict['val/RS-cDist'] = rs_cdists

        return loss_dict

    def _get_exp_name(self):
        which_ds = self.hparams.get("test_which", 'pm')
        is_pred_G = self.hparams.get("test_pred_G", False)
        is_label_free = self.hparams.get("test_label_free", False)
        guidance_scaler = self.hparams.get("guidance_scaler", 0)
        # config saving directory
        exp_postfix = f"_w={guidance_scaler}_{which_ds}"
        if is_pred_G:
            exp_postfix += "_pred_G"
        if is_label_free:
            exp_postfix += "_label_free"
        
        exp_name = "epoch_" + str(self.current_epoch).zfill(3) + exp_postfix
        return exp_name