def run_make_train_annotation(data_root, image_set): imageset_file_loc = os.path.join(data_root, 'image_sets', image_set) # split = 'train1_ids_all_670' newimage_dir = os.path.join(data_root, 'stag`e1_images', 'stage1_train') os.makedirs(newimage_dir, exist_ok=True) multimask_dir = os.path.join(data_root, 'stage1_images', 'multi_masks') os.makedirs(multimask_dir, exist_ok=True) overlays_dir = os.path.join(data_root, 'stage1_images', 'overlays') os.makedirs(overlays_dir, exist_ok=True) ids = read_list_from_file(imageset_file_loc, comment='#') num_ids = len(ids) for i in range(num_ids): id = ids[i] print(id) name, folder = id.split('/') image_loc = os.path.join(data_root, 'stage1_images', 'official', folder, name, 'images', '*.png') image_files = glob.glob(image_loc) assert (len(image_files) == 1) image_file = image_files[0] # image image = cv2.imread(image_file, cv2.IMREAD_COLOR) H, W, C = image.shape multi_mask = np.zeros((H, W), np.int32) masks_loc = os.path.join(data_root, 'stage1_images', 'official', folder, name, 'masks', '*.png') mask_files = glob.glob(masks_loc) mask_files.sort() num_masks = len(mask_files) for k in range(num_masks): mask_file = mask_files[k] mask = cv2.imread(mask_file, cv2.IMREAD_GRAYSCALE) multi_mask[np.where(mask > 128)] = k + 1 # check color_overlay = multi_mask_to_color_overlay(multi_mask, color='summer') color1_overlay = multi_mask_to_contour_overlay(multi_mask, color_overlay, [255, 255, 255]) contour_overlay = multi_mask_to_contour_overlay( multi_mask, image, [0, 255, 0]) all = np.hstack(( image, contour_overlay, color1_overlay, )).astype(np.uint8) cv2.imwrite(os.path.join(newimage_dir, name + '.png'), image) np.save(os.path.join(multimask_dir, name + '.npy'), multi_mask) cv2.imwrite(os.path.join(multimask_dir, name + '.png'), color_overlay) cv2.imwrite(os.path.join(overlays_dir, name + '.png'), all)
def run_make_test_annotation(cfg, norm=False): ids = read_list_from_file(os.path.join(cfg.split_dir, cfg.annotation_test_split), comment='#') s_test = SourceFolder(cfg.source_test_dir) d_test = DataFolder(os.path.join(cfg.data_dir, 'stage2_test_final')) for i in range(len(ids)): folder = ids[i].split('/')[0] name = ids[i].split('/')[1] image = s_test.get_image(name) # show and save into image folder_name cv2.imwrite(os.path.join(d_test.image_folder, '%s.png' % name), image) print('\rannotate: ', i) print('run_make_test_annotation success!')
def __init__(self, cfg, split, transform=None, mode='train'): super(ScienceDataset, self).__init__() start = timer() self.cfg = cfg self.split = split self.transform = transform self.mode = mode # read split self.ids = read_list_from_file(os.path.join(self.cfg.split_dir, split), comment='#') # print print('\ttime = %0.2f min' % ((timer() - start) / 60)) print('\tnum_ids = %d' % (len(self.ids))) print('')
def __init__(self, split, transform=None, mode='train'): super(ScienceDataset, self).__init__() start = timer() self.split = split self.transform = transform self.mode = mode #read split ids = read_list_from_file(DATA_DIR + '/split/' + split, comment='#') #save self.ids = ids #print print('\ttime = %0.2f min' % ((timer() - start) / 60)) print('\tnum_ids = %d' % (len(self.ids))) print('')
def run_make_train_annotation(cfg): ids = read_list_from_file(os.path.join(cfg.split_dir, cfg.annotation_train_split), comment='#') s_train = SourceFolder(cfg.source_train_dir) d_train = DataFolder(os.path.join(cfg.data_dir, 'stage1_train')) for i in range(len(ids)): # load image and mask files id = ids[i] folder = id.split('/')[0] name = id.split('/')[-1] image = s_train.get_image(name) multi_masks = s_train.get_masks(name) # check color_overlay = multi_mask_to_color_overlay(multi_masks, color='summer') color1_overlay = multi_mask_to_contour_overlay(multi_masks, color_overlay, [255, 255, 255]) contour_overlay = multi_mask_to_contour_overlay( multi_masks, image, [0, 255, 0]) stacked_overlay = np.hstack(( image, contour_overlay, color1_overlay, )).astype(np.uint8) # dump images and multi-masks cv2.imwrite(os.path.join(d_train.image_folder, '%s.png' % name), image) cv2.imwrite(os.path.join(d_train.mask_folder, '%s.png' % name), color_overlay) np.save(os.path.join(d_train.mask_folder, '%s.npy' % name), multi_masks) cv2.imwrite(os.path.join(d_train.overlay_folder, '%s.png' % name), stacked_overlay) print('\rannotate: ', i) print('run_make_train_annotation success!')
def run_make_test_annotation(data_root, image_set): imageset_file_loc = os.path.join(data_root, 'image_sets', image_set) # split = 'test1_ids_all_65' ids = read_list_from_file(imageset_file_loc, comment='#') newimage_dir = os.path.join(data_root, 'stage1_images', 'stage1_test') os.makedirs(newimage_dir, exist_ok=True) num_ids = len(ids) for i in range(num_ids): id = ids[i] print(id) folder, name = ids[i].split('/') image_loc = os.path.join(data_root, 'stage1_images', 'official', folder, name, 'images', '*.png') image_files = glob.glob(image_loc) assert (len(image_files) == 1) image_file = image_files[0] # image image = cv2.imread(image_file, cv2.IMREAD_COLOR) cv2.imwrite(os.path.join(newimage_dir, name + '.png'), image)
def run_make_extra_annotation(cfg, min_mask_threshold): ids = read_list_from_file(os.path.join(cfg.split_dir, cfg.annotation_extra_split), comment='#') s_extra = SourceFolder(cfg.source_extra_dir) d_extra = DataFolder(os.path.join(cfg.data_dir, 'extra_data')) extra_train_270 = [] for row in ids: # load image and mask files folder = row.split('/')[0] name = row.split('/')[-1] image = s_extra.get_image(name) multi_masks = s_extra.get_masks(name) for i in range(3): for j in range(3): x = i * 256 y = j * 256 img_idx = i * 3 + j image_crop = image[y:y + 256, x:x + 256] multi_mask_crop = multi_masks[y:y + 256, x:x + 256] mask_idx = np.unique(multi_mask_crop) assert (mask_idx[0] == 0) mask_idx = mask_idx[1:] re_assigned_multi_mask = np.zeros((256, 256), np.int32) for x in range(len(mask_idx)): mask_size = np.sum(multi_mask_crop == mask_idx[x]) if mask_size < min_mask_threshold: continue re_assigned_multi_mask[np.where( multi_mask_crop == mask_idx[x])] = x + 1 # check color_overlay = multi_mask_to_color_overlay( re_assigned_multi_mask, color='summer') color1_overlay = multi_mask_to_contour_overlay( re_assigned_multi_mask, color_overlay, [255, 255, 255]) contour_overlay = multi_mask_to_contour_overlay( re_assigned_multi_mask, image_crop, [0, 255, 0]) stacked_overlay = np.hstack(( image_crop, contour_overlay, color1_overlay, )).astype(np.uint8) # dump images and multi-masks extra_train_270.append('%s_%s' % (name, img_idx)) cv2.imwrite( os.path.join(d_extra.image_folder, '%s_%s.png' % (name, img_idx)), image_crop) cv2.imwrite( os.path.join(d_extra.mask_folder, '%s_%s.png' % (name, img_idx)), color_overlay) np.save( os.path.join(d_extra.mask_folder, '%s_%s.npy' % (name, img_idx)), re_assigned_multi_mask) cv2.imwrite( os.path.join(d_extra.overlay_folder, '%s_%s.png' % (name, img_idx)), stacked_overlay) print('\r %s_%s' % (name, img_idx)) print('run_make_extra_annotation success!')
def run_make_train_annotation_fixing_masks(data_root, image_set): imageset_file_loc = os.path.join(data_root, 'image_sets', image_set) multimask_dir = os.path.join(data_root, 'stage1_images', 'fixed_multi_masks_watershred_debug') os.makedirs(multimask_dir, exist_ok=True) overlays_dir = os.path.join(data_root, 'stage1_images', 'fixed_overlays_watershred_debug') os.makedirs(overlays_dir, exist_ok=True) ids = read_list_from_file(imageset_file_loc, comment='#') fixed_masks_loc = os.path.join(data_root, 'stage1_images', 'fixed_mask_new', '*') fixed_masks_image_keys = [ loc.split('/')[-1] for loc in glob.glob(fixed_masks_loc) ] num_ids = len(ids) for i in range(num_ids): id = ids[i] print(id) folder, name = id.split('/') image_loc = os.path.join(data_root, 'stage1_images', 'official', folder, name, 'images', '*.png') image_files = glob.glob(image_loc) assert (len(image_files) == 1) image_file = image_files[0] # image image = cv2.imread(image_file, cv2.IMREAD_COLOR) fixed_masks_instance_keys = set() if name in fixed_masks_image_keys: fixed_masks_loc_instances = os.path.join(data_root, 'stage1_images', 'fixed_mask_new', name, '*') fixed_masks_instance_keys = [ loc.split('/')[-1][:-7] for loc in glob.glob(fixed_masks_loc_instances) ] fixed_masks_instance_keys = set(fixed_masks_instance_keys) print(fixed_masks_instance_keys) H, W, C = image.shape multi_mask = np.zeros((H, W), np.int32) masks_loc = os.path.join(data_root, 'stage1_images', 'official', folder, name, 'masks', '*.png') mask_files = glob.glob(masks_loc) mask_files.sort() instance_count = 1 for mask_file in mask_files: mask_key = mask_file.split('/')[-1].strip('.png') if mask_key in fixed_masks_instance_keys: fixed_mask_files = glob.glob( os.path.join(data_root, 'stage1_images', 'fixed_mask_new', name, mask_key + '*.png')) print(fixed_mask_files) for fixed_mask_file in fixed_mask_files: mask = cv2.imread(fixed_mask_file, cv2.IMREAD_GRAYSCALE) instance_contour_overlay = mask_to_inner_contour(mask) mask = watershred_post_proess(mask, instance_contour_overlay, 20) multi_mask[np.where(mask > 128)] = instance_count instance_count += 1 else: mask = cv2.imread(mask_file, cv2.IMREAD_GRAYSCALE) instance_contour_overlay = mask_to_inner_contour(mask) mask = watershred_post_proess(mask, instance_contour_overlay, 20) multi_mask[np.where(mask > 128)] = instance_count instance_count += 1 # check color_overlay = multi_mask_to_color_overlay(multi_mask, color='summer') color1_overlay = multi_mask_to_contour_overlay(multi_mask, color_overlay, [255, 255, 255]) contour_overlay = multi_mask_to_contour_overlay( multi_mask, image, [0, 255, 0]) all = np.hstack(( image, contour_overlay, color1_overlay, )).astype(np.uint8) np.save(os.path.join(multimask_dir, name + '.npy'), multi_mask) cv2.imwrite(os.path.join(multimask_dir, name + '.png'), color_overlay) cv2.imwrite(os.path.join(overlays_dir, name + '.png'), all)
def run_make_annotation(): split = 'train1_ids_all_670' ids = read_list_from_file(DATA_DIR + '/split/' + split, comment='#') num_ids = len(ids) for i in range(num_ids): id = ids[i] # image_files = glob.glob(DATA_DIR + '/image/' + id + '/images/*.png') image_files = list( (DATA_DIR / ('image' + id) / 'images').glob('*.png')) assert (len(image_files) == 1) image_file = image_files[0] print(id) # ----clear old ----------------------------- if 1: for f in [ 'one_mask.png', 'one_countour_mask.png', 'one_countour_image.png', 'one_countour.png', 'overlap.png', 'one_center.png', '/masks.npy', '/labels.npy', '/countour_on_image.png', '/cut_mask.png', '/label.npy', '/mask.png', '/overlay.png', '/multi.npy', '/multi.png', '/instance.npy', '/instance.png', '/multi_instance.npy', '/multi_instance.png', ]: file = DATA_DIR + '/image/' + id + '/' + f if os.path.exists(file): os.remove(file) # ----clear old ----------------------------- # image image = cv2.imread(image_file, cv2.IMREAD_COLOR) H, W, C = image.shape multi_mask = np.zeros((H, W), np.int32) mask = np.zeros((H, W), np.uint8) countour = np.zeros((H, W), np.uint8) # mask_files = glob.glob(DATA_DIR + '/image/' + id + '/masks/*.png') mask_files = list((DATA_DIR / ('image' + id) / 'masks').glob('*.png')) mask_files.sort() count = len(mask_files) for i in range(count): mask_file = mask_files[i] thresh = cv2.imread(mask_file, cv2.IMREAD_GRAYSCALE) thresh = thresh > 128 index = np.where(thresh == True) multi_mask[thresh] = i + 1 mask = np.logical_or(mask, thresh) countour = np.logical_or(countour, thresh_to_inner_contour(thresh)) ## save and show ------------------------------------------- countour_on_image = image.copy() countour_on_image = countour[:, :, np.newaxis] * np.array( (0, 255, 0)) + (1 - countour[:, :, np.newaxis]) * countour_on_image countour_overlay = countour * 255 mask_overlay = mask * 255 multi_mask_overlay = multi_mask_to_overlay(multi_mask) image_show('image', image) image_show('mask', mask_overlay) image_show('multi_mask', multi_mask_overlay) image_show('countour', countour_overlay) image_show('countour_on_image', countour_on_image) np.save(DATA_DIR + '/image/' + id + '/multi_mask.npy', multi_mask) cv2.imwrite(DATA_DIR + '/image/' + id + '/multi_mask.png', multi_mask_overlay) cv2.imwrite(DATA_DIR + '/image/' + id + '/mask.png', mask_overlay) cv2.imwrite(DATA_DIR + '/image/' + id + '/countour.png', countour_overlay) cv2.imwrite(DATA_DIR + '/image/' + id + '/countour_on_image.png', countour_on_image) cv2.waitKey(1)