老早以前就希望能写一篇关于人脸识别的工程化落地的案例,一年前做疲劳驾驶时使用的dlib插件,它封装好了,人脸检测、对齐、相似度计算三个部分,就是插件比较难装,但同时也少了很多细节.
今天我们来做一些高级一点的, facenet网络
谷歌人脸识别算法,发表于 CVPR 2015,利用相同人脸在不同角度等姿态的照片下有高内聚性,不同人脸有低耦合性,提出使用 cnn + triplet mining 方法,在 LFW 数据集上准确度达到 99.63%。
测试时只需要计算人脸特征EMBEDDING,然后计算距离使用阈值即可判定两张人脸照片是否属于相同的个体。
在这里插入图片描述
简单来讲,在使用阶段,facenet即是:
1、输入一张人脸图片
2、通过深度卷积网络提取特征
3、L2标准化
4、得到一个长度为128特征向量。
其它人在使用中一般是这样的
#---------------------------------------------------## 检测图片#---------------------------------------------------#def detect_image(self, image_1, image_2):#---------------------------------------------------## 图片预处理,归一化#---------------------------------------------------#with torch.no_grad():image_1 = resize_image(image_1, [self.input_shape[1], self.input_shape[0]], letterbox_image=self.letterbox_image)image_2 = resize_image(image_2, [self.input_shape[1], self.input_shape[0]], letterbox_image=self.letterbox_image)photo_1 = torch.from_numpy(np.expand_dims(np.transpose(preprocess_input(np.array(image_1, np.float32)), (2, 0, 1)), 0))photo_2 = torch.from_numpy(np.expand_dims(np.transpose(preprocess_input(np.array(image_2, np.float32)), (2, 0, 1)), 0))if self.cuda:photo_1 = photo_1.cuda()photo_2 = photo_2.cuda()#---------------------------------------------------## 图片传入网络进行预测#---------------------------------------------------#output1 = self.net(photo_1).cpu().numpy()output2 = self.net(photo_2).cpu().numpy()#---------------------------------------------------## 计算二者之间的距离#---------------------------------------------------#l1 = np.linalg.norm(output1 - output2, axis=1)# plt.subplot(1, 2, 1)# plt.imshow(np.array(image_1))# plt.subplot(1, 2, 2)# plt.imshow(np.array(image_2))# plt.text(-12, -12, 'Distance:%.3f' % l1, ha='center', va= 'bottom',fontsize=11)# plt.show()return l1
核心思想是:传入两张图片,计算距离,设置阈值判断是否是同一个人.
可是,这种放在工程上,比如人脸开门是不行的,所谓人脸识别,必须有一个人脸库,送入一张图片和人脸库的所有图片进行遍历比对,挑出相似度最高的一张图片,和阈值比对,决定是否开门,并留存记录.
那肯定不能次次遍历人脸库啊,所以人脸库的图片要先转出特征向量的collection,存起来, 送入一张图片后,用模型抽取向量特征,和collections里面的向量比较.
如果人脸库太大,比如几万张特征,我们可以分batch存,每个batch找出最合适的,有点像桶排序,并且并行起来计算,是不是比较有意思.
#---------------------------------------------------## 保存人脸库特征#---------------------------------------------------#def save_to_tensor(self):#---------------------------------------------------## 图片预处理,归一化#---------------------------------------------------#path_dir = "./img"file_name_list = os.listdir(path_dir)img_feature = {}with torch.no_grad():for file_name in file_name_list:image_1 = Image.open(os.path.join(path_dir,file_name))image_1 = resize_image(image_1, [self.input_shape[1], self.input_shape[0]], letterbox_image=self.letterbox_image)photo_1 = torch.from_numpy(np.expand_dims(np.transpose(preprocess_input(np.array(image_1, np.float32)), (2, 0, 1)), 0))if self.cuda:photo_1 = photo_1.cuda() #---------------------------------------------------## 图片传入网络进行预测#---------------------------------------------------#output1 = self.net(photo_1).cpu().numpy()img_feature[file_name] = output1print(img_feature)with open("img_feature.txt", "wb") as file:pickle.dump(img_feature, file)return None
经过这些代码,把./img/ 下的人脸库向量都以二进制的方式存入了文件中.
送入一张图片,只需要,将图片送入模型,获取当前人的人脸信息,然后和人脸库的向量比较,
def get_from_face_collection(self):input_file = "img/1_001.jpg"image_1 = Image.open(input_file)image_1 = resize_image(image_1, [self.input_shape[1], self.input_shape[0]], letterbox_image=self.letterbox_image)with torch.no_grad():photo_1 = torch.from_numpy(np.expand_dims(np.transpose(preprocess_input(np.array(image_1, np.float32)), (2, 0, 1)), 0))if self.cuda:photo_1 = photo_1.cuda() #---------------------------------------------------## 图片传入网络进行预测#---------------------------------------------------#output1 = self.net(photo_1).cpu().numpy()f = open('img_feature.txt','rb')img_feature_json = pickle.load(f)distance_map = {}for img_name, featrue_map in img_feature_json.items():l1 = np.linalg.norm(output1 - featrue_map, axis=1)distance_map[img_name] = l1print(distance_map)
distance_map 就是你说要的结果集,然后做个排序,找到top1 和阈值比对即可
git:https://github.com/justinge/facenet_pytorch.git
还有用milivus向量库的,先挖个坑,最终工程化一个到位的.