Cainvas
Tags: car damage

Similar Use Cases: Pothole Detection App

Car Damage Detection using Deep Learning

Credit: AITS Cainvas Community

Photo by Guillaume Kurkdjian on Dribbble

In [1]:
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/data_final.zip"
!unzip -qo data_final.zip 
!rm data_final.zip
--2021-07-16 11:22:07--  https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/data_final.zip
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.62.84
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.62.84|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 128654048 (123M) [application/x-zip-compressed]
Saving to: ‘data_final.zip’

data_final.zip      100%[===================>] 122.69M  57.9MB/s    in 2.1s    

2021-07-16 11:22:10 (57.9 MB/s) - ‘data_final.zip’ saved [128654048/128654048]

Importing prerequisites

In [2]:
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping,ReduceLROnPlateau,LearningRateScheduler
from tensorflow.keras.preprocessing import image as ig
from tensorflow.keras.preprocessing.image import img_to_array
import random

Defining train and test data and performing image augmentation

In [3]:
train_dir = 'data1a/training'
test_dir = 'data1a/validation'
In [4]:
train_data = ImageDataGenerator(rescale = 1./255,shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True)

#defining training set, here size of image is reduced to 150x150, batch of images is kept as 128 and class is defined as 'categorical'.
training_set = train_data.flow_from_directory(train_dir, batch_size = 32, target_size = (64,64), class_mode = 'categorical')
Found 1840 images belonging to 2 classes.
In [5]:
#applying same scale as training set, but only feature scaling is applied. image augmentation is avoided to prevent leakage of testing data.
test_data = ImageDataGenerator(rescale = 1./255)

#defining testing set
testing_set = test_data.flow_from_directory(test_dir, batch_size = 32, target_size = (64,64), class_mode = 'categorical')
Found 460 images belonging to 2 classes.

Defining model callbacks

In [6]:
checkpoint = ModelCheckpoint(
    './base.model',
    monitor='val_accuracy',
    verbose=1,
    save_best_only=True,
    mode='max',
    save_weights_only=False,
    save_frequency=1
)
earlystop = EarlyStopping(
    monitor='val_loss',
    min_delta=0.001,
    patience=50,
    verbose=1,
    mode='auto'
)

opt1 = tf.keras.optimizers.Adam()

callbacks = [checkpoint,earlystop]

Model architecture

Using the Adam optimizer and categorical cross entropy as a loss function. The model was trained for 100 epochs and achieved a validation accuracy of 87%.

In [7]:
model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(64,64,3)),
    tf.keras.layers.MaxPool2D(pool_size=(2,2)),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Conv2D(32, 3, activation='relu'),
    tf.keras.layers.MaxPool2D(pool_size=(2,2)),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Conv2D(32, 3, activation='relu'),
    tf.keras.layers.MaxPool2D(pool_size=(2,2)),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Conv2D(32, 3, activation='relu'),
    tf.keras.layers.MaxPool2D(pool_size=(2,2)),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(2, activation='softmax')
])

model.summary()

# Compile the model
model.compile(loss=tf.keras.losses.CategoricalCrossentropy(),
              optimizer=opt1,
              metrics=['accuracy'])

# Fit the model
history = model.fit(training_set,
                    epochs=100,
                    steps_per_epoch=len(training_set),
                    validation_data=testing_set,
                    validation_steps=len(testing_set),
                    callbacks=callbacks)
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 62, 62, 32)        896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 31, 31, 32)        0         
_________________________________________________________________
dropout (Dropout)            (None, 31, 31, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 29, 29, 32)        9248      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 14, 14, 32)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 14, 14, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 12, 12, 32)        9248      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 6, 6, 32)          0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 6, 6, 32)          0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 4, 4, 32)          9248      
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 2, 2, 32)          0         
_________________________________________________________________
dropout_3 (Dropout)          (None, 2, 2, 32)          0         
_________________________________________________________________
flatten (Flatten)            (None, 128)               0         
_________________________________________________________________
dense (Dense)                (None, 64)                8256      
_________________________________________________________________
dropout_4 (Dropout)          (None, 64)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 130       
=================================================================
Total params: 37,026
Trainable params: 37,026
Non-trainable params: 0
_________________________________________________________________
Epoch 1/100
 1/58 [..............................] - ETA: 0s - loss: 0.7791 - accuracy: 0.5312WARNING:tensorflow:Callbacks method `on_train_batch_end` is slow compared to the batch time (batch time: 0.0039s vs `on_train_batch_end` time: 0.0097s). Check your callbacks.
58/58 [==============================] - ETA: 0s - loss: 0.7051 - accuracy: 0.5125
Epoch 00001: val_accuracy improved from -inf to 0.49783, saving model to ./base.model
WARNING:tensorflow:From /opt/tljh/user/lib/python3.7/site-packages/tensorflow/python/training/tracking/tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
WARNING:tensorflow:From /opt/tljh/user/lib/python3.7/site-packages/tensorflow/python/training/tracking/tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 11s 187ms/step - loss: 0.7051 - accuracy: 0.5125 - val_loss: 0.6925 - val_accuracy: 0.4978
Epoch 2/100
58/58 [==============================] - ETA: 0s - loss: 0.6923 - accuracy: 0.5152
Epoch 00002: val_accuracy improved from 0.49783 to 0.50000, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 181ms/step - loss: 0.6923 - accuracy: 0.5152 - val_loss: 0.6934 - val_accuracy: 0.5000
Epoch 3/100
58/58 [==============================] - ETA: 0s - loss: 0.6938 - accuracy: 0.5207
Epoch 00003: val_accuracy did not improve from 0.50000
58/58 [==============================] - 9s 161ms/step - loss: 0.6938 - accuracy: 0.5207 - val_loss: 0.6940 - val_accuracy: 0.5000
Epoch 4/100
58/58 [==============================] - ETA: 0s - loss: 0.6903 - accuracy: 0.5147
Epoch 00004: val_accuracy improved from 0.50000 to 0.59565, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 180ms/step - loss: 0.6903 - accuracy: 0.5147 - val_loss: 0.6852 - val_accuracy: 0.5957
Epoch 5/100
58/58 [==============================] - ETA: 0s - loss: 0.6636 - accuracy: 0.6158
Epoch 00005: val_accuracy did not improve from 0.59565
58/58 [==============================] - 9s 160ms/step - loss: 0.6636 - accuracy: 0.6158 - val_loss: 0.6669 - val_accuracy: 0.5848
Epoch 6/100
58/58 [==============================] - ETA: 0s - loss: 0.6451 - accuracy: 0.6380
Epoch 00006: val_accuracy improved from 0.59565 to 0.65870, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 179ms/step - loss: 0.6451 - accuracy: 0.6380 - val_loss: 0.6317 - val_accuracy: 0.6587
Epoch 7/100
58/58 [==============================] - ETA: 0s - loss: 0.6287 - accuracy: 0.6549
Epoch 00007: val_accuracy improved from 0.65870 to 0.68043, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 180ms/step - loss: 0.6287 - accuracy: 0.6549 - val_loss: 0.6173 - val_accuracy: 0.6804
Epoch 8/100
58/58 [==============================] - ETA: 0s - loss: 0.6285 - accuracy: 0.6630
Epoch 00008: val_accuracy improved from 0.68043 to 0.68913, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 178ms/step - loss: 0.6285 - accuracy: 0.6630 - val_loss: 0.6092 - val_accuracy: 0.6891
Epoch 9/100
58/58 [==============================] - ETA: 0s - loss: 0.6078 - accuracy: 0.6821
Epoch 00009: val_accuracy did not improve from 0.68913
58/58 [==============================] - 9s 160ms/step - loss: 0.6078 - accuracy: 0.6821 - val_loss: 0.6134 - val_accuracy: 0.6848
Epoch 10/100
58/58 [==============================] - ETA: 0s - loss: 0.5987 - accuracy: 0.6859
Epoch 00010: val_accuracy improved from 0.68913 to 0.70217, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 180ms/step - loss: 0.5987 - accuracy: 0.6859 - val_loss: 0.5958 - val_accuracy: 0.7022
Epoch 11/100
58/58 [==============================] - ETA: 0s - loss: 0.5968 - accuracy: 0.6864
Epoch 00011: val_accuracy improved from 0.70217 to 0.71087, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 179ms/step - loss: 0.5968 - accuracy: 0.6864 - val_loss: 0.5801 - val_accuracy: 0.7109
Epoch 12/100
58/58 [==============================] - ETA: 0s - loss: 0.5877 - accuracy: 0.6886
Epoch 00012: val_accuracy improved from 0.71087 to 0.71957, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 181ms/step - loss: 0.5877 - accuracy: 0.6886 - val_loss: 0.5568 - val_accuracy: 0.7196
Epoch 13/100
58/58 [==============================] - ETA: 0s - loss: 0.5855 - accuracy: 0.6826
Epoch 00013: val_accuracy did not improve from 0.71957
58/58 [==============================] - 9s 160ms/step - loss: 0.5855 - accuracy: 0.6826 - val_loss: 0.5715 - val_accuracy: 0.7152
Epoch 14/100
58/58 [==============================] - ETA: 0s - loss: 0.5767 - accuracy: 0.6989
Epoch 00014: val_accuracy did not improve from 0.71957
58/58 [==============================] - 9s 159ms/step - loss: 0.5767 - accuracy: 0.6989 - val_loss: 0.5484 - val_accuracy: 0.7130
Epoch 15/100
58/58 [==============================] - ETA: 0s - loss: 0.5685 - accuracy: 0.7185
Epoch 00015: val_accuracy improved from 0.71957 to 0.74130, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 178ms/step - loss: 0.5685 - accuracy: 0.7185 - val_loss: 0.5325 - val_accuracy: 0.7413
Epoch 16/100
58/58 [==============================] - ETA: 0s - loss: 0.5709 - accuracy: 0.7174
Epoch 00016: val_accuracy did not improve from 0.74130
58/58 [==============================] - 9s 158ms/step - loss: 0.5709 - accuracy: 0.7174 - val_loss: 0.5514 - val_accuracy: 0.7196
Epoch 17/100
58/58 [==============================] - ETA: 0s - loss: 0.5722 - accuracy: 0.7185
Epoch 00017: val_accuracy did not improve from 0.74130
58/58 [==============================] - 9s 160ms/step - loss: 0.5722 - accuracy: 0.7185 - val_loss: 0.5331 - val_accuracy: 0.7304
Epoch 18/100
58/58 [==============================] - ETA: 0s - loss: 0.5546 - accuracy: 0.7163
Epoch 00018: val_accuracy improved from 0.74130 to 0.74348, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 177ms/step - loss: 0.5546 - accuracy: 0.7163 - val_loss: 0.5214 - val_accuracy: 0.7435
Epoch 19/100
58/58 [==============================] - ETA: 0s - loss: 0.5570 - accuracy: 0.7130
Epoch 00019: val_accuracy did not improve from 0.74348
58/58 [==============================] - 9s 160ms/step - loss: 0.5570 - accuracy: 0.7130 - val_loss: 0.5338 - val_accuracy: 0.7370
Epoch 20/100
58/58 [==============================] - ETA: 0s - loss: 0.5491 - accuracy: 0.7293
Epoch 00020: val_accuracy improved from 0.74348 to 0.74565, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 11s 182ms/step - loss: 0.5491 - accuracy: 0.7293 - val_loss: 0.5060 - val_accuracy: 0.7457
Epoch 21/100
58/58 [==============================] - ETA: 0s - loss: 0.5430 - accuracy: 0.7277
Epoch 00021: val_accuracy did not improve from 0.74565
58/58 [==============================] - 9s 160ms/step - loss: 0.5430 - accuracy: 0.7277 - val_loss: 0.5295 - val_accuracy: 0.7326
Epoch 22/100
58/58 [==============================] - ETA: 0s - loss: 0.5238 - accuracy: 0.7402
Epoch 00022: val_accuracy improved from 0.74565 to 0.75435, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 179ms/step - loss: 0.5238 - accuracy: 0.7402 - val_loss: 0.4846 - val_accuracy: 0.7543
Epoch 23/100
58/58 [==============================] - ETA: 0s - loss: 0.5407 - accuracy: 0.7337
Epoch 00023: val_accuracy improved from 0.75435 to 0.76957, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 164ms/step - loss: 0.5407 - accuracy: 0.7337 - val_loss: 0.4858 - val_accuracy: 0.7696
Epoch 24/100
58/58 [==============================] - ETA: 0s - loss: 0.5209 - accuracy: 0.7500
Epoch 00024: val_accuracy did not improve from 0.76957
58/58 [==============================] - 8s 145ms/step - loss: 0.5209 - accuracy: 0.7500 - val_loss: 0.5008 - val_accuracy: 0.7326
Epoch 25/100
58/58 [==============================] - ETA: 0s - loss: 0.5210 - accuracy: 0.7582
Epoch 00025: val_accuracy improved from 0.76957 to 0.80652, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 9s 163ms/step - loss: 0.5210 - accuracy: 0.7582 - val_loss: 0.4527 - val_accuracy: 0.8065
Epoch 26/100
57/58 [============================>.] - ETA: 0s - loss: 0.5151 - accuracy: 0.7533
Epoch 00026: val_accuracy did not improve from 0.80652
58/58 [==============================] - 8s 145ms/step - loss: 0.5150 - accuracy: 0.7533 - val_loss: 0.4456 - val_accuracy: 0.7870
Epoch 27/100
58/58 [==============================] - ETA: 0s - loss: 0.5026 - accuracy: 0.7734
Epoch 00027: val_accuracy improved from 0.80652 to 0.81522, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 165ms/step - loss: 0.5026 - accuracy: 0.7734 - val_loss: 0.4458 - val_accuracy: 0.8152
Epoch 28/100
58/58 [==============================] - ETA: 0s - loss: 0.5076 - accuracy: 0.7707
Epoch 00028: val_accuracy improved from 0.81522 to 0.81957, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 9s 164ms/step - loss: 0.5076 - accuracy: 0.7707 - val_loss: 0.4391 - val_accuracy: 0.8196
Epoch 29/100
58/58 [==============================] - ETA: 0s - loss: 0.4938 - accuracy: 0.7636
Epoch 00029: val_accuracy did not improve from 0.81957
58/58 [==============================] - 8s 145ms/step - loss: 0.4938 - accuracy: 0.7636 - val_loss: 0.4268 - val_accuracy: 0.8043
Epoch 30/100
58/58 [==============================] - ETA: 0s - loss: 0.4797 - accuracy: 0.7783
Epoch 00030: val_accuracy did not improve from 0.81957
58/58 [==============================] - 8s 145ms/step - loss: 0.4797 - accuracy: 0.7783 - val_loss: 0.4241 - val_accuracy: 0.8087
Epoch 31/100
58/58 [==============================] - ETA: 0s - loss: 0.4812 - accuracy: 0.7783
Epoch 00031: val_accuracy did not improve from 0.81957
58/58 [==============================] - 8s 145ms/step - loss: 0.4812 - accuracy: 0.7783 - val_loss: 0.4331 - val_accuracy: 0.8152
Epoch 32/100
58/58 [==============================] - ETA: 0s - loss: 0.4756 - accuracy: 0.7750
Epoch 00032: val_accuracy did not improve from 0.81957
58/58 [==============================] - 8s 143ms/step - loss: 0.4756 - accuracy: 0.7750 - val_loss: 0.4225 - val_accuracy: 0.8152
Epoch 33/100
58/58 [==============================] - ETA: 0s - loss: 0.4916 - accuracy: 0.7658
Epoch 00033: val_accuracy improved from 0.81957 to 0.82174, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 164ms/step - loss: 0.4916 - accuracy: 0.7658 - val_loss: 0.4058 - val_accuracy: 0.8217
Epoch 34/100
58/58 [==============================] - ETA: 0s - loss: 0.4719 - accuracy: 0.7918
Epoch 00034: val_accuracy improved from 0.82174 to 0.82609, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 165ms/step - loss: 0.4719 - accuracy: 0.7918 - val_loss: 0.4083 - val_accuracy: 0.8261
Epoch 35/100
58/58 [==============================] - ETA: 0s - loss: 0.4569 - accuracy: 0.7897
Epoch 00035: val_accuracy improved from 0.82609 to 0.83043, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 9s 163ms/step - loss: 0.4569 - accuracy: 0.7897 - val_loss: 0.3994 - val_accuracy: 0.8304
Epoch 36/100
58/58 [==============================] - ETA: 0s - loss: 0.4793 - accuracy: 0.7810
Epoch 00036: val_accuracy did not improve from 0.83043
58/58 [==============================] - 8s 145ms/step - loss: 0.4793 - accuracy: 0.7810 - val_loss: 0.4111 - val_accuracy: 0.8087
Epoch 37/100
58/58 [==============================] - ETA: 0s - loss: 0.4563 - accuracy: 0.7935
Epoch 00037: val_accuracy did not improve from 0.83043
58/58 [==============================] - 8s 145ms/step - loss: 0.4563 - accuracy: 0.7935 - val_loss: 0.4377 - val_accuracy: 0.7978
Epoch 38/100
58/58 [==============================] - ETA: 0s - loss: 0.4511 - accuracy: 0.8082
Epoch 00038: val_accuracy did not improve from 0.83043
58/58 [==============================] - 8s 144ms/step - loss: 0.4511 - accuracy: 0.8082 - val_loss: 0.4065 - val_accuracy: 0.8109
Epoch 39/100
58/58 [==============================] - ETA: 0s - loss: 0.4446 - accuracy: 0.8114
Epoch 00039: val_accuracy did not improve from 0.83043
58/58 [==============================] - 8s 144ms/step - loss: 0.4446 - accuracy: 0.8114 - val_loss: 0.4074 - val_accuracy: 0.8261
Epoch 40/100
58/58 [==============================] - ETA: 0s - loss: 0.4251 - accuracy: 0.8163
Epoch 00040: val_accuracy did not improve from 0.83043
58/58 [==============================] - 8s 144ms/step - loss: 0.4251 - accuracy: 0.8163 - val_loss: 0.3914 - val_accuracy: 0.8239
Epoch 41/100
58/58 [==============================] - ETA: 0s - loss: 0.4557 - accuracy: 0.7918
Epoch 00041: val_accuracy did not improve from 0.83043
58/58 [==============================] - 8s 145ms/step - loss: 0.4557 - accuracy: 0.7918 - val_loss: 0.3983 - val_accuracy: 0.8022
Epoch 42/100
58/58 [==============================] - ETA: 0s - loss: 0.4410 - accuracy: 0.8130
Epoch 00042: val_accuracy did not improve from 0.83043
58/58 [==============================] - 8s 144ms/step - loss: 0.4410 - accuracy: 0.8130 - val_loss: 0.4105 - val_accuracy: 0.8065
Epoch 43/100
58/58 [==============================] - ETA: 0s - loss: 0.4326 - accuracy: 0.8087
Epoch 00043: val_accuracy improved from 0.83043 to 0.84130, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 164ms/step - loss: 0.4326 - accuracy: 0.8087 - val_loss: 0.3814 - val_accuracy: 0.8413
Epoch 44/100
58/58 [==============================] - ETA: 0s - loss: 0.4391 - accuracy: 0.7962
Epoch 00044: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 144ms/step - loss: 0.4391 - accuracy: 0.7962 - val_loss: 0.4172 - val_accuracy: 0.8043
Epoch 45/100
58/58 [==============================] - ETA: 0s - loss: 0.4337 - accuracy: 0.8060
Epoch 00045: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 144ms/step - loss: 0.4337 - accuracy: 0.8060 - val_loss: 0.3820 - val_accuracy: 0.8283
Epoch 46/100
58/58 [==============================] - ETA: 0s - loss: 0.4163 - accuracy: 0.8185
Epoch 00046: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 145ms/step - loss: 0.4163 - accuracy: 0.8185 - val_loss: 0.3943 - val_accuracy: 0.8283
Epoch 47/100
58/58 [==============================] - ETA: 0s - loss: 0.4401 - accuracy: 0.8049
Epoch 00047: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 144ms/step - loss: 0.4401 - accuracy: 0.8049 - val_loss: 0.3975 - val_accuracy: 0.8261
Epoch 48/100
58/58 [==============================] - ETA: 0s - loss: 0.4399 - accuracy: 0.8071
Epoch 00048: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 145ms/step - loss: 0.4399 - accuracy: 0.8071 - val_loss: 0.3951 - val_accuracy: 0.8152
Epoch 49/100
58/58 [==============================] - ETA: 0s - loss: 0.4282 - accuracy: 0.8147
Epoch 00049: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 145ms/step - loss: 0.4282 - accuracy: 0.8147 - val_loss: 0.3926 - val_accuracy: 0.8391
Epoch 50/100
58/58 [==============================] - ETA: 0s - loss: 0.4254 - accuracy: 0.8109
Epoch 00050: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 143ms/step - loss: 0.4254 - accuracy: 0.8109 - val_loss: 0.3769 - val_accuracy: 0.8283
Epoch 51/100
58/58 [==============================] - ETA: 0s - loss: 0.4290 - accuracy: 0.8043
Epoch 00051: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 144ms/step - loss: 0.4290 - accuracy: 0.8043 - val_loss: 0.3831 - val_accuracy: 0.8217
Epoch 52/100
58/58 [==============================] - ETA: 0s - loss: 0.4076 - accuracy: 0.8250
Epoch 00052: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 144ms/step - loss: 0.4076 - accuracy: 0.8250 - val_loss: 0.3854 - val_accuracy: 0.8196
Epoch 53/100
58/58 [==============================] - ETA: 0s - loss: 0.4107 - accuracy: 0.8245
Epoch 00053: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 143ms/step - loss: 0.4107 - accuracy: 0.8245 - val_loss: 0.4033 - val_accuracy: 0.8109
Epoch 54/100
58/58 [==============================] - ETA: 0s - loss: 0.4226 - accuracy: 0.8234
Epoch 00054: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 144ms/step - loss: 0.4226 - accuracy: 0.8234 - val_loss: 0.3815 - val_accuracy: 0.8304
Epoch 55/100
58/58 [==============================] - ETA: 0s - loss: 0.4124 - accuracy: 0.8250
Epoch 00055: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 144ms/step - loss: 0.4124 - accuracy: 0.8250 - val_loss: 0.3848 - val_accuracy: 0.8239
Epoch 56/100
58/58 [==============================] - ETA: 0s - loss: 0.4148 - accuracy: 0.8174
Epoch 00056: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 145ms/step - loss: 0.4148 - accuracy: 0.8174 - val_loss: 0.3773 - val_accuracy: 0.8370
Epoch 57/100
58/58 [==============================] - ETA: 0s - loss: 0.4014 - accuracy: 0.8283
Epoch 00057: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 145ms/step - loss: 0.4014 - accuracy: 0.8283 - val_loss: 0.3830 - val_accuracy: 0.8326
Epoch 58/100
58/58 [==============================] - ETA: 0s - loss: 0.4170 - accuracy: 0.8152
Epoch 00058: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 145ms/step - loss: 0.4170 - accuracy: 0.8152 - val_loss: 0.3732 - val_accuracy: 0.8326
Epoch 59/100
58/58 [==============================] - ETA: 0s - loss: 0.4145 - accuracy: 0.8109
Epoch 00059: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 144ms/step - loss: 0.4145 - accuracy: 0.8109 - val_loss: 0.3853 - val_accuracy: 0.8283
Epoch 60/100
58/58 [==============================] - ETA: 0s - loss: 0.3980 - accuracy: 0.8266
Epoch 00060: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 145ms/step - loss: 0.3980 - accuracy: 0.8266 - val_loss: 0.3991 - val_accuracy: 0.8239
Epoch 61/100
58/58 [==============================] - ETA: 0s - loss: 0.4002 - accuracy: 0.8266
Epoch 00061: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 144ms/step - loss: 0.4002 - accuracy: 0.8266 - val_loss: 0.3827 - val_accuracy: 0.8326
Epoch 62/100
58/58 [==============================] - ETA: 0s - loss: 0.4207 - accuracy: 0.8076
Epoch 00062: val_accuracy did not improve from 0.84130
58/58 [==============================] - 8s 144ms/step - loss: 0.4207 - accuracy: 0.8076 - val_loss: 0.4016 - val_accuracy: 0.8109
Epoch 63/100
58/58 [==============================] - ETA: 0s - loss: 0.4004 - accuracy: 0.8201
Epoch 00063: val_accuracy improved from 0.84130 to 0.84348, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 9s 163ms/step - loss: 0.4004 - accuracy: 0.8201 - val_loss: 0.3846 - val_accuracy: 0.8435
Epoch 64/100
58/58 [==============================] - ETA: 0s - loss: 0.4007 - accuracy: 0.8239
Epoch 00064: val_accuracy did not improve from 0.84348
58/58 [==============================] - 8s 145ms/step - loss: 0.4007 - accuracy: 0.8239 - val_loss: 0.3945 - val_accuracy: 0.8217
Epoch 65/100
58/58 [==============================] - ETA: 0s - loss: 0.3967 - accuracy: 0.8299
Epoch 00065: val_accuracy did not improve from 0.84348
58/58 [==============================] - 8s 143ms/step - loss: 0.3967 - accuracy: 0.8299 - val_loss: 0.3742 - val_accuracy: 0.8348
Epoch 66/100
58/58 [==============================] - ETA: 0s - loss: 0.3954 - accuracy: 0.8310
Epoch 00066: val_accuracy did not improve from 0.84348
58/58 [==============================] - 8s 145ms/step - loss: 0.3954 - accuracy: 0.8310 - val_loss: 0.3795 - val_accuracy: 0.8283
Epoch 67/100
58/58 [==============================] - ETA: 0s - loss: 0.3803 - accuracy: 0.8304
Epoch 00067: val_accuracy did not improve from 0.84348
58/58 [==============================] - 8s 145ms/step - loss: 0.3803 - accuracy: 0.8304 - val_loss: 0.4279 - val_accuracy: 0.8130
Epoch 68/100
58/58 [==============================] - ETA: 0s - loss: 0.3920 - accuracy: 0.8234
Epoch 00068: val_accuracy did not improve from 0.84348
58/58 [==============================] - 8s 144ms/step - loss: 0.3920 - accuracy: 0.8234 - val_loss: 0.3828 - val_accuracy: 0.8348
Epoch 69/100
58/58 [==============================] - ETA: 0s - loss: 0.3988 - accuracy: 0.8223
Epoch 00069: val_accuracy did not improve from 0.84348
58/58 [==============================] - 8s 145ms/step - loss: 0.3988 - accuracy: 0.8223 - val_loss: 0.3923 - val_accuracy: 0.8326
Epoch 70/100
58/58 [==============================] - ETA: 0s - loss: 0.3909 - accuracy: 0.8277
Epoch 00070: val_accuracy did not improve from 0.84348
58/58 [==============================] - 8s 145ms/step - loss: 0.3909 - accuracy: 0.8277 - val_loss: 0.3816 - val_accuracy: 0.8370
Epoch 71/100
58/58 [==============================] - ETA: 0s - loss: 0.3930 - accuracy: 0.8288
Epoch 00071: val_accuracy did not improve from 0.84348
58/58 [==============================] - 8s 145ms/step - loss: 0.3930 - accuracy: 0.8288 - val_loss: 0.3832 - val_accuracy: 0.8391
Epoch 72/100
58/58 [==============================] - ETA: 0s - loss: 0.3895 - accuracy: 0.8299
Epoch 00072: val_accuracy did not improve from 0.84348
58/58 [==============================] - 8s 143ms/step - loss: 0.3895 - accuracy: 0.8299 - val_loss: 0.3739 - val_accuracy: 0.8348
Epoch 73/100
58/58 [==============================] - ETA: 0s - loss: 0.3923 - accuracy: 0.8293
Epoch 00073: val_accuracy did not improve from 0.84348
58/58 [==============================] - 8s 142ms/step - loss: 0.3923 - accuracy: 0.8293 - val_loss: 0.3692 - val_accuracy: 0.8348
Epoch 74/100
58/58 [==============================] - ETA: 0s - loss: 0.3830 - accuracy: 0.8326
Epoch 00074: val_accuracy did not improve from 0.84348
58/58 [==============================] - 8s 144ms/step - loss: 0.3830 - accuracy: 0.8326 - val_loss: 0.3778 - val_accuracy: 0.8391
Epoch 75/100
58/58 [==============================] - ETA: 0s - loss: 0.3734 - accuracy: 0.8397
Epoch 00075: val_accuracy improved from 0.84348 to 0.85435, saving model to ./base.model
INFO:tensorflow:Assets written to: ./base.model/assets
58/58 [==============================] - 10s 165ms/step - loss: 0.3734 - accuracy: 0.8397 - val_loss: 0.3635 - val_accuracy: 0.8543
Epoch 76/100
58/58 [==============================] - ETA: 0s - loss: 0.3778 - accuracy: 0.8391
Epoch 00076: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 144ms/step - loss: 0.3778 - accuracy: 0.8391 - val_loss: 0.3526 - val_accuracy: 0.8457
Epoch 77/100
58/58 [==============================] - ETA: 0s - loss: 0.3860 - accuracy: 0.8326
Epoch 00077: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3860 - accuracy: 0.8326 - val_loss: 0.3591 - val_accuracy: 0.8391
Epoch 78/100
58/58 [==============================] - ETA: 0s - loss: 0.3668 - accuracy: 0.8440
Epoch 00078: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3668 - accuracy: 0.8440 - val_loss: 0.3725 - val_accuracy: 0.8239
Epoch 79/100
58/58 [==============================] - ETA: 0s - loss: 0.3742 - accuracy: 0.8348
Epoch 00079: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 144ms/step - loss: 0.3742 - accuracy: 0.8348 - val_loss: 0.3659 - val_accuracy: 0.8283
Epoch 80/100
58/58 [==============================] - ETA: 0s - loss: 0.3749 - accuracy: 0.8446
Epoch 00080: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3749 - accuracy: 0.8446 - val_loss: 0.3591 - val_accuracy: 0.8391
Epoch 81/100
58/58 [==============================] - ETA: 0s - loss: 0.4005 - accuracy: 0.8277
Epoch 00081: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.4005 - accuracy: 0.8277 - val_loss: 0.3537 - val_accuracy: 0.8370
Epoch 82/100
58/58 [==============================] - ETA: 0s - loss: 0.3801 - accuracy: 0.8370
Epoch 00082: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3801 - accuracy: 0.8370 - val_loss: 0.3574 - val_accuracy: 0.8370
Epoch 83/100
58/58 [==============================] - ETA: 0s - loss: 0.3846 - accuracy: 0.8288
Epoch 00083: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3846 - accuracy: 0.8288 - val_loss: 0.3885 - val_accuracy: 0.8174
Epoch 84/100
58/58 [==============================] - ETA: 0s - loss: 0.3665 - accuracy: 0.8375
Epoch 00084: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3665 - accuracy: 0.8375 - val_loss: 0.3655 - val_accuracy: 0.8435
Epoch 85/100
58/58 [==============================] - ETA: 0s - loss: 0.3596 - accuracy: 0.8386
Epoch 00085: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 144ms/step - loss: 0.3596 - accuracy: 0.8386 - val_loss: 0.3475 - val_accuracy: 0.8370
Epoch 86/100
58/58 [==============================] - ETA: 0s - loss: 0.3821 - accuracy: 0.8293
Epoch 00086: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 144ms/step - loss: 0.3821 - accuracy: 0.8293 - val_loss: 0.3683 - val_accuracy: 0.8304
Epoch 87/100
58/58 [==============================] - ETA: 0s - loss: 0.3567 - accuracy: 0.8467
Epoch 00087: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 144ms/step - loss: 0.3567 - accuracy: 0.8467 - val_loss: 0.3755 - val_accuracy: 0.8348
Epoch 88/100
58/58 [==============================] - ETA: 0s - loss: 0.3534 - accuracy: 0.8402
Epoch 00088: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3534 - accuracy: 0.8402 - val_loss: 0.3522 - val_accuracy: 0.8500
Epoch 89/100
58/58 [==============================] - ETA: 0s - loss: 0.3574 - accuracy: 0.8473
Epoch 00089: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 141ms/step - loss: 0.3574 - accuracy: 0.8473 - val_loss: 0.3607 - val_accuracy: 0.8391
Epoch 90/100
58/58 [==============================] - ETA: 0s - loss: 0.3778 - accuracy: 0.8424
Epoch 00090: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3778 - accuracy: 0.8424 - val_loss: 0.3668 - val_accuracy: 0.8391
Epoch 91/100
58/58 [==============================] - ETA: 0s - loss: 0.3666 - accuracy: 0.8402
Epoch 00091: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 146ms/step - loss: 0.3666 - accuracy: 0.8402 - val_loss: 0.3550 - val_accuracy: 0.8435
Epoch 92/100
58/58 [==============================] - ETA: 0s - loss: 0.3610 - accuracy: 0.8380
Epoch 00092: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 144ms/step - loss: 0.3610 - accuracy: 0.8380 - val_loss: 0.3529 - val_accuracy: 0.8500
Epoch 93/100
58/58 [==============================] - ETA: 0s - loss: 0.3774 - accuracy: 0.8375
Epoch 00093: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3774 - accuracy: 0.8375 - val_loss: 0.3502 - val_accuracy: 0.8522
Epoch 94/100
58/58 [==============================] - ETA: 0s - loss: 0.3610 - accuracy: 0.8462
Epoch 00094: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3610 - accuracy: 0.8462 - val_loss: 0.3542 - val_accuracy: 0.8500
Epoch 95/100
58/58 [==============================] - ETA: 0s - loss: 0.3416 - accuracy: 0.8582
Epoch 00095: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3416 - accuracy: 0.8582 - val_loss: 0.3481 - val_accuracy: 0.8413
Epoch 96/100
58/58 [==============================] - ETA: 0s - loss: 0.3589 - accuracy: 0.8440
Epoch 00096: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3589 - accuracy: 0.8440 - val_loss: 0.3524 - val_accuracy: 0.8522
Epoch 97/100
58/58 [==============================] - ETA: 0s - loss: 0.3720 - accuracy: 0.8391
Epoch 00097: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 144ms/step - loss: 0.3720 - accuracy: 0.8391 - val_loss: 0.3576 - val_accuracy: 0.8543
Epoch 98/100
58/58 [==============================] - ETA: 0s - loss: 0.3395 - accuracy: 0.8620
Epoch 00098: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 144ms/step - loss: 0.3395 - accuracy: 0.8620 - val_loss: 0.3619 - val_accuracy: 0.8500
Epoch 99/100
58/58 [==============================] - ETA: 0s - loss: 0.3638 - accuracy: 0.8435
Epoch 00099: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 143ms/step - loss: 0.3638 - accuracy: 0.8435 - val_loss: 0.3626 - val_accuracy: 0.8370
Epoch 100/100
58/58 [==============================] - ETA: 0s - loss: 0.3451 - accuracy: 0.8516
Epoch 00100: val_accuracy did not improve from 0.85435
58/58 [==============================] - 8s 145ms/step - loss: 0.3451 - accuracy: 0.8516 - val_loss: 0.3697 - val_accuracy: 0.8435

Function to plot model metrics

In [8]:
def show_final_history(history):
    fig, ax = plt.subplots(1, 2, figsize=(15,5))
    ax[0].set_title('loss')
    ax[0].plot(history.epoch, history.history["loss"], label="Train loss")
    ax[0].plot(history.epoch, history.history["val_loss"], label="Validation loss")
    ax[1].set_title('acc')
    ax[1].plot(history.epoch, history.history["accuracy"], label="Train acc")
    ax[1].plot(history.epoch, history.history["val_accuracy"], label="Validation acc")
    ax[0].legend()
    ax[1].legend()

Loss/Accuracy vs Epoch

In [9]:
show_final_history(history)
model.save("model.h5")
print("Weights Saved")
Weights Saved

Making predictions on the test images

In [10]:
labels = ["00-damage","01-whole"]

it = iter(testing_set)
batch = next(it) # Gets a batch of 16 test images

fig, axes = plt.subplots(3, 3, figsize=(10,10))
fig.tight_layout()
fig.subplots_adjust(hspace=.25)

for i in range(3):
    for j in range(3):
        ax = axes[i,j]
        image = batch[0][i*3+j]
        net_input = image.reshape((1, 64, 64, 3))
        truth = np.argmax(batch[1][i*3+j])
        prediction = np.argmax(model.predict(net_input))
        ax.set_title('Label: %s\nPrediction: %s' % (labels[truth].capitalize(), labels[prediction].capitalize()))
        ax.imshow(image)

Function to make predictions on custom images

In [11]:
def custom_predictions(path):
    img = ig.load_img(path, target_size=(64, 64))
    plt.imshow(img)
    img = np.expand_dims(img, axis=0)
    img.reshape(1,64,64,3)
    prediction = np.argmax(model.predict(img))
#     result=loaded_model.predict_classes(img)
    plt.title(labels[prediction])
    plt.show()

Displaying predicted label along with the input image

In [13]:
custom_predictions('data1a/training/00-damage/0001.JPEG')
In [15]:
custom_predictions('data1a/training/01-whole/0002.jpg')
In [16]:
model.save('Car_damage_prediction.h')
INFO:tensorflow:Assets written to: Car_damage_prediction.h/assets
In [ ]: