Import Library and Dataset¶
In [77]:
!pip install wget
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import seaborn as sns
import cv2
from sklearn.model_selection import StratifiedShuffleSplit,train_test_split
import glob
import tensorflow as tf
from sklearn import metrics
from sklearn import preprocessing
from tensorflow import keras
sns.set_style('darkgrid')
!wget -N 'https://cainvas-static.s3.amazonaws.com/media/user_data/DevanshChowd/signature.zip'
!unzip signature.zip
Each Image in Folder with suffix _forg are forged
Each Image in Folders without this suffix are Authentic or signed by the same person .
In [11]:
display(sorted(glob.glob('train_data/*'))[:4])
print('Each Folder has' ,len(glob.glob('train_data/001/*')),'Authentic Images and Folders with suffix forf have',len(glob.glob('train_data/001_forg/*')),'Images.')
print('Training Size is ',len(glob.glob('train_data/*/*')),'Validation Size is ',len(glob.glob('validation_data/*/*')),'Test Size is ',len(glob.glob('test_data/*/*')))
In [10]:
plt.imshow(plt.imread('train_data/001/001_01.PNG'))
Out[10]:
In [22]:
def get_data(dir, labels):
features = []
targets = []
for name in tqdm.tqdm(sorted(os.listdir(dir))):
for image_name in sorted(os.listdir(os.path.join(dir,name))):
img = cv2.imread(os.path.join(dir,name,image_name), cv2.IMREAD_GRAYSCALE)
features.append(img)
if labels:
if 'forg' in name.lower():
targets.append(1)
else:
targets.append(0)
if labels:
return np.array(features),np.array(targets)
else:
return np.array(features)
train_features, train_labels = get_data('train_data', True)
validation_features, validation_labels = get_data('validation_data', True)
test_features, test_labels = get_data('test_data', True)
print(train_features.shape,validation_features.shape,test_features.shape)
train_features = train_features.reshape((826, 268, 650, 1))
train_features = train_features.astype('float32') / 255
validation_features = validation_features.reshape((413, 268, 650, 1))
validation_features = validation_features.astype('float32') / 255
test_features = test_features.reshape((410, 268, 650, 1))
test_features = test_features.astype('float32') / 255
In [32]:
def get_model():
keras.backend.clear_session()
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(268, 650, 1),padding='same'))
model.add(keras.layers.MaxPooling2D((2, 2),padding='same'))
model.add(keras.layers.Conv2D(32, (3, 3), activation='relu',padding='same'))
model.add(keras.layers.MaxPooling2D((2, 2),padding='same'))
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu',padding='same'))
model.add(keras.layers.MaxPooling2D((2, 2),padding='same'))
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu',padding='same'))
model.add(keras.layers.MaxPooling2D((2, 2),padding='same'))
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu',padding='same'))
model.add(keras.layers.MaxPooling2D((2, 2),padding='same'))
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu',padding='same'))
model.add(keras.layers.MaxPooling2D((2, 2),padding='same'))
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu',padding='same'))
model.add(keras.layers.MaxPooling2D((2, 2),padding='same'))
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu',padding='same'))
model.add(keras.layers.MaxPooling2D((2, 2),padding='same'))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(32, activation='relu'))
model.add(keras.layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])
model.summary()
return model
model = get_model()
In [34]:
model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])
early_stop = keras.callbacks.EarlyStopping(monitor= 'val_accuracy',patience=10,restore_best_weights=True)
red_lr = keras.callbacks.ReduceLROnPlateau(patience=3,monitor= 'val_accuracy',factor=0.75)
model_check = keras.callbacks.ModelCheckpoint(f"signature.h5",save_best_only=True)
history = model.fit(train_features, train_labels ,validation_data=(validation_features, validation_labels),epochs=30,callbacks = [early_stop,model_check,red_lr])
In [82]:
fig = plt.figure()
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
fig.savefig('Accuracy vs Epoch.png')
In [83]:
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
fig.savefig('Loss vs Epoch.png')
In [37]:
model.evaluate(train_features, train_labels)
model.evaluate(validation_features, validation_labels)
model.evaluate(test_features, test_labels)
Out[37]:
In [70]:
indices = np.random.randint(len(test_features),size=15)
demo,demo_pred = test_features[indices],test_labels[indices]
predictions = (model.predict(demo)>.5).astype('int32')
In [80]:
fig,ax = plt.subplots(3,5,figsize=(15,7))
plt.tick_params(left = False, bottom = False)
for i in range(3):
for j in range(5):
ax[i][j].imshow(demo[i*5+j])
ax[i][j].tick_params(left = False, bottom = False)
ax[i][j].set_xticks([])
ax[i][j].set_yticks([])
ax[i][j].set_title("Truth Label "+str(demo_pred[i*5+j])+" \n PREDICTED "+str(predictions[i*5+j][0]))
fig.savefig('Predict.png')
In [81]:
model.save('signature.h5')