def load(self, add_gt=True): with timed_operation("Load groundtruth boxes from {}".format( self.tfrecords_pattern)): examples = self.get_examples(self.tfrecords_pattern) result = [] skipped = 0 for i, example in enumerate(examples): image_path = example_to_str(example, 'image_path') old_path = example_to_str(example, 'path') w = example_to_int(example, 'width') h = example_to_int(example, 'height') bboxes = example_to_numpy(example, 'bboxes', np.float32, (-1, 4)) if bboxes.shape[0] == 0: skipped += 1 assert len(bboxes.shape) == 2 assert bboxes.shape[1] == 4 lbl = example_to_int(example, 'label') + 1 # BACKGROUND is 0 labels = np.asarray(list(repeat(lbl, len(bboxes)))) is_crowd = np.zeros(len(bboxes)) result.append({ 'height': h, 'width': w, 'id': old_path, 'file_name': image_path, 'boxes': bboxes, 'class': labels, 'is_crowd': is_crowd, 'label': lbl - 1 }) logger.info('{} images have zero boxes.'.format(skipped)) return result
def create_new_example(example, inbreast_corrections=None, suffix='', flip_label_gen=False): # Save image as png image = get_image(example, 'image', suffix=suffix) old_path = example_to_str(example, 'path') old_path_with_suffix = old_path + suffix new_path = os.path.join(img_dir, old_path_with_suffix.replace( '/', '_')) + '.png' to_png(image, new_path) mask = get_image(example, 'mask', suffix) old_mask_path = example_to_str(example, 'path') + suffix + '_mask' new_mask_path = os.path.join(img_dir, old_mask_path.replace('/', '_')) + '.png' to_png(mask, new_mask_path) # Convert mask to bboxes bboxes = mask_to_boxes(mask) lbl = example_to_int(example, 'label') lbl = inbreast_label(old_path, lbl, inbreast_corrections) if suffix and flip_label_gen: print("Swapping label for image: {}".format(old_path_with_suffix)) lbl = int(not bool(lbl)) # Swap the label # Save it all into Example proto features = feature_dict(img_path=new_path, old_path=old_path_with_suffix, bboxes=bboxes, width=np.int64(image.shape[1]), height=np.int64(image.shape[0]), label=np.int64(lbl)) return to_example(features)
def parse_example_no_session(proto): def get_image(example, feature_name, suffix=''): w = features.example_to_int(example, 'width') h = features.example_to_int(example, 'height') return features.example_to_numpy(example, feature_name + suffix, np.float32, (h, w)) path = features.example_to_str(proto, 'path') image = get_image(proto, 'image') mask = get_image(proto, 'mask') imgs = [normalize_image_np(img, img.shape) for img in [image, mask]] concat = np.concatenate(imgs, axis=-1) return concat, image, mask, features.example_to_int(proto, 'label'), path
def convert(examples, img_dir): os.makedirs(img_dir, exist_ok=True) for example in examples: # Save image as png image = get_image(example, 'image') old_path = example_to_str(example, 'path') new_path = os.path.join(img_dir, old_path.replace('/', '_')) to_png(image, new_path) # Convert mask to bboxes mask = get_image(example, 'mask') bboxes = mask_to_boxes(mask) # Save it all into Example proto features = feature_dict( img_path=new_path, old_path=old_path, bboxes=bboxes, width=np.int64(image.shape[1]), height=np.int64(image.shape[0]), label=np.int64(example_to_int(example, 'label'))) yield to_example(features)
def augment_example(example): image = get_image(example, 'image') img_path = example_to_str(example, 'path') mask = get_image(example, 'mask') aug_image, aug_mask = augment_image(image, mask) lbl = example_to_int(example, 'label') # Save it all into Example proto assert image.shape[0] == height assert image.shape[1] == width features = to_feature_dict(img_path=img_path, img=image, mask=mask, width=np.int64(image.shape[1]), height=np.int64(image.shape[0]), label=np.int64(lbl)) features.update( to_feature_dict(img_path=img_path, img=aug_image, mask=aug_mask, width=np.int64(image.shape[1]), height=np.int64(image.shape[0]), label=np.int64(lbl), suffix='_gen')) return to_example(features)
def get_image(example, feature_name, suffix=''): w = features.example_to_int(example, 'width') h = features.example_to_int(example, 'height') return features.example_to_numpy(example, feature_name + suffix, np.float32, (h, w))