Deep Learning: Simple Image Classification using Convolutional Neural Network (Dog and Cat Dataset)
(Deep Learning using Python and Tensorflow)
Hello everyone, glad to see you again.
This time we will try to make an image classification model using CNN. We will use Keras as a deep learning library in building our CNN model.
Deep Learning
Deep Learning (also known as deep structured learning or hierarchical learning) is part of a wider family of machine learning methods based on artificial neural networks. Learning can be supervised, semi-supervised or not supervised.
Convolutional Neural Network (CNN)
In Deep Learning, Convolutional Neural Networks (CNN, or ConvNet) are deep neural networks classes, which are most commonly applied to analyze visual images.
1. Data Pre-Processing
The dataset is divided into training data and test data. Each is divided into cat and dog image data categories. For training data, there are 8000 images for each category. As for the test data, each category is 2000 images.
Data can be downloaded here.
Some of the stages in building the CNN model here are as follows:
Step 1: Convolution
Step 2: Pooling
Step 3: Flattening
Step 4: Full Connection
Code Implementation
First, we import all the packages/libraries needed
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
import timefrom tqdm import tqdmfrom tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
Following are some of the main libraries needed to build a model:
- import Conv2D to carry out convolutional operations on training drawings.
- import MaxPooling2D for its pooling operations.
- import Flatten for its flattening function.
- Import Dense to run our full neural network connection.
Now, we begin to create class categories objects like the following:
dir_file = ‘/directory_location_of_your_file/cat-and-dog/training_set/training_set/’class_category = [“cats”, “dogs”]
Then create a path for each class and create a variable that can store images in an array type, and show one as an example.
for category in class_category: # there are to categories, dogs and cats
path = os.path.join(dir_file,category) # create path to dogs and cats
for img in os.listdir(path): # iterate over each image per dogs and cats
img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) # convert to array
plt.imshow(img_array, cmap=’gray’) # graph it
plt.show() # display! break # we just want one for now so break
break #…and one more!print(img_array)
Resize the image size according to the most optimal value, this time we resize the image to 50.
IMG_SIZE = 50new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
plt.imshow(new_array, cmap=’gray’)
plt.show()
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
plt.imshow(new_array, cmap=’gray’)
plt.show()
training_data = []
def create_training_data():
for category in CATEGORIES: # do dogs and catspath = os.path.join(DATADIR,category) # create path to dogs and cats
class_num = CATEGORIES.index(category) # get the classification (0 or a 1). 0=dog 1=catfor img in tqdm(os.listdir(path)): # iterate over each image per dogs and cats
try:
img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) # convert to array
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) # resize to normalize data size
training_data.append([new_array, class_num]) # add this to our training_data
except Exception as e: # in the interest in keeping the output clean…
pass
#except OSError as e:
# print(“OSErrroBad img most likely”, e, os.path.join(path,img))
#except Exception as e:
# print(“general exception”, e, os.path.join(path,img))create_training_data()print(len(training_data))
Then we use the random library to sort the training data randomly.
import randomrandom.shuffle(training_data)for sample in training_data[:10]:
print(sample[1])X = []
y = []for features,label in training_data:
X.append(features)
y.append(label)print(X[0].reshape(-1, IMG_SIZE, IMG_SIZE, 1))X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
We also use pickle, pickling is a way to convert a python object (list, dict, etc.) into a character stream.
import picklepickle_out = open(“X.pickle”,”wb”)
pickle.dump(X, pickle_out)
pickle_out.close()pickle_out = open(“y.pickle”,”wb”)
pickle.dump(y, pickle_out)
pickle_out.close()pickle_in = open(“X.pickle”,”rb”)
X = pickle.load(pickle_in)pickle_in = open(“y.pickle”,”rb”)
y = pickle.load(pickle_in)
Next is the dense process. Dense is a function to add layers that are fully connected.
X = X/255.0X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)y = np.array(y)
dense_layers = [0]
layer_sizes = [64]
conv_layers = [3]for dense_layer in dense_layers:
for layer_size in layer_sizes:
for conv_layer in conv_layers:
NAME = “{}-conv-{}-nodes-{}-dense-{}”.format(conv_layer, layer_size, dense_layer, int(time.time()))
print(NAME)
2. Build a model
Now, we begin to create sequential class objects like the following:
model = Sequential()
The next step is pooling. Pooling aims to reduce the size of the image as much as possible. Here also we try to reduce the total number of nodes in the next layer. We begin by adding our classifier object to the pooling layer. Pooling in the form of a 2x2 matrix as minimum pixel loss and precise region where features are allocated.
model.add(Conv2D(layer_size, (3, 3), input_shape=X.shape[1:]))
model.add(Activation(‘relu’))
model.add(MaxPooling2D(pool_size=(2, 2)))for l in range(conv_layer-1):
model.add(Conv2D(layer_size, (3, 3)))
model.add(Activation(‘relu’))
model.add(MaxPooling2D(pool_size=(2, 2)))
Next is the flattening process. The pooling data that we have been in the form of 2-dimensional arrays and then converted to single vector one-dimensional data.
model.add(Flatten())for _ in range(dense_layer):
model.add(Dense(layer_size))
model.add(Activation(‘relu’))model.add(Dense(1))
model.add(Activation(‘sigmoid’))tensorboard = TensorBoard(log_dir=”logs/{}”.format(NAME))model.compile(loss=’binary_crossentropy’,
optimizer=’adam’,
metrics=[‘accuracy’],
)
3. Training data
Finally, we arrive at the data training stage. After doing the training data then save the model.
model.fit(X,
y,
batch_size=32,
epochs=25,
validation_split=0.3,
callbacks=[tensorboard])model.save(‘is_Cat_or_Dog.model’)
4. Testing Data (Predict image)
Finally, we need to test the model we have made before to predict an image.
class_category = [“Dog”, “Cat”]def prepare(filepath):
IMG_SIZE = 50 # 50 in txt-basedimg_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
#new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)model = tf.keras.models.load_model(‘is_Cat_or_Dog.model’)
Here you can upload an image or using your file on the current drive for sample prediction.
prediction = model.predict([prepare(‘/directory_location_of_your_file/cat-and-dog/test_set/test_set/cats/cat.4001.jpg’)])
And Taraaaaa, let’s see the results of our predictions……
print(prediction) # will be a list in a list.
print(class_category[int(prediction[0][0])])
For full source code :
Reference :