-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_mean.py
203 lines (167 loc) · 6.58 KB
/
train_mean.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
from pathlib import Path
import os
import argparse
import pdb
import time
import random
from tqdm import tqdm
import yaml
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
import torch
from torch.utils.data import DataLoader
import torch.optim as optim
from dataset import SceneDataset
import utils
from mean_model import load_model
# Add by myself, to solve the plt error
matplotlib.use('Agg')
parser = argparse.ArgumentParser(description='training networks')
parser.add_argument('--config_file', type=str, required=True)
parser.add_argument('--seed', type=int, default=0, required=False,
help='set the seed to reproduce result')
parser.add_argument('--cuda', type=int, default=0, required=False,
help='set the cuda device')
args = parser.parse_args()
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
with open(args.config_file, "r") as reader:
config = yaml.load(reader, Loader=yaml.FullLoader)
mean_std_audio = np.load(config["data"]["audio_norm"])
mean_audio = mean_std_audio["global_mean"]
std_audio = mean_std_audio["global_std"]
mean_std_video = np.load(config["data"]["video_norm"])
mean_video = mean_std_video["global_mean"]
std_video = mean_std_video["global_std"]
audio_transform = lambda x: (x - mean_audio) / std_audio
video_transform = lambda x: (x - mean_video) / std_video
tr_ds = SceneDataset(config["data"]["train"]["audio_feature"],
config["data"]["train"]["video_feature"],
audio_transform,
video_transform)
tr_dataloader = DataLoader(tr_ds, shuffle=True, **config["data"]["dataloader_args"])
cv_ds = SceneDataset(config["data"]["cv"]["audio_feature"],
config["data"]["cv"]["video_feature"],
audio_transform,
video_transform)
cv_dataloader = DataLoader(cv_ds, shuffle=False, **config["data"]["dataloader_args"])
model_cfg = config['model']
model = load_model(config['model_name'])(**model_cfg)
print(model)
output_dir = config["output_dir"]
Path(output_dir).mkdir(exist_ok=True, parents=True)
logging_writer = utils.getfile_outlogger(os.path.join(output_dir, "train.log"))
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device = "cuda:{}".format(args.cuda)
print('Using device {}'.format(device))
model = model.to(device)
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = getattr(optim, config["optimizer"]["type"])(
model.parameters(),
**config["optimizer"]["args"])
lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer,
**config["lr_scheduler"]["args"])
print('-----------start training-----------')
def train(epoch):
model.train()
train_loss = 0.
start_time = time.time()
count = len(tr_dataloader) * (epoch - 1)
loader = tqdm(tr_dataloader)
for batch_idx, batch in enumerate(loader):
count = count + 1
audio_feat = batch["audio_feat"].to(device)
video_feat = batch["video_feat"].to(device)
target = batch["target"].to(device)
# target [b_s] in [0, 9]
# training
optimizer.zero_grad()
logit = model(audio_feat, video_feat)
# logit [b_s, num_classes]
# 最后的outputlayer是直接2层nn.Linear, 没有sigmoid什么的,范围没框定
# pred = torch.argmax(logit, 1)
# 随着训练出现超过1的 [-0.24, -0.07, -0.16, 0.16, -0.27, 0.13, -0.35, -0.11, 0.47, -0.08]
loss = loss_fn(logit, target)
# loss_fn = torch.nn.CrossEntropyLoss()
loss.backward()
train_loss += loss.item()
optimizer.step()
if (batch_idx + 1) % 100 == 0:
elapsed = time.time() - start_time
print('| epoch {:3d} | {:5d}/{:5d} batches | ms/batch {:5.2f} | loss {:5.2f} |'.format(
epoch, batch_idx + 1, len(tr_dataloader),
elapsed * 1000 / (batch_idx + 1), loss.item()))
train_loss /= (batch_idx + 1)
logging_writer.info('-' * 99)
logging_writer.info('| epoch {:3d} | time: {:5.2f}s | training loss {:5.2f} |'.format(
epoch, (time.time() - start_time), train_loss))
return train_loss
def validate(epoch):
model.eval()
validation_loss = 0.
start_time = time.time()
# data loading
cv_loss = 0.
targets = []
preds = []
with torch.no_grad():
for batch_idx, batch in enumerate(cv_dataloader):
audio_feat = batch["audio_feat"].to(device)
video_feat = batch["video_feat"].to(device)
target = batch["target"].to(device)
logit = model(audio_feat, video_feat)
loss = loss_fn(logit, target)
pred = torch.argmax(logit, 1)
targets.append(target.cpu().numpy())
preds.append(pred.cpu().numpy())
cv_loss += loss.item()
cv_loss /= (batch_idx+1)
preds = np.concatenate(preds, axis=0)
targets = np.concatenate(targets, axis=0)
accuracy = accuracy_score(targets, preds)
logging_writer.info('| epoch {:3d} | time: {:5.2f}s | cv loss {:5.2f} | cv accuracy: {:5.2f} |'.format(
epoch, time.time() - start_time, cv_loss, accuracy))
logging_writer.info('-' * 99)
return cv_loss
training_loss = []
cv_loss = []
with open(os.path.join(output_dir, 'config.yaml'), "w") as writer:
yaml.dump(config, writer, default_flow_style=False)
not_improve_cnt = 0
for epoch in range(1, config["epoch"]):
training_loss.append(train(epoch))
cv_loss.append(validate(epoch))
print('-' * 99)
print('epoch', epoch, 'training loss: ', training_loss[-1], 'cv loss: ', cv_loss[-1])
if cv_loss[-1] == np.min(cv_loss):
# save current best model
torch.save(model.state_dict(), os.path.join(output_dir, 'best_model.pt'))
print('best validation model found and saved.')
print('-' * 99)
not_improve_cnt = 0
else:
not_improve_cnt += 1
lr_scheduler.step(cv_loss[-1])
if not_improve_cnt == config["early_stop"]:
print('Use early stop')
break
print('End training')
print('Start plt')
minmum_cv_index = np.argmin(cv_loss)
minmum_loss = np.min(cv_loss)
plt.plot(training_loss, 'r')
#plt.hold(True)
plt.plot(cv_loss, 'b')
plt.axvline(x=minmum_cv_index, color='k', linestyle='--')
plt.plot(minmum_cv_index, minmum_loss,'r*')
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train_loss', 'val_loss'], loc='upper left')
plt.savefig(os.path.join(output_dir, 'loss.png'))