forked from mikehuisman/subspace-adaptation-prior
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_min.py
231 lines (181 loc) · 8.86 KB
/
custom_min.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
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
import os
import pickle
import h5py
import json
from PIL import Image
from torchmeta.utils.data import Dataset, ClassDataset, CombinationMetaDataset
# QKFIX: See torchmeta.datasets.utils for more informations
from torchmeta.datasets.utils import download_file_from_google_drive
class CustomMiniImagenet(CombinationMetaDataset):
"""
The Mini-Imagenet dataset, introduced in [1]. This dataset contains images
of 100 different classes from the ILSVRC-12 dataset (Imagenet challenge).
The meta train/validation/test splits are taken from [2] for reproducibility.
Parameters
----------
root : string
Root directory where the dataset folder `miniimagenet` exists.
num_classes_per_task : int
Number of classes per tasks. This corresponds to "N" in "N-way"
classification.
meta_train : bool (default: `False`)
Use the meta-train split of the dataset. If set to `True`, then the
arguments `meta_val` and `meta_test` must be set to `False`. Exactly one
of these three arguments must be set to `True`.
meta_val : bool (default: `False`)
Use the meta-validation split of the dataset. If set to `True`, then the
arguments `meta_train` and `meta_test` must be set to `False`. Exactly one
of these three arguments must be set to `True`.
meta_test : bool (default: `False`)
Use the meta-test split of the dataset. If set to `True`, then the
arguments `meta_train` and `meta_val` must be set to `False`. Exactly one
of these three arguments must be set to `True`.
meta_split : string in {'train', 'val', 'test'}, optional
Name of the split to use. This overrides the arguments `meta_train`,
`meta_val` and `meta_test` if all three are set to `False`.
transform : callable, optional
A function/transform that takes a `PIL` image, and returns a transformed
version. See also `torchvision.transforms`.
target_transform : callable, optional
A function/transform that takes a target, and returns a transformed
version. See also `torchvision.transforms`.
dataset_transform : callable, optional
A function/transform that takes a dataset (ie. a task), and returns a
transformed version of it. E.g. `torchmeta.transforms.ClassSplitter()`.
class_augmentations : list of callable, optional
A list of functions that augment the dataset with new classes. These classes
are transformations of existing classes. E.g.
`torchmeta.transforms.HorizontalFlip()`.
download : bool (default: `False`)
If `True`, downloads the pickle files and processes the dataset in the root
directory (under the `miniimagenet` folder). If the dataset is already
available, this does not download/process the dataset again.
Notes
-----
The dataset is downloaded from [this repository]
(https://github.com/renmengye/few-shot-ssl-public/). The meta train/
validation/test splits are over 64/16/20 classes.
References
----------
.. [1] Vinyals, O., Blundell, C., Lillicrap, T. and Wierstra, D. (2016).
Matching Networks for One Shot Learning. In Advances in Neural
Information Processing Systems (pp. 3630-3638) (https://arxiv.org/abs/1606.04080)
.. [2] Ravi, S. and Larochelle, H. (2016). Optimization as a Model for
Few-Shot Learning. (https://openreview.net/forum?id=rJY0-Kcll)
"""
def __init__(self, root, num_classes_per_task=None, meta_train=False,
meta_val=False, meta_test=False, meta_split=None,
transform=None, target_transform=None, dataset_transform=None,
class_augmentations=None, download=False):
dataset = MiniImagenetClassDataset(root, meta_train=meta_train,
meta_val=meta_val, meta_test=meta_test, meta_split=meta_split,
transform=transform, class_augmentations=class_augmentations,
download=download)
super(CustomMiniImagenet, self).__init__(dataset, num_classes_per_task,
target_transform=target_transform, dataset_transform=dataset_transform)
class MiniImagenetClassDataset(ClassDataset):
folder = 'miniimagenet'
# Google Drive ID from https://github.com/renmengye/few-shot-ssl-public
gdrive_id = '16V_ZlkW4SsnNDtnGmaBRq2OoPmUOc5mY'
gz_filename = 'mini-imagenet.tar.gz'
gz_md5 = 'b38f1eb4251fb9459ecc8e7febf9b2eb'
pkl_filename = 'mini-imagenet-cache-{0}.pkl'
filename = '{0}_data.hdf5'
filename_labels = '{0}_labels.json'
def __init__(self, root, meta_train=False, meta_val=False, meta_test=False,
meta_split=None, transform=None, class_augmentations=None,
download=False):
super(MiniImagenetClassDataset, self).__init__(meta_train=meta_train,
meta_val=meta_val, meta_test=meta_test, meta_split=meta_split,
class_augmentations=class_augmentations)
self.root = os.path.join(os.path.expanduser(root), self.folder)
self.transform = transform
self.split_filename = os.path.join(self.root,
self.filename.format(self.meta_split))
self.split_filename_labels = os.path.join(self.root,
self.filename_labels.format(self.meta_split))
self._data = None
self._labels = None
if download:
self.download()
if not self._check_integrity():
raise RuntimeError('MiniImagenet integrity check failed')
self._num_classes = len(self.labels)
def __getitem__(self, index):
class_name = self.labels[index % self.num_classes]
data = self.data[class_name]
transform = self.get_transform(index, self.transform)
target_transform = self.get_target_transform(index)
return MiniImagenetDataset(index, data, class_name,
transform=transform, target_transform=target_transform)
@property
def num_classes(self):
return self._num_classes
@property
def data(self):
if self._data is None:
self._data_file = h5py.File(self.split_filename, 'r')
self._data = self._data_file['datasets']
return self._data
@property
def labels(self):
if self._labels is None:
with open(self.split_filename_labels, 'r') as f:
self._labels = json.load(f)
return self._labels
def _check_integrity(self):
return (os.path.isfile(self.split_filename)
and os.path.isfile(self.split_filename_labels))
def close(self):
if self._data_file is not None:
self._data_file.close()
self._data_file = None
self._data = None
def download(self):
import tarfile
if self._check_integrity():
return
download_file_from_google_drive(self.gdrive_id, self.root,
self.gz_filename, md5=self.gz_md5)
filename = os.path.join(self.root, self.gz_filename)
with tarfile.open(filename, 'r') as f:
f.extractall(self.root)
for split in ['train', 'val', 'test']:
filename = os.path.join(self.root, self.filename.format(split))
if os.path.isfile(filename):
continue
pkl_filename = os.path.join(self.root, self.pkl_filename.format(split))
if not os.path.isfile(pkl_filename):
raise IOError()
with open(pkl_filename, 'rb') as f:
data = pickle.load(f)
images, classes = data['image_data'], data['class_dict']
with h5py.File(filename, 'w') as f:
group = f.create_group('datasets')
for name, indices in classes.items():
group.create_dataset(name, data=images[indices])
labels_filename = os.path.join(self.root, self.filename_labels.format(split))
with open(labels_filename, 'w') as f:
labels = sorted(list(classes.keys()))
json.dump(labels, f)
if os.path.isfile(pkl_filename):
os.remove(pkl_filename)
class MiniImagenetDataset(Dataset):
def __init__(self, index, data, class_name,
transform=None, target_transform=None):
super(MiniImagenetDataset, self).__init__(index, transform=transform,
target_transform=target_transform)
self.data = data
self.class_name = class_name
self.class_idx = index
#print("class idx:", self.class_idx)
def __len__(self):
return self.data.shape[0]
def __getitem__(self, index):
image = Image.fromarray(self.data[index])
target = self.class_name
if self.transform is not None:
image = self.transform(image)
if self.target_transform is not None:
target = self.target_transform(target)
return (image, target, self.class_idx)