Exemplo n.º 1
0
def _define_camvid_class_hierarcy(dset):
    # add extra supercategories
    # NOTE: life-conscious, and life-inanimate are disjoint in this
    # forumlation because we are restricted to a tree structure.  If
    # this changse, then we can try rencoding with multiple parents.
    extra_structure = {
        # Break down the image into things that are part of the system, and
        # things that aren't
        'background': 'root',
        'system': 'root',

        # The system is made up of environmental components and actor
        # components.
        'environment': 'system',
        'actor': 'system',

        # Break actors (things with complex movement) into subtypes
        'life-conscious': 'actor',
        'vehicle-land': 'actor',
        'actor-other': 'actor',

        # Break the environment (things with simple movement) info subtypes
        'life-inanimate': 'environment',
        'civil-structure': 'environment',
        'civil-notice': 'environment',
        'transport-way': 'environment',

        # Subclassify transport mediums
        'drive-way': 'transport-way',
        'walk-way': 'transport-way',
    }

    for child, parent in extra_structure.items():
        if child in dset.name_to_cat:
            dset.name_to_cat[child]['supercategory'] = parent
        else:
            dset.add_category(name=child, supercategory=parent)

    dset.name_to_cat['background']['supercategory'] = 'root'

    dset.name_to_cat['Sky']['supercategory'] = 'environment'

    dset.name_to_cat['Animal']['supercategory'] = 'life-conscious'
    dset.name_to_cat['Bicyclist']['supercategory'] = 'life-conscious'
    dset.name_to_cat['Pedestrian']['supercategory'] = 'life-conscious'
    dset.name_to_cat['Child']['supercategory'] = 'life-conscious'

    dset.name_to_cat['OtherMoving']['supercategory'] = 'actor-other'
    dset.name_to_cat['CartLuggagePram']['supercategory'] = 'actor-other'

    dset.name_to_cat['Car']['supercategory'] = 'vehicle-land'
    dset.name_to_cat['Train']['supercategory'] = 'vehicle-land'
    dset.name_to_cat['Truck_Bus']['supercategory'] = 'vehicle-land'
    dset.name_to_cat['SUVPickupTruck']['supercategory'] = 'vehicle-land'
    dset.name_to_cat['MotorcycleScooter']['supercategory'] = 'vehicle-land'

    dset.name_to_cat['VegetationMisc']['supercategory'] = 'life-inanimate'
    dset.name_to_cat['Tree']['supercategory'] = 'life-inanimate'

    dset.name_to_cat['Column_Pole']['supercategory'] = 'civil-structure'
    dset.name_to_cat['Fence']['supercategory'] = 'civil-structure'
    dset.name_to_cat['Wall']['supercategory'] = 'civil-structure'
    dset.name_to_cat['Building']['supercategory'] = 'civil-structure'
    dset.name_to_cat['Archway']['supercategory'] = 'civil-structure'
    dset.name_to_cat['Bridge']['supercategory'] = 'civil-structure'
    dset.name_to_cat['Tunnel']['supercategory'] = 'civil-structure'

    dset.name_to_cat['TrafficCone']['supercategory'] = 'civil-notice'
    dset.name_to_cat['TrafficLight']['supercategory'] = 'civil-notice'
    dset.name_to_cat['LaneMkgsDriv']['supercategory'] = 'civil-notice'
    dset.name_to_cat['LaneMkgsNonDriv']['supercategory'] = 'civil-notice'
    dset.name_to_cat['SignSymbol']['supercategory'] = 'civil-notice'
    dset.name_to_cat['ParkingBlock']['supercategory'] = 'civil-notice'
    dset.name_to_cat['Misc_Text']['supercategory'] = 'civil-notice'

    dset.name_to_cat['Road']['supercategory'] = 'drive-way'
    dset.name_to_cat['RoadShoulder']['supercategory'] = 'drive-way'
    dset.name_to_cat['Sidewalk']['supercategory'] = 'walk-way'

    for cat in list(dset.cats.values()):
        parent = cat.get('supercategory', None)
        if parent is not None:
            if parent not in dset.name_to_cat:
                print('Missing parent = {!r}'.format(parent))
                dset.add_category(name=parent, supercategory=parent)

    if 0:
        graph = dset.category_graph()
        import graphid
        graphid.util.show_nx(graph)

    # Add in some hierarcy information
    if 0:
        for x in dset.name_to_cat:
            print(
                "dset.name_to_cat[{!r}]['supercategory'] = 'object'".format(x))

    if 0:
        example_cat_aids = []
        for cat in dset.cats.values():
            cname = cat['name']
            aids = dset.index.cid_to_aids[dset.name_to_cat[cname]['id']]
            if len(aids):
                aid = ub.peek(aids)
                example_cat_aids.append(aid)
            else:
                print('No examples of cat = {!r}'.format(cat))

        import xdev
        import kwplot
        kwplot.autompl()
        for aid in xdev.InteractiveIter(example_cat_aids):
            print('aid = {!r}'.format(aid))
            ann = dset.anns[aid]
            cat = dset.cats[ann['category_id']]
            print('cat = {!r}'.format(cat))
            dset.show_image(aid=aid)
            xdev.InteractiveIter.draw()

        if 0:
            cname = 'CartLuggagePram'
            cname = 'ParkingBlock'
            cname = 'LaneMkgsDriv'
            aids = dset.index.cid_to_aids[dset.name_to_cat[cname]['id']]
            if len(aids):
                aid = ub.peek(aids)
                print('aid = {!r}'.format(aid))
                ann = dset.anns[aid]
                cat = dset.cats[ann['category_id']]
                print('cat = {!r}'.format(cat))
                dset.show_image(aid=aid)
Exemplo n.º 2
0
    def main(cls, cmdline=True, **kw):
        """
        TODO:
            - [ ] Visualize auxiliary data

        Example:
            >>> # xdoctest: +SKIP
            >>> kw = {'src': 'special:shapes8'}
            >>> cmdline = False
            >>> cls = CocoShowCLI
            >>> cls.main(cmdline, **kw)
        """
        import kwcoco
        import kwimage
        import kwplot

        config = cls.CLIConfig(kw, cmdline=cmdline)
        print('config = {}'.format(ub.repr2(dict(config), nl=1)))

        if config['src'] is None:
            raise Exception('must specify source: {}'.format(config['src']))

        dset = kwcoco.CocoDataset.coerce(config['src'])
        print('dset.fpath = {!r}'.format(dset.fpath))

        plt = kwplot.autoplt()

        gid = config['gid']
        aid = config['aid']

        out_fpath = config['dst']

        if gid is None and aid is None:
            gid = ub.peek(dset.imgs)

        if config['mode'] == 'matplotlib':
            show_kw = {
                'show_annots': config['show_annots'],
                'channels': config['channels'],
            }
            if config['show_annots'] == 'both':
                show_kw.pop('show_annots')
                show_kw['title'] = ''
                ax = dset.show_image(gid=gid,
                                     aid=aid,
                                     pnum=(1, 2, 1),
                                     show_annots=False,
                                     **show_kw)
                ax = dset.show_image(gid=gid,
                                     aid=aid,
                                     pnum=(1, 2, 2),
                                     show_annots=True,
                                     **show_kw)
            else:
                ax = dset.show_image(gid=gid, aid=aid, **show_kw)
            if out_fpath is None:

                if 1:
                    try:
                        import xdev
                    except Exception:
                        pass
                    else:
                        gids = [gid] + list(set(dset.imgs.keys()) - {gid})
                        for gid in xdev.InteractiveIter(gids):
                            ax = dset.show_image(gid=gid, aid=aid, **show_kw)
                            xdev.InteractiveIter.draw()
                            plt.show(block=False)
                plt.show()
            else:
                ax.figure.savefig(out_fpath)
        elif config['mode'] == 'opencv':
            canvas = dset.draw_image(gid, channels=config['channels'])
            if out_fpath is None:
                kwplot.imshow(canvas)
                plt.show()
            else:
                kwimage.imwrite(out_fpath, canvas)

        else:
            raise KeyError(config['mode'])
Exemplo n.º 3
0
def convert_camvid_raw_to_coco(camvid_raw_info):
    """
    Converts the raw camvid format to an MSCOCO based format, ( which lets use
    use kwcoco's COCO backend).

    Example:
        >>> # xdoctest: +REQUIRES(--download)
        >>> camvid_raw_info = grab_raw_camvid()
        >>> # test with a reduced set of data
        >>> del camvid_raw_info['img_paths'][2:]
        >>> del camvid_raw_info['mask_paths'][2:]
        >>> dset = convert_camvid_raw_to_coco(camvid_raw_info)
        >>> # xdoctest: +REQUIRES(--show)
        >>> import kwplot
        >>> plt = kwplot.autoplt()
        >>> kwplot.figure(fnum=1, pnum=(1, 2, 1))
        >>> dset.show_image(gid=1)
        >>> kwplot.figure(fnum=1, pnum=(1, 2, 2))
        >>> dset.show_image(gid=2)
    """
    import re
    import kwimage
    import kwcoco
    print('Converting CamVid to MS-COCO format')

    dset_root, img_paths, label_path, mask_paths = ub.take(
        camvid_raw_info,
        'dset_root, img_paths, label_path, mask_paths'.split(', '))

    img_infos = {
        'img_fname': img_paths,
        'mask_fname': mask_paths,
    }
    keys = list(img_infos.keys())
    next_vals = list(zip(*img_infos.values()))
    image_items = [{k: v for k, v in zip(keys, vals)} for vals in next_vals]

    dataset = {
        'img_root': dset_root,
        'images': [],
        'categories': [],
        'annotations': [],
    }

    lines = ub.readfrom(label_path).split('\n')
    lines = [line for line in lines if line]
    for line in lines:
        color_text, name = re.split('\t+', line)
        r, g, b = map(int, color_text.split(' '))
        color = (r, g, b)

        # Parse the special camvid format
        cid = (r << 16) + (g << 8) + (b << 0)
        cat = {
            'id': cid,
            'name': name,
            'color': color,
        }
        dataset['categories'].append(cat)

    for gid, img_item in enumerate(image_items, start=1):
        img = {
            'id': gid,
            'file_name': img_item['img_fname'],
            # nonstandard image field
            'segmentation': img_item['mask_fname'],
        }
        dataset['images'].append(img)

    dset = kwcoco.CocoDataset(dataset)
    dset.rename_categories({'Void': 'background'})

    assert dset.name_to_cat['background']['id'] == 0
    dset.name_to_cat['background'].setdefault('alias', []).append('Void')

    if False:
        _define_camvid_class_hierarcy(dset)

    if 1:
        # TODO: Binarize CCs (and efficiently encode if possible)
        import numpy as np

        bad_info = []
        once = False

        # Add images
        dset.remove_annotations(list(dset.index.anns.keys()))
        for gid, img in ub.ProgIter(dset.imgs.items(),
                                    desc='parse label masks'):
            mask_fpath = join(dset_root, img['segmentation'])

            rgb_mask = kwimage.imread(mask_fpath, space='rgb')
            r, g, b = rgb_mask.T.astype(np.int64)
            cid_mask = np.ascontiguousarray(rgb_to_cid(r, g, b).T)

            cids = set(np.unique(cid_mask)) - {0}

            for cid in cids:
                if cid not in dset.cats:
                    if gid == 618:
                        # Handle a known issue with image 618
                        c_mask = (cid == cid_mask).astype(np.uint8)
                        total_bad = c_mask.sum()
                        if total_bad < 32:
                            if not once:
                                print(
                                    'gid 618 has a few known bad pixels, ignoring them'
                                )
                                once = True
                            continue
                        else:
                            raise Exception('more bad pixels than expected')
                    else:
                        raise Exception(
                            'UNKNOWN cid = {!r} in gid={!r}'.format(cid, gid))

                    # bad_rgb = cid_to_rgb(cid)
                    # print('bad_rgb = {!r}'.format(bad_rgb))
                    # print('WARNING UNKNOWN cid = {!r} in gid={!r}'.format(cid, gid))
                    # bad_info.append({
                    #     'gid': gid,
                    #     'cid': cid,
                    # })
                else:
                    ann = {
                        'category_id': cid,
                        'image_id': gid
                        # 'segmentation': mask.to_coco()
                    }
                    assert cid in dset.cats
                    c_mask = (cid == cid_mask).astype(np.uint8)
                    mask = kwimage.Mask(c_mask, 'c_mask')

                    box = kwimage.Boxes([mask.get_xywh()], 'xywh')
                    # box = mask.to_boxes()

                    ann['bbox'] = ub.peek(box.to_coco())
                    ann['segmentation'] = mask.to_coco()
                    dset.add_annotation(**ann)

        if 0:
            bad_cids = [i['cid'] for i in bad_info]
            print(sorted([c['color'] for c in dataset['categories']]))
            print(sorted(set([cid_to_rgb(i['cid']) for i in bad_info])))

            gid = 618
            img = dset.imgs[gid]
            mask_fpath = join(dset_root, img['segmentation'])
            rgb_mask = kwimage.imread(mask_fpath, space='rgb')
            r, g, b = rgb_mask.T.astype(np.int64)
            cid_mask = np.ascontiguousarray(rgb_to_cid(r, g, b).T)
            cid_hist = ub.dict_hist(cid_mask.ravel())

            bad_cid_hist = {}
            for cid in bad_cids:
                bad_cid_hist[cid] = cid_hist.pop(cid)

            import kwplot
            kwplot.autompl()
            kwplot.imshow(rgb_mask)

    if 0:
        import kwplot
        plt = kwplot.autoplt()
        plt.clf()
        dset.show_image(1)

        import xdev
        gid_list = list(dset.imgs)
        for gid in xdev.InteractiveIter(gid_list):
            dset.show_image(gid)
            xdev.InteractiveIter.draw()

    dset._build_index()
    dset._build_hashid()
    return dset
Exemplo n.º 4
0
def main():
    # Regress a 3D surface as in https://stackoverflow.com/a/53151677/887074

    # Mapping from 2D coordinates to the elevation in the 3rd dimension
    import netharn as nh
    import numpy as np
    import torch
    import ubelt as ub

    num_train = 100

    TRAIN_SURFACE = 'rosenbrock'
    if TRAIN_SURFACE == 'random':
        train_points = torch.rand(num_train, 3)
        train_XY = train_points[:, 0:2]
        train_X = train_points[:, 0:1]
        train_Y = train_points[:, 1:2]
        train_Z = train_points[:, 2:3]
    elif TRAIN_SURFACE == 'rosenbrock':
        # Train with the Rosenbrock function
        # https://en.wikipedia.org/wiki/Rosenbrock_function
        train_points = torch.rand(num_train, 2)
        train_XY = train_points[:, 0:2]
        train_X = train_points[:, 0:1]
        train_Y = train_points[:, 1:2]

        a = 1
        b = 100
        train_Z = (a - train_X)**2 + b * (train_Y - train_X**2)**2 + 2

    np_train_X = train_X.data.cpu().numpy()
    np_train_Y = train_Y.data.cpu().numpy()
    np_train_Z = train_Z.data.cpu().numpy()

    test_resolution = 100
    xbasis = np.linspace(0, 1, test_resolution).astype(np.float32)
    ybasis = np.linspace(0, 1, test_resolution).astype(np.float32)
    X, Y = np.meshgrid(xbasis, ybasis)

    test_X = X.ravel()[:, None]
    test_Y = Y.ravel()[:, None]
    test_XY = np.concatenate([test_X, test_Y], axis=1)
    test_XY = torch.from_numpy(test_XY)

    import kwplot
    plt = kwplot.autoplt()
    from mpl_toolkits.mplot3d import Axes3D  # NOQA
    ax = plt.gca(projection='3d')
    ax.cla()

    # Plot the training data
    train_data_pc = ax.scatter3D(np_train_X,
                                 np_train_Y,
                                 np_train_Z,
                                 color='red')

    model = nh.layers.MultiLayerPerceptronNd(dim=0,
                                             in_channels=2,
                                             hidden_channels=[100] * 10,
                                             out_channels=1,
                                             bias=False)

    optim = torch.optim.Adam(model.parameters(), lr=1e-2)
    max_iters = 100

    if 0:
        iter_ = ub.ProgIter(range(max_iters), desc='iterate')
    else:
        import xdev
        iter_ = xdev.InteractiveIter(list(range(max_iters)))

    poly3d_pc = None
    for iter_idx in iter_:
        optim.zero_grad()

        pred_Z = model.forward(train_XY)
        loss = torch.nn.functional.mse_loss(pred_Z, train_Z)

        loss.backward()
        optim.step()

        test_Z = model.forward(test_XY).data.cpu().numpy()

        param_total = sum(p.sum() for p in model.parameters())
        print('param_total = {!r}'.format(param_total))

        if hasattr(iter_, 'draw'):
            num = test_X.shape[0]
            s = np.sqrt(num)
            assert s % 1 == 0
            s = int(s)
            X = test_X.reshape(s, s)
            Y = test_Y.reshape(s, s)
            Z = test_Z.reshape(s, s)
            if poly3d_pc is not None:
                # Remove previous surface
                poly3d_pc.remove()
            poly3d_pc = ax.plot_surface(X, Y, Z, color='blue')
            iter_.draw()