尝试使用flask创建API用于对象检测YOLOV4.它抛出了一些tensorflow错误



我正在学习深度学习,并使用YOLO练习对象检测。最近,我修改了一些代码,并仅使用中可用的代码中的YOLOV4 tensorflow GPU成功地检测到了人类https://github.com/hunglc007/tensorflow-yolov4-tflite.现在,我正在尝试使用flask实现客户端-服务器体系结构,但遇到了一个惊人的问题。

首先,我注意到,

app.run(main_func)

每当我启动程序时都会运行两次。后来,它总是被卡住

pred_bbox = infer(batch_data)

它抛出这样的错误:

tensorflow.python.framework.errors_impl.FailedPreconditionError:  
Error while reading resource variable batch_normalization_95/gamma_57417 from Container: localhost. 
This could mean that the variable was uninitialized. 
Not found: Resource localhost/batch_normalization_95/gamma_57417/class tensorflow::Var does not exist.
[[{{node StatefulPartitionedCall/functional_1/batch_normalization_95/ReadVariableOp}}]] 
[Op:__inference_signature_wrapper_5589]

函数调用堆栈:

signature_wrapper

这个错误是在我把程序分成两个函数之后开始出现的。我把程序分成两个函数,因为我不想每次都加载模型,我打API浪费时间,因为加载大约需要10-15秒。

我的代码片段在这里:

import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
from absl import app, flags, logging
from absl.flags import FLAGS
import core.utils as utils
from core.yolov4 import filter_boxes
from tensorflow.python.saved_model import tag_constants
from PIL import Image
import os
import cv2
import time
import numpy as np
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
from helping_functions import get_centroids, get_human_box_detection, get_points_from_box
from flask import Flask, request, Response, jsonify, send_from_directory, abort
flags.DEFINE_string('framework', 'tf', '(tf, tflite, trt')
flags.DEFINE_string('weights', './checkpoints/yolov4-416',
'path to weights file')
flags.DEFINE_integer('size', 416, 'resize images to')
flags.DEFINE_boolean('tiny', False, 'yolo or yolo-tiny')
flags.DEFINE_string('model', 'yolov4', 'yolov3 or yolov4')
flags.DEFINE_string('image', './data/kite.jpg', 'path to input image')
flags.DEFINE_string('output', 'result.png', 'path to output image')
flags.DEFINE_float('iou', 0.45, 'iou threshold')
flags.DEFINE_float('score', 0.25, 'score threshold')

infer = ""
input_size = 0
# Initialize Flask application
flask_app = Flask(__name__)
# API that returns JSON with classes found in images
@flask_app.route('/detection', methods=['POST'])
def human_detect():
# flask_app.run(debug=True, host = '0.0.0.0', port=5000)
while True:
image = request.files["images"]
image_path = image.filename
image.save(os.path.join(os.getcwd(), image_path))
# image_path = FLAGS.image
if image_path != "":
original_image = cv2.imread(f"./{image_path}")
# print(original_image)
original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
image_data = cv2.resize(original_image, (input_size, input_size))
image_data = image_data / 255.
image_data = image_data[np.newaxis, ...].astype(np.float32)
batch_data = tf.constant(image_data)
print("infer")
print(infer)
print()
pred_bbox = infer(batch_data)
print("pred_box")
print(pred_bbox)
print()
for key, value in pred_bbox.items():
boxes = value[:, :, 0:4]
pred_conf = value[:, :, 4:]
boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
scores=tf.reshape(
pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
max_output_size_per_class=50,
max_total_size=50,
iou_threshold=FLAGS.iou,
score_threshold=FLAGS.score
)
final_boxes = boxes.numpy()
final_scores = scores.numpy()
final_classes = classes.numpy()
array_boxes_detected = []
if len(boxes)>0:
array_boxes_detected = get_human_box_detection(final_boxes,final_scores[0].tolist(),final_classes[0].tolist(),original_image.shape[0],original_image.shape[1])
try:
return jsonify({"response":array_boxes_detected}), 200
except FileNotFoundError:
abort(404)
else:
print("Waiting for imagepath")
time.sleep(10)
def main_func(_argv):
print()
print("Running main function.")
print()
global infer
global input_size
if infer == "":
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
# image_path = FLAGS.image
saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
infer = saved_model_loaded.signatures['serving_default']
input_size = FLAGS.size
# human_detect(infer)

if __name__ == '__main__':
try:
app.run(main_func)
except SystemExit:
pass
# app.run(main_func)
flask_app.run(debug=True, host = '0.0.0.0', port=5000)

这是在我通过添加烧瓶并将代码划分为两个函数来修改代码之前:

import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
from absl import app, flags, logging
from absl.flags import FLAGS
import core.utils as utils
from core.yolov4 import filter_boxes
from tensorflow.python.saved_model import tag_constants
from PIL import Image
import cv2
import numpy as np
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
from helping_functions import get_centroids, get_human_box_detection, get_points_from_box
flags.DEFINE_string('framework', 'tf', '(tf, tflite, trt')
flags.DEFINE_string('weights', './checkpoints/yolov4-416',
'path to weights file')
flags.DEFINE_integer('size', 416, 'resize images to')
flags.DEFINE_boolean('tiny', False, 'yolo or yolo-tiny')
flags.DEFINE_string('model', 'yolov4', 'yolov3 or yolov4')
flags.DEFINE_string('image', './data/kite.jpg', 'path to input image')
flags.DEFINE_string('output', 'result.png', 'path to output image')
flags.DEFINE_float('iou', 0.45, 'iou threshold')
flags.DEFINE_float('score', 0.25, 'score threshold')
def main(_argv):
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
input_size = FLAGS.size
image_path = FLAGS.image
original_image = cv2.imread(image_path)
original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
# image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size])
image_data = cv2.resize(original_image, (input_size, input_size))
image_data = image_data / 255.
# image_data = image_data[np.newaxis, ...].astype(np.float32)
images_data = []
for i in range(1):
images_data.append(image_data)
images_data = np.asarray(images_data).astype(np.float32)
if FLAGS.framework == 'tflite':
interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print(input_details)
print(output_details)
interpreter.set_tensor(input_details[0]['index'], images_data)
interpreter.invoke()
pred = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))]
if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
boxes, pred_conf = filter_boxes(pred[1], pred[0], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
else:
boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
else:
saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
infer = saved_model_loaded.signatures['serving_default']
batch_data = tf.constant(images_data)
pred_bbox = infer(batch_data)
for key, value in pred_bbox.items():
boxes = value[:, :, 0:4]
pred_conf = value[:, :, 4:]
boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
scores=tf.reshape(
pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
max_output_size_per_class=50,
max_total_size=50,
iou_threshold=FLAGS.iou,
score_threshold=FLAGS.score
)
final_boxes = boxes.numpy()
final_scores = scores.numpy()
final_classes = classes.numpy()
array_boxes_detected = get_human_box_detection(final_boxes,final_scores[0].tolist(),final_classes[0].tolist(),original_image.shape[0],original_image.shape[1])
print(array_boxes_detected)

#Defining red color rgb value
COLOR_RED = (0, 0, 255)
for i,items in enumerate(array_boxes_detected):
first_point = array_boxes_detected[i][0]
second_point = array_boxes_detected[i][1]
third_point = array_boxes_detected[i][2]
fourth_point = array_boxes_detected[i][3]
cv2.rectangle(original_image,(second_point,first_point),(fourth_point,third_point),COLOR_RED,2)
image = cv2.cvtColor(np.array(original_image), cv2.COLOR_BGR2RGB)
cv2.imshow("final", image)
cv2.waitKey(0)
if __name__ == '__main__':
try:
app.run(main)
except SystemExit:
pass

我正处于deep learningtensorflowflask的学习阶段。因此,我们将非常感谢您的帮助。非常感谢。

这段代码对我有效。我已经使用poster进行了测试。

import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
from absl import app, flags, logging
from absl.flags import FLAGS
import core.utils as utils
from core.yolov4 import filter_boxes
from tensorflow.python.saved_model import tag_constants
from PIL import Image
import os
import sys
import cv2
import time
import numpy as np
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
from helping_functions import get_centroids, get_human_box_detection, get_points_from_box
from flask import Flask, request, Response, jsonify, send_from_directory, abort
flags.DEFINE_string('framework', 'tf', '(tf, tflite, trt')
flags.DEFINE_string('weights', './checkpoints/yolov4-416',
'path to weights file')
flags.DEFINE_integer('size', 320, 'resize images to')
flags.DEFINE_boolean('tiny', False, 'yolo or yolo-tiny')
flags.DEFINE_string('model', 'yolov4', 'yolov3 or yolov4')
flags.DEFINE_string('image', './data/kite.jpg', 'path to input image')
flags.DEFINE_string('output', 'result.png', 'path to output image')
flags.DEFINE_float('iou', 0.45, 'iou threshold')
flags.DEFINE_float('score', 0.25, 'score threshold')
FLAGS(sys.argv)
# Initialize Flask application
flask_app = Flask(__name__)
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
infer = saved_model_loaded.signatures['serving_default']
input_size = FLAGS.size
# API that returns JSON with classes found in images
@flask_app.route('/detection', methods=['POST'])
def human_detect():
image = request.files["images"]
image_path = image.filename
image.save(os.path.join(os.getcwd(), image_path))
if image_path != "":
original_image = cv2.imread(f"./{image_path}")
original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
image_data = cv2.resize(original_image, (input_size, input_size))
image_data = image_data / 255.
image_data = image_data[np.newaxis, ...].astype(np.float32)
batch_data = tf.constant(image_data)
pred_bbox = infer(batch_data)
for key, value in pred_bbox.items():
boxes = value[:, :, 0:4]
pred_conf = value[:, :, 4:]
boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
scores=tf.reshape(
pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
max_output_size_per_class=50,
max_total_size=50,
iou_threshold=FLAGS.iou,
score_threshold=FLAGS.score
)
final_boxes = boxes.numpy()
final_scores = scores.numpy()
final_classes = classes.numpy()
array_boxes_detected = []
if len(boxes)>0:
array_boxes_detected = get_human_box_detection(final_boxes,final_scores[0].tolist(),final_classes[0].tolist(),original_image.shape[0],original_image.shape[1])
try:
return jsonify({"response":array_boxes_detected}), 200
except FileNotFoundError:
abort(404)
if __name__ == '__main__':
flask_app.run(debug=True, host = '127.0.0.1', port=5000)

最新更新