Cainvas
Model Files
tyre_v1.h5
keras
Model
deepSea Compiled Models
tyre_v1.exe
deepSea
Ubuntu

Importing Libraries

Credit: AITS Cainvas Community

Photo by Nikolay Ivanov for Lobster on Dribbble

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"
--2021-12-08 09:29:02--  https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/tyre.zip
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.62.12
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.62.12|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6245714 (6.0M) [application/x-zip-compressed]
Saving to: ‘tyre.zip’

tyre.zip            100%[===================>]   5.96M  --.-KB/s    in 0.04s   

2021-12-08 09:29:02 (141 MB/s) - ‘tyre.zip’ saved [6245714/6245714]

In [3]:
!unzip -qo tyre.zip

Preprocessing

In [4]:
ls tire-dataset
flat/  full/  no-tire/
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')
Found 720 images belonging to 3 classes.
Found 180 images belonging to 3 classes.

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()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 98, 98, 32)        320       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 49, 49, 32)        0         
_________________________________________________________________
dropout (Dropout)            (None, 49, 49, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 47, 47, 16)        4624      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 24, 24, 16)        0         
_________________________________________________________________
flatten (Flatten)            (None, 9216)              0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 9216)              0         
_________________________________________________________________
dense (Dense)                (None, 25)                230425    
_________________________________________________________________
dense_1 (Dense)              (None, 3)                 78        
=================================================================
Total params: 235,447
Trainable params: 235,447
Non-trainable params: 0
_________________________________________________________________

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)
WARNING:tensorflow:From <ipython-input-10-aaa02491087e>:1: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
Please use Model.fit, which supports generators.
Epoch 1/20
36/36 [==============================] - 8s 214ms/step - loss: 0.5747 - accuracy: 0.7472 - val_loss: 0.5480 - val_accuracy: 0.7056
Epoch 2/20
36/36 [==============================] - 8s 210ms/step - loss: 0.2041 - accuracy: 0.9278 - val_loss: 0.4337 - val_accuracy: 0.7556
Epoch 3/20
36/36 [==============================] - 8s 209ms/step - loss: 0.1131 - accuracy: 0.9472 - val_loss: 0.4676 - val_accuracy: 0.7667
Epoch 4/20
36/36 [==============================] - 7s 208ms/step - loss: 0.0758 - accuracy: 0.9764 - val_loss: 0.3038 - val_accuracy: 0.8778
Epoch 5/20
36/36 [==============================] - 7s 208ms/step - loss: 0.0227 - accuracy: 0.9958 - val_loss: 0.2489 - val_accuracy: 0.9167
Epoch 6/20
36/36 [==============================] - 7s 208ms/step - loss: 0.0270 - accuracy: 0.9931 - val_loss: 0.2763 - val_accuracy: 0.8833
Epoch 7/20
36/36 [==============================] - 7s 206ms/step - loss: 0.0169 - accuracy: 0.9958 - val_loss: 0.2847 - val_accuracy: 0.9167
Epoch 8/20
36/36 [==============================] - 7s 207ms/step - loss: 0.0170 - accuracy: 0.9944 - val_loss: 0.2209 - val_accuracy: 0.9000
Epoch 9/20
36/36 [==============================] - 7s 207ms/step - loss: 0.0412 - accuracy: 0.9833 - val_loss: 0.2777 - val_accuracy: 0.8833
Epoch 10/20
36/36 [==============================] - 7s 208ms/step - loss: 0.0044 - accuracy: 1.0000 - val_loss: 0.1879 - val_accuracy: 0.9333
Epoch 11/20
36/36 [==============================] - 7s 207ms/step - loss: 0.0033 - accuracy: 1.0000 - val_loss: 0.2278 - val_accuracy: 0.9111
Epoch 12/20
36/36 [==============================] - 8s 210ms/step - loss: 0.0227 - accuracy: 0.9889 - val_loss: 0.3007 - val_accuracy: 0.8889
Epoch 13/20
36/36 [==============================] - 7s 208ms/step - loss: 0.0085 - accuracy: 0.9972 - val_loss: 0.1952 - val_accuracy: 0.9500
Epoch 14/20
36/36 [==============================] - 8s 209ms/step - loss: 0.0034 - accuracy: 1.0000 - val_loss: 0.2396 - val_accuracy: 0.9000
Epoch 15/20
36/36 [==============================] - 7s 207ms/step - loss: 0.0106 - accuracy: 0.9986 - val_loss: 0.1993 - val_accuracy: 0.9500
Epoch 16/20
36/36 [==============================] - 8s 209ms/step - loss: 0.0075 - accuracy: 0.9972 - val_loss: 0.1873 - val_accuracy: 0.9333
Epoch 17/20
36/36 [==============================] - 7s 205ms/step - loss: 0.0021 - accuracy: 1.0000 - val_loss: 0.2115 - val_accuracy: 0.9444
Epoch 18/20
36/36 [==============================] - 7s 206ms/step - loss: 5.2553e-04 - accuracy: 1.0000 - val_loss: 0.2605 - val_accuracy: 0.9056
Epoch 19/20
36/36 [==============================] - 7s 208ms/step - loss: 3.4928e-04 - accuracy: 1.0000 - val_loss: 0.2436 - val_accuracy: 0.9167
Epoch 20/20
36/36 [==============================] - 7s 205ms/step - loss: 0.0022 - accuracy: 1.0000 - val_loss: 0.2278 - val_accuracy: 0.9222

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]])
(100, 100, 1)
[[1.00000000e+00 5.63426283e-08 1.03149524e-08]]
Flat Tyre
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]])
(100, 100, 1)
[[3.6402596e-07 9.9999964e-01 5.2352363e-12]]
Full Tyre
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]])
(100, 100, 1)
[[2.9872587e-12 7.5309132e-15 1.0000000e+00]]
No Tyre

Saving model & deepCC

In [17]:
model.save('tyre_v1.h5')
In [18]:
!deepCC tyre_v1.h5
[INFO]
Reading [keras model] 'tyre_v1.h5'
[SUCCESS]
Saved 'tyre_v1_deepC/tyre_v1.onnx'
[INFO]
Reading [onnx model] 'tyre_v1_deepC/tyre_v1.onnx'
[INFO]
Model info:
  ir_vesion : 4
  doc       : 
[WARNING]
[ONNX]: terminal (input/output) conv2d_input's shape is less than 1. Changing it to 1.
[WARNING]
[ONNX]: terminal (input/output) dense_1's shape is less than 1. Changing it to 1.
[INFO]
Running DNNC graph sanity check ...
[SUCCESS]
Passed sanity check.
[INFO]
Writing C++ file 'tyre_v1_deepC/tyre_v1.cpp'
[INFO]
deepSea model files are ready in 'tyre_v1_deepC/' 
[RUNNING COMMAND]
g++ -std=c++11 -O3 -fno-rtti -fno-exceptions -I. -I/opt/tljh/user/lib/python3.7/site-packages/deepC-0.13-py3.7-linux-x86_64.egg/deepC/include -isystem /opt/tljh/user/lib/python3.7/site-packages/deepC-0.13-py3.7-linux-x86_64.egg/deepC/packages/eigen-eigen-323c052e1731 "tyre_v1_deepC/tyre_v1.cpp" -D_AITS_MAIN -o "tyre_v1_deepC/tyre_v1.exe"
[RUNNING COMMAND]
size "tyre_v1_deepC/tyre_v1.exe"
   text	   data	    bss	    dec	    hex	filename
1114085	   3976	    760	1118821	 111265	tyre_v1_deepC/tyre_v1.exe
[SUCCESS]
Saved model as executable "tyre_v1_deepC/tyre_v1.exe"
In [ ]: