Tags:
vehicle
industry
auto
tyre
Similar Use Cases: Marble Defection App Vehicle Classification App Car - Bus Classification App
Similar Use Cases: Marble Defection App Vehicle Classification App Car - Bus Classification App
In [1]:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import tensorflow as tf
import cv2 as cv
from sklearn import preprocessing
from tensorflow import keras
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dropout, Dense
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.callbacks import ModelCheckpoint
Loading Dataset¶
In [2]:
!wget "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/tyre.zip"
In [3]:
!unzip -qo tyre.zip
Preprocessing¶
In [4]:
ls tire-dataset
In [5]:
train_dir= "./tire-dataset/"
In [6]:
train_datagen= tf.keras.preprocessing.image.ImageDataGenerator(rescale= 1./255, validation_split=0.2)
In [7]:
train_generator= train_datagen.flow_from_directory(train_dir, target_size= (100,100), color_mode= 'grayscale', batch_size= 20, class_mode= 'categorical', subset= 'training')
val_generator= train_datagen.flow_from_directory(train_dir, target_size= (100,100), color_mode= 'grayscale', batch_size= 20, class_mode= 'categorical', subset= 'validation')
Model Building¶
In [8]:
model= Sequential([
layers.Conv2D(32, (3,3), activation= 'relu', input_shape= (100,100,1)),
layers.MaxPooling2D(pool_size= (2,2), padding= 'same'),
layers.Dropout(0.3),
layers.Conv2D(16, (3,3), activation= 'relu'),
layers.MaxPooling2D(pool_size= (2,2), padding= 'same'),
layers.Flatten(),
layers.Dropout(0.3),
layers.Dense(25, activation= 'relu'),
# layers.Dense(64, activation= 'relu'),
layers.Dense(3, activation= 'softmax')
])
model.summary()
Compiling & Fitting the Model¶
In [9]:
model.compile(optimizer= 'adam', loss= 'categorical_crossentropy', metrics= ['accuracy'])
In [10]:
history= model.fit_generator(train_generator, epochs= 20, validation_data= val_generator)
Plotting Accuracy and Loss¶
In [11]:
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title("Model Accuracy")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.legend(["Train", "Test"], loc= "lower right");
In [12]:
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend(['Train','Test'], loc= 'upper right');
Predictions¶
In [13]:
tyre = ["Flat Tyre","Full Tyre","No Tyre"]
def Single_Image_Prediction(file):
#image = load_img(file, color_mode='rgb', target_size=(128, 128))
image= file
plt.imshow(image,cmap= 'gray')
plt.show()
print(image.shape)
# cv.imshow('image',file)
# cv.waitKey(0)
# cv.destroyAllWimdows()
# image = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
img_arr = img_to_array(image)
# img_arr = img_arr/255.
np_image = np.expand_dims(img_arr, axis=0)
return np_image
In [14]:
image = Single_Image_Prediction(val_generator[0][0][11])
pred_value = model.predict(image)
print(pred_value)
index_value = np.argmax(pred_value,axis=1) #For categorical model
print(tyre[index_value[0]])
In [15]:
image = Single_Image_Prediction(val_generator[0][0][12])
pred_value = model.predict(image)
print(pred_value)
index_value = np.argmax(pred_value,axis=1) #For categorical model
print(tyre[index_value[0]])
In [16]:
image = Single_Image_Prediction(val_generator[0][0][0])
pred_value = model.predict(image)
print(pred_value)
index_value = np.argmax(pred_value,axis=1) #For categorical model
print(tyre[index_value[0]])
Saving model & deepCC¶
In [17]:
model.save('tyre_v1.h5')
In [18]:
!deepCC tyre_v1.h5
In [ ]: