AttributeError: 'NoneType' object has no attribute '_inbound_nodes' Keras

给你一囗甜甜゛ 提交于 2021-01-27 18:51:19

问题


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()

I am trying to create a Siamese network on top of my existing FaceNet model. I am creating feature vectors using my pre-trained model.

Vector1 and Vector2 are my feature vectors which I am feeding to my Siamese network. Taking the absolute difference between them and feeding it to the Dense layer.

I am getting following Error when I define the model:

*Traceback (most recent call last): File "C:/Users/techolution/Desktop/Facenet/Create Vectors/Siamese Network - Test.py", line 104, in

siamese_net = Model(inputs=[img_a, img_b], outputs=prediction)

File "C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(*args, **kwargs)

File "C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py", line 94, in init self._init_graph_network(*args, **kwargs)

File "C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py", line 241, in _init_graph_network self.inputs, self.outputs)

File "C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py", line 1434, in _map_graph_network tensor_index=tensor_index)

File "C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py", line 1421, in build_map node_index, tensor_index)

File "C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py", line 1421, in build_map node_index, tensor_index)

File "C:\Users\techolution\Desktop\Facenet\Create Vectors\venv\lib\site-packages\keras\engine\network.py", line 1393, in build_map node = layer._inbound_nodes[node_index]

AttributeError: 'NoneType' object has no attribute '_inbound_nodes'*

来源:https://stackoverflow.com/questions/62138536/attributeerror-nonetype-object-has-no-attribute-inbound-nodes-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!