Ubuntu14.04 + Text-Detection-with-FRCN(CPU)

时间:2023-03-08 22:06:51

操作系统:

yt@yt-MS-:~$ cat /etc/issue
Ubuntu 14.04. LTS \n \l

Python版本:

yt@yt-MS-:~$ python --version
Python 2.7.

pip版本:

yt@yt-MS-:~$ pip --version
pip 1.5. from /usr/lib/python2./dist-packages (python 2.7)

源文件:

git clone --recursive https://github.com/jugg1024/Text-Detection-with-FRCN.git

1. 安装Caffe需要的依赖包:

sudo apt-get install build-essential  # basic requirement
sudo apt-get install libblas-dev libopenblas-base liblapack-dev libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler #required by caffe

将caffe-fast-rcnn/python目录下的requirements下的依赖都装一遍:

cd Text-Detection-with-FRCN/py-faster-rcnn/caffe-fast-rcnn/python
cat requirements.txt for req in $(cat requirements.txt); do pip install $req; done
Cython>=0.19.
numpy>=1.7.
scipy>=0.13.
scikit-image>=0.9.
matplotlib>=1.3.
ipython>=3.0.
h5py>=2.2.
leveldb>=0.191
networkx>=1.8.
nose>=1.3.
pandas>=0.12.
python-dateutil>=1.4,<
protobuf>=2.5.
python-gflags>=2.0
pyyaml>=3.10
Pillow>=2.3.
six>=1.1.

这里有一个小技巧,因为pip这个工具对应的网络非常的烂,这个时候,可以将其改为国内的镜像网站,速度将提升几个数量级,方法如下:

sudo pip install ipython -i http://pypi.douban.com/simple

如果出现有依赖包安装失败可以使用这种形式安装

sudo apt-get install python-matplotlib

除此之外还有以下依赖包:

sudo pip install easydict
sudo pip install opencv_python

2. 编译

编译 py-faster-rcnn

2.1 change the branch of py-faster-rcnn to text-detection-demo.

cd Text-Detection-with-FRCN/py-faster-rcnn
git checkout text-detection

2.2 Build Caffe and pycaffe.

cd Text-Detection-with-FRCN/py-faster-rcnn/caffe-fast-rcnn
cp Makefile.config.example Makefile.config

修改 Makefile.config

CPU_ONLY :=
WITH_PYTHON_LAYER :=
# 以下可选择性改变

BLAS_INCLUDE := /usr/include/atlas-x86_64-base
BLAS_LIB := /usr/lib64/atlas PYTHON_INCLUDE := /usr/include/python2. \
/usr/lib64/python2./site-packages/numpy/core/include PYTHON_LIB := /usr/lib64 # Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/lib64

编译(也可以直接 make , make pycaffe )

make -j16 && make pycaffe  # here only python api is used.

测试下

cd python
python
>>> import caffe
>>> caffe.__version__
'1.0.0-rc3'

2.2 Build the Cython modules.

cd Text-Detection-with-FRCN/py-faster-rcnn/lib

修改 setup.py 把所以关于gpu的部分注释掉

# CUDA = locate_cuda()

            # self.set_executable('compiler_so', CUDA['nvcc'])

    # Extension('nms.gpu_nms',
# ['nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],
# library_dirs=[CUDA['lib64']],
# libraries=['cudart'],
# language='c++',
# runtime_library_dirs=[CUDA['lib64']],
# # this syntax is specific to this build system
# # we're only going to use certain compiler args with nvcc and not with
# # gcc the implementation of this trick is in customize_compiler() below
# extra_compile_args={'gcc': ["-Wno-unused-function"],
# 'nvcc': ['-arch=sm_35',
# '--ptxas-options=-v',
# '-c',
# '--compiler-options',
# "'-fPIC'"]},
# include_dirs = [numpy_include, CUDA['include']]
# ),

修改 ./fast_rcnn/nms_wrapper.py

#from nms.gpu_nms import gpu_nms

def nms(dets, thresh, force_cpu=True):

修改 ./fast_rcnn/config.py

__C.USE_GPU_NMS = False

修改 py-faster-rcnn/tools/test_net.py和 py-faster-rcnn/tools/train_net.py
将 caffe.set_mode_gpu()修改为caffe.set_mode_cpu().

编译

make

3. Run demo

  • Run text detection demo
    1. 下载训练好的模型,解压放到 Text-Detection-with-FRCN/models
    URL: http://pan.baidu.com/s/1dE2Ori5 Extract Code: phxk

    2. run demo,检测结果会保存在Text-Detection-with-FRCN/output_img

    cd Text-Detection-with-FRCN/
    ./script/text_detect_demo.sh

    3. training, if you think the model is not ok, then you can trainning with your own dataset, take coco-text for example.

    3.1 download coco-text dataset

      cd Text-Detection-with-FRCN/datasets/script
    ./fetch_dataset.sh coco-text
    # download it takes long!
    # ensure you have both data and label
    # for coco-text label is in COCO-text.json, and data is in train2014.zip

    3.2 download pre-train model

      # finetune on this model, you can also use one model you train before
    cd Text-Detection-with-FRCN/py-faster-rcnn
    ./data/scripts/fetch_imagenet_models.sh
    # download it takes long!

    3.3 format the data(you should write your code here)

      # format the raw image and label into the type of pascal_voc
    # follow the code in $Text-Detection-with-FRCN/datasets/script/format_annotation.py
    cd Text-Detection-with-FRCN/datasets/script
    ./format_annotation.py --dataset coco-text

    3.4 create a softlink the formatted data to working directorry

      # link your data folder to train_data
    cd Text-Detection-with-FRCN/datasets/
    ln -s train_data coco-text # $YOUR_DATA

    3.5 training

      cd Text-Detection-with-FRCN/py-faster-rcnn/
    ./experiments/scripts/faster_rcnn_end2end.sh [gpu-id] [net](VGG16) [label_type](must be pascal_voc)
  • Run text detection demo
    1. 下载训练好的模型,解压放到 Text-Detection-with-FRCN/py-faster-rcnn/data/faster_rcnn_models
    cd Text-Detection-with-FRCN/py-faster-rcnn/data/scripts
    ./ fetch_faster_rcnn_models.sh

    2. 修改 demo.py, 添加保存输出语句

    cd Text-Detection-with-FRCN/py-faster-rcnn/tools
    def demo(net, image_name):
    '''''' output_dir = os.path.join(cfg.ROOT_DIR, '..', 'output_img',
    image_name.split('/')[-] + "_detect_rst.jpg")
    plt.savefig(output_dir)

    3. run demo,检测结果会保存在Text-Detection-with-FRCN/output_img

    cd Text-Detection-with-FRCN/py-faster-rcnn/tools
    ./demo.py --cpu

参考:

  1. http://www.cnblogs.com/justinzhang/p/5386837.html
  2. http://blog.sina.com.cn/s/blog_679f93560102wpyf.html
  3. http://blog.****.net/wuzuyu365/article/details/5189525
  4. http://blog.****.net/u011762313/article/details/47262549