-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier_embed_contras.py
207 lines (188 loc) · 8.24 KB
/
classifier_embed_contras.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.optim as optim
import numpy as np
import util
from sklearn.preprocessing import MinMaxScaler
import sys
class CLASSIFIER:
# train_Y is interger
def __init__(self, _train_X, _train_Y, map_net, embed_size, data_loader, _nclass, _cuda,hs_dim, _lr=0.001, _beta1=0.5, _nepoch=20, _batch_size=100, generalized=True):
self.train_X = _train_X
self.train_Y = _train_Y
self.test_seen_feature = data_loader.test_seen_feature
self.test_seen_label = data_loader.test_seen_label
self.test_unseen_feature = data_loader.test_unseen_feature
self.test_unseen_label = data_loader.test_unseen_label
self.seenclasses = data_loader.seenclasses
self.unseenclasses = data_loader.unseenclasses
self.MapNet=map_net
self.batch_size = _batch_size
self.nepoch = _nepoch
self.nclass = _nclass
self.input_dim = hs_dim
self.cuda = _cuda
self.model = LINEAR_LOGSOFTMAX(self.input_dim, self.nclass)
self.model.apply(util.weights_init)
self.criterion = nn.NLLLoss()
self.input = torch.FloatTensor(_batch_size, _train_X.size(1))
self.label = torch.LongTensor(_batch_size)
self.lr = _lr
self.beta1 = _beta1
# setup optimizer
self.optimizer = optim.Adam(self.model.parameters(), lr=_lr, betas=(_beta1, 0.999))
if self.cuda:
self.model.cuda()
self.criterion.cuda()
self.input = self.input.cuda()
self.label = self.label.cuda()
self.index_in_epoch = 0
self.epochs_completed = 0
self.ntrain = self.train_X.size()[0]
if generalized:
self.acc_seen, self.acc_unseen, self.H = self.fit()
else:
self.acc = self.fit_zsl()
def fit_zsl(self):
best_acc = 0
mean_loss = 0
for epoch in range(self.nepoch):
for i in range(0, self.ntrain, self.batch_size):
self.model.zero_grad()
batch_input, batch_label = self.next_batch(self.batch_size)
self.input.copy_(batch_input)
self.label.copy_(batch_label)
embed, _=self.MapNet(self.input)
output = self.model(embed)
loss = self.criterion(output, self.label)
mean_loss += loss.data
loss.backward()
self.optimizer.step()
acc = self.val(self.test_unseen_feature, self.test_unseen_label, self.unseenclasses)
if acc > best_acc:
best_acc = acc
print('Training classifier loss= %.4f' % (loss))
return best_acc
def fit(self):
best_H = 0
best_seen = 0
best_unseen = 0
for epoch in range(self.nepoch):
for i in range(0, self.ntrain, self.batch_size):
self.model.zero_grad()
batch_input, batch_label = self.next_batch(self.batch_size)
self.input.copy_(batch_input)
self.label.copy_(batch_label)
# embed, _ = self.MapNet(self.input)
hs, hu, z, _, _ = self.MapNet(self.input)
# _, embed = self.MapNet(self.input)
output = self.model(z)
loss = self.criterion(output, self.label)
loss.backward()
self.optimizer.step()
acc_seen = self.val_gzsl(self.test_seen_feature, self.test_seen_label, self.seenclasses)
acc_unseen = self.val_gzsl(self.test_unseen_feature, self.test_unseen_label, self.unseenclasses)
if (acc_seen+acc_unseen)==0:
print('a bug')
H=0
else:
H = 2*acc_seen*acc_unseen / (acc_seen+acc_unseen)
if H > best_H:
best_seen = acc_seen
best_unseen = acc_unseen
best_H = H
return best_seen, best_unseen, best_H
def next_batch(self, batch_size):
start = self.index_in_epoch
# shuffle the data at the first epoch
if self.epochs_completed == 0 and start == 0:
perm = torch.randperm(self.ntrain)
self.train_X = self.train_X[perm]
self.train_Y = self.train_Y[perm]
# the last batch
if start + batch_size > self.ntrain:
self.epochs_completed += 1
rest_num_examples = self.ntrain - start
if rest_num_examples > 0:
X_rest_part = self.train_X[start:self.ntrain]
Y_rest_part = self.train_Y[start:self.ntrain]
# shuffle the data
perm = torch.randperm(self.ntrain)
self.train_X = self.train_X[perm]
self.train_Y = self.train_Y[perm]
# start next epoch
start = 0
self.index_in_epoch = batch_size - rest_num_examples
end = self.index_in_epoch
X_new_part = self.train_X[start:end]
Y_new_part = self.train_Y[start:end]
#print(start, end)
if rest_num_examples > 0:
return torch.cat((X_rest_part, X_new_part), 0) , torch.cat((Y_rest_part, Y_new_part), 0)
else:
return X_new_part, Y_new_part
else:
self.index_in_epoch += batch_size
end = self.index_in_epoch
#print(start, end)
# from index start to index end-1
return self.train_X[start:end], self.train_Y[start:end]
def val_gzsl(self, test_X, test_label, target_classes):
start = 0
ntest = test_X.size()[0]
predicted_label = torch.LongTensor(test_label.size())
for i in range(0, ntest, self.batch_size):
end = min(ntest, start+self.batch_size)
with torch.no_grad():
if self.cuda:
# _, embed = self.MapNet(test_X[start:end].cuda())
hs, hu, z, _, _ = self.MapNet(test_X[start:end].cuda())
# output = self.model(embed)
output = self.model(z.cuda())
else:
embed, _ = self.MapNet(test_X[start:end])
output = self.model(embed)
_, predicted_label[start:end] = torch.max(output, 1)
start = end
acc = self.compute_per_class_acc_gzsl(test_label, predicted_label, target_classes)
return acc
def compute_per_class_acc_gzsl(self, test_label, predicted_label, target_classes):
acc_per_class = 0
for i in target_classes:
idx = (test_label == i)
acc_per_class += float(torch.sum(test_label[idx] == predicted_label[idx])) / float(torch.sum(idx))
acc_per_class /= target_classes.size(0)
return acc_per_class
# test_label is integer
def val(self, test_X, test_label, target_classes):
start = 0
ntest = test_X.size()[0]
predicted_label = torch.LongTensor(test_label.size())
for i in range(0, ntest, self.batch_size):
end = min(ntest, start+self.batch_size)
with torch.no_grad():
if self.cuda:
embed, _ = self.MapNet(test_X[start:end].cuda())
output = self.model(embed)
else:
embed, _ = self.MapNet(test_X[start:end])
output = self.model(embed)
_, predicted_label[start:end] = torch.max(output, 1)
start = end
acc = self.compute_per_class_acc(util.map_label(test_label, target_classes), predicted_label, target_classes.size(0))
return acc
def compute_per_class_acc(self, test_label, predicted_label, nclass):
acc_per_class = torch.FloatTensor(nclass).fill_(0)
for i in range(nclass):
idx = (test_label == i)
acc_per_class[i] = float(torch.sum(test_label[idx]==predicted_label[idx])) / float(torch.sum(idx))
return acc_per_class.mean()
class LINEAR_LOGSOFTMAX(nn.Module):
def __init__(self, input_dim, nclass):
super(LINEAR_LOGSOFTMAX, self).__init__()
self.fc = nn.Linear(input_dim, nclass)
self.logic = nn.LogSoftmax(dim=1)
def forward(self, x):
o = self.logic(self.fc(x))
return o