forked from VITA-Group/AutoSpeech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_verification.py
179 lines (145 loc) · 5.75 KB
/
train_verification.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
# -*- coding: utf-8 -*-
# @Date : 2019-08-09
# @Author : Xinyu Gong ([email protected])
# @Link : None
# @Version : 0.0
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import numpy as np
import shutil
import os
from pathlib import Path
from tensorboardX import SummaryWriter
from tqdm import tqdm
import torch
import torch.optim as optim
import torch.nn as nn
import torch.backends.cudnn as cudnn
from models.model import Network
from config import cfg, update_config
from utils import set_path, create_logger, save_checkpoint, count_parameters, Genotype
from data_objects.DeepSpeakerDataset import DeepSpeakerDataset
from data_objects.VoxcelebTestset import VoxcelebTestset
from functions import train_from_scratch, validate_verification
from loss import CrossEntropyLoss
def parse_args():
parser = argparse.ArgumentParser(description='Train energy network')
# general
parser.add_argument('--cfg',
help='experiment configure file name',
required=True,
type=str)
parser.add_argument('opts',
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER)
parser.add_argument('--load_path',
help="The path to resumed dir",
default=None)
parser.add_argument('--text_arch',
help="The text to arch",
default=None)
args = parser.parse_args()
return args
def main():
args = parse_args()
update_config(cfg, args)
assert args.text_arch
# cudnn related setting
cudnn.benchmark = cfg.CUDNN.BENCHMARK
torch.backends.cudnn.deterministic = cfg.CUDNN.DETERMINISTIC
torch.backends.cudnn.enabled = cfg.CUDNN.ENABLED
# Set the random seed manually for reproducibility.
np.random.seed(cfg.SEED)
torch.manual_seed(cfg.SEED)
torch.cuda.manual_seed_all(cfg.SEED)
# Loss
criterion = CrossEntropyLoss(cfg.MODEL.NUM_CLASSES).cuda()
# load arch
genotype = eval(args.text_arch)
model = Network(cfg.MODEL.INIT_CHANNELS, cfg.MODEL.NUM_CLASSES, cfg.MODEL.LAYERS, genotype)
model = model.cuda()
optimizer = optim.Adam(
model.parameters(),
lr=cfg.TRAIN.LR
)
# resume && make log dir and logger
if args.load_path and os.path.exists(args.load_path):
checkpoint_file = os.path.join(args.load_path, 'Model', 'checkpoint_best.pth')
assert os.path.exists(checkpoint_file)
checkpoint = torch.load(checkpoint_file)
# load checkpoint
begin_epoch = checkpoint['epoch']
last_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['state_dict'])
best_eer = checkpoint['best_eer']
optimizer.load_state_dict(checkpoint['optimizer'])
args.path_helper = checkpoint['path_helper']
logger = create_logger(args.path_helper['log_path'])
logger.info("=> loaded checkloggpoint '{}'".format(checkpoint_file))
else:
exp_name = args.cfg.split('/')[-1].split('.')[0]
args.path_helper = set_path('logs_scratch', exp_name)
logger = create_logger(args.path_helper['log_path'])
begin_epoch = cfg.TRAIN.BEGIN_EPOCH
best_eer = 1.0
last_epoch = -1
logger.info(args)
logger.info(cfg)
logger.info(f"selected architecture: {genotype}")
logger.info("Number of parameters: {}".format(count_parameters(model)))
# dataloader
train_dataset = DeepSpeakerDataset(
Path(cfg.DATASET.DATA_DIR), cfg.DATASET.SUB_DIR, cfg.DATASET.PARTIAL_N_FRAMES)
train_loader = torch.utils.data.DataLoader(
dataset=train_dataset,
batch_size=cfg.TRAIN.BATCH_SIZE,
num_workers=cfg.DATASET.NUM_WORKERS,
pin_memory=True,
shuffle=True,
drop_last=True,
)
test_dataset_verification = VoxcelebTestset(
Path(cfg.DATASET.DATA_DIR), cfg.DATASET.PARTIAL_N_FRAMES)
test_loader_verification = torch.utils.data.DataLoader(
dataset=test_dataset_verification,
batch_size=1,
num_workers=cfg.DATASET.NUM_WORKERS,
pin_memory=True,
shuffle=False,
drop_last=False,
)
# training setting
writer_dict = {
'writer': SummaryWriter(args.path_helper['log_path']),
'train_global_steps': begin_epoch * len(train_loader),
'valid_global_steps': begin_epoch // cfg.VAL_FREQ,
}
# training loop
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer, cfg.TRAIN.END_EPOCH, cfg.TRAIN.LR_MIN,
last_epoch=last_epoch
)
for epoch in tqdm(range(begin_epoch, cfg.TRAIN.END_EPOCH), desc='train progress'):
model.train()
model.drop_path_prob = cfg.MODEL.DROP_PATH_PROB * epoch / cfg.TRAIN.END_EPOCH
train_from_scratch(cfg, model, optimizer, train_loader, criterion, epoch, writer_dict)
if epoch % cfg.VAL_FREQ == 0 or epoch == cfg.TRAIN.END_EPOCH - 1:
eer = validate_verification(cfg, model, test_loader_verification)
# remember best acc@1 and save checkpoint
is_best = eer < best_eer
best_eer = min(eer, best_eer)
# save
logger.info('=> saving checkpoint to {}'.format(args.path_helper['ckpt_path']))
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'best_eer': best_eer,
'optimizer': optimizer.state_dict(),
'path_helper': args.path_helper
}, is_best, args.path_helper['ckpt_path'], 'checkpoint_{}.pth'.format(epoch))
lr_scheduler.step(epoch)
if __name__ == '__main__':
main()