• 用python调用caffe时出错:AttributeError: 'module' object has no attribute 'bool_'


    由于用caffe的时候需要将/somepath/your_caffe/python include进来作为环境变量,但是caffe中有个io.py和numpy的io冲突,所以导致这种现象。

    下面给出了一种解决方法,即对有冲突的io文件进行重命名:

    numpyのioとPyCaffeのio.pyが競合するようです。(Strange Issue using Python #782)

    $ python classify.py --raw_scale 255 ~/caffe/101_ObjectCategories/airplanes/image_0001.jpg ../result.npy
    Traceback (most recent call last):
      File "classify.py", line 7, in <module>
        import numpy as np
    
    ...
    
    
      File "/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py", line 8, in <module>
        dtype_range = {np.bool_: (False, True),
    AttributeError: 'module' object has no attribute 'bool_'

    無理やりな方法ですがio.pyをcaffe_io.pyにrenameします。
    ここでrenameした場合には、次の”Caffeを特徴抽出器として使った分類”に作成するfeature.pyでもrenameする必要があります。

    $ cd ~/caffe/python/
    $ aftfile="caffe_io"
    $ for file in `find . -name "*.py"`; do; cat $file | sed -e "s/import [w.]*io/import $aftfile/g" | sed -e "s/caffe.io/caffe.$aftfile/g" > $file".tmp";mv $file".tmp" $file; done
    $ mv "caffe/io.py" "caffe/"$aftfile".py"

    另外,还可以先将PATH中的caffe的路径去掉,然后import numpy,然后在通过sys.path.append把caffe的路径显式添加进路径,从而调用caffe。

    import sys
    if '/somepath/caffe-yolov2-master/python' in sys.path:
        sys.path.remove('/somepath/caffe-yolov2-master/python')
    if '/somepath/caffe-yolov2-master/python/caffe' in sys.path:
        sys.path.remove('/somepath/caffe-yolov2-master/python/caffe')
    if '' in sys.path:
        sys.path.remove('')
    import numpy as np
    import time
    # caffe_root = '../../'  # this file should be run from {caffe_root}/examples (otherwise change this line)
    caffe_root = '/home/gpu_share/jz/about-caffe/gklz1982/caffe-yolov2-master/'
    if caffe_root + 'python' not in sys.path:
        sys.path.append(caffe_root + 'python')
    # sys.path.insert(0, caffe_root + 'python')
    import caffe
    import math

    reference:
    Caffe, Pylearn2をまとめて試す https://qiita.com/CORDEA/items/9fad27ae024928b6a7b1

  • 相关阅读:
    C# 与 Java Rsa加密与解密互通
    PHP 读取Postgresql中的数组
    ArcGis Javascript API (V3.6)加载天地图
    Entity Framework 6.0 对枚举的支持/实体添加后会有主键反回
    ubuntu 中 ssh连接用UTF8
    Entity Framework PostgresQL CodeFirst
    Golang 字符编码
    CentOS 安装 mono
    C和C++中的不定参数
    WisDom.Net 框架设计(一) 总体框架
  • 原文地址:https://www.cnblogs.com/morikokyuro/p/13256712.html
Copyright © 2020-2023  润新知