发新帖

求助,如何实图片的现连续识别

[复制链接]
394 7

快来加入 TensorFlowers 大家庭!

您需要 登录 才可以下载或查看,没有帐号?加入社区

x
本帖最后由 Ritalin 于 2018-7-3 18:34 编辑

我做了一个文字识别的程序,输入一张图片(一张图片只包含一个汉字),输出识别结果。然后我打算连续输入几十到几百张图片,让程序进行识别,但是只能成功识别第一个图片,之后就开始报错了。求助
这是全部代码:
因某些原因  删掉代码(2018年5月21日15时25分)





这是运行结果:

PS C:\6> cd 'c:\6'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; & 'python' 'C:\Users\Michael\.vscode\extensions\ms-python.python-2018.4.0\pythonFiles\PythonTools\visualstudio_py_launcher.py' 'c:\6' '53500' '34806ad9-833a-4524-8cd6-18ca4aa74f14' 'RedirectOutput,RedirectOutput' 'c:\6\hhh.py'
WARNING:tensorflow:From C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py:198: retry (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Use the retry module or similar alternatives.
inference
2018-05-19 17:08:52.073874: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2018-05-19 17:08:53.197151: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1344] Found device 0 with properties:
name: GeForce GTX 1050 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.62
pciBusID: 0000:01:00.0
totalMemory: 4.00GiB freeMemory: 3.30GiB
2018-05-19 17:08:53.200433: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1423] Adding visible gpu devices: 0
2018-05-19 17:08:53.864254: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:911] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-05-19 17:08:53.868128: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:917]      0
2018-05-19 17:08:53.871216: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:930] 0:   N
2018-05-19 17:08:53.872871: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1041] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3030 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)





inference
2018-05-19 17:08:57.894860: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1423] Adding visible gpu devices: 0
2018-05-19 17:08:57.897791: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:911] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-05-19 17:08:57.900049: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:917]      0
2018-05-19 17:08:57.901537: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:930] 0:   N
2018-05-19 17:08:57.903425: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1041] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3030 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
Traceback (most recent call last):
  File "c:\6\hhh.py", line 115, in <module>
    main()
  File "c:\6\hhh.py", line 106, in main
    final_predict_val, final_predict_index = inference2('C:/OCR/program/result/0.jpg') #/
  File "c:\6\hhh.py", line 80, in inference2
    graph = build_graph(3)
  File "c:\6\hhh.py", line 31, in build_graph
    conv_1 = slim.conv2d(images, 64, [3, 3], 1, padding='SAME', scope='conv1')
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\framework\python\ops\arg_scope.py", line 183, in func_with_args
    return func(*args, **current_args)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\layers\python\layers\layers.py", line 1049, in convolution
    outputs = layer.apply(inputs)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\layers\base.py", line 825, in apply
    return self.__call__(inputs, *args, **kwargs)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\layers\base.py", line 696, in __call__
    self.build(input_shapes)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\layers\convolutional.py", line 144, in build
    dtype=self.dtype)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\layers\base.py", line 546, in add_variable
    partitioner=partitioner)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\checkpointable.py", line 415, in _add_variable_with_custom_getter
    **kwargs_for_getter)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1297, in get_variable
    constraint=constraint)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1093, in get_variable
    constraint=constraint)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 431, in get_variable
    return custom_getter(**custom_getter_kwargs)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\layers\python\layers\layers.py", line 1611, in layer_variable_getter
    return _model_variable_getter(getter, *args, **kwargs)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\layers\python\layers\layers.py", line 1602, in _model_variable_getter
    use_resource=use_resource)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\framework\python\ops\arg_scope.py", line 183, in func_with_args
    return func(*args, **current_args)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\framework\python\ops\variables.py", line 291, in model_variable
    use_resource=use_resource)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\framework\python\ops\arg_scope.py", line 183, in func_with_args
    return func(*args, **current_args)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\framework\python\ops\variables.py", line 246, in variable
    use_resource=use_resource)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 408, in _true_getter
    use_resource=use_resource, constraint=constraint)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 747, in _get_single_variable
    name, "".join(traceback.format_list(tb))))
ValueError: Variable conv1/weights already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1654, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3290, in create_op
    op_def=op_def)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)

PS C:\6>



可见,红色的部分我标注出来了,第一章图片成功识别了,但是到第二章就开始报错了。我认为,可能是程序的某个模块只要启动一次就行了,但是我重复的启动了,但是又不知道怎么改,哪里有错跪求各位大佬帮助


我知道答案 回答被采纳将会获得10 金币 + 9 金币 已有7人回答
本楼点评(0) 收起

精彩评论7

scholar_zry  TF荚荚  发表于 2018-5-20 00:38:16 | 显示全部楼层
你的程序结构有问题,第一次调用inference2 时创建了各个张量,然后创建了session。inference2退出时虽然session关闭了,但是各个tensor还是存在的。第二次调用时再次创建tensor,肯定失败了。

要么你的程序结构改一下。要不你在inference2中退出时,清空所有tensor。

本楼点评(0) 收起
yunhai_luo  TF豆豆  发表于 2018-5-20 00:39:35 | 显示全部楼层
你自己已经回答了一半了,至于另一半在错误信息里:
  1.   File "c:\6\hhh.py", line 80, in inference2
  2.     graph = build_graph(3)
  3.   File "c:\6\hhh.py", line 31, in build_graph
  4.     conv_1 = slim.conv2d(images, 64, [3, 3], 1, padding='SAME', scope='conv1')

  5.   ...

  6.   File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 747, in _get_single_variable
  7.     name, "".join(traceback.format_list(tb))))
  8. ValueError: Variable conv1/weights already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
复制代码


解释一下就是:在构建计算图的过程中发现conv1这一层的其中一个参数conv1/weights已经在计算图中存在了,而且你要构建的参数又必须是新参数(没有指明reuse),结果新老参数同名撞车。这是错误细节,从大面上讲,就是你说的“程序的某个模块只要启动一次就行了”,“某个模块”就是构建计算图的部分graph = build_graph(3)

比较粗暴的解决办法就是,你说我计算图上有东西,我就把计算图给你擦干净。具体做法就是在每次调用inference2前,做一次tf.reset_default_graph()。之所以说粗暴,其中一个原因是,既然用一个模型做两次及两次以上预测时,那就应该只构建一次模型,每次预测都构建模型是多余的。

相对好一点的做法是,构建一次模型,然后进行多次预测。根据你的例子,精简之后完整可独立运行的例子是:
  1. import tensorflow as tf
  2. from tensorflow.contrib import slim
  3. import numpy as np

  4. def build_graph(top_k):
  5.     images = tf.placeholder(dtype=tf.float32, shape=[None, 64, 64, 1], name='image_batch')
  6.     conv_1 = slim.conv2d(images, 64, [3, 3], 1, padding='SAME', scope='conv1')
  7.     flatten = slim.flatten(conv_1)
  8.     logits = slim.fully_connected(slim.dropout(flatten, 1.), 3, activation_fn=None, scope='fc2')
  9.     probabilities = tf.nn.softmax(logits)
  10.     predicted_val_top_k, predicted_index_top_k = tf.nn.top_k(probabilities, k=top_k)

  11.     return {'images': images,
  12.             'predicted_index_top_k': predicted_index_top_k,
  13.             'predicted_val_top_k': predicted_val_top_k}

  14. def inference2(graph, image):
  15.     print('inference')
  16.     with tf.Session(graph=tf.get_default_graph()) as sess:
  17.         sess.run(tf.global_variables_initializer())
  18.         predict_val, predict_index = sess.run([graph['predicted_val_top_k'], graph['predicted_index_top_k']],
  19.                                               feed_dict={graph['images']: image})
  20.         return predict_val, predict_index
  21. def main():
  22.     graph = build_graph(3)
  23.     final_predict_val, final_predict_index = inference2(graph, np.random.rand(1, 64, 64, 1))
  24.     a = final_predict_index.flat[0]
  25.     print('\n' + str(a))
  26.     final_predict_val, final_predict_index = inference2(graph, np.random.rand(1, 64, 64, 1))
  27.     a = final_predict_index.flat[0]
  28.     print('\n' + str(a))

  29. main()
复制代码


当然,更好的做法是调整一次参数。简单的情况下,这可能要求你的多次预测发生在同一个session。你的例子中输入调整在main,预测在inference2,要想做到可能需要大幅调整你的程序结构,或者用其他高级的做法。
本楼点评(3) 收起
  • Ritalin您说的太对了,真是很感动。谢谢大佬花时间为我解答,已采纳
    2018-5-20 00:48 回复
  • yunhai_luo回复 Ritalin :不客气,我也不是大佬,共同学习。你重复设置参数的问题我已经点评你的楼了。
    2018-5-20 00:51 回复
  • Ritalin回复 yunhai_luo :真的超级感动谢谢您。我会好好研究的
    2018-5-20 00:52 回复
Ritalin  TF荚荚  发表于 2018-5-20 00:40:55 | 显示全部楼层
scholar_zry 发表于 2018-5-20 00:38
你的程序结构有问题,第一次调用inference2 时创建了各个张量,然后创建了session。inference2退出时虽然se ...

我终于搞定了,刚刚发现inference这个函数会调用build_graph事实上 build_graph 只是建个东西
并没有必要每次都建
只需要建一次就行了  所以把那个语句拿到inference外面就行

本楼点评(0) 收起
scholar_zry  TF荚荚  发表于 2018-5-20 00:41:54 | 显示全部楼层
全是夜猫子。。。。。。。。。。。。。。。。
本楼点评(0) 收起
Ritalin  TF荚荚  发表于 2018-5-20 00:44:55 | 显示全部楼层
yunhai_luo 发表于 2018-5-20 00:39
你自己已经回答了一半了,至于另一半在错误信息里:

谢谢大佬

这是我改好的代码

已经可以正常连续识别了(尽管是自己完成的,修改以下代码并没有用到二位大佬的帮助,但是非常感动大佬们在半夜为我解答疑惑,我也会认真学习大佬的指点)

import tensorflow as tf
from tensorflow.contrib import slim
import os
import random
import time
import logging
import numpy as np
import pickle
from PIL import Image



tf.app.flags.DEFINE_integer('charset_size', 3755,
                            "Choose the first `charset_size` character to conduct our experiment.")
tf.app.flags.DEFINE_integer('image_size', 64, "Needs to provide same value as in training.")


tf.app.flags.DEFINE_string('checkpoint_dir', './checkpoint/', 'the checkpoint dir')


tf.app.flags.DEFINE_string('mode', 'train', 'Running mode. One of {"train", "valid", "test"}')
FLAGS = tf.app.flags.FLAGS


def build_graph(top_k):
    # with tf.device('/cpu:0'):
    keep_prob = tf.placeholder(dtype=tf.float32, shape=[], name='keep_prob')  # placeholder 形参,用于定义过程,在执行的时候再赋具体的值
    images = tf.placeholder(dtype=tf.float32, shape=[None, 64, 64, 1], name='image_batch') # batch批处理
    labels = tf.placeholder(dtype=tf.int64, shape=[None], name='label_batch')

    # 三个卷积层
    conv_1 = slim.conv2d(images, 64, [3, 3], 1, padding='SAME', scope='conv1') #  卷积 padding 填充 SAME代表卷积核可以停留图像边缘
    max_pool_1 = slim.max_pool2d(conv_1, [2, 2], [2, 2], padding='SAME')   # 减少代码量
    conv_2 = slim.conv2d(max_pool_1, 128, [3, 3], padding='SAME', scope='conv2')
    max_pool_2 = slim.max_pool2d(conv_2, [2, 2], [2, 2], padding='SAME')
    conv_3 = slim.conv2d(max_pool_2, 256, [3, 3], padding='SAME', scope='conv3')
    max_pool_3 = slim.max_pool2d(conv_3, [2, 2], [2, 2], padding='SAME')

    flatten = slim.flatten(max_pool_3)  # 输入扁平化  保留size
    fc1 = slim.fully_connected(slim.dropout(flatten, keep_prob), 1024, activation_fn=tf.nn.tanh, scope='fc1') # stack简化一个多卷积层塔
    logits = slim.fully_connected(slim.dropout(fc1, keep_prob), FLAGS.charset_size, activation_fn=None, scope='fc2')


        # 损失函数  训练一个tensorflow模型,需要一个网络模型,一个损失函数,梯度计算方式和用于迭代计算模型权重的训练过程
    # 模型训练的好坏
    loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels))  # reduce_mean求平均值
    accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits, 1), labels), tf.float32))

    global_step = tf.get_variable("step", [], initializer=tf.constant_initializer(0.0), trainable=False)  # 步数 trainable 卷积层的参数是否可被训练
    rate = tf.train.exponential_decay(2e-4, global_step, decay_steps=2000, decay_rate=0.97, staircase=True)  # 梯度下降?
    train_op = tf.train.AdamOptimizer(learning_rate=rate).minimize(loss, global_step=global_step)
    probabilities = tf.nn.softmax(logits)

    tf.summary.scalar('loss', loss)
    tf.summary.scalar('accuracy', accuracy)  #summary.scalar 标量相加
    merged_summary_op = tf.summary.merge_all()
    predicted_val_top_k, predicted_index_top_k = tf.nn.top_k(probabilities, k=top_k)
    accuracy_in_top_k = tf.reduce_mean(tf.cast(tf.nn.in_top_k(probabilities, labels, top_k), tf.float32))

    return {'images': images,  # 卷积的输入图像
            'labels': labels,
            'keep_prob': keep_prob,
            'top_k': top_k,
            'global_step': global_step,
            'train_op': train_op,
            'loss': loss,
            'accuracy': accuracy,
            'accuracy_top_k': accuracy_in_top_k,
            'merged_summary_op': merged_summary_op,
            'predicted_distribution': probabilities,
            'predicted_index_top_k': predicted_index_top_k,
            'predicted_val_top_k': predicted_val_top_k}

graph = build_graph(3)  # 建立图
ckpt = "C:\OCR\program\model\model.ckpt"

def inference2(image):
    temp_image = Image.open(image).convert('L')  # 图片变为灰色8bit
    temp_image = temp_image.resize((FLAGS.image_size, FLAGS.image_size), Image.ANTIALIAS)  # 图片缩放
    temp_image = np.asarray(temp_image) / 255.0   # 转化为矩阵
    temp_image = temp_image.reshape([-1, 64, 64, 1])   # 矩阵重新组织大小
    with tf.Session() as sess:
        # logger.info('========start inference============')
        # images = tf.placeholder(dtype=tf.float32, shape=[None, 64, 64, 1])
        # Pass a shadow label 0. This label will not affect the computation graph.

        saver = tf.train.Saver()


        saver.restore(sess, ckpt)
        predict_val, predict_index = sess.run([graph['predicted_val_top_k'], graph['predicted_index_top_k']],
                                              feed_dict={graph['images']: temp_image, graph['keep_prob']: 1.0})

    return predict_val, predict_index


def main():
    file = open('C:/OCR/program/zidian.txt')
    lines = file.readlines()
    aa = []
    rl = 'C:/OCR/program/result/{}.jpg'
    for line in lines:
        temp = line
        aa.append(temp)
    for i in range(744, 821):
        final_predict_val, final_predict_index = inference2(rl.format(i)) #/
        a = final_predict_index.flat[0]
        print(aa[a] ,end='')  # 文字
    # final_predict_val, final_predict_index = inference2('C:/OCR/program/result/4.jpg')  # /
    # a = final_predict_index.flat[0]
    # print('\n' + aa[a])

main()




本楼点评(2) 收起
  • yunhai_luo不错,解决了就好。多说一句,你现在的代码还是每次预测重复设置参数,这应该也是没有必要的,简单的修改就是把inference2并入main,不过具体实现要根据你的具体情况判断。
    2018-5-20 00:50 回复
  • Ritalin回复 yunhai_luo :好的
    2018-5-20 00:52 回复
neverchange  TF豆豆  发表于 2018-7-4 12:18:59 | 显示全部楼层
android(java)的从摄像头多次获取图片的思路是,回调finish就用当前的buffer里的数据,buffer用的时候就拷贝走。
这样做的好处是总会有数据进来,而且不会阻塞。
本楼点评(0) 收起
ViolinSolo  TF豆豆  发表于 2018-7-6 13:12:29 | 显示全部楼层
支持一下,emmm 就是你的语法高亮有点奇怪
本楼点评(0) 收起
您需要登录后才可以回帖 登录 | 加入社区

本版积分规则

主题

帖子

7

积分
快速回复 返回顶部 返回列表