def load_images(image_dir, file_list, rgb_to_id=False, trainid_to_rgb=False, dtype=np.uint8, labels_table=None): image_list = [] for file_name in tqdm.tqdm(file_list, desc='Loading images from <>/{}'.format( os.path.basename(image_dir)), total=len(file_list)): # Hackery. possible_extensions = [ '_sem255instid2rgb_cocopano.png', '_id2rgb_cocopano.png' ] if not os.path.exists(os.path.join(image_dir, file_name)) and '_gt' in image_dir: for ext in possible_extensions: new_file_name = 'groundtruth_{}'.format(file_name).replace( '_image.png', ext) if os.path.join(image_dir, new_file_name): file_name = new_file_name break assert os.path.exists(os.path.join( image_dir, file_name)), '{} does not exist'.format(file_name) if not os.path.exists(os.path.join( image_dir, file_name)) and '_pred' in image_dir: for ext in possible_extensions: new_file_name = 'predictions_{}'.format(file_name).replace( '_image.png', ext) if os.path.join(image_dir, new_file_name): file_name = new_file_name break assert os.path.exists(os.path.join( image_dir, file_name)), '{} does not exist'.format(file_name) if not os.path.exists(os.path.join( image_dir, file_name)) and 'images' in image_dir: file_name = 'image_{}'.format(file_name).replace('_image', '') assert rgb_to_id is False assert trainid_to_rgb is False assert os.path.exists(os.path.join( image_dir, file_name)), '{} does not exist'.format( os.path.join(image_dir, file_name)) with Image.open(os.path.join(image_dir, file_name)) as img: pan_im = np.array(img, dtype=dtype) if rgb_to_id: assert trainid_to_rgb is False panoptic_id_im = rgb2id(pan_im) im = panoptic_id_im elif trainid_to_rgb: panoptic_id_im = rgb2id(pan_im) im = panoid_img_to_rgb(panoptic_id_im, labels_table) else: im = pan_im image_list.append(im) return image_list
def check_annotation(out_dirs, gt_annotation, pred_annotation, categories, VOID=(0, rgb2id((255, 255, 255)))): pan_gt = np.array(Image.open( os.path.join(out_dirs['gt'], gt_annotation['file_name'])), dtype=np.uint32) pan_gt = rgb2id(pan_gt) pan_pred = np.array(Image.open( os.path.join(out_dirs['pred'], pred_annotation['file_name'])), dtype=np.uint32) pan_pred = rgb2id(pan_pred) gt_segms = {el['id']: el for el in gt_annotation['segments_info']} pred_segms = {el['id']: el for el in pred_annotation['segments_info']} # predicted segments area calculation + prediction sanity checks pred_labels_set = set(el['id'] for el in pred_annotation['segments_info']) labels, labels_cnt = np.unique(pan_pred, return_counts=True) for label, label_cnt in zip(labels, labels_cnt): if label not in pred_segms: if label == VOID: continue raise KeyError( 'In the image with ID {} segment with ID {} is presented in PNG and not presented in JSON.' .format(gt_annotation['image_id'], label)) pred_segms[label]['area'] = label_cnt pred_labels_set.remove(label) if pred_segms[label]['category_id'] not in categories: raise KeyError( 'In the image with ID {} segment with ID {} has unknown category_id {}.' .format(gt_annotation['image_id'], label, pred_segms[label]['category_id'])) if len(pred_labels_set) != 0: raise KeyError( 'In the image with ID {} the following segment IDs {} are presented in JSON and not presented in ' 'PNG.'.format(gt_annotation['image_id'], list(pred_labels_set)))
def load_gt_img_in_panoptic_form(gt_img_file): with Image.open(gt_img_file) as img: pan_gt = np.array(img, dtype=np.uint32) pan_gt = rgb2id(pan_gt) return pan_gt
def panoptic_converter_from_rgb_ids( out_folder, out_json_file, labels_file_list, problem_config: instance_utils.InstanceProblemConfig, labels_table=None, VOID_RGB=(255, 255, 255), VOID_INSTANCE_G=255, overwrite=None): """ Takes predictions output from tester (special Trainer type) and outputs coco panoptic format Inputs should represent the different channels, where rgb2id creates the channel id (R + G*255 + B*255*255) """ # Replace split with file_list labels_table = labels_table or problem_config.labels_table if not os.path.isdir(out_folder): print("Creating folder {} for panoptic segmentation PNGs".format( out_folder)) os.mkdir(out_folder) categories_dict = {cat.id: cat for cat in labels_table} images = [] annotations = [] n_files = len(labels_file_list) cocopano_ext = '_cocopano.png' all_files_already_exist = os.path.exists(out_json_file) file_exists = [] for working_idx, label_f in tqdm.tqdm(enumerate(labels_file_list), desc='Finding files', total=n_files): file_name = label_f.split('/')[-1] out_file_name = file_name.replace('.png', cocopano_ext) file_exists.append( os.path.exists(os.path.join(out_folder, out_file_name))) all_files_already_exist = all_files_already_exist and all(file_exists) some_files_already_exist = any(file_exists) if all_files_already_exist: if overwrite is None: y_or_n = y_or_n_input('All files already exist. Overwrite?') if y_or_n == 'n': return elif overwrite is False: print('All files already exist.') return elif some_files_already_exist: print( 'Warning: some ({}/{}) files already existed. I may be overwriting them.' .format(sum(file_exists), len(file_exists))) for working_idx, label_f in tqdm.tqdm( enumerate(labels_file_list), desc='Converting to COCO panoptic format', total=n_files): rgb_format = np.array(Image.open(label_f), dtype=np.uint8) assert len(rgb_format.shape) == 3, 'Image should be in rgb format' file_name = label_f.split('/')[-1] out_file_name = file_name.replace('.png', cocopano_ext) if os.path.exists(out_file_name): if not overwrite: continue assert file_name.rsplit('_', 2)[2] == 'sem255instid2rgb.png' image_id = file_name.rsplit('_', 2)[1] image_filename = '{}_image.png'.format(image_id) # image entry, id for image is its filename without extension images.append({ "id": image_id, "width": rgb_format.shape[1], "height": rgb_format.shape[0], "file_name": image_filename }) id_generator = IdGenerator(categories_dict) idx = 0 present_channel_colors = np.unique(rgb_format.reshape( -1, rgb_format.shape[2]), axis=0) present_channel_colors = [ c for c in present_channel_colors if rgb2id(c) != rgb2id(VOID_RGB) and c[1] != VOID_INSTANCE_G ] pan_format = np.zeros((rgb_format.shape[0], rgb_format.shape[1], 3), dtype=np.uint8) segm_info = [] pan_ids = np.zeros((rgb_format.shape[0], rgb_format.shape[1])) semantic_ids_not_in_category_dict = [] unique_segm_ids = [] for color_val in present_channel_colors: semantic_id = color_val[0] instance_count_id = color_val[1] is_crowd = instance_count_id < 1 if semantic_id not in categories_dict: if semantic_id not in semantic_ids_not_in_category_dict: semantic_ids_not_in_category_dict.append(semantic_id) continue if categories_dict[semantic_id]['isthing'] == 0: is_crowd = 0 mask = (rgb_format == color_val).all(axis=2) area, bbox = get_bounding_box(mask) segment_id = semantic_id * 1000 + instance_count_id pan_color = id2rgb(segment_id) pan_format[mask, :] = pan_color pan_ids[mask] = segment_id assert segment_id not in unique_segm_ids # every segment should be unique unique_segm_ids.append(segment_id) segm_info.append({ "id": int(segment_id), "category_id": int(semantic_id), "area": area, "bbox": bbox, "iscrowd": is_crowd }) if len(semantic_ids_not_in_category_dict) > 0: print( 'The following semantic ids were present in the image, but not in the categories dict ({catdict}): ' '{semids}'.format(catdict=categories_dict, semids=semantic_ids_not_in_category_dict)) Image.fromarray(pan_format).save( os.path.join(out_folder, out_file_name)) # Reverse the process and ensure we get the right id reloaded_pan_img = np.array(Image.open( os.path.join(out_folder, out_file_name)), dtype=np.uint32) reloaded_pan_id = rgb2id(reloaded_pan_img) assert np.all(reloaded_pan_id == pan_ids) # print('Max pan id: {}'.format(reloaded_pan_id.max())) if len(segm_info) == 0: raise Exception('No segments in this image') annotations.append({ 'image_id': image_id, 'file_name': out_file_name, "segments_info": segm_info }) # shutil.copy(label_f, os.path.join(out_folder, file_name)) if len(segm_info) != len(present_channel_colors): import ipdb ipdb.set_trace() assert len(segm_info) == len(present_channel_colors) d = { 'images': images, 'annotations': annotations, 'categories': [l.__dict__ for l in labels_table], } rm_json_if_breaks = not os.path.exists(out_json_file) try: save_json(d, out_json_file) except: if rm_json_if_breaks: os.remove(out_json_file) raise
def load_rgb_predictions_or_gt_to_id(in_file): rgb_img = imgutils.load_img_as_dtype(in_file, dtype=None) lbl = rgb2id(rgb_img) # convert void class lbl[255 + 256 * 255 + 256 * 256 * 255] = -1 return