-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbase.py
More file actions
169 lines (144 loc) · 5.48 KB
/
base.py
File metadata and controls
169 lines (144 loc) · 5.48 KB
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
import logging
import os
from abc import ABC, abstractmethod
from typing import List, Union
class Base(ABC):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
@abstractmethod
def show(self):
pass
@abstractmethod
def save(self, cfg):
pass
class BaseConfig(Base):
def __init__(self, **kwargs):
super(BaseConfig, self).__init__(**kwargs)
def show(self):
for key, value in self.__dict__.items():
logging.info(f"{key}: {value}")
def save(self, cfg):
message = "\n"
for k, v in sorted(vars(cfg).items()):
message += f"{str(k):>30}: {str(v):<40}\n"
os.makedirs(os.path.join(cfg.checkpoint_dir), exist_ok=True)
out_opt = os.path.join(cfg.checkpoint_dir, "cfg.log")
with open(out_opt, "w") as opt_file:
opt_file.write(message)
opt_file.write("\n")
logging.info(message)
def load(self, cfg_path: str):
def decode_value(value: str):
value = value.strip()
convert_value = None
if "." in value and value.replace(".", "").isdigit():
convert_value = float(value)
elif value.isdigit():
convert_value = int(value)
elif value == "True":
convert_value = True
elif value == "False":
convert_value = False
elif value == "None":
convert_value = None
elif (
value.startswith("'")
and value.endswith("'")
or value.startswith('"')
and value.endswith('"')
):
convert_value = value[1:-1]
else:
convert_value = value
return convert_value
with open(cfg_path, "r") as f:
data = f.read().split("\n")
# remove all empty strings
data = list(filter(None, data))
# convert to dict
data_dict = {}
for i in range(len(data)):
key, value = (
data[i].split(":")[0].strip(),
data[i].split(":")[1].strip(),
)
if value.startswith("[") and value.endswith("]"):
value = value[1:-1].split(",")
value = [decode_value(x) for x in value]
else:
value = decode_value(value)
data_dict[key] = value
for key, value in data_dict.items():
setattr(self, key, value)
class Config(BaseConfig):
# Base
def __init__(self, **kwargs):
super(Config, self).__init__(**kwargs)
self.name = "default"
self.set_args()
for key, value in kwargs.items():
setattr(self, key, value)
def set_args(self, **kwargs):
# Training settings
self.trainer = "Trainer" # Trainer type use for training model [MSER_Trainer, Trainer, MarginTrainer]
self.num_epochs: int = 100
self.checkpoint_dir: str = "checkpoints"
self.save_all_states: bool = False
self.save_best_val: bool = True
self.max_to_keep: int = 1
self.save_freq: int = 100000
self.batch_size: int = 1
# Learning rate
self.learning_rate: float = 0.0001
self.learning_rate_step_size: int = 30
self.learning_rate_gamma: float = 0.1
self.optimizer_type: str = "Adam" # Adam, SGD, AdamW
# Adam config
self.adam_beta_1 = 0.9
self.adam_beta_2 = 0.999
self.adam_eps = 1e-08
self.adam_weight_decay = 0
# SGD config
self.momemtum = 0.99
self.sdg_weight_decay = 1e-6
# Resume training
self.resume: bool = False
# path to checkpoint.pt file, only available when using save_all_states = True in previous training
self.resume_path: Union[str, None] = None
self.cfg_path: Union[str, None] = None
if self.resume:
assert os.path.exists(str(self.resume_path)), "Resume path not found"
self.loss_type: str = "CrossEntropyLoss"
# Dataset
self.data_name: str = (
"IEMOCAP" # [IEMOCAP, ESD, MELD, IEMOCAPAudio, IEMOCAP_MSER]
)
self.data_root: str = (
"data/IEMOCAP_preprocessed" # folder contains train.pkl and test.pkl
)
self.data_valid: str = (
"val.pkl" # change this to your validation subset name if you want to use validation dataset. If None, test.pkl will be use
)
self.num_workers = 0
# use for training with batch size > 1
self.text_max_length: int = 297
self.audio_max_length: int = 546220
# Model
self.num_classes: int = 4
self.num_attention_head: int = 8
self.dropout: float = 0.05
self.model_type: str = "MemoCMT" #
self.text_encoder_type: str = "bert" # [bert, roberta]
self.text_encoder_dim: int = 768
self.text_unfreeze: bool = False
self.audio_encoder_type: str = "hubert_base"
self.audio_encoder_dim: int = 768
self.audio_unfreeze: bool = False
self.fusion_dim: int = 768
self.fusion_head_output_type: str = "cls" # [cls, mean, max]
# Search for linear layer output dimension
self.linear_layer_output: List = [128]
self.linear_layer_last_dim: int = 64
for key, value in kwargs.items():
setattr(self, key, value)