예제 #1
0
    def make_part_scene(task, scene, scale=1):
        """
        Slices the full scene into smaller parts that fit into the network but
        are at the original resolution (or higher).

        >>> scene = '0001'
        >>> scale = 1
        """
        if task.part_overlap < 0 or task.part_overlap >= 1:
            raise ValueError(('part overlap was {}, but it must be '
                              'in the range [0, 1)').format(task.part_overlap))

        input_shape = task.input_shape
        overlap = task.part_overlap
        keepbound = task.part_keepbound

        scene_gtfull_dpath = task.datasubdir('gt' + 'full', scene)
        scene_imfull_dpath = task.datasubdir('im' + 'full', scene)
        gt_fpaths = sorted(glob.glob(join(scene_gtfull_dpath, '*.png')))
        im_fpaths = sorted(glob.glob(join(scene_imfull_dpath, '*.png')))

        # Define the output path for this preprocessing mode
        mode = 'part-scale{}'.format(scale)
        scene_gtout_dpath = task.datasubdir('gt' + mode, scene)
        scene_imout_dpath = task.datasubdir('im' + mode, scene)
        # Start fresh. Remove existing files
        ub.delete(scene_gtout_dpath, verbose=False)
        ub.delete(scene_imout_dpath, verbose=False)
        ub.ensuredir(scene_gtout_dpath)
        ub.ensuredir(scene_imout_dpath)

        for impath, gtpath in zip(im_fpaths, gt_fpaths):
            im = cv2.imread(impath, flags=cv2.IMREAD_UNCHANGED)
            gt = cv2.imread(gtpath, flags=cv2.IMREAD_UNCHANGED)

            if scale != 1.0:
                im = imutil.imscale(im, scale, cv2.INTER_LANCZOS4)[0]
                gt = imutil.imscale(gt, scale, cv2.INTER_NEAREST)[0]

            assert gt.max() <= task.labels.max(), (
                'be careful not to change gt labels')

            fname_we = splitext(basename(impath))[0]
            sl_gen = imutil.image_slices(im.shape[0:2], input_shape, overlap,
                                         keepbound)
            for idx, rc_slice in enumerate(sl_gen):
                # encode the slice in the image name?
                fname = '{}_part{:0=4d}.png'.format(fname_we, idx)
                im_part = im[rc_slice]
                gt_part = gt[rc_slice]

                cv2.imwrite(join(scene_imout_dpath, fname), im_part)
                cv2.imwrite(join(scene_gtout_dpath, fname), gt_part)
        return scene_imout_dpath, scene_gtout_dpath
예제 #2
0
        def colorize(chan):
            norm, min_val, max_val = normalize(chan)

            domain = np.linspace(min_val, max_val)

            shape = (norm.shape[0], norm.shape[0] / 25)
            cb = colorutil.colorbar_image(domain, dpi=200, shape=shape)[:, :, 0: 3]
            sfy = norm.shape[0] / cb.shape[0]
            cb = imutil.imscale(cb, scale=(sfy, sfy))[0]

            color_chan = util.make_heatmask(norm)[:, :, 0:3]
            color_chan[np.isnan(chan)] = [[0, 0, 1]]
            blend_chan = util.overlay_colorized(color_chan, bgr, alpha=.2)[:, :, 0:3]

            blend_gt_chan = draw_instance_contours(blend_chan, gti, gtl,
                                                   thickness=2, alpha=.3)

            # Add a colorbar
            blend_chan = np.hstack([blend_chan, cb])
            blend_gt_chan = np.hstack([blend_gt_chan, cb])

            return blend_chan, blend_gt_chan
예제 #3
0
    def make_parts(prep, fullres, scale=1, clear=False):
        """
        Slices the fullres images into smaller parts that fit into the network
        but are at the original resolution (or higher).

        >>> from clab.tasks.urban_mapper_3d import *
        >>> task = UrbanMapper3D(root='~/remote/aretha/data/UrbanMapper3D', workdir='~/data/work/urban_mapper')
        >>> task.prepare_fullres_inputs()
        >>> fullres = task.fullres
        >>> datadir = ub.ensuredir((task.workdir, 'data'))
        >>> prep = Preprocessor(datadir)
        >>> scale = 1
        >>> clear = False
        >>> lowres = prep.make_parts(fullres, scale)
        """
        part_config = prep.part_config
        hashid = hashutil.hash_data(ub.repr2(part_config), hashlen=8)
        shapestr = '_'.join(list(map(str, prep.input_shape)))
        mode = 'part-scale{}-{}-{}'.format(scale, shapestr, hashid)

        parts, flag = prep._mode_new_input(mode, fullres, clear=clear)
        if flag:
            return parts

        input_shape = prep.input_shape
        overlap = part_config['overlap']
        keepbound = part_config['keepbound']

        records = list(fullres.iter_records())
        for record in ub.ProgIter(records, label='make ' + mode):
            dump_fname = basename(record['dump_fname'])

            im_shape = np.array(Image.open(record['im']).size[::-1])
            im_shape = tuple(np.floor(im_shape * scale).astype(np.int))

            # Consolodate all channels that belong to this record
            in_paths = record.get('aux').copy()
            for k in ['im', 'gt']:
                if k in record:
                    in_paths[k] = record[k]

            # Read the images for this record and resize if necessary
            in_images = {k: imutil.imread(v)
                         for k, v in in_paths.items()}  # 9% of the time
            if scale != 1.0:
                for k in in_images.keys():
                    interp = cv2.INTER_LANCZOS4 if k == 'im' else cv2.INTER_NEAREST
                    in_images[k] = imutil.imscale(in_images[k], scale,
                                                  interp)[0]

            sl_gen = imutil.image_slices(im_shape, input_shape, overlap,
                                         keepbound)
            for idx, rc_slice in enumerate(sl_gen):
                rsl, csl = rc_slice
                suffix = '_part{:0=4d}_{:0=3d}_{:0=3d}'.format(
                    idx, rsl.start, csl.start)
                fname = ub.augpath(dump_fname, suffix=suffix)

                for k, in_data in in_images.items():
                    out_data = in_data[rc_slice]
                    out_fpath = join(parts.dirs[k], fname)
                    imutil.imwrite(out_fpath, out_data)  # 84% of the time
                    parts.paths[k].append(out_fpath)

        return parts