Radhesh Started Speaking:-,
Hello Everyone this is my third project named "Electric Cars V/S Electric Bus Classification".
Downloading The Dataset:-
I Downloaded the Dataset From:
After downloading the dataset,i imported the dataset to my google drive to use this into google colab.I opened the google colab and then first imports the necessory libraries and then my dataset and so on....
Importing necessory libraries:-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
from tqdm import tqdm
Importing Tensorflow libraries and keras:-
import tensorflow as tf
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
import tensorflow_hub as hub
After importing the libraries i loaded the dataset into my colab notebook
train_path='/content/drive/MyDrive/Car V S Bus/Training_set'
Now,for extracting the labels from the dataset i performed the following task:-
- Creating list of categories
CATEGORIES=['electric car',"electric bus"]
- Now,iterating over the list and converting the features(images) into metrics and storing them into training variable.
Demo:-
for category in CATEGORIES: # do dogs and cats
path = os.path.join(train_path,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)) # 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)
Output:-
- Now,Actual code is:-
training_and_data=[]
def create_training_data():
for category in CATEGORIES: # do dogs and cats
path = os.path.join(train_path,category) # create path to dogs and cats
class_num = CATEGORIES.index(category) # get the classification (0 or a 1). 0=dog 1=cat
for img in tqdm(os.listdir(path)): # iterate over each image per dogs and cats
img_array = cv2.imread(os.path.join(path,img) ) # convert to array
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE),cv2.IMREAD_GRAYSCALE)
training_and_data.append([new_array, class_num])
create_training_data()
print(len(training_and_data))
Output:-
- Creating X and Y List variables:-
X=[]
y=[]
- After creating the variables ,append the respective datas into them:-
import random
random.shuffle(training_and_data)
for features,labels in training_and_data:
X.append(features)
y.append(labels)
Preprocessing:-
X=np.array(X)
X.shape
Output:- (1486, 128, 128, 3)
Z=X.copy()
Z=np.array(Z)
Z=Z/255.0
Z.shape
Output:- (1486, 128, 128, 3)
y=np.array(y)
y.shape
Output:- (1486,) So, we have features of size (1486,128,128,3) and labels_size = 1486
Creating a Model:-
new_model=keras.models.Sequential([
keras.layers.Conv2D(filters=32,kernel_size=5,input_shape=(128,128,3),activation='relu'),
keras.layers.MaxPool2D(pool_size=(4,4)),
keras.layers.Conv2D(filters=64,kernel_size=4),
keras.layers.MaxPool2D(pool_size=(3,3)),
keras.layers.Conv2D(filters=128,kernel_size=3),
keras.layers.MaxPool2D(pool_size=(2,2)),
keras.layers.Conv2D(filters=256,kernel_size=2),
keras.layers.MaxPool2D(pool_size=(2,2)),
keras.layers.Flatten(),
keras.layers.Dense(units=128,activation='relu'),
keras.layers.Dense(units=64,activation='relu'),
keras.layers.Dense(units=2,activation='softmax')
])
- Compiling the model:-
new_model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
- Model Summary:-
print(new_model.summary())
Output:-
- Fit the datas into model:-
tensorbord_d=create_tb_callback()
result=new_model.fit(Z,y,
batch_size=32,
epochs=50,
verbose=1,
validation_split=0.3,
callbacks=[tensorbord_d,callbacks_list])
Output:-
BOO yeyy! We got the model with good accuracy :)
So,this is the end of project ("Electric Cars V/S Electric Bus classification by CNN"),after training the model you can implemet the object detection by this model. Sooner i will came back with this concept till then Thank You:)