zoukankan      html  css  js  c++  java
  • 用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

  • 相关阅读:
    [leetcode]5最长回文子串
    [leetcode]4寻找两个有序数组的中位数
    [leetcode]3无重复字符的最长字串
    [leetcode]2两数相加
    [leetcode]1两数之和
    [学习记录]堆
    [学习记录]平衡树
    [学习记录]二叉树删除
    [学习记录]排序算法总结
    创建mysql数据库
  • 原文地址:https://www.cnblogs.com/morikokyuro/p/13256712.html
Copyright © 2011-2022 走看看