Project Creation: Part One
In this lesson, we will start our project to classify different Pokemons.
Introduction to the project
Welcome to our first project in the course.
The project is divided into two lessons for better understanding. In this section, we will set up all the resources, the dataset, and the preprocessing of the dataset so that it becomes compatible with our ConvNet Model, ResNet50.
Before starting the project, please read the guidelines below so that it will be easy to work along with the course:
- Download the dataset here
- The dataset contains 10 different Pokemons. But if you want to build a model on a larger dataset, you can get the download from here. It contains around 150 types of Pokemons.
- If you do not have a nicely configured CPU, then we suggest using Google Colab for building the model.
- The following packages are used:
keras
numpy
matplotlib
You are now ready to start the project.
Creating the dataset
The first step is to create the dataset. We will create a list, image_data
that contains the images in a numpy
array format. We will also convert the image labels to One Hot Encodings
.
One hot encoding: it produces a vector with the length equal to the number of categories in the data set. If a data point belongs to the category, then components of this vector are assigned the value 0 except for the component, which is assigned a value of 1.
# Import the required librariesimport osfrom keras.preprocessing import imagefolders = os.listdir('Train')# Uncommment the below statement to view the folders# print(folders)image_data = []labels = []count = 0for ix in folders:path = os.path.join("Train", ix)# Uncommment the below statement to view the path to the images and their labels# print(path, count)for im in os.listdir(path):try:img = image.load_img(os.path.join(path,im), target_size = (224,224))img_array = image.img_to_array(img)image_data.append(img_array)labels.append(count)except:passcount += 1
Explanation:
-
On line 5 we get the list of all the folders that are inside the images directory, i.e., all the Pokemon names. If you want to see what the contents the
folder
variable has, just print it. It will show something like this:['Aerodactyl', 'Bulbasaur', 'Charmander', 'Dratini', 'Fearow', 'Meowth', 'Pikachu', 'Psyduck', 'Spearow', 'Squirtle']
-
On line 14, we join the path to reach each directory. The content of the
path
variable, when printed, gives you the following output:Train/Bulbasaur 0 Train/Spearow 1 Train/Psyduck 2 Train/Aerodactyl 3 Train/Meowth 4 Train/Charmander 5 Train/Fearow 6 Train/Squirtle 7 Train/Dratini 8 Train/Pikachu 9
-
On line 19, we then join the path to reach each of the images and then load the image one by one by specifying the target_size of the image.
-
One line 20 and line 21, we convert the image to a
numpy
array and then append the data to ourimage_data
list. -
On line 22, we append the labels as well. We maintain an integer value for each of the labels. You can see the mapping above when we printed on line 14.
-
On line 25, we finally increment the value of
count
as we see a new image label.
Shuffle the dataset
We don’t want our model to overfit or underfit. Right now we have the data in a sequence: first Pokemon images, then second Pokemon images, and so on. It is a good practice to shuffle the dataset randomly to avoid any model overfit and achieve more generalization.
import randomcombined_dataset = list(zip(image_data, labels))random.shuffle(combined_dataset)image_data[:], labels[:] = zip(*combined_dataset)
Explanation:
-
On line 3, we combine the
image_data
andlabels
usingzip()
which creates a tuple for each element from both of the lists. -
One line 4, we used the
random
module to shuffle the dataset randomly. Each time you run this statement, you will see a different shuffling. -
On line 5, we unzip the combined dataset and save them in the
image_data
andlabels
variable. Note that thecombined_dataset
consists of tuples in the format (image_data
,labels
).
Final preprocessing of the dataset
Now, we will need to convert the labels to One Hot Encodings
as this is what a Multi-layer Perceptron accepts.
from keras.utils import np_utilsX_train = np.array(image_data)Y_train = np.array(labels)Y_train = np_utils.to_categorical(Y_train)
Explanation:
-
On line 2 and line 3, we converted out lists to a numpy array.
-
On line 5, we used
np_utils
to convert our labels toOne Hot Encodings
. You can view theY_train
before converting it to the one hot encoding and after converting it. You will see something similar to the image below. Note that you can have different output as it depends on the shuffling we did earlier.
That’s all for this lesson. Congratulations, you did the first step successfully and now we are ready with our dataset.