-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototypical_train.py
More file actions
287 lines (236 loc) · 8.65 KB
/
prototypical_train.py
File metadata and controls
287 lines (236 loc) · 8.65 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from prototypical_loss import prototypical_loss as loss_fn
from csv_dataloader import Dataset
from data_sample import CategoriesSampler
from protonet import ProtoNet
from parser_util import get_parser
from torch.utils.data import DataLoader
from tqdm import tqdm
import numpy as np
import torch
import os
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
#DATASET RELATED
import pandas as pd
import random
from pandas.core.indexes.datetimes import date_range
#data preprocess
df=pd.read_csv('fer2013.csv')
df['Usage'].unique()
print(len(df[df['Usage']=='Training']))
print(len(df[df['Usage']=='PublicTest']))
print(len(df[df['Usage']=='PrivateTest']))
df['pixelss']=[[int(y) for y in x.split()] for x in df['pixels']]
df_train=df[df['Usage']=='Training']
df_valid=df[df['Usage']=='PrivateTest']
df_test=df[df['Usage']=='PublicTest']
part={}
part['train']= list(range(0,len(df_train)))
part['valid']= list(range(0,len(df_valid)))
part['test']= list(range(0,len(df_test)))
N_WAY = 5
N_SHOT = 5
N_QUERY = 1
# function to get unique values
def unique(list1):
# initialize a null list
unique_list = []
# traverse for all elements
for x in list1:
# check if exists in unique_list or not
if x not in unique_list:
unique_list.append(x)
# print list
#for x in unique_list:
return unique_list
def init_seed(opt):
'''
Disable cudnn to maximize reproducibility
'''
torch.cuda.cudnn_enabled = False
np.random.seed(opt.manual_seed)
torch.manual_seed(opt.manual_seed)
torch.cuda.manual_seed(opt.manual_seed)
def init_dataloader(N_WAY, N_SHOT, N_QUERY,dataframe):
df=dataframe['emotion'].tolist()
classes = unique(df)
classes = list(range(0,len(classes)))
chosen_classes = random.sample(classes, N_WAY)
print(chosen_classes)
dataset = Dataset(dataframe, transforms=None)
sampler = CategoriesSampler(df, chosen_classes, 10,
N_SHOT + N_QUERY)
dataloader = DataLoader(dataset=dataset, batch_sampler=sampler,
num_workers=8, pin_memory=True)
return dataloader
def init_protonet(opt):
'''
Initialize the ProtoNet
'''
device = 'cuda:0' if torch.cuda.is_available() and opt.cuda else 'cpu'
model = ProtoNet().to(device)
return model
def init_optim(opt, model):
'''
Initialize optimizer
'''
return torch.optim.Adam(params=model.parameters(),
lr=opt.learning_rate)
def init_lr_scheduler(opt, optim):
'''
Initialize the learning rate scheduler
'''
return torch.optim.lr_scheduler.StepLR(optimizer=optim,
gamma=opt.lr_scheduler_gamma,
step_size=opt.lr_scheduler_step)
def save_list_to_file(path, thelist):
with open(path, 'w') as f:
for item in thelist:
f.write("%s\n" % item)
def train(opt, tr_dataloader, model, optim, lr_scheduler, val_dataloader=None):
'''
Train the model with the prototypical learning algorithm
'''
device = 'cuda:0' if torch.cuda.is_available() and opt.cuda else 'cpu'
if val_dataloader is None:
best_state = None
train_loss = []
train_acc = []
val_loss = []
val_acc = []
best_acc = 0
best_model_path = os.path.join(opt.experiment_root, 'best_model.pth')
last_model_path = os.path.join(opt.experiment_root, 'last_model.pth')
for epoch in range(opt.epochs):
print('=== Epoch: {} ==='.format(epoch))
tr_iter = iter(tr_dataloader)
model.train()
for batch in tqdm(tr_iter):
optim.zero_grad()
x, y = batch
x= x.float()
y = y.float()
x, y = x.to(device), y.to(device)
model_output = model(x)
loss, acc = loss_fn(model_output, target=y,
n_support=opt.num_support_tr)
loss.backward()
optim.step()
train_loss.append(loss.item())
train_acc.append(acc.item())
avg_loss = np.mean(train_loss[-opt.iterations:])
avg_acc = np.mean(train_acc[-opt.iterations:])
print('Avg Train Loss: {}, Avg Train Acc: {}'.format(avg_loss, avg_acc))
lr_scheduler.step()
if val_dataloader is None:
continue
val_iter = iter(val_dataloader)
model.eval()
for batch in val_iter:
x, y = batch
x= x.float()
y = y.float()
x, y = x.to(device), y.to(device)
model_output = model(x)
loss, acc = loss_fn(model_output, target=y,
n_support=opt.num_support_val)
val_loss.append(loss.item())
val_acc.append(acc.item())
avg_loss = np.mean(val_loss[-opt.iterations:])
avg_acc = np.mean(val_acc[-opt.iterations:])
postfix = ' (Best)' if avg_acc >= best_acc else ' (Best: {})'.format(
best_acc)
print('Avg Val Loss: {}, Avg Val Acc: {}{}'.format(
avg_loss, avg_acc, postfix))
if avg_acc >= best_acc:
torch.save(model.state_dict(), best_model_path)
best_acc = avg_acc
best_state = model.state_dict()
torch.save(model.state_dict(), last_model_path)
for name in ['train_loss', 'train_acc', 'val_loss', 'val_acc']:
save_list_to_file(os.path.join(opt.experiment_root,
name + '.txt'), locals()[name])
return best_state, best_acc, train_loss, train_acc, val_loss, val_acc
def test(opt, test_dataloader, model):
'''
Test the model trained with the prototypical learning algorithm
'''
device = 'cuda:0' if torch.cuda.is_available() and opt.cuda else 'cpu'
avg_acc = list()
for epoch in range(10):
test_iter = iter(test_dataloader)
for batch in test_iter:
x, y = batch
x= x.float()
y = y.float()
x, y = x.to(device), y.to(device)
model_output = model(x)
_, acc = loss_fn(model_output, target=y,
n_support=opt.num_support_val)
avg_acc.append(acc.item())
avg_acc = np.mean(avg_acc)
print('Test Acc: {}'.format(avg_acc))
return avg_acc
def eval(opt):
'''
Initialize everything and train
'''
options = get_parser().parse_args()
if torch.cuda.is_available() and not options.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
init_seed(options)
test_dataloader = init_dataset(options)[-1]
model = init_protonet(options)
model_path = os.path.join(opt.experiment_root, 'best_model.pth')
model.load_state_dict(torch.load(model_path))
test(opt=options,
test_dataloader=test_dataloader,
model=model)
def main():
'''
Initialize everything and train
'''
options = get_parser().parse_args()
if not os.path.exists(options.experiment_root):
os.makedirs(options.experiment_root)
if torch.cuda.is_available() and not options.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
init_seed(options)
tr_dataloader = init_dataloader(N_WAY, N_SHOT, N_QUERY,df_train)
val_dataloader = init_dataloader(N_WAY, N_SHOT, N_QUERY,df_valid)
# trainval_dataloader = init_dataloader(options, 'trainval')
test_dataloader = init_dataloader(N_WAY, N_SHOT, N_QUERY,df_test)
model = init_protonet(options)
optim = init_optim(options, model)
lr_scheduler = init_lr_scheduler(options, optim)
res = train(opt=options,
tr_dataloader=tr_dataloader,
val_dataloader=val_dataloader,
model=model,
optim=optim,
lr_scheduler=lr_scheduler)
best_state, best_acc, train_loss, train_acc, val_loss, val_acc = res
print('Testing with last model..')
test(opt=options,
test_dataloader=test_dataloader,
model=model)
model.load_state_dict(best_state)
print('Testing with best model..')
test(opt=options,
test_dataloader=test_dataloader,
model=model)
# optim = init_optim(options, model)
# lr_scheduler = init_lr_scheduler(options, optim)
# print('Training on train+val set..')
# train(opt=options,
# tr_dataloader=trainval_dataloader,
# val_dataloader=None,
# model=model,
# optim=optim,
# lr_scheduler=lr_scheduler)
# print('Testing final model..')
# test(opt=options,
# test_dataloader=test_dataloader,
# model=model)
if __name__ == '__main__':
main()