mmseg报错ValueError: size shape must match input shape. Input is 2D,size is 3

文章讲述了在使用mmsegmentation时遇到的关于标签尺寸和输入格式的错误,以及如何通过修改loading.py文件中的flag值或dataset配置来解决。同时提醒了cv2.imread处理图像通道的问题。
摘要由CSDN通过智能技术生成

运行mmsegmentation碰到报错:size shape must match input shape. Input is 2D, size is 3
这个问题比较坑。有时候我们的标签size是[512,512,3],然而mmsegmentation需要的标签输入应该是8位的图片,即[512,512]。这点得怪mmsegmentation写的不好,没有考虑到输入标签是三通道灰度图的情况。

解决方案:进入mmseg/datasets/pipelines/loading.py,找到LoadAnnotations类的__call__函数,将gt_semantic_seg = mmcv.imfrombytes(img_bytes, flag=’unchanged’,backend=self.imdecode_backend).squeeze().astype(np.uint8)语句中的flag值改为grayscale,即gt_semantic_seg = mmcv.imfrombytes( img_bytes, flag='grayscale', backend=self.imdecode_backend).squeeze().astype(np.uint8)。

Btw,也可以在local_configs/base/datasets/ade20k.py的dict(type=‘LoadAnnotations’, reduce_zero_label=True),语句中,加上flag=grayscale,但这种方式还是得到mmseg/datasets/pipelines/loading.py文件中在__init__函数里加上self.flag,即将LoadAnnotations类的init改为:

    def __init__(self,
                 reduce_zero_label=False,
                 file_client_args=dict(backend='disk'),
                 imdecode_backend='pillow',
                 flag='grayscale'):
        self.reduce_zero_label = reduce_zero_label
        self.file_client_args = file_client_args.copy()
        self.file_client = None
        self.imdecode_backend = imdecode_backend
        self.flag = flag



这个问题我看了半天,主要犯了个大错。我一直检查标签数据,用的是cv2.imread(‘xxx.png’),但这样读取图片,无论是不是单通道的灰度图,都会被读成三通道的彩色图。btw,如果用cv2.imread(‘xxx.png’,0)读取图片,即使是三通道的图,也会被读成单通道的。这点需要注意一下。
 

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

坠金

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

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

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

打赏作者

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

抵扣说明:

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

余额充值