-
Notifications
You must be signed in to change notification settings - Fork 19
/
dataset.py
58 lines (42 loc) · 1.53 KB
/
dataset.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from torchvision.datasets import CIFAR100, CIFAR10
from PIL import Image
from typing import Any, Tuple
class CIFAR100_idx(CIFAR100):
def __getitem__(self, index: int) -> Tuple[Any, Any, Any]:
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
img, target = self.data[index], self.targets[index]
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return index, img, target
class CIFAR10_idx(CIFAR10):
def __getitem__(self, index: int) -> Tuple[Any, Any, Any]:
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
img, target = self.data[index], self.targets[index]
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return index, img, target
def dataset_with_indices(cls):
def __getitem__(self, index):
data, target = cls.__getitem__(self, index)
return index, data, target
return type(cls.__name__, (cls,), {
'__getitem__': __getitem__,
})