环境简要说明
python3.7
设计主要相关依赖包版本
tensorflow = 1.15.0
keras = 2.3.1
flask = 1.1.1
imageai = 2.1.5
文件说明
init.py
from imageai.Detection import ObjectDetection
from flask import Flask
import tensorflow as tf
# 省略
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath("./people/app/image_recognition/resnet50_coco_best_v2.0.1.h5")
detector.loadModel(detection_speed='fast')
# 省略
app = Flask(__name__)
app.config.from_object("food.config")
views.py
# 省略其他库的导入
from .. import app, detector
def cutting_image(filename):
custom_objects = detector.CustomObjects(fork=True)
output_objects_array, detections, detected_objects_image_array = detector.detectCustomObjectsFromImage(input_image=filename,
output_type = 'array',
minimum_percentage_probability=27,
extract_detected_objects = True,
custom_objects=custom_objects)
@xxx.route('/xxxx',methods=['POST'])
def get_msg():
# 省略
msg = cutting_image(filename)
# 省略
return result
运行代码
运行flask程序,设置为多线程
python run.py runservere -h 0.0.0.0 -p 5000 --thread
这里没什么问题
现在开始调用接口
r = requests.post("http://127.0.0.1:5000/xxxx", data=body)
返回结果如下
2019-12-26 17:17:29,911:ERROR:food.app:Exception on /recommend/recognition_food [POST]
Traceback (most recent call last):
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/imageai/Detection/__init__.py", line 717, in detectCustomObjectsFromImage
_, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/keras/engine/training.py", line 1505, in predict_on_batch
self._make_predict_function()
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/keras/engine/training.py", line 557, in _make_predict_function
**kwargs)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 3010, in function
return Function(inputs, outputs, updates=updates, **kwargs)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 2808, in __init__
with tf.control_dependencies(self.outputs):
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 5254, in control_dependencies
return get_default_graph().control_dependencies(control_inputs)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 4688, in control_dependencies
c = self.as_graph_element(c)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 3607, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 3686, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("regression/concat:0", shape=(?, ?, 4), dtype=float32) is not an element of this graph.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/yolanda/MyYolanda/user_analyze/food_recommend/project/food/app/image_recognition/views.py", line 198, in get_fod_msg
all_foods_msg = recognition_all_food(path_open=image_path)
File "/home/yolanda/MyYolanda/user_analyze/food_recommend/project/food/app/image_recognition/views.py", line 140, in recognition_all_food
images_base64 = cutting_image(path_open)
File "/home/yolanda/MyYolanda/user_analyze/food_recommend/project/food/app/image_recognition/views.py", line 45, in cutting_image
custom_objects=custom_objects) # 指定所要检测的物品类型,默认为全部
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/imageai/Detection/__init__.py", line 920, in detectCustomObjectsFromImage
"Ensure you specified correct input image, input type, output type and/or output image path ")
ValueError: Ensure you specified correct input image, input type, output type and/or output image path
之前遇到过这问题,出现ValueError: Tensor Tensor("regression/concat:0", shape=(?, ?, 4), dtype=float32) is not an element of this graph.
都是表明模型导入时图混乱,需要固定图
解决方式如下:
方法一:
init.py
加入detector.__graph = tf.get_default_graph()
from imageai.Detection import ObjectDetection
from flask import Flask
import tensorflow as tf
# 省略
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath("./people/app/image_recognition/resnet50_coco_best_v2.0.1.h5")
detector.loadModel(detection_speed='fast')
detector.__graph = tf.get_default_graph() # <<<<<<<<<<<<<<<保存模型图,防止模型混乱
# 省略
app = Flask(__name__)
app.config.from_object("food.config")
views.py
加入with detector.__graph.as_default():
### 省略
from .. import app, detector
def cutting_image(filename):
with detector.__graph.as_default(): # <<<<<<<<<<<<<<<<<<<<<添加
custom_objects = detector.CustomObjects(bowl=True)
output_objects_array, detections, detected_objects_image_array = detector.detectCustomObjectsFromImage(input_image=filename,
output_type = 'array',
minimum_percentage_probability=27,
extract_detected_objects = True,
custom_objects=custom_objects)
@xxx.route('/xxxx',methods=['POST'])
def get_msg():
# 省略
msg = cutting_image(filename)
# 省略
return result
方法二
views.py
detectCustomObjectsFromImage()
自带参数thread_safe,设置为True则表示允许多线程下运行,
比较简便,直接用着方法就行了
def cutting_image(filename):
custom_objects = detector.CustomObjects(bowl=True)
output_objects_array, detections, detected_objects_image_array = detector.detectCustomObjectsFromImage(input_image=filename,
output_type = 'array',
minimum_percentage_probability=27,
extract_detected_objects = True,
thread_safe=True, # <<<<<<<<<<<<<<<<<<<<<<允许多线程
custom_objects=custom_objects)
程序启动,输出如下所示,这里应该没什么问题,不用细看
现在开始调用接口
r = requests.post("http://127.0.0.1:5000/xxxx", data=body)
返回结果如下:
2019-12-26 11:29:01.091649: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node nms/strided_slice. Error: Pack node (boxes/stack) axis attribute is out of bounds: 2
2019-12-26 11:29:01.765712: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node nms/strided_slice. Error: Pack node (boxes/stack) axis attribute is out of bounds: 2
2019-12-26 11:29:02,142:ERROR:food.app:Exception on /recommend/recognition_food [POST]
Traceback (most recent call last):
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/imageai/Detection/__init__.py", line 717, in detectCustomObjectsFromImage
_, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/keras/engine/training.py", line 1580, in predict_on_batch
outputs = self.predict_function(ins)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/backend.py", line 3076, in __call__
run_metadata=self.run_metadata)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1439, in __call__
run_metadata_ptr)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable bn2a_branch2b/gamma from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/bn2a_branch2b/gamma)
[[{{node bn2a_branch2b/ReadVariableOp}}]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/yolanda/MyYolanda/user_analyze/food_recommend/project/food/app/image_recognition/views.py", line 198, in get_fod_msg
all_foods_msg = recognition_all_food(path_open=image_path)
File "/home/yolanda/MyYolanda/user_analyze/food_recommend/project/food/app/image_recognition/views.py", line 140, in recognition_all_food
images_base64 = cutting_image(path_open)
File "/home/yolanda/MyYolanda/user_analyze/food_recommend/project/food/app/image_recognition/views.py", line 45, in cutting_image
custom_objects=custom_objects) # 指定所要检测的物品类型,默认为全部
File "/home/yolanda/anaconda3/lib/python3.7/site-packages/imageai/Detection/__init__.py", line 920, in detectCustomObjectsFromImage
"Ensure you specified correct input image, input type, output type and/or output image path ")
ValueError: Ensure you specified correct input image, input type, output type and/or output image path
里面主要是这么个错误tensorflow.python.framework.errors_impl.FailedPreconditionError
,提示是说读取错误
解决方式
因为我没有gpu,是用cpu的跑的,我怀疑有这方面原因
于是我到google colab上选择gpu进行测试,(google提供的免费云服务,免费提供gpu)
flask多线程下是能正常使用imageai的,输出结果正常
接着我还是在google colab上取消了gpu再次进行测试,发现结果依旧正常
对比了我本地和google colab里面依赖坏境,我keras版本较高
对此,我解决的方法是将keras降为2.2.5版本
pip install keras==2.2.5
问题解决