tf-faster-rcnn Ubuntu 14.0 CPU環境下調試記錄

 

參考 https://github.com/endernewton/tf-faster-rcnn中的readme。

1.克隆源碼

 git clone https://github.com/endernewton/tf-faster-rcnn.git

2.修改配置文件使用CPU

        因爲使用的是CPU,根據readme中提示,需要 set USE_GPU_NMS False。所以編輯配置文件:

cd tf-faster-rcnn/lib
vim model/config.py

        大概在270行修改:

# Use GPU implementation of non-maximum suppression
#__C.USE_GPU_NMS = True
__C.USE_GPU_NMS = False

        Build the Cython modules:

        readme中這麼操作:

make clean
make
cd ..

很不幸,make之後報錯:

python setup.py build_ext --inplace
Traceback (most recent call last):
  File "setup.py", line 55, in <module>
    CUDA = locate_cuda()
  File "setup.py", line 43, in locate_cuda
    raise EnvironmentError('The nvcc binary could not be '
EnvironmentError: The nvcc binary could not be located in your $PATH. Either add it to your path, or set $CUDAHOME
make: *** [all] 錯誤 1

         提示沒有nvcc,於是尋找解決辦法,這裏參考:https://blog.csdn.net/sinat_33486980/article/details/81045315。主要是去掉查找cuda部分代碼和編譯用到的cuda相關的庫。

setup.py 55行註釋掉:

#CUDA = locate_cuda()

120-136行註釋掉:

ext_modules = [
    Extension(
        "utils.cython_bbox",
        ["utils/bbox.pyx"],
        extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
        include_dirs = [numpy_include]
    ),
    Extension(
        "nms.cpu_nms",
        ["nms/cpu_nms.pyx"],
        extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
        include_dirs = [numpy_include]
    ),
    #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_52',
    #                                 '--ptxas-options=-v',
    #                                 '-c',
    #                                 '--compiler-options',
    #                                 "'-fPIC'"]},
    #    include_dirs = [numpy_include, CUDA['include']]
    #)
]

然後 make,無錯誤。再cd ..。

3.安裝Python COCO API,訪問COCO數據集需要

        按照readme步驟:

cd data
git clone https://github.com/pdollar/coco.git
cd coco/PythonAPI
make
cd ../../..

很不幸,又報錯了:

python setup.py build_ext --inplace
running build_ext
building 'pycocotools._mask' extension
error: unknown file type '.pyx' (from 'pycocotools/_mask.pyx')
make: *** [all] 錯誤 1

參考:https://blog.csdn.net/weixin_41813620/article/details/85590891,下載Pyrex並安裝,其官網:http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/,其安裝:

wget http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/Pyrex-0.9.9.tar.gz
tar -zxvf Pyrex-0.9.9.tar.gz
cd Pyrex-0.9.9
python setup.py install

再去make Python COCO API,還是報錯:

python setup.py build_ext --inplace
running build_ext
pyrexc pycocotools/_mask.pyx --> pycocotools/_mask.c
/home/kmin/tianchi/jinjin/faster-rcnn/tf-faster-rcnn/data/coco/PythonAPI/pycocotools/_mask.pyx:42:34: Expected ')'
building 'pycocotools._mask' extension
creating build
creating build/common
creating build/temp.linux-x86_64-2.7
creating build/temp.linux-x86_64-2.7/pycocotools
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/local/lib/python2.7/dist-packages/numpy/core/include -I../common -I/usr/include/python2.7 -c ../common/maskApi.c -o build/temp.linux-x86_64-2.7/../common/maskApi.o -Wno-cpp -Wno-unused-function -std=c99
../common/maskApi.c: In function ‘rleToBbox’:
../common/maskApi.c:141:31: warning: ‘xp’ may be used uninitialized in this function [-Wmaybe-uninitialized]
       if(j%2==0) xp=x; else if(xp<x) { ys=0; ye=h-1; }
                               ^
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/local/lib/python2.7/dist-packages/numpy/core/include -I../common -I/usr/include/python2.7 -c pycocotools/_mask.c -o build/temp.linux-x86_64-2.7/pycocotools/_mask.o -Wno-cpp -Wno-unused-function -std=c99
x86_64-linux-gnu-gcc: error: pycocotools/_mask.c: 沒有那個文件或目錄
x86_64-linux-gnu-gcc: fatal error: no input files
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 4
make: *** [all] 錯誤 1

提示沒有_mask.c,參考https://blog.csdn.net/weixin_41813620/article/details/85590891,手動生成這個文件:

cd pycocotools
cython _mask.pyx
cd ..

再make,無報錯。

4.Demo

下載預訓練模型:

./data/scripts/fetch_faster_rcnn_models.sh

報錯:

Downloading Resnet 101 Faster R-CNN models Pret-trained on VOC 07+12 (340M)...
--2019-03-01 05:31:53--  http://ladoga.graphics.cs.cmu.edu/xinleic/tf-faster-rcnn/res101/voc_0712_80k-110k.tgz
正在解析主機 ladoga.graphics.cs.cmu.edu (ladoga.graphics.cs.cmu.edu)... 128.2.220.68
正在連接 ladoga.graphics.cs.cmu.edu (ladoga.graphics.cs.cmu.edu)|128.2.220.68|:80... 已連接。
已發出 HTTP 請求,正在等待迴應... 讀取文件頭錯誤 (連接被對方重設)。
重試中。

打開這個文件看下,發現作者已經提示,網址連接不上換一下,於是重新換一下URL:

vim data/scripts/fetch_faster_rcnn_models.sh

第九行:

# replace it with gs11655.sp.cs.cmu.edu if ladoga.graphics.cs.cmu.edu does not work
#URL=http://ladoga.graphics.cs.cmu.edu/xinleic/tf-faster-rcnn/$NET/$FILE
URL=http://gs11655.sp.cs.cmu.edu/xinleic/tf-faster-rcnn/$NET/$FILE

重新運行腳本,好吧,根本不行:

Downloading Resnet 101 Faster R-CNN models Pret-trained on VOC 07+12 (340M)...
--2019-03-01 05:37:41--  http://gs11655.sp.cs.cmu.edu/xinleic/tf-faster-rcnn/res101/voc_0712_80k-110k.tgz
正在解析主機 gs11655.sp.cs.cmu.edu (gs11655.sp.cs.cmu.edu)... 失敗:未知的名稱或服務。
wget: 無法解析主機地址 “gs11655.sp.cs.cmu.edu”
Unzipping...

gzip: stdin: unexpected end of file
tar: Child returned status 1
tar: Error is not recoverable: exiting now
Done. Please run this command again to verify that checksum = cb32e9df553153d311cc5095b2f8c340.

參考:https://blog.csdn.net/weixin_41813620/article/details/85590891,到百度網盤下載:https://pan.baidu.com/s/1kWkF3fT。下載之後,把它放到tf-faster-rcnn/data下,然後解壓:

tar zxvf data/voc_0712_80k-110k.tgz

創建一個文件夾和一個軟連接來使用模型

NET=res101
TRAIN_IMDB=voc_2007_trainval+voc_2012_trainval
mkdir -p output/${NET}/${TRAIN_IMDB}
cd output/${NET}/${TRAIN_IMDB}
ln -s ../../../data/voc_2007_trainval+voc_2012_trainval ./default
cd ../../..

這裏基本沒什麼問題。

運行Demo:

./tools/demo.py

報錯:

Traceback (most recent call last):
  File "./tools/demo.py", line 19, in <module>
    from model.config import cfg
  File "/home/kmin/tianchi/jinjin/faster-rcnn/tf-faster-rcnn/tools/../lib/model/__init__.py", line 1, in <module>
    from . import config
  File "/home/kmin/tianchi/jinjin/faster-rcnn/tf-faster-rcnn/tools/../lib/model/config.py", line 9, in <module>
    from easydict import EasyDict as edict
ImportError: No module named easydict

這個比較簡單,安裝easydict就行:

pip install easydict

然後./tools/demo.py,還是報錯:

Traceback (most recent call last):
  File "./tools/demo.py", line 20, in <module>
    from model.test import im_detect
  File "/home/kmin/tianchi/jinjin/faster-rcnn/tf-faster-rcnn/tools/../lib/model/test.py", line 24, in <module>
    from model.nms_wrapper import nms
  File "/home/kmin/tianchi/jinjin/faster-rcnn/tf-faster-rcnn/tools/../lib/model/nms_wrapper.py", line 12, in <module>
    from nms.gpu_nms import gpu_nms
ImportError: No module named gpu_nms

因爲沒有gpu,要把lib/model/nms_wrapper.py的12行註釋掉,再運行。

Traceback (most recent call last):
  File "./tools/demo.py", line 25, in <module>
    import matplotlib.pyplot as plt
ImportError: No module named matplotlib.pyplot

額,沒裝matplotlib,於是又安裝matplotlib:

pip install matplotlib

結果還在安裝過程報錯:

Downloading/unpacking matplotlib
  Downloading matplotlib-3.0.3.tar.gz (36.6MB): 36.6MB downloaded
  Running setup.py (path:/tmp/pip_build_root/matplotlib/setup.py) egg_info for package matplotlib
    
    Matplotlib 3.0+ does not support Python 2.x, 3.0, 3.1, 3.2, 3.3, or 3.4.
    Beginning with Matplotlib 3.0, Python 3.5 and above is required.
    
    This may be due to an out of date pip.
    
    Make sure you have pip >= 9.0.1.
    
    Complete output from command python setup.py egg_info:
    

Matplotlib 3.0+ does not support Python 2.x, 3.0, 3.1, 3.2, 3.3, or 3.4.

Beginning with Matplotlib 3.0, Python 3.5 and above is required.



This may be due to an out of date pip.



Make sure you have pip >= 9.0.1.



----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip_build_root/matplotlib
Storing debug log for failure in /home/kmin/.pip/pip.log

按照提示,要更新pip,於是更新:

sudo pip install --upgrade pip

再安裝matplotlib,成功裝上。再運行./tools/demo.py:

Traceback (most recent call last):
  File "./tools/demo.py", line 30, in <module>
    from nets.vgg16 import vgg16
  File "/home/kmin/tianchi/jinjin/faster-rcnn/tf-faster-rcnn/tools/../lib/nets/vgg16.py", line 11, in <module>
    import tensorflow.contrib.slim as slim
ImportError: No module named contrib.slim

更新下tensorflow:

pip install --upgrade tensorflow

再運行,報錯:

Traceback (most recent call last):
  File "./tools/demo.py", line 20, in <module>
    from model.test import im_detect
  File "/home/kmin/tianchi/jinjin/faster-rcnn/tf-faster-rcnn/tools/../lib/model/test.py", line 23, in <module>
    from model.bbox_transform import clip_boxes, bbox_transform_inv
  File "/home/kmin/tianchi/jinjin/faster-rcnn/tf-faster-rcnn/tools/../lib/model/bbox_transform.py", line 12, in <module>
    import tensorflow as tf
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 52, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/core/framework/graph_pb2.py", line 6, in <module>
    from google.protobuf import descriptor as _descriptor
ImportError: No module named protobuf

下載protobuf,下載地址:https://github.com/protocolbuffers/protobuf/releases/tag/v3.7.0。然後解壓,進入解壓的文件夾,裏面有個python文件夾,進入,然後:

python setup.py build
python setup.py install

再運行,報錯:

Traceback (most recent call last):
  File "./tools/demo.py", line 20, in <module>
    from model.test import im_detect
  File "/home/kmin/tianchi/jinjin/faster-rcnn/tf-faster-rcnn/tools/../lib/model/test.py", line 23, in <module>
    from model.bbox_transform import clip_boxes, bbox_transform_inv
  File "/home/kmin/tianchi/jinjin/faster-rcnn/tf-faster-rcnn/tools/../lib/model/bbox_transform.py", line 12, in <module>
    import tensorflow as tf
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 122, in <module>
    from tensorflow.python.platform import test
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/test.py", line 30, in <module>
    from tensorflow.python.framework import test_util as _test_util
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/test_util.py", line 73, in <module>
    from tensorflow.python.platform import googletest
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/googletest.py", line 35, in <module>
    from tensorflow.python.platform import benchmark
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/benchmark.py", line 122, in <module>
    class Benchmark(six.with_metaclass(_BenchmarkRegistrar, object)):
  File "/usr/lib/python2.7/dist-packages/six.py", line 617, in with_metaclass
    return meta("NewBase", bases, {})
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/benchmark.py", line 117, in __new__
    if not newclass.is_abstract():
AttributeError: type object 'NewBase' has no attribute 'is_abstract'

更新six可以解決:

sudo easy_install --upgrade six

終於,可以運行了:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章