【问题标题】:Tensor Tensor("flatten/Reshape:0", shape=(?, 2622), dtype=float32) is not an element of this graphTensor Tensor("flatten/Reshape:0", shape=(?, 2622), dtype=float32) 不是该图的元素
【发布时间】:2021-05-23 11:42:24
【问题描述】:

您好 StackOverFlow 团队: 我基于(Vgg_Face_Model)构建了一个模型,加载了权重(vgg_face_weights.h5)。 请注意,我使用 tensorflow-gpu = 2.1.0 和 keras=2.3.1 ,Anaconda 3 将其创建为解释器并与 pycharm 一起使用 但是代码显示部分错误:

 input_descriptor = [model.predict(face), img]

代码是:

def face_recognizer(face, db_descriptors):
            # face = cv2.imread(img)
            # face = cv2.resize(face, (IMG_Size, IMG_Size))
            t0 = time.perf_counter()
            face = np.array(face).reshape(-1, IMG_Size, IMG_Size, 3)

            ###### here error #################################
            input_descriptor = [model.predict(face), img]
            ###################################################

            K_nn_result = K_nn_Classifier(input_descriptor[0], db_descriptors, 5)
            input_result = Knn_Distance_Score(K_nn_result)
            if input_result[0] <= 10:
                identity = 'stranger'
            else:
                identity = input_result[1]
            # print('Done in',time.perf_counter()-t0)
            return input_result, identity

    def PrepareModels(self):
        global  mpFaceDetection, FaceDetector, model
        mpFaceDetection = mp.solutions.face_detection
        FaceDetector = mpFaceDetection.FaceDetection()
        model = loadModel()

型号是:

import os
from pathlib import Path
# from tensorflow.keras.models import Model, Sequential

from tensorflow.keras.models import Model, Sequential, load_model
from tensorflow.keras.layers import Input, Convolution2D, ZeroPadding2D, MaxPooling2D, Flatten, Dense, Dropout, \
    Activation
import gdown


# ---------------------------------------

def Vgg_Face_Model():
    model = Sequential()
    model.add(ZeroPadding2D((1, 1), input_shape=(224, 224, 3)))
    model.add(Convolution2D(64, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(Convolution2D(4096, (7, 7), activation='relu'))
    model.add(Dropout(0.5))
    model.add(Convolution2D(4096, (1, 1), activation='relu'))
    model.add(Dropout(0.5))
    model.add(Convolution2D(2622, (1, 1)))
    model.add(Flatten())
    model.add(Activation('softmax'))

    return model


def loadModel():
    model = Vgg_Face_Model()

    # -----------------------------------

    home = str(Path.home())

    if os.path.isfile(home + '/.deepface/weights/vgg_face_weights.h5') != True:
        print("vgg_face_weights.h5 will be downloaded...")

        url = 'https://drive.google.com/uc?id=1CPSeum3HpopfomUEK1gybeuIVoeJT_Eo'
        output = home + '/.deepface/weights/vgg_face_weights.h5'
        gdown.download(url, output, quiet=False)

    # -----------------------------------

    model.load_weights(home + '/.deepface/weights/vgg_face_weights.h5')

    # -----------------------------------

    # TO-DO: why?
    vgg_model_descriptor = Model(inputs=model.layers[0].input, outputs=model.layers[-2].output)

    return vgg_model_descriptor


# model = loadModel()

输出:

Tensor Tensor("flatten/Reshape:0", shape=(?, 2622), dtype=float32) is not an element of this graph.'

【问题讨论】:

    标签: tensorflow deep-learning conv-neural-network keras-vggface


    【解决方案1】:
    from tensorflow.python.keras.backend import set_session
    sess = tf.Session()
    
    #This is a global session and graph
    graph = tf.get_default_graph()
    set_session(sess)
    
    
    #now where you are calling the model
    global sess
    global graph
    with graph.as_default():
        set_session(sess)
        input_descriptor = [model.predict(face), img]
    

    【讨论】:

    • 谢谢 Akash,它正在工作。但在 114 fbps 时变慢 7 Fbps。注意:我使用了 GPU。为什么?
    • 你确定这个进程使用的是gpu吗?
    猜你喜欢
    • 2019-04-22
    • 2020-06-09
    • 2019-09-17
    • 2019-10-23
    • 2018-04-27
    • 1970-01-01
    • 2019-09-24
    • 2018-04-25
    • 2019-07-18
    相关资源
    最近更新 更多