File size: 3,511 Bytes
7c9a8ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "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"
   ]
  },
  {
   "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": [
    "## Image Captioning Task"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data = COCO('annotations/captions_train2017.json')\n",
    "val_data = COCO('annotations/captions_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/captions_{split}.jsonl', mode='w') as writer:\n",
    "        for image_id, image_info in tqdm(data.imgs.items()):\n",
    "            captions = []\n",
    "            anns = data.imgToAnns[image_id]\n",
    "            for ann in anns:\n",
    "                captions.append(ann['caption'])\n",
    "            writer.write({\n",
    "                'image': image_info['file_name'], 'captions': captions\n",
    "            })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "for split in ['train', 'validation']:\n",
    "    file_path = f'data/captions_{split}.jsonl'\n",
    "    with zipfile.ZipFile(f'data/captions_{split}.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:\n",
    "        zipf.write(file_path, os.path.basename(file_path))"
   ]
  }
 ],
 "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
}