def load_image_gt(image, mask, boxes, class_ids, image_id, config, use_mini_mask=False): #------------------------------# # 原始shape #------------------------------# original_shape = image.shape #------------------------------# # 对图片和mask进行填充 #------------------------------# image, window, scale, padding, crop = letterbox_image( image, max_dim=config.IMAGE_MAX_DIM) mask = letterbox_mask(mask, scale, padding, crop) #------------------------------# # 检漏,防止resize后目标消失 #------------------------------# _idx = np.sum(mask, axis=(0, 1)) * (boxes[:, 3] - boxes[:, 1]) * ( boxes[:, 2] - boxes[:, 0]) > 0 mask = mask[:, :, _idx] boxes = boxes[_idx] class_ids = class_ids[_idx] #------------------------------# # 对mask再次进行缩小 #------------------------------# if use_mini_mask: mask = minimize_mask(boxes, mask, config.MINI_MASK_SHAPE) #------------------------------# # 生成Image_meta #------------------------------# active_class_ids = np.zeros(config.NUM_CLASSES) active_class_ids[0] = 1 for id in class_ids: active_class_ids[id] = 1 image_meta = compose_image_meta(image_id, original_shape, image.shape, window, scale, active_class_ids) return image, image_meta, class_ids, boxes, mask
def load_image_gt(dataset, config, image_id, augment=False, augmentation=None, use_mini_mask=False): # 载入图片和语义分割效果 image = dataset.load_image(image_id) mask, class_ids = dataset.load_mask(image_id) # print("\nbefore:",image_id,np.shape(mask),np.shape(class_ids)) # 原始shape original_shape = image.shape # 获得新图片,原图片在新图片中的位置,变化的尺度,填充的情况等 image, window, scale, padding, crop = utils.resize_image( image, min_dim=config.IMAGE_MIN_DIM, min_scale=config.IMAGE_MIN_SCALE, max_dim=config.IMAGE_MAX_DIM, mode=config.IMAGE_RESIZE_MODE) mask = utils.resize_mask(mask, scale, padding, crop) # print("\nafter:",np.shape(mask),np.shape(class_ids)) # print(np.shape(image),np.shape(mask)) # 可以把图片进行翻转 if augment: logging.warning("'augment' is deprecated. Use 'augmentation' instead.") if random.randint(0, 1): image = np.fliplr(image) mask = np.fliplr(mask) if augmentation: import imgaug # 可用于图像增强 MASK_AUGMENTERS = [ "Sequential", "SomeOf", "OneOf", "Sometimes", "Fliplr", "Flipud", "CropAndPad", "Affine", "PiecewiseAffine" ] def hook(images, augmenter, parents, default): """Determines which augmenters to apply to masks.""" return augmenter.__class__.__name__ in MASK_AUGMENTERS image_shape = image.shape mask_shape = mask.shape det = augmentation.to_deterministic() image = det.augment_image(image) mask = det.augment_image(mask.astype(np.uint8), hooks=imgaug.HooksImages(activator=hook)) assert image.shape == image_shape, "Augmentation shouldn't change image size" assert mask.shape == mask_shape, "Augmentation shouldn't change mask size" mask = mask.astype(np.bool) # 检漏,防止某些层内部实际上不存在语义分割情况 _idx = np.sum(mask, axis=(0, 1)) > 0 # print("\nafterer:",np.shape(mask),np.shape(_idx)) mask = mask[:, :, _idx] class_ids = class_ids[_idx] # 找到mask对应的box bbox = utils.extract_bboxes(mask) active_class_ids = np.zeros([dataset.num_classes], dtype=np.int32) source_class_ids = dataset.source_class_ids[dataset.image_info[image_id] ["source"]] active_class_ids[source_class_ids] = 1 if use_mini_mask: mask = utils.minimize_mask(bbox, mask, config.MINI_MASK_SHAPE) # 生成Image_meta image_meta = utils.compose_image_meta(image_id, original_shape, image.shape, window, scale, active_class_ids) return image, image_meta, class_ids, bbox, mask