-
Notifications
You must be signed in to change notification settings - Fork 3
/
data.py
176 lines (144 loc) · 5.98 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
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
import os
import random
import numpy as np
import h5py
import PIL
from PIL import Image
import torch
import torch.nn as nn
from torchvision import datasets, transforms
from torch.utils.data.sampler import SubsetRandomSampler
import utils
torch.manual_seed(1234)
torch.cuda.manual_seed(1234)
random.seed(1234)
np.random.seed(1234)
class DomainNetDataset(torch.utils.data.Dataset):
def __init__(self, name, domain, split, transforms):
self.name = 'DomainNet'
self.domain = domain
self.split = split
self.file_path = os.path.join('/srv/testing/prithvi/', 'cond-comp-dg', \
'DomainNet-Large-tv_1.0', '{}_{}.h5'.format(self.domain, self.split))
self.data, self.labels = None, None
with h5py.File(self.file_path, 'r') as file:
self.dataset_len = len(file["images"])
self.num_classes = len(set(list(np.array(file['labels']))))
self.transforms = transforms
def __len__(self):
return self.dataset_len
def __getitem__(self, idx):
if self.data is None:
self.data = h5py.File(self.file_path, 'r')["images"]
self.labels = h5py.File(self.file_path, 'r')["labels"]
datum, label = Image.fromarray(np.uint8(np.array(self.data[idx]))), np.array(self.labels[idx])
return (self.transforms(datum), int(label))
def get_num_classes(self):
# return self.num_classes
#! Hardcoded
return 345
class ASDADataset:
# Active Semi-supervised DA Dataset class
def __init__(self, name, data_dir='data', valid_ratio=0.2, batch_size=128, augment=False):
self.name = name
self.data_dir = data_dir
self.valid_ratio = valid_ratio
self.batch_size = batch_size
self.train_size = None
self.train_dataset = None
self.num_classes = None
def get_num_classes(self):
return self.num_classes
def get_dsets(self, normalize=True, apply_transforms=True):
if self.name == "mnist":
mean, std = 0.5, 0.5
normalize_transform = transforms.Normalize((mean,), (std,)) \
if normalize else transforms.Normalize((0,), (1,))
train_transforms = transforms.Compose([
transforms.ToTensor(),
normalize_transform
])
test_transforms = transforms.Compose([
transforms.ToTensor(),
normalize_transform
])
train_dataset = datasets.MNIST(self.data_dir, train=True, download=True, transform=train_transforms)
val_dataset = datasets.MNIST(self.data_dir, train=True, download=True, transform=test_transforms)
test_dataset = datasets.MNIST(self.data_dir, train=False, download=True, transform=test_transforms)
train_dataset.name, val_dataset.name, test_dataset.name = 'DIGITS','DIGITS', 'DIGITS'
self.num_classes = 10
elif self.name == "svhn":
mean, std = 0.5, 0.5
normalize_transform = transforms.Normalize((mean,), (std,)) \
if normalize else transforms.Normalize((0,), (1,))
RGB2Gray = transforms.Lambda(lambda x: x.convert('L'))
train_transforms = transforms.Compose([
RGB2Gray,
transforms.Resize((28, 28)),
transforms.ToTensor(),
normalize_transform
])
test_transforms = transforms.Compose([
RGB2Gray,
transforms.Resize((28, 28)),
transforms.ToTensor(),
normalize_transform
])
train_dataset = datasets.SVHN(self.data_dir, split='train', download=True, transform=train_transforms)
val_dataset = datasets.SVHN(self.data_dir, split='train', download=True, transform=test_transforms)
test_dataset = datasets.SVHN(self.data_dir, split='test', download=True, transform=test_transforms)
self.num_classes = 10
elif self.name in ["real", "quickdraw", "sketch", "infograph", "clipart", "painting"]:
normalize_transform = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) \
if normalize else transforms.Normalize([0, 0, 0], [1, 1, 1])
if apply_transforms:
data_transforms = {
'train': transforms.Compose([
transforms.Resize(256),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize_transform
]),
}
else:
data_transforms = {
'train': transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
normalize_transform
]),
}
data_transforms['test'] = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
normalize_transform
])
train_dataset = DomainNetDataset('DomainNet', self.name, 'train', data_transforms['train'])
val_dataset = DomainNetDataset('DomainNet', self.name, 'val', data_transforms['test'])
test_dataset = DomainNetDataset('DomainNet', self.name, 'test', data_transforms['test'])
self.num_classes = train_dataset.get_num_classes()
self.train_dataset = train_dataset
self.val_dataset = val_dataset
self.test_dataset = test_dataset
return train_dataset, val_dataset, test_dataset
def get_loaders(self, shuffle=True, num_workers=4, normalize=True):
if not self.train_dataset: self.get_dsets(normalize=normalize)
num_train = len(self.train_dataset)
self.train_size = num_train
if self.name in ["mnist", "svhn"]:
indices = list(range(num_train))
split = int(np.floor(self.valid_ratio * num_train))
if shuffle == True: np.random.shuffle(indices)
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
elif self.name in ["real", "quickdraw", "sketch", "infograph", "painting", "clipart"]:
train_idx = np.arange(len(self.train_dataset))
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(np.arange(len(self.val_dataset)))
train_loader = torch.utils.data.DataLoader(self.train_dataset, sampler=train_sampler, \
batch_size=self.batch_size, num_workers=num_workers)
val_loader = torch.utils.data.DataLoader(self.val_dataset, sampler=valid_sampler, batch_size=self.batch_size)
test_loader = torch.utils.data.DataLoader(self.test_dataset, batch_size=self.batch_size)
return train_loader, val_loader, test_loader, train_idx