Data input

A quick browse of the images should convince you that you that our images all vary in resolution and size. As you know from Chapter 7, Convolutional Neural Networks, however, we will need these images to be a consistent size for our neural network's input tensor. This is a very real-world problem that you'll often face with computer vision tasks. While it's certainly possible to use a program such as ImageMagick (http://www.imagemagick.org) to batch resize our images, the Keras ImageDataGenerator class can be used to resize images on the fly, which is what we will do.

Inception-V3 expects 299 x 299 x 3 images. We can specify this target size in the data generators, as shown in the following code:

train_datagen = ImageDataGenerator(rescale=1./255)
val_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')

validation_generator = val_datagen.flow_from_directory(
val_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')

We most certainly could use data augmentation here if we wanted to, but we won't really need it.

Probably the most interesting thing we're doing here is using the data generator's flow_from_directory() method. This method takes a path and generates batches of images given that path. It does all the work of lifting the images off the disk for us. Because it does this a batch at a time, we don't even have to keep all 50,000 images in RAM when they aren't needed. Pretty cool, right?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.236.70