forked from cdb342/IJCAI-2022-ZLA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
137 lines (123 loc) · 6.42 KB
/
util.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
import numpy as np
import scipy.io as sio
import torch
from sklearn import preprocessing
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
m.weight.data.normal_(0.0, 0.02)
m.bias.data.fill_(0)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
def map_label(label, classes):
mapped_label = torch.LongTensor(label.size())
for i in range(classes.size(0)):
mapped_label[label==classes[i]] = i
return mapped_label
class Logger(object):
def __init__(self, filename):
self.filename = filename
f = open(self.filename+'.log', "a")
f.close()
def write(self, message):
f = open(self.filename+'.log', "a")
f.write(message)
f.close()
class DATA_LOADER(object):
def __init__(self, opt):
if opt.matdataset:
if opt.dataset == 'imageNet1K':
self.read_matimagenet(opt)
else:
self.read_matdataset(opt)
self.index_in_epoch = 0
self.epochs_completed = 0
def read_matdataset(self, opt):
matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/" + opt.image_embedding + ".mat")
feature = matcontent['features'].T
label = matcontent['labels'].astype(int).squeeze() - 1
matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/" + opt.class_embedding + "_splits.mat")
# numpy array index starts from 0, matlab starts from 1
trainval_loc = matcontent['trainval_loc'].squeeze() - 1
# train_loc = matcontent['train_loc'].squeeze() - 1
# val_unseen_loc = matcontent['val_loc'].squeeze() - 1
test_seen_loc = matcontent['test_seen_loc'].squeeze() - 1
test_unseen_loc = matcontent['test_unseen_loc'].squeeze() - 1
self.attribute = torch.from_numpy(matcontent['att'].T).float()
if not opt.validation:
if opt.preprocessing:
if opt.standardization:
print('standardization...')
scaler = preprocessing.StandardScaler()
else:
scaler = preprocessing.MinMaxScaler()
_train_feature = scaler.fit_transform(feature[trainval_loc])
_test_seen_feature = scaler.transform(feature[test_seen_loc])
_test_unseen_feature = scaler.transform(feature[test_unseen_loc])
self.train_feature = torch.from_numpy(_train_feature).float()
mx = self.train_feature.max()
self.train_feature.mul_(1/mx)
self.train_label = torch.from_numpy(label[trainval_loc]).long()
self.test_unseen_feature = torch.from_numpy(_test_unseen_feature).float()
self.test_unseen_feature.mul_(1/mx)
self.test_unseen_label = torch.from_numpy(label[test_unseen_loc]).long()
self.test_seen_feature = torch.from_numpy(_test_seen_feature).float()
self.test_seen_feature.mul_(1/mx)
self.test_seen_label = torch.from_numpy(label[test_seen_loc]).long()
else:
self.train_feature = torch.from_numpy(feature[trainval_loc]).float()
self.train_label = torch.from_numpy(label[trainval_loc]).long()
self.test_unseen_feature = torch.from_numpy(feature[test_unseen_loc]).float()
self.test_unseen_label = torch.from_numpy(label[test_unseen_loc]).long()
self.test_seen_feature = torch.from_numpy(feature[test_seen_loc]).float()
self.test_seen_label = torch.from_numpy(label[test_seen_loc]).long()
else:
self.train_feature = torch.from_numpy(feature[train_loc]).float()
self.train_label = torch.from_numpy(label[train_loc]).long()
self.test_unseen_feature = torch.from_numpy(feature[val_unseen_loc]).float()
self.test_unseen_label = torch.from_numpy(label[val_unseen_loc]).long()
self.seenclasses = torch.from_numpy(np.unique(self.train_label.numpy()))
self.unseenclasses = torch.from_numpy(np.unique(self.test_unseen_label.numpy()))
self.ntrain = self.train_feature.size()[0]
self.ntrain_class = self.seenclasses.size(0)
self.ntest_class = self.unseenclasses.size(0)
self.train_class = self.seenclasses.clone()
self.allclasses = torch.arange(0, self.ntrain_class+self.ntest_class).long()
self.train_mapped_label = map_label(self.train_label, self.seenclasses)
def next_batch_one_class(self, batch_size):
if self.index_in_epoch == self.ntrain_class:
self.index_in_epoch = 0
perm = torch.randperm(self.ntrain_class)
self.train_class[perm] = self.train_class[perm]
iclass = self.train_class[self.index_in_epoch]
idx = self.train_label.eq(iclass).nonzero().squeeze()
perm = torch.randperm(idx.size(0))
idx = idx[perm]
iclass_feature = self.train_feature[idx]
iclass_label = self.train_label[idx]
self.index_in_epoch += 1
return iclass_feature[0:batch_size], iclass_label[0:batch_size], self.attribute[iclass_label[0:batch_size]]
def next_batch(self, batch_size):
idx = torch.randperm(self.ntrain)[0:batch_size]
batch_feature = self.train_feature[idx]
batch_label = self.train_label[idx]
return batch_feature, batch_label
# select batch samples by randomly drawing batch_size classes
def next_batch_uniform_class(self, batch_size):
batch_class = torch.LongTensor(batch_size)
for i in range(batch_size):
idx = torch.randperm(self.ntrain_class)[0]
batch_class[i] = self.train_class[idx]
batch_feature = torch.FloatTensor(batch_size, self.train_feature.size(1))
batch_label = torch.LongTensor(batch_size)
batch_att = torch.FloatTensor(batch_size, self.attribute.size(1))
for i in range(batch_size):
iclass = batch_class[i]
idx_iclass = self.train_label.eq(iclass).nonzero().squeeze()
idx_in_iclass = torch.randperm(idx_iclass.size(0))[0]
idx_file = idx_iclass[idx_in_iclass]
batch_feature[i] = self.train_feature[idx_file]
batch_label[i] = self.train_label[idx_file]
batch_att[i] = self.attribute[batch_label[i]]
return batch_feature, batch_label, batch_att