-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_filter.py
278 lines (236 loc) · 8.39 KB
/
train_filter.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
"""
Reference : https://www.kaggle.com/code/awsaf49/uwmgi-unet-train-pytorch
"""
# python train.py -e baseline --run res18_wd
import random
import gc
import argparse
import os, shutil
import time
import copy
import pandas as pd
import numpy as np
from collections import defaultdict
import matplotlib.pyplot as plt
# PyTorch
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision.ops import sigmoid_focal_loss
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
# WandB
import wandb
# Custom Import
from utils.loader import get_loaders
from utils.train_utils import train_one_epoch, valid_one_epoch
from configs import filter_config
from utils.models import build_model, load_model
from utils import secrets
def setup_wandb():
api_key = secrets.wandb
wandb.login(key=api_key)
run = (
wandb.init(
project="bollworm",
entity="curiousmonkey7",
config={k: v for k, v in dict(vars(CFG)).items() if "__" not in k},
name=CFG.run_name,
group=CFG.exp_name,
)
if not CFG.resume
else wandb.init(
project="bollworm",
entity="curiousmonkey7",
config={k: v for k, v in dict(vars(CFG)).items() if "__" not in k},
name=CFG.run_name,
group=CFG.exp_name,
id=CFG.wandb.run_id,
resume="must",
settings=wandb.Settings(start_method="thread"),
)
)
return run
def set_seed(seed=42):
# For reproducibility
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
# When running on the CuDNN backend, two further options must be set
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Set a fixed value for the hash seed
os.environ["PYTHONHASHSEED"] = str(seed)
print(f"> SEEDING DONE {seed}")
def save_checkpoint(state, is_best, checkpoint, filename="checkpoint.pth.tar"):
filepath = os.path.join(checkpoint, filename)
torch.save(state, filepath)
print("best?", is_best)
if is_best:
# print("hmm")
shutil.copyfile(filepath, os.path.join(checkpoint, "model_best.pth.tar"))
def load_checkpoint(path, model, optimizer):
print(f"Loading Checkpoint at {path}")
checkpoint = torch.load(path)
best = checkpoint["best_f1"], checkpoint["best_epoch"]
model.load_state_dict(checkpoint["state_dict"])
optimizer.load_state_dict(checkpoint["optimizer"])
return model, optimizer, best
def run_training(
model, dataloaders, optimizer, scheduler, criterion, device, num_epochs
):
# To automatically log gradients
if not CFG.debug:
wandb.watch(model, log_freq=100)
if torch.cuda.is_available():
print("cuda: {}\n".format(torch.cuda.get_device_name()))
start = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
best_f1 = -np.inf if not CFG.resume else CFG.best[0]
best_epoch = -1 if not CFG.resume else CFG.best[1]
history = defaultdict(list)
train_loader = dataloaders["train"]
val_loader = dataloaders["val"]
for epoch in range(1, num_epochs + 1):
gc.collect()
print(f"Epoch {epoch}/{num_epochs}")
train_loss, train_scores = train_one_epoch(
model,
train_loader,
optimizer,
scheduler,
criterion,
device=CFG.device,
epoch=epoch,
)
train_acc, train_f1 = train_scores
val_loss, val_scores = valid_one_epoch(
model, val_loader, criterion, device=CFG.device, epoch=epoch
)
val_acc, val_f1 = val_scores
history["Train_Loss"].append(train_loss)
history["Val_Loss"].append(val_loss)
history["Train_Acc"].append(train_acc)
history["Val_Acc"].append(val_acc)
history["Train_f1"].append(train_f1)
history["Val_f1"].append(val_f1)
# Log the metrics
lr = scheduler.get_last_lr()[0] if scheduler is not None else CFG.lr
if not CFG.debug:
wandb.log(
{
"Train Loss": train_loss,
"Val Loss": val_loss,
"Train Acc": train_acc,
"Val Acc": val_acc,
"Train f1": train_f1,
"Val_f1": val_f1,
}
)
print(
f"Train F1:{train_f1} | Val F1: {val_f1:0.4f} | Train ACC: {train_acc:0.4f} | Val ACC: {val_acc:0.4f}"
)
# deep copy the model
# last_model_wts = copy.deepcopy(model.state_dict())
is_best = val_f1 > best_f1
if is_best:
print(f"Valid Score Improved ({best_f1:0.4f} ---> {val_f1:0.4f})")
best_f1 = val_f1
best_epoch = epoch
save_checkpoint(
{
"epoch": epoch + 1,
"state_dict": model.state_dict(),
"f1": val_f1,
"best_f1": best_f1,
"best_epoch": best_epoch,
"optimizer": optimizer.state_dict(),
},
is_best,
checkpoint=CFG.run_path,
)
print()
print()
end = time.time()
time_elapsed = end - start
print(
"Training complete in {:.0f}h {:.0f}m {:.0f}s".format(
time_elapsed // 3600,
(time_elapsed % 3600) // 60,
(time_elapsed % 3600) % 60,
)
)
print("Best F1 Score: {:.4f}".format(best_f1))
# load best model weights
model.load_state_dict(best_model_wts)
return model, history
if __name__ == "__main__":
# print("sleeping")
# time.sleep(80*60)
# print("ready")
parser = argparse.ArgumentParser(description="Filter boolworm")
# parser.add_argument(
# "-e", "--experiment", default="trail", type=str, help="enter experiment name"
# )
# parser.add_argument("--run", default="trail", type=str, help="enter run name")
parser.add_argument("--debug", default=0, type=int, help="enter run name")
parser.add_argument("--loss", default="", type=str, help="enter loss type")
parser.add_argument("--viz", default=False, type=bool, help="")
args = parser.parse_args()
CFG = filter_config.get_config()
# CFG.run_name = args.run
# CFG.exp_name = args.experiment
CFG.debug = bool(args.debug)
CFG.loss = args.loss
print(CFG.loss)
CFG.run_path = f"runs/{CFG.exp_name}/{CFG.run_name}"
os.makedirs(CFG.run_path, exist_ok=True)
try:
# Set Seed For Reproducibility
set_seed(seed=CFG.seed)
# Login WandB
if not CFG.debug:
run = setup_wandb()
# Create Folds
train_filter = pd.read_csv("data/preproc/train_filter.csv")
fold =0
train_df = train_filter.query("fold!=@fold").reset_index(drop=True)
val_df = train_filter.query("fold==@fold").reset_index(drop=True)
print(f"No of Examples used(train+val): {len(train_filter)}")
# Get DataLoaders
dataloaders = {"train":get_loaders(train_df,preload=True),"val":get_loaders(val_df,phase="val",preload=True)}
if args.viz:
trainiter = iter(dataloaders["train"])
images,labels = next(trainiter)
for i in range(5):
plt.imshow(images[i].T)
plt.savefig(f'{CFG.run_path}/{i}.jpg') # save the fi
plt.show()
# Get Model
model = build_model()
print(f"Model Parameters: {sum(p.numel() for p in model.parameters())}")
# Configure Optimizer, Scheduler, and Criterion
optimizer = optim.Adam(model.parameters(), lr=CFG.lr, weight_decay=CFG.wd)
scheduler = None # lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
criterion = nn.BCEWithLogitsLoss() # nn.CrossEntropyLoss(weight=class_weights)
if CFG.resume:
model, optimizer, best = load_checkpoint(CFG.resume, model, optimizer)
CFG.best = best
# Run Training
print("Starting Training")
model, history = run_training(
model,
dataloaders,
optimizer,
scheduler,
criterion,
device=CFG.device,
num_epochs=CFG.epochs if not CFG.debug else 2,
)
# Save History
torch.save(history, f"{CFG.run_path}/history.pth")
except Exception as e:
print(e)
finally:
if not CFG.debug:
run.finish()