-
Notifications
You must be signed in to change notification settings - Fork 15
/
data_utility.py
142 lines (120 loc) · 4.03 KB
/
data_utility.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
import dataset
import torch
from collections import defaultdict
from combine_sampler import CombineSampler
import numpy as np
import pickle
def create_loaders(data_root, num_classes, is_extracted, num_workers, num_classes_iter, num_elements_class, size_batch):
Dataset = dataset.Birds(
root=data_root,
labels=list(range(0, num_classes)),
is_extracted=is_extracted,
transform=dataset.utils.make_transform())
ddict = defaultdict(list)
for idx, label in enumerate(Dataset.ys):
ddict[label].append(idx)
list_of_indices_for_each_class = []
for key in ddict:
list_of_indices_for_each_class.append(ddict[key])
dl_tr = torch.utils.data.DataLoader(
Dataset,
batch_size=size_batch,
shuffle=False,
sampler=CombineSampler(list_of_indices_for_each_class, num_classes_iter, num_elements_class),
num_workers=num_workers,
drop_last=True,
pin_memory=True
)
if data_root == 'Stanford':
class_end = 2 * num_classes - 2
else:
class_end = 2 * num_classes
dl_ev = torch.utils.data.DataLoader(
dataset.Birds(
root=data_root,
labels=list(range(num_classes, class_end)),
is_extracted=is_extracted,
transform=dataset.utils.make_transform(is_train=False)
),
batch_size=50,
shuffle=False,
num_workers=1,
pin_memory=True
)
dl_finetune = torch.utils.data.DataLoader(
dataset.Birds(
root=data_root,
labels=list(range(num_classes)),
is_extracted=is_extracted,
transform=dataset.utils.make_transform(is_train=False)
),
batch_size=size_batch,
shuffle=True,
num_workers=num_workers,
pin_memory=True
)
dl_train_evaluate = torch.utils.data.DataLoader(
dataset.Birds(
root=data_root,
labels=list(range(num_classes)),
is_extracted=is_extracted,
transform=dataset.utils.make_transform(is_train=False)
),
batch_size=150,
shuffle=False,
num_workers=1,
pin_memory=True
)
return dl_tr, dl_ev, dl_finetune, dl_train_evaluate
def create_loaders_finetune(data_root, num_classes, is_extracted, num_workers, size_batch):
if data_root == 'Stanford':
class_end = 2 * num_classes - 2
else:
class_end = 2 * num_classes
dl_ev = torch.utils.data.DataLoader(
dataset.Birds(
root=data_root,
labels=list(range(num_classes, class_end)),
is_extracted=is_extracted,
transform=dataset.utils.make_transform(is_train=False)
),
batch_size=150,
shuffle=False,
num_workers=1,
pin_memory=True
)
dl_finetune = torch.utils.data.DataLoader(
dataset.Birds(
root=data_root,
labels=list(range(num_classes)),
is_extracted=is_extracted,
transform=dataset.utils.make_transform(is_train=False)
),
batch_size=size_batch,
shuffle=True,
num_workers=num_workers,
pin_memory=True
)
return dl_ev, dl_finetune
def get_labeled_and_unlabeled_points(labels, num_points_per_class, num_classes=100):
labs, L, U = [], [], []
labs_buffer = np.zeros(num_classes)
num_points = labels.shape[0]
for i in range(num_points):
if labs_buffer[labels[i]] == num_points_per_class:
U.append(i)
else:
L.append(i)
labs.append(labels[i])
labs_buffer[labels[i]] += 1
return labs, L, U
def debug_info(gtg, model):
for name, param in gtg.named_parameters():
if param.requires_grad:
if param.grad is not None:
print(name, torch.mean(param.grad.data))
for name, param in model.named_parameters():
if param.requires_grad:
if param.grad is not None:
print(name, torch.mean(param.grad.data))
print("\n\n\n")