Python 使用在32x32字母图像上训练的模型预测整个文档ocr文本
因此,我使用从网站下载的字母数据集训练了一个用于OCR的tensorflow模型 创建Xtrain、Xtest和Ytrain、Ytest:文件夹包含每个字母表的文件夹,其中包含6个32x32的15k图像Python 使用在32x32字母图像上训练的模型预测整个文档ocr文本,python,tensorflow,keras,deep-learning,ocr,Python,Tensorflow,Keras,Deep Learning,Ocr,因此,我使用从网站下载的字母数据集训练了一个用于OCR的tensorflow模型 创建Xtrain、Xtest和Ytrain、Ytest:文件夹包含每个字母表的文件夹,其中包含6个32x32的15k图像 import os from PIL import Image from numpy import asarray folders = os.listdir(path) train_max = 100 test_max = 10 Xtrain = [] Ytrain = [] Xtest
import os
from PIL import Image
from numpy import asarray
folders = os.listdir(path)
train_max = 100
test_max = 10
Xtrain = []
Ytrain = []
Xtest = []
Ytest = []
for folder in folders:
folder_opened = path + folder + '/'
count = 0
for chars in os.listdir(folder_opened):
count += 1
if count <= train_max:
image = Image.open(folder_opened + chars)
data = asarray(image)
Xtrain.append(data)
Ytrain.append(folder)
elif count > train_max and count <= train_max + test_max:
image = Image.open(folder_opened + chars)
data = asarray(image)
Xtest.append(data)
Ytest.append(folder)
else:
break
该模型在预测包含32x32大小的单个字母表的图像时非常有效
但对于实际应用程序,我需要使用此模型从文档中提取整个文本(例如:PAN卡、身份证、护照等)
我所做的一切:
我试着用枕头读取图像,并将其转换成numpy数组,然后在上面使用model.predict
image_adhar = Image.open(path_2 + 'adhar1.jpeg')
image_adhar = asarray(image_adhar)
model.predict([image_adhar])
当这样做时,我得到了这个错误
WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor, but we receive a <class 'tuple'> input: (<tf.Tensor 'IteratorGetNext:0' shape=(None, 500, 3) dtype=uint8>,)
Consider rewriting this model with the Functional API.
WARNING:tensorflow:Model was constructed with shape (None, 32, 32) for input KerasTensor(type_spec=TensorSpec(shape=(None, 32, 32), dtype=tf.float32, name='flatten_30_input'), name='flatten_30_input', description="created by layer 'flatten_30_input'"), but it was called on an input with incompatible shape (None, 500, 3).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-165-bba8716b47d4> in <module>
----> 1 model.predict([image_adhar])
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1725 for step in data_handler.steps():
1726 callbacks.on_predict_batch_begin(step)
-> 1727 tmp_batch_outputs = self.predict_function(iterator)
1728 if data_handler.should_sync:
1729 context.async_wait()
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
887
888 with OptionalXlaContext(self._jit_compile):
--> 889 result = self._call(*args, **kwds)
890
891 new_tracing_count = self.experimental_get_tracing_count()
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
931 # This is the first call of __call__, so we have to initialize.
932 initializers = []
--> 933 self._initialize(args, kwds, add_initializers_to=initializers)
934 finally:
935 # At this point we know that the initialization is complete (or less
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _initialize(self, args, kwds, add_initializers_to)
761 self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph)
762 self._concrete_stateful_fn = (
--> 763 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
764 *args, **kwds))
765
~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
3048 args, kwargs = None, None
3049 with self._lock:
-> 3050 graph_function, _ = self._maybe_define_function(args, kwargs)
3051 return graph_function
3052
~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _maybe_define_function(self, args, kwargs)
3442
3443 self._function_cache.missed.add(call_context_key)
-> 3444 graph_function = self._create_graph_function(args, kwargs)
3445 self._function_cache.primary[cache_key] = graph_function
3446
~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3277 arg_names = base_arg_names + missing_arg_names
3278 graph_function = ConcreteFunction(
-> 3279 func_graph_module.func_graph_from_py_func(
3280 self._name,
3281 self._python_function,
~\anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
997 _, original_func = tf_decorator.unwrap(python_func)
998
--> 999 func_outputs = python_func(*func_args, **func_kwargs)
1000
1001 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in wrapped_fn(*args, **kwds)
670 # the function a weak reference to itself to avoid a reference cycle.
671 with OptionalXlaContext(compile_with_xla):
--> 672 out = weak_wrapped_fn().__wrapped__(*args, **kwds)
673 return out
674
~\anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py in wrapper(*args, **kwargs)
984 except Exception as e: # pylint:disable=broad-except
985 if hasattr(e, "ag_error_metadata"):
--> 986 raise e.ag_error_metadata.to_exception(e)
987 else:
988 raise
ValueError: in user code:
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:1569 predict_function *
return step_function(self, iterator)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:1559 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:1552 run_step **
outputs = model.predict_step(data)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:1525 predict_step
return self(x, training=False)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1030 __call__
outputs = call_fn(inputs, *args, **kwargs)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\sequential.py:380 call
return super(Sequential, self).call(inputs, training=training, mask=mask)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\functional.py:420 call
return self._run_internal_graph(
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\functional.py:556 _run_internal_graph
outputs = node.layer(*args, **kwargs)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1013 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\input_spec.py:251 assert_input_compatibility
raise ValueError(
ValueError: Input 0 of layer dense_94 is incompatible with the layer: expected axis -1 of input shape to have value 1024 but received input with shape (None, 1500)
警告:tensorflow:顺序模型中的层应该只有一个输入张量,但我们收到一个输入:(,)
考虑用函数API重写这个模型。
警告:tensorflow:为输入KerasTensor传感器(type_spec=TensorSpec(shape=(None,32,32),dtype=tf.float32,name='Flatte_30_input')、name='Flatte_30_input',description=(由层'Flatte_30_input'创建)构建了模型,但在形状不兼容的输入上调用了该模型(None,500,3)。
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
---->1.模型预测([image\u adhar])
预测中的~\anaconda3\lib\site packages\tensorflow\python\keras\engine\training.py(self、x、批大小、详细程度、步骤、回调、最大队列大小、工人、使用多处理)
1725对于数据处理程序中的步骤。步骤()
1726回调。开始预测批处理(步骤)
->1727 tmp_批处理_输出=自预测函数(迭代器)
1728如果数据处理程序应同步:
1729 context.async_wait()
~\anaconda3\lib\site packages\tensorflow\python\eager\def\u function.py在调用中(self,*args,**kwds)
887
888,带可选XLAContext(自动即时编译):
-->889结果=自调用(*args,**kwds)
890
891 new_tracing_count=self.experimental_get_tracing_count()
~\anaconda3\lib\site packages\tensorflow\python\eager\def_function.py in_调用(self,*args,**kwds)
931#这是uu call uuu的第一个调用,所以我们必须初始化。
932初始值设定项=[]
-->933自初始化(参数、KWD、添加初始化器到=初始化器)
934最后:
935#此时我们知道初始化已完成(或更少)
初始化中的~\anaconda3\lib\site packages\tensorflow\python\eager\def_function.py(self、args、kwds、add_initializers_to)
761 self.\u graph\u deleter=函数deleter(self.\u lifted\u initializer\u graph)
762自身的具体状态=(
-->763 self._stateful_fn._get_concrete_function_internal_garbage_collected(#pylint:disable=protected access
764*args,**科威特第纳尔)
765
~\anaconda3\lib\site packages\tensorflow\python\eager\function.py in\u get\u concrete\u function\u internal\u garbage\u collected(self,*args,**kwargs)
3048 args,kwargs=无,无
3049带自锁:
->3050图形函数,u=self._可能定义函数(args,kwargs)
3051返回图函数
3052
~\anaconda3\lib\site packages\tensorflow\python\eager\function.py in\u maybe\u define\u函数(self、args、kwargs)
3442
3443自.\u函数\u缓存.missed.add(调用上下文\u键)
->3444图形函数=self.\u创建图形函数(args,kwargs)
3445 self.\u function\u cache.primary[cache\u key]=图形函数
3446
~\anaconda3\lib\site packages\tensorflow\python\eager\function.py in\u create\u graph\u函数(self、args、kwargs、override\u flat\u arg\u shapes)
3277参数名称=基本参数名称+缺少的参数名称
3278图形函数=具体函数(
->3279 func_graph_module.func_graph_from_py_func(
3280自我名称,
3281 self.\u python\u函数,
~\anaconda3\lib\site packages\tensorflow\python\framework\func\u中的func\u graph.py from\u py\u func(名称、python\u func、args、kwargs、签名、func\u图、自动签名、自动签名、自动签名、自动签名选项、添加控制依赖项、参数名、op\u返回值、集合、按值捕获、覆盖平面\u arg\u形状)
997,original\u func=tf\u decorator.unwrap(python\u func)
998
-->999 func_outputs=python_func(*func_args,**func_kwargs)
1000
1001#不变量:`func_outputs`只包含张量、复合传感器、,
~\anaconda3\lib\site packages\tensorflow\python\eager\def_function.py in wrapped_fn(*args,**kwds)
670#函数对自身进行弱引用以避免引用循环。
671带可选xla文本(用xla编译):
-->672 out=弱包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹包裹
673返回
674
包装中的~\anaconda3\lib\site packages\tensorflow\python\framework\func\u graph.py(*args,**kwargs)
984例外情况为e:#pylint:disable=broad Exception
985如果hasattr(e,“ag错误元数据”):
-->986将e.ag\u错误\u元数据引发到\u异常(e)
987其他:
988提高
ValueError:在用户代码中:
C:\Users\faris\anaconda3\lib\site packages\tensorflow\python\keras\engine\training.py:1569 predict\u函数*
返回步骤_函数(self、迭代器)
C:\Users\faris\anaconda3\lib\site packages\tensorflow\python\keras\engine\training.py:1559步骤\函数**
输出=模型。分配策略。运行(运行步骤,参数=(数据,)
C:\Users\faris\anaconda3\lib\site packages\tensorflow\python\distribute\distribute\u lib.py:1285运行
返回self.\u扩展。为每个\u副本调用\u(fn,args=args,kwargs=kwargs)
C:\Users\faris\anaconda3\lib\site packages\tensorflow\python\distribute\distribute\u lib.py:2833为每个\u副本调用\u
返回自我。给每个回复者打电话
WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor, but we receive a <class 'tuple'> input: (<tf.Tensor 'IteratorGetNext:0' shape=(None, 500, 3) dtype=uint8>,)
Consider rewriting this model with the Functional API.
WARNING:tensorflow:Model was constructed with shape (None, 32, 32) for input KerasTensor(type_spec=TensorSpec(shape=(None, 32, 32), dtype=tf.float32, name='flatten_30_input'), name='flatten_30_input', description="created by layer 'flatten_30_input'"), but it was called on an input with incompatible shape (None, 500, 3).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-165-bba8716b47d4> in <module>
----> 1 model.predict([image_adhar])
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1725 for step in data_handler.steps():
1726 callbacks.on_predict_batch_begin(step)
-> 1727 tmp_batch_outputs = self.predict_function(iterator)
1728 if data_handler.should_sync:
1729 context.async_wait()
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
887
888 with OptionalXlaContext(self._jit_compile):
--> 889 result = self._call(*args, **kwds)
890
891 new_tracing_count = self.experimental_get_tracing_count()
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
931 # This is the first call of __call__, so we have to initialize.
932 initializers = []
--> 933 self._initialize(args, kwds, add_initializers_to=initializers)
934 finally:
935 # At this point we know that the initialization is complete (or less
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _initialize(self, args, kwds, add_initializers_to)
761 self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph)
762 self._concrete_stateful_fn = (
--> 763 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
764 *args, **kwds))
765
~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
3048 args, kwargs = None, None
3049 with self._lock:
-> 3050 graph_function, _ = self._maybe_define_function(args, kwargs)
3051 return graph_function
3052
~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _maybe_define_function(self, args, kwargs)
3442
3443 self._function_cache.missed.add(call_context_key)
-> 3444 graph_function = self._create_graph_function(args, kwargs)
3445 self._function_cache.primary[cache_key] = graph_function
3446
~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3277 arg_names = base_arg_names + missing_arg_names
3278 graph_function = ConcreteFunction(
-> 3279 func_graph_module.func_graph_from_py_func(
3280 self._name,
3281 self._python_function,
~\anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
997 _, original_func = tf_decorator.unwrap(python_func)
998
--> 999 func_outputs = python_func(*func_args, **func_kwargs)
1000
1001 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in wrapped_fn(*args, **kwds)
670 # the function a weak reference to itself to avoid a reference cycle.
671 with OptionalXlaContext(compile_with_xla):
--> 672 out = weak_wrapped_fn().__wrapped__(*args, **kwds)
673 return out
674
~\anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py in wrapper(*args, **kwargs)
984 except Exception as e: # pylint:disable=broad-except
985 if hasattr(e, "ag_error_metadata"):
--> 986 raise e.ag_error_metadata.to_exception(e)
987 else:
988 raise
ValueError: in user code:
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:1569 predict_function *
return step_function(self, iterator)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:1559 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:1552 run_step **
outputs = model.predict_step(data)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:1525 predict_step
return self(x, training=False)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1030 __call__
outputs = call_fn(inputs, *args, **kwargs)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\sequential.py:380 call
return super(Sequential, self).call(inputs, training=training, mask=mask)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\functional.py:420 call
return self._run_internal_graph(
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\functional.py:556 _run_internal_graph
outputs = node.layer(*args, **kwargs)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1013 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
C:\Users\faris\anaconda3\lib\site-packages\tensorflow\python\keras\engine\input_spec.py:251 assert_input_compatibility
raise ValueError(
ValueError: Input 0 of layer dense_94 is incompatible with the layer: expected axis -1 of input shape to have value 1024 but received input with shape (None, 1500)