-
Notifications
You must be signed in to change notification settings - Fork 512
/
imagenet_v2.py
167 lines (139 loc) · 5.87 KB
/
imagenet_v2.py
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
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import argparse
import tarfile
from pathlib import Path
from typing import Dict, Tuple
import torch
from corenet.data.datasets import DATASET_REGISTRY
from corenet.data.datasets.dataset_base import BaseImageDataset
from corenet.data.transforms import image_pil as T
from corenet.data.transforms.common import Compose
from corenet.utils import logger
from corenet.utils.download_utils import get_local_path
IMAGENETv2_SPLIT_LINK_MAP = {
"matched_frequency": {
"url": "https://s3-us-west-2.amazonaws.com/imagenetv2public/imagenetv2-matched-frequency.tar.gz",
"extracted_folder_name": "imagenetv2-matched-frequency-format-val",
},
"threshold_0.7": {
"url": "https://s3-us-west-2.amazonaws.com/imagenetv2public/imagenetv2-threshold0.7.tar.gz",
"extracted_folder_name": "imagenetv2-threshold0.7-format-val",
},
"top_images": {
"url": "https://s3-us-west-2.amazonaws.com/imagenetv2public/imagenetv2-top-images.tar.gz",
"extracted_folder_name": "imagenetv2-top-images-format-val",
},
}
@DATASET_REGISTRY.register(name="imagenet_v2", type="classification")
class Imagenetv2Dataset(BaseImageDataset):
"""
`ImageNetv2 Dataset <https://arxiv.org/abs/1902.10811>`_ for studying the robustness of models trained on ImageNet dataset
Args:
opts: command-line arguments
"""
def __init__(
self,
opts,
*args,
**kwargs,
) -> None:
super().__init__(opts=opts, *args, **kwargs)
if self.is_training:
logger.error(
"{} can only be used for evaluation".format(self.__class__.__name__)
)
split = getattr(opts, "dataset.imagenet_v2.split", None)
if split is None or split not in IMAGENETv2_SPLIT_LINK_MAP.keys():
logger.error(
"Please specify split for ImageNetv2. Supported ImageNetv2 splits are: {}".format(
IMAGENETv2_SPLIT_LINK_MAP.keys()
)
)
split_path = get_local_path(opts, path=IMAGENETv2_SPLIT_LINK_MAP[split]["url"])
with tarfile.open(split_path) as tf:
tf.extractall(self.root)
root = Path(
"{}/{}".format(
self.root,
IMAGENETv2_SPLIT_LINK_MAP[split]["extracted_folder_name"],
)
)
file_names = list(root.glob("**/*.jpeg"))
self.file_names = file_names
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
"""Add dataset-specific arguments to the parser."""
if cls != Imagenetv2Dataset:
# Don't re-register arguments in subclasses that don't override `add_arguments()`.
return parser
group = parser.add_argument_group(title=cls.__name__)
group.add_argument(
"--dataset.imagenet-v2.split",
type=str,
default="matched-frequency",
help="ImageNetv2 dataset. Possible choices are: {}".format(
[
f"{i + 1}: {split_name}"
for i, split_name in enumerate(IMAGENETv2_SPLIT_LINK_MAP.keys())
]
),
choices=IMAGENETv2_SPLIT_LINK_MAP.keys(),
)
return parser
def _validation_transforms(self, *args, **kwargs):
"""Data transforms during validation
Order of transform is Resize, CenterCrop, ToTensor
Returns:
An instance of `corenet.data.transforms.image_pil.BaseTransformation.`
"""
aug_list = [
T.Resize(opts=self.opts),
T.CenterCrop(opts=self.opts),
T.ToTensor(opts=self.opts),
]
return Compose(opts=self.opts, img_transforms=aug_list)
def __getitem__(self, sample_size_and_index: Tuple) -> Dict:
"""Returns the sample corresponding to the input sample index.
Returned sample is transformed into the size specified by the input.
Args:
sample_size_and_index: Tuple of the form (crop_size_h, crop_size_w, sample_index)
Shapes:
The output data dictionary contains three keys (samples, sample_id, and target). The values of these
keys has the following shapes:
data["samples"]: Shape is [Channels, Height, Width]
data["sample_id"]: Shape is 1
data["targets"]: Shape is 1
Returns:
A dictionary with `samples`, `sample_id` and `targets` as keys corresponding to input, index and label of
a sample, respectively.
"""
crop_size_h, crop_size_w, img_index = sample_size_and_index
# same for validation and evaluation
transform_fn = self.get_augmentation_transforms(size=(crop_size_h, crop_size_w))
# infer target label from the file name
# file names are organized as SPLIT_NAME-format-val/class_idx/*.jpg
# Example: All images in this folder (imagenetv2-matched-frequency-format-val/0/*.jpg) belong to class 0
img_path = str(self.file_names[img_index])
target = int(self.file_names[img_index].parent.name)
input_img = self.read_image_pil(img_path)
if input_img is None:
# Sometimes images are corrupt
# Skip such images
logger.log("Img index {} is possibly corrupt.".format(img_index))
input_tensor = torch.zeros(
size=(3, crop_size_h, crop_size_w), dtype=torch.float
)
target = -1
data = {"image": input_tensor}
else:
data = {"image": input_img}
data = transform_fn(data)
data["samples"] = data["image"]
data["targets"] = target
data["sample_id"] = img_index
return data
def __len__(self) -> int:
return len(self.file_names)