Keras: Converting a JPEG to a form in which it can be send into a neural net

Created on 1 Jul 2015  路  4Comments  路  Source: keras-team/keras

Hello there,

I am experimenting with keras and imagenet , my network is setup so is everything else, I am just making sure that my inputs are correct by asking you here. So basically I take the JPEG images and resize them , change their format from BGR to RGB and finally using numpy change their shape so they would fit the shape of the network (samples,3,224,224), when I load them with OpenCV the format is 224,224,3 so I have to reshape them , but I am afraid that numpy may not be reshaping them correctly. Can you tell me if what I am doing looks correct?

file=cv2.imread(filename)
file=cv2.resize(file,(224,224))
file=cv2.cvtColor(file, cv2.COLOR_BGR2RGB)
file=np.array(image).reshape((3,224,224))
files.append([file])
print(file.shape[0])

Most helpful comment

You can use Numpy's swapaxes function to reshape the image into Keras' format in one line of code.
http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.swapaxes.html

import numpy as np
import random
import cv2
import matplotlib.pyplot as plot
from skimage import data

#read image into cv2 format
image=data.astronaut()
image=cv2.resize(image,(224,224))

#use swapaxes to convert image to Keras' format
image_convert=np.swapaxes(np.swapaxes(image, 1, 2), 0, 1)

#Show that it worked
print(image_convert.shape)
plot.imshow(image_convert[1,:,:])
plot.show()

All 4 comments

Hey , for anyone having the same question I managed to fix it. There was indeed an issue with the way numpy reshaped things, I found a better way to do it. Simply extract all 3 channels separately and put them in a new array which changes the shape very simply. Here is a piece of code that showcases the 2 different approaches , the numpy reshape and the manual reshape. The first one gives a weird output whereas the second produces the correct image.

import numpy as np
import random
import cv2
import matplotlib.pyplot as plot
from skimage import data

image=data.hubble_deep_field()

image=data.astronaut()
image=cv2.resize(image,(224,224))

image=cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

array=np.array(image)
print(array.shape)
print(array)
new_image=np.array(image).reshape((3,224,224))
print(new_image.shape)
print(new_image)
plot.imshow(image[:,:,1])
plot.show()
plot.imshow(new_image[1,:,:])
plot.show()

red=image[:,:,0]
green=image[:,:,1]
blue=image[:,:,2]

image_convert=np.array([red,green,blue])
print(image_convert.shape)

print(image_convert)

plot.imshow(image_convert[1,:,:])
plot.show()

You can use Numpy's swapaxes function to reshape the image into Keras' format in one line of code.
http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.swapaxes.html

import numpy as np
import random
import cv2
import matplotlib.pyplot as plot
from skimage import data

#read image into cv2 format
image=data.astronaut()
image=cv2.resize(image,(224,224))

#use swapaxes to convert image to Keras' format
image_convert=np.swapaxes(np.swapaxes(image, 1, 2), 0, 1)

#Show that it worked
print(image_convert.shape)
plot.imshow(image_convert[1,:,:])
plot.show()

can u please tell me ..what is (image, 1, 2), 0, 1) in the code...?

Hi!
There is one issue that I am facing, I need to swap the dimensions of the image for the neural network. But, doing that changes the colour of the images. Is there any way to get this done?

Was this page helpful?
0 / 5 - 0 ratings