Coder Social home page Coder Social logo

hed-tutorial-for-document-scanning's Introduction

hed-tutorial-for-document-scanning

Code for blog 手机端运行卷积神经网络实现文档检测功能(二) -- 从 VGG 到 MobileNetV2 知识梳理

get code

git clone https://github.com/fengjian0106/hed-tutorial-for-document-scanning

how to run

1 准备图片资源,合成训练样本

1.1 背景图片下载到 ./sample_images/background_images 目录。

1.2 前景图片下载到 ./sample_images/rect_images 目录。

2 使用 iPhone 模拟器合成训练样本

2.1 打开 ./generate_training_dataset/generate_training_dataset.xcodeproj 工程,先检查 ViewController.m 的 loadImagePaths 函数,确保 self.backgroundImagesPath 和 self.rectImagesPath 分别指向了 1.1 和 1.2 对应的目录,然后运行程序,并且根据打印的日志信息,在 Mac 上找到 self.imageSaveFolder 对应的目录,生成的样本图片就将保存在这个目录里。

2.2 将 2.1 里面生成的图片,全部移动到 ./dataset/generate_sample_by_ios_image_size_256_256_thickness_0.2 目录里。

2.3 在 UIView 上绘制的白色矩形边框,是有平滑处理的,白色的 Point 对应的 像素数值并不是 255,所以还需要对这些白色的 Point 进行二值化处理,运行如下程序:

python preprocess_generate_training_dataset.py \
			--dataset_root_dir dataset \
			--dataset_folder_name generate_sample_by_ios_image_size_256_256_thickness_0.2

这个程序执行完毕后,会得到 ./dataset/generate_sample_by_ios_image_size_256_256_thickness_0.2.csv 文件。

2.4 利用 gshuf 工具,随机打乱 ./dataset/generate_sample_by_ios_image_size_256_256_thickness_0.2.csv 文件的内容,执行如下命令:

gshuf ./dataset/generate_sample_by_ios_image_size_256_256_thickness_0.2.csv > ./dataset/temp.txt
gshuf ./dataset/temp.txt > ./dataset/generate_sample_by_ios_image_size_256_256_thickness_0.2.csv

执行到这一步,就得到了一批合成的训练样本图片。

准备训练样本的过程,应该根据具体的需求定制化开发,这里给的只是一种参考方式。比如还可以人工标注一批图片,也按照同样的格式组织到 csv 文件里。

3 训练网络

运行如下程序:

python train_hed.py --dataset_root_dir dataset \
                    --csv_path dataset/generate_sample_by_ios_image_size_256_256_thickness_0.2.csv \
                    --display_step 5

4 在 python 环境中测试 HED 网络

运行如下程序,处理一张图片:

python evaluate_hed.py --checkpoint_dir checkpoint \
                       --image test_image/test27.jpg \
                       --output_dir test_image

5 在 iPhone 真机环境,运行完整的流程,包括运行 HED 网络和执行基于 OpenCV 实现的找点算法

5.1 导出 pb 格式的模型文件,运行如下程序:

python freeze_model.py --checkpoint_dir checkpoint

成功运行后,可以在 ./checkpoint 目录里看到一个名为 hed_graph.pb 的模型文件,iOS 程序中会加载这个模型文件。

5.2 运行 iOS demo 程序

./ios_demo/DemoWithStaticLib/DemoWithStaticLib.xcodeproj 是一个 demo 程序,工程里面已经包含了编译好的各种依赖的静态库,可以直接运行。demo 里面有完整的流程,第一步是调用 HED 网络得到边缘检测图,第二步是执行找四边形顶点的算法。

5.3 编译 FMHEDNet 静态库

./ios_demo/FMHEDNet/FMHEDNet.xcodeproj 是一个静态库工程项目,里面封装了对 HED 网络的调用过程,这样可以避免在业务层 app 的工程文件中引入 TensorFlow 的源码文件。如果想编译这个 FMHEDNet 静态库,需要先编译 TensorFlow Mobile,关于如何编译 TensorFlow Mobile,请看后面的 5.4 。编译 FMHEDNet 的流程,请看这里

5.4 编译 TensorFlow Mobile TensorFlow 的官方文档有介绍编译的步骤。我使用的是手动裁剪过的版本,并且修改过 Protobuf 源码中的 namespace,具体步骤请看这里

hed-tutorial-for-document-scanning's People

Contributors

ajinkya933 avatar fengjian0106 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hed-tutorial-for-document-scanning's Issues

mat和数组的相互转化

求教java种Mat如何转为float数组的啊,float数组又是如何转mat的啊,
我试了下纯opencv检测文档时,视频卡的不要不要的你们怎么处理的

操作耗时统计

利用TensorFlow Lite进行部署,BN消耗的计算时间很少,反倒是transposed convolution占用了大部分的时间,32位和64位的耗时也差不太多,统计数据如下:
screenshot from 2018-11-05 19 43 59

Compile DemoWithStaticLib Failed

FrameworkAndLib/FMHEDNet/libFMHEDNet.a (3 slices)
Undefined symbols for architecture x86_64:
"OBJC_CLASS$_FMHEDNet", referenced from:
objc-class-ref in ViewController.o
"ProcessEdgeImage(cv::Mat, cv::Mat, bool)", referenced from:
-[ViewController processImage:] in ViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

重新训练模型大概需要多久时间?

因为线段比较粗的问题,所以想打算重新训练模型。借此也了解一下人工智能相关开发。
但是因为我之前从未在人工智能这块有涉猎,所以一开始只是基于现有的模型,做开发。然后部署了android版的。
所以我需要做个时间评估,从0开始搞这块需要多久时间?或者你从一开始搞这块花了多久时间。

图片背景预测结果是0.5的原因是什么

作者您好,我在实现边缘检测网络时遇到了背景色全是灰色的情况,我查了一下,背景像素的预测结果全是0.5,请问造成这种情况的原因可能是什么呢?

图片尺寸问题

@fengjian0106 您好,我制作了你的那个数据集 3个多G,只不过中途运行退出了。不知道是不是这个大小。也成功运行了网络。但现在有一个小疑问,原始HED caffe代码,训练数据好像可以是任意尺寸,我想拿BSDS 数据来训练,就运行出错了。所以我想请教一下,如果要改进代码使得适应任意尺寸的数据?我想到最简单的方法就是吧数据全部缩放256 256 ,或者是我在tf中的指定一个尺寸 ,然后将输出结果缩放到原始尺寸,但应该不是这样做的吧?因为无论我怎么该const.height 与 width ,只能适应一个尺寸。

模型部署

image
基于以上图片,有个疑问了。
ios_demo 里的hed_graph.pb是基于mobilenetv2 style hed训练的?
android要使用性能好的,是不是要改成vgg_style_hed的代码重新训练生成一个用于android的?

您好,请问如何定量评价边缘检测结果

您好,在进行边缘检测的时候,数据量过大的时候不能单凭借loss是否收敛来判断网络训练好吧,或者肉眼观察网络检测结果,请问,您在对模型进行训练的时候是如何定量评估网络训练结果的呢?

感谢楼主!

代码还没有跑,但是看起来非常棒,思路很清晰,特此感谢~

样本制作思路

我有个制作样本的想法,把nyud_cropped做语义分割的样本,通过对训练样本做canny识别,成这样
image
image

这样就有几万样本,来训练,不知道是否可行,因为原先是整体类别作为一类,所以有的边缘canny检测不出来,这样做样本 是否能对模型有提升,楼主觉得可行吗

FIFOQueue '_2_batch/fifo_queue' is closed and has insufficient elements (requested 1, current size 0)

hi, when i run your code with my own dataset, it's crashed with below error:

OutOfRangeError (see above for traceback): FIFOQueue '_2_batch/fifo_queue' is closed and has insufficient elements (requested 1, current size 0)
[[Node: batch = QueueDequeueManyV2[component_types=[DT_FLOAT, DT_FLOAT, DT_STRING], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](batch/fifo_queue, batch/n)]]

my dataset total 1996 color img and 1996 gray_thresh img, and i set batch=1, it's always crash at same point--at the 1994th img. I accept it's certainly almost run out the hole dataset,but why can't it reture from the 1th data to retrain again? Besize, i set my iterations=2000, but it's always crash at the 620th iterations and notice me no data. I want to know can it reuse my dataset when there is no sufficient data? Please help , Thanks!

Base code - C/C++

Hi :)
I would like to use this code to return the coordinates of the four corners; how can I do that?
With the Python instructions given in the README, I only get the resulting image, not the corners :s

线段延展成参考线

线段延展成参考线函数GetRefLine好像有点问题,在OpenCV坐标系中y轴方向是向下的,最后一个else分支这逻辑会导致一些参考线段在图像外部?
另外,判断参考线是否相近的函数IsTwoRefLineCloseToEachOther好像也有问题,例如下图所示,这情况函数的返回应该是true,但实际却不相近,不知道是不是哪里理解错了?
p81114-174123

build android error

| #define PACKAGE_NAME "Protocol Buffers"
| #define PACKAGE_TARNAME "protobuf"
| #define PACKAGE_VERSION "3.5.0"
| #define PACKAGE_STRING "Protocol Buffers 3.5.0"
| #define PACKAGE_BUGREPORT "[email protected]"
| #define PACKAGE_URL ""
| #define PACKAGE "protobuf"
| #define VERSION "3.5.0"
| /* end confdefs.h. /
| #ifdef STDC
| # include <limits.h>
| #else
| # include <assert.h>
| #endif
| Syntax error
configure:5027: result: /lib/cpp
configure:5047: /lib/cpp conftest.c
./configure: line 1689: /lib/cpp: No such file or directory
configure:5047: $? = 127
configure: failed program was:
| /
confdefs.h /
| #define PACKAGE_NAME "Protocol Buffers"
| #define PACKAGE_TARNAME "protobuf"
| #define PACKAGE_VERSION "3.5.0"
| #define PACKAGE_STRING "Protocol Buffers 3.5.0"
| #define PACKAGE_BUGREPORT "[email protected]"
| #define PACKAGE_URL ""
| #define PACKAGE "protobuf"
| #define VERSION "3.5.0"
| /
end confdefs.h. /
| #ifdef STDC
| # include <limits.h>
| #else
| # include <assert.h>
| #endif
| Syntax error
configure:5047: /lib/cpp conftest.c
./configure: line 1689: /lib/cpp: No such file or directory
configure:5047: $? = 127
configure: failed program was:
| /
confdefs.h /
| #define PACKAGE_NAME "Protocol Buffers"
| #define PACKAGE_TARNAME "protobuf"
| #define PACKAGE_VERSION "3.5.0"
| #define PACKAGE_STRING "Protocol Buffers 3.5.0"
| #define PACKAGE_BUGREPORT "[email protected]"
| #define PACKAGE_URL ""
| #define PACKAGE "protobuf"
| #define VERSION "3.5.0"
| /
end confdefs.h. */
| #ifdef STDC
| # include <limits.h>
| #else
| # include <assert.h>
| #endif
| Syntax error
configure:5077: error: in /Users/luojie/WorkSpaceOfSaiDeSheng/WorkSpaceOfOpenCV/tensorflow-1.7.0-change-protobuf-namespace/tensorflow/contrib/makefile/downloads/protobuf': configure:5079: error: C preprocessor "/lib/cpp" fails sanity check See config.log' for more details

轮廓线条太粗

debug1
simulator screen shot - iphone 6s - 2018-08-01 at 19 50 45
simulator screen shot - iphone 6s - 2018-08-01 at 19 51 05
debug4
android 和 ios 都测过很多图片都是左边和顶部都会有一些间隙,但右边和底部匹配的很好。也看过你的算法也没有太大的问题。
我的猜测还是觉得识别出来的线条太粗了造成的。虽然在debug_img没有间隙,绘制的线条本身也很粗。
但在裁剪的时候是以像素为单位的。
轮廓识别还能有优化的空间,变得更细?

关于标注工具

你好,想请问下文章中所说的标注工具,是否有开放出来,感谢!

合成数据

2.2 将 2.1 里面生成的图片,全部移动到 ./dataset/generate_sample_by_ios_image_size_256_256_thickness_0.2 目录里。
我不懂object_c,目前在学习训练模型,合成的图片能不能发给我邮箱一下[email protected]
或者能不能把合成图片的原理说一下,为什么要用Iphone模拟器合成

android issues

i had been ported to android, but precise is less than ios.

the android underlying code is the same as ios since i using c++.

why?

tensorflow 1.7 android ios both

opencv ios the same as yours

android use the latest version of opencv, but code is the same as ios, since core code is same(c++)

android 端实时监测效果不佳

单张识别还可以,做成拍照实时预览识别,卡顿明显,尤其在比较差的机子上。
请问你在android是怎么处理的?

咨询几个问题

  1. Android上的性能数据是基于哪个手机?
  2. 改成vgg_style_hed, 正确率有没有变化? 这个网络的模型尺寸是多大?
  3. 你博文中提到模型尺寸从4.4M降到1.6M,是什么原因?
  4. Android中QQ邮箱的“扫描文件”功能,跟这个算法是一样的么?

vgg_hed_style ios测试输出全黑

74b74b502915fa797bcbdfa3317b562d
这个是通过evaluate_hed.py 输出的结果
hedoutputimage
这个是手机上输出的结果
image
代码只改了这一处,因为改为vgg_hed_style 去掉bash_norm后,传is_training 出错。
错误日志:tf_session->Run error: FeedInputs: unable to find feed output is_training:0
所以去掉了is_training.

项目License

作者你好,请问可以给这个项目添加一个License吗?因为感觉Github上默认copyright还挺严格的。
license

ios opencv 用最新的sdk报错

tensorflow::Status load_graph_status = ReadBinaryProto(tensorflow::Env::Default(), pbPath, &graphDef);
在这行bad_access_error,博主有遇到过?

模型转换的问题

利用tf.contrib.lite.TocoConverter.from_frozen_graph将提供的hed_graph.pb模型转换成.tflite,一直不成功,看提示应该是与Batch normalization相关。
另外,在freeze_model.py文件中is_training_placeholder是不是可以省略,dsn_fuse, dsn1, dsn2, dsn3, dsn4, dsn5 = mobilenet_v2_style_hed(image_float, batch_size, False),这里直接给False就行?

关于输出

作者你好:请问下,在hed中 最终的 fused 输出不需要进行sigmod 压缩像素点到0-1之间么? 你的loss来看 是使用了 tf.nn.weighted_cross_entropy_with_logits 里面是对 logis 进行sigmod 压缩的 这点不是太理解..

关于训练图片

作者你好, 我对这个工作很敢兴趣, 想自己训练一个网络, 请问下, 你贡献的那个模型, 在训练的时候 我看文章报道是合成了大概8万张 人为图片, 那请问下 实际在训练的时候, 就只用到这8万张合成图片?还是包含比如其他通用边缘检测例如BSDS500 这样的数据集

IsSegmentsHasSameSegment过滤条件

原代码是:
if (diff < kSameSegmentsMaxAngle) { return true; }
这样角度0和178这样相似的线会被过滤掉,所以不应该是下面的过滤条件吗?
if (diff < kSameSegmentsMaxAngle || diff > 85) { return true; }

关于图片合成

你好,打扰致歉。
在学习过程中遇到一些问题,希望能指点一二。
1.为什么要在iPhone模拟器上进行图片合成?
2.如果是安卓真机要怎么合成图片,依然可以在iPhone模拟器上进行合成吗?
不胜感激。
@fengjian0106

filters 数目为

image
为什么filters 数目设置规律是翻倍的,3、6、12、24、48。在mobilenetv2论文里没有看到有关filter num问题

另外在这个图上
image
对比你的代码,好像只实现到这一层。
image

Suggestion

The order of "logits" and "targets" in sentences "cost = tf.nn.weighted_cross_entropy_with_logits(logits=logits, targets=label, pos_weight=pos_weight)" may be wrong in python 3.6.

自定义训练图片

楼主能帮我理解一下样本标签吗,HED网络检测边缘线,理论上来说是对边缘位置处的像素做像素级的分类。
特定场景识别矩形,自己标注图片,是不是只标注待识别的矩形框,比如
image
是不是只要标注成类似这样?
image
其他的边缘都不需要,还是要把其他的边缘也标注出来?
把想要的边缘作为标签输入,去训练。自己合成图片是不是也是这样的。原图的边缘,记录位置,经过变化以后,计算出对应的边缘位置,作为训练标签。

CSV format for annotations

Hi!
First, awesome work you did :)
I had already developed a way to generate data, and I would like to use your training algorithm, but I need to know what kind of format you use (like image_path, top-left corner, top-right corner, ...).
I currently use Pascal VOC format (for Yolo), is the needed csv format different?

关于样本合成

请问样本合成的时候,样本有了,但是样本对应的标签,也就是边缘是怎么得到的呢?

源码阅读

一直想找时间阅读下您的代码,终于最近有空。发现看起代码好累,尤其在卷积操作这块,完全搞不懂为什么这么写,而且网上也试着搜寻了一些资料,想搞明白你是根据怎样的思路来构建此模型训练的代码。为此我查阅了相关知识点,例如cost function 、weight 、bias 、NN 、CNN 、convergence、 overfitting、MobileNet是基于CNN在移动设备上的另一种实现等知识点、还看过hed的文献。
最后还是云里雾里的,我的初衷是想通过此项目入门AI,但是看起代码来进展很慢。

关于运行环境

1、TensorFlow版本是多少
2、OpenCV需要吗
你们训练、测试环境都没讲清楚啊

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.