-
Notifications
You must be signed in to change notification settings - Fork 95
/
main_fer2013.py
88 lines (67 loc) · 2 KB
/
main_fer2013.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
import json
import os
import random
import warnings
warnings.simplefilter(action="ignore", category=FutureWarning)
import imgaug
import numpy as np
import torch
import torch.multiprocessing as mp
seed = 1234
random.seed(seed)
imgaug.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
import models
from models import segmentation
def main(config_path):
"""
This is the main function to make the training up
Parameters:
-----------
config_path : srt
path to config file
"""
# load configs and set random seed
configs = json.load(open(config_path))
configs["cwd"] = os.getcwd()
# load model and data_loader
model = get_model(configs)
train_set, val_set, test_set = get_dataset(configs)
# init trainer and make a training
# from trainers.fer2013_trainer import FER2013Trainer
from trainers.tta_trainer import FER2013Trainer
# from trainers.centerloss_trainer import FER2013Trainer
trainer = FER2013Trainer(model, train_set, val_set, test_set, configs)
if configs["distributed"] == 1:
ngpus = torch.cuda.device_count()
mp.spawn(trainer.train, nprocs=ngpus, args=())
else:
trainer.train()
def get_model(configs):
"""
This function get raw models from models package
Parameters:
------------
configs : dict
configs dictionary
"""
try:
return models.__dict__[configs["arch"]]
except KeyError:
return segmentation.__dict__[configs["arch"]]
def get_dataset(configs):
"""
This function get raw dataset
"""
from utils.datasets.fer2013dataset import fer2013
# todo: add transform
train_set = fer2013("train", configs)
val_set = fer2013("val", configs)
test_set = fer2013("test", configs, tta=True, tta_size=10)
return train_set, val_set, test_set
if __name__ == "__main__":
main("./configs/fer2013_config.json")