Пример #1
0
    def test__generate_class_to_label_mapper__produces_same_results_with_different_ordering_for_binary(self):
        classes_a = ['a', 'b']
        classes_b = ['b', 'a']

        npt.assert_equal(
            GeneratorUtils.generate_class_to_label_mapper(classes_a, 'binary'),
            GeneratorUtils.generate_class_to_label_mapper(classes_b, 'binary')
        )
Пример #2
0
    def test__process_img__returns_img_in_specified_type(self):
        # Default
        img_path = Path('fake_dataset/1/1.png')
        img = GeneratorUtils.process_img(img_path, (10, 10))

        self.assertEqual(img.dtype, np.uint8)

        # Not default
        img_path = Path('fake_dataset/1/1.png')
        img = GeneratorUtils.process_img(img_path, (10, 10), dtype=np.uint32)

        self.assertEqual(img.dtype, np.uint32)
    def test__process_sample__without_transformations(self):
        nb_frames = 3
        frame_size = (20, 20)

        img_paths = GeneratorUtils.pick_at_intervals(
            GeneratorUtils.get_sample_images(self.fake_sample), nb_frames)
        expected = [
            GeneratorUtils.process_img(img_path, frame_size)
            for img_path in img_paths
        ]

        actual = process_sample(self.fake_sample, nb_frames, frame_size)

        npt.assert_equal(desired=expected, actual=actual)
def process_sample(sample: Path,
                   nb_frames: int,
                   frame_size: Tuple[int, int],
                   dtype=np.uint8,
                   transformations: list = None):
    img_paths = GeneratorUtils.pick_at_intervals(
        GeneratorUtils.get_sample_images(sample),
        nb_frames)  # Add third param - random round op

    img_arrays = [GeneratorUtils.process_img(img_path, frame_size, dtype)
                  for img_path in img_paths]

    if transformations is None:
        return img_arrays
    else:
        return GeneratorUtils.augment(img_arrays, transformations)
Пример #5
0
    def test__generate_class_to_label_mapper__when_strategy_is_binary_with_2_classes(self):
        classes = ['a', 'b']

        actual = GeneratorUtils.generate_class_to_label_mapper(
            classes, 'binary')

        npt.assert_equal(desired={'a': 0, 'b': 1}, actual=actual)
Пример #6
0
    def test__augment_imgs__transforms_imgs(self):
        sample_path = Path('fake_dataset/1')
        transformations = [A.HorizontalFlip(p=1)]
        transform = A.Compose(
            transformations,
            additional_targets={f'image{i}': 'image' for i in range(4)})
        imgs = GeneratorUtils.get_sample_images(sample_path)
        imgs = [GeneratorUtils.process_img(img) for img in imgs]
        expected = transform(image=imgs[0], **{f'image{i}': img for i, img in enumerate(imgs[1:])})

        actual = GeneratorUtils.augment(imgs, transformations)

        npt.assert_equal(expected['image'], actual[0])
        npt.assert_equal(expected['image0'], actual[1])
        npt.assert_equal(expected['image1'], actual[2])
        npt.assert_equal(expected['image2'], actual[3])
        npt.assert_equal(expected['image3'], actual[4])
    def test__pandas_generator__labels_for_categorical_labels(self):
        gen = PandasGenerator(self.source,
                              self.data_path)

        classes = sorted(self.source.iloc[:, 1].unique())
        expected = GeneratorUtils.generate_class_to_label_mapper(classes, 'categorical')

        npt.assert_equal(expected, gen.class_label_map)
Пример #8
0
    def test__generate_class_to_label_mapper__when_strategy_is_categorical(self):
        classes = ['a', 'b', 'c']
        labels = to_categorical(np.arange(len(classes)))
        class_to_label_map = dict(zip(classes, labels))

        actual = GeneratorUtils.generate_class_to_label_mapper(
            classes, 'categorical')

        npt.assert_equal(class_to_label_map, actual)
    def test__process_sample__with_transformations(self):
        nb_frames = 3
        frame_size = (20, 20)
        transformations = [A.HorizontalFlip(p=1)]

        img_paths = GeneratorUtils.pick_at_intervals(
            GeneratorUtils.get_sample_images(self.fake_sample), nb_frames)
        img_arrays = [
            GeneratorUtils.process_img(img_path, frame_size)
            for img_path in img_paths
        ]
        expected = GeneratorUtils.augment(img_arrays, transformations)

        actual = process_sample(self.fake_sample,
                                nb_frames,
                                frame_size,
                                transformations=transformations)

        npt.assert_equal(desired=expected, actual=actual)
Пример #10
0
    def test__get_sample_images(self):
        sample_path = Path('fake_dataset/1')
        expected = np.array([
            Path('fake_dataset/1/1.png'),
            Path('fake_dataset/1/2.png'),
            Path('fake_dataset/1/3.png'),
            Path('fake_dataset/1/4.png'),
            Path('fake_dataset/1/5.png')])
        actual = GeneratorUtils.get_sample_images(sample_path)

        npt.assert_equal(actual, expected)
    def test__pandas_generator__labels_for_binary_labels(self):
        source = pd.DataFrame([[1, 'a'], [2, 'b']])

        gen = PandasGenerator(source,
                              self.data_path,
                              labelling_strategy='binary')

        classes = sorted(source.iloc[:, 1].unique())
        expected = GeneratorUtils.generate_class_to_label_mapper(classes, 'binary')

        npt.assert_equal(expected, gen.class_label_map)
    def test__pandas_generator__labels(self):
        classes = self.source.loc[:, 1].unique()
        class_label_map = GeneratorUtils.generate_class_to_label_mapper(
            classes, 'categorical')
        expected = list(map(lambda c: class_label_map[c], self.source.iloc[:, 1].values))

        gen = PandasGenerator(self.source,
                              self.data_path,
                              nb_frames=5,
                              batch_size=self.nb_samples)

        _, labels = gen.__getitem__(0)
        npt.assert_equal(actual=labels, desired=expected)
Пример #13
0
    def test__augment_imgs__maintains_order(self):
        sample_path = Path('fake_dataset/1')
        transformations = [A.HorizontalFlip(p=1)]
        transform = A.Compose(transformations)
        imgs = GeneratorUtils.get_sample_images(sample_path)
        imgs = [GeneratorUtils.process_img(img) for img in imgs]

        actual = GeneratorUtils.augment(imgs, transformations)

        transformed = transform(image=imgs[0])
        self.assertTrue(np.array_equal(transformed['image'], actual[0]))

        transformed = transform(image=imgs[1])
        self.assertTrue(np.array_equal(transformed['image'], actual[1]))

        transformed = transform(image=imgs[2])
        self.assertTrue(np.array_equal(transformed['image'], actual[2]))

        transformed = transform(image=imgs[3])
        self.assertTrue(np.array_equal(transformed['image'], actual[3]))

        transformed = transform(image=imgs[4])
        self.assertTrue(np.array_equal(transformed['image'], actual[4]))
    def test__pandas_generator__yields_sample_images_correctly(self):
        transformations = [A.HorizontalFlip(p=1)]
        nb_frames = 5
        batch_size = self.nb_samples
        frame_size = (10, 10)
        gen = PandasGenerator(self.source,
                              self.data_path,
                              batch_size=batch_size,
                              nb_frames=nb_frames,
                              transformations=transformations,
                              frame_size=frame_size)

        expected = []
        for i in range(1, batch_size + 1):
            imgs = GeneratorUtils.get_sample_images(Path(f'fake_dataset/{i}'))
            imgs = GeneratorUtils.pick_at_intervals(imgs, nb_frames, math.floor)
            imgs = [GeneratorUtils.process_img(img_path, frame_size)
                    for img_path in imgs]
            imgs = GeneratorUtils.augment(imgs, transformations)
            expected.append(imgs)
        expected = np.stack(expected)

        sample, _ = gen.__getitem__(0)
        npt.assert_equal(actual=sample, desired=expected)
Пример #15
0
    def get_sample_list(self, source: pd.DataFrame, printer=print):
        self.class_label_map = GeneratorUtils.generate_class_to_label_mapper(
            source.iloc[:, 1].unique(), self.labelling_strategy)

        samples = []
        class_count = {}
        for [sample, _class] in source.values.tolist():
            sample_path = self.data_path / str(sample)
            if len(os.listdir(sample_path)) >= self.nb_frames:
                samples.append([sample_path, self.class_label_map[_class]])

            if _class in class_count:
                class_count[_class] += 1
            else:
                class_count[_class] = 1

        printer(f'Sample size: {len(samples)}')
        for k, v in class_count.items():
            printer(f'Class {k}: {v}')

        return samples
Пример #16
0
 def test__generate_class_to_label_mapper__when_strategy_is_binary_with_more_than_two_classes(self):
     with self.assertRaises(Exception):
         GeneratorUtils.generate_class_to_label_mapper(
             [0, 1, 2], 'binary')
Пример #17
0
 def test__pick_at_intervals__when_required_elements_less_than_available_using_ceil(self):
     actual = GeneratorUtils.pick_at_intervals([1, 2, 3, 4], 3, math.ceil)
     self.assertEqual([2, 3, 4], actual)
Пример #18
0
 def test__generate_class_to_label_mapper__when_strategy_is_neither_binary_nor_categorical(self):
     with self.assertRaises(ValueError):
         GeneratorUtils.generate_class_to_label_mapper(
             [0, 1], 'not a valid strategy')
Пример #19
0
 def test__pick_at_intervals__when_max_equals_list_length(self):
     expected = [1, 2, 3, 4]
     actual = GeneratorUtils.pick_at_intervals(expected, 4, math.floor)
     self.assertEqual(expected, actual)
Пример #20
0
    def test__process_img__resizes_img(self):
        img_path = Path('fake_dataset/1/1.png')
        img = GeneratorUtils.process_img(img_path, (10, 10))

        self.assertEqual((10, 10), img.shape[:2])
Пример #21
0
    def test__augment_imgs__returns_numpy_array(self):
        imgs = GeneratorUtils.get_sample_images(Path('fake_dataset/1'))
        imgs = [GeneratorUtils.process_img(img) for img in imgs]
        augmented_imgs = GeneratorUtils.augment(imgs, [A.HorizontalFlip()])

        self.assertIsInstance(augmented_imgs, np.ndarray)
Пример #22
0
    def test__process_img__converts_img_to_numpy_array(self):
        img_path = Path('fake_dataset/1/1.png')
        img = GeneratorUtils.process_img(img_path, (10, 10))

        self.assertIsInstance(img, np.ndarray)