Search code examples
caffescikit-image

TypeError: self._open() got an unexpected keyword argument 'as_grey' python


One of my colleague getting error in python service while classify the image. Below is the error trace log

 File "nsfw.py", line 7, in <module>
score = classifier.get_score('8.jpg')
 File "/home/arunsharma/.local/lib/python3.6/site-packages/open_nsfw_python3/__init__.py", line 104, in get_score
image_data, caffe_transformer=caffe_transformer, caffe_net=nsfw_net, output_layers=['prob'])


File "/home/arunsharma/.local/lib/python3.6/site-packages/open_nsfw_python3/__init__.py", line 52, in caffe_preprocess_and_compute
    image = caffe.io.load_image(img_data_rs)
  File "/usr/lib/python3/dist-packages/caffe/io.py", line 301, in load_image
    img = skimage.img_as_float(skimage.io.imread(filename, as_grey=not color)).astype(np.float32)
  File "/home/arunsharma/.local/lib/python3.6/site-packages/skimage/io/_io.py", line 48, in imread
    img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
  File "/home/arunsharma/.local/lib/python3.6/site-packages/skimage/io/manage_plugins.py", line 210, in call_plugin
    return func(*args, **kwargs)
  File "/home/arunsharma/.local/lib/python3.6/site-packages/skimage/io/_plugins/imageio_plugin.py", line 10, in imread
    return np.asarray(imageio_imread(*args, **kwargs))
  File "/home/arunsharma/.local/lib/python3.6/site-packages/imageio/core/functions.py", line 264, in imread
    reader = read(uri, format, "i", **kwargs)
  File "/home/arunsharma/.local/lib/python3.6/site-packages/imageio/core/functions.py", line 186, in get_reader
    return format.get_reader(request)
  File "/home/arunsharma/.local/lib/python3.6/site-packages/imageio/core/format.py", line 164, in get_reader
    return self.Reader(self, request)
  File "/home/arunsharma/.local/lib/python3.6/site-packages/imageio/core/format.py", line 214, in __init__
    self._open(**self.request.kwargs.copy())
TypeError: _open() got an unexpected keyword argument 'as_grey'

Here is the library which he has used. He had firstly installed caffe library and upgraded the skimage library. but still no luck.

Any help will be appreciated.

Code:

from open_nsfw_python3 import NSFWClassifier

classifier = NSFWClassifier()

score = classifier.get_score('image.jpg')

print(score)

He gets the error when get_score gets executed.


Solution

  • pycaffe depends on skimage to do image preprocessing. As skimage moved forward in versions, they decided to rename some function argument from as_grey to as_gray.

    However, Caffe still uses as_grey:

    https://github.com/BVLC/caffe/blob/9b891540183ddc834a02b2bd81b31afae71b2153/python/caffe/io.py#L302

    You have two options, install an older version of skimage, or change that line of code in pycaffe, to use as_gray.