├── umd1.jpg
├── umd2.jpg
├── folder.jpg
├── images.zip
├── dataset.zip
├── fine_search.jpg
├── babysitting1.jpg
├── babysitting2.jpg
├── babysitting3.jpg
├── coarse_search.jpg
├── keras-tensorflow-logo.jpg
├── 1_reg_10minus6_lr_10minus4.png
├── 2_reg_10minus6_lr_10power4.png
├── README.txt
├── README.md
├── cnn_code.py
└── UMD.py
/umd1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/umd1.jpg
--------------------------------------------------------------------------------
/umd2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/umd2.jpg
--------------------------------------------------------------------------------
/folder.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/folder.jpg
--------------------------------------------------------------------------------
/images.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/images.zip
--------------------------------------------------------------------------------
/dataset.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/dataset.zip
--------------------------------------------------------------------------------
/fine_search.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/fine_search.jpg
--------------------------------------------------------------------------------
/babysitting1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/babysitting1.jpg
--------------------------------------------------------------------------------
/babysitting2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/babysitting2.jpg
--------------------------------------------------------------------------------
/babysitting3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/babysitting3.jpg
--------------------------------------------------------------------------------
/coarse_search.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/coarse_search.jpg
--------------------------------------------------------------------------------
/keras-tensorflow-logo.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/keras-tensorflow-logo.jpg
--------------------------------------------------------------------------------
/1_reg_10minus6_lr_10minus4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/1_reg_10minus6_lr_10minus4.png
--------------------------------------------------------------------------------
/2_reg_10minus6_lr_10power4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kalyanghosh/Face-Detection-using-CNN/HEAD/2_reg_10minus6_lr_10power4.png
--------------------------------------------------------------------------------
/README.txt:
--------------------------------------------------------------------------------
1 | ###########################################################################################################
2 | DATA PREPROCESSING IO CODE (UMD.py)
3 | ###########################################################################################################
4 | #Instructions to run the code UMD.py inside the dataset subfolder folder inside Project2 folder
5 |
6 | #1. Download the data(Batch 3, 12GB version) from http://www.umdfaces.io/
7 | #2. Create a folder named data_UMD inside the dataset folder.Inside the data_UMD folder,
8 | # create two dirs named Annotation and original_pics
9 | #3. Extract the dataset inside the original_pics folder and copy the umdfaces_batch3_ultraface.csv annotation
10 | # file to the Annotation folder.
11 | #4. Run the code UMD.py inside the dataset folder by specifying the following parameters.
12 |
13 | # INPUT PARAMETERS
14 | # image_dir = .\data_UMD\original_pics
15 | # anno_dir = .\data_UMD\Annotation
16 | # save_dir = .\data_UMD\cache
17 | # use_Color = True for color image , False for gray
18 | # patch_size = 60, you are free to give your own size
19 | # train_size = 10000, number of training images
20 | # test_size = 1000, number of testing images
21 |
22 | # OUTPUT
23 | # After running the code UMD.py, the folder structure that will be generatd is as follows:
24 | # 1. Inside the data_UMD folder, a folder 'cache' will be created
25 | # 2. Inside the data_UMD folder, a folder 'color' (if use_Color=True) else 'gray' will be created
26 | # 3. Inside the 'color'/'gray' folder, two folders namely 'train' and 'test' will be created
27 | # 4. Inside each of the 'train' and 'test' folders,
28 | # 'neg' folder for negative images and 'pos' foder for positive images will be created
29 | ###########################################################################################################
30 |
31 | NEURAL NETWORK CODE (cnn_code.py)
32 | ###########################################################################################################
33 |
34 | #Instructions to run the code cnn_code.py inside the code folder
35 |
36 | #1. Navigate to the code folder inside the Project_2 folder
37 | #2. Give the path of train_dir = '/home/kghosh/dataset/data_UMD/cache1/color/train/'
38 | # Give the path to test_dir = '/home/kghosh/dataset/data_UMD/cache1/color/test/'
39 | #3. Run the python file cnn_code.py in the terminal using the below command:
40 | # ($python cnn_code.py)
41 | #4. After the code is run and all the epochs complete,the model will be saved in
42 | # face_vs_nonface.h5
43 |
44 |
45 |
46 | #############################################################################################################
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # PROJECT TITLE: FACE DETECTION USING CNN aka ConvNets
4 |
5 | ## DESCRIPTION:
6 |
This repository gives budding Deep Learning enthusiats a gentle introduction to the topic of Deep Learning.
7 |
In this repository , I will list down all the steps needed to get started with your first project in Deep Learning.
8 |
So go ahead, Fork this repo and get started with Deep Learning.
9 |
10 | ## CONTENTS:
11 | 1. Dataset download
12 | 2. Tools & Libraries
13 | 3. Code
14 | 4. Instructions to run the code
15 | 5. Future improvements
16 |
17 | ## EXPLANATIONS & STEPS:
18 | 1. Dataset download:
19 |
For this project we will be using the UMD face dataset which can be downloaded from: [UMD Dataset](http://www.umdfaces.io/)
20 |

21 |
Go ahead and download the Batch 3 dataset, which will have faces of personalities and and .csv file that contains the annotations
22 | to crop out the face from each of the images:
23 | 
24 |
25 | 1.1. Data Preprocessing Input/Output Code-UMD.py:
26 |
In the next step, we need to write a code that would read the annotations from the .csv file , crop out the faces to create the
27 | positive dataset and crop out the background to create the negative dataset.The code should be self sufficient and modular so that when parameters like "dataset path", "# of train images", "# of test images", "color/gray", the code should perform all the tasks and divide the dataset into a folder structure as below:
28 |

29 |
30 | 2. Tools & Libraries:
31 |
2.1 In this project we will be using the Keras Deep Learning Library and we will be running it on top of the Tensorflow backend.
32 |

33 |
Instructions on how to install Keras & Tensorflow on an Ubuntu machine are available online.
34 | 2.2 The code editors we will be using are Anaconda with OpenCV,Numpy,Scipy support.
35 |
36 | 3. Code-CNN_code.py:
37 |
The python file CNN_code is the main code which encapsulates the CNN architecture that was used for this project.
38 |
The steps performed by the code are as follows:
39 |
3.1: In this code, I have used a simplistic CNN architecture as below:
40 |
2D CONVOLUTION LAYER->2D MAX POOLING->2D CONVOLUTION LAYER->2D MAX POOLING->2D CONVOLUTION LAYER->2D MAX POOLING->2D CONVOLUTION LAYER->2D MAX POOLING->FULLY CONNECTED->FULLY CONNECTED
41 |
3.2: In this code, I have used Binary CrossEntropy as the Loss Function , the RMSProp as the Gradient Descent algorithm:
42 |
3.3 The hyperparameters used are as follows:
43 |
LEARNING RATE =1e-4
44 |
STEPS PER EPOCH =100
45 |
EPOCHS =30
46 |
VALIDATION STEPS =50
47 |
48 | 4. HYPER PARAMETER OPTIMIZATION:
49 |
The tune the hyperparameters, we run a random search over the hyperparameter space, by sampling the Learning Rate & Regularization from a uniform distribution.
50 |
The optimal set of hyperparameters after running a coarse search and fine search are as follows:
51 |
COARSE SEARCH:
52 |
Maximum Testing Accuracy=96.89%
53 |
Minimum Testing Loss=0.094
54 |
Value of optimum Learning Rate=4.48e-4
55 |
Value of optimum Regularization=2.27e-5
56 |
57 |
58 |
FINE SEARCH:
59 |
Maximum Testing Accuracy=97.79%
60 |
Minimum Testing Loss=0.1034
61 |
Value of optimum Learning Rate=8.29e-4
62 |
Value of optimum Regularization=3.91e-3
63 |
64 |
The plots of TRAINING VS TESTING ACCURACY and TRAINING VS TESTING LOSS:
65 |

66 |

67 | 5. SETUP INSTRUCTIONS:
68 |
The entire setup instructions to run the code can be found in README.txt
69 |
--------------------------------------------------------------------------------
/cnn_code.py:
--------------------------------------------------------------------------------
1 |
2 | # coding: utf-8
3 |
4 | # In[7]:
5 |
6 | #Instructions to run the code cnn_code.py inside the code folder
7 |
8 | #1. Navigate to the code folder inside the Project_2 folder
9 | #2. Give the path of train_dir = '/home/kghosh/dataset/data_UMD/cache1/color/train/'
10 | # Give the path to test_dir = '/home/kghosh/dataset/data_UMD/cache1/color/test/'
11 | #3. Run the python file cnn_code.py in the terminal using the below command:
12 | # ($python cnn_code.py)
13 | #4. After the code is run and all the epochs complete,the model will be saved in
14 | # face_vs_nonface.h5
15 |
16 |
17 |
18 |
19 |
20 |
21 | #*******************************************#
22 | #Importing the libraries
23 | import numpy as np
24 | from keras import layers
25 | from keras import models
26 | from keras import regularizers
27 | from keras import optimizers
28 | from keras.preprocessing.image import ImageDataGenerator
29 | import matplotlib.pyplot as plt
30 |
31 | train_dir='/home/kghosh/dataset/data_UMD/cache1/color/train/'
32 | test_dir='/home/kghosh/dataset/data_UMD/cache1/color/test/'
33 | #*******************************************#
34 |
35 |
36 | max_count=100
37 | reg_val=[]
38 | lr_val=[]
39 | test_loss=[]
40 | test_acc=[]
41 |
42 | for i in range(max_count):
43 |
44 | print ("*"*30)
45 | print (str(i+1)+"/"+str(max_count))
46 | print ("*"*30)
47 | # Sampling learning rate and regularization from a uniform distribution
48 |
49 | reg=10**(np.random.uniform(-4,0))
50 | lr=10**(np.random.uniform(-3,-4))
51 |
52 |
53 |
54 | #*******************************************#
55 | #Defining the architechture
56 |
57 | model=models.Sequential()
58 |
59 | model.add(layers.Conv2D(32,(3,3),activation='relu',input_shape=(60,60,3)))
60 |
61 | model.add(layers.MaxPooling2D((2,2)))
62 |
63 | model.add(layers.Conv2D(64,(3,3),activation='relu'))
64 |
65 | model.add(layers.MaxPooling2D(2,2))
66 |
67 | model.add(layers.Conv2D(128,(3,3),activation='relu'))
68 |
69 | model.add(layers.MaxPooling2D((2,2)))
70 |
71 | model.add(layers.Conv2D(128,(3,3),activation='relu'))
72 |
73 | model.add(layers.MaxPooling2D((2,2)))
74 |
75 | model.add(layers.Flatten())
76 |
77 | model.add(layers.Dense(512,activation='relu',kernel_regularizer=regularizers.l2(reg)))
78 |
79 | model.add(layers.Dense(1,activation='sigmoid',kernel_regularizer=regularizers.l2(reg)))
80 |
81 | #**********************************************#
82 | # Summazing the model
83 |
84 | #model.summary()
85 |
86 | #**********************************************#
87 | # Configuring the model for training
88 |
89 |
90 |
91 | model.compile(loss='binary_crossentropy',
92 | optimizer=optimizers.RMSprop(lr=lr),
93 | metrics=['acc'])
94 |
95 | #***********************************************#
96 | # Using the ImageDataGenerator class to read the..
97 | # images from the directories
98 |
99 |
100 |
101 | #Rescale all the images by 1/255
102 | train_datagen=ImageDataGenerator(rescale=1./255)
103 | test_datagen=ImageDataGenerator(rescale=1./255)
104 |
105 | train_generator=train_datagen.flow_from_directory(
106 | train_dir,
107 | target_size=(60,60),
108 | batch_size=20,
109 | class_mode='binary')
110 | test_generator=test_datagen.flow_from_directory(
111 | test_dir,
112 | target_size=(60,60),
113 | batch_size=20,
114 | class_mode='binary'
115 | )
116 | #Fit the model using batch generator
117 | history=model.fit_generator(
118 | train_generator,
119 | steps_per_epoch=100,
120 | epochs=5,
121 | validation_data=test_generator,
122 | validation_steps=50)
123 |
124 | reg_val.append(reg)
125 | lr_val.append(lr)
126 | test_loss.append(history.history['val_loss'])
127 | test_acc.append(history.history['val_acc'])
128 |
129 | #Save the model
130 | #model.save('face_vs_nonface.h5')
131 |
132 | #Plotting accuracy and loss
133 | '''
134 | acc=history.history['acc']
135 | test_acc=history.history['val_acc']
136 | loss=history.history['loss']
137 | test_loss=history.history['val_loss']
138 | epochs=range(1,len(acc)+1)
139 |
140 | plt.plot(epochs,acc,'bo',label='TRAINING ACCURACY')
141 | plt.plot(epochs,test_acc,'b',label='TEST ACCURACY')
142 | plt.title('TRAINING AND TEST ACCURACY')
143 | plt.xlabel('Epochs')
144 | plt.ylabel('Accuracy')
145 | plt.legend()
146 |
147 | plt.figure()
148 |
149 | plt.plot(epochs,loss,'bo',label='TRAINING LOSS')
150 | plt.plot(epochs,test_loss,'b',label='TEST LOSS')
151 | plt.title('TRAINING AND TESTING LOSS')
152 | plt.xlabel('Epochs')
153 | plt.ylabel('Loss')
154 | plt.legend()
155 |
156 | plt.show()
157 | '''
158 |
159 | print ("*"*30)
160 | print ("Finding the highest Test Accuracy and lowest Test Loss...")
161 |
162 | index1=0
163 | index2=0
164 | max_test_acc=max(test_acc[0])
165 | min_test_loss=min(test_loss[0])
166 | for i in range(max_count):
167 | temp1=max(test_acc[i])
168 | if(temp1>=max_test_acc):
169 | max_test_acc=temp1
170 | index1=i
171 | temp2=min(test_loss[i])
172 | if(temp2