请教关于ocr模型在flask重用的问题
Created by: fisipro
各位老师好:
请教一个问题,我用ocr_recognition将训练好的模型放在flask上进行预测,预测文件用的是infer.py,因为每次模型都要重新加载,所以我想在flask app里重用这个模型,于是我进行了以下改造,尝试缓存加载好的exe或者prog对象,但是出现了奇怪的问题:
当第一次HTTP请求时,一切都正常预测;当后续HTTP请求时,报出异常
希望各位老师能帮忙看看,万分感谢!!
改过的infer.py代码,app参数是调用文件传过来的flask对象:
def inference(app, infer=ctc_infer, data_reader=ctc_reader):
num_classes = data_reader.num_classes()
data_shape = data_reader.data_shape()
# define network
images = fluid.layers.data(name='pixel', shape=data_shape, dtype='float32')
sequence = infer(images, num_classes)
# data reader
infer_reader = data_reader.inference(
infer_images_dir=args.input_images_dir,
infer_list_file=args.input_images_list)
place = fluid.CPUPlace()
prog = None
if (hasattr(app, 'exe') == False):
# prepare environment
exe = fluid.Executor(place)
# load init model
model_dir = os.path.dirname(args.model_path)
model_file_name = os.path.basename(args.model_path)
prog = fluid.default_main_program()
fluid.io.load_params(exe, dirname=model_dir, main_program=prog, filename=model_file_name)
app.exe = exe
# load dictionary
dict_map = keys.alphabet[:]
if args.dict is not None and os.path.isfile(args.dict):
dict_map = {}
with open(args.dict) as dict_file:
for i, word in enumerate(dict_file):
dict_map[i] = word.strip()
print "Loaded dict from %s" % args.dict
for data in infer_reader():
result = app.exe.run(prog,
feed=get_feeder_data(
data, place, need_label=False),
fetch_list=[sequence],
return_numpy=False)
indexes = np.array(result[0]).flatten()
if dict_map is not None:
s = ""
for index in indexes:
s += dict_map[index]
#s += str(index) + " "
print s
else:
print "result: %s" % (indexes, )
后续请求报出的异常:
paddle.fluid.core.EnforceNotMet
EnforceNotMet: enforce in_dims.size() == filter_dims.size() failed, 4 != 1 Conv input dimension and filter dimension should be the same. at [/paddle/paddle/fluid/operators/conv_op.cc:49] PaddlePaddle Call Stacks: 0 0x7f6d33160c76p paddle::platform::EnforceNotMet::EnforceNotMet(std::exception_ptr::exception_ptr, char const*, int) + 486 1 0x7f6d33c6490ap paddle::operators::ConvOp::InferShape(paddle::framework::InferShapeContext*) const + 7482 2 0x7f6d33f97a1bp paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) const + 91 3 0x7f6d33f9518dp paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) + 205 4 0x7f6d331fc9efp paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool) + 255 5 0x7f6d331fda40p paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool) + 128 6 0x7f6d3317863bp pybind11::cpp_function::initialize<pybind11::cpp_function::initialize<void, paddle::framework::Executor, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, pybind11::name, pybind11::is_method, pybind11::sibling>(void (paddle::framework::Executor::)(paddle::framework::ProgramDesc const&, paddle::framework::Scope, int, bool, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(paddle::framework::Executor*, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool)#1 (closed)}, void, paddle::framework::Executor*, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, pybind11::name, pybind11::is_method, pybind11::sibling>(pybind11::cpp_function::initialize<void, paddle::framework::Executor, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, pybind11::name, pybind11::is_method, pybind11::sibling>(void (paddle::framework::Executor::)(paddle::framework::ProgramDesc const&, paddle::framework::Scope, int, bool, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(paddle::framework::Executor*, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool)#1 (closed)}&&, void ()(paddle::framework::Executor, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(pybind11::detail::function_call&)#3 (closed)}::_FUN(pybind11::detail::function_call) + 555 7 0x7f6d33170b44p pybind11::cpp_function::dispatcher(_object*, _object*, _object*) + 2596 8 0x555e73c78d57p PyEval_EvalFrameEx + 29351 9 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 10 0x555e73c7724ep PyEval_EvalFrameEx + 22430 11 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 12 0x555e73c7724ep PyEval_EvalFrameEx + 22430 13 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 14 0x555e73c8b44cp 15 0x555e73c5b33ep PyObject_Call + 62 16 0x555e73c7477bp PyEval_EvalFrameEx + 11467 17 0x555e73c76d72p PyEval_EvalFrameEx + 21186 18 0x555e73c76d72p PyEval_EvalFrameEx + 21186 19 0x555e73c76d72p PyEval_EvalFrameEx + 21186 20 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 21 0x555e73c8b1a9p 22 0x555e73ca378ep 23 0x555e73c5b33ep PyObject_Call + 62 24 0x555e73cfed83p 25 0x555e73c76f60p PyEval_EvalFrameEx + 21680 26 0x555e73c8a888p 27 0x555e73c72591p PyEval_EvalFrameEx + 2785 28 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 29 0x555e73c777d3p PyEval_EvalFrameEx + 23843 30 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 31 0x555e73c777d3p PyEval_EvalFrameEx + 23843 32 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 33 0x555e73c777d3p PyEval_EvalFrameEx + 23843 34 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 35 0x555e73c8b1a9p 36 0x555e73ca378ep 37 0x555e73c76f60p PyEval_EvalFrameEx + 21680 38 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 39 0x555e73c777d3p PyEval_EvalFrameEx + 23843 40 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 41 0x555e73c8b1a9p 42 0x555e73ca378ep 43 0x555e73ca333ap 44 0x555e73c604abp 45 0x555e73c76f60p PyEval_EvalFrameEx + 21680 46 0x555e73c76d72p PyEval_EvalFrameEx + 21186 47 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 48 0x555e73c8b44cp 49 0x555e73c5b33ep PyObject_Call + 62 50 0x555e73c7477bp PyEval_EvalFrameEx + 11467 51 0x555e73c76d72p PyEval_EvalFrameEx + 21186 52 0x555e73c76d72p PyEval_EvalFrameEx + 21186 53 0x555e73c6f8cap PyEval_EvalCodeEx + 1754 54 0x555e73c8b1a9p 55 0x555e73ca378ep 56 0x555e73c5b33ep PyObject_Call + 62 57 0x555e73c7b360p PyEval_CallObjectWithKeywords + 48 58 0x555e73d0e652p 59 0x7f6da1a566dbp 60 0x7f6da1d8f88fp clone + 63