forked from Fezaries/CRnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCUB_train.py
194 lines (134 loc) · 5.58 KB
/
CUB_train.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
import torch
import torch.nn as nn
import torch.tensor as tensor
from torch.utils.data import DataLoader,TensorDataset
import numpy as np
import scipy.io as sio
from sklearn.cluster import KMeans
from net import CooperationModule, RelationModule
from utils import Logger, evaluate
import sys
import time
sys.stdout = Logger('log/'+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+'.log')
with open('CUB_train.py') as f:
contents = f.read()
print(contents)
f.close()
###############################################################################
K = 4
BATCH_SIZE = 32
EPISODE = 130000
TEST_EPISODE = 1000
LEARNING_RATE = 1e-5
GPU = 0
###############################################################################
# step 1: init dataset
print("init dataset")
dataroot = './data'
dataset = 'CUB'
image_embedding = 'res101'
class_embedding = 'att_splits'
matcontent = sio.loadmat(dataroot + "/" + dataset + "/" + image_embedding + ".mat")
feature = matcontent['features'].T
label = matcontent['labels'].astype(int).squeeze() - 1
matcontent = sio.loadmat(dataroot + "/" + dataset + "/" + class_embedding + ".mat")
# numpy array index starts from 0, matlab starts from 1
trainval_loc = matcontent['trainval_loc'].squeeze() - 1
test_seen_loc = matcontent['test_seen_loc'].squeeze() - 1
test_unseen_loc = matcontent['test_unseen_loc'].squeeze() - 1
attribute = matcontent['original_att'].T
x = feature[trainval_loc] # train_features
train_label_ori = label[trainval_loc].astype(int) # train_label
att = attribute[train_label_ori] # train attributes
att = torch.tensor(att).float().cuda()
x_test = feature[test_unseen_loc] # test_feature
test_label = label[test_unseen_loc].astype(int) # test_label
x_test_seen = feature[test_seen_loc] #test_seen_feature
test_label_seen = label[test_seen_loc].astype(int) # test_seen_label
test_id = np.unique(test_label) # test_id
att_pro = attribute[test_id] # test_attribute
# train set
train_features=torch.from_numpy(x)
print(train_features.shape)
train_label=torch.from_numpy(train_label_ori).unsqueeze(1)
print(train_label.shape)
# attributes
all_attributes=np.array(attribute)
print(all_attributes.shape)
attributes = torch.from_numpy(attribute)
# test set
test_features=torch.from_numpy(x_test)
print(test_features.shape)
test_label=torch.from_numpy(test_label).unsqueeze(1)
print(test_label.shape)
testclasses_id = np.array(test_id)
print(testclasses_id.shape)
test_attributes = torch.from_numpy(att_pro).float()
print(test_attributes.shape)
test_seen_features = torch.from_numpy(x_test_seen)
print(test_seen_features.shape)
test_seen_label = torch.from_numpy(test_label_seen)
train_data = TensorDataset(train_features,train_label)
###########################################################################
# initial algorithm
print("init networks")
att = np.unique(att,axis=0)
kmeans = KMeans(n_clusters=K, random_state=1337).fit(att) ### field number : K=4
att = kmeans.cluster_centers_
att = torch.tensor(att).float().cuda()
CM = CooperationModule(att)
RM = RelationModule()
CM.cuda(GPU)
RM.cuda(GPU)
CM_optim = torch.optim.Adam(CM.parameters(),lr=LEARNING_RATE, weight_decay=1e-5)
RM_optim = torch.optim.Adam(RM.parameters(),lr=LEARNING_RATE, weight_decay=0)
print("training...")
last_H = 0.0
for episode in range(EPISODE):
train_loader = DataLoader(train_data,batch_size=BATCH_SIZE,shuffle=True)
batch_features,batch_labels = train_loader.__iter__().next()
sample_labels = []
for label in batch_labels.numpy():
if label not in sample_labels:
sample_labels.append(label)
sample_attributes = torch.Tensor([all_attributes[i] for i in sample_labels]).squeeze(1)
class_num = sample_attributes.shape[0]
batch_features = tensor(batch_features).cuda(GPU).float() # 32*2048
sample_features = CM(tensor(sample_attributes).cuda(GPU)) # x*312
sample_features_ext = sample_features.unsqueeze(0).repeat(BATCH_SIZE,1,1)
batch_features_ext = batch_features.unsqueeze(0).repeat(class_num,1,1)
batch_features_ext = torch.transpose(batch_features_ext,0,1)
relation_pairs = torch.cat((sample_features_ext,batch_features_ext),2).view(-1,4096)
relations = RM(relation_pairs).view(-1,class_num)
# re-build batch_labels according to sample_labels
sample_labels = np.array(sample_labels)
re_batch_labels = []
for label in batch_labels.numpy():
index = np.argwhere(sample_labels==label)
re_batch_labels.append(index[0][0])
re_batch_labels = torch.LongTensor(re_batch_labels)
# loss
mse = nn.MSELoss().cuda(GPU)
one_hot_labels = tensor(torch.zeros(BATCH_SIZE, class_num).scatter_(1, re_batch_labels.view(-1,1), 1)).cuda(GPU)
loss = mse(relations,one_hot_labels)
# update
CM.zero_grad()
RM.zero_grad()
loss.backward()
CM_optim.step()
RM_optim.step()
if (episode+1)%100 == 0:
print("episode:",episode+1,"loss %.4f" % loss.item())
if (episode+1)%2000 == 0:
# test
print("Testing...")
gzsl_unseen_accuracy = evaluate(test_features,test_label,np.arange(200),attributes, CM, RM, GPU)
gzsl_seen_accuracy = evaluate(test_seen_features,test_seen_label,np.arange(200),attributes, CM, RM, GPU)
H = 2 * gzsl_seen_accuracy * gzsl_unseen_accuracy / (gzsl_unseen_accuracy + gzsl_seen_accuracy)
print('seen=%.4f, unseen=%.4f, h=%.4f' % (gzsl_seen_accuracy, gzsl_unseen_accuracy, H))
if H > last_H:
# save networks
torch.save(CM,"./models/CUB_CM.pkl")
torch.save(RM,"./models/CUB_RM.pkl")
print("save networks for episode:",episode)
last_H = H