Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Data Set You Will Need For This Lab This lab will teach you how to build a convolutional neural network to make predictions about

The Data Set You Will Need For This Lab
This lab will teach you how to build a convolutional neural network to make predictions about whether an image contains a cat or a dog.
To do this, you will need a data set to train the model. The data set for this lab can be obtained from GitHub repository. You can download it from here https://github.com/nickmccullum/cats-and-dogs
Here's what the repository contains:
A folder called training_data that holds 4000 images of cats and 4000 images of dogs
A folder called test_data that contains 1000 images of cats and 1000 images of dogs
A folder called predictions that 1 image of a cat and 1 image of a dog. We will use the images in this predictions folder to make single predictions using our trained model later in this tutorial.
Every image in this data set is a .jpg file.
The Libraries You Will Need For This Tutorial
This convolutional neural network tutorial will make use of a number of open-source Python libraries, including NumPy and (most importantly) TensorFlow.
The only import that we will execute that may be unfamiliar to you is the ImageDataGenerator function that lives inside of the keras.preprocessing.image module. The purpose of this function is to "Generate batches of tensor image data with real-time data augmentation." You will see the import of this function included in the code cell below.
Let's now import these libraries into our Python script.
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
Now that our libraries have been imported, we can move on to preprocessing the data we will use in our convolutional neural network.
Preprocessing Our Data
The process that we will use to preprocess our training data will be slightly different than the process we'll use on our test data. The reason for this is that to avoid overfitting, we will add an extra transformation to the training data images. Overfitting is a common problem in machine learning and deep learning and is characterized by having very high accuracy on the training data and much lower accuracy on the test data.
Let's start by preprocessing our training data!
Preprocessing the Training Data
The transformations that we will apply to our training data with the goal of avoiding overfitting are more simple than you might imagine. They are simple image transformations like:
Rotations, Cropping and Zooming
These transformations are known as geographical transformations or geographic transformations. The broader process of modifying the original data set to avoid overfitting is called image augmentation.
The tool that we will use for image preprocessing is called the ImageDataGenerator class, which we imported earlier in this lab.
This function is capable of applying a number of different transformations. The transformations and their definitions (from the keras documentation) are shown below:
zoom_range: Float or [lower, upper]. Range for random zoom. If a float, [lower, upper]=[1-zoomrange, 1+zoomrange].
horizontal_flip: Boolean. Randomly flip inputs horizontally.
shear_range: Float. Shear Intensity (Shear angle in counter-clockwise direction in degrees)
The only other argument that the ImageDataGenerator class needs is rescale =1/255, which scales every pixel in the image such that its value lies between 0 and 255- which, as you'll recall from earlier in this course, is required for convolutional neural networks.
With all of this out of the way, let's create an instance of the ImageDataGenerator class called training_generator with a 20% shear range, a 20% zoom range, and a horizontal flip:
training_generator = ImageDataGenerator(
rescale =1/255,
shear_range =0.2,
zoom_range =0.2,
horizontal_flip = True)
We have now created an object that can be used to perform image augmentation on our data set. However, the augmentation has not yet been done. We do not yet have the training data we will use to train our convolutional neural network.
To generate our data, we'll need to call the flow_from_directory method on our new training_generator object. This method takes a number of parameters. This method will apply the necessary image augmentation techniques to our training data.
training_set = training_generator.flow_from_directory('training_data',
target_size =(64,64),
batch_size =32,
class_mode = 'binary')
Let's examine each of the parameters from this method one-by-one:
The first parameter is the folder that the training data is contained in
The target_size variable contains the dimensions that each image in the data set will be resized to
The batch_size variable represents the size of ba

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions