from Config import Config
from FaceDetection.MTCNNDetect import MTCNNDetect
import cv2
import tensorflow as tf
import keras
from keras import backend as K
from keras.layers import Input, Lambda, Dense, Dropout, Convolution2D, MaxPooling2D, Flatten, Concatenate, concatenate
from keras.models import Model
face_detect = MTCNNDetect(model_path=Config.MTCNN_MODEL)
from FaceRecognition.TensorflowGraph import FaceRecGraph
from src.FaceAlignment import AlignCustom
from FaceRecognition.FaceFeature import FaceFeature
FRGraph = FaceRecGraph()
aligner = AlignCustom()
extract_feature = FaceFeature(FRGraph)
img1 = cv2.imread('data5/s1/8.jpg')
rects, landmarks = face_detect.detect_face(img1, Config.MINIMUM_FACE_SIZE_FOR_REGISTRATION)
# print("length of rect", len(rects))
if len(rects) == 1:
for i, rect in enumerate(rects):
if rect != []:
aligned_face = aligner.align(160, img1, landmarks[i])
vector1 = np.squeeze(extract_feature.get_features(np.expand_dims(aligned_face, axis=0)), axis=0)
img2 = cv2.imread('data5/s1/10.jpg')
rects, landmarks = face_detect.detect_face(img2, Config.MINIMUM_FACE_SIZE_FOR_REGISTRATION)
# print("length of rect", len(rects))
if len(rects) == 1:
for i, rect in enumerate(rects):
if rect != []:
aligned_face = aligner.align(160, img2, landmarks[i])
vector2 = np.squeeze(extract_feature.get_features(np.expand_dims(aligned_face, axis=0)), axis=0)
vec1 = vector1.reshape(1, 128)
vec2 = vector2.reshape(1, 128)
data_tensor1 = tf.convert_to_tensor(vec1)
data_tensor2 = tf.convert_to_tensor(vec2)
input_dim = (224,224,3)
img_a = tf.keras.layers.Input(shape=input_dim)
img_b = tf.keras.layers.Input(shape=input_dim)
L1_layer = Lambda(lambda tensors: tf.abs(tensors[0] - tensors[1]))
L1_distance = L1_layer([data_tensor1, data_tensor2])
prediction = Dense(1, activation='sigmoid')(L1_distance)
siamese_net = Model(inputs=[img_a, img_b], outputs=prediction)
siamese_net.summary()
我正在尝试在我现有的 FaceNet 模型之上创建一个连体网络。我正在使用我的预训练模型创建特征向量。
Vector1 和 Vector2 是我提供给连体网络的特征向量。取它们之间的绝对差并将其馈送到 Dense 层。
定义模型时出现以下错误:
* Traceback(最近一次通话最后):文件“C:/Users/techolution/Desktop/Facenet/Create Vectors/Siamese Network - Test.py”,第 104 行,在
siamese_net = Model(inputs=[img_a, img_b], outputs=prediction)
文件“C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\legacy\interfaces.py”,第 91 行,在包装返回 func(*args, **kwargs)
文件“C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py”,第 94 行,init self._init_graph_network(*args, **kwargs)
文件“C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py”,第 241 行,在 _init_graph_network self.inputs,self.outputs 中)
文件“C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py”,第 1434 行,在 _map_graph_network tensor_index=tensor_index)
文件“C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py”,第 1421 行,在 build_map 节点索引、张量索引中)
文件“C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py”,第 1421 行,在 build_map 节点索引、张量索引中)
文件“C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py”,第 1393 行,在 build_map node = layer._inbound_nodes[node_index]
AttributeError:“NoneType”对象没有属性“_inbound_nodes”*