Hi, I have a coco json annotation file with 3 classes.
I generate mask images from segmentation points in coco json, through the following code. Through the pillow putpalette each class in the mask images has a pixel value between 1 and 3. But the evaluation result is always nan.
def unet_convert_dataset(path_to_annotation_json, path_to_original_images_folder, path_to_temp_images_folder, path_to_masks_folder, palette=None, train=False):
coco = COCO(path_to_annotation_json)
cats = coco.loadCats(coco.getCatIds())
categories = [cat['name'] for cat in cats]
if train:
palette = [0, 0, 0]
for i in range(len(categories)):
palette += ([random.randint(20, 255)] * 3)
for img in coco.imgs:
height, width = coco.imgs[img]['height'], coco.imgs[img]['width']
mask = np.zeros((height, width))
image_path = os.path.join(path_to_original_images_folder, coco.imgs[img]['file_name'])
image = cv2.imread(image_path)
image_name = coco.imgs[img]['file_name'].rsplit(".", 1)[0]
cv2.imwrite(path_to_temp_images_folder + '/' + image_name + '.png', image)
catIds = coco.getCatIds(catNms=cats)
annIds = coco.getAnnIds(imgIds=[img], catIds=catIds)
anns = coco.loadAnns(annIds)
for i, ann in enumerate(anns):
if ann['image_id'] == coco.imgs[img]['id']:
x = []
y = []
for s in ann['segmentation']:
category = ann['category_id']
for k in range(0, len(s)):
if k % 2 == 0:
x.append(s[k])
y.append(s[k+1])
fill_row_coords, fill_col_coords = draw.polygon(y, x, (height, width))
mask[fill_row_coords, fill_col_coords] = category
# Zero-pad the palette to 256 RGB colours, i.e. 768 values
palette += (768-len(palette))*[0]
# Make black and white (bw) PIL/Pillow image from the binary array
bw = Image.fromarray(mask.astype(np.uint8))
bw.convert("L")
# Push the palette into image and save
bw.putpalette(palette)
bw.save(path_to_masks_folder + '/' + image_name + ".png")
evaluation result:
“{‘background’: {‘precision’: 0.9648212, ‘Recall’: 1.0, ‘F1 Score’: 0.9820956837153484, ‘iou’: 0.9648212}, ‘darz’: {‘precision’: nan, ‘Recall’: nan, ‘F1 Score’: nan, ‘iou’: nan}, ‘2color_fibers’: {‘precision’: nan, ‘Recall’: 0.0, ‘F1 Score’: nan, ‘iou’: 0.0}, ‘paregi’: {‘precision’: nan, ‘Recall’: 0.0, ‘F1 Score’: nan, ‘iou’: 0.0}}”