-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_tune.py
163 lines (140 loc) · 5.81 KB
/
main_tune.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
import json
import random
import torch
import torch.optim as optim
import wandb
import numpy as np
import os
import optuna
import joblib
# from model.unet import UNet
from train_tune import Train
from data_loader.dataset import Dataset
import gc
def set_seed(seed):
"""Set all random seeds to a fixed value and take out any randomness from cuda kernels"""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.enabled = False
return True
with open(
"/share/projects/erasmus/pratichhya_sharma/DAoptim/DAoptim/utils/config.json",
"r",
) as read_file:
config = json.load(read_file)
# set network
from seg_model_smp.models_predefined import segmentation_models_pytorch as psmp
net = psmp.Unet( encoder_name="resnet34",
encoder_weights=None,
in_channels=3,
classes=1,
)
net.cuda()
def main_hyper(trial):
set_seed(42)
# seting training and testing dataset
dsource_loaders = Dataset(config["data_folder"], config["patchsize"], "both")
dsource_loaders.array_torch()
source_dataloader = dsource_loaders.source_dataloader
val_source_dataloader = dsource_loaders.valid_source_dataloader
#dtarget_loaders = Dataset(config["data_folder"], config["patchsize"], "training_target")
#dtarget_loaders.array_torch()
target_dataloader = dsource_loaders.target_dataloader
val_target_dataloader = dsource_loaders.valid_target_dataloader
# computing the length
len_train_source = len(source_dataloader) # training steps
len_train_target = len(target_dataloader)
print(
f"length of train source:{len_train_source}, lenth of train target is {len_train_target}"
)
# computing the length
len_val_source = len(val_source_dataloader) # training steps
len_val_target = len(val_target_dataloader)
print(
f"length of validation source:{len_val_source}, lenth of validation target is {len_val_target}"
)
#autocast
scaler = torch.cuda.amp.GradScaler()
cfg = {
"n_epochs":30,
"seed": 42,
# "lr": trial.suggest_loguniform('lr', 1e-5, 1e-2),
"lr": 1e-3,
"momentum": 0.6,
"optimizer": optim.Adam,
"save_model": False,
# 'alpha':trial.suggest_categorical("alpha", [0.001,0.01,0.1,1.0,10,100,1000]),
'alpha':1,
'lambda_t':trial.suggest_categorical("alpha", [0.001,0.01,0.1,1.0,10,100,1000]),
# 'lambda_t': 0.4,
# 'lambda_t':trial.suggest_uniform('lambda_t', 0.001, 0.9),
# 'reg_m':trial.suggest_uniform('reg_m', 0.01, 1),
# 'reg':trial.suggest_uniform('reg', 0.01, 1),
'reg_m':0.06,
'reg':0.7
}
torch.manual_seed(cfg["seed"])
parameter_num = sum(p.numel() for p in net.parameters() if p.requires_grad)
print(f"The model has {parameter_num:,} trainable parameters")
model = net
# optimizer = cfg['optimizer'](model.parameters(), lr=cfg['lr'],momentum=cfg['momentum'], weight_decay=cfg['weight_decay'])
# optimizer = cfg["optimizer"](model.parameters(), lr=cfg["lr"], weight_decay=cfg['weight_decay'])
optimizer=optim.Adam(net.parameters(),lr=cfg["lr"])
f1 = []
loss = []
val_f1 =[]
send = []
config["trial.number"] = trial.number
'''reinit argument is necessary to call wandb.init method multiple times in the same process because objective is called multiple times in Optuna’s optimization.'''
# wandb.init(project="all_v2", config=cfg, reinit=True)
for epoch in range(1, cfg["n_epochs"] + 1):
print(f"in epoch {epoch}")
print("----------------------Traning phase-----------------------------")
train_loss,transfer_loss, acc_mat = Train.train_epoch(net,optimizer,source_dataloader,target_dataloader,
cfg["alpha"],
cfg["lambda_t"],
cfg["reg"],
cfg["reg_m"],
)
# f1.append(acc_mat[0])
# loss.append(train_loss)
# send = sum(f1/len(f1))
# # send = 1-(sum(loss)/len(loss))
# print(f"Training loss in average for epoch {str(epoch)} is {train_loss}")
# print(f"Training F1 in average for epoch {str(epoch)} is {acc_mat[0]}")
# print(f"Training Accuracy in average for epoch {str(epoch)} is {acc_mat[1]}")
# print(f"Training IOU in average for epoch {str(epoch)} is {acc_mat[2]}")
# print(f"f1 was {f1} and returned is{send}")
del train_loss, acc_mat
# torch.cuda.empty_cache()
print("----------------------Evaluation phase-----------------------------")
val_acc_mat = Train.val_epoch(
net,
val_source_dataloader,
val_target_dataloader,
cfg["alpha"],
cfg["lambda_t"],
cfg["reg"],
cfg["reg_m"],
)
print(f"Evaluation F1 in average for epoch {str(epoch)} is {val_acc_mat[0]}")
print(f"Evaluation Accuracy in average for epoch {str(epoch)} is {val_acc_mat[1]}")
print(f"Evaluation IOU in average for epoch {str(epoch)} is {val_acc_mat[2]}")
send.append(val_acc_mat[0])
# report validation accuracy to wandb
# wandb.log(data={"validation accuracy": val_acc_mat[1],"validation F1": val_acc_mat[0]}, step=epoch)
del val_acc_mat
# if cfg["save_model"]:
# torch.save(model.state_dict(), "hyperparam.pt")
return max(send)
if __name__ == "__main__":
sampler = optuna.samplers.TPESampler(seed=set_seed(42))
study = optuna.create_study(sampler=sampler, direction="maximize")
study.optimize(func=main_hyper, n_trials=10)
joblib.dump(
study,
"/share/projects/erasmus/pratichhya_sharma/DAoptim/DAoptim/model/opt_f1/lambda_cat10.pkl",
)