# Keras loading images in incorrect format

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

Where as the original image is

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?

## 2 Answers

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.

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.

• actually i was asking about the keras load_image function but the answer to the second part of the question makes sense.Thanks – lakshay taneja Sep 15 '17 at 6:59
• @lakshaytaneja sorry for that. Please check my edit. – Gowtham Ramesh Sep 15 '17 at 8:15

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.

• But the same image when loaded with pillow library is displayed correctly by matplotlib – lakshay taneja Sep 13 '17 at 16:39