2
$\begingroup$

So I was working with the the vgg16 model for dogs vs cats classification and I noticed that keras is not loading images in correct color format. The code is as follows:

import cv2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from keras.preprocessing import image

path='data/dogscats/sample/train/dogs/dog.1402.jpg'

imgkeras=image.load_img(path)
imgkeras=image.img_to_array(imgkeras)

plt.imshow(imgkeras)
plt.show()

The output of the following code is enter image description here

Where as the original image is

enter image description here

Can someone explain why is this happening? , also when the image is loaded through opencv and fed into vgg16 the predicted label is more accurate for this particular image than when it is loaded through keras as above,is the improper color format affecting that?

$\endgroup$

2 Answers 2

3
$\begingroup$

This is caused due to the img_to_array method which converts the image to a float32 array.

x = np.asarray(img, dtype=K.floatx())

Matplotlib interprets NxMx3 uint8 array as a standard image (0..255 components) in which case there is no preprocessing. Otherwise the pixels are multiplied by 255(without checking the range) and then cast into uint8, which I guess leads to this behaviour.

Check this:https://stackoverflow.com/questions/39925420/bizzare-matplotlib-behaviour-in-displaying-images-cast-as-floats

To answer the second part of your question, I guess the imagenet competitors used OpenCV to load images in BGR format to train vgg16 and hence the pretrained weights work well with images opened in BGR format.

$\endgroup$
2
  • $\begingroup$ actually i was asking about the keras load_image function but the answer to the second part of the question makes sense.Thanks $\endgroup$ Sep 15, 2017 at 6:59
  • $\begingroup$ @lakshaytaneja sorry for that. Please check my edit. $\endgroup$ Sep 15, 2017 at 8:15
0
$\begingroup$

Keras might not be the issue. The issue might be with the displaying done by matplotlib. The matplotlib image tutorial covers different ways of displaying images.

$\endgroup$
1
  • $\begingroup$ But the same image when loaded with pillow library is displayed correctly by matplotlib $\endgroup$ Sep 13, 2017 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.