├── README.md └── fingerprintRecog.py /README.md: -------------------------------------------------------------------------------- 1 | # Recreating-Fingerprints-using-Convolutional-Autoencoders 2 | 3 | Build a Neural network that is capable of recreating or reconstructing fingerprint images using Convolutional Autoencoders. 4 | 5 | The dataset that I’m using is the FVC2002 fingerprint dataset. It consists of 4 different sensor fingerprints namely Low-cost Optical Sensor, Low-cost Capacitive Sensor, Optical Sensor and Synthetic Generator, each sensor having varying image sizes. The dataset has 320 images, 80 images per sensor. 6 | 7 | Download dataset: http://bias.csr.unibo.it/fvc2002/databases.asp 8 | 9 | Follow [this article](https://www.theaidream.com/post/recreating-fingerprints-using-convolutional-autoencoders 10 | ) to understand the process of reconstructing fingerprint images step by step. 11 | -------------------------------------------------------------------------------- /fingerprintRecog.py: -------------------------------------------------------------------------------- 1 | #pip install scipy==1.1.0 2 | import os 3 | os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" 4 | os.environ["CUDA_VISIBLE_DEVICES"]="1" #model will be trained on GPU 1 5 | import cv2 6 | import matplotlib.pyplot as plt 7 | %matplotlib inline 8 | from skimage.filters import threshold_otsu 9 | import numpy as np 10 | from glob import glob 11 | import scipy.misc 12 | from matplotlib.patches import Circle,Ellipse 13 | from matplotlib.patches import Rectangle 14 | import os 15 | from PIL import Image 16 | 17 | import keras 18 | from matplotlib import pyplot as plt 19 | import numpy as np 20 | import gzip 21 | %matplotlib inline 22 | from keras.layers import Input,Conv2D,MaxPooling2D,UpSampling2D 23 | from keras.models import Model 24 | from keras.optimizers import RMSprop 25 | from keras.layers.normalization import BatchNormalization 26 | 27 | 28 | data = glob('./drive/My Drive/fingerprint/DB*/*') 29 | len(data) 30 | 31 | images = [] 32 | def read_images(data): 33 | for i in range(len(data)): 34 | img = scipy.misc.imread(data[i]) 35 | img = scipy.misc.imresize(img,(224,224)) 36 | images.append(img) 37 | return images 38 | 39 | images = read_images(data) 40 | 41 | images_arr = np.asarray(images) 42 | images_arr = images_arr.astype('float32') 43 | images_arr.shape 44 | #(320, 224, 224) 45 | #Data Exploration 46 | print("Dataset (images) shape: {shape}".format(shape=images_arr.shape)) 47 | 48 | # Display the first image in training data 49 | for i in range(5): 50 | plt.figure(figsize=[5, 5]) 51 | curr_img = np.reshape(images_arr[i], (224,224)) 52 | plt.imshow(curr_img, cmap='gray') 53 | plt.show() 54 | 55 | 56 | #You'll first convert each 224 x 224 image of the dataset into a matrix of size 224 x 224 x 1, which you can then feed into the network: 57 | 58 | images_arr = images_arr.reshape(-1, 224,224, 1) 59 | images_arr.shape 60 | 61 | images_arr.dtype 62 | 63 | 64 | #rescale the data with the maximum pixel value of the images in the data: 65 | np.max(images_arr) 66 | images_arr = images_arr / np.max(images_arr) 67 | 68 | np.max(images_arr), np.min(images_arr) 69 | 70 | from sklearn.model_selection import train_test_split 71 | train_X,valid_X,train_ground,valid_ground = train_test_split(images_arr,images_arr,test_size=0.2,random_state=13) 72 | 73 | 74 | #The Convolutional Autoencoder 75 | """ 76 | The images are of size 224 x 224 x 1 or a 50,176-dimensional vector. You convert the image matrix to an array, rescale it between 0 and 1, reshape it so that it's of size 224 x 224 x 1, and feed this as an input to the network. 77 | 78 | Also, you will use a batch size of 128 using a higher batch size of 256 or 512 is also preferable it all depends on the system you train your model. It contributes heavily in determining the learning parameters and affects the prediction accuracy. You will train your network for 50 epochs. 79 | """ 80 | batch_size = 256 81 | epochs = 300 82 | inChannel = 1 83 | x, y = 224, 224 84 | input_img = Input(shape = (x, y, inChannel)) 85 | 86 | def autoencoder(input_img): 87 | #encoder 88 | #input = 28 x 28 x 1 (wide and thin) 89 | conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img) #28 x 28 x 32 90 | pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) #14 x 14 x 32 91 | conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1) #14 x 14 x 64 92 | pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) #7 x 7 x 64 93 | conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2) #7 x 7 x 128 (small and thick) 94 | 95 | #decoder 96 | conv4 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3) #7 x 7 x 128 97 | up1 = UpSampling2D((2,2))(conv4) # 14 x 14 x 128 98 | conv5 = Conv2D(64, (3, 3), activation='relu', padding='same')(up1) # 14 x 14 x 64 99 | up2 = UpSampling2D((2,2))(conv5) # 28 x 28 x 64 100 | decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(up2) # 28 x 28 x 1 101 | return decoded 102 | 103 | autoencoder = Model(input_img, autoencoder(input_img)) 104 | 105 | autoencoder.compile(loss='mean_squared_error', optimizer = RMSprop()) 106 | 107 | autoencoder_train = autoencoder.fit(train_X, train_ground, batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(valid_X, valid_ground)) 108 | 109 | #Validation and training loss. 110 | loss = autoencoder_train.history['loss'] 111 | val_loss = autoencoder_train.history['val_loss'] 112 | epochs = range(300) 113 | plt.figure() 114 | plt.plot(epochs, loss, 'bo', label='Training loss') 115 | plt.plot(epochs, val_loss, 'b', label='Validation loss') 116 | plt.title('Training and validation loss') 117 | plt.legend() 118 | plt.show() 119 | 120 | 121 | #Prediction 122 | pred = autoencoder.predict(valid_X) 123 | 124 | 125 | #Reconstruction of Test Images 126 | plt.figure(figsize=(20, 4)) 127 | print("Test Images") 128 | for i in range(5): 129 | plt.subplot(1, 5, i+1) 130 | plt.imshow(valid_ground[i, ..., 0], cmap='gray') 131 | plt.show() 132 | plt.figure(figsize=(20, 4)) 133 | print("Reconstruction of Test Images") 134 | for i in range(5): 135 | plt.subplot(1, 5, i+1) 136 | plt.imshow(pred[i, ..., 0], cmap='gray') 137 | plt.show() 138 | 139 | 140 | score = model.evaluate([X_test], [y_test], verbose=0) 141 | print("Score: ",score[1]*100) 142 | 143 | 144 | """ 145 | From the above figures, you can observe that your model did a fantastic job of reconstructing the test images that you predicted using the model. 146 | At least visually, the test and the reconstructed images look almost exactly similar. 147 | """ 148 | 149 | --------------------------------------------------------------------------------