-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
207 lines (170 loc) · 5.55 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
import math
import argparse
from pathlib import Path
import os, gc
from fastai.vision.all import *
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import WeightedRandomSampler, RandomSampler, DataLoader
import numpy as np
import pandas as pd
from config import ArmNetConfig
from model import ArmNet
from dataset import RNA_Dataset
from training_utils import parameter_count, loss, seed_everything, get_dataloaders
from training_utils import MAE, MAE_2A3, MAE_DMS, DeviceDataLoader
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config_path", type = str)
parser.add_argument("--forget_config", action = "store_true")
parser.add_argument("--out_dir_path", default = "./results", type = str)
parser.add_argument("--pretrained_model_weights", type = str)
parser.add_argument("--no_weights", action = "store_true")
parser.add_argument("--num_folds", type=int)
parser.add_argument("--fold", type=int)
parser.add_argument("--lr_max", type=float)
parser.add_argument("--weight_decay", type=float)
parser.add_argument("--pct_start", type=float)
parser.add_argument("--gradclip", type=float)
parser.add_argument("--num_epochs", type=int)
parser.add_argument("--num_workers", type=int)
parser.add_argument("--batch_size", type=int)
parser.add_argument("--batch_count", type=int)
parser.add_argument("--device", type=int)
parser.add_argument("--seed", type=int)
parser.add_argument("--sgd_lr", type=float)
parser.add_argument("--sgd_num_epochs", type=int)
parser.add_argument("--sgd_batch_count", type=int)
parser.add_argument("--sgd_weight_decay", type=float)
args = parser.parse_args()
config = ArmNetConfig()
OUT_DIR_PATH = Path(args.out_dir_path)
os.makedirs(OUT_DIR_PATH, exist_ok=True)
if args.config_path is not None:
config.load(args.config_path)
config.load_dict(vars(args))
if not args.forget_config:
config.save(OUT_DIR_PATH / "config.json")
config.device = torch.device(f"cuda:{config.device}") ##########
BPPM_PATH = config.train_bppm_data_path
if BPPM_PATH is not None:
BPPM_PATH = Path(BPPM_PATH).resolve()
MODEL_WEIGHTS_PATH = config.pretrained_model_weights
if MODEL_WEIGHTS_PATH is not None:
MODEL_WEIGHTS_PATH = Path(MODEL_WEIGHTS_PATH).resolve()
seed_everything(config.seed)
df = pd.read_parquet(config.train_data_path)
###Make convenient saving and paths
save_model_cbk = SaveModelCallback(
monitor='valid_loss',
fname='model',
with_opt=True)
main_run_path = OUT_DIR_PATH / 'main_run'
main_run_path.mkdir(parents=True, exist_ok=True)
logger = CSVLogger(fname = str(main_run_path / "loss.csv"))
print("Constructing training dataset.")
train_dataset = RNA_Dataset(
df,
mode = 'train',
fold = config.fold,
nfolds = config.num_folds,
use_bppm = config.use_bppm,
bppm_path = BPPM_PATH
)
print("Constructing validation dataset.")
val_dataset = RNA_Dataset(
df,
mode = 'eval',
fold = config.fold,
nfolds = config.num_folds,
use_bppm = config.use_bppm,
bppm_path = BPPM_PATH
)
print("Constructing dataloaders.")
data = get_dataloaders(
train_dataset = train_dataset,
val_dataset = val_dataset,
batch_size = config.batch_size,
batch_count = config.batch_count,
num_workers = config.num_workers,
no_weights = config.no_weights,
device = config.device
)
gc.collect()
model = ArmNet(
depth = config.num_encoder_layers,
num_convs = config.num_conv_layers,
adj_ks = config.conv_2d_kernel_size,
attn_kernel_size = config.conv_1d_kernel_size,
dropout = config.dropout,
conv_use_drop1d = config.conv_1d_use_dropout,
use_bppm = config.use_bppm,
)
print("Parameter count: ", parameter_count(model))
if MODEL_WEIGHTS_PATH is not None:
model.load_state_dict(
torch.load(MODEL_WEIGHTS_PATH, map_location="cpu")['model']
)
model = model.to(config.device)
if config.num_epochs > 0:
learn = Learner(
data,
model,
loss_func = loss,
model_dir = main_run_path,
cbs=[GradientClip(config.gradclip),
logger,
save_model_cbk],
metrics=[MAE(),
MAE_DMS(),
MAE_2A3()]
).to_fp16()
print("Start learning cycle")
learn.fit_one_cycle(
config.num_epochs,
lr_max = config.lr_max,
wd = config.weight_decay,
pct_start = config.pct_start
)
torch.save(
learn.model.state_dict(),
OUT_DIR_PATH / "model.pth"
)
gc.collect()
print("Constructing dataloaders for sgd run.")
data = get_dataloaders(
train_dataset = train_dataset,
val_dataset = val_dataset,
batch_size = config.batch_size,
batch_count = config.sgd_batch_count,
num_workers = config.num_workers,
no_weights = config.no_weights,
device = config.device
)
save_model_cbk = SaveModelCallback(
monitor='valid_loss',
fname='model',
with_opt=True,
)
sgd_run_path = OUT_DIR_PATH / 'sgd_run'
sgd_run_path.mkdir(parents=True, exist_ok=True)
logger = CSVLogger(fname = str(sgd_run_path / "loss.csv"))
if config.sgd_num_epochs > 0:
learn = Learner(
data,
model,
model_dir = sgd_run_path,
lr = config.sgd_lr,
opt_func = partial(
OptimWrapper,
opt=torch.optim.SGD
),
loss_func = loss,
cbs = [GradientClip(config.gradclip),
save_model_cbk,
logger],
metrics=[MAE(),
MAE_DMS(),
MAE_2A3()]).to_fp16()
learn.fit(config.sgd_num_epochs,
wd=config.sgd_weight_decay)