-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
140 lines (116 loc) · 4.62 KB
/
data.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
from typing import Any
import numpy as np
import ignite.distributed as idist
import torchvision
# import torchvision.transforms as T
from torchvision import transforms
from torch.utils.data import DataLoader, Subset
def setup_data(config: Any, is_test = False, few_shot_num = None):
"""Download datasets and create dataloaders
Parameters
----------
config: needs to contain `data_path`, `train_batch_size`, `eval_batch_size`, and `num_workers`
"""
local_rank = idist.get_local_rank()
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
)
train_transform = transforms.Compose(
[
transforms.Resize((256, 256)),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]
)
test_transform = transforms.Compose(
[
transforms.Resize((256, 256)),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
]
)
if local_rank > 0:
# Ensure that only rank 0 download the dataset
idist.barrier()
if config.dataset.name == "cifar10":
if is_test:
raise NotImplementedError("Cifar10 test not implemented.")
train_dataset = torchvision.datasets.CIFAR10(
root=config.data_path,
train=True,
download=False,
transform=train_transform,
)
val_dataset = torchvision.datasets.CIFAR10(
root=config.data_path,
train=False,
download=False,
transform=test_transform,
)
else:
name = config.dataset.name
name = name.replace("-", "")
print(name)
import importlib
dataset_module = importlib.import_module(f"dataloader.{name}")
namespace = vars(dataset_module)
public = (name for name in namespace if name[:1] != "_")
matches = [n for n in public if name.lower() == n.lower()]
print(matches)
assert len(matches) == 1
dataset_cls = getattr(dataset_module, matches[0])
train_dataset = dataset_cls(
config.dataset.root, config.dataset.domain, transform=train_transform
)
val_dataset = dataset_cls(
config.dataset.root, config.dataset.domain, transform=test_transform
)
assert len(train_dataset) == len(val_dataset)
if is_test: # few-shot on target domain
if few_shot_num is None:
raise ValueError("few_shot_num should be specified for test dataset.")
# cnt = [ [] for _ in range(val_dataset.num_classes) ] # CAN'T DO THIS, because task has been divied
cnt = [ [] for _ in range(config.dataset.num_classes) ]
indecies = np.random.permutation(len(val_dataset.targets))
# for i, v in enumerate(val_dataset.targets):
for i in indecies:
v = val_dataset.targets[i]
if len(cnt[v]) < few_shot_num:
cnt[v].append(i)
for i in cnt:
assert len(i) == few_shot_num
# turn cnt into numpy array and flatten it
train_indices = np.array(cnt).flatten()
val_indices = np.array([i for i in range(len(val_dataset)) if i not in train_indices])
np.random.shuffle(val_indices) # np.random.randint() or sample
if config.val_sample_num is not None:
val_indices = val_indices[:config.val_sample_num]
train_dataset = Subset(train_dataset, train_indices)
val_dataset = Subset(val_dataset, val_indices)
else:
# split the dataset with indices
indices = np.random.permutation(len(train_dataset))
num_train = int(len(train_dataset) * config.data.train_ratio)
train_dataset = Subset(train_dataset, indices[:num_train])
val_dataset = Subset(val_dataset, indices[num_train:])
if local_rank == 0:
# Ensure that only rank 0 download the dataset
idist.barrier()
# dataloader_train = idist.auto_dataloader(
dataloader_train = DataLoader(
train_dataset,
batch_size=config.train_batch_size,
shuffle=False,
# shuffle=True,
num_workers=config.num_workers,
)
dataloader_eval = DataLoader(
val_dataset,
batch_size=config.eval_batch_size,
shuffle=False,
num_workers=config.num_workers,
)
return dataloader_train, dataloader_eval