Answered step by step
Verified Expert Solution
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:githubcomnickmccullumcatsanddogs
Here's what the repository contains:
A folder called trainingdata that holds images of cats and images of dogs
A folder called testdata that contains images of cats and images of dogs
A folder called predictions that image of a cat and 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 opensource 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 realtime 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:
zoomrange: Float or lower upper Range for random zoom. If a float, lower upperzoomrange, zoomrange
horizontalflip: Boolean. Randomly flip inputs horizontally.
shearrange: Float. Shear Intensity Shear angle in counterclockwise direction in degrees
The only other argument that the ImageDataGenerator class needs is rescale which scales every pixel in the image such that its value lies between and 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 traininggenerator with a shear range, a zoom range, and a horizontal flip:
traininggenerator ImageDataGenerator
rescale
shearrange
zoomrange
horizontalflip 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 flowfromdirectory method on our new traininggenerator object. This method takes a number of parameters. This method will apply the necessary image augmentation techniques to our training data.
trainingset traininggenerator.flowfromdirectorytrainingdata',
targetsize
batchsize
classmode 'binary'
Let's examine each of the parameters from this method onebyone:
The first parameter is the folder that the training data is contained in
The targetsize variable contains the dimensions that each image in the data set will be resized to
The batchsize variable represents the size of ba
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started