首页 > 解决方案 > ValueError:您的图层或模型处于无效状态

问题描述

我建立了一个包含人员检测和性别检测的两个模型的管道。人物检测模型基于 FasterRCNN,性别检测模型基于 Keras。当我尝试在烧瓶应用程序上运行管道时,我得到模型错误处于无效状态。人员检测代码如下:

from keras.preprocessing.image import img_to_array
from keras.models import load_model
from keras.utils import get_file
import numpy as np
import argparse
import cv2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import cvlib as cv
import tensorflow as tf
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
class GenderDetection():

    def __init__(self):
        model_path = './person_tracker/pre-trained/gender_detection.model'
        self.graph = tf.Graph()
        with tf.device('/cpu:0'):
             self.model = load_model(model_path)
             self.classes = ['Male','Female']
 

    def highlight_gender (self,image):   
        with tf.device('/cpu:0'):
            face, confidence = cv.detect_face(image)
        print("FACE IS DETECTED =", confidence)
        print("FACE IS DETECTED =", face)
        gender = []
        for idx, f in enumerate(face):       
            (startX, startY) = f[0], f[1]
            (endX, endY) = f[2], f[3]
            face_crop = np.copy(image[startY:endY,startX:endX])
            face_crop = cv2.resize(face_crop, (96,96))
            face_crop = face_crop.astype("float") / 255.0
            face_crop = img_to_array(face_crop)
            face_crop = np.expand_dims(face_crop, axis=0)        
            with tf.device('/cpu:0'):
                conf = self.model.predict(face_crop)[0]
            idx = np.argmax(conf)
            label = self.classes[idx]
            gender.append(label)
        return gender

当我尝试调用 self.model.predict() 函数来预测性别检测时,我在烧瓶服务器上遇到的错误如下所示:

ValueError: Your Layer or Model is in an invalid state. This can happen if you are interleaving estimator/non-estimator models or interleaving models/layers made in tf.compat.v1.Graph.as_default() with models/layers created outside of it. Converting a model to an estimator (via model_to_estimator) invalidates all models/layers made before the conversion (even if they were not the model converted to an estimator). Similarly, making a layer or a model inside a a tf.compat.v1.Graph invalidates all layers/models you previously made outside of the graph.

任何人都可以建议我解决它吗?如果我尝试在没有烧瓶服务器的情况下运行它,该管道可以正常工作。

标签: pythontensorflowflaskkerasfaster-rcnn

解决方案


推荐阅读