-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathstacking_random_search.py
126 lines (106 loc) · 4.22 KB
/
stacking_random_search.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
import json
import time
import torch
import random
import numpy as np
from pprint import pprint
from argus.callbacks import MonitorCheckpoint, \
EarlyStopping, LoggingToFile, ReduceLROnPlateau
from torch.utils.data import DataLoader
from src.stacking.datasets import get_out_of_folds_data, StackingDataset
from src.stacking.transforms import get_transforms
from src.stacking.argus_models import StackingModel
from src import config
EXPERIMENT_NAME = 'fcnet_stacking_rs_004'
START_FROM = 0
EXPERIMENTS = [
'auxiliary_007',
'auxiliary_010',
'auxiliary_012',
'auxiliary_014'
]
DATASET_SIZE = 128 * 256
CORRECTIONS = True
if config.kernel:
NUM_WORKERS = 2
else:
NUM_WORKERS = 4
SAVE_DIR = config.experiments_dir / EXPERIMENT_NAME
def train_folds(save_dir, folds_data):
random_params = {
'base_size': int(np.random.choice([64, 128, 256, 512])),
'reduction_scale': int(np.random.choice([2, 4, 8, 16])),
'p_dropout': float(np.random.uniform(0.0, 0.5)),
'lr': float(np.random.uniform(0.0001, 0.00001)),
'patience': int(np.random.randint(3, 12)),
'factor': float(np.random.uniform(0.5, 0.8)),
'batch_size': int(np.random.choice([32, 64, 128])),
}
pprint(random_params)
save_dir.mkdir(parents=True, exist_ok=True)
with open(save_dir / 'random_params.json', 'w') as outfile:
json.dump(random_params, outfile)
params = {
'nn_module': ('FCNet', {
'in_channels': len(config.classes) * len(EXPERIMENTS),
'num_classes': len(config.classes),
'base_size': random_params['base_size'],
'reduction_scale': random_params['reduction_scale'],
'p_dropout': random_params['p_dropout']
}),
'loss': 'BCEWithLogitsLoss',
'optimizer': ('Adam', {'lr': random_params['lr']}),
'device': 'cuda',
}
for fold in config.folds:
val_folds = [fold]
train_folds = list(set(config.folds) - set(val_folds))
save_fold_dir = save_dir / f'fold_{fold}'
print(f"Val folds: {val_folds}, Train folds: {train_folds}")
print(f"Fold save dir {save_fold_dir}")
train_dataset = StackingDataset(folds_data, train_folds,
get_transforms(True),
DATASET_SIZE)
val_dataset = StackingDataset(folds_data, val_folds,
get_transforms(False))
train_loader = DataLoader(train_dataset,
batch_size=random_params['batch_size'],
shuffle=True, drop_last=True,
num_workers=NUM_WORKERS)
val_loader = DataLoader(val_dataset,
batch_size=random_params['batch_size'] * 2,
shuffle=False, num_workers=NUM_WORKERS)
model = StackingModel(params)
callbacks = [
MonitorCheckpoint(save_fold_dir, monitor='val_lwlrap', max_saves=1),
ReduceLROnPlateau(monitor='val_lwlrap',
patience=random_params['patience'],
factor=random_params['factor'],
min_lr=1e-8),
EarlyStopping(monitor='val_lwlrap', patience=20),
LoggingToFile(save_fold_dir / 'log.txt'),
]
model.fit(train_loader,
val_loader=val_loader,
max_epochs=300,
callbacks=callbacks,
metrics=['multi_accuracy', 'lwlrap'])
if __name__ == "__main__":
SAVE_DIR.mkdir(parents=True, exist_ok=True)
with open(SAVE_DIR / 'source.py', 'w') as outfile:
outfile.write(open(__file__).read())
if CORRECTIONS:
with open(config.corrections_json_path) as file:
corrections = json.load(file)
print("Corrections:", corrections)
else:
corrections = None
folds_data = get_out_of_folds_data(EXPERIMENTS, corrections)
for num in range(START_FROM, 10000):
np.random.seed(num)
random.seed(num)
save_dir = SAVE_DIR / f'{num:04}'
train_folds(save_dir, folds_data)
time.sleep(5.0)
torch.cuda.empty_cache()
time.sleep(5.0)