Loading

Lidar Car Detection

Keras Classification for Lidar Car Detection (counting)

Count the cars in 3D lidar data

eric_parisot

In this dataset we got 3D points cloud from lidars and the task is to train a classifier to count other cars on the road.

To make things easyer to work with, we are going to remove useless data (over the car, floor informations), flatten the 3D points to obtain 2D images (view from top), augment the quantity of data (x4) by rotating the images and then train a Keras NN classifer on it...

Let's go !

Starter Code for Lidar Car Detection

What we are going to Learn

  • Learning about how lidar works
  • data normalisation
  • data augmentation
  • Using Keras for binary classification.

Note : Create a copy of the notebook and use the copy for submission. Go to File > Save a Copy in Drive to create a new copy

Hi fellas !

In this dataset we got 3D points cloud from lidars and the task is to train a classifier to count other cars on the road. To make things easyer to work with, we are going to remove useless data (over the car, floor informations), flatten the 3D points to obtain 2D images (view from top), augment the quantity of data (x4) by rotating the images and then train a Keras NN classifer on it... Let's go !

Downloading Dataset

Installing aicrowd-cli

In [1]:
!pip install aicrowd-cli
%load_ext aicrowd.magic
Requirement already satisfied: aicrowd-cli in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (0.1.8)
Requirement already satisfied: click<8,>=7.1.2 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from aicrowd-cli) (7.1.2)
Requirement already satisfied: tqdm<5,>=4.56.0 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from aicrowd-cli) (4.61.2)
Requirement already satisfied: rich<11,>=10.0.0 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from aicrowd-cli) (10.6.0)
Requirement already satisfied: requests-toolbelt<1,>=0.9.1 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from aicrowd-cli) (0.9.1)
Requirement already satisfied: toml<1,>=0.10.2 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from aicrowd-cli) (0.10.2)
Requirement already satisfied: requests<3,>=2.25.1 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from aicrowd-cli) (2.25.1)
Requirement already satisfied: GitPython==3.1.18 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from aicrowd-cli) (3.1.18)
Requirement already satisfied: gitdb<5,>=4.0.1 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from GitPython==3.1.18->aicrowd-cli) (4.0.7)
Requirement already satisfied: smmap<5,>=3.0.1 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from gitdb<5,>=4.0.1->GitPython==3.1.18->aicrowd-cli) (4.0.0)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from requests<3,>=2.25.1->aicrowd-cli) (2021.5.30)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from requests<3,>=2.25.1->aicrowd-cli) (1.26.5)
Requirement already satisfied: idna<3,>=2.5 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from requests<3,>=2.25.1->aicrowd-cli) (2.10)
Requirement already satisfied: chardet<5,>=3.0.2 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from requests<3,>=2.25.1->aicrowd-cli) (4.0.0)
Requirement already satisfied: commonmark<0.10.0,>=0.9.0 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from rich<11,>=10.0.0->aicrowd-cli) (0.9.1)
Requirement already satisfied: pygments<3.0.0,>=2.6.0 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from rich<11,>=10.0.0->aicrowd-cli) (2.9.0)
Requirement already satisfied: colorama<0.5.0,>=0.4.0 in c:\users\rock_\appdata\local\programs\python\python39\lib\site-packages (from rich<11,>=10.0.0->aicrowd-cli) (0.4.4)
WARNING: You are using pip version 21.1.3; however, version 21.2.4 is available.
You should consider upgrading via the 'c:\users\rock_\appdata\local\programs\python\python39\python.exe -m pip install --upgrade pip' command.
In [2]:
%aicrowd login
Please login here: https://api.aicrowd.com/auth/FJDaE6YjE__bJBYjYrp4qRGURS94q3xFToMLEbuYi4Q
API Key valid
Saved API Key successfully!
In [3]:
!rm -rf data
!mkdir data
%aicrowd ds dl -c lidar-car-detection -o data

Importing Libraries

In [1]:
import pandas as pd
import numpy as np
from sklearn.model_selection import StratifiedKFold
import random
import os
import matplotlib.pyplot as plt
import plotly.graph_objects as go
%matplotlib notebook

import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
import tensorflow.keras.backend as K

Reading the dataset

In [2]:
# Reading the training dataset
train_data = np.load("data/train.npz", allow_pickle=True)
test_data = np.load("data/test.npz", allow_pickle=True)
train_data = train_data['train']
test_data = test_data['test']
train_data.shape, test_data.shape
Out[2]:
((400, 2), (601,))

Visualizing the dataset

In this section, we will be visualizing a sample 3D lidar data

In [3]:
# Getting a random 3D lidar sample data
INDEX = random.randint(0, train_data.shape[0]-1)

# Getting the individual x,y and z points.
x = train_data[INDEX][0][:, 0].tolist()
y = train_data[INDEX][0][:, 1].tolist()
z = train_data[INDEX][0][:, 2].tolist()

# Label for the corrosponding sample ( no. of cars )
label  = train_data[INDEX][1]

# Generating the 3D graph
fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z,
                                   mode='markers',
                                   marker=dict(
                                   size=1,       
                                   colorscale='Viridis',
                                   opacity=0.8))])
print("No. of cars : ", label)
fig.show()
No. of cars :  4

Labels Distribution

In [4]:
print(train_data[:, 1].max())
plt.figure()
plt.hist(train_data[:, 1])
7
Out[4]:
(array([78., 79., 76.,  0., 67., 63.,  0., 33.,  2.,  2.]),
 array([0.0, 0.7, 1.4, 2.0999999999999996, 2.8, 3.5, 4.199999999999999,
        4.8999999999999995, 5.6, 6.3, 7.0], dtype=object),
 <BarContainer object of 10 artists>)

Can you try finding cars in this 3d data ?

turn X and X_test to DataFrames

In [38]:
X = np.array([sample for sample in train_data[:, 0].tolist()])
Y = np.array(train_data[:, 1], dtype=np.int)
X_test = np.array([sample for sample in test_data.tolist()])

m,n,r = X.shape
out_arr = np.column_stack((np.repeat(np.arange(m),n), X.reshape(m*n,-1)))
df = pd.DataFrame(out_arr)
df.columns = ["idx", "x", "y", "z"]

m,n,r = X_test.shape
out_arr = np.column_stack((np.repeat(np.arange(m),n), X_test.reshape(m*n,-1)))
df_test = pd.DataFrame(out_arr)
df_test.columns = ["idx", "x", "y", "z"]
In [ ]:

In [39]:
df, df_test
Out[39]:
(            idx          x          y          z
 0           0.0 -12.568300  -5.545469   7.304270
 1           0.0  -2.542295  16.209188   8.723945
 2           0.0   7.204814  -5.573594   4.843357
 3           0.0  15.317773 -21.307129  13.952970
 4           0.0  14.821630 -21.701406  13.973258
 ...         ...        ...        ...        ...
 15517195  399.0  -1.000000  -1.000000  -1.000000
 15517196  399.0  -1.000000  -1.000000  -1.000000
 15517197  399.0  -1.000000  -1.000000  -1.000000
 15517198  399.0  -1.000000  -1.000000  -1.000000
 15517199  399.0  -1.000000  -1.000000  -1.000000
 
 [15517200 rows x 4 columns],
             idx         x          y         z
 0           0.0  7.224199  -8.725420  6.023175
 1           0.0  6.964531  -8.836748  5.982453
 2           0.0  6.134199 -11.935176  7.135154
 3           0.0 -6.177275  -5.975996  4.569962
 4           0.0  7.622715  -8.767959  6.069382
 ...         ...       ...        ...       ...
 23314588  600.0 -1.000000  -1.000000 -1.000000
 23314589  600.0 -1.000000  -1.000000 -1.000000
 23314590  600.0 -1.000000  -1.000000 -1.000000
 23314591  600.0 -1.000000  -1.000000 -1.000000
 23314592  600.0 -1.000000  -1.000000 -1.000000
 
 [23314593 rows x 4 columns])

Normalize

In [15]:
df["x"] = df["x"] / 60 + 0.5
df["y"] = df["y"] / 60 + 0.5
df["z"] = df["z"] / 16 + 0.2

df_test["x"] = df_test["x"] / 60 + 0.5
df_test["y"] = df_test["y"] / 60 + 0.5
df_test["z"] = df_test["z"] / 16 + 0.2

Drop low / high z values rows

In [16]:
df = df.drop(df[(df["z"] > 0.25) | (df["z"] < 0.095)].index).reset_index()
df = df.drop(["z",], axis=1).reset_index()

df_test = df_test.drop(df_test[(df_test["z"] > 0.25) | (df_test["z"] < 0.095)].index).reset_index()
df_test = df_test.drop(["z",], axis=1).reset_index()
In [17]:
# Getting a random 3D lidar sample data
INDEX = random.randint(0, train_data.shape[0]-1)

# Getting the individual x,y and z points.
x = df["x"][df["idx"] == INDEX]
y = df["y"][df["idx"] == INDEX]


plt.figure()
plt.plot(x)
plt.plot(y)


# Label for the corrosponding sample ( no. of cars )
label  = Y[INDEX]

# Generating the 3D graph
fig = go.Figure(data=[go.Scatter(x=x, y=y,
                                   mode='markers',
                                   marker=dict(
                                   size=1,       
                                   colorscale='Viridis',
                                   opacity=0.8))])
print("No. of cars : ", label)
fig.show()
No. of cars :  [0. 0. 0. 1. 0. 0. 0. 0.]

Turn 3D points cloud to flat images

In [18]:
img_size = 128

images = np.zeros((int(df["idx"].max()+1), img_size, img_size, 1))
images_test = np.zeros((int(df_test["idx"].max()+1), img_size, img_size, 1))

def make_img(points, images):
    for i, r in points.iterrows():
        images[int(r["idx"])][int(r["x"]*img_size)][int(r["y"]*img_size)] = [1]

df.groupby("idx").apply(lambda x: make_img(x, images))
df_test.groupby("idx").apply(lambda x: make_img(x, images_test))
Out[18]:
In [19]:
plt.figure()
plt.imshow(images[INDEX], cmap="gray")
plt.show()

Data augmentation (+ 90, 180, 270 rotations)

In [42]:
augmented_images = np.vstack((np.rot90(images, axes=(1, 2)), images))
augmented_images = np.vstack((np.rot90(images, k=2, axes=(1, 2)), augmented_images))
augmented_images = np.vstack((np.rot90(images, k=3, axes=(1, 2)), augmented_images))
augmented_Y = np.hstack((Y, Y, Y, Y))
In [43]:
augmented_images.shape, augmented_Y.shape
Out[43]:
((1600, 128, 128, 1), (1600,))

Splitting the dataset

In [47]:
# Splitting the dataset into training and testing
skf = StratifiedKFold(n_splits=5)
for train_index, test_index in skf.split(augmented_images, augmented_Y):
    X_train, X_val = augmented_images[train_index], augmented_images[test_index]
    Y_train, Y_val = augmented_Y[train_index], augmented_Y[test_index]

Make targets categorical

In [48]:
cat_Y = to_categorical(augmented_Y, num_classes=8)
Y_train = to_categorical(Y_train, num_classes=8)
Y_val = to_categorical(Y_val, num_classes=8)

#X_train, X_val, Y_train, Y_val = train_test_split(images, Y, test_size=0.2)
print(X_train.shape, X_val.shape, Y_train.shape, Y_val.shape)
(1280, 128, 128, 1) (320, 128, 128, 1) (1280, 8) (320, 8)

Training the model

In [74]:
K.clear_session()
############################################################# 

img_in = Input(shape=(img_size, img_size, 1), name='img_in')
x = img_in

x = Convolution2D(32, (3,3), strides=(2,2), activation="relu")(x)       
x = MaxPooling2D(2)(x) # MaxPooling will preserve the "geographical" information of images
x = Dropout(0.4)(x)
x = Convolution2D(64, (3,3), strides=(2,2), activation="relu")(x)       
x = MaxPooling2D(2)(x)
x = Dropout(0.3)(x)
x = Convolution2D(128, (1,1), activation="relu")(x)       
x = MaxPooling2D(2)(x)
x = Dropout(0.2)(x)
x = Flatten(name='flattened')(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.2)(x)
# Output
out = Dense(8, activation='softmax')(x)
# Compile Model
model = Model(inputs=[img_in], outputs=[out])
model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=1e-3), metrics=['accuracy'])

model.summary()
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
img_in (InputLayer)          [(None, 128, 128, 1)]     0         
_________________________________________________________________
conv2d (Conv2D)              (None, 63, 63, 32)        320       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 31, 31, 32)        0         
_________________________________________________________________
dropout (Dropout)            (None, 31, 31, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 15, 15, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 64)          0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 7, 7, 64)          0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 7, 7, 128)         8320      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 3, 3, 128)         0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 3, 3, 128)         0         
_________________________________________________________________
flattened (Flatten)          (None, 1152)              0         
_________________________________________________________________
dense (Dense)                (None, 256)               295168    
_________________________________________________________________
dropout_3 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 8)                 2056      
=================================================================
Total params: 324,360
Trainable params: 324,360
Non-trainable params: 0
_________________________________________________________________
In [75]:
#save best model if model improved
model_name = "car_lidar_detect.h5"
best_checkpoint = tf.keras.callbacks.ModelCheckpoint(model_name, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
In [76]:
#h = model.fit(X_train, Y_train, validation_data=(X_val, Y_val), batch_size=32, epochs=600, callbacks=[best_checkpoint])
h = model.fit(augmented_images, cat_Y, batch_size=32, epochs=200)
Epoch 1/200
50/50 [==============================] - 5s 9ms/step - loss: 1.8475 - accuracy: 0.2119
Epoch 2/200
50/50 [==============================] - 0s 10ms/step - loss: 1.8142 - accuracy: 0.2013
Epoch 3/200
50/50 [==============================] - 5s 97ms/step - loss: 1.7887 - accuracy: 0.2331
Epoch 4/200
50/50 [==============================] - 0s 9ms/step - loss: 1.7790 - accuracy: 0.2469
Epoch 5/200
50/50 [==============================] - 5s 100ms/step - loss: 1.7481 - accuracy: 0.2656
Epoch 6/200
50/50 [==============================] - 0s 8ms/step - loss: 1.7204 - accuracy: 0.2937
Epoch 7/200
50/50 [==============================] - 0s 9ms/step - loss: 1.6707 - accuracy: 0.3288
Epoch 8/200
50/50 [==============================] - 5s 99ms/step - loss: 1.6088 - accuracy: 0.3431
Epoch 9/200
50/50 [==============================] - 0s 8ms/step - loss: 1.5502 - accuracy: 0.3837
Epoch 10/200
50/50 [==============================] - 5s 102ms/step - loss: 1.5294 - accuracy: 0.3894
Epoch 11/200
50/50 [==============================] - 0s 9ms/step - loss: 1.4622 - accuracy: 0.4319
Epoch 12/200
50/50 [==============================] - 0s 9ms/step - loss: 1.4316 - accuracy: 0.4475
Epoch 13/200
50/50 [==============================] - 5s 99ms/step - loss: 1.3665 - accuracy: 0.4650
Epoch 14/200
50/50 [==============================] - 0s 9ms/step - loss: 1.3061 - accuracy: 0.4888
Epoch 15/200
50/50 [==============================] - 5s 99ms/step - loss: 1.3009 - accuracy: 0.5013
Epoch 16/200
50/50 [==============================] - 0s 8ms/step - loss: 1.2318 - accuracy: 0.5238
Epoch 17/200
50/50 [==============================] - 0s 9ms/step - loss: 1.1833 - accuracy: 0.5500
Epoch 18/200
50/50 [==============================] - 5s 96ms/step - loss: 1.1617 - accuracy: 0.5569
Epoch 19/200
50/50 [==============================] - 0s 8ms/step - loss: 1.1044 - accuracy: 0.5706
Epoch 20/200
50/50 [==============================] - 5s 98ms/step - loss: 1.0790 - accuracy: 0.6037
Epoch 21/200
50/50 [==============================] - 0s 8ms/step - loss: 1.0483 - accuracy: 0.6050
Epoch 22/200
50/50 [==============================] - 0s 9ms/step - loss: 1.0192 - accuracy: 0.6019
Epoch 23/200
50/50 [==============================] - 5s 97ms/step - loss: 0.9771 - accuracy: 0.6269
Epoch 24/200
50/50 [==============================] - 0s 9ms/step - loss: 0.9584 - accuracy: 0.6363
Epoch 25/200
50/50 [==============================] - 5s 98ms/step - loss: 0.8961 - accuracy: 0.6569
Epoch 26/200
50/50 [==============================] - 0s 9ms/step - loss: 0.8808 - accuracy: 0.6756
Epoch 27/200
50/50 [==============================] - 0s 9ms/step - loss: 0.8641 - accuracy: 0.6844
Epoch 28/200
50/50 [==============================] - 5s 98ms/step - loss: 0.8160 - accuracy: 0.6875
Epoch 29/200
50/50 [==============================] - 0s 9ms/step - loss: 0.7868 - accuracy: 0.7131
Epoch 30/200
50/50 [==============================] - 5s 99ms/step - loss: 0.7594 - accuracy: 0.7250
Epoch 31/200
50/50 [==============================] - 0s 9ms/step - loss: 0.7318 - accuracy: 0.7362
Epoch 32/200
50/50 [==============================] - 0s 10ms/step - loss: 0.7086 - accuracy: 0.7494
Epoch 33/200
50/50 [==============================] - 5s 96ms/step - loss: 0.6900 - accuracy: 0.7412
Epoch 34/200
50/50 [==============================] - 0s 10ms/step - loss: 0.6605 - accuracy: 0.7581
Epoch 35/200
50/50 [==============================] - 5s 97ms/step - loss: 0.6188 - accuracy: 0.7625
Epoch 36/200
50/50 [==============================] - 0s 9ms/step - loss: 0.6321 - accuracy: 0.7725
Epoch 37/200
50/50 [==============================] - 0s 9ms/step - loss: 0.5921 - accuracy: 0.7900
Epoch 38/200
50/50 [==============================] - 5s 99ms/step - loss: 0.5967 - accuracy: 0.7756
Epoch 39/200
50/50 [==============================] - 0s 9ms/step - loss: 0.5628 - accuracy: 0.7975
Epoch 40/200
50/50 [==============================] - 5s 97ms/step - loss: 0.5735 - accuracy: 0.7969
Epoch 41/200
50/50 [==============================] - 0s 9ms/step - loss: 0.5313 - accuracy: 0.8081
Epoch 42/200
50/50 [==============================] - 0s 10ms/step - loss: 0.5374 - accuracy: 0.7931
Epoch 43/200
50/50 [==============================] - 5s 95ms/step - loss: 0.5045 - accuracy: 0.8156
Epoch 44/200
50/50 [==============================] - 0s 9ms/step - loss: 0.5030 - accuracy: 0.8131
Epoch 45/200
50/50 [==============================] - 5s 96ms/step - loss: 0.4637 - accuracy: 0.8325
Epoch 46/200
50/50 [==============================] - 0s 9ms/step - loss: 0.4640 - accuracy: 0.8363
Epoch 47/200
50/50 [==============================] - 0s 9ms/step - loss: 0.4468 - accuracy: 0.8425: 0s - loss: 0.4086 - accura
Epoch 48/200
50/50 [==============================] - 5s 95ms/step - loss: 0.4619 - accuracy: 0.8394
Epoch 49/200
50/50 [==============================] - 0s 9ms/step - loss: 0.4330 - accuracy: 0.8500
Epoch 50/200
50/50 [==============================] - 5s 97ms/step - loss: 0.3948 - accuracy: 0.8569
Epoch 51/200
50/50 [==============================] - 0s 9ms/step - loss: 0.4037 - accuracy: 0.8506
Epoch 52/200
50/50 [==============================] - 5s 96ms/step - loss: 0.3929 - accuracy: 0.8606
Epoch 53/200
50/50 [==============================] - 0s 9ms/step - loss: 0.3315 - accuracy: 0.8800
Epoch 54/200
50/50 [==============================] - 0s 9ms/step - loss: 0.3900 - accuracy: 0.8606
Epoch 55/200
50/50 [==============================] - 5s 94ms/step - loss: 0.3910 - accuracy: 0.8600
Epoch 56/200
50/50 [==============================] - 0s 9ms/step - loss: 0.3284 - accuracy: 0.8800
Epoch 57/200
50/50 [==============================] - 5s 98ms/step - loss: 0.3365 - accuracy: 0.8850
Epoch 58/200
50/50 [==============================] - 0s 9ms/step - loss: 0.3198 - accuracy: 0.8869
Epoch 59/200
50/50 [==============================] - 0s 9ms/step - loss: 0.3651 - accuracy: 0.8725
Epoch 60/200
50/50 [==============================] - 5s 96ms/step - loss: 0.3180 - accuracy: 0.8881
Epoch 61/200
50/50 [==============================] - 0s 9ms/step - loss: 0.3148 - accuracy: 0.8925
Epoch 62/200
50/50 [==============================] - 5s 96ms/step - loss: 0.3128 - accuracy: 0.8819
Epoch 63/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2862 - accuracy: 0.8988
Epoch 64/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2953 - accuracy: 0.8900
Epoch 65/200
50/50 [==============================] - 5s 8ms/step - loss: 0.2823 - accuracy: 0.8975
Epoch 66/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2975 - accuracy: 0.8906
Epoch 67/200
50/50 [==============================] - 5s 97ms/step - loss: 0.2728 - accuracy: 0.9050
Epoch 68/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2609 - accuracy: 0.9075
Epoch 69/200
50/50 [==============================] - 5s 95ms/step - loss: 0.2825 - accuracy: 0.8994
Epoch 70/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2699 - accuracy: 0.8969
Epoch 71/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2526 - accuracy: 0.9094
Epoch 72/200
50/50 [==============================] - 5s 96ms/step - loss: 0.2775 - accuracy: 0.8981
Epoch 73/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2724 - accuracy: 0.9094
Epoch 74/200
50/50 [==============================] - 5s 97ms/step - loss: 0.2838 - accuracy: 0.8981
Epoch 75/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2644 - accuracy: 0.9112
Epoch 76/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2003 - accuracy: 0.9331
Epoch 77/200
50/50 [==============================] - 5s 96ms/step - loss: 0.2454 - accuracy: 0.9131
Epoch 78/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2448 - accuracy: 0.9194
Epoch 79/200
50/50 [==============================] - 5s 96ms/step - loss: 0.2381 - accuracy: 0.9244 0s - loss: 0.2484 - accura
Epoch 80/200
50/50 [==============================] - 0s 8ms/step - loss: 0.2218 - accuracy: 0.9262
Epoch 81/200
50/50 [==============================] - 5s 98ms/step - loss: 0.2312 - accuracy: 0.9119
Epoch 82/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2176 - accuracy: 0.9219
Epoch 83/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1949 - accuracy: 0.9262
Epoch 84/200
50/50 [==============================] - 5s 97ms/step - loss: 0.2363 - accuracy: 0.9137
Epoch 85/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2095 - accuracy: 0.9200
Epoch 86/200
50/50 [==============================] - 5s 96ms/step - loss: 0.2169 - accuracy: 0.9256
Epoch 87/200
50/50 [==============================] - 0s 8ms/step - loss: 0.2067 - accuracy: 0.9312
Epoch 88/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2130 - accuracy: 0.9256
Epoch 89/200
50/50 [==============================] - 5s 97ms/step - loss: 0.1866 - accuracy: 0.9356
Epoch 90/200
50/50 [==============================] - 0s 9ms/step - loss: 0.2050 - accuracy: 0.9281
Epoch 91/200
50/50 [==============================] - 5s 96ms/step - loss: 0.1927 - accuracy: 0.9287
Epoch 92/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1832 - accuracy: 0.9306
Epoch 93/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1776 - accuracy: 0.9406
Epoch 94/200
50/50 [==============================] - 5s 95ms/step - loss: 0.1982 - accuracy: 0.9300
Epoch 95/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1795 - accuracy: 0.9325
Epoch 96/200
50/50 [==============================] - 5s 96ms/step - loss: 0.1783 - accuracy: 0.9362
Epoch 97/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1618 - accuracy: 0.9431
Epoch 98/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1679 - accuracy: 0.9413
Epoch 99/200
50/50 [==============================] - 5s 8ms/step - loss: 0.1987 - accuracy: 0.9244
Epoch 100/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1867 - accuracy: 0.9337
Epoch 101/200
50/50 [==============================] - 5s 96ms/step - loss: 0.1730 - accuracy: 0.9425
Epoch 102/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1558 - accuracy: 0.9488
Epoch 103/200
50/50 [==============================] - 5s 97ms/step - loss: 0.1620 - accuracy: 0.9381
Epoch 104/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1655 - accuracy: 0.9406
Epoch 105/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1751 - accuracy: 0.9413
Epoch 106/200
50/50 [==============================] - 5s 96ms/step - loss: 0.1553 - accuracy: 0.9500
Epoch 107/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1640 - accuracy: 0.9438
Epoch 108/200
50/50 [==============================] - 5s 96ms/step - loss: 0.1529 - accuracy: 0.9444
Epoch 109/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1698 - accuracy: 0.9463
Epoch 110/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1774 - accuracy: 0.9356
Epoch 111/200
50/50 [==============================] - 5s 98ms/step - loss: 0.1694 - accuracy: 0.9444
Epoch 112/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1765 - accuracy: 0.9344
Epoch 113/200
50/50 [==============================] - 5s 99ms/step - loss: 0.1429 - accuracy: 0.9494
Epoch 114/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1574 - accuracy: 0.9444
Epoch 115/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1332 - accuracy: 0.9525
Epoch 116/200
50/50 [==============================] - 5s 97ms/step - loss: 0.1226 - accuracy: 0.9588
Epoch 117/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1522 - accuracy: 0.9394
Epoch 118/200
50/50 [==============================] - 5s 98ms/step - loss: 0.1682 - accuracy: 0.9400
Epoch 119/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1484 - accuracy: 0.9444
Epoch 120/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1609 - accuracy: 0.9362
Epoch 121/200
50/50 [==============================] - 5s 97ms/step - loss: 0.1427 - accuracy: 0.9481
Epoch 122/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1521 - accuracy: 0.9481
Epoch 123/200
50/50 [==============================] - 5s 96ms/step - loss: 0.1485 - accuracy: 0.9438
Epoch 124/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1547 - accuracy: 0.9500
Epoch 125/200
50/50 [==============================] - 5s 98ms/step - loss: 0.1459 - accuracy: 0.9563
Epoch 126/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1440 - accuracy: 0.9494
Epoch 127/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1409 - accuracy: 0.9506
Epoch 128/200
50/50 [==============================] - 5s 99ms/step - loss: 0.1162 - accuracy: 0.9588
Epoch 129/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1205 - accuracy: 0.9588
Epoch 130/200
50/50 [==============================] - 5s 98ms/step - loss: 0.1358 - accuracy: 0.9506
Epoch 131/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1384 - accuracy: 0.9500
Epoch 132/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1145 - accuracy: 0.9600
Epoch 133/200
50/50 [==============================] - 5s 9ms/step - loss: 0.1230 - accuracy: 0.9544
Epoch 134/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1235 - accuracy: 0.9606
Epoch 135/200
50/50 [==============================] - 5s 102ms/step - loss: 0.1211 - accuracy: 0.9594
Epoch 136/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1210 - accuracy: 0.9563
Epoch 137/200
50/50 [==============================] - 5s 96ms/step - loss: 0.1147 - accuracy: 0.9644
Epoch 138/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1097 - accuracy: 0.9619
Epoch 139/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1417 - accuracy: 0.9469
Epoch 140/200
50/50 [==============================] - 5s 97ms/step - loss: 0.1399 - accuracy: 0.9513
Epoch 141/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1171 - accuracy: 0.9613
Epoch 142/200
50/50 [==============================] - 5s 96ms/step - loss: 0.0962 - accuracy: 0.9712
Epoch 143/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1158 - accuracy: 0.9575
Epoch 144/200
50/50 [==============================] - 5s 100ms/step - loss: 0.1348 - accuracy: 0.9531
Epoch 145/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1110 - accuracy: 0.9606
Epoch 146/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1115 - accuracy: 0.9556
Epoch 147/200
50/50 [==============================] - 5s 97ms/step - loss: 0.1022 - accuracy: 0.9638
Epoch 148/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1045 - accuracy: 0.9600
Epoch 149/200
50/50 [==============================] - 5s 101ms/step - loss: 0.1186 - accuracy: 0.9650
Epoch 150/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1395 - accuracy: 0.9525
Epoch 151/200
50/50 [==============================] - 0s 9ms/step - loss: 0.0768 - accuracy: 0.9731
Epoch 152/200
50/50 [==============================] - 5s 97ms/step - loss: 0.1109 - accuracy: 0.9625
Epoch 153/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1211 - accuracy: 0.9556
Epoch 154/200
50/50 [==============================] - 5s 97ms/step - loss: 0.0914 - accuracy: 0.9700
Epoch 155/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1079 - accuracy: 0.9594
Epoch 156/200
50/50 [==============================] - 5s 96ms/step - loss: 0.1286 - accuracy: 0.9594
Epoch 157/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1304 - accuracy: 0.9531
Epoch 158/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1356 - accuracy: 0.9556
Epoch 159/200
50/50 [==============================] - 5s 95ms/step - loss: 0.1116 - accuracy: 0.9600
Epoch 160/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1229 - accuracy: 0.9606
Epoch 161/200
50/50 [==============================] - 5s 97ms/step - loss: 0.1145 - accuracy: 0.9581
Epoch 162/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1142 - accuracy: 0.9644
Epoch 163/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1221 - accuracy: 0.9581
Epoch 164/200
50/50 [==============================] - 5s 97ms/step - loss: 0.1193 - accuracy: 0.9625
Epoch 165/200
50/50 [==============================] - 0s 9ms/step - loss: 0.0960 - accuracy: 0.9700
Epoch 166/200
50/50 [==============================] - 5s 95ms/step - loss: 0.1053 - accuracy: 0.9650
Epoch 167/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1169 - accuracy: 0.9619
Epoch 168/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1312 - accuracy: 0.9494
Epoch 169/200
50/50 [==============================] - 5s 98ms/step - loss: 0.1128 - accuracy: 0.9625
Epoch 170/200
50/50 [==============================] - 0s 9ms/step - loss: 0.0919 - accuracy: 0.9700
Epoch 171/200
50/50 [==============================] - 5s 101ms/step - loss: 0.0987 - accuracy: 0.9669
Epoch 172/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1007 - accuracy: 0.9644
Epoch 173/200
50/50 [==============================] - 5s 95ms/step - loss: 0.1015 - accuracy: 0.9650
Epoch 174/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1003 - accuracy: 0.9700
Epoch 175/200
50/50 [==============================] - 0s 8ms/step - loss: 0.0957 - accuracy: 0.9675
Epoch 176/200
50/50 [==============================] - 5s 95ms/step - loss: 0.0974 - accuracy: 0.9656
Epoch 177/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1001 - accuracy: 0.9669
Epoch 178/200
50/50 [==============================] - 5s 97ms/step - loss: 0.0933 - accuracy: 0.9706
Epoch 179/200
50/50 [==============================] - 0s 9ms/step - loss: 0.0999 - accuracy: 0.9681
Epoch 180/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1100 - accuracy: 0.9631
Epoch 181/200
50/50 [==============================] - 5s 95ms/step - loss: 0.1084 - accuracy: 0.9606
Epoch 182/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1087 - accuracy: 0.9619
Epoch 183/200
50/50 [==============================] - 5s 97ms/step - loss: 0.1151 - accuracy: 0.9669
Epoch 184/200
50/50 [==============================] - 0s 8ms/step - loss: 0.1010 - accuracy: 0.9644
Epoch 185/200
50/50 [==============================] - 0s 9ms/step - loss: 0.0983 - accuracy: 0.9681
Epoch 186/200
50/50 [==============================] - 5s 8ms/step - loss: 0.0940 - accuracy: 0.9669
Epoch 187/200
50/50 [==============================] - 0s 9ms/step - loss: 0.0802 - accuracy: 0.9719
Epoch 188/200
50/50 [==============================] - 5s 98ms/step - loss: 0.1003 - accuracy: 0.9663
Epoch 189/200
50/50 [==============================] - 0s 9ms/step - loss: 0.0953 - accuracy: 0.9688
Epoch 190/200
50/50 [==============================] - 5s 101ms/step - loss: 0.1005 - accuracy: 0.9619
Epoch 191/200
50/50 [==============================] - 0s 8ms/step - loss: 0.0759 - accuracy: 0.9762
Epoch 192/200
50/50 [==============================] - 0s 9ms/step - loss: 0.0964 - accuracy: 0.9675
Epoch 193/200
50/50 [==============================] - 5s 95ms/step - loss: 0.0923 - accuracy: 0.9594
Epoch 194/200
50/50 [==============================] - 0s 9ms/step - loss: 0.1001 - accuracy: 0.9688
Epoch 195/200
50/50 [==============================] - 5s 95ms/step - loss: 0.0815 - accuracy: 0.9712
Epoch 196/200
50/50 [==============================] - 0s 8ms/step - loss: 0.0942 - accuracy: 0.9631
Epoch 197/200
50/50 [==============================] - 0s 9ms/step - loss: 0.0957 - accuracy: 0.9650
Epoch 198/200
50/50 [==============================] - 5s 96ms/step - loss: 0.0964 - accuracy: 0.9700
Epoch 199/200
50/50 [==============================] - 0s 9ms/step - loss: 0.0822 - accuracy: 0.9675
Epoch 200/200
50/50 [==============================] - 5s 97ms/step - loss: 0.0800 - accuracy: 0.9681
In [ ]:

In [77]:
#print History graph
historydf = pd.DataFrame(h.history, index=h.epoch)
historydf.plot(ylim=(0,1))
Out[77]:
<AxesSubplot:>

Validation

In [68]:
X_val.shape
Out[68]:
(320, 128, 128, 1)
In [69]:
#model = load_model(model_name)
In [ ]:
score = 0
for i, d in enumerate(X_val):
    val_pred = model.predict(np.array([d]))
    #print(val_pred[0], Y_val[i])
    if np.argmax(val_pred) == np.argmax(Y_val[i]):
        score += 1
print(score, "/", len(Y_val))

Generating the predictions

In [ ]:
# Generating the predictions
pred = model.predict(images_test)
predictions = np.argmax(pred, axis=1)

predictions.shape
In [ ]:
submission = pd.DataFrame({"label":predictions})
submission.head(1000)
In [ ]:
# Saving the predictions
!rm -rf assets
!mkdir assets
submission.to_csv(os.path.join("assets", "submission.csv"))
In [132]:
model.save(model_name)

LeaderBoard Result : TotErrors = 14.000 MaxError = 6.000

Thank you !


Comments

You must login before you can post a comment.

Execute