def main(): args = parse_args() cityscapes_path = args.cityscapes_path out_dir = args.out_dir if args.out_dir else cityscapes_path mmcv.mkdir_or_exist(out_dir) gt_dir = osp.join(cityscapes_path, args.gt_dir) poly_files = [] for poly in mmcv.scandir(gt_dir, '_polygons.json', recursive=True): poly_file = osp.join(gt_dir, poly) poly_files.append(poly_file) if args.nproc > 1: mmcv.track_parallel_progress(convert_json_to_label, poly_files, args.nproc) else: mmcv.track_progress(convert_json_to_label, poly_files) split_names = ['train', 'val', 'test'] for split in split_names: filenames = [] for poly in mmcv.scandir( osp.join(gt_dir, split), '_polygons.json', recursive=True): filenames.append(poly.replace('_gtFine_polygons.json', '')) with open(osp.join(out_dir, f'{split}.txt'), 'w') as f: f.writelines(f + '\n' for f in filenames)
def test_scandir(): folder = osp.join(osp.dirname(osp.dirname(__file__)), 'data/for_scan') filenames = ['a.bin', '1.txt', '2.txt', '1.json', '2.json'] assert set(mmcv.scandir(folder)) == set(filenames) assert set(mmcv.scandir(Path(folder))) == set(filenames) assert set(mmcv.scandir(folder, '.txt')) == set( [filename for filename in filenames if filename.endswith('.txt')]) assert set(mmcv.scandir(folder, ('.json', '.txt'))) == set([ filename for filename in filenames if filename.endswith(('.txt', '.json')) ]) assert set(mmcv.scandir(folder, '.png')) == set() # path of sep is `\\` in windows but `/` in linux, so osp.join should be # used to join string for compatibility filenames_recursive = [ 'a.bin', '1.txt', '2.txt', '1.json', '2.json', osp.join('sub', '1.json'), osp.join('sub', '1.txt'), '.file' ] # .file starts with '.' and is a file so it will not be scanned assert set(mmcv.scandir(folder, recursive=True)) == set( [filename for filename in filenames_recursive if filename != '.file']) assert set(mmcv.scandir(Path(folder), recursive=True)) == set( [filename for filename in filenames_recursive if filename != '.file']) assert set(mmcv.scandir(folder, '.txt', recursive=True)) == set([ filename for filename in filenames_recursive if filename.endswith('.txt') ]) with pytest.raises(TypeError): list(mmcv.scandir(123)) with pytest.raises(TypeError): list(mmcv.scandir(folder, 111))
def evaluate(pred_root, gt_root, trimap_root, verbose, nproc): """Evaluate test results of Adobe composition-1k dataset. There are 50 different ground truth foregrounds and alpha mattes pairs, each of the foreground will be composited with 20 different backgrounds, producing 1000 images for testing. In some repo, the ground truth alpha matte will be copied 20 times and named the same as the images. This function accept both original alpha matte folder (contains 50 ground truth alpha mattes) and copied alpha matte folder (contains 1000 ground truth alpha mattes) for `gt_root`. Example of copied name: ``` alpha_matte1.png -> alpha_matte1_0.png alpha_matte1_1.png ... alpha_matte1_19.png alpha_matte1_20.png ``` Args: pred_root (str): Path to the predicted alpha matte folder. gt_root (str): Path to the ground truth alpha matte folder. trimap_root (str): Path to the predicted alpha matte folder. verbose (bool): Whether print result for each predicted alpha matte. nproc (int): number of processers. """ images = sorted(mmcv.scandir(pred_root)) gt_files_num = len(list(mmcv.scandir(gt_root))) # If ground truth alpha mattes are not copied (number of files is 50), we # use the below pattern to recover the name of the original alpha matte. if gt_files_num == 50: pattern = re.compile(r'(.+)_(?:\d+)(.png)') pairs = [] for img in images: pred_alpha_path = osp.join(pred_root, img) # if ground truth alpha matte are not copied, recover the original name if gt_files_num == 50: groups = pattern.match(img).groups() alpha_path = osp.join(gt_root, ''.join(groups)) # if ground truth alpha matte are copied, the name should be the same else: # gt_files_num == 1000 alpha_path = osp.join(gt_root, img) trimap_path = (osp.join(trimap_root, img) if trimap_root is not None else None) pairs.append((pred_alpha_path, alpha_path, trimap_path)) results = mmcv.track_parallel_progress(evaluate_one, pairs, nproc) if verbose: # for sad_result, mse_result, grad_result, conn_result in results: for i, img in enumerate(images): sad_result, mse_result, grad_result, conn_result = results[i] print(f'{img} SAD: {sad_result:.6g} MSE: {mse_result:.6g} ' f'GRAD: {grad_result:.6g} CONN: {conn_result:.6g}') sad_mean, mse_mean, grad_mean, conn_mean = np.mean(results, axis=0) print(f'MEAN: SAD: {sad_mean:.6g} MSE: {mse_mean:.6g} ' f'GRAD: {grad_mean:.6g} CONN: {conn_mean:.6g}')
def test_scandir(): folder = osp.join(osp.dirname(osp.dirname(__file__)), 'data/for_scan') filenames = ['a.bin', '1.txt', '2.txt', '1.json', '2.json'] assert set(mmcv.scandir(folder)) == set(filenames) assert set(mmcv.scandir(Path(folder))) == set(filenames) assert set(mmcv.scandir(folder, '.txt')) == set( [filename for filename in filenames if filename.endswith('.txt')]) assert set(mmcv.scandir(folder, ('.json', '.txt'))) == set([ filename for filename in filenames if filename.endswith(('.txt', '.json')) ]) assert set(mmcv.scandir(folder, '.png')) == set() filenames_recursive = [ 'a.bin', '1.txt', '2.txt', '1.json', '2.json', 'sub/1.json', 'sub/1.txt' ] assert set(mmcv.scandir(folder, recursive=True)) == set(filenames_recursive) assert set(mmcv.scandir(Path(folder), recursive=True)) == set(filenames_recursive) assert set(mmcv.scandir(folder, '.txt', recursive=True)) == set([ filename for filename in filenames_recursive if filename.endswith('.txt') ]) with pytest.raises(TypeError): list(mmcv.scandir(123)) with pytest.raises(TypeError): list(mmcv.scandir(folder, 111))
def paired_paths_from_folder(folders, keys, filename_tmpl): """Generate paired paths from folders. Args: folders (list[str]): A list of folder path. The order of list should be [input_folder, gt_folder]. keys (list[str]): A list of keys identifying folders. The order should be in consistent with folders, e.g., ['lq', 'gt']. filename_tmpl (str): Template for each filename. Note that the template excludes the file extension. Usually the filename_tmpl is for files in the input folder. Returns: list[str]: Returned path list. """ assert len(folders) == 2, ( 'The len of folders should be 2 with [input_folder, gt_folder]. ' f'But got {len(folders)}') assert len(keys) == 2, ( 'The len of keys should be 2 with [input_key, gt_key]. ' f'But got {len(keys)}') input_folder, gt_folder = folders input_key, gt_key = keys input_paths = list(mmcv.scandir(input_folder)) gt_paths = list(mmcv.scandir(gt_folder)) assert len(input_paths) == len(gt_paths), ( f'{input_key} and {gt_key} datasets have different number of images: ' f'{len(input_paths)}, {len(gt_paths)}.') paths = [] progress = 0 total = len(gt_paths) for gt_path in gt_paths: basename, ext = osp.splitext(osp.basename(gt_path)) input_name = f'{filename_tmpl.format(basename)}{ext}' input_path = osp.join(input_folder, input_name) assert input_name in input_paths, (f'{input_name} is not in ' f'{input_key}_paths.') gt_path = osp.join(gt_folder, gt_path) paths.append( dict([(f'{input_key}_path', input_path), (f'{gt_key}_path', gt_path)])) progress += 1 if progress % 5000 == 0: print('Path creation progress: {} / {}'.format(progress, total)) return paths
def extract_subimages(opt): """Crop images to subimages. Args: opt (dict): Configuration dict. It contains: input_folder (str): Path to the input folder. save_folder (str): Path to save folder. n_thread (int): Thread number. """ input_folder = opt['input_folder'] save_folder = opt['save_folder'] if not osp.exists(save_folder): os.makedirs(save_folder) print(f'mkdir {save_folder} ...') else: print(f'Folder {save_folder} already exists. Exit.') sys.exit(1) img_list = list(mmcv.scandir(input_folder)) img_list = [osp.join(input_folder, v) for v in img_list] prog_bar = mmcv.ProgressBar(len(img_list)) pool = Pool(opt['n_thread']) for path in img_list: pool.apply_async(worker, args=(path, opt), callback=lambda arg: prog_bar.update()) pool.close() pool.join() print('All processes done.')
def read_img_seq(path, require_mod_crop=False, scale=1): """Read a sequence of images from a given folder path. Args: path (list[str] | str): List of image paths or image folder path. require_mod_crop (bool): Require mod crop for each image. Default: False. scale (int): Scale factor for mod_crop. Default: 1. Returns: Tensor: size (t, c, h, w), RGB, [0, 1]. """ if isinstance(path, list): img_paths = path else: img_paths = sorted([osp.join(path, v) for v in mmcv.scandir(path)]) imgs = [mmcv.imread(v).astype(np.float32) / 255. for v in img_paths] # imgs = [] # for v in img_paths: # imm = mmcv.imread(v).astype(np.float32) / 255. # w = v[:-4]+'_40.png' # imm_3d = mmcv.imread(w).astype(np.float32) / 255. # imm_concat_3d = np.concatenate((imm,imm_3d),axis=2) # print(imm_concat_3d.shape) # print(imm_3d.shape) # imgs.append(imm_concat_3d) if require_mod_crop: imgs = [mod_crop(img, scale) for img in imgs] imgs = totensor(imgs, bgr2rgb=True, float32=True) imgs = torch.stack(imgs, dim=0) return imgs
def load_annotations(self): """Load annotations.""" # recursively find all of the valid images from imgs_root imgs_list = mmcv.scandir(self.imgs_root, self._VALID_IMG_SUFFIX, recursive=True) self.imgs_list = [osp.join(self.imgs_root, x) for x in imgs_list]
def unzip(zip_path): """Unzip zip files. It will scan all zip files in zip_path and return unzip folder names. Args: zip_path (str): Path for zip files. Returns: list: unzip folder names. """ zip_files = mmcv.scandir(zip_path, suffix='zip', recursive=False) import zipfile import shutil unzip_folders = [] for zip_file in zip_files: zip_file = osp.join(zip_path, zip_file) unzip_folder = zip_file.replace('.zip', '').split('_part')[0] print(f'Unzip {zip_file} to {unzip_folder}') with zipfile.ZipFile(zip_file, 'r') as zip_ref: zip_ref.extractall(unzip_folder) data_name = osp.basename(unzip_folder) data_type = data_name.split('_')[0] # if data path like `train_sharp/train/train_sharp/*` # begin reorganizing to `train_sharp/*` if osp.isdir(osp.join(unzip_folder, data_type, data_name)): data_folder = osp.join(unzip_folder, data_type, data_name) for i in os.listdir(data_folder): shutil.move(osp.join(data_folder, i), unzip_folder) shutil.rmtree(osp.join(unzip_folder, data_type)) # end reorganizing unzip_folders.append(unzip_folder) return unzip_folders
def __init__(self, bg_dir, io_backend='disk', flag='color', **kwargs): self.bg_dir = bg_dir self.bg_list = list(mmcv.scandir(bg_dir)) self.io_backend = io_backend self.flag = flag self.kwargs = kwargs self.file_client = None
def _get_file_list(fg_dirs, alpha_dirs): all_fg_list = list() all_alpha_list = list() for fg_dir, alpha_dir in zip(fg_dirs, alpha_dirs): fg_list = sorted(mmcv.scandir(fg_dir)) alpha_list = sorted(mmcv.scandir(alpha_dir)) # we assume the file names for fg and alpha are the same assert len(fg_list) == len(alpha_list), ( f'{fg_dir} and {alpha_dir} should have the same number of ' f'images ({len(fg_list)} differs from ({len(alpha_list)})') fg_list = [osp.join(fg_dir, fg) for fg in fg_list] alpha_list = [osp.join(alpha_dir, alpha) for alpha in alpha_list] all_fg_list.extend(fg_list) all_alpha_list.extend(alpha_list) return all_fg_list, all_alpha_list
def __init__(self, opt): super(SingleImageDataset, self).__init__() self.opt = opt # file client (io backend) self.file_client = None self.io_backend_opt = opt['io_backend'] self.mean = opt['mean'] if 'mean' in opt else None self.std = opt['std'] if 'std' in opt else None self.lq_folder = opt['dataroot_lq'] if self.io_backend_opt['type'] == 'lmdb': self.io_backend_opt['db_paths'] = [self.lq_folder] self.io_backend_opt['client_keys'] = ['lq'] self.paths = single_paths_from_lmdb([self.lq_folder], ['lq']) elif 'meta_info_file' in self.opt and self.opt[ 'meta_info_file'] is not None: with open(self.opt['meta_info_file'], 'r') as fin: self.paths = [ osp.join(self.lq_folder, line.split(' ')[0]) for line in fin ] else: self.paths = [ osp.join(self.lq_folder, v) for v in mmcv.scandir(self.lq_folder) ]
def main(): """Calculate PSNR and SSIM for images. Configurations: folder_gt (str): Path to gt (Ground-Truth). folder_restored (str): Path to restored images. crop_border (int): Crop border for each side. suffix (str): Suffix for restored images. test_y_channel (bool): If True, test Y channel (In MatLab YCbCr format) If False, test RGB channels. """ # Configurations # ------------------------------------------------------------------------- folder_gt = 'datasets/val_set14/Set14' folder_restored = 'results/exp/visualization/val_set14' crop_border = 4 suffix = '_expname' test_y_channel = False # ------------------------------------------------------------------------- psnr_all = [] ssim_all = [] img_list = sorted(mmcv.scandir(folder_gt, recursive=True)) if test_y_channel: print('Testing Y channel.') else: print('Testing RGB channels.') for i, img_path in enumerate(img_list): basename, ext = osp.splitext(osp.basename(img_path)) img_gt = mmcv.imread( osp.join(folder_gt, img_path), flag='unchanged').astype( np.float32) / 255. img_restored = mmcv.imread( osp.join(folder_restored, basename + suffix + ext), flag='unchanged').astype(np.float32) / 255. if test_y_channel and img_gt.ndim == 3 and img_gt.shape[2] == 3: img_gt = mmcv.bgr2ycbcr(img_gt, y_only=True) img_restored = mmcv.bgr2ycbcr(img_restored, y_only=True) # calculate PSNR and SSIM psnr = calculate_psnr( img_gt * 255, img_restored * 255, crop_border=crop_border, input_order='HWC') ssim = calculate_ssim( img_gt * 255, img_restored * 255, crop_border=crop_border, input_order='HWC') print(f'{i+1:3d}: {basename:25}. \tPSNR: {psnr:.6f} dB, ' f'\tSSIM: {ssim:.6f}') psnr_all.append(psnr) ssim_all.append(ssim) print(f'Average: PSNR: {sum(psnr_all) / len(psnr_all):.6f} dB, ' f'SSIM: {sum(ssim_all) / len(ssim_all):.6f}')
def _evaluate_cityscapes(self, results, logger, imgfile_prefix): """Evaluation in Cityscapes protocol. Args: results (list): Testing results of the dataset. logger (logging.Logger | str | None): Logger used for printing related information during evaluation. Default: None. imgfile_prefix (str | None): The prefix of output image file Returns: dict[str: float]: Cityscapes evaluation results. """ try: import cityscapesscripts.evaluation.evalPixelLevelSemanticLabeling as CSEval # noqa except ImportError: raise ImportError('Please run "pip install cityscapesscripts" to ' 'install cityscapesscripts first.') msg = 'Evaluating in Cityscapes style' if logger is None: msg = '\n' + msg print_log(msg, logger=logger) result_files, tmp_dir = self.format_results(results, imgfile_prefix) print("result_files", result_files) if tmp_dir is None: result_dir = imgfile_prefix else: result_dir = tmp_dir.name eval_results = dict() print_log(f'Evaluating results under {result_dir} ...', logger=logger) CSEval.args.evalInstLevelScore = True CSEval.args.predictionPath = osp.abspath(result_dir) CSEval.args.evalPixelAccuracy = True CSEval.args.JSONOutput = False seg_map_list = [] pred_list = [] # when evaluating with official cityscapesscripts, # **_gtFine_labelIds.png is used for seg_map in mmcv.scandir(self.ann_dir, 'gtCoarse_labelIds.png', recursive=True): seg_map_list.append(osp.join(self.ann_dir, seg_map)) pred_list.append(CSEval.getPrediction(CSEval.args, seg_map)) eval_results.update( CSEval.evaluateImgLists(pred_list, seg_map_list, CSEval.args)) if tmp_dir is not None: tmp_dir.cleanup() return eval_results
def load_annotations(self): """Load annotations.""" # recursively find all of the valid images from imgs_root imgs_list = mmcv.scandir( self.imgs_root, self._VALID_IMG_SUFFIX, recursive=True) self.imgs_list = [osp.join(self.imgs_root, x) for x in imgs_list] if self.len_per_stage > 0: self.concat_imgs_list_to(self.len_per_stage) self.samples_per_gpu = self.gpu_samples_per_scale.get( str(self._actual_curr_scale), self.gpu_samples_base)
def main(): args = parse_args() devkit_path = args.devkit_path aug_path = args.aug_path nproc = args.nproc if args.out_dir is None: out_dir = osp.join(devkit_path, 'VOC2012', 'SegmentationClassAug') else: out_dir = args.out_dir mmcv.mkdir_or_exist(out_dir) in_dir = osp.join(aug_path, 'dataset', 'cls') mmcv.track_parallel_progress(partial(convert_mat, in_dir=in_dir, out_dir=out_dir), list(mmcv.scandir(in_dir, suffix='.mat')), nproc=nproc) full_aug_list = [] with open(osp.join(aug_path, 'dataset', 'train.txt')) as f: full_aug_list += [line.strip() for line in f] with open(osp.join(aug_path, 'dataset', 'val.txt')) as f: full_aug_list += [line.strip() for line in f] with open( osp.join(devkit_path, 'VOC2012/ImageSets/Segmentation', 'train.txt')) as f: ori_train_list = [line.strip() for line in f] with open( osp.join(devkit_path, 'VOC2012/ImageSets/Segmentation', 'val.txt')) as f: val_list = [line.strip() for line in f] aug_train_list = generate_aug_list(ori_train_list + full_aug_list, val_list) assert len(aug_train_list) == AUG_LEN, 'len(aug_train_list) != {}'.format( AUG_LEN) with open( osp.join(devkit_path, 'VOC2012/ImageSets/Segmentation', 'trainaug.txt'), 'w') as f: f.writelines(line + '\n' for line in aug_train_list) aug_list = generate_aug_list(full_aug_list, ori_train_list + val_list) assert len(aug_list) == AUG_LEN - len( ori_train_list), 'len(aug_list) != {}'.format(AUG_LEN - len(ori_train_list)) with open( osp.join(devkit_path, 'VOC2012/ImageSets/Segmentation', 'aug.txt'), 'w') as f: f.writelines(line + '\n' for line in aug_list) print('Done!')
def paths_from_folder(folder): """Generate paths from folder. Args: folder (str): Folder path. Returns: list[str]: Returned path list. """ paths = list(mmcv.scandir(folder)) paths = [osp.join(folder, path) for path in paths] return paths
def load_annotations(self, img_dir, img_suffix, ann_dir, seg_map_suffix, split): """Load annotation from directory. Args: img_dir (str): Path to image directory img_suffix (str): Suffix of images. ann_dir (str|None): Path to annotation directory. seg_map_suffix (str|None): Suffix of segmentation maps. split (str|None): Split txt file. If split is specified, only file with suffix in the splits will be loaded. Otherwise, all images in img_dir/ann_dir will be loaded. Default: None Returns: list[dict]: All image info of dataset. #TODO: currently, we need to ensure that the label files have exactly the same folder structures as the image files, which is not flexible. """ img_infos = [] if split is not None: with open(split) as f: for line in f: img_name = line.strip() img_info = dict(filename=img_name + img_suffix) if ann_dir is not None: # seg_map = osp.join(ann_dir, img_name + seg_map_suffix) # img_info['ann'] = dict(seg_map=seg_map, exist_label=osp.exists(seg_map)) seg_map = img_name + seg_map_suffix img_info['ann'] = dict(seg_map=seg_map, exist_label=osp.exists( osp.join(ann_dir, seg_map))) img_infos.append(img_info) else: for img in mmcv.scandir(img_dir, img_suffix, recursive=True): # img_info = dict(filename=osp.join(img_dir, img)) img_info = dict(filename=img) if ann_dir is not None: # seg_map = osp.join(ann_dir, # img.replace(img_suffix, seg_map_suffix)) # img_info['ann'] = dict(seg_map=seg_map, exist_label=osp.exists(seg_map)) seg_map = img.replace(img_suffix, seg_map_suffix) img_info['ann'] = dict(seg_map=seg_map, exist_label=osp.exists( osp.join(ann_dir, seg_map))) img_infos.append(img_info) print_log(f'Loaded {len(img_infos)} images', logger=get_root_logger()) return img_infos
def prepare_keys_imagenet_jpg(folder_path): """Prepare image path list and keys for ImageNet dataset. Args: folder_path (str): Folder path. Returns: list[str]: Image path list. list[str]: Key list. """ print('Reading image path list ...') img_path_list = sorted(list(mmcv.scandir(folder_path, suffix=('', 'jpg')))) keys = [img_path.split('.jpg')[0] for img_path in sorted(img_path_list)] return img_path_list, keys
def main( download_dir, username, password, nproc, ): dataset_dir = Path(download_dir) / "cityscapes" if username is None or password is None: raise ValueError( "You must indicate your username and password either in the script variables or by passing options --username and --pasword." ) download_cityscapes(dataset_dir, username, password, overwrite=False) install_cityscapes_api() gt_dir = dataset_dir / "gtFine" poly_files = [] for poly in mmcv.scandir(str(gt_dir), "_polygons.json", recursive=True): poly_file = str(gt_dir / poly) poly_files.append(poly_file) mmcv.track_parallel_progress(convert_json_to_label, poly_files, nproc) split_names = ["train", "val", "test"] for split in split_names: filenames = [] for poly in mmcv.scandir(str(gt_dir / split), "_polygons.json", recursive=True): filenames.append(poly.replace("_gtFine_polygons.json", "")) with open(str(dataset_dir / f"{split}.txt"), "w") as f: f.writelines(f + "\n" for f in filenames)
def prepare_keys_reds(folder_path): """Prepare image path list and keys for REDS dataset. Args: folder_path (str): Folder path. Returns: list[str]: Image path list. list[str]: Key list. """ print('Reading image path list ...') img_path_list = sorted( list(mmcv.scandir(folder_path, suffix='png', recursive=True))) keys = [v.split('.png')[0] for v in img_path_list] # example: 000/00000000 return img_path_list, keys
def prepare_keys_div2k(folder_path): """Prepare image path list and keys for DIV2K dataset. Args: folder_path (str): Folder path. Returns: list[str]: Image path list. list[str]: Key list. """ print('Reading image path list ...') img_path_list = sorted( list(mmcv.scandir(folder_path, suffix='png', recursive=False))) keys = [img_path.split('.png')[0] for img_path in sorted(img_path_list)] return img_path_list, keys
def collect_image_infos(path, exclude_extensions=None): img_infos = [] images_generator = mmcv.scandir(path, recursive=True) for image_path in mmcv.track_iter_progress(list(images_generator)): if exclude_extensions is None or ( exclude_extensions is not None and not image_path.lower().endswith(exclude_extensions)): image_path = os.path.join(path, image_path) img_pillow = Image.open(image_path) img_info = { 'filename': image_path, 'width': img_pillow.width, 'height': img_pillow.height, } img_infos.append(img_info) return img_infos
def load_annotations(self, img_dir, img_suffix, ann_dir, seg_map_suffix=None, split=None): """Load annotation from directory. Args: img_dir (str): Path to image directory img_suffix (str): Suffix of images. ann_dir (str|None): Path to annotation directory. seg_map_suffix (str|None): Suffix of segmentation maps. split (str|None): Split txt file. If split is specified, only file with suffix in the splits will be loaded. Otherwise, all images in img_dir/ann_dir will be loaded. Default: None Returns: list[dict]: All image info of dataset. """ img_infos = [] if split is not None: with open(split) as f: for line in f: name = line.strip() img_info = dict(filename=name + img_suffix) if ann_dir is not None: ann_name = name + '_instance_color_RGB' seg_map = ann_name + seg_map_suffix img_info['ann'] = dict(seg_map=seg_map) img_infos.append(img_info) else: for img in mmcv.scandir(img_dir, img_suffix, recursive=True): img_info = dict(filename=img) if ann_dir is not None: seg_img = img seg_map = seg_img.replace( img_suffix, '_instance_color_RGB' + seg_map_suffix) img_info['ann'] = dict(seg_map=seg_map) img_infos.append(img_info) print_log(f'Loaded {len(img_infos)} images', logger=get_root_logger()) return img_infos
def __init__(self, opt): super(SingleImageDataset, self).__init__() self.opt = opt # file client (io backend) self.file_client = None self.io_backend_opt = opt['io_backend'] self.lq_folder = opt['dataroot_lq'] if 'ann_file' in self.opt: with open(self.opt['ann_file'], 'r') as fin: self.paths = [ osp.join(self.lq_folder, line.split(' ')[0]) for line in fin ] else: self.paths = [ osp.join(self.lq_folder, v) for v in mmcv.scandir(self.lq_folder) ]
def test_scandir(): folder = osp.join(osp.dirname(__file__), 'data/for_scan') filenames = ['a.bin', '1.txt', '2.txt', '1.json', '2.json'] assert set(mmcv.scandir(folder)) == set(filenames) assert set(mmcv.scandir(folder, '.txt')) == set( [filename for filename in filenames if filename.endswith('.txt')]) assert set(mmcv.scandir(folder, ('.json', '.txt'))) == set([ filename for filename in filenames if filename.endswith(('.txt', '.json')) ]) assert set(mmcv.scandir(folder, '.png')) == set() with pytest.raises(TypeError): mmcv.scandir(folder, 111)
def read_img_seq(path, require_mod_crop=False, scale=1): """Read a sequence of images from a given folder path. Args: path (list[str] | str): List of image paths or image folder path. require_mod_crop (bool): Require mod crop for each image. Default: False. scale (int): Scale factor for mod_crop. Default: 1. Returns: Tensor: size (t, c, h, w), RGB, [0, 1]. """ if isinstance(path, list): img_paths = path else: img_paths = sorted([osp.join(path, v) for v in mmcv.scandir(path)]) imgs = [mmcv.imread(v).astype(np.float32) / 255. for v in img_paths] if require_mod_crop: imgs = [mod_crop(img, scale) for img in imgs] imgs = totensor(imgs, bgr2rgb=True, float32=True) imgs = torch.stack(imgs, dim=0) return imgs
def scan_folder(path): """Obtain image path list (including sub-folders) from a given folder. Args: path (str | :obj:`Path`): Folder path. Returns: list[str]: image list obtained form given folder. """ if isinstance(path, (str, Path)): path = str(path) else: raise TypeError("'path' must be a str or a Path object, " f'but received {type(path)}.') images = list(scandir(path, suffix=IMG_EXTENSIONS, recursive=True)) images = [osp.join(path, v) for v in images] assert images, f'{path} has no valid image file.' return images
def upload_dir(client, local_dir, remote_dir, exp_name=None, suffix=None, remove_local_file=True): """Upload a directory to the cloud server. Args: client (obj): AWS client. local_dir (str): Path for the local data. remote_dir (str): Path for the remote server. exp_name (str, optional): The experiment name. Defaults to None. suffix (str, optional): Suffix for the data files. Defaults to None. remove_local_file (bool, optional): Whether to removing the local files after uploading. Defaults to True. """ files = mmcv.scandir(local_dir, suffix=suffix, recursive=False) files = [os.path.join(local_dir, x) for x in files] # remove the rebundant symlinks in the data directory files = [x for x in files if not os.path.islink(x)] # get the actual exp_name in work_dir if exp_name is None: exp_name = local_dir.split('/')[-1] mmcv.print_log(f'Uploading {len(files)} files to ceph.', 'mmgen') for file in files: with open(file, 'rb') as f: data = f.read() _path_splits = file.split('/') idx = _path_splits.index(exp_name) _rel_path = '/'.join(_path_splits[idx:]) _ceph_path = os.path.join(remote_dir, _rel_path) client.put(_ceph_path, data) # remove the local file to save space if remove_local_file: os.remove(file)
def load_annotations(self, img_dir, img_suffix, ann_dir, seg_map_suffix, split): """Load annotation from directory. Args: img_dir (str): Path to image directory img_suffix (str): Suffix of images. ann_dir (str|None): Path to annotation directory. seg_map_suffix (str|None): Suffix of segmentation maps. split (str|None): Split txt file. If split is specified, only file with suffix in the splits will be loaded. Otherwise, all images in img_dir/ann_dir will be loaded. Default: None Returns: list[dict]: All image info of dataset. """ img_infos = [] if split is not None: with open(split) as f: for line in f: img_name = line.strip() img_file = osp.join(img_dir, img_name + img_suffix) img_info = dict(filename=img_file) if ann_dir is not None: seg_map = osp.join(ann_dir, img_name + seg_map_suffix) img_info['ann'] = dict(seg_map=seg_map) img_infos.append(img_info) else: for img in mmcv.scandir(img_dir, img_suffix, recursive=True): img_file = osp.join(img_dir, img) img_info = dict(filename=img_file) if ann_dir is not None: seg_map = osp.join(ann_dir, img.replace(img_suffix, seg_map_suffix)) img_info['ann'] = dict(seg_map=seg_map) img_infos.append(img_info) if udist.is_master(): print(f'Loaded {len(img_infos)} images') return img_infos