Beispiel #1
0
 def __init__(self, cfg, logger):
     super(AugmentBuilder, self).__init__(cfg)
     self.logger = logger
     self._default_transform_train = iaa.Sequential([iaa.Identity()])
     self._default_transform_val = iaa.Sequential([iaa.Identity()])
     self.normalize = True
     self._use_default_train = False
     self._use_default_val = False
 def __init__(self, cfg):
     self.cfg = cfg
     self._default_transform_train = iaa.Sequential([iaa.Identity()])
     self._default_transform_val = iaa.Sequential([iaa.Identity()])
     self.normalize = True
     self._use_default_train = False
     self._use_default_val = False
     self.train_aug_cfg = self.cfg.get('AUGMENT')['train']
     self.val_aug_cfg = self.cfg.get('AUGMENT')['val']
Beispiel #3
0
    def test_processes(self):
        augseq = iaa.Identity()
        mock_Pool = mock.MagicMock()
        mock_cpu_count = mock.Mock()

        patch_pool = mock.patch("multiprocessing.Pool", mock_Pool)
        patch_cpu_count = mock.patch("multiprocessing.cpu_count",
                                     mock_cpu_count)
        with patch_pool, patch_cpu_count:
            # (cpu cores available, processes requested, processes started)
            combos = [(1, 1, 1), (2, 1, 1), (3, 1, 1), (1, 2, 2), (3, 2, 2),
                      (1, None, None), (2, None, None), (3, None, None),
                      (1, -1, 1), (2, -1, 1), (3, -1, 2), (4, -2, 2)]

            for cores_available, processes_req, expected in combos:
                with self.subTest(cpu_count_available=cores_available,
                                  processes_requested=processes_req):
                    mock_cpu_count.return_value = cores_available
                    with multicore.Pool(augseq,
                                        processes=processes_req) as _pool:
                        pass

                    if expected is None:
                        assert mock_Pool.call_args[0][0] is None
                    else:
                        assert mock_Pool.call_args[0][0] == expected
Beispiel #4
0
    def test_propagation_hooks_ctx(self):
        def propagator(images, augmenter, parents, default):
            if ia.is_np_array(images):
                return False
            else:
                return True

        hooks = ia.HooksImages(propagator=propagator)

        batch = ia.BatchInAugmentation(
            images=np.zeros((3, 3, 4, 1), dtype=np.uint8),
            keypoints=[
                ia.KeypointsOnImage([ia.Keypoint(0, 0)], shape=(3, 4, 1)),
                ia.KeypointsOnImage([ia.Keypoint(1, 1)], shape=(3, 4, 1)),
                ia.KeypointsOnImage([ia.Keypoint(2, 2)], shape=(3, 4, 1))
            ])

        with batch.propagation_hooks_ctx(iaa.Identity(), hooks, []) \
                as batch_prop:
            assert batch_prop.images is None
            assert batch_prop.keypoints is not None
            assert len(batch_prop.keypoints) == 3

            batch_prop.keypoints[0].keypoints[0].x = 10

        assert batch.images is not None
        assert batch.keypoints is not None
        assert batch.keypoints[0].keypoints[0].x == 10
Beispiel #5
0
def data_aug(images):

    seq = iaa.Sometimes(
        0.5, iaa.Identity(),
        iaa.Sometimes(
            0.5,
            iaa.Sequential([
                iaa.Sometimes(
                    0.5,
                    iaa.OneOf([
                        iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255)),
                        iaa.AdditiveLaplaceNoise(scale=(0, 0.1 * 255)),
                        iaa.ReplaceElementwise(0.03, [0, 255]),
                        iaa.GaussianBlur(sigma=(0.0, 3.0)),
                        iaa.BilateralBlur(d=(3, 10),
                                          sigma_color=(10, 250),
                                          sigma_space=(10, 250))
                    ])),
                iaa.OneOf([
                    iaa.Add((-40, 40)),
                    iaa.AddElementwise((-20, 20)),
                    iaa.pillike.EnhanceBrightness()
                ]),
                iaa.OneOf([
                    iaa.GammaContrast((0.2, 2.0)),
                    iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)),
                    iaa.LogContrast(gain=(0.6, 1.4)),
                    iaa.AllChannelsCLAHE(),
                    iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)),
                ])
            ])))

    images = seq(images=images)

    return images
def main():
    def z(shape):
        return np.zeros(shape, dtype=np.uint8)

    seq = iaa.Identity()

    print("This should generate NO warning:")
    _image_aug = seq.augment_images(z((1, 16, 16, 3)))

    print("This should generate NO warning:")
    _image_aug = seq.augment_images(z((16, 16, 8)))

    print("This should generate NO warning:")
    _image_aug = seq.augment_images([z((16, 16, 3))])

    print("This should generate NO warning:")
    _image_aug = seq.augment_images([z((16, 16))])

    print("This should generate a warning:")
    _image_aug = seq.augment_images(z((16, 16, 3)))

    print("This should generate a warning:")
    for _ in range(2):
        _image_aug = seq.augment_images(z((16, 16, 1)))

    print("This should fail:")
    _image_aug = seq.augment_images(z((16, 16)))
Beispiel #7
0
    def test_property_pool(self):
        mock_Pool = mock.MagicMock()
        mock_Pool.return_value = mock_Pool
        mock_Pool.close.return_value = None
        mock_Pool.join.return_value = None

        # We cannot just mock multiprocessing.Pool here, because of using
        # a custom context. We would have to mock each possible context's
        # Pool() method or overwrite here the Pool() method of the
        # actually used context.
        with mock.patch("multiprocessing.pool.Pool", mock_Pool):
            augseq = iaa.Identity()
            pool_config = multicore.Pool(augseq,
                                         processes=1,
                                         maxtasksperchild=4,
                                         seed=123)
            with pool_config as pool:
                assert pool.processes == 1
            assert pool._pool is None
        assert mock_Pool.call_count == 1
        assert mock_Pool.close.call_count == 1
        assert mock_Pool.join.call_count == 1
        # see
        # https://github.com/
        # python/cpython/blob/master/Lib/multiprocessing/context.py
        # L119 (method Pool()) for an example of how Pool() is called
        # internally.
        assert mock_Pool.call_args[0][0] == 1  # processes
        assert mock_Pool.call_args[0][1] is multicore._Pool_initialize_worker
        assert mock_Pool.call_args[0][2] == (augseq, 123)
        assert mock_Pool.call_args[0][3] == 4
Beispiel #8
0
    def test_cpu_count_does_not_exist(self, mock_pool):
        def _side_effect():
            raise NotImplementedError

        old_method = multicore._get_context().cpu_count
        mock_cpu_count = mock.Mock()
        mock_cpu_count.side_effect = _side_effect
        multicore._get_context().cpu_count = mock_cpu_count

        augseq = iaa.Identity()
        with warnings.catch_warnings(record=True) as caught_warnings:
            warnings.simplefilter("always")
            with multicore.Pool(augseq, processes=-1):
                pass

        assert mock_cpu_count.call_count == 1
        assert mock_pool.call_count == 1
        # 'processes' arg to Pool was expected to be set to None as cpu_count
        # produced an error
        assert mock_pool.call_args_list[0][0][0] is None

        assert len(caught_warnings) == 1
        assert ("Could not find method multiprocessing.cpu_count(). "
                in str(caught_warnings[-1].message))

        multicore._get_context().cpu_count = old_method
Beispiel #9
0
    def __call__(self, input_dict : {str : Union[Image.Image, np.ndarray]}) -> dict:
        image, label = input_dict[tag_image], input_dict[tag_label]
        image = np.array(image)
        label = np.array(label)

        # size measure
        y_max = image.shape[0]
        x_max = image.shape[1]

        # np.ndarray -> imgaug.augmentables.segmaps.SegmentationMapsOnImage
        label = SegmentationMapsOnImage(label, shape=image.shape)

        # augmentation
        zoomset = iaa.OneOf([
            iaa.Identity(),  # do nothing
            iaa.Affine(scale=self.outscale),  # zoom out
            RandomCrop(y_max, x_max).cut()  # zoom in
        ])

        image, label = zoomset(image=image, segmentation_maps=label)
        image, label = self.transformSet(image=image, segmentation_maps=label)

        # imgaug.augmentables.segmaps.SegmentationMapsOnImage -> np.ndarray
        label = label.get_arr()

        return {tag_image : image,
                tag_label : label}
    def __init__(self, root, transforms=iaa.Identity()):
        self.root = root
        self.transforms = transforms

        # load all image files
        self.ann_file = os.path.join(root, 'labels.csv')
        self.ann = load_labels(self.ann_file)
        self.n_imgs = len(self.ann)
Beispiel #11
0
    def __init__(self, train_file, class_list, batch_size=1, shuffle=True, transform=None, augment = False, crops_per_snapshot = 8, crop_size = (512,512), debug=False):

        self.train_file = train_file
        self.class_list = class_list
        self.transform = transform

        #region parse the provided class file
        try:
            with open(self.class_list, 'r', newline='') as file:
                self.classes = self.load_classes(csv.reader(file, delimiter=','))
        except ValueError as e:
            raise (ValueError('invalid CSV class file: {}: {}'.format(self.class_list, e)))

        self.labels = {}
        for key, value in self.classes.items():
            self.labels[value] = key
        #endregion

        #region parse annotations csv file
        # lines should be of format "img_path, x1, y1, x2, y2, x3, y3, class_name"
        try:
            with open(self.train_file, 'r', newline='') as file:
                self.image_data = self._read_annotations(csv.reader(file, delimiter=','), self.classes)
        except ValueError as e:
            raise (ValueError('invalid CSV annotations file: {}: {}'.format(self.train_file, e)))
        #endregion

        self.image_names = list(self.image_data.keys())
        self.obj_indices = np.arange(len(self.image_names))

        self.batch_size = batch_size
        self.shuffle = shuffle
        self.augment = augment
        self.debug = debug
        self.lock = threading.Lock()  # mutex for input path
        self.yield_lock = threading.Lock()  # mutex for generator yielding of batch
        self.init_count = 0
        self.objects_id_generator = threadsafe_iter(get_objects_i_generator(len(self.image_names)))
        self.crops_per_snapshot = crops_per_snapshot
        self.crop_size = crop_size
        self.cache = {}

        if self.augment:
            self.seq = iaa.Sequential([iaa.Fliplr(0.5),
                                       iaa.Flipud(0.5),
                                       iaa.GaussianBlur(sigma=(0, 5)),
                                       iaa.Affine(rotate=(-180, 180),
                                                  translate_percent={'x': (-0.1, 0.1), 'y': (-0.1, 0.1)},
                                                  shear={'x': (-15, 15), 'y': (-15, 15)})],
                                      random_order=True)
            self.crop = iaa.CropToFixedSize(self.crop_size[0], self.crop_size[1])
            self.crop_augmentations = iaa.Sequential([iaa.Cutout(nb_iterations=(1, 3), size=0.3, squared=False, cval=0),
                                                      iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25))],
                                                     random_order=True)
        else:
            self.seq = iaa.Identity()
Beispiel #12
0
    def test_warns_that_it_is_deprecated(self):
        children_fg = iaa.Identity()

        with warnings.catch_warnings(record=True) as caught_warnings:
            warnings.simplefilter("always")
            _ = overlay.SimplexNoiseAlpha(children_fg)

        assert len(caught_warnings) == 1
        assert ("imgaug.augmenters.blend.SimplexNoiseAlpha"
                in str(caught_warnings[-1].message))
Beispiel #13
0
    def test_warns_that_it_is_deprecated(self):
        children_fg = iaa.Identity()

        with warnings.catch_warnings(record=True) as caught_warnings:
            warnings.simplefilter("always")
            _ = overlay.FrequencyNoiseAlpha(first=children_fg)

        assert len(caught_warnings) == 2
        assert ("imgaug.augmenters.blend.BlendAlphaFrequencyNoise"
                in str(caught_warnings[0].message))
def get_transform(train):
    transforms = iaa.Identity()
    if train:
        transforms = iaa.SomeOf((0, None), [
            iaa.Affine(scale=(1.0, 1.5)),
            iaa.PerspectiveTransform(scale=(0.01, 0.1)),
            iaa.Fliplr(),
            iaa.MotionBlur(k=(3, 7))
        ])
    return transforms
Beispiel #15
0
    def test_augment_images_worker(self):
        warnings.simplefilter("always")
        with warnings.catch_warnings(record=True) as caught_warnings:

            def gen():
                yield ia.Batch(images=np.zeros((1, 4, 4, 3), dtype=np.uint8))

            bl = multicore.BatchLoader(gen(), queue_size=2)
            bgaug = multicore.BackgroundAugmenter(bl,
                                                  iaa.Identity(),
                                                  queue_size=1,
                                                  nb_workers=1)

            queue_source = multiprocessing.Queue(2)
            queue_target = multiprocessing.Queue(2)
            queue_source.put(
                pickle.dumps(
                    ia.Batch(images=np.zeros((1, 4, 8, 3), dtype=np.uint8)),
                    protocol=-1))
            queue_source.put(pickle.dumps(None, protocol=-1))
            bgaug._augment_images_worker(iaa.Add(1), queue_source,
                                         queue_target, 1)

            batch_aug = pickle.loads(queue_target.get())
            assert isinstance(batch_aug, ia.Batch)
            assert batch_aug.images_unaug is not None
            assert batch_aug.images_unaug.dtype == np.uint8
            assert batch_aug.images_unaug.shape == (1, 4, 8, 3)
            assert np.array_equal(batch_aug.images_unaug,
                                  np.zeros((1, 4, 8, 3), dtype=np.uint8))
            assert batch_aug.images_aug is not None
            assert batch_aug.images_aug.dtype == np.uint8
            assert batch_aug.images_aug.shape == (1, 4, 8, 3)
            assert np.array_equal(batch_aug.images_aug,
                                  np.zeros((1, 4, 8, 3), dtype=np.uint8) + 1)

            finished_signal = pickle.loads(queue_target.get())
            assert finished_signal is None

            source_finished_signal = pickle.loads(queue_source.get())
            assert source_finished_signal is None

            assert queue_source.empty()
            assert queue_target.empty()

            queue_source.close()
            queue_target.close()
            queue_source.join_thread()
            queue_target.join_thread()
            bl.terminate()
            bgaug.terminate()

        assert len(caught_warnings) > 0
        for warning in caught_warnings:
            assert "is deprecated" in str(warning.message)
Beispiel #16
0
    def test_warns_that_it_is_deprecated(self):
        children_fg = iaa.Identity()
        factor = 1

        with warnings.catch_warnings(record=True) as caught_warnings:
            warnings.simplefilter("always")
            _ = overlay.AlphaElementwise(factor, children_fg)

        assert len(caught_warnings) == 2
        assert ("imgaug.augmenters.blend.BlendAlphaElementwise"
                in str(caught_warnings[0].message))
Beispiel #17
0
def data_aug2(image):

    seq = iaa.Sometimes(
        0.5, iaa.Identity(),
        iaa.OneOf([
            iaa.CoarseDropout((0.1, 0.2), size_percent=(0.01, 0.02)),
            iaa.CoarseSaltAndPepper(0.1, size_percent=(0.01, 0.02))
        ]))

    image = seq(image=image)

    return image
Beispiel #18
0
    def test_processes(self):
        augseq = iaa.Identity()
        mock_Pool = mock.MagicMock()
        mock_cpu_count = mock.Mock()

        # We cannot just mock multiprocessing.Pool here, because of using
        # a custom context. We would have to mock each possible context's
        # Pool() method or overwrite here the Pool() method of the
        # actually used context.
        patch_pool = mock.patch("multiprocessing.pool.Pool", mock_Pool)

        # Multiprocessing seems to always access os.cpu_count to get the
        # current count of cpu cores.
        # See
        # https://github.com/
        # python/cpython/blob/master/Lib/multiprocessing/context.py
        # L41.
        fname = ("os.cpu_count" if IS_SUPPORTING_CONTEXTS
                 else "multiprocessing.cpu_count")
        patch_cpu_count = mock.patch(fname, mock_cpu_count)

        with patch_pool, patch_cpu_count:
            # (cpu cores available, processes requested, processes started)
            combos = [
                (1, 1, 1),
                (2, 1, 1),
                (3, 1, 1),
                (1, 2, 2),
                (3, 2, 2),
                (1, None, None),
                (2, None, None),
                (3, None, None),
                (1, -1, 1),
                (2, -1, 1),
                (3, -1, 2),
                (4, -2, 2)
            ]

            for cores_available, processes_req, expected in combos:
                with self.subTest(cpu_count_available=cores_available,
                                  processes_requested=processes_req):
                    mock_cpu_count.return_value = cores_available
                    with multicore.Pool(augseq,
                                        processes=processes_req) as _pool:
                        pass

                    if expected is None:
                        assert mock_Pool.call_args[0][0] is None
                    else:
                        assert mock_Pool.call_args[0][0] == expected
    def __init__(self, root, transforms=iaa.Identity()):
        self.root = root
        self.transforms = transforms
        pattern = re.compile('img(.*).jpg')
        self.imgs = list()
        sequences = glob(os.path.join(root, "seq*"))
        for seq in sequences:
            seq_num = seq[-3::]
            img_files = glob(os.path.join(seq, "img*.jpg"))
            for img_f in img_files:

                img_num = pattern.search(os.path.basename(img_f)).group(1)
                self.imgs.append((img_f, seq_num, img_num))
        self.imgs.sort(key=lambda x: (int(x[1]), int(x[2])))
Beispiel #20
0
    def _test_imap_batches_both(cls, call_unordered):
        for clazz in [Batch, UnnormalizedBatch]:
            batches = [
                clazz(images=[ia.quokka()]),
                clazz(images=[ia.quokka() + 1])
            ]

            def _generate_batches():
                for batch in batches:
                    yield batch

            augseq = iaa.Identity()
            mock_Pool = mock.MagicMock()
            mock_Pool.return_value = mock_Pool
            mock_Pool.imap.return_value = batches
            mock_Pool.imap_unordered.return_value = batches
            with mock.patch("multiprocessing.pool.Pool", mock_Pool):
                with multicore.Pool(augseq, processes=1) as pool:
                    gen = _generate_batches()
                    if call_unordered:
                        _ = list(pool.imap_batches_unordered(gen))
                    else:
                        _ = list(pool.imap_batches(gen))

                if call_unordered:
                    to_check = mock_Pool.imap_unordered
                else:
                    to_check = mock_Pool.imap

                assert to_check.call_count == 1

                assert to_check.call_args[0][0] == multicore._Pool_starworker

                # convert generator to list, make it subscriptable
                arg_batches = list(to_check.call_args[0][1])

                # args, arg 1 (batches with ids), tuple 0,
                # entry 0 in tuple (=> batch id)
                assert arg_batches[0][0] == 0

                # tuple 0, entry 1 in tuple (=> batch)
                assert np.array_equal(arg_batches[0][1].images_unaug,
                                      batches[0].images_unaug)

                # tuple 1, entry 0 in tuple (=> batch id)
                assert arg_batches[1][0] == 1

                # tuple 1, entry 1 in tuple (=> batch)
                assert np.array_equal(arg_batches[1][1].images_unaug,
                                      batches[1].images_unaug)
Beispiel #21
0
    def test_inputs_not_lost(self):
        """Test to make sure that inputs (e.g. images) are never lost."""
        def _assert_contains_all_ids(batches_aug):
            # batch.images_unaug
            ids = set()
            for batch_aug in batches_aug:
                ids.add(int(batch_aug.images_unaug.flat[0]))
                ids.add(int(batch_aug.images_unaug.flat[1]))
            for idx in sm.xrange(2 * 100):
                assert idx in ids
            assert len(ids) == 200

            # batch.images_aug
            ids = set()
            for batch_aug in batches_aug:
                ids.add(int(batch_aug.images_aug.flat[0]))
                ids.add(int(batch_aug.images_aug.flat[1]))
            for idx in sm.xrange(2 * 100):
                assert idx in ids
            assert len(ids) == 200

        augseq = iaa.Identity()
        image = np.zeros((1, 1, 1), dtype=np.uint8)
        # creates batches containing images with ids from 0 to 199 (one pair
        # of consecutive ids per batch)
        batches = [
            ia.Batch(
                images=np.uint8([image + b_idx * 2, image + b_idx * 2 + 1]))
            for b_idx in sm.xrange(100)
        ]

        with multicore.Pool(augseq, processes=2, maxtasksperchild=25) as pool:
            batches_aug = pool.map_batches(batches)
            _assert_contains_all_ids(batches_aug)

        with multicore.Pool(augseq, processes=2, maxtasksperchild=25,
                            seed=1) as pool:
            batches_aug = pool.map_batches(batches)
            _assert_contains_all_ids(batches_aug)

        with multicore.Pool(augseq, processes=3, seed=2) as pool:
            batches_aug = pool.map_batches(batches)
            _assert_contains_all_ids(batches_aug)

        with multicore.Pool(augseq, processes=2, seed=None) as pool:
            batches_aug = pool.map_batches(batches)
            _assert_contains_all_ids(batches_aug)

            batches_aug = pool.map_batches(batches)
            _assert_contains_all_ids(batches_aug)
Beispiel #22
0
    def test_join_via_mock(self, mock_pool):
        # According to codecov, the join() does not get beyond its initial
        # if statement in the test_join() test, even though it should be.
        # Might be a simple travis multicore problem?
        # It is tested here again via some mocking.
        mock_pool.return_value = mock_pool
        mock_pool.join.return_value = True
        with multicore.Pool(iaa.Identity(), processes=2) as pool:
            pool.join()

            # Make sure that __exit__ does not call close(), which would then
            # call join() again and we would get a call_count of 2
            pool._pool = None

        assert mock_pool.join.call_count == 1
Beispiel #23
0
    def augstore(self, src:dict, dst_base:str,
                 dataname_extension='.tiff', labelname_extension='.tif',
                 identifier=None):

        os.makedirs(dst_base, exist_ok=True)
        os.makedirs(os.path.join(dst_base, label_folder_name), exist_ok=True)
        # get image
        image = src[tag_image] # PIL.Image.Image
        label = src[tag_label] # PIL.Image.Image
        name = src[tag_name] # str

        # PIL -> numpy
        image = np.array(image)
        label = np.array(label)

        # size measure
        y_max = image.shape[0]
        x_max = image.shape[1]

        # np.ndarray -> imgaug.augmentables.segmaps.SegmentationMapsOnImage
        label = SegmentationMapsOnImage(label, shape=label.shape)

        # augmentation
        zoomset = iaa.OneOf([
            iaa.Identity(),  # do nothing
            iaa.Affine(scale=self.outscale),  # zoom out
            RandomCrop(y_max, x_max).cut()  # zoom in
        ])
        image, label = zoomset(image=image, segmentation_maps=label)
        image, label = self.transformSet(image=image, segmentation_maps=label)

        # imgaug.augmentables.segmaps.SegmentationMapsOnImage -> np.ndarray
        label = label.get_arr()

        if not identifier == None:
            name = name + '_' + str(identifier)

        # numpy -> PIL.Image.Image
        image = Image.fromarray(image)
        label = Image.fromarray(label)

        image.save(os.path.join(dst_base, name + dataname_extension))
        label.save(os.path.join(dst_base, label_folder_name, name + labelname_extension))

        return {tag_image : image,
                tag_label : label,
                tag_name : name}
Beispiel #24
0
    def _test_map_batches_both(cls, call_async):
        for clazz in [Batch, UnnormalizedBatch]:
            augseq = iaa.Identity()
            mock_Pool = mock.MagicMock()
            mock_Pool.return_value = mock_Pool
            mock_Pool.map.return_value = "X"
            mock_Pool.map_async.return_value = "X"
            with mock.patch("multiprocessing.pool.Pool", mock_Pool):
                batches = [
                    clazz(images=[ia.quokka()]),
                    clazz(images=[ia.quokka() + 1])
                ]
                with multicore.Pool(augseq, processes=1) as pool:
                    if call_async:
                        _ = pool.map_batches_async(batches)
                    else:
                        _ = pool.map_batches(batches)

                if call_async:
                    to_check = mock_Pool.map_async
                else:
                    to_check = mock_Pool.map

                assert to_check.call_count == 1

                # args, arg 0
                assert to_check.call_args[0][0] == multicore._Pool_starworker

                # args, arg 1 (batches with ids), tuple 0,
                # entry 0 in tuple (=> batch id)
                assert to_check.call_args[0][1][0][0] == 0

                # args, arg 1 (batches with ids), tuple 0,
                # entry 1 in tuple (=> batch)
                assert np.array_equal(
                    to_check.call_args[0][1][0][1].images_unaug,
                    batches[0].images_unaug)

                # args, arg 1 (batches with ids), tuple 1,
                # entry 0 in tuple (=> batch id)
                assert to_check.call_args[0][1][1][0] == 1

                # args, arg 1 (batches with ids), tuple 1,
                # entry 1 in tuple (=> batch)
                assert np.array_equal(
                    to_check.call_args[0][1][1][1].images_unaug,
                    batches[1].images_unaug)
Beispiel #25
0
 def test_property_pool(self):
     mock_Pool = mock.MagicMock()
     mock_Pool.return_value = mock_Pool
     mock_Pool.close.return_value = None
     mock_Pool.join.return_value = None
     with mock.patch("multiprocessing.Pool", mock_Pool):
         augseq = iaa.Identity()
         pool_config = multicore.Pool(augseq,
                                      processes=1,
                                      maxtasksperchild=4,
                                      seed=123)
         with pool_config as pool:
             assert pool.processes == 1
         assert pool._pool is None
     assert mock_Pool.call_count == 1
     assert mock_Pool.close.call_count == 1
     assert mock_Pool.join.call_count == 1
     assert mock_Pool.call_args[0][0] == 1  # processes
     assert mock_Pool.call_args[1]["initargs"] == (augseq, 123)
     assert mock_Pool.call_args[1]["maxtasksperchild"] == 4
    def __call__(self, image):
        image = np.array(image)

        # size measure
        y_max = image.shape[0]
        x_max = image.shape[1]

        # augmentation
        zoomset = iaa.OneOf([
            iaa.Identity(),  # do nothing
            iaa.Affine(scale=self.outscale),  # zoom out
            RandomCrop(y_max, x_max).cut()  # zoom in
        ])

        image = zoomset(image=image)
        image = self.transformSet(image=image)

        image = Image.fromarray(image)

        return image
Beispiel #27
0
 def __init__(self):
     self.augmenter = iaa.OneOf([
         iaa.Identity(),
         iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.03 * 255)),
         iaa.Fliplr(1),
         iaa.Flipud(1),
         iaa.Rot90(k=1),
         iaa.Rot90(k=2),
         iaa.Rot90(k=3),
         iaa.Sequential([
             iaa.Flipud(1),
             iaa.Rot90(k=1),
         ]),
         iaa.Sequential([
             iaa.Flipud(1),
             iaa.Rot90(k=2),
         ]),
         iaa.Sequential([
             iaa.Flipud(1),
             iaa.Rot90(k=3),
         ])
     ])
def getImageAug():
    seq = iaa.Sequential(
        [
            iaa.SomeOf(
                (0, 2),
                [
                    iaa.Identity(),
                    iaa.AverageBlur(k=((3, 5), (5, 7))),
                    iaa.Rotate((-45, 45)),
                    iaa.Affine(scale=(0.5, 0.95)),
                    iaa.Multiply((0.50, 1.1))
                    #,iaa.BlendAlphaRegularGrid(nb_rows=(4, 6), nb_cols=(1, 4),
                    #                        foreground=iaa.Multiply(0.0))
                    #,iaa.Cartoon()
                    ,
                    iaa.Cutout(
                        nb_iterations=(1, 3), size=0.2, squared=False, cval=0),
                    iaa.Affine(shear=(-48, 48)),
                    iaa.Affine(translate_px={
                        "x": (-42, 42),
                        "y": (-36, 36)
                    }),
                    iaa.KeepSizeByResize(
                        iaa.Resize({
                            "height": (0.70, 0.90),
                            "width": (0.70, 0.90)
                        })),
                    iaa.CropAndPad(percent=(-0.2, 0.2))
                    #,iaa.PiecewiseAffine(scale=(0.01, 0.05))
                    ,
                    iaa.PerspectiveTransform(scale=(0.01, 0.1))
                    #,iaa.WithPolarWarping(iaa.CropAndPad(percent=(-0.1, 0.1)))
                    #,iaa.ElasticTransformation(alpha=(0, 3.0), sigma=0.5)
                ])
            #,iaa.SaveDebugImageEveryNBatches(folder_path, 100)
        ],
        random_order=True)
    return seq
Beispiel #29
0
def test_dtype_preservation():
    reseed()

    size = (4, 16, 16, 3)
    images = [
        np.random.uniform(0, 255, size).astype(np.uint8),
        np.random.uniform(0, 65535, size).astype(np.uint16),
        np.random.uniform(0, 4294967295, size).astype(np.uint32),
        np.random.uniform(-128, 127, size).astype(np.int16),
        np.random.uniform(-32768, 32767, size).astype(np.int32),
        np.random.uniform(0.0, 1.0, size).astype(np.float32),
        np.random.uniform(-1000.0, 1000.0, size).astype(np.float16),
        np.random.uniform(-1000.0, 1000.0, size).astype(np.float32),
        np.random.uniform(-1000.0, 1000.0, size).astype(np.float64)
    ]

    default_dtypes = set([arr.dtype for arr in images])
    # Some dtypes are here removed per augmenter, because the respective
    # augmenter does not support them. This test currently only checks whether
    # dtypes are preserved from in- to output for all dtypes that are supported
    # per augmenter.
    # dtypes are here removed via list comprehension instead of
    # `default_dtypes - set([dtype])`, because the latter one simply never
    # removed the dtype(s) for some reason

    def _not_dts(dts):
        return [dt for dt in default_dtypes if dt not in dts]

    augs = [
        (iaa.Add((-5, 5), name="Add"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.AddElementwise((-5, 5), name="AddElementwise"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.AdditiveGaussianNoise(0.01*255, name="AdditiveGaussianNoise"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Multiply((0.95, 1.05), name="Multiply"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Dropout(0.01, name="Dropout"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Invert(0.01, per_channel=True, name="Invert"),
         default_dtypes),
        (iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"),
         _not_dts([np.float16])),
        (iaa.AverageBlur((3, 5), name="AverageBlur"),
         _not_dts([np.uint32, np.int32, np.float16])),
        (iaa.MedianBlur((3, 5), name="MedianBlur"),
         _not_dts([np.uint32, np.int32, np.float16, np.float64])),
        (iaa.BilateralBlur((3, 5), name="BilateralBlur"),
         _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float16,
                   np.float64])),
        (iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0,
                                name="DirectedEdgeDetect"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.Fliplr(0.5, name="Fliplr"), default_dtypes),
        (iaa.Flipud(0.5, name="Flipud"), default_dtypes),
        (iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"),
         _not_dts([np.uint32, np.int32])),
        (iaa.Affine(translate_percent=(-0.05, 0.05),
                    name="Affine-translate-percent"),
         _not_dts([np.uint32, np.int32])),
        (iaa.Affine(rotate=(-20, 20), name="Affine-rotate"),
         _not_dts([np.uint32, np.int32])),
        (iaa.Affine(shear=(-20, 20), name="Affine-shear"),
         _not_dts([np.uint32, np.int32])),
        (iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"),
         _not_dts([np.uint32, np.int32])),
        (iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"),
         default_dtypes),
        (iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2),
                                   name="ElasticTransformation"),
         _not_dts([np.float16])),
        (iaa.Sequential([iaa.Identity(), iaa.Identity()],
                        name="SequentialNoop"),
         default_dtypes),
        (iaa.SomeOf(1, [iaa.Identity(), iaa.Identity()], name="SomeOfNoop"),
         default_dtypes),
        (iaa.OneOf([iaa.Identity(), iaa.Identity()], name="OneOfNoop"),
         default_dtypes),
        (iaa.Sometimes(0.5, iaa.Identity(), name="SometimesNoop"),
         default_dtypes),
        (iaa.Sequential([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))],
                        name="Sequential"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.SomeOf(1,
                    [iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))],
                    name="SomeOf"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.OneOf([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))],
                   name="OneOf"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Identity(name="Identity"), default_dtypes),
        (iaa.BlendAlpha((0.0, 0.1), iaa.Identity(), name="BlendAlphaIdentity"),
         _not_dts([np.float64])),  # float64 requires float128 support
        (iaa.BlendAlphaElementwise((0.0, 0.1), iaa.Identity(),
                                   name="BlendAlphaElementwiseIdentity"),
         _not_dts([np.float64])),  # float64 requires float128 support
        (iaa.BlendAlphaSimplexNoise(iaa.Identity(),
                                    name="BlendAlphaSimplexNoiseIdentity"),
         _not_dts([np.float64])),  # float64 requires float128 support
        (iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2),
                                      foreground=iaa.Identity(),
                                      name="BlendAlphaFrequencyNoiseIdentity"),
         _not_dts([np.float64])),
        (iaa.BlendAlpha((0.0, 0.1), iaa.Add(10), name="BlendAlpha"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.BlendAlphaElementwise((0.0, 0.1), iaa.Add(10),
                                   name="BlendAlphaElementwise"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.BlendAlphaSimplexNoise(iaa.Add(10), name="BlendAlphaSimplexNoise"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2),
                                      foreground=iaa.Add(10),
                                      name="BlendAlphaFrequencyNoise"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Superpixels(p_replace=0.01, n_segments=64),
         _not_dts([np.float16, np.float32, np.float64])),
        (iaa.Resize({"height": 4, "width": 4}, name="Resize"),
         _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32,
                   np.float16, np.float64])),
        (iaa.CropAndPad(px=(-10, 10), name="CropAndPad"),
         _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32,
                   np.float16, np.float64])),
        (iaa.Pad(px=(0, 10), name="Pad"),
         _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32,
                   np.float16, np.float64])),
        (iaa.Crop(px=(0, 10), name="Crop"),
         _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32,
                   np.float16, np.float64]))
    ]

    for (aug, allowed_dtypes) in augs:
        for images_i in images:
            if images_i.dtype in allowed_dtypes:
                images_aug = aug.augment_images(images_i)
                assert images_aug.dtype == images_i.dtype
Beispiel #30
0
def test_unusual_channel_numbers():
    reseed()

    images = [
        (0, create_random_images((4, 16, 16))),
        (1, create_random_images((4, 16, 16, 1))),
        (2, create_random_images((4, 16, 16, 2))),
        (4, create_random_images((4, 16, 16, 4))),
        (5, create_random_images((4, 16, 16, 5))),
        (10, create_random_images((4, 16, 16, 10))),
        (20, create_random_images((4, 16, 16, 20)))
    ]

    augs = [
        iaa.Add((-5, 5), name="Add"),
        iaa.AddElementwise((-5, 5), name="AddElementwise"),
        iaa.AdditiveGaussianNoise(0.01*255, name="AdditiveGaussianNoise"),
        iaa.Multiply((0.95, 1.05), name="Multiply"),
        iaa.Dropout(0.01, name="Dropout"),
        iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"),
        iaa.Invert(0.01, per_channel=True, name="Invert"),
        iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"),
        iaa.AverageBlur((3, 5), name="AverageBlur"),
        iaa.MedianBlur((3, 5), name="MedianBlur"),
        iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"),
        iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"),
        iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"),
        iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0,
                               name="DirectedEdgeDetect"),
        iaa.Fliplr(0.5, name="Fliplr"),
        iaa.Flipud(0.5, name="Flipud"),
        iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"),
        iaa.Affine(translate_percent=(-0.05, 0.05),
                   name="Affine-translate-percent"),
        iaa.Affine(rotate=(-20, 20), name="Affine-rotate"),
        iaa.Affine(shear=(-20, 20), name="Affine-shear"),
        iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"),
        iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"),
        iaa.PerspectiveTransform(scale=(0.01, 0.10),
                                 name="PerspectiveTransform"),
        iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2),
                                  name="ElasticTransformation"),
        iaa.Sequential([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]),
        iaa.SomeOf(1, [iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]),
        iaa.OneOf([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]),
        iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"),
        iaa.Identity(name="Noop"),
        iaa.BlendAlpha((0.0, 0.1), iaa.Add(10), name="BlendAlpha"),
        iaa.BlendAlphaElementwise((0.0, 0.1), iaa.Add(10),
                                  name="BlendAlphaElementwise"),
        iaa.BlendAlphaSimplexNoise(iaa.Add(10), name="BlendAlphaSimplexNoise"),
        iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2),
                                     foreground=iaa.Add(10),
                                     name="BlendAlphaSimplexNoise"),
        iaa.Superpixels(p_replace=0.01, n_segments=64),
        iaa.Resize({"height": 4, "width": 4}, name="Resize"),
        iaa.CropAndPad(px=(-10, 10), name="CropAndPad"),
        iaa.Pad(px=(0, 10), name="Pad"),
        iaa.Crop(px=(0, 10), name="Crop")
    ]

    for aug in augs:
        for (nb_channels, images_c) in images:
            if aug.name != "Resize":
                images_aug = aug.augment_images(images_c)
                assert images_aug.shape == images_c.shape
                image_aug = aug.augment_image(images_c[0])
                assert image_aug.shape == images_c[0].shape
            else:
                images_aug = aug.augment_images(images_c)
                image_aug = aug.augment_image(images_c[0])
                if images_c.ndim == 3:
                    assert images_aug.shape == (4, 4, 4)
                    assert image_aug.shape == (4, 4)
                else:
                    assert images_aug.shape == (4, 4, 4, images_c.shape[3])
                    assert image_aug.shape == (4, 4, images_c.shape[3])