Hi,
I’m doing a project making an image recognizer which uses labelled greyscale images with associated classes as input. These images can be either JPEG or PIL images, and I’ve tried both formats. The number of colour channels is 1 in either case - and when I call
im = Image.open(filenames[0]); im.mode
the image mode prints as ‘L’ which is how I know they’re greyscale.
I can train a model successfully by:
using the DataBlock API,
creating a dataloaders object from the DataBlock object,
then calling learn.fit_one_cycle(dataloaders)
But after the model is trained I try to use the ImageClassifierCleaner function to show any wrongly labelled data:
cleaner = ImageClassifierCleaner(learn, max_n=2)
Then I get an error which implies that the image was converted from greyscale into RGBA at some point along the way.
I’ll post the stack trace below.
So far I’ve tried building a channel conversion function into the item_tfms pipeline of the DataBlock, but this didn’t seem to work.
I’d like to know if anybody else has had this issue when working with the fastai ImageClassifierCleaner with greyscale images, and what you did to fix it. Or if you have some other piece of knowledge which can help, please let me know.
Thanks!
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File /opt/homebrew/lib/python3.10/site-packages/PIL/JpegImagePlugin.py:639, in _save(im, fp, filename)
638 try:
--> 639 rawmode = RAWMODE[im.mode]
640 except KeyError as e:
KeyError: 'RGBA'
The above exception was the direct cause of the following exception:
OSError Traceback (most recent call last)
File /opt/homebrew/lib/python3.10/site-packages/PIL/Image.py:643, in Image._repr_image(self, image_format, **kwargs)
642 try:
--> 643 self.save(b, image_format, **kwargs)
644 except Exception as e:
File /opt/homebrew/lib/python3.10/site-packages/PIL/Image.py:2413, in Image.save(self, fp, format, **params)
2412 try:
-> 2413 save_handler(self, fp, filename)
2414 except Exception:
File /opt/homebrew/lib/python3.10/site-packages/PIL/JpegImagePlugin.py:642, in _save(im, fp, filename)
641 msg = f"cannot write mode {im.mode} as JPEG"
--> 642 raise OSError(msg) from e
644 info = im.encoderinfo
OSError: cannot write mode RGBA as JPEG
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
File /opt/homebrew/lib/python3.10/site-packages/IPython/core/formatters.py:342, in BaseFormatter.__call__(self, obj)
340 method = get_real_method(obj, self.print_method)
341 if method is not None:
--> 342 return method()
343 return None
344 else:
File /opt/homebrew/lib/python3.10/site-packages/PIL/Image.py:661, in Image._repr_jpeg_(self)
656 def _repr_jpeg_(self):
657 """iPython display hook support for JPEG format.
658
659 :returns: JPEG version of the image as bytes
660 """
--> 661 return self._repr_image("JPEG")
File /opt/homebrew/lib/python3.10/site-packages/PIL/Image.py:646, in Image._repr_image(self, image_format, **kwargs)
644 except Exception as e:
645 msg = f"Could not save to {image_format} for display"
--> 646 raise ValueError(msg) from e
647 return b.getvalue()
ValueError: Could not save to JPEG for display