def extend_data_from(task, other, force=False): """ Map the images / groundtruth from another similar task to this one >>> from clab.tasks import * >>> other = CamVid(repo=expanduser('~/sseg/SegNet')) >>> task = DivaV1(clean=2) >>> force = False """ src_fpaths = other.all_gt_paths() dst_dir = task.datasubdir('extern', other.name) dst_fpaths = [join(dst_dir, p) for p in fnameutil.dumpsafe(src_fpaths)] need_convert = True if not force: existing_fpaths = imutil.load_image_paths(dst_dir, ext='.png') existing_fpaths == dst_fpaths try: existing_fpaths = fnameutil.align_paths( dst_fpaths, existing_fpaths) except Exception: pass else: dst_fpaths = existing_fpaths need_convert = False if need_convert: from_other_label = task.convert_labels_from(other) for src_fpath, dst_fpath in ub.ProgIter( zip(src_fpaths, dst_fpaths), length=len(src_fpaths), label='converting ' + other.name): src_gt = cv2.imread(src_fpath, flags=cv2.IMREAD_UNCHANGED) dst_gt = from_other_label[src_gt] cv2.imwrite(dst_fpath, dst_gt) im_fpaths = other.all_im_paths() task.extern_train_im_paths += im_fpaths task.extern_train_gt_paths += dst_fpaths assert fnameutil.check_aligned( task.extern_train_im_paths, task.extern_train_gt_paths), ('should be aligned. unknown error')
def draw_data_overlay(task, sl=None): """ >>> from clab.tasks import * >>> import clab >>> task = DivaV1(clean=2) >>> arch = 'segnet_proper' >>> # Use external dataset to increase the amount of training data >>> tutorial_dir = './SegNet-Tutorial' >>> task.extend_data_from(clab.tasks.CamVid(tutorial_dir)) >>> task.draw_data_overlay() """ keys = task._preprocessing_keys() scenes = task.scene_ids[:] keys = keys + ['extern'] for key in ub.ProgIter(keys, label='overlay', verbose=3): scene_overlay_dir = task.datasubdir('overlay', key) if key == 'extern': # HACK im_paths = task.extern_train_im_paths gt_paths = task.extern_train_gt_paths else: im_paths, gt_paths = task._scene_data_subset(scenes, [key]) gt_paths = fnameutil.align_paths(im_paths, gt_paths) overlay_fnames = list(fnameutil.dumpsafe(im_paths)) if sl is not None: im_paths = im_paths[sl] gt_paths = gt_paths[sl] overlay_fnames = overlay_fnames[sl] prog = ub.ProgIter(zip(im_paths, gt_paths, overlay_fnames), length=len(im_paths), label='overlay key={}'.format(key)) for impath, gtpath, safename in prog: # Make a nice visualization fpath = join(scene_overlay_dir, safename) gt_img = cv2.imread(gtpath, cv2.IMREAD_UNCHANGED) im_img = cv2.imread(impath, cv2.IMREAD_UNCHANGED) gt_color = task.colorize(gt_img) gt_overlay = imutil.overlay_colorized(gt_color, im_img) cv2.imwrite(fpath, gt_overlay)
def make_dumpsafe_names(self): """ makes paths suitable for dumping into a single directory """ if self.dump_im_names is None: self.dump_im_names = list(fnameutil.dumpsafe(self.im_paths))