Repository navigation
Expand file tree
/
Copy pathreader.py
More file actions
410 lines (359 loc) · 16.7 KB
/
Copy pathreader.py
File metadata and controls
410 lines (359 loc) · 16.7 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# usage: from visionset.formats.coco_panoptic import CocoPanopticImporter
"""COCO panoptic in: each segment of each picture becomes one mask of its category.
The source holds ``panoptic_<name>.json`` files under ``annotations/`` or at its
top. Each names its pictures in ``images``, its categories in ``categories``,
and per picture one RGB PNG and its ``segments_info``. The PNGs live in the
directory named after the JSON file, beside it (``annotations/panoptic_val2017/``)
or at the top of the source. Pictures are looked for under ``images/<name>/``,
``images/``, ``<name>/`` and then the top of the source.
A PNG pixel's segment id is ``R + 256 * G + 65536 * B``; ``0`` is void. Each id
the PNG holds becomes one mask, exactly the pixels holding it, of the category
its ``segments_info`` entry names. A panoptic PNG is never read as a class-index
mask.
Refused, by file and id: a PNG that is missing, unreadable or not RGB; an id in
``segments_info`` the PNG does not hold, or one in the PNG that ``segments_info``
does not list; a segment id given twice in one picture, or ``0``; an image or
category id given twice; an annotation naming an image the file does not list,
or a picture annotated twice.
Reported as losses, never dropped silently: void pixels (``label``), a PNG not
the size of its picture (``label``, the picture is imported unlabelled), a
segment naming no declared category (``label``, its pixels are imported
unlabelled), ``iscrowd`` ``1`` (``attribute``, the segment is kept as an
ordinary mask), ``isthing`` ``0`` (``attribute``, a stuff category is a class
like any other), a ``supercategory`` other than the name, a ``color`` and any
other field with no place in the schema (``attribute``). ``bbox`` and ``area``
are read off the pixels, segment and image ids are not kept, and the split is
not imported.
"""
from __future__ import annotations
import json
from collections.abc import Iterable, Mapping, Sequence
from dataclasses import dataclass, field
from pathlib import Path, PurePosixPath
from typing import Any, Final, TypeGuard
from visionset.formats._layout import IMAGES_DIRNAME
from visionset.formats._reading import Losses, contained, read_capped, relative, to_dataset
from visionset.kernel.domain import (
GeometryType,
ImportedClass,
ImportedDataset,
ImportedImage,
ImportedLabel,
ImportLossKind,
MaskEncodingError,
require_relative_path,
split_class_map,
)
from visionset.kernel.domain.rle import check_mask_size
from visionset.kernel.errors import ImportSourceUnreadable
FORMAT_NAME: Final = "coco-panoptic"
ANNOTATIONS_DIRNAME: Final = "annotations"
PANOPTIC_PREFIX: Final = "panoptic_"
VOID: Final = 0
#: The largest id three 8-bit channels hold.
MAX_SEGMENT_ID: Final = 256**3 - 1
_LABEL: Final = "COCO panoptic"
_SEGMENT_FIELDS: Final = frozenset({"id", "category_id", "area", "bbox", "iscrowd"})
_CATEGORY_FIELDS: Final = frozenset({"id", "name", "supercategory", "isthing", "color"})
_VOID_PIXELS: Final = (
"void pixels (id 0) mark an area left unlabelled or ambiguous, which no geometry holds"
)
_CROWD: Final = "iscrowd is not imported; the segment is kept as an ordinary mask"
_STUFF: Final = "isthing 0 (stuff) is not imported; its segments are kept as ordinary masks"
_SUPERCATEGORY: Final = "its supercategory is not imported; a class has no parent"
_COLOUR: Final = "its colour is not imported"
_UNKNOWN_CATEGORY: Final = "it names no declared category; its pixels are imported unlabelled"
@dataclass
class _Picture:
path: PurePosixPath
width: int | None
height: int | None
labels: list[ImportedLabel] = field(default_factory=list)
annotated: bool = False
class CocoPanopticImporter:
"""Reads COCO panoptic JSON and its segment PNGs into an ``ImportedDataset``."""
format_name = FORMAT_NAME
def read(self, src: Path) -> ImportedDataset:
if not src.is_dir():
raise ImportSourceUnreadable(f"the {_LABEL} source {src.name!r} is not a directory")
root = src.resolve()
losses: Losses = Losses()
classes: dict[str, None] = {}
reported: set[tuple[str, str]] = set()
pictures: dict[PurePosixPath, _Picture] = {}
for document_path in _panoptic_files(src):
where = relative(src, document_path)
document = _load(document_path, where)
categories = _categories(document, where, losses, reported)
classes.update(dict.fromkeys(categories.values()))
image_dir = _image_dir(src, document_path)
by_id = _images(document, where, image_dir, pictures)
png_dir = _png_dir(src, document_path)
for row in _rows(document, "annotations", where):
self._annotation(src, root, row, where, png_dir, categories, by_id, losses)
return to_dataset(
(ImportedClass(name=name, geometries=(GeometryType.MASK,)) for name in classes),
(
ImportedImage(
path=one.path, width=one.width, height=one.height, labels=tuple(one.labels)
)
for one in pictures.values()
),
losses,
_LABEL,
)
def _annotation(
self,
src: Path,
root: Path,
row: Mapping[str, Any],
where: str,
png_dir: Path,
categories: Mapping[int, str],
by_id: Mapping[int, _Picture],
losses: Losses,
) -> None:
image_id = row.get("image_id")
picture = by_id.get(image_id) if _is_id(image_id) else None
if picture is None:
raise ImportSourceUnreadable(
f"{where}: an annotation names image id {image_id!r}, which its images do not list"
)
if picture.annotated:
raise ImportSourceUnreadable(
f"{where}: image {str(picture.path)!r} has two annotations; a picture has one "
"segment map"
)
picture.annotated = True
segments = _segments(row, where, str(picture.path))
file_name = row.get("file_name")
if not isinstance(file_name, str):
raise ImportSourceUnreadable(
f"{where}: the annotation of image {str(picture.path)!r} names no PNG file_name"
)
png = _png_path(src, root, png_dir, file_name, where)
shown = relative(src, png)
width, height, values = read_segment_map(png, shown)
if picture.width is None or picture.height is None:
picture.width, picture.height = width, height
if (width, height) != (picture.width, picture.height):
losses[
(
ImportLossKind.LABEL,
shown,
f"it is {width} by {height} and its picture {picture.width} by "
f"{picture.height}; its segments are not imported",
)
] += 1
return
try:
check_mask_size(height, width)
except MaskEncodingError as exc:
losses[(ImportLossKind.LABEL, shown, f"{exc}; its segments are not imported")] += 1
return
masks = split_class_map(values, height=height, width=width, ignore={VOID})
listed = {segment_id for segment_id, _ in segments}
unlisted = sorted(set(masks) - listed)
if unlisted:
raise ImportSourceUnreadable(
f"PNG {shown!r} holds segment ids {unlisted[:10]} that segments_info of image "
f"{str(picture.path)!r} in {where} does not list"
)
absent = sorted(listed - set(masks))
if absent:
raise ImportSourceUnreadable(
f"segments_info of image {str(picture.path)!r} in {where} lists segment ids "
f"{absent[:10]} that PNG {shown!r} does not hold"
)
if VOID in values:
losses[(ImportLossKind.LABEL, "void", _VOID_PIXELS)] += 1
for segment_id, segment in segments:
category_id = segment.get("category_id")
name = categories.get(category_id) if _is_id(category_id) else None
if name is None:
losses[(ImportLossKind.LABEL, "segment", _UNKNOWN_CATEGORY)] += 1
continue
if segment.get("iscrowd") == 1:
losses[(ImportLossKind.ATTRIBUTE, name, _CROWD)] += 1
for key in sorted(set(segment) - _SEGMENT_FIELDS):
reason = f"segment field {key[:40]!r} is not imported"
losses[(ImportLossKind.ATTRIBUTE, name, reason)] += 1
picture.labels.append(ImportedLabel(class_name=name, geometry=masks[segment_id]))
def read_segment_map(path: Path, shown: str) -> tuple[int, int, Sequence[int]]:
"""The PNG's width, height and row-major segment ids, ``R + 256 * G + 65536 * B``.
Raises:
ImportSourceUnreadable: the file is not a readable RGB PNG.
"""
from PIL import Image
try:
with Image.open(path) as image:
image_format, mode, (width, height) = image.format, image.mode, image.size
if image_format == "PNG" and mode == "RGB":
image.load()
data = image.tobytes()
except (OSError, ValueError, SyntaxError, Image.DecompressionBombError) as exc:
raise ImportSourceUnreadable(f"PNG {shown!r} is not readable: {exc}") from None
if image_format != "PNG":
raise ImportSourceUnreadable(f"segment map {shown!r} is a {image_format} file, not a PNG")
if mode != "RGB":
raise ImportSourceUnreadable(
f"PNG {shown!r} is a {mode} PNG; a panoptic segment map is RGB, each pixel's id "
"R + 256 * G + 65536 * B"
)
values = [
red | green << 8 | blue << 16
for red, green, blue in zip(data[0::3], data[1::3], data[2::3], strict=True)
]
return width, height, values
def _panoptic_files(src: Path) -> list[Path]:
for directory in (src / ANNOTATIONS_DIRNAME, src):
if not directory.is_dir():
continue
found = sorted(
path for path in directory.glob(f"{PANOPTIC_PREFIX}*.json") if path.is_file()
)
if found:
root = src.resolve()
return [contained(src, root, path) for path in found]
raise ImportSourceUnreadable(
f"the {_LABEL} source {src.name!r} holds no {PANOPTIC_PREFIX}*.json under "
f"{ANNOTATIONS_DIRNAME}/ or at its top, such as "
f"{ANNOTATIONS_DIRNAME}/{PANOPTIC_PREFIX}val2017.json beside the PNG directory "
f"{ANNOTATIONS_DIRNAME}/{PANOPTIC_PREFIX}val2017/"
)
#: Above this a document is refused before it is read.
MAX_DOCUMENT_BYTES: Final = 512 * 1024 * 1024
def _refuse_constant(name: str) -> float:
raise ImportSourceUnreadable(f"the {_LABEL} file holds the non-finite number {name}")
def _load(path: Path, where: str) -> Mapping[str, Any]:
try:
raw = read_capped(path, MAX_DOCUMENT_BYTES, where)
document = json.loads(raw.decode("utf-8"), parse_constant=_refuse_constant)
except (OSError, ValueError, RecursionError) as exc:
raise ImportSourceUnreadable(f"{where} is not readable JSON: {exc}") from None
if not isinstance(document, dict):
raise ImportSourceUnreadable(f"{where} is not a {_LABEL} document")
return document
def _rows(document: Mapping[str, Any], key: str, where: str) -> Iterable[Mapping[str, Any]]:
rows = document.get(key, [])
if not isinstance(rows, list) or not all(isinstance(row, dict) for row in rows):
raise ImportSourceUnreadable(f"{where}: {key!r} is not a list of objects")
return rows
def _is_id(value: object) -> TypeGuard[int]:
return isinstance(value, int) and not isinstance(value, bool)
def _categories(
document: Mapping[str, Any], where: str, losses: Losses, reported: set[tuple[str, str]]
) -> dict[int, str]:
categories: dict[int, str] = {}
for row in _rows(document, "categories", where):
category_id, name = row.get("id"), row.get("name")
if not _is_id(category_id):
raise ImportSourceUnreadable(f"{where}: category id {category_id!r} is not an integer")
if not isinstance(name, str) or not name.strip():
raise ImportSourceUnreadable(f"{where}: category {category_id} has no name")
if category_id in categories:
raise ImportSourceUnreadable(f"{where}: category id {category_id} repeats")
name = name.strip()
categories[category_id] = name
isthing = row.get("isthing", 1)
if not _is_id(isthing) or isthing not in (0, 1):
raise ImportSourceUnreadable(
f"{where}: category {name!r} has isthing {isthing!r}, which is not 0 or 1"
)
found: list[str] = []
if isthing == 0:
found.append(_STUFF)
supercategory = row.get("supercategory")
if supercategory is not None and supercategory != name:
found.append(_SUPERCATEGORY)
if row.get("color") is not None:
found.append(_COLOUR)
found.extend(
f"category field {key[:40]!r} is not imported"
for key in sorted(set(row) - _CATEGORY_FIELDS)
)
for reason in found:
if (name, reason) not in reported:
reported.add((name, reason))
losses[(ImportLossKind.ATTRIBUTE, name, reason)] += 1
return categories
def _images(
document: Mapping[str, Any],
where: str,
image_dir: PurePosixPath,
pictures: dict[PurePosixPath, _Picture],
) -> dict[int, _Picture]:
by_id: dict[int, _Picture] = {}
for row in _rows(document, "images", where):
image_id, file_name = row.get("id"), row.get("file_name")
if not _is_id(image_id):
raise ImportSourceUnreadable(f"{where}: image id {image_id!r} is not an integer")
if image_id in by_id:
raise ImportSourceUnreadable(f"{where}: image id {image_id} repeats")
if not isinstance(file_name, str):
raise ImportSourceUnreadable(f"{where}: image {image_id} has no file_name")
try:
path = require_relative_path(image_dir / require_relative_path(file_name))
except ValueError as exc:
raise ImportSourceUnreadable(f"{where}: {exc}") from None
if path in pictures:
raise ImportSourceUnreadable(f"{where}: image {str(path)!r} is listed twice")
picture = _Picture(
path=path, width=_dimension(row, "width"), height=_dimension(row, "height")
)
pictures[path] = by_id[image_id] = picture
return by_id
def _segments(
row: Mapping[str, Any], where: str, picture: str
) -> list[tuple[int, Mapping[str, Any]]]:
segments: list[tuple[int, Mapping[str, Any]]] = []
seen: set[int] = set()
for segment in _rows(row, "segments_info", f"{where}, image {picture!r}"):
segment_id = segment.get("id")
if not _is_id(segment_id) or not 1 <= segment_id <= MAX_SEGMENT_ID:
raise ImportSourceUnreadable(
f"{where}: image {picture!r} has segment id {segment_id!r}; a segment id is an "
f"integer from 1 to {MAX_SEGMENT_ID}, 0 being void"
)
if segment_id in seen:
raise ImportSourceUnreadable(
f"{where}: image {picture!r} lists segment id {segment_id} twice"
)
seen.add(segment_id)
segments.append((segment_id, segment))
return segments
def _png_path(src: Path, root: Path, png_dir: Path, file_name: str, where: str) -> Path:
try:
path = png_dir / require_relative_path(file_name)
except ValueError as exc:
raise ImportSourceUnreadable(f"{where}: {exc}") from None
if not path.is_file():
shown = relative(src, path) if path.is_relative_to(src) else file_name
raise ImportSourceUnreadable(
f"{where} names the segment PNG {shown!r}, which the source does not hold; the PNGs "
f"live in the directory named after the JSON file, beside it or at the source's top"
)
return contained(src, root, path)
def _image_dir(src: Path, document_path: Path) -> PurePosixPath:
name = document_path.stem.removeprefix(PANOPTIC_PREFIX)
candidates = [PurePosixPath(IMAGES_DIRNAME)]
if name:
candidates = [PurePosixPath(IMAGES_DIRNAME, name), *candidates, PurePosixPath(name)]
return next(
(directory for directory in candidates if (src / directory).is_dir()), PurePosixPath()
)
def _png_dir(src: Path, document_path: Path) -> Path:
beside = document_path.parent / document_path.stem
return beside if beside.is_dir() else src / document_path.stem
def _dimension(row: Mapping[str, Any], key: str) -> int | None:
value = row.get(key)
return value if _is_id(value) and value > 0 else None
__all__ = [
"ANNOTATIONS_DIRNAME",
"FORMAT_NAME",
"MAX_SEGMENT_ID",
"PANOPTIC_PREFIX",
"VOID",
"CocoPanopticImporter",
"read_segment_map",
]