-
Notifications
You must be signed in to change notification settings - Fork 0
/
current.py
192 lines (147 loc) · 5.6 KB
/
current.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
from config import config_values, Config
import json
from data.audioiter import AudioIterator
import os
import zounds
import argparse
from datetime import datetime
import os
import torch
from conjure.serve import serve_conjure
from pathlib import Path
from train.experiment_runner import BaseExperimentRunner
from util.store_trained_weights_remotely import store_trained_weights_remoteley
torch.backends.cudnn.benchmark = True
def templatized_init(class_name):
return f'''from .experiment import {class_name}'''
def templatized_experiment(class_name):
experiment_template = f'''
import torch
from torch import nn
from torch.nn import functional as F
import zounds
from config.experiment import Experiment
from train.experiment_runner import BaseExperimentRunner
from util import device
from util.readmedocs import readme
exp = Experiment(
samplerate=22050,
n_samples=2**15,
weight_init=0.1,
model_dim=128,
kernel_size=512)
def train(batch, i):
pass
@readme
class {class_name}(BaseExperimentRunner):
def __init__(self, stream, port=None, save_weights=False, load_weights=False):
super().__init__(stream, train, exp, port=port, save_weights=save_weights, load_weights=load_weights)
'''
return experiment_template
def new_experiment(class_name=None, postfix=''):
dt = datetime.now()
if postfix and not postfix.startswith('_'):
postfix = '_' + postfix
dirname = f'e_{dt.year}_{dt.month}_{dt.day}{postfix}'
path, _ = os.path.split(__file__)
exp_path = os.path.join(path, 'experiments', dirname)
if os.path.exists(exp_path):
print(
f'Experiment {exp_path} already exists. Appending to dirname.')
exp_path += 'b'
os.mkdir(exp_path)
with open(os.path.join(exp_path, '__init__.py'), 'w') as f:
if class_name:
f.write(templatized_init(class_name))
with open(os.path.join(exp_path, 'experiment.py'), 'w') as f:
if class_name:
f.write(templatized_experiment(class_name))
with open(os.path.join(exp_path, 'readme.md'), 'w'):
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--push-trained-weights', action='store_true')
parser.add_argument('--new', action='store_true')
parser.add_argument('--overfit', action='store_true')
parser.add_argument('--batch-size', type=int, default=4)
parser.add_argument('--normalize', action='store_true')
parser.add_argument('--nsamples', type=int, default=14)
parser.add_argument('--classname', type=str, default=None)
parser.add_argument('--postfix', type=str, default='')
parser.add_argument('--step', type=int, default=1)
parser.add_argument('--pattern', type=str, default='*.wav')
parser.add_argument('--save-weights', action='store_true')
parser.add_argument('--load-weights', action='store_true')
parser.add_argument('--clean', action='store_true')
parser.add_argument('--indices', action='store_true')
args = parser.parse_args()
if args.new:
new_experiment(args.classname, args.postfix)
elif args.push_trained_weights:
from experiments import Current
port = os.environ['PORT']
print(args)
path = Current.__module__
stream = AudioIterator(
args.batch_size,
2**args.nsamples,
zounds.SR22050(),
args.normalize,
args.overfit,
step_size=args.step,
pattern=args.pattern,
return_indices=args.indices)
exp: BaseExperimentRunner = Current(
stream,
port=port,
save_weights=args.save_weights,
load_weights=args.load_weights)
model = exp.model
if model is None:
raise ValueError(f'Experiment {Current.__class__.__name__} does not have an associated model')
store_trained_weights_remoteley(
path, model, device='cpu', s3_bucket=Config.s3_bucket())
else:
from experiments import Current
port = os.environ['PORT']
path = Current.__module__
exp, date, _ = path.split('.')
base_path = os.path.join(exp, date)
# TODO: This should probably be a method on the base
# experiment class
if args.clean:
try:
data_path = Path(base_path) / Path('experiment_data/data.mdb')
os.remove(data_path)
print('Removed old experiment data')
except IOError:
print('No old experiment data to remove')
print(args)
stream = AudioIterator(
args.batch_size,
2**args.nsamples,
zounds.SR22050(),
args.normalize,
args.overfit,
step_size=args.step,
pattern=args.pattern)
exp: BaseExperimentRunner = Current(
stream,
port=port,
save_weights=args.save_weights,
load_weights=args.load_weights)
funcs = exp.conjure_funcs
serve_conjure(
funcs,
port=port,
n_workers=2
)
if exp.__doc__ is None or exp.__doc__.strip() == '':
raise ValueError('Please write a little about your experiment')
print('\n\nTODAY\'S EXPERIMENT ==============================\n\n')
print(exp.__doc__)
print('config: ')
print(json.dumps(config_values, indent=4))
print('\n\n==================================================\n\n')
exp.run()
input('Check it out...')