caffe.io.load_image(IMAGE_FILE, color=False)函数报错

【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) 

caffe.io.load_image(IMAGE_FILE, color=False)函数报错

1. TypeError: _open() got an unexpected keyword argument 'as_grey'

解决方法:
  把caffe.io.load_image读取图片改成cv2读取:

image = cv2.imread(IMAGE_FILE)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = image/255

上面的解决方法针对于caffe.io.load_image(IMAGE_FILE, color=True), 当你想要的是color=False的效果需要另作修改, 请接着往下看.

2. ModuleNotFoundError: No module named 'cv2'

解决方法:

  1. python2: pip install opencv-python
  2. python3: pip3 install opencv-python

3. ValueError: could not broadcast input array from shape (64,3,28,28) into shape (64,1,28,28)

原因:

# img = caffe.io.load_image(imagefile) 采用的是 skimage.io.imread()
def load_image(filename, color=True):
    """
    加载图片,
    Load an image converting from grayscale or alpha as needed.

    参数:
    filename : 图片路径
    color : boolean. 
                默认值 color=True, 表示 RGB 格式加载.
                color=False, 表示强度(intensity) 格式, 如果图片是灰度图.

    返回值:
    image : an image with type np.float32 in range [0, 1]
                of size (H x W x 3) in RGB or
                of size (H x W x 1) in grayscale.
    """
    img = skimage.img_as_float(skimage.io.imread(filename, as_grey=not color)).astype(np.float32)
    if img.ndim == 2:
        img = img[:, :, np.newaxis]
        if color:
            img = np.tile(img, (1, 1, 3))
    elif img.shape[2] == 4:
        img = img[:, :, :3]
    return img

  caffe.io.load_image() 基于 skimage.io.imread() 库读取图片, 默认得到的是 RGB 格式,像素值范围为 [0, 1] (float) 的. 当color=false时得到的是灰度图.
  我们在第一步用opencv替代caffe的load_image时, opencv转换后得到的RGB图像, 而我们需要的是grey图像.

解决方法:

input_image = cv2.imread(IMAGE_FILE, cv2.IMREAD_GRAYSCALE)

这时候确实拿到了灰度图, 但是这个灰度图可能是uint8, 255的(大概知道这么个概念没有深究), 而caffe.io.load_image(IMAGE_FILE, color=False)出来的效果是float, [0, 1]的, 结论就是这样还不行. 给自己提个醒, 免得再踩坑, 详情可以参考文章: https://www.aiuai.cn/aifarm244.html 和 https://www.cnblogs.com/houjun/p/9816361.html 和 https://blog.csdn.net/wfei101/article/details/79602659?utm_source=blogxgwz6

4. TypeError: _open() got an unexpected keyword argument 'as_grey'

关键来了, 你碰到了这个问题, 证明你的环境中的skimage.io.imread函数改变了, 现在变成了skimage.io.imread(filename, as_grey=not color), 而以前是skimage.io.imread(file_dir, as_grey=False).

怎么解决呢? 请看下面: 参考我的修改更改自己的.

# xxxxxxxxxxx/caffe/python/caffe/io.py, 我的io.py在这里, 修改这里的其中一行.
将
img = skimage.img_as_float(skimage.io.imread(filename, as_grey=not color)).astype(np.float32)
更改为
img = skimage.img_as_float(skimage.io.imread(filename)).astype(np.float32)
完成

其实就是将as_grey=not color删掉, 不让它报这个错, 亲测通过, 好开心.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安河桥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值