-- coding: utf-8 --
"""
Created on Sat Nov 24 14:13:47 2018
@author: ltx
"""
from keras.models import Sequential
from keras.layers import Conv2D, ZeroPadding2D, Activation, Input, concatenate
from keras.models import Model
from keras.layers.normalization import BatchNormalization
from keras.layers.pooling import MaxPooling2D, AveragePooling2D
from keras.layers.merge import Concatenate
from keras.layers.core import Lambda, Flatten, Dense
from keras.initializers import glorot_uniform
from keras.engine.topology import Layer
from keras import backend as K
------------用于繪制模型細節(jié)罢洲,可選--------------#
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
------------------------------------------------#
K.set_image_data_format('channels_first')
import time
import cv2
import os
import numpy as np
from numpy import genfromtxt
import pandas as pd
import tensorflow as tf
import fr_utils
from inception_blocks_v2 import * #Szegedy et al.設計了初始模型,用來將圖片生成向量
np.set_printoptions(threshold=np.nan)
--------------導入一個人臉模型----------------------
輸入一張人臉圖片(96,96,3),模型的維度結(jié)構(gòu)為(3,96,96)
輸出為一個128位的向量
FRmodel= faceRecoModel(input_shape=(3,96,96))
print("參數(shù)的數(shù)量為:"+str(FRmodel.count_params())) #3743280
使用三元組損失函數(shù)(A,P,N)進行訓練來實現(xiàn)同一個人的圖像編碼很近喉悴,不同的人的圖像編碼很遠
def triplet_loss(y_true,y_pred,alpha=0.2):
"""
參數(shù):
y_true--true標簽外潜,當你在Keras里定義了一個損失函數(shù)的時候需要它,但是這里不需要
y_pred---列表類型贞间,包含了如下參數(shù):
anchor -- 給定的“anchor”圖像的編碼贿条,維度為(None,128)
positive -- “positive”圖像的編碼,維度為(None,128)
negative -- “negative”圖像的編碼增热,維度為(None,128)
alpha整以;超參數(shù),閾值
返回:
loss--實數(shù)峻仇,損失的值
"""
#獲取anchor, positive, negative的圖像編碼
anchor,positive,negative=y_pred[0],y_pred[1],y_pred[2]
#第一步:計算"anchor" 與 "positive"之間編碼的距離公黑,這里需要使用axis=-1
pos_dist=tf.reduce_sum(tf.square(tf.subtract(anchor,positive)),axis=-1)
#第二步:計算"anchor" 與 "negative"之間編碼的距離,這里需要使用axis=-1
neg_dist=tf.reduce_sum(tf.square(tf.subtract(anchor,negative)),axis=-1)
#第三步:減去之前的兩個距離,然后加上alpha
basic_loss=tf.add(tf.subtract(pos_dist,neg_dist),alpha)
#通過取帶零的最大值和對訓練樣本的求和來計算整個公式
loss=tf.reduce_sum(tf.maximum(basic_loss,0))
return loss
with tf.Session() as test:
tf.set_random_seed(1)
y_true = (None, None, None)
y_pred = (tf.random_normal([3, 128], mean=6, stddev=0.1, seed = 1),
tf.random_normal([3, 128], mean=1, stddev=1, seed = 1),
tf.random_normal([3, 128], mean=3, stddev=4, seed = 1))
loss = triplet_loss(y_true, y_pred)
print("loss = " + str(loss.eval()))
----------------加載已訓練好的FaceNet模型-----------------
開始時間
start_time=time.clock()
編譯模型,反向傳播凡蚜,訓練和優(yōu)化模型
FRmodel.compile(optimizer='adam',loss=triplet_loss,metrics=['accuracy'])
加載權(quán)值
fr_utils.load_weights_from_FaceNet(FRmodel)
結(jié)束時間
end_time=time.clock()
計算時間差
minium=end_time-start_time
print("執(zhí)行了:" + str(int(minium / 60)) + "分" + str(int(minium%60)) + "秒")
---------加載數(shù)據(jù)庫---------------------
database = {}
database["danielle"] = fr_utils.img_to_encoding("images/danielle.png", FRmodel)
database["younes"] = fr_utils.img_to_encoding("images/younes.jpg", FRmodel)
database["tian"] = fr_utils.img_to_encoding("images/tian.jpg", FRmodel)
database["andrew"] = fr_utils.img_to_encoding("images/andrew.jpg", FRmodel)
database["kian"] = fr_utils.img_to_encoding("images/kian.jpg", FRmodel)
database["dan"] = fr_utils.img_to_encoding("images/dan.jpg", FRmodel)
database["sebastiano"] = fr_utils.img_to_encoding("images/sebastiano.jpg", FRmodel)
database["bertrand"] = fr_utils.img_to_encoding("images/bertrand.jpg", FRmodel)
database["kevin"] = fr_utils.img_to_encoding("images/kevin.jpg", FRmodel)
database["felix"] = fr_utils.img_to_encoding("images/felix.jpg", FRmodel)
database["benoit"] = fr_utils.img_to_encoding("images/benoit.jpg", FRmodel)
database["arnaud"] = fr_utils.img_to_encoding("images/arnaud.jpg", FRmodel)
實現(xiàn)了人臉驗證
def verify(image_path,identity,database,model):
encoding=fr_utils.img_to_encoding(image_path,model)
dist = np.linalg.norm(encoding - database[identity])
if dist < 0.7:
print("歡迎 " + str(identity) + "回家奠骄!")
is_door_open = True
else:
print("經(jīng)驗證,您與" + str(identity) + "不符番刊!")
is_door_open = False
return dist, is_door_open
verify("images/camera_0.jpg","younes",database,FRmodel)
verify("images/camera_2.jpg", "kian", database, FRmodel)
實現(xiàn)了人臉識別
def who_is_it(image_path,database,model):
#步驟1:計算指定圖像的編碼含鳞,使用fr_utils.img_to_encoding()來計算
encoding=fr_utils.img_to_encoding(image_path,model)
#步驟2 :找到最相近的編碼
## 初始化min_dist變量為足夠大的數(shù)字,這里設置為100
min_dist=100
for(name,enc)in database.items():
dist = np.linalg.norm(encoding - enc)
if dist < min_dist:
min_dist=dist
identity=name
if min_dist > 0.7:
print("抱歉芹务,您的信息不在數(shù)據(jù)庫中蝉绷。")
else:
print("姓名" + str(identity) + " 差距:" + str(min_dist))
return min_dist, identity
who_is_it("images/camera_0.jpg", database, FRmodel)
----------------------------------實驗結(jié)果-------------------------------------