0

I am using a pretrained resnet50 model to validate some classes. I am using LIME to test how the model is testing this data as well. However, some of the images are not RGB and may be different formats, and I noticed that RGB arrays are value 3 instead of other numbers (like 4). I am using skimage to preprocess the images and test it with LIME. Any suggestions on how I can fix this with skimage and tensorflow? I am using panda dataframes to collect the images and train and test generators to see if the model is able to guess correctly.

code:


def transform_img_fn_ori(url):

 img = skimage.io.imread(url)
 img = skimage.transform.resize(img, (299,299))
 img = (img - 0.5)*2
 img = np.expand_dims(img, axis=0)

 return img

url="" #this is a path on pc
images=transform_img_fn_ori(url)
explanation= explainer.explain_instance(images[0].astype('double'), model.predict,  top_labels=3, hide_color=0, num_samples=1000)

temp_1, mask_1 = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=True, num_features=5, hide_rest=True)
temp_2, mask_2 = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=False, num_features=10, hide_rest=False)




fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15,15))
ax1.imshow(mark_boundaries(temp_1, mask_1))
ax2.imshow(mark_boundaries(temp_2, mask_2))
ax1.axis('off')
ax2.axis('off')

1 Answers1

0

Your model expects RGB images and your url may point to non-RGB images.

In this situation the best is to make sure images are read in RGB. For instance, OpenCV reads images always in BGR by default.

In skimage, you can't ensure the format being read, it can be grayscale, RGB or RGBA according to docs.

In addition to this, skimage doesn't provide a single method to convert any image to RGB, like convert method in in Pillow. So, you need to guess which is your color mode and convert it to RGB.


img = skimage.io.imread(url)

if img.ndim == 2 or (img.ndim==3 and img.shape[2] ==1):
    # your image is in grayscale
    img = skimage.color.gray2rgb(img)
elif img.ndim==3 and img.shape[2] == 4:
    # your image is in RGBA
    img = skimage.color.rgba2rgb(img)
else:
    # your image is already in RGB
    assert img.ndim == 3 and img.shape[2] == 3

The last assert is to make sure everything is ok.

Finally, probably not your case, but images may contain any number of channels and other space colors than RGB. That's why I don't like skimage and prefer OpenCV. So, whatever method you using to read images, check out the docs to make sure what does it returns: it is impossible in some cases to distinguish, like between RGB and BGR for instance.

Rodrigo Laguna
  • 1,796
  • 1
  • 26
  • 46