ValueError('need at least one array to stack') ValueError: need at least one array to stac
使用mmdetection做實(shí)例分割過(guò)程中踩的一個(gè)大坑。
?經(jīng)過(guò)反復(fù)調(diào)試后,終于發(fā)現(xiàn)是數(shù)據(jù)集的問(wèn)題。在實(shí)例分割中,分割標(biāo)簽的鍵'iscrowd'為1的時(shí)候,標(biāo)簽會(huì)被拋棄,全為1的時(shí)候,就會(huì)發(fā)生以上錯(cuò)誤。出錯(cuò)原因代碼如下
?iscrowd表示一個(gè)以上物體連在一個(gè),作為一個(gè)整體,如人群,所以做實(shí)例分割時(shí)候會(huì)被過(guò)濾掉。而在coco數(shù)據(jù)集中。iscrowd的取值取決于標(biāo)簽segmentation的格式,格式為RLE的時(shí)候,iscrowd為1,格式為polygon的時(shí)候,iscrowd為0。所以當(dāng)你自己的數(shù)據(jù)標(biāo)簽的segmentation全為RLE格式的時(shí)候,你的所有標(biāo)簽都會(huì)被拋棄,錯(cuò)誤在所難免。于是就要將RLE格式轉(zhuǎn)為polygon格式。代碼如下,
代碼參考:
https://github.com/facebookresearch/Detectron/issues/100
https://tianchi.aliyun.com/competition/entrance/231787/information
RLE轉(zhuǎn)polygon
import numpy as np import json from tqdm import tqdm import random import os import cv2 import matplotlib.pyplot as plt import pycocotools.mask as maskUtilsdef annToRLE(ann, i_w, i_h):h, w = i_h, i_wsegm = ann['segmentation']if type(segm) == list:# polygon -- a single object might consist of multiple parts# we merge all parts into one mask rle coderles = maskUtils.frPyObjects(segm, h, w)rle = maskUtils.merge(rles)elif type(segm['counts']) == list:# uncompressed RLErle = maskUtils.frPyObjects(segm, h, w)else:# rlerle = ann['segmentation']return rlerle = annToRLE(ann_fu, ann_fu['segmentation']['size'][0], ann_fu['segmentation']['size'][1]) mask = maskUtils.decode(rle) contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # after opencv 3.2 # _, contours, hierarchy = cv2.findContours((mask).astype(np.uint8), cv2.RETR_TREE, # cv2.CHAIN_APPROX_SIMPLE) segmentation = [] for contour in contours:contour = contour.flatten().tolist()if len(contour) > 4:segmentation.append(contour) ann_coco['segmentation'] = segmentation?
?值得注意的是,contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)這句可能報(bào)錯(cuò),原因是版本問(wèn)題。opencv版本高于3.2時(shí),有三個(gè)輸出,要用??_, contours, _ , 否則,使用 contours, _? 。
總結(jié)
以上是生活随笔為你收集整理的ValueError('need at least one array to stack') ValueError: need at least one array to stac的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: easyui01
- 下一篇: ffmpeg奇偶场帧Interlace