Skip to content

Commit

Permalink
add the predict threshold, change the directory architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyiqiao committed Mar 30, 2018
1 parent fdaee7d commit fe9027a
Show file tree
Hide file tree
Showing 33 changed files with 92 additions and 42 deletions.
66 changes: 45 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Politicians_FaceRecongnise
# Politicians_FaceRecognise

## Introduction
This is a project to realise 55 Chinese politicians face recongnise. refernce the repository ([facenet](https://github.com/davidsandberg/facenet.git)).
This is a project to realise **55 Chinese politicians** face recognise. refernce the repository **([facenet](https://github.com/davidsandberg/facenet.git))**.
## Pre-trained models
| Model name | LFW accuracy | Training dataset | Architecture |
|-----------------|--------------|------------------|-------------|
Expand All @@ -13,41 +13,65 @@ This is a project to realise 55 Chinese politicians face recongnise. refernce th
The code is tested using Tensorflow 0.12 under Ubuntu 16.04 with Python3.6 and Python3.5
* tensorflow>0.12
* sklearn
* matpl
* numpy
* scipy
* pickle
* cv2
* matplotlib

I set the code:

CLASS_PROBABILITY_THRESHOLD=0.2 #the probability of the predict threshold,if less than the threshold ,set the prediction="unknown"

## Dataset
1. for train set:

images/train/aligned_policy/ #person num: 55 + others, pictures num: 2409

2. for test dataset:

images/test/others or images/test/policy

3. for train the model:

python train_model.py

4. for test the model:

python test_model # I didn't set the predict threshod,the result will output the max probobility of classname.


if you want to change the pictures ,in order to use your own data,also if someone is interested to use my dataset about 55 Chinese politicians,you can email to me ,I am glad to share you my dataset.
1. put your images that haven't be aligned into the directory align/images/,like:
```

align/images/policy/
people1/
1.jpg
2.jpg
people2/
1.jpg
2.jpg
```


2. you can change the input or output directory
```

parser.add_argument('--input_dir', type=str, help='Directory with unaligned images.',default='images/policy/')
parser.add_argument('--output_dir', type=str, help='Directory with aligned face thumbnails.',default='images/aligned_policy/')
```


then run the code

```

python align_dataset_mtcnn.py
```


after run this code ,you will get the anigned_pictures,you can change the parameters to choose if you want to detect_multiple_faces,set:

after run this code ,you will get the anigned_pictures,you can change the parameters to choose if you want to detect_multiple_faces,the result like:
parser.add_argument('--detect_multiple_faces', type=bool, help='Detect and align multiple faces per image.', default=True)

```
align/images/aligned_policy/
the result like:

align/images/train/aligned_policy/
people1/
1.jpg
2.jpg
Expand All @@ -56,37 +80,37 @@ after run this code ,you will get the anigned_pictures,you can change the parame
1.jpg
1_1.jpg
2.jpg
```


3. copy the files align/images/aligned_policy into images/
3. copy the files align/images/aligned_policy into images/train/

if you want to use my model directly,and run my project and see the result, you can

1. show the politician pictures and see the prediction or show the other people which is not the politicican one by one:
```

python calacc_plt.py
python calerror_plt.py
```


2. I alse provide the multi thread python code to calculate the accuracy.
```

python multiThread_process.py
```


# Results
1. can recongnise profile
1. can recognise profile

![Figure_1](/result/Figure_1.png)

2. alse has some error

![Figure_1-1](/result/Figure_1-1.png)

3. can recongise sepcial part face
3. can recogise sepcial part face

![Figure_1-2](/result/Figure_1-2.png)

4. can recongise sepcial part face
4. can recogise sepcial part face

![Figure_1-3](/result/Figure_1-3.png)

Expand Down
Binary file added __pycache__/facenet.cpython-35.pyc
Binary file not shown.
Binary file modified align/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file modified align/__pycache__/detect_face.cpython-35.pyc
Binary file not shown.
22 changes: 15 additions & 7 deletions calacc_plt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from sklearn.svm import SVC

count_rightpic=0
CLASS_PROBABILITY_THRESHOLD=0.2 #设置阈值准确度,否则识别为unknown
def main(args):
dataset = facenet.get_dataset(args.input_dir)

Expand Down Expand Up @@ -56,14 +57,21 @@ def main(args):
feed_dict = {images_placeholder: [np.array(image)], phase_train_placeholder: False}
emb_datas = sess.run(embeddings, feed_dict=feed_dict)

print("emb_datas")
#print(emb_datas)
print('Testing classifier')
prediction = model.predict(emb_datas)
print(prediction)
predictions.append(class_names[prediction[0]])

#每张图所有预测人脸结果
predict_proba=model.predict_proba(emb_datas)
best_class_indice = np.argmax(predict_proba, axis=1)
best_class_probability = predict_proba[np.arange(len(best_class_indice)), best_class_indice]
for i in range(len(best_class_indice)):
class_name = class_names[best_class_indice[i]]
class_probability = best_class_probability[i]
print("class_probability:",class_probability)
if class_probability < CLASS_PROBABILITY_THRESHOLD:
prediction = "unknown"
else:
prediction=class_name
break
predictions.append(prediction)

for prediction in predictions:
print("prediction:%s"%prediction)

Expand Down
21 changes: 15 additions & 6 deletions calerror_plt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'jiangzeming','lipeng','maozedong','wenjiabao','zhaoziyang','zhurongji','boxilai','guoboxiong','linjihua',
'xucaihou','zhouyongkang']

CLASS_PROBABILITY_THRESHOLD=0.2 #设置阈值准确度,否则识别为unknown

def main(args):
dataset = facenet.get_dataset(args.input_dir)
Expand Down Expand Up @@ -63,13 +64,21 @@ def main(args):
for image in images:
feed_dict = {images_placeholder: [np.array(image)], phase_train_placeholder: False}
emb_datas = sess.run(embeddings, feed_dict=feed_dict)

print("emb_datas")
#print(emb_datas)

print('Testing classifier')
prediction = model.predict(emb_datas)
print(prediction)
predictions.append(class_names[prediction[0]])
predict_proba=model.predict_proba(emb_datas)
best_class_indice = np.argmax(predict_proba, axis=1)
best_class_probability = predict_proba[np.arange(len(best_class_indice)), best_class_indice]
for i in range(len(best_class_indice)):
class_name = class_names[best_class_indice[i]]
class_probability = best_class_probability[i]
print("class_probability:",class_probability)
if class_probability < CLASS_PROBABILITY_THRESHOLD:
prediction = "unknown"
else:
prediction=class_name
break
predictions.append(prediction)

#每张图所有预测人脸结果
for prediction in predictions:
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
19 changes: 14 additions & 5 deletions multiThread_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
batch_size=90
facenet_image_size=160

CLASS_PROBABILITY_THRESHOLD=0.2 #设置阈值准确度,否则识别为unknown

def parsePicture(picture_paths,model,class_names):
num=0
Expand Down Expand Up @@ -78,12 +79,20 @@ def embeding(image_path,model,class_names):
for image in images:
feed_dict = {images_placeholder: [np.array(image)], phase_train_placeholder: False}
emb_datas = sess.run(embeddings, feed_dict=feed_dict)
print("emb_datas")

print('Testing classifier')
prediction = model.predict(emb_datas)
print(prediction)
predictions.append(class_names[prediction[0]])
predict_proba=model.predict_proba(emb_datas)
best_class_indice = np.argmax(predict_proba, axis=1)
best_class_probability = predict_proba[np.arange(len(best_class_indice)), best_class_indice]
for i in range(len(best_class_indice)):
class_name = class_names[best_class_indice[i]]
class_probability = best_class_probability[i]
print("class_probability:",class_probability)
if class_probability < CLASS_PROBABILITY_THRESHOLD:
prediction = "unknown"
else:
prediction=class_name
break
predictions.append(prediction)

#每张图所有预测人脸结果
for prediction in predictions:
Expand Down
4 changes: 2 additions & 2 deletions test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def main(args):
print('Testing classifier')
prediction = model.predict(emb_datas) #预测结果
print(prediction)
predictions.append(class_names[prediction[0]])
predictions.append(class_names[prediction[0]]) #直接预测,总会得到最大的可能值


for prediction in predictions: #每张图所有预测人脸结果
Expand Down Expand Up @@ -134,7 +134,7 @@ def load_and_align_data(image_path, image_size, margin, gpu_memory_fraction):
def parse_arguments(argv):
parser = argparse.ArgumentParser()

parser.add_argument('--input_dir', type=str, help='Directory with unaligned images.',default='./images/test/')
parser.add_argument('--input_dir', type=str, help='Directory with unaligned images.',default='./images/test/policy')
parser.add_argument('--image_size', type=int,
help='Image size (height, width) in pixels.', default=182)
parser.add_argument('--margin', type=int,
Expand Down
2 changes: 1 addition & 1 deletion train_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def parse_arguments(argv):
parser = argparse.ArgumentParser()

parser.add_argument('--data_dir', type=str,
help='Path to the data directory containing aligned LFW face patches.',default='images/aligned_policy/')
help='Path to the data directory containing aligned LFW face patches.',default='images/train/aligned_policy/')
parser.add_argument('--model', type=str,
help='Could be either a directory containing the meta_file and ckpt_file or a model protobuf (.pb) file',default='models/policy/embeding.pb')
parser.add_argument('--classifier_filename',
Expand Down

0 comments on commit fe9027a

Please sign in to comment.