What we need for this tutorial:
- Basic knowledge of Python
- Understanding of Python syntaxes
- Basic of Machine Learning
- Editor of your choice (I’m using Jupyter Notebook)
- Some modules (You can install it by “pip install module_name” on cmd or terminal).
Let’s Load our modules first.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
%matplotlib inline
Since I’m using jupyter notebook, I typed this line “%matplotlib inline”.
Now let’s load our data, We are using CIFAR-10 datasets in the keras module. We are dealing with categorical problem but our data Y only have one value. To fix this, we need to pass it to our to_categorical function built in keras.
I divided trainX, trainY by 255.0, because operations on large number is expensive and time consuming.
You will see shape before and after of our data.
(trainX, trainY), (testX, testY) = cifar10.load_data()
print(trainX.shape, trainY.shape)
print(testX.shape, testY.shape)
trainX = trainX / 255.0
testX = testX / 255.0
trainY = to_categorical(trainY)
testY = to_categorical(testY)
print(trainX.shape, trainY.shape)
print(testX.shape, testY.shape)
Now I’m showing the first image of our trainX.
plt.imshow(trainX[0])
Now let’s get our labels, Which are string. and convert it to list.
labels = "airplane automobile bird cat deer dog frog horse ship truck".split()
print(labels)
print(labels[np.argmax(trainY[0])])
You can see that trainX at index 0 is frog. np.argmax function return the index of max value.
Let’s build our model. First we need a Conv2D layer and MaxPooling Layer. Which will be our feature generator. Then We will need classifier. I’m gonna create Sequential Model.
model = Sequential([
# Feature Generator
layers.Conv2D(32, kernel_size=(3, 3), input_shape=(32, 32, 3), activation='relu'),
layers.Conv2D(64, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
# Classifier
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(10, activation='softmax')
])
Now I’m gonna compile it with Adam optimizer and categorical_crossentropy loss function which is for categorical problems where classes are more than 2.
model = Sequential([
# Feature Generator
layers.Conv2D(32, kernel_size=(3, 3), input_shape=(32, 32, 3), activation='relu'),
layers.Conv2D(64, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
# Classifier
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
Let’s train the model with our data. I’m going to store history to history variable, We will plot graph. I’m using 5 epochs for fast approach. More epochs you will give, better the results will be.
history = model.fit(trainX, trainY, batch_size=64, epochs=5)
Plotting the Accuracy and Loss graph with matplotlib.
plt.figure(figsize=(8, 6))
plt.plot(history.history["accuracy"], label="Accuracy")
plt.plot(history.history["loss"], label="Loss")
plt.xlabel("Epoch")
plt.ylabel("Percentage")
plt.legend()
I’m going to save the model to models directory.
model.save('models/74_percent_model')
Now let’s get our predictions from our test data.
predictions = model.predict(testX)
We now have our predictions but it is in numbers, We need to convert this to our labels
i = 15
actual_label = labels[np.argmax(testY[i])]
predicted_label = labels[np.argmax(predictions[i])]
plt.imshow(testX[i])
plt.title("Actual: {}".format(actual_label))
plt.xlabel("Predicted: {}".format(predicted_label))
plt.show()
In this code, I’m going to take 15th index of test data and show the image and Actual Label and our predicted label. If you change i value. You will see, some values are wrong, The more you train, Better the neural network will get.
We now have our object classifier.
Link to Notebook is below. Download Notebook