def test_HeatmapsOnImage_change_normalization(): # (0.0, 1.0) -> (0.0, 2.0) arr = np.float32([[0.0, 0.5, 1.0], [1.0, 0.5, 0.0]]) observed = ia.HeatmapsOnImage.change_normalization(arr, (0.0, 1.0), (0.0, 2.0)) expected = np.float32([[0.0, 1.0, 2.0], [2.0, 1.0, 0.0]]) assert np.allclose(observed, expected) # (0.0, 1.0) -> (-1.0, 0.0) observed = ia.HeatmapsOnImage.change_normalization(arr, (0.0, 1.0), (-1.0, 0.0)) expected = np.float32([[-1.0, -0.5, 0.0], [0.0, -0.5, -1.0]]) assert np.allclose(observed, expected) # (-1.0, 1.0) -> (1.0, 3.0) arr = np.float32([[-1.0, 0.0, 1.0], [1.0, 0.0, -1.0]]) observed = ia.HeatmapsOnImage.change_normalization(arr, (-1.0, 1.0), (1.0, 3.0)) expected = np.float32([[1.0, 2.0, 3.0], [3.0, 2.0, 1.0]]) assert np.allclose(observed, expected) # (-1.0, 1.0) -> (1.0, 3.0) # value ranges given as HeatmapsOnImage arr = np.float32([[-1.0, 0.0, 1.0], [1.0, 0.0, -1.0]]) source = ia.HeatmapsOnImage(np.float32([[0.0]]), min_value=-1.0, max_value=1.0, shape=(1, 1, 3)) target = ia.HeatmapsOnImage(np.float32([[1.0]]), min_value=1.0, max_value=3.0, shape=(1, 1, 3)) observed = ia.HeatmapsOnImage.change_normalization(arr, source, target) expected = np.float32([[1.0, 2.0, 3.0], [3.0, 2.0, 1.0]]) assert np.allclose(observed, expected)
def apply_augs(sample, aug, min_fx=-2, max_fx=2, min_fy=-2, max_fy=2): warnings.simplefilter("ignore", UserWarning) aug_norescale = iaa.Sequential(aug[:-1]) fnorm = ia.HeatmapsOnImage(sample['fnorm'], shape=sample['fnorm'].shape, min_value=min_fx, max_value=max_fx) fx = ia.HeatmapsOnImage(sample['fx'], shape=sample['fx'].shape, min_value=min_fx, max_value=max_fx) fy = ia.HeatmapsOnImage(sample['fy'], shape=sample['fy'].shape, min_value=min_fy, max_value=max_fy) fnorm = aug_norescale(heatmaps=fnorm).get_arr() fx = aug_norescale(heatmaps=fx).get_arr() fy = aug_norescale(heatmaps=fy).get_arr() fnorm = np.squeeze(fnorm)[..., None] fx = np.squeeze(fx)[..., None] fy = np.squeeze(fy)[..., None] sample['fx'] = fx sample['fy'] = fy sample['fnorm'] = fnorm sample = loc_apply_augs(sample, aug) return sample
def iter(): for z in self.ta.augment_batches([self.rs]): res = self.model.predict(np.array(z.images_aug)) z.heatmaps_aug = [ imgaug.HeatmapsOnImage(x, x.shape) for x in res ] yield z
def _segmentation_entropy_image(logits, caption='Entropy', wandb_image=True): """ calculate entropy for semantic prediction logits E = sum(p_i * log(p_i)) arguments: logits: C x H x W return: wandb Image of entropy heatmap """ logits = logits.cpu().detach().float() log_p = F.log_softmax(logits, dim=0) p = F.softmax(logits, dim=0) entropy = torch.sum(log_p * p, 0).numpy() entropy_image = ia.HeatmapsOnImage(entropy, shape=entropy.shape, min_value=np.min(entropy) - 1e-6, max_value=np.max(entropy) + 1e-6).invert().draw()[0] if wandb_image: return wandb.Image(entropy_image, caption=caption) return entropy_image
def test_basic_functionality(self): heatmaps_arr = np.float32([ [0.5, 0.0, 0.0, 0.5], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.5, 0.0, 0.0, 0.5], ]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(4, 4, 3)) heatmaps_drawn = heatmaps.draw()[0] assert heatmaps_drawn.shape == (4, 4, 3) v1 = heatmaps_drawn[0, 1] v2 = heatmaps_drawn[0, 0] v3 = heatmaps_drawn[1, 1] v1_coords = [(0, 1), (0, 2), (1, 0), (1, 3), (2, 0), (2, 3), (3, 1), (3, 2)] v2_coords = [(0, 0), (0, 3), (3, 0), (3, 3)] v3_coords = [(1, 1), (1, 2), (2, 1), (2, 2)] for y, x in v1_coords: assert np.allclose(heatmaps_drawn[y, x], v1) for y, x in v2_coords: assert np.allclose(heatmaps_drawn[y, x], v2) for y, x in v3_coords: assert np.allclose(heatmaps_drawn[y, x], v3)
def test_batch_contains_no_images(self): aug = iaa.pillike.Affine(translate_px={"x": 10}) hm_arr = np.ones((3, 3, 1), dtype=np.float32) hm = ia.HeatmapsOnImage(hm_arr, shape=(3, 3, 3)) with self.assertRaises(AssertionError): _hm_aug = aug(heatmaps=hm)
def test_HeatmapsOnImage_draw_on_image(): heatmaps_arr = np.float32([[0.0, 1.0], [0.0, 1.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(2, 2, 3)) image = np.uint8([[0, 0, 0, 255], [0, 0, 0, 255], [0, 0, 0, 255], [0, 0, 0, 255]]) image = np.tile(image[..., np.newaxis], (1, 1, 3)) heatmaps_drawn = heatmaps.draw_on_image(image, alpha=0.5, cmap=None)[0] assert heatmaps_drawn.shape == (4, 4, 3) assert np.all(heatmaps_drawn[0:4, 0:2, :] == 0) assert np.all(heatmaps_drawn[0:4, 2:3, :] == 128) or np.all( heatmaps_drawn[0:4, 2:3, :] == 127) assert np.all(heatmaps_drawn[0:4, 3:4, :] == 255) or np.all( heatmaps_drawn[0:4, 3:4, :] == 254) image = np.uint8([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) image = np.tile(image[..., np.newaxis], (1, 1, 3)) heatmaps_drawn = heatmaps.draw_on_image(image, alpha=0.5, resize="image", cmap=None)[0] assert heatmaps_drawn.shape == (2, 2, 3) assert np.all(heatmaps_drawn[0:2, 0, :] == 0) assert np.all(heatmaps_drawn[0:2, 1, :] == 128) or np.all( heatmaps_drawn[0:2, 1, :] == 127)
def test_two_images_and_heatmaps(self): rng = iarandom.RNG(0) images = rng.integers(0, 256, size=(2, 256, 256, 3), dtype=np.uint8) heatmap = np.zeros((256, 256, 1), dtype=np.float32) heatmap[128 - 25:128 + 25, 128 - 25:128 + 25] = 1.0 heatmap1 = ia.HeatmapsOnImage(np.copy(heatmap), shape=images[0].shape) heatmap2 = ia.HeatmapsOnImage(1.0 - heatmap, shape=images[1].shape) image1_w_overlay = heatmap1.draw_on_image(images[0])[0] image2_w_overlay = heatmap2.draw_on_image(images[1])[0] debug_image = iaa.draw_debug_image(images, heatmaps=[heatmap1, heatmap2]) assert self._image_contains(images[0, ...], debug_image) assert self._image_contains(images[1, ...], debug_image) assert self._image_contains(image1_w_overlay, debug_image) assert self._image_contains(image2_w_overlay, debug_image)
def test_value_ranges_given_as_heatmaps_on_image(self): # (-1.0, 1.0) -> (1.0, 3.0) # value ranges given as HeatmapsOnImage arr = np.float32([[-1.0, 0.0, 1.0], [1.0, 0.0, -1.0]]) source = ia.HeatmapsOnImage(np.float32([[0.0]]), min_value=-1.0, max_value=1.0, shape=(1, 1, 3)) target = ia.HeatmapsOnImage(np.float32([[1.0]]), min_value=1.0, max_value=3.0, shape=(1, 1, 3)) observed = ia.HeatmapsOnImage.change_normalization(arr, source, target) expected = np.float32([[1.0, 2.0, 3.0], [3.0, 2.0, 1.0]]) assert np.allclose(observed, expected)
def test_batch_contains_no_images(self): aug = iaa.pillike.EnhanceSharpness(0.75) hm_arr = np.ones((3, 3, 1), dtype=np.float32) hm = ia.HeatmapsOnImage(hm_arr, shape=(3, 3, 3)) hm_aug = aug(heatmaps=hm) assert np.allclose(hm_aug.get_arr(), hm.get_arr())
def test_HeatmapsOnImage_avg_pool(): heatmaps_arr = np.float32([[0.0, 0.0, 0.5, 1.0], [0.0, 0.0, 0.5, 1.0], [0.0, 0.0, 0.5, 1.0], [0.0, 0.0, 0.5, 1.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(4, 4, 3)) heatmaps_pooled = heatmaps.avg_pool(2) assert heatmaps_pooled.arr_0to1.shape == (2, 2, 1) assert np.allclose(heatmaps_pooled.arr_0to1[:, :, 0], np.float32([[0.0, 0.75], [0.0, 0.75]]))
def test_with_kernel_size_2(self): heatmaps_arr = np.float32([[0.0, 0.0, 0.5, 1.0], [0.0, 0.0, 0.5, 1.0], [0.0, 0.0, 0.5, 1.0], [0.0, 0.0, 0.5, 1.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(4, 4, 3)) heatmaps_pooled = heatmaps.max_pool(2) assert heatmaps_pooled.arr_0to1.shape == (2, 2, 1) assert np.allclose(heatmaps_pooled.arr_0to1[:, :, 0], np.float32([[0.0, 1.0], [0.0, 1.0]]))
def test_HeatmapsOnImage_invert(): heatmaps_arr = np.float32([[0.0, 5.0, 10.0], [-1.0, -2.0, 7.5]]) expected = np.float32([[8.0, 3.0, -2.0], [9.0, 10.0, 0.5]]) # (H, W) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(2, 3), min_value=-2.0, max_value=10.0) assert np.allclose(heatmaps.get_arr(), heatmaps_arr) assert np.allclose(heatmaps.invert().get_arr(), expected) # (H, W, 1) heatmaps = ia.HeatmapsOnImage(heatmaps_arr[..., np.newaxis], shape=(2, 3), min_value=-2.0, max_value=10.0) assert np.allclose(heatmaps.get_arr(), heatmaps_arr[..., np.newaxis]) assert np.allclose(heatmaps.invert().get_arr(), expected[..., np.newaxis])
def test_with_2d_input_array(self): # (H, W) heatmaps_arr = self.heatmaps_arr expected = self.expected_arr heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(2, 3), min_value=-2.0, max_value=10.0) assert np.allclose(heatmaps.get_arr(), heatmaps_arr) assert np.allclose(heatmaps.invert().get_arr(), expected)
def test_resize_to_twice_the_size(self): heatmaps_arr = np.float32([[0.0, 1.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(4, 4, 3)) heatmaps_scaled = heatmaps.resize(2.0, interpolation="nearest") assert heatmaps_scaled.arr_0to1.shape == (2, 4, 1) assert heatmaps_scaled.arr_0to1.dtype.name == "float32" assert np.allclose( heatmaps_scaled.arr_0to1[:, :, 0], np.float32([[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]]))
def evaluate(self, d, fold, stage, negatives="all", limit=16): mdl = self.load_model(fold, stage) ta = self.transformAugmentor() folds = self.kfold(d, range(0, len(d))) rs = folds.load(fold, False, negatives, limit) for z in ta.augment_batches([rs]): res = mdl.predict(np.array(z.images_aug)) z.heatmaps_aug = [imgaug.HeatmapsOnImage(x, x.shape) for x in res] yield z pass
def test_HeatmapsOnImage_draw(): heatmaps_arr = np.float32([ [0.5, 0.0, 0.0, 0.5], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.5, 0.0, 0.0, 0.5], ]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(4, 4, 3)) heatmaps_drawn = heatmaps.draw()[0] assert heatmaps_drawn.shape == (4, 4, 3) v1 = heatmaps_drawn[0, 1] v2 = heatmaps_drawn[0, 0] v3 = heatmaps_drawn[1, 1] for y, x in [(0, 1), (0, 2), (1, 0), (1, 3), (2, 0), (2, 3), (3, 1), (3, 2)]: assert np.allclose(heatmaps_drawn[y, x], v1) for y, x in [(0, 0), (0, 3), (3, 0), (3, 3)]: assert np.allclose(heatmaps_drawn[y, x], v2) for y, x in [(1, 1), (1, 2), (2, 1), (2, 2)]: assert np.allclose(heatmaps_drawn[y, x], v3) # size differs from heatmap array size heatmaps_arr = np.float32([[0.0, 1.0], [0.0, 1.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(2, 2, 3)) heatmaps_drawn = heatmaps.draw(size=(4, 4))[0] assert heatmaps_drawn.shape == (4, 4, 3) v1 = heatmaps_drawn[0, 0] v2 = heatmaps_drawn[0, -1] for y in range(4): for x in range(2): assert np.allclose(heatmaps_drawn[y, x], v1) for y in range(4): for x in range(2, 4): assert np.allclose(heatmaps_drawn[y, x], v2)
def test_two_images_and_heatmaps__multichannel(self): rng = iarandom.RNG(0) images = rng.integers(0, 256, size=(2, 256, 256, 3), dtype=np.uint8) heatmap = np.zeros((256, 256, 2), dtype=np.float32) heatmap[100-25:100+25, 100-25:100+25, 0] = 1.0 heatmap[200-25:200+25, 200-25:200+25, 1] = 1.0 heatmap1 = ia.HeatmapsOnImage(np.copy(heatmap), shape=images[0].shape) heatmap2 = ia.HeatmapsOnImage(1.0 - heatmap, shape=images[1].shape) image1_w_overlay_c1, image1_w_overlay_c2 = \ heatmap1.draw_on_image(images[0]) image2_w_overlay_c1, image2_w_overlay_c2 = \ heatmap2.draw_on_image(images[1]) debug_image = iaa.draw_debug_image(images, heatmaps=[heatmap1, heatmap2]) assert self._image_contains(images[0, ...], debug_image) assert self._image_contains(images[1, ...], debug_image) assert self._image_contains(image1_w_overlay_c1, debug_image) assert self._image_contains(image1_w_overlay_c2, debug_image) assert self._image_contains(image2_w_overlay_c1, debug_image) assert self._image_contains(image2_w_overlay_c2, debug_image)
def test_HeatmapsOnImage_scale(): heatmaps_arr = np.float32([[0.0, 1.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(4, 4, 3)) heatmaps_scaled = heatmaps.resize((4, 4), interpolation="nearest") assert heatmaps_scaled.arr_0to1.shape == (4, 4, 1) assert heatmaps_scaled.arr_0to1.dtype.type == np.float32 assert np.allclose( heatmaps_scaled.arr_0to1[:, :, 0], np.float32([[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]])) heatmaps_arr = np.float32([[0.0, 1.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(4, 4, 3)) heatmaps_scaled = heatmaps.resize(2.0, interpolation="nearest") assert heatmaps_scaled.arr_0to1.shape == (2, 4, 1) assert heatmaps_scaled.arr_0to1.dtype.type == np.float32 assert np.allclose( heatmaps_scaled.arr_0to1[:, :, 0], np.float32([[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]]))
def apply_weight(self, weight): if self.img_type == 'numpy': weight = 255 * weight elif self.img_type == 'pil': weight = numpy_to_pil(255 * pil_to_numpy(weight)) elif self.img_type == 'imgaug': weight = ia.HeatmapsOnImage(255 * weight.get_arr(), weight.shape) elif self.img_type == 'tensor': weight = weight.mul(255) return weight
def test_one_image_float32_and_heatmap(self): rng = iarandom.RNG(0) image = rng.random(size=(256, 256, 3)).astype(np.float32) heatmap = np.zeros((256, 256, 1), dtype=np.float32) heatmap[128-25:128+25, 128-25:128+25] = 1.0 heatmap = ia.HeatmapsOnImage(heatmap, shape=image.shape) image1_w_overlay = heatmap.draw_on_image( (image*255).astype(np.uint8))[0] debug_image = iaa.draw_debug_image([image], heatmaps=[heatmap]) assert self._image_contains((image * 255).astype(np.uint8), debug_image) assert self._image_contains(image1_w_overlay, debug_image)
def apply_weight(self, weight): if self.img_type == 'numpy': weight = weight / 255. elif self.img_type == 'pil': if self.validate_pil(weight): weight = numpy_to_pil(pil_to_numpy(weight) / 255.) elif self.img_type == 'imgaug': weight = ia.HeatmapsOnImage(weight.get_arr() / 255., weight.shape) elif self.img_type == 'tensor': weight = weight.div(255.) return weight
def test_fill_from_augmented_normalized_batch(self): batch = ia.UnnormalizedBatch( images=np.zeros((1, 2, 2, 3), dtype=np.uint8), heatmaps=[np.zeros((2, 2, 1), dtype=np.float32)], segmentation_maps=[np.zeros((2, 2, 1), dtype=np.int32)], keypoints=[[(0, 0)]], bounding_boxes=[[ia.BoundingBox(0, 0, 1, 1)]], polygons=[[ia.Polygon([(0, 0), (1, 0), (1, 1)])]], line_strings=[[ia.LineString([(0, 0), (1, 0)])]]) batch_norm = ia.Batch( images=np.zeros((1, 2, 2, 3), dtype=np.uint8), heatmaps=[ ia.HeatmapsOnImage(np.zeros((2, 2, 1), dtype=np.float32), shape=(2, 2, 3)) ], segmentation_maps=[ ia.SegmentationMapsOnImage(np.zeros((2, 2, 1), dtype=np.int32), shape=(2, 2, 3)) ], keypoints=[ ia.KeypointsOnImage([ia.Keypoint(0, 0)], shape=(2, 2, 3)) ], bounding_boxes=[ ia.BoundingBoxesOnImage([ia.BoundingBox(0, 0, 1, 1)], shape=(2, 2, 3)) ], polygons=[ ia.PolygonsOnImage([ia.Polygon([(0, 0), (1, 0), (1, 1)])], shape=(2, 2, 3)) ], line_strings=[ ia.LineStringsOnImage([ia.LineString([(0, 0), (1, 0)])], shape=(2, 2, 3)) ]) batch_norm.images_aug = batch_norm.images_unaug batch_norm.heatmaps_aug = batch_norm.heatmaps_unaug batch_norm.segmentation_maps_aug = batch_norm.segmentation_maps_unaug batch_norm.keypoints_aug = batch_norm.keypoints_unaug batch_norm.bounding_boxes_aug = batch_norm.bounding_boxes_unaug batch_norm.polygons_aug = batch_norm.polygons_unaug batch_norm.line_strings_aug = batch_norm.line_strings_unaug batch = batch.fill_from_augmented_normalized_batch(batch_norm) assert batch.images_aug.shape == (1, 2, 2, 3) assert ia.is_np_array(batch.heatmaps_aug[0]) assert ia.is_np_array(batch.segmentation_maps_aug[0]) assert batch.keypoints_aug[0][0] == (0, 0) assert batch.bounding_boxes_aug[0][0].x1 == 0 assert batch.polygons_aug[0][0].exterior[0][0] == 0 assert batch.line_strings_aug[0][0].coords[0][0] == 0
def numpy_to_imgaug(img, is_label=False, is_weight=False, img_shape=None): if img_shape is None: img_shape = img.shape if is_label: if img.dtype != 'uint8': img = img.astype(np.uint8) return ia.SegmentationMapsOnImage(img, img_shape) elif is_weight: return ia.HeatmapsOnImage(img, img_shape) else: #Assume uint8 numpy array return img
def test_use_size_arg_with_different_shape_than_heatmap_arr_shape(self): # size differs from heatmap array size heatmaps_arr = np.float32([[0.0, 1.0], [0.0, 1.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(2, 2, 3)) heatmaps_drawn = heatmaps.draw(size=(4, 4))[0] assert heatmaps_drawn.shape == (4, 4, 3) v1 = heatmaps_drawn[0, 0] v2 = heatmaps_drawn[0, -1] for y in sm.xrange(4): for x in sm.xrange(2): assert np.allclose(heatmaps_drawn[y, x], v1) for y in sm.xrange(4): for x in sm.xrange(2, 4): assert np.allclose(heatmaps_drawn[y, x], v2)
def test_segmentation_prediction(self, i_file: str, o_file: str): self.ta = self.cfg.transformAugmentor() i_content = imageio.imread(o_file) ai = self.ta.augment_image(i_content) data = np.array([ai]) res = self.keras_model.predict(data) map = imgaug.SegmentationMapOnImage(res[0], res[0].shape) scaledMap = imgaug.augmenters.Scale({ "height": i_content.shape[0], "width": i_content.shape[1] }).augment_segmentation_maps([map]) imageio.imwrite( i_file, imgaug.HeatmapsOnImage( scaledMap[0].arr, scaledMap[0].arr.shape).draw_on_image(i_content)[0])
def test_to_batch_in_augmentation__all_columns(self): batch = ia.Batch( images=np.zeros((1, 2, 2, 3), dtype=np.uint8), heatmaps=[ ia.HeatmapsOnImage(np.zeros((2, 2, 1), dtype=np.float32), shape=(2, 2, 3)) ], segmentation_maps=[ ia.SegmentationMapsOnImage(np.zeros((2, 2, 1), dtype=np.int32), shape=(2, 2, 3)) ], keypoints=[ ia.KeypointsOnImage([ia.Keypoint(x=0, y=0)], shape=(2, 2, 3)) ], bounding_boxes=[ ia.BoundingBoxesOnImage([ia.BoundingBox(0, 0, 1, 1)], shape=(2, 2, 3)) ], polygons=[ ia.PolygonsOnImage([ia.Polygon([(0, 0), (1, 0), (1, 1)])], shape=(2, 2, 3)) ], line_strings=[ ia.LineStringsOnImage([ia.LineString([(0, 0), (1, 0)])], shape=(2, 2, 3)) ]) batch_inaug = batch.to_batch_in_augmentation() assert isinstance(batch_inaug, ia.BatchInAugmentation) assert ia.is_np_array(batch_inaug.images) assert batch_inaug.images.shape == (1, 2, 2, 3) assert isinstance(batch_inaug.heatmaps[0], ia.HeatmapsOnImage) assert isinstance(batch_inaug.segmentation_maps[0], ia.SegmentationMapsOnImage) assert isinstance(batch_inaug.keypoints[0], ia.KeypointsOnImage) assert isinstance(batch_inaug.bounding_boxes[0], ia.BoundingBoxesOnImage) assert isinstance(batch_inaug.polygons[0], ia.PolygonsOnImage) assert isinstance(batch_inaug.line_strings[0], ia.LineStringsOnImage) assert batch_inaug.get_column_names() == [ "images", "heatmaps", "segmentation_maps", "keypoints", "bounding_boxes", "polygons", "line_strings" ]
def process_face(image_path, label_path, augment=True): img = cv2.imread(image_path) parts_xy = load_parts_xy(label_path) xmin = np.min(parts_xy[:, 0]) xmax = np.max(parts_xy[:, 0]) ymin = np.min(parts_xy[:, 1]) ymax = np.max(parts_xy[:, 1]) margin_p = 0.2 margin = (xmax - xmin) * margin_p xmin = max(int(xmin - margin / 2), 0) xmax = max(int(xmax + margin / 2), 0) ymin = max(int(ymin - margin / 2), 0) ymax = max(int(ymax + margin / 2), 0) parts_xy[:, 0] -= xmin parts_xy[:, 1] -= ymin face = img[ymin:ymax, xmin:xmax] try: face, parts_xy = resize_crop(face, parts_xy) except: print("ymin:ymax xmin:xmax = %d:%d %d:%d" % (ymin, ymax, xmin, xmax)) raise face = face[:, :, ::-1] gtmap = generate_gtmap(parts_xy, sigma=2., outres=HM_DIM) if augment: seq_det = seq.to_deterministic( ) # call this for each batch again, NOT only once at the start face = seq_det.augment_image(face) gtmap = seq_det.augment_heatmaps( ia.HeatmapsOnImage(gtmap, shape=face.shape)).get_arr() face = face / 255. gtmaps = [gtmap.copy() for i in range(HG_STACK)] return face, gtmaps, image_path
def example_visualize_augmented_non_image_data(): print("Example: Visualize Augmented Non-Image Data") import numpy as np import imgaug as ia image = np.zeros((64, 64, 3), dtype=np.uint8) # points kps = [ia.Keypoint(x=10.5, y=20.5), ia.Keypoint(x=60.5, y=60.5)] kpsoi = ia.KeypointsOnImage(kps, shape=image.shape) image_with_kps = kpsoi.draw_on_image(image, size=7, color=(0, 0, 255)) ia.imshow(image_with_kps) # bbs bbsoi = ia.BoundingBoxesOnImage( [ia.BoundingBox(x1=10.5, y1=20.5, x2=50.5, y2=30.5)], shape=image.shape) image_with_bbs = bbsoi.draw_on_image(image) image_with_bbs = ia.BoundingBox(x1=50.5, y1=10.5, x2=100.5, y2=16.5).draw_on_image(image_with_bbs, color=(255, 0, 0), size=3) ia.imshow(image_with_bbs) # polygons psoi = ia.PolygonsOnImage( [ia.Polygon([(10.5, 20.5), (50.5, 30.5), (10.5, 50.5)])], shape=image.shape) image_with_polys = psoi.draw_on_image(image, alpha_points=0, alpha_face=0.5, color_lines=(255, 0, 0)) ia.imshow(image_with_polys) # heatmaps # pick first result via [0] here, because one image per heatmap channel # is generated hms = ia.HeatmapsOnImage(np.random.random(size=(32, 32, 1)).astype(np.float32), shape=image.shape) image_with_hms = hms.draw_on_image(image)[0] ia.imshow(image_with_hms)
def test_get_rowwise_shapes__nonimages(self): heatmaps = [ ia.HeatmapsOnImage(np.zeros((1, 2, 1), dtype=np.float32), shape=(1, 2, 3)) ] segmaps = [ ia.SegmentationMapsOnImage(np.zeros((1, 2, 1), dtype=np.int32), shape=(1, 2, 3)) ] keypoints = [ia.KeypointsOnImage([ia.Keypoint(0, 0)], shape=(1, 2, 3))] bounding_boxes = [ ia.BoundingBoxesOnImage([ia.BoundingBox(0, 1, 2, 3)], shape=(1, 2, 3)) ] polygons = [ ia.PolygonsOnImage([ia.Polygon([(0, 0), (1, 0), (1, 1)])], shape=(1, 2, 3)) ] line_strings = [ ia.LineStringsOnImage([ia.LineString([(0, 0), (1, 0)])], shape=(1, 2, 3)) ] kwargs = [{ "heatmaps": heatmaps }, { "segmentation_maps": segmaps }, { "keypoints": keypoints }, { "bounding_boxes": bounding_boxes }, { "polygons": polygons }, { "line_strings": line_strings }] for kwargs_i in kwargs: batch = ia.BatchInAugmentation(**kwargs_i) shapes = batch.get_rowwise_shapes() assert shapes == [(1, 2, 3)]