File size: 3,971 Bytes
162bd9a
 
 
 
f85ed11
162bd9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f85ed11
162bd9a
 
 
 
 
 
 
 
 
 
 
 
 
f85ed11
 
 
 
 
162bd9a
 
 
 
 
 
 
 
 
 
 
f85ed11
162bd9a
f85ed11
162bd9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f85ed11
162bd9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "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 = 'stuff_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",
    "\n",
    "# for split in ['train', 'val']:\n",
    "#     file = f'./annotations/stuff_{split}2017_pixelmaps'\n",
    "#     if not Path(file).exists():\n",
    "#         with zipfile.ZipFile(file + '.zip', 'r') as zipf:\n",
    "#             zipf.extractall(Path('./annotations'))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Stuff Segmentation Task"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data = COCO('annotations/stuff_train2017.json')\n",
    "val_data = COCO('annotations/stuff_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/stuff_{split}.jsonl', mode='w') as writer:\n",
    "        for image_id, image_info in tqdm(data.imgs.items()):\n",
    "            categories, sem_rles = [], []\n",
    "            anns = data.imgToAnns[image_id]\n",
    "            file_name = image_info['file_name']\n",
    "            height, width = image_info['height'], image_info['width']\n",
    "            for ann in anns:\n",
    "                categories.append(ann['category_id'] - 92)\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",
    "                else:\n",
    "                    rle = segm\n",
    "                sem_rles.append(rle)\n",
    "            writer.write({\n",
    "                'image': file_name, 'categories': categories, 'sem.rles': sem_rles\n",
    "            })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for split in ['train', 'validation']:\n",
    "    file_path = f'data/stuff_{split}.jsonl'\n",
    "    with zipfile.ZipFile(f'data/stuff_{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
}