-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathrandom_search.py
126 lines (109 loc) · 4.44 KB
/
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 torch
import numpy as np
import random
import json
import time
from pprint import pprint
from argus.callbacks import MonitorCheckpoint, \
EarlyStopping, LoggingToFile, ReduceLROnPlateau
from torch.utils.data import DataLoader
from src.datasets import FreesoundDataset, CombinedDataset, FreesoundNoisyDataset
from src.transforms import get_transforms
from src.argus_models import FreesoundModel
from src.utils import load_folds_data, load_noisy_data
from src import config
EXPERIMENT_NAME = 'noisy_lsoft_rs_002'
VAL_FOLDS = [0]
TRAIN_FOLDS = [1, 2, 3, 4]
BATCH_SIZE = 128
CROP_SIZE = 128
DATASET_SIZE = 128 * 256
if config.kernel:
NUM_WORKERS = 2
else:
NUM_WORKERS = 8
SAVE_DIR = config.experiments_dir / EXPERIMENT_NAME
START_FROM = 0
def train_experiment(folds_data, noisy_data, num):
experiment_dir = SAVE_DIR / f'{num:04}'
np.random.seed(num)
random.seed(num)
random_params = {
'p_dropout': float(np.random.uniform(0.1, 0.3)),
'batch_size': int(np.random.choice([128])),
'lr': float(np.random.choice([0.001, 0.0006, 0.0003])),
'add_prob': float(np.random.uniform(0.0, 1.0)),
'noisy_prob': float(np.random.uniform(0.0, 1.0)),
'lsoft_beta': float(np.random.uniform(0.2, 0.8)),
'noisy_weight': float(np.random.uniform(0.3, 0.7)),
'patience': int(np.random.randint(2, 10)),
'factor': float(np.random.uniform(0.5, 0.8))
}
pprint(random_params)
params = {
'nn_module': ('SimpleKaggle', {
'num_classes': len(config.classes),
'dropout': random_params['p_dropout'],
'base_size': 64
}),
'loss': ('OnlyNoisyLSoftLoss', {
'beta': random_params['lsoft_beta'],
'noisy_weight': random_params['noisy_weight'],
'curated_weight': 1 - random_params['noisy_weight']
}),
'optimizer': ('Adam', {'lr': random_params['lr']}),
'device': 'cuda',
'amp': {
'opt_level': 'O2',
'keep_batchnorm_fp32': True,
'loss_scale': "dynamic"
}
}
pprint(params)
try:
train_transfrom = get_transforms(True, CROP_SIZE)
curated_dataset = FreesoundDataset(folds_data, TRAIN_FOLDS,
transform=train_transfrom,
add_prob=random_params['add_prob'])
noisy_dataset = FreesoundNoisyDataset(noisy_data,
transform=train_transfrom)
train_dataset = CombinedDataset(noisy_dataset, curated_dataset,
noisy_prob=random_params['noisy_prob'],
size=DATASET_SIZE)
val_dataset = FreesoundDataset(folds_data, VAL_FOLDS,
get_transforms(False, CROP_SIZE))
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 = FreesoundModel(params)
callbacks = [
MonitorCheckpoint(experiment_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(experiment_dir / 'log.txt'),
]
with open(experiment_dir / 'random_params.json', 'w') as outfile:
json.dump(random_params, outfile)
model.fit(train_loader,
val_loader=val_loader,
max_epochs=100,
callbacks=callbacks,
metrics=['multi_accuracy', 'lwlrap'])
except KeyboardInterrupt as e:
raise e
except BaseException as e:
print(f"Exception '{e}' with random params '{random_params}'")
if __name__ == "__main__":
print("Start load train data")
noisy_data = load_noisy_data()
folds_data = load_folds_data()
for i in range(START_FROM, 10000):
train_experiment(folds_data, noisy_data, i)
time.sleep(5.0)
torch.cuda.empty_cache()
time.sleep(5.0)