python - 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.
任何人都可以建议我解决它吗?如果我尝试在没有烧瓶服务器的情况下运行它,该管道可以正常工作。
解决方案
推荐阅读
- reactjs - 数学图像名称。[hash] 通过与从数据库中获取的纯图像名称反应构建
- php - 找不到类“App\TestService” - Laravel
- azure - 授予对 Azure 存储帐户的用户分配标识的读取访问权限
- javascript - 查看 WebGL 内存
- header - 使用空手道修改标题
- c# - 通过 C# .NET 应用程序调用时无法识别命令,但直接调用时有效
- r - 使用 ShinyFiles::ShinyFileChoose() 访问网络驱动器上的文件时出现问题 - parseFilePaths() 函数返回 NA
- upgrade - Apache Ignite 从 1.8 升级到最新版本
- angular - 如何取消 Ag-grid OnFilterChanged 事件?
- javascript - Swiper.js 延迟图像加载在移动设备上以错误的高度启动