|
| 1 | +from collections import defaultdict |
| 2 | +from csv import DictReader, reader as TupleReader |
| 3 | +from pathlib import Path |
| 4 | +from typing import Dict, List, Any |
| 5 | +import warnings |
| 6 | + |
| 7 | +from taming.data.annotated_objects_dataset import AnnotatedObjectsDataset |
| 8 | +from taming.data.helper_types import Annotation, Category |
| 9 | +from tqdm import tqdm |
| 10 | + |
| 11 | +OPEN_IMAGES_STRUCTURE = { |
| 12 | + 'train': { |
| 13 | + 'top_level': '', |
| 14 | + 'class_descriptions': 'class-descriptions-boxable.csv', |
| 15 | + 'annotations': 'oidv6-train-annotations-bbox.csv', |
| 16 | + 'file_list': 'train-images-boxable.csv', |
| 17 | + 'files': 'train' |
| 18 | + }, |
| 19 | + 'validation': { |
| 20 | + 'top_level': '', |
| 21 | + 'class_descriptions': 'class-descriptions-boxable.csv', |
| 22 | + 'annotations': 'validation-annotations-bbox.csv', |
| 23 | + 'file_list': 'validation-images.csv', |
| 24 | + 'files': 'validation' |
| 25 | + }, |
| 26 | + 'test': { |
| 27 | + 'top_level': '', |
| 28 | + 'class_descriptions': 'class-descriptions-boxable.csv', |
| 29 | + 'annotations': 'test-annotations-bbox.csv', |
| 30 | + 'file_list': 'test-images.csv', |
| 31 | + 'files': 'test' |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +def load_annotations(descriptor_path: Path, min_object_area: float, category_no_for_id: Dict[str, int]) -> \ |
| 37 | + Dict[str, List[Annotation]]: |
| 38 | + annotations: Dict[str, List[Annotation]] = defaultdict(list) |
| 39 | + with open(descriptor_path) as file: |
| 40 | + reader = DictReader(file) |
| 41 | + for i, row in tqdm(enumerate(reader), total=14620000, desc='Loading OpenImages annotations'): |
| 42 | + width = float(row['XMax']) - float(row['XMin']) |
| 43 | + height = float(row['YMax']) - float(row['YMin']) |
| 44 | + area = width * height |
| 45 | + category_id = row['LabelName'] |
| 46 | + if area >= min_object_area and category_id in category_no_for_id: |
| 47 | + annotations[row['ImageID']].append( |
| 48 | + Annotation( |
| 49 | + id=i, |
| 50 | + image_id=row['ImageID'], |
| 51 | + source=row['Source'], |
| 52 | + category_id=category_id, |
| 53 | + category_no=category_no_for_id[category_id], |
| 54 | + confidence=float(row['Confidence']), |
| 55 | + bbox=(float(row['XMin']), float(row['YMin']), width, height), |
| 56 | + area=area, |
| 57 | + is_occluded=bool(int(row['IsOccluded'])), |
| 58 | + is_truncated=bool(int(row['IsTruncated'])), |
| 59 | + is_group_of=bool(int(row['IsGroupOf'])), |
| 60 | + is_depiction=bool(int(row['IsDepiction'])), |
| 61 | + is_inside=bool(int(row['IsInside'])) |
| 62 | + ) |
| 63 | + ) |
| 64 | + if 'train' in str(descriptor_path) and i < 14000000: |
| 65 | + warnings.warn(f'Running with subset of Open Images. Train dataset has length [{len(annotations)}].') |
| 66 | + return dict(annotations) |
| 67 | + |
| 68 | + |
| 69 | +def load_image_ids(csv_path: Path) -> List[str]: |
| 70 | + with open(csv_path) as file: |
| 71 | + reader = DictReader(file) |
| 72 | + return [row['image_name'] for row in reader] |
| 73 | + |
| 74 | + |
| 75 | +def load_categories(csv_path: Path) -> Dict[str, Category]: |
| 76 | + with open(csv_path) as file: |
| 77 | + reader = TupleReader(file) |
| 78 | + return {row[0]: Category(id=row[0], name=row[1], super_category=None) for row in reader} |
| 79 | + |
| 80 | + |
| 81 | +class AnnotatedObjectsOpenImages(AnnotatedObjectsDataset): |
| 82 | + def __init__(self, **kwargs): |
| 83 | + """ |
| 84 | + @param data_path: is the path to the following folder structure: |
| 85 | + open_images/ |
| 86 | + │ oidv6-train-annotations-bbox.csv |
| 87 | + ├── class-descriptions-boxable.csv |
| 88 | + ├── oidv6-train-annotations-bbox.csv |
| 89 | + ├── test |
| 90 | + │ ├── 000026e7ee790996.jpg |
| 91 | + │ ├── 000062a39995e348.jpg |
| 92 | + │ └── ... |
| 93 | + ├── test-annotations-bbox.csv |
| 94 | + ├── test-images.csv |
| 95 | + ├── train |
| 96 | + │ ├── 000002b66c9c498e.jpg |
| 97 | + │ ├── 000002b97e5471a0.jpg |
| 98 | + │ └── ... |
| 99 | + ├── train-images-boxable.csv |
| 100 | + ├── validation |
| 101 | + │ ├── 0001eeaf4aed83f9.jpg |
| 102 | + │ ├── 0004886b7d043cfd.jpg |
| 103 | + │ └── ... |
| 104 | + ├── validation-annotations-bbox.csv |
| 105 | + └── validation-images.csv |
| 106 | + @param: split: one of 'train', 'validation' or 'test' |
| 107 | + @param: desired image size (returns square images) |
| 108 | + """ |
| 109 | + |
| 110 | + super().__init__(**kwargs) |
| 111 | + |
| 112 | + self.categories = load_categories(self.paths['class_descriptions']) |
| 113 | + self.filter_categories() |
| 114 | + self.setup_category_id_and_number() |
| 115 | + |
| 116 | + self.image_descriptions = {} |
| 117 | + annotations = load_annotations(self.paths['annotations'], self.min_object_area, self.category_number) |
| 118 | + self.annotations = self.filter_object_number(annotations, self.min_object_area, self.min_objects_per_image, |
| 119 | + self.max_objects_per_image) |
| 120 | + self.image_ids = list(self.annotations.keys()) |
| 121 | + self.clean_up_annotations_and_image_descriptions() |
| 122 | + |
| 123 | + def get_path_structure(self) -> Dict[str, str]: |
| 124 | + if self.split not in OPEN_IMAGES_STRUCTURE: |
| 125 | + raise ValueError(f'Split [{self.split} does not exist for Open Images data.]') |
| 126 | + return OPEN_IMAGES_STRUCTURE[self.split] |
| 127 | + |
| 128 | + def get_image_path(self, image_id: str) -> Path: |
| 129 | + return self.paths['files'].joinpath(f'{image_id:0>16}.jpg') |
| 130 | + |
| 131 | + def get_image_description(self, image_id: str) -> Dict[str, Any]: |
| 132 | + return {'file_path': str(self.get_image_path(image_id))} |
0 commit comments