mdbloice / Augmentor

Image augmentation library in Python for machine learning.
https://augmentor.readthedocs.io/en/stable
MIT License
5.08k stars 866 forks source link

Grayscale image loading error can be fixed #142

Open KokeCacao opened 6 years ago

KokeCacao commented 6 years ago

Here is the code that WORK:

p = Augmentor.Pipeline(args.image_dir)
        # Point to a directory containing ground truth data.
        # Images with the same file names will be added as ground truth data
        # and augmented in parallel to the original data.
        p.ground_truth(args.mask_dir)

        p.greyscale(probability=1.0) # I don't need grey scale because resnet does not support, but add it anyway in case it says wrong mode
        p.resize(1, 224, 224, resample_filter=u'BICUBIC') #BICUBIC, BILINEAR, ANTIALIAS, or NEAREST. https://i.stack.imgur.com/orwNd.png
        p.random_brightness(probability=0.5, min_factor=0.95, max_factor=1.05)
        # p.black_and_white(probability=1.0, threshold=128)
        p.flip_random(probability=0.75)
        p.rotate_random_90(probability=0.75)
        p.rotate(probability=0.5, max_left_rotation=5, max_right_rotation=5)
        # p.crop_random(probability=0.5, percentage_area, randomise_percentage_area=False) already 'croped' by skew
        p.skew(probability=0.5, magnitude=0.1)
        p.random_distortion(probability=0.8, grid_width=16, grid_height=16, magnitude=8)

        # 1.5625% data will not be modified

        # Add operations to the pipeline as normal:
        p.sample(args.sample)

However, here is the code that doesn't work if you remove the line:

p.greyscale(probability=1.0) # I don't need grey scale because resnet does not support, but add it anyway in case it says wrong mode

Here is the error message

Traceback (most recent call last):
  File "data/augmentation.py", line 60, in <module>
    p.sample(args.sample)
  File "/home/k1412042720/.local/lib/python2.7/site-packages/Augmentor/Pipeline.py", line 359, in sample
    for result in executor.map(self, augmentor_images):
  File "/home/k1412042720/.local/lib/python2.7/site-packages/concurrent/futures/_base.py", line 641, in result_iterator
    yield fs.pop().result()
  File "/home/k1412042720/.local/lib/python2.7/site-packages/concurrent/futures/_base.py", line 455, in result
    return self.__get_result()
  File "/home/k1412042720/.local/lib/python2.7/site-packages/concurrent/futures/thread.py", line 63, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/k1412042720/.local/lib/python2.7/site-packages/Augmentor/Pipeline.py", line 100, in __call__
    return self._execute(augmentor_image)
  File "/home/k1412042720/.local/lib/python2.7/site-packages/Augmentor/Pipeline.py", line 228, in _execute
    images = operation.perform_operation(images)
  File "/home/k1412042720/.local/lib/python2.7/site-packages/Augmentor/Operations.py", line 315, in perform_operation
    augmented_images.append(do(image))
  File "/home/k1412042720/.local/lib/python2.7/site-packages/Augmentor/Operations.py", line 310, in do
    return image_enhancer_brightness.enhance(factor)
  File "/home/k1412042720/.local/lib/python2.7/site-packages/PIL/ImageEnhance.py", line 37, in enhance
    return Image.blend(self.degenerate, self.image, factor)
  File "/home/k1412042720/.local/lib/python2.7/site-packages/PIL/Image.py", line 2698, in blend
    return im1._new(core.blend(im1.im, im2.im, alpha))

It is really clear that the method random_brightness does not support grayscale image in RGB form. Correct me if I am wrong. Thanks! O(∩_∩)O But anyway, the program should be more user-firendly.

mdbloice commented 6 years ago

Hi @KokeCacao, ok thanks for making the issue and finding this bug. I will take a look at that as soon as I can.