Eli-YiLi / PMM

Pseudo-mask Matters in Weakly-supervised Semantic Segmentation
MIT License
44 stars 4 forks source link

coco2017? #9

Closed ZechengLi19 closed 2 years ago

ZechengLi19 commented 2 years ago

Thanks for your perfect work! Could you provide the code for converting COCO2014 to VOC format?

Eli-YiLi commented 2 years ago

The dataset of coco2014 is provided in baiduyun. The previous script is missing. There is a script for reference only.

from pycocotools.coco import COCO from pycocotools.mask import encode,decode,area,toBbox import numpy as np from PIL import Image import sys, os

names = ['background', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']

ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90]

coco=COCO(sys.argv[1])

os.makedirs(sys.argv[2], exist_ok=True)

imgIds = coco.getImgIds() imags = coco.loadImgs(imgIds)

annIds = coco.getAnnIds(imgIds=imgIds)

anns = coco.loadAnns(annIds)

img_id = anns[0]['image_id'] mask = coco.annToMask(anns[0]) mask[mask == 1] = ids.index(anns[0]['category_id'])

for ann in anns: m = coco.annToMask(ann) m[m == 1] = ids.index(ann['category_id']) if ann['image_id'] != img_id: name = imags[imgIds.index(img_id)]['file_name'] mask = Image.fromarray(mask) mask.save(os.path.join(sys.argv[2], name.replace('.jpg', '.png'))) print(name) img_id = ann['image_id'] mask = m mask[m != 0] = m[m != 0]

ZechengLi19 commented 2 years ago

Thanks!