forked from CVI-SZU/ME-GraphAU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_stage1.py
148 lines (122 loc) · 6.02 KB
/
train_stage1.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
import os
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import torch.optim as optim
from tqdm import tqdm
import logging
from model.ANFL import MEFARG
from dataset import *
from utils import *
from conf import get_config,set_logger,set_outdir,set_env
def get_dataloader(conf):
print('==> Preparing data...')
if conf.dataset == 'BP4D':
trainset = BP4D(conf.dataset_path, train=True, fold = conf.fold, transform=image_train(crop_size=conf.crop_size), crop_size=conf.crop_size, stage = 1)
train_loader = DataLoader(trainset, batch_size=conf.batch_size, shuffle=True, num_workers=conf.num_workers)
valset = BP4D(conf.dataset_path, train=False, fold=conf.fold, transform=image_test(crop_size=conf.crop_size), stage = 1)
val_loader = DataLoader(valset, batch_size=conf.batch_size, shuffle=False, num_workers=conf.num_workers)
elif conf.dataset == 'DISFA':
trainset = DISFA(conf.dataset_path, train=True, fold = conf.fold, transform=image_train(crop_size=conf.crop_size), crop_size=conf.crop_size, stage = 1)
train_loader = DataLoader(trainset, batch_size=conf.batch_size, shuffle=True, num_workers=conf.num_workers)
valset = DISFA(conf.dataset_path, train=False, fold=conf.fold, transform=image_test(crop_size=conf.crop_size), stage = 1)
val_loader = DataLoader(valset, batch_size=conf.batch_size, shuffle=False, num_workers=conf.num_workers)
return train_loader, val_loader, len(trainset), len(valset)
# Train
def train(conf,net,train_loader,optimizer,epoch,criterion):
losses = AverageMeter()
net.train()
train_loader_len = len(train_loader)
for batch_idx, (inputs, targets) in enumerate(tqdm(train_loader)):
adjust_learning_rate(optimizer, epoch, conf.epochs, conf.learning_rate, batch_idx, train_loader_len)
targets = targets.float()
if torch.cuda.is_available():
inputs, targets = inputs.cuda(), targets.cuda()
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
losses.update(loss.data.item(), inputs.size(0))
return losses.avg
# Val
def val(net,val_loader,criterion):
losses = AverageMeter()
net.eval()
statistics_list = None
for batch_idx, (inputs, targets) in enumerate(tqdm(val_loader)):
with torch.no_grad():
targets = targets.float()
if torch.cuda.is_available():
inputs, targets = inputs.cuda(), targets.cuda()
outputs = net(inputs)
loss = criterion(outputs, targets)
losses.update(loss.data.item(), inputs.size(0))
update_list = statistics(outputs, targets.detach(), 0.5)
statistics_list = update_statistics_list(statistics_list, update_list)
mean_f1_score, f1_score_list = calc_f1_score(statistics_list)
mean_acc, acc_list = calc_acc(statistics_list)
return losses.avg, mean_f1_score, f1_score_list, mean_acc, acc_list
def main(conf):
if conf.dataset == 'BP4D':
dataset_info = BP4D_infolist
elif conf.dataset == 'DISFA':
dataset_info = DISFA_infolist
start_epoch = 0
# data
train_loader,val_loader,train_data_num,val_data_num = get_dataloader(conf)
train_weight = torch.from_numpy(np.loadtxt(os.path.join(conf.dataset_path, 'list', conf.dataset+'_weight_fold'+str(conf.fold)+'.txt')))
logging.info("Fold: [{} | {} val_data_num: {} ]".format(conf.fold, conf.N_fold, val_data_num))
net = MEFARG(num_classes=conf.num_classes, backbone=conf.arc, neighbor_num=conf.neighbor_num, metric=conf.metric)
# resume
if conf.resume != '':
logging.info("Resume form | {} ]".format(conf.resume))
net = load_state_dict(net, conf.resume)
if torch.cuda.is_available():
net = nn.DataParallel(net).cuda()
train_weight = train_weight.cuda()
criterion = WeightedAsymmetricLoss(weight=train_weight)
optimizer = optim.AdamW(net.parameters(), betas=(0.9, 0.999), lr=conf.learning_rate, weight_decay=conf.weight_decay)
print('the init learning rate is ', conf.learning_rate)
#train and val
for epoch in range(start_epoch, conf.epochs):
lr = optimizer.param_groups[0]['lr']
logging.info("Epoch: [{} | {} LR: {} ]".format(epoch + 1, conf.epochs, lr))
train_loss = train(conf,net,train_loader,optimizer,epoch,criterion)
val_loss, val_mean_f1_score, val_f1_score, val_mean_acc, val_acc = val(net, val_loader, criterion)
# log
infostr = {'Epoch: {} train_loss: {:.5f} val_loss: {:.5f} val_mean_f1_score {:.2f},val_mean_acc {:.2f}'
.format(epoch + 1, train_loss, val_loss, 100.* val_mean_f1_score, 100.* val_mean_acc)}
logging.info(infostr)
infostr = {'F1-score-list:'}
logging.info(infostr)
infostr = dataset_info(val_f1_score)
logging.info(infostr)
infostr = {'Acc-list:'}
logging.info(infostr)
infostr = dataset_info(val_acc)
logging.info(infostr)
# save checkpoints
if (epoch+1) % 4 == 0:
checkpoint = {
'epoch': epoch,
'state_dict': net.state_dict(),
'optimizer': optimizer.state_dict(),
}
torch.save(checkpoint, os.path.join(conf['outdir'], 'epoch' + str(epoch + 1) + '_model_fold' + str(conf.fold) + '.pth'))
checkpoint = {
'epoch': epoch,
'state_dict': net.state_dict(),
'optimizer': optimizer.state_dict(),
}
torch.save(checkpoint, os.path.join(conf['outdir'], 'cur_model_fold' + str(conf.fold) + '.pth'))
# ---------------------------------------------------------------------------------
if __name__=="__main__":
conf = get_config()
set_env(conf)
# generate outdir name
set_outdir(conf)
# Set the logger
set_logger(conf)
main(conf)