File size: 4,639 Bytes
b012d38 0382386 b012d38 0382386 b012d38 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import zipfile\n",
"import requests\n",
"import jsonlines\n",
"from tqdm import tqdm\n",
"from pathlib import Path\n",
"from pycocotools.coco import COCO\n",
"from pycocotools import mask as maskUtils"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Download Annotations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url = 'http://images.cocodataset.org/annotations/'\n",
"file = 'annotations_trainval2017.zip'\n",
"if not Path(f'./{file}').exists():\n",
" response = requests.get(url + file)\n",
" with open(file, 'wb') as f:\n",
" f.write(response.content)\n",
"\n",
" with zipfile.ZipFile(file, 'r') as zipf:\n",
" zipf.extractall(Path())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Read annotations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"coco91_to_coco80 = [\n",
" None, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, None,\n",
" 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,\n",
" 23, None, 24, 25, None, None, 26, 27, 28, 29, 30,\n",
" 31, 32, 33, 34, 35, 36, 37, 38, 39, None, 40, 41,\n",
" 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,\n",
" 55, 56, 57, 58, 59, None, 60, None, None, 61, None,\n",
" 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, None,\n",
" 73, 74, 75, 76, 77, 78, 79\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Instance Segmentation Task"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_data = COCO('annotations/instances_train2017.json')\n",
"val_data = COCO('annotations/instances_val2017.json')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for split, data in zip(['train', 'validation'], [train_data, val_data]):\n",
" with jsonlines.open(f'data/instance_{split}.jsonl', mode='w') as writer:\n",
" for image_id, image_info in tqdm(data.imgs.items()):\n",
" bboxes, categories, instance_rles = [], [], []\n",
" anns = data.imgToAnns[image_id]\n",
" height, width = image_info['height'], image_info['width']\n",
" for ann in anns:\n",
" bboxes.append(ann['bbox'])\n",
" categories.append(coco91_to_coco80[ann['category_id']])\n",
" segm = ann['segmentation']\n",
" if isinstance(segm, list):\n",
" rles = maskUtils.frPyObjects(segm, height, width)\n",
" rle = maskUtils.merge(rles)\n",
" rle['counts'] = rle['counts'].decode()\n",
" elif isinstance(segm['counts'], list):\n",
" rle = maskUtils.frPyObjects(segm, height, width)\n",
" rle['counts'] = rle['counts'].decode()\n",
" else:\n",
" rle = segm\n",
" instance_rles.append(rle)\n",
" writer.write({\n",
" 'image': image_info['file_name'],\n",
" 'bboxes': bboxes,\n",
" 'categories': categories,\n",
" 'inst.rles': instance_rles\n",
" })"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for split in ['train', 'validation']:\n",
" file_path = f'data/instance_{split}.jsonl'\n",
" with zipfile.ZipFile(f'data/instance_{split}.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:\n",
" zipf.write(file_path, os.path.basename(file_path))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|