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

Cynthia ·
更新时间:2024-09-20
· 824 次阅读

【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) 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'

解决方法:

python2: pip install opencv-python 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删掉, 不让它报这个错, 亲测通过, 好开心.
作者:安河桥



caffe io FALSE LOAD file 函数 image

需要 登录 后方可回复, 如果你还没有账号请 注册新账号