-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
353 lines (184 loc) · 9.95 KB
/
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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import logging
import os
import sys
import argparse
from datetime import datetime
import numpy as np
from tqdm import tqdm
import torch
from torch.optim import Adam, SGD
from torch.utils.data import DataLoader
from sklearn.metrics import mean_absolute_error
from dataloader.MicroLens100k.dataset import MyData, custom_collate_fn
from model.MicroLens100k.MMRA import Model
import random
from functools import partial
BLUE = '\033[94m'
ENDC = '\033[0m'
def seed_init(seed):
seed = int(seed)
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def print_init_msg(logger, args):
logger.info(BLUE + 'Random Seed: ' + ENDC + f"{args.seed} ")
logger.info(BLUE + 'Device: ' + ENDC + f"{args.device} ")
logger.info(BLUE + 'Model: ' + ENDC + f"{args.model_id} ")
logger.info(BLUE + "Dataset: " + ENDC + f"{args.dataset_id}")
logger.info(BLUE + "Metric: " + ENDC + f"{args.metric}")
logger.info(BLUE + "Optimizer: " + ENDC + f"{args.optim}(lr = {args.lr})")
logger.info(BLUE + "Total Epoch: " + ENDC + f"{args.epochs} Turns")
logger.info(BLUE + "Early Stop: " + ENDC + f"{args.early_stop_turns} Turns")
logger.info(BLUE + "Batch Size: " + ENDC + f"{args.batch_size}")
logger.info(BLUE + "Number of retrieved items used in this training: " + ENDC + f"{args.num_of_retrieved_items}")
logger.info(BLUE + "Alpha: " + ENDC + f"{args.alpha}")
logger.info(BLUE + "Number of frames: " + ENDC + f"{args.frame_num}")
logger.info(BLUE + "Training Starts!" + ENDC)
def make_saving_folder_and_logger(args):
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
folder_name = f"train_{args.model_id}_{args.dataset_id}_{args.metric}_{timestamp}"
father_folder_name = args.save
if not os.path.exists(father_folder_name):
os.makedirs(father_folder_name)
folder_path = os.path.join(father_folder_name, folder_name)
os.mkdir(folder_path)
os.mkdir(os.path.join(folder_path, "trained_model"))
logger = logging.getLogger()
logger.handlers = []
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
file_handler = logging.FileHandler(f'{father_folder_name}/{folder_name}/log.txt')
file_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.addHandler(file_handler)
return father_folder_name, folder_name, logger
def delete_model(father_folder_name, folder_name, min_turn):
model_name_list = os.listdir(f"{father_folder_name}/{folder_name}/trained_model")
for i in range(len(model_name_list)):
if model_name_list[i] != f'model_{min_turn}.pth':
os.remove(os.path.join(f'{father_folder_name}/{folder_name}/trained_model', model_name_list[i]))
def force_stop(msg):
print(msg)
sys.exit(1)
def delete_special_tokens(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
content = content.replace(BLUE, '')
content = content.replace(ENDC, '')
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
def train_val(args):
father_folder_name, folder_name, logger = make_saving_folder_and_logger(args)
device = torch.device(args.device)
custom_collate_fn_partial = partial(custom_collate_fn, num_of_retrieved_items=args.num_of_retrieved_items,
num_of_frames=args.frame_num)
train_data = MyData(os.path.join(args.dataset_path, args.dataset_id, 'train.pkl'))
valid_data = MyData(os.path.join(os.path.join(args.dataset_path, args.dataset_id, 'valid.pkl')))
train_data_loader = DataLoader(dataset=train_data, batch_size=args.batch_size, collate_fn=custom_collate_fn_partial)
valid_data_loader = DataLoader(dataset=valid_data, batch_size=args.batch_size, collate_fn=custom_collate_fn_partial)
model = Model(feature_dim=args.feature_dim, alpha=args.alpha, frame_num=args.frame_num)
model = model.to(device)
if args.loss == 'BCE':
loss_fn = torch.nn.BCELoss()
elif args.loss == 'MSE':
loss_fn = torch.nn.MSELoss()
else:
force_stop('Invalid parameter loss!')
loss_fn.to(device)
if args.optim == 'Adam':
optim = Adam(model.parameters(), args.lr)
elif args.optim == 'SGD':
optim = SGD(model.parameters(), args.lr)
else:
force_stop('Invalid parameter optim!')
min_total_valid_loss = 1008611
min_turn = 0
print_init_msg(logger, args)
for i in range(args.epochs):
logger.info(f"-----------------------------------Epoch {i + 1} Start!-----------------------------------")
min_train_loss, total_valid_loss = run_one_epoch(model, loss_fn, optim, train_data_loader, valid_data_loader,
device)
logger.info(f"[ Epoch {i + 1} (train) ]: loss = {min_train_loss}")
logger.info(f"[ Epoch {i + 1} (valid) ]: total_loss = {total_valid_loss}")
if total_valid_loss < min_total_valid_loss:
min_total_valid_loss = total_valid_loss
min_turn = i + 1
logger.critical(
f"Current Best Total Loss comes from Epoch {min_turn} , min_total_loss = {min_total_valid_loss}")
torch.save(model, f"{father_folder_name}/{folder_name}/trained_model/model_{i + 1}.pth")
logger.info("Model has been saved successfully!")
if (i + 1) - min_turn > args.early_stop_turns:
break
delete_model(father_folder_name, folder_name, min_turn)
logger.info(BLUE + "Training is ended!" + ENDC)
delete_special_tokens(f"{father_folder_name}/{folder_name}/log.txt")
def run_one_epoch(model, loss_fn, optim, train_data_loader, valid_data_loader, device):
model.train()
min_train_loss = 1008611
for batch in tqdm(train_data_loader, desc='Training Progress'):
batch = [item.to(device) if isinstance(item, torch.Tensor) else item for item in batch]
visual_feature_embedding, textual_feature_embedding, similarity, retrieved_visual_feature_embedding, \
retrieved_textual_feature_embedding, retrieved_label, label = batch
output = model.forward(visual_feature_embedding, textual_feature_embedding, similarity,
retrieved_visual_feature_embedding,
retrieved_textual_feature_embedding, retrieved_label)
loss = loss_fn(output, label)
optim.zero_grad()
loss.backward()
optim.step()
if min_train_loss > loss:
min_train_loss = loss
model.eval()
total_valid_loss = 0
with torch.no_grad():
for batch in tqdm(valid_data_loader, desc='Validating Progress'):
batch = [item.to(device) if isinstance(item, torch.Tensor) else item for item in batch]
visual_feature_embedding, textual_feature_embedding, similarity, retrieved_visual_feature_embedding, \
retrieved_textual_feature_embedding, retrieved_label, label = batch
output = model.forward(visual_feature_embedding, textual_feature_embedding, similarity,
retrieved_visual_feature_embedding, retrieved_textual_feature_embedding,
retrieved_label)
output = output.to('cpu')
label = label.to('cpu')
output = np.array(output)
label = np.array(label)
MAE = mean_absolute_error(label, output)
nMSE = np.mean(np.square(output - label)) / (label.std() ** 2)
loss = MAE + nMSE
total_valid_loss += loss
return min_train_loss, total_valid_loss
def main(args):
seed_init(args.seed)
train_val(args)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=str, default='2024', help='Seed for reproducibility')
parser.add_argument('--device', type=str, default='cuda:0', help='Device for training')
parser.add_argument('--metric', type=str, default='MSE', help='Metric for evaluation')
parser.add_argument('--save', type=str, default='train_results', help='Directory to save results')
parser.add_argument('--epochs', type=int, default=100, help='Number of training epochs')
parser.add_argument('--batch_size', type=int, default=64, help='Batch size for training')
parser.add_argument('--early_stop_turns', type=int, default=20, help='Number of turns for early stopping')
parser.add_argument('--loss', type=str, default='MSE', help='Loss function for training')
parser.add_argument('--optim', type=str, default='Adam', help='Optimizer for training')
parser.add_argument('--lr', type=float, default=1e-5, help='Learning rate')
parser.add_argument('--dataset_id', type=str, default='MicroLens-100k', help='Dataset identifier')
parser.add_argument('--dataset_path', type=str, default='data', help='Path to the dataset')
parser.add_argument('--model_id', type=str, default='MMRA', help='Model id')
parser.add_argument('--feature_num', type=int, default=2, help='Number of features')
parser.add_argument('--num_of_retrieved_items', type=int, default=10, help='Number of retrieved items, hyper-parameter')
parser.add_argument('--feature_dim', type=int, default=768, help='Dimension of features')
parser.add_argument('--label_dim', type=int, default=1, help='Dimension of labels')
parser.add_argument('--alpha', type=float, default=0.6, help='Alpha, hyper-parameter')
parser.add_argument('--frame_num', type=int, default=10, help='Number of frames, hyper-parameter')
args_ = parser.parse_args()
main(args_)