🚀Debug专栏
其他debug记录请参考上方【debug专栏】
目录
🍀🍀背景
调用ann2RLE函数时,有的识别结果像素非常小,没有过滤,直接转RLE格式的txt文本格式,报错TypeError: Argument ‘bb‘ has incorrect type (expected numpy.ndarray, got list)
❓❓报错
详细报错信息:
pycocotools 包处调用coco.py 报错TypeError: Argument 'bb' has incorrect type (expected numpy.ndarray, got list)
🌻🌻解决方案
1.分析原因
报这个错误是传入的segment点是四个以内(包含四个)的会触发的错误。
2.增加条件判断
找到环境安装位置/data/XXX/anaconda3/envs/openmmlab/lib/python3.8/site-packages/pycocotools-2.0-py3.8-linux-x86_64.egg/pycocotools的coco.py文件,修改420行,增加条件判断就好了✔️。
python代码展示如下:
if len(segm[0])<5:
segm[0].append(0)
修改后python完整代码如下:
def annToRLE(self, ann):
"""
Convert annotation which can be polygons, uncompressed RLE to RLE.
:return: binary mask (numpy 2D array)
"""
t = self.imgs[ann['image_id']]
h, w = t['height'], t['width']
segm = ann['segmentation']
if len(segm[0])<5: #增加此行
segm[0].append(0) #增加此行
if type(segm) == list:
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
rles = maskUtils.frPyObjects(segm, h, w)
rle = maskUtils.merge(rles)
elif type(segm['counts']) == list:
# uncompressed RLE
rle = maskUtils.frPyObjects(segm, h, w)
else:
# rle
rle = ann['segmentation']
return rle
至此就解决该问题了😎😎
3.如果还报错,比如ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (15,) + inhomogeneous part.
比如我的新报错信息:
报错信息:File "pycocotools/_mask.pyx", line 303, in pycocotools._mask.frPyObjects
File "pycocotools/_mask.pyx", line 265, in pycocotools._mask.frPoly
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (15,) + inhomogeneous part.
4. 报错原因分析
说我的数据是不均匀的,打印了以下segm才发现,是因为我的数据里有长度为3的list,也就是说我的数据中有两个点甚至一个点组成的目标,显然这不是我关心的目标,因为他太小了,所以我索性在这把这种样本删除了。
5.继续修改代码
增加如下python代码,把各种情况都考虑到。
if len(segm[0])==4: #如果差一个点,可以补全
segm[0].append(0)
elif len(segm[0])<4: #如果差好几个,索性删掉
segm.remove(segm[0])
6.修改后完整python代码
完整python代码如下:
def annToRLE(self, ann):
"""
Convert annotation which can be polygons, uncompressed RLE to RLE.
:return: binary mask (numpy 2D array)
"""
t = self.imgs[ann['image_id']]
h, w = t['height'], t['width']
segm = ann['segmentation']
#-------------------修改位置----------------------------
if len(segm[0])==4: #如果差一个点,可以补全
segm[0].append(0)
elif len(segm[0])<4: #如果差好几个,索性删掉
segm.remove(segm[0])
#-------------------修改位置----------------------------
if type(segm) == list:
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
rles = maskUtils.frPyObjects(segm, h, w)
rle = maskUtils.merge(rles)
elif type(segm['counts']) == list:
# uncompressed RLE
rle = maskUtils.frPyObjects(segm, h, w)
else:
# rle
rle = ann['segmentation']
return rle
至此真的就没有bug了,如果有,留言我们一起debug
整理不易,欢迎一键三连!!!
送你们一条美丽的--分割线--
🌷🌷🍀🍀🌾🌾🍓🍓🍂🍂🙋🙋🐸🐸🙋🙋💖💖🍌🍌🔔🔔🍉🍉🍭🍭🍋🍋🍇🍇🏆🏆📸📸⛵⛵⭐⭐🍎🍎👍👍🌷🌷