Breaking Down get_data() Function

I’m trying to break down the get_data function that Jeremy created and I have some questions. Here is my current code:

%matplotlib inline
from keras.preprocessing import image
import numpy as np
import matplotlib
from matplotlib import pyplot as plt

idg = image.ImageDataGenerator()
train_img = idg.flow_from_directory("label_train_img",target_size=(224,224), batch_size=1)

data = train_img.next()
plt.imshow(data[0][0])

This gives me the following image (Just the picture part, not the black outline):

My thought is that I must have done something wrong with the RGB, but I’m not even sure how to modify that or see what each of those looks like. I am hoping somebody can point me in the right direction and provide some links to look into this further. I have had some issues with accuracy and I think this is the problem.

Besides this issue though, I believe what the get_data() function that Jeremy created does is pulls data using the flow_from_directory function built into keras for preprocessing images, then iterates through that using a loop that gives a batch of values at a time, but Jeremy uses a batch size of 1 here so that every image gets read in without having to do an extra loop. He also has class_mode=None which is important because if you don’t have that, you will get a "ValueError: all the input arrays must have same number of dimensions
" error. That’s where I am on this so far. I have gotten a lot more familiar with what is actually happening behind the scenes on this.

Hopefully this can maybe be helpful to somebody and maybe somebody will be able to figure out my image issues.

1 Like

Hey Kevin! I’ve just spent the last twenty minutes working through the same problem you’ve just had.

The key thing that’s missing for displaying the image is casting the NumPy array to a uint8.
Instead of
plt.imshow(data[0][0])
try
plt.imshow(data[0][0].astype(np.uint8)).

Seems that generated image is in a float64 format, which appears to be fine for Keras, but not for Matplotlib.

You’ll also note that this conversion is done under Jeremy’s ‘plots’ function under utils.py.

Obligatory SO link: https://stackoverflow.com/questions/36137980/matplotlib-imshow-inverting-colors-of-2d-ifft-array

1 Like

Matplotlib tries to be smart sometimes about how it interprets the data, so what it plots may be a little misleading.

That was totally my issue. I really appreciate the help on that. I just had no idea how to even Google that.