-
Notifications
You must be signed in to change notification settings - Fork 0
/
untitled5.py
153 lines (119 loc) · 4.15 KB
/
untitled5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 8 19:44:05 2017
@author: Bappi Parvez
"""
from __future__ import division, print_function, absolute_import
# Import tflearn and some helpers
import tflearn
from tflearn.data_utils import shuffle
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
import os,cv2
import numpy as np
import matplotlib.pyplot as plt
import scipy
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
from keras import backend as K
K.set_image_dim_ordering('tf')
from keras.utils import np_utils
import sys
from skimage import io
from PIL import Image
import glob
PATH = os.getcwd()
# Define data path
data_path = PATH + '/modified-training-dataset'
data_dir_list = os.listdir(data_path)
img_rows=32
img_cols=32
num_channel=3
# Define the number of classes
num_classes = 2
img_data_list=[]
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()
for dataset in data_dir_list:
img_list=os.listdir(data_path+'/'+ dataset)
print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
for img in img_list:
input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
input_img_resize=cv2.resize(input_img,(32,32))
img_data_list.append(input_img_resize)
img_data = np.array(img_data_list)
img_data = img_data.astype('float32')
print (img_data.shape)
num_classes = 2
num_of_samples = img_data.shape[0]
labels = np.ones((num_of_samples,),dtype='int64')
labels[0:22200]=0
labels[22200:]=1
names = ['human','non-human']
Y = np_utils.to_categorical(labels, num_classes)
x,y = shuffle(img_data,Y, random_state=2)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)
convnet = input_data(shape=[None, 32, 32, 3],data_preprocessing=img_prep,name = 'input_data')
convnet = conv_2d(convnet, 32, 3, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 3, activation='relu')
convnet = conv_2d(convnet, 64, 3, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = fully_connected(convnet, 512, activation='relu')
convnet = dropout(convnet, 0.5)
convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam',
loss='categorical_crossentropy',
learning_rate=0.001)
model = tflearn.DNN(convnet, checkpoint_path='faceDetect.tfl.ckpt')
"""
print("start training")
hist = model.fit(X_train, y_train, n_epoch=50, shuffle=True, validation_set=(X_test, y_test),
show_metric=True, batch_size=96,
snapshot_epoch=True,
run_id='faceDetect')
model.save("faceDetect.model")
"""
model.load("faceDetect.model")
print("model loaded")
TP=0
TN=0
FP=0
FN=0
filenames = [img for img in glob.glob("single Check/*.jpg")]
filenames.sort()
images = []
count = 0
total_images = 0
for image in filenames:
img = scipy.ndimage.imread(image)
img = scipy.misc.imresize(img, (32, 32), interp="bicubic").astype(np.float32, casting='unsafe')
test_img = np.array(img)
#test_img = np.expand_dims(test_img, axis=4)
outcome = image[11]
prediction = model.predict([test_img])
#print(prediction)
#print(np.array(prediction))
#is_human = np.argmax(prediction[0]) == 1
#print(outcome)
if (prediction[0][0]) > (prediction[0][1]):
if outcome=='n':
TP += 1
else:
FP +=1
print("Face Found")
count +=1
total_images += 1
else:
if outcome=='n':
FN += 1
else:
TN +=1
print("face not found")
total_images += 1
print("false Positive "+str(FP)+" True Positive "+str(TP)+" True Nagetive "+str(TN)+" False Negative "+str(FN))
print("Total Face Found "+str(count)+" in "+str(total_images)+" images")