Ejemplo n.º 1
0
def format_kv_recursive_flat(handler, kvs):
    if kvs is None or kvs == '':
        return '<pre>N/A</pre>'
    if not isinstance(kvs, dict):
        kvs = json.loads(kvs)
    return '<pre>' + kvformat(
        {k: v
         for k, v in dict_deep_kv(kvs) if not '__' in k}) + '</pre>'
Ejemplo n.º 2
0
def validate_epoch(epoch,
                   model,
                   val_dataloader,
                   meters,
                   meter_prefix='validation'):
    end = time.time()

    visualized = 0
    vis = HTMLTableVisualizer(args.vis_dir, 'NSCL Execution Visualization')
    vis.begin_html()

    try:
        with tqdm_pbar(total=len(val_dataloader)) as pbar:
            for feed_dict in val_dataloader:
                if args.use_gpu:
                    if not args.gpu_parallel:
                        feed_dict = async_copy_to(feed_dict, 0)

                data_time = time.time() - end
                end = time.time()

                output_dict = model(feed_dict)
                monitors = {
                    meter_prefix + '/' + k: v
                    for k, v in as_float(output_dict['monitors']).items()
                }
                step_time = time.time() - end
                end = time.time()

                n = feed_dict['image'].size(0)
                meters.update(monitors, n=n)
                meters.update({'time/data': data_time, 'time/step': step_time})

                feed_dict = GView(as_detached(as_cpu(feed_dict)))
                output_dict = GView(as_detached(as_cpu(output_dict)))

                for i in range(n):
                    with vis.table(
                            'Visualize #{} Metainfo'.format(visualized), [
                                HTMLTableColumnDesc('id', 'QID', 'text',
                                                    {'width': '50px'}),
                                HTMLTableColumnDesc('image', 'Image', 'figure',
                                                    {'width': '400px'}),
                                HTMLTableColumnDesc('qa', 'QA', 'text',
                                                    {'width': '200px'}),
                                HTMLTableColumnDesc('p', 'Program', 'code',
                                                    {'width': '200px'})
                            ]):
                        image_filename = osp.join(args.data_image_root,
                                                  feed_dict.image_filename[i])
                        image = Image.open(image_filename)
                        fig, ax = vis_bboxes(image,
                                             feed_dict.objects_raw[i],
                                             'object',
                                             add_text=False)
                        _ = ax.set_title('object bounding box annotations')
                        QA_string = """
                            <p><b>Q</b>: {}</p>
                            <p><b>A</b>: {}</p>
                        """.format(feed_dict.question_raw[i],
                                   feed_dict.answer[i])
                        P_string = '\n'.join(
                            [repr(x) for x in feed_dict.program_seq[i]])

                        vis.row(id=i, image=fig, qa=QA_string, p=P_string)
                        plt.close()

                    with vis.table(
                            'Visualize #{} Metainfo'.format(visualized), [
                                HTMLTableColumnDesc('id', 'QID', 'text',
                                                    {'width': '50px'}),
                                HTMLTableColumnDesc('image', 'Image', 'figure',
                                                    {'width': '400px'}),
                                HTMLTableColumnDesc('mask', 'Mask', 'figure',
                                                    {'width': '700px'})
                            ]):
                        image_filename = osp.join(args.data_image_root,
                                                  feed_dict.image_filename[i])
                        image = Image.open(image_filename)
                        fig, ax = vis_bboxes(image,
                                             feed_dict.objects_raw[i],
                                             'object',
                                             add_text=False)
                        _ = ax.set_title('object bounding box annotations')
                        if not args.show_mask:
                            montage = fig
                        else:
                            num_slots = output_dict['monet/m'].shape[1]
                            monet_fig = [
                                [
                                    tensor2im(output_dict['monet/m'][i, k])
                                    for k in range(num_slots)
                                ],
                                [
                                    tensor2im(output_dict['monet/x'][i, k])
                                    for k in range(num_slots)
                                ],
                                [
                                    tensor2im(output_dict['monet/xm'][i, k])
                                    for k in range(num_slots)
                                ],
                                [tensor2im(output_dict['monet/x_input'][i])] +
                                [
                                    tensor2im(output_dict['monet/x_tilde'][i])
                                    for k in range(num_slots - 1)
                                ]
                            ]
                            montage = montage_fig(monet_fig)
                        vis.row(id=i, image=fig, mask=montage)
                        plt.close()

                    with vis.table('Visualize #{} Trace'.format(visualized), [
                            HTMLTableColumnDesc('id', 'Step', 'text',
                                                {'width': '50px'}),
                            HTMLTableColumnDesc('image', 'Image', 'figure',
                                                {'width': '600px'}),
                            HTMLTableColumnDesc('p', 'operation', 'text',
                                                {'width': '200px'}),
                            HTMLTableColumnDesc('r', 'result', 'code',
                                                {'width': '200px'})
                    ]):
                        # TODO(Jiayuan Mao @ 11/20): support output_dict.programs.
                        for j, (prog, buf) in enumerate(
                                zip(feed_dict.program_seq[i],
                                    output_dict.buffers[i])):
                            if j != len(feed_dict.program_seq[i]) - 1 and (
                                    buf > 0
                            ).long().sum().item() > 0 and buf.size(
                                    0) == feed_dict.objects_raw[i].shape[0]:
                                this_objects = feed_dict.objects_raw[i][
                                    torch.nonzero(buf > 0)[:, 0].numpy()]
                                fig, ax = vis_bboxes(image,
                                                     this_objects,
                                                     'object',
                                                     add_text=False)
                            else:
                                fig, ax = vis_bboxes(image, [],
                                                     'object',
                                                     add_text=False)
                            vis.row(id=j, image=fig, p=repr(prog), r=repr(buf))
                            plt.close()

                    visualized += 1
                    if visualized > args.nr_visualize:
                        raise StopIteration()

                pbar.set_description(
                    meters.format_simple(
                        'Epoch {} (validation)'.format(epoch), {
                            k: v
                            for k, v in meters.val.items()
                            if k.startswith('validation') and k.count('/') <= 1
                        },
                        compressed=True))
                pbar.update()

                end = time.time()
    except StopIteration:
        pass

    from jacinle.utils.meta import dict_deep_kv
    from jacinle.utils.printing import kvformat
    with vis.table('Info', [
            HTMLTableColumnDesc('name', 'Name', 'code', {}),
            HTMLTableColumnDesc('info', 'KV', 'code', {})
    ]):
        vis.row(name='args', info=kvformat(args.__dict__, max_key_len=32))
        vis.row(name='configs',
                info=kvformat(dict(dict_deep_kv(configs)), max_key_len=32))
    vis.end_html()

    logger.info(
        'Happy Holiday! You can find your result at "http://monday.csail.mit.edu/xiuming'
        + osp.realpath(args.vis_dir) + '".')
Ejemplo n.º 3
0
def dumps_env(value):
    return '\n'.join(['{} = {}'.format(k, v) for k, v in dict_deep_kv(value)])
Ejemplo n.º 4
0
 def print(self, sep=': ', end='\n', file=None):
     return kvprint(dict(dict_deep_kv(self)), sep=sep, end=end, file=file)
Ejemplo n.º 5
0
 def format(self, sep=': ', end='\n'):
     return kvformat(dict(dict_deep_kv(self)), sep=sep, end=end)