網頁

2019年1月10日 星期四

Tesseract 訓練, 使用 jTessBoxEditor

這是舊版的訓練方式,Tesseract 4.0 開始了新的 LSTM(AI) 的方式

D:\>java -jar jTessBoxEditor.jar
TIFF/Box Generator
text2image 不要勾
Input 選擇 .txt 文字檔
Output 按 ... 設定輸出位置
英文輸入 eng, 中文輸入 chi_tra
選擇字型
按 Generate 產生 .box 和 .font_properties

tif文面命名格式[lang].[fontname].exp[num].tif
lang:plt
fontname:normal
num:0

手動由 .tif -> .box
D:\>ttesseract.exe plt.normal.exp0.tif plt.normal.exp0 --psm 7 -l eng batch.nochop makebox

手動生成 .font_properties, 內容為 <fontname> <italic> <bold> <fixed> <serif> <fraktur>
echo normal 0 1 1 0 1 >plt.font_properties

由 .box 文件 -> .tr
D:\>tesseract.exe plt.normal.exp0.tif plt.normal.exp0 box.train.stderr
由 .box 文件 -> unicharset
D:\>unicharset_extractor.exe plt.normal.exp0.box
由 font_properties, unicharset, .tr -> shapetable
D:\>shapeclustering.exe -F plt.font_properties -U unicharset plt.normal.exp0.tr
由 font_properties, unicharset, .tr ->lang.unicharset, inttemp, pffmtable
D:\>mftraining.exe -F plt.font_properties -U unicharset -O plt.unicharset plt.normal.exp0.tr
由.tr 文件 -> normproto
D:\>cntraining.exe plt.normal.exp0.tr
重命名 以 lang. 開頭重命名 inttemp, normproto, pffmtable, shapetable
D:\>move inttemp plt.inttemp
D:\>move normproto plt.normproto
D:\>move pffmtable plt.pffmtable
D:\>move shapetable plt.shapetable
合併生成 lang.traineddata文件,在這裏爲 plt.traineddata
D:\>combine_tessdata.exe plt.

2018年12月7日 星期五

EAST Tesseract 效能測試

EAST: An Efficient and Accurate Scene Text Detector

參考 OpenCV OCR and text recognition with Tesseract

發現 opencv 使用硬體加速是
net = cv2.dnn.readNet(args["east"])
net.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL);
而 OPENCL 並不是 NVDIA 的 CUDA 是 Intel(GPU)

使用了 tensorflow 的 gpu(CUDA)
效能的卻比較好
640x480 從 400ms 到 340ms

但 Tsseract 沒有加速,只能加速 EAST

build tensorflow 1.10

參考 build tensorflow 1.11 from source in visual studio

(base) D:\TensorFlowB>conda env list
(base) D:\TensorFlowB>conda env remove -n tensorflow-1.10
(base) D:\TensorFlowB>conda create -n tensorflow-1.10 pip python=3.6
(base) D:\TensorFlowB>activate tensorflow-1.10
(tensorflow-1.10) D:\TensorFlowB>pip install six numpy wheel protobuf absl-py
(tensorflow-1.10) D:\TensorFlowB>pip install keras_applications==1.0.5 --no-deps
(tensorflow-1.10) D:\TensorFlowB>pip install keras_preprocessing==1.0.3 --no-deps

D:\TensorFlowB>git clone https://github.com/tensorflow/tensorflow.git tensorflow-1.10
D:\TensorFlowB>cd tensorflow-1.10
D:\TensorFlowB\tensorflow-1.10>git checkout r1.10
D:\TensorFlowB\tensorflow-1.10>git pull origin master
D:\TensorFlowB\tensorflow-1.10>bazel clean
D:\TensorFlowB\tensorflow-1.10>python ./configure.py

修改 D:/TensorFlowB/tensorflow-1.10/tensorflow/contrib/cmake/CMakeLists.txt

if (tensorflow_OPTIMIZE_FOR_NATIVE_ARCH)
  include(CheckCXXCompilerFlag)
  CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_OPT_ARCH_NATIVE_SUPPORTED)
  if (COMPILER_OPT_ARCH_NATIVE_SUPPORTED)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
  else()
    CHECK_CXX_COMPILER_FLAG("/arch:AVX2" COMPILER_OPT_ARCH_AVX_SUPPORTED)
    if(COMPILER_OPT_ARCH_AVX_SUPPORTED)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2")
      add_definitions(-D__AVX2__)
    endif()
  endif()
endif()

D:/TensorFlowB/tensorflow-1.10/tensorflow/contrib/cmake
選 Visual Studio 14 2015 Win64
Optional toolset to use, 輸入 "host=x64"
tensorflow_BUILD_SHARED_LIB v
tensorflow_ENABLE_GPU v

問題與解答
D:\TensorFlowB\tensorflow-1.10\tensorflow\stream_executor\dnn.pb.h
This file was generated by a newer version of protoc
which is incompatible with your Protocol Buffer headers.
Please update your headers.

D:\TensorFlowB\tensorflow-1.10\tensorflow\contrib\cmake\external\protobuf.cmake
set(PROTOBUF_TAG v3.6.0)
set(PROTOBUF_TAG v3.6.1)

D:\TensorFlowB\tensorflow-1.10\tensorflow\workspace.bzl

  tf_http_archive(
      name = "protobuf_archive",
      urls = [
          "https://mirror.bazel.build/github.com/google/protobuf/archive/v3.6.1.tar.gz",
          "https://github.com/google/protobuf/archive/v3.6.1.tar.gz",
      ],
      sha256 = "3d4e589d81b2006ca603c1ab712c9715a76227293032d05b26fca603f90b3f5b",
      strip_prefix = "protobuf-3.6.1",
  )
  tf_http_archive(
      name = "eigen_archive",
      urls = [
          "https://mirror.bazel.build/bitbucket.org/eigen/eigen/get/fd6845384b86.tar.gz",
          "https://bitbucket.org/eigen/eigen/get/fd6845384b86.tar.gz",
      ],
      sha256 = "d956415d784fa4e42b6a2a45c32556d6aec9d0a3d8ef48baee2522ab762556a9",
      strip_prefix = "eigen-eigen-fd6845384b86",
      build_file = clean_dep("//third_party:eigen.BUILD"),
      patch_file = clean_dep("//third_party:eigen_half.patch"),
  )

download eigen_half.patch to D:\TensorFlowB\tensorflow-1.10\third_party\eigen_half.patch

build project tf_python_build_pip_package 產生
EXEC : error : [WinError 5] 存取被拒。:
'build\\bdist.win-amd64\\wheel\\tensorflow_gpu-1.10.1.data\\purelib\\tensorflow\\include\\tensorflow\\stream_executor\\dnn.pb.h'
改變 dnn.pb.h 檔案屬性
使用 Administrator 開啟 vs2015 x64 Native Tools Command Prompt
D:\TensorFlowB\build-1.10>D:\Anaconda3\envs\tensorflow-1.10\python.exe D:/TensorFlowB/build-1.10/tf_python/setup.py bdist_wheel --project_name tensorflow_gpu
另外開啟 Anaconda
(base) D:\TensorFlowB\build-1.10\tf_python>activate tensorflow-1.10
(tensorflow-1.10) D:\TensorFlowB\build-1.10\tf_python>pip install dist\tensorflow_gpu-1.10.1-cp36-cp36m-win_amd64.whl

bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
失敗,沒有到解決方法
c:\users\mark\appdata\local\temp\nvcc_inter_files_tmp_dir\depthwise_conv_op_gpu.cu.compute_70.cudafe1.stub.c(3):
fatal error C1083: Cannot open include file: 'depthwise_conv_op_gpu.cu.fatbin.c': No such file or directory

2018年11月28日 星期三

build tensorflow 1.11 from source in visual studio

先說重點
目前 windows 下 GPU 的版本來到 tensorflow_gpu-1.12.0 使用 Bazel
但是發現目前的 Bazel 產生的 library 不能在 Visual Studio 中使用
退到 tensorflow_gpu-1.11 使用 Cmake 建立 library

另外只能建立 Release 版本,並使用 RelWithDebInfo 版本,取代 Debug 版本
但只有 Release 能成功

library 建立起來後,程式可以編譯,可以執行,但結果是錯的

開啟 Anaconda Prompt
(base) D:\>conda create -n tensorflow-1.11 pip python=3.6
(base) D:\>activate tensorflow-1.11
(tensorflow-1.11) D:\>pip install six numpy wheel
(tensorflow-1.11) D:\>pip install keras_applications==1.0.5 --no-deps
(tensorflow-1.11) D:\>pip install keras_preprocessing==1.0.3 --no-deps

http://www.msys2.org/
下載 msys2-x86_64-20180531.exe
開啟 msys2/MinGW 64-bit
$ pacman -Syu
$ pacman -Su
$ pacman -S git patch unzip

安裝 Bazel
https://github.com/bazelbuild/bazel/releases
下載 bazel-0.18.1-windows-x86_64.exe
rename bazel-0.18.1-windows-x86_64.exe bazel.exe
move bazel.exe D:\msys64\usr\bin
add PATH D:\msys64\usr\bin

安裝 JDK 8
下載 jdk-8u191-windows-x64.exe
add JAVA_HOME C:\Program Files\Java\jdk1.8.0_191

copy cudnn-9.0-windows10-x64-v7\cuda\* to
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0

下載 swigwin-3.0.12.zip
解壓縮於 D:\TensorFlowB\swigwin-3.0.12

開啟 VS3215 x64 Native Tools Command Prompt
D:\TensorFlowB>git clone https://github.com/tensorflow/tensorflow.git tensorflow-1.11
D:\TensorFlowB>cd tensorflow-1.11
D:\TensorFlowB\tensorflow-1.11>git checkout r1.11
D:\TensorFlowB\tensorflow-1.11>python ./configure.py
Please specify the location of python. [Default is D:\Anaconda3\python.exe]:
Please input the desired Python library path to use.  Default is [D:\Anaconda3\lib\site-packages]
Do you wish to build TensorFlow with nGraph support? [y/N]:
Do you wish to build TensorFlow with CUDA support? [y/N]: y
Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]:
Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0]:
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]:
Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0]:
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 3.5,7.0]:
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]: /arch:AVX2
Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]:

修改 D:/TensorFlowB/tensorflow-1.11/tensorflow/contrib/cmake/CMakeLists.txt 增加 AVX2 功能

if (tensorflow_OPTIMIZE_FOR_NATIVE_ARCH)
  include(CheckCXXCompilerFlag)
  CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_OPT_ARCH_NATIVE_SUPPORTED)
  if (COMPILER_OPT_ARCH_NATIVE_SUPPORTED)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
  else()
    CHECK_CXX_COMPILER_FLAG("/arch:AVX2" COMPILER_OPT_ARCH_AVX_SUPPORTED)
    if(COMPILER_OPT_ARCH_AVX_SUPPORTED)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2")
      add_definitions(-D__AVX2__)
    endif()
  endif()
endif()

參考 Add abseil_cpp cmake dependence. 修改
D:/TensorFlowB/tensorflow-1.11/tensorflow/contrib/cmake/CMakeLists.txt
增加 tensorflow/contrib/cmake/external/abseil_cpp.cmake
增加 tensorflow/contrib/cmake/modules/FindAbseilCpp.cmake
以免出現找不到 absl/strings/string_view.h 錯誤

    add_definitions(-DGOOGLE_CUDA=1 -DTF_EXTRA_CUDA_CAPABILITIES=3.5,3.7,5.2,6.0,6.1,7.0)


修改 D:\TensorFlowB\tensorflow-1.11\tensorflow\contrib\cmake\external\eigen.cmake
option(eigen_PATCH_FILE "Patch file to apply to eigen" OFF)
set(eigen_PATCH_FILE "D:/TensorFlowB/eigen_half.patch")
修改 D:\TensorFlowB\tensorflow-1.11\bazel-tensorflow\tensorflow\workspace.bzl

  tf_http_archive(
      name = "eigen_archive",
      build_file = clean_dep("//third_party:eigen.BUILD"),
      patch_file = clean_dep("//third_party:eigen_half.patch"),
  )

下載 https://github.com/amsokol/tensorflow-windows-build-tutorial/blob/master/eigen_half.patch
置於 D:/TensorFlowB/eigen_half.patch
之後會修改
D:\TensorFlowB\build-1.11\eigen\src\eigen\Eigen\src\Core\arch\CUDA\Half.h
D:\TensorFlowB\build-1.11\external\eigen_archive\Eigen\src\Core\arch\CUDA\Half.h
避免下列錯誤

Error  more than one instance of overloaded function "__hadd" matches the argument list: tf_core_gpu_kernels d:\tensorflowb\build-1.11\external\eigen_archive\eigen\src\Core\arch\CUDA\Half.h 212 

CMake
source: D:/TensorFlowB/tensorflow-1.11/tensorflow/contrib/cmake
build: D:/TensorFlowB/build-1.11
Configure
Visual Studio 14 2015 Win64
Optional toolset: host=x64
SWIG_EXECUTABLE=D:/TensorFlowB/swigwin-3.0.12/swig.exe
tensorflow_BUILD_SHARED_LIB=v
tensorflow_ENABLE_GPU=v
eigen_PATCH_FILE=v

cmake 使用 message() debug

以 Administrator 開啟 Visual Studio 2015
開啟 D:\TensorFlowB\build-1.11\tensorflow.sln
換成 Release 版本
開啟下列專案的屬性設定
_beam_search_ops, _gru_ops, _lstm_ops, _nearest_neighbor_ops, _periodic_resample_op
Property Pages/Configuration Properties/Linker/Input/Additional Dependencies
\pywrap_tensorflow_internal.lib 改為 Release\pywrap_tensorflow_internal.lib

出現 cuda_kernel_helper.h 找不到 cuda_fp16.h

Severity Code Description Project File Line Suppression State
Error C1083 Cannot open include file: 'cuda/include/cuda_fp16.h': No such file or directory _beam_search_ops D:\TensorFlowB\tensorflow-1.11\tensorflow\core\util\cuda_kernel_helper.h 24 

開啟 D:\TensorFlowB\tensorflow-1.11\tensorflow\core\util\cuda_kernel_helper.h
//#include "cuda/include/cuda_fp16.h"
#include "cuda_fp16.h"

出現錯誤

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "class absl::uint128 __cdecl absl::operator%(class absl::uint128,class absl::uint128)" (??Labsl@@YA?AVuint128@0@V10@0@Z) referenced in function "private: void __cdecl absl::str_format_internal::`anonymous namespace'::ConvertedIntInfo::UnsignedToStringRight(class absl::uint128,struct absl::str_format_internal::ConversionChar)" (??$UnsignedToStringRight@Vuint128@absl@@@ConvertedIntInfo@?A0x0d227ec7@str_format_internal@absl@@AEAAXVuint128@3@UConversionChar@23@@Z) tf_tutorials_example_trainer D:\TensorFlowB\build-1.11\arg.obj 1 

Linker/Input/Additional Dependencies 加入
abseil_cpp\src\abseil_cpp_build\absl\numeric\Release\absl_int128.lib

最後是 estimator_python_api 和 tf_python_api 失敗
需要開啟 VS2015 的 Tools/Options/Projects and Solutions/Build and Run
MSBuild project build output verbosity: Normal 才能看到訊息

修改 D:\TensorFlowB\build-1.11\tf_python_api.vcxproj

from "C:\Program Files\CMake\bin\cmake.exe" -E env PYTHONPATH=D:/TensorFlowB/build-1.11/tf_python "" D:/Anaconda3/python.exe D:/TensorFlowB/build-1.11/tf_python/tensorflow/python/tools/api/generator/create_python_api.py --root_init_template=D:/TensorFlowB/build-1.11/tf_python/tensorflow/api_template.__init__.py --apidir=D:/TensorFlowB/build-1.11/tf_python/tensorflow --package=tensorflow.python --apiname=tensorflow D:/TensorFlowB/tensorflow-1.11/api_init_files_list.txt
to "C:\Program Files\CMake\bin\cmake.exe" -E env PYTHONPATH=D:/TensorFlowB/build-1.11/tf_python D:/Anaconda3/python.exe D:/TensorFlowB/build-1.11/tf_python/tensorflow/python/tools/api/generator/create_python_api.py --root_init_template=D:/TensorFlowB/build-1.11/tf_python/tensorflow/api_template.__init__.py --apidir=D:/TensorFlowB/build-1.11/tf_python/tensorflow --package=tensorflow.python --apiname=tensorflow D:/TensorFlowB/tensorflow-1.11/api_init_files_list.txt

copy D:\TensorFlowB\tensorflow-1.11\tensorflow\tools\docs
to D:\TensorFlowB\build-1.11\tf_python\tensorflow\tools\docs
copy D:\TensorFlowB\tensorflow-1.11\tensorflow\python\distribute
to D:\TensorFlowB\build-1.11\tf_python\tensorflow\python\distribute

build tf_python_build_pip_package
產生 D:\TensorFlowB\build-1.11\tf_python\dist\tensorflow_gpu-1.11.0-cp36-cp36m-win_amd64.whl
(tensorflow-1.11) D:\TensorFlowB\build-1.11>pip install tf_python\dist\tensorflow_gpu-1.11.0-cp36-cp36m-win_amd64.whl


使用 bazel
D:\TensorFlowB\tensorflow-1.11>bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
執行很久後可以發現
D:\TensorFlowB\tensorflow-1.11\bazel-out\x64_windows-opt\bin\tensorflow\tools\pip_package\simple_console_for_windows.zip
產生失敗,size=0

D:\TensorFlowB\tensorflow-1.11>cd bazel-out/x64_windows-opt/bin/tensorflow/tools/pip_package
edit simple_console_for_windows.zip-0.params
刪除有 .zip 的每一行

執行
D:\TensorFlowB\tensorflow-1.11\bazel-tensorflow>external\bazel_tools\tools\zip\zipper\zipper.exe vcC bazel-out/x64_windows-opt/bin/tensorflow/tools/pip_package/simple_console_for_windows.zip @bazel-out/x64_windows-opt/bin/tensorflow/tools/pip_package/simple_console_for_windows.zip-0.params
D:\TensorFlowB\tensorflow-1.11\bazel-tensorflow>cd ..
D:\TensorFlowB\tensorflow-1.11>bazel-bin\tensorflow\tools\pip_package\build_pip_package ..\tensorflow_pkg
安裝
(tensorflow-1.11) D:\TensorFlowB>pip install tensorflow_pkg\tensorflow-1.11.0-cp36-cp36m-win_amd64.whl

2018年11月17日 星期六

Build python from source

原先是為了要準備重建 OpenCV with CUDA
所以要準備 debug 版本的 python library
但是玩到後面發現,目前不需要了
費了我好多時間,所以要記錄下過程

VS2017 安裝時要選擇 Python 開發
下載安裝 Windows 10 SDK
https://www.python.org/downloads/source/
Python 3.6.7 - 2018-10-20 失敗
Python 3.6.7rc2 - 2018-10-13 選擇 Download XZ compressed source tarball
下載後解壓縮置於 D:\OpenCV_4\Python-3.6.7
開啟 powershell
到 D:\OpenCV_4\Python-3.6.7rc2\PCbuild 目錄
./build.bat -c Debug -p x64
./build.bat -c Debug -p Win32
./build.bat -t CleanAll

編譯過程會失敗,解決方法為
VS2017 開啟 D:\OpenCV_4\Python-3.6.7\PCbuild\pcbuild.sln
Property Pages/Configuration Properties/General/Windows SDK Version
10.0.17763.0
Property Pages/Configuration Properties/C/C++/Preprocessor
_X86_ 32位元
_AMD64_ 64位元



2018年11月7日 星期三

Windows PATH 環境變數太大,最多 2047 個字元

利用 regedit 修改

電腦\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment


2018年11月6日 星期二

OpenCV Tesseract sample

samples/text/(sample)webcam_demo
Configuration Properties/Debugging/Environment
PATH=C:\Program Files\tesseract\bin;D:\TensorFlow\OCR\tesseract\win64\bin\Debug;D:\Anaconda3\Library\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin;%PATH%
QT_PLUGIN_PATH=D:\Anaconda3\Library\plugins

修改 webcam_demo.cpp 指定下列檔案的路徑
D:/OpenCV_4/build34/testdata/contrib/text/trained_classifierNM1.xml
D:/OpenCV_4/build34/testdata/contrib/text/trained_classifierNM2.xml
D:/OpenCV_4/build34/testdata/contrib/text/OCRHMM_knn_model_data.xml.gz