-
Notifications
You must be signed in to change notification settings - Fork 0
/
bronte.py
271 lines (239 loc) · 8.33 KB
/
bronte.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
import os
import subprocess
import torch
import traceback
import numpy as np
import pandas as pd
from flask import Flask, request, jsonify
from sqlalchemy import create_engine, pool
from torch import nn
from src.arch import FFN, RNN
from src.task import Classification, Regression
try:
from google.colab import drive
colab = True
except:
colab = False
verbose = 1 # 0-3
class Bronte:
archs = {"ffn": FFN, "rnn": RNN}
tasks = {"class": Classification, "reg": Regression}
losses = {
"mse": nn.MSELoss,
"cross": nn.CrossEntropyLoss,
"bce": nn.BCELoss,
"bcew": nn.BCEWithLogitsLoss,
"nll": nn.NLLLoss,
"huber": nn.HuberLoss,
}
def __init__(self, options=None, path=None, full=True):
super().__init__()
if options is None:
options = {}
options["verbose"] = verbose # flag
options["colab"] = colab # flag
if path is not None and path.endswith(".pt") and os.path.exists(path):
checkpoint = torch.load(path)
prev = checkpoint["options"]
task = (
self.tasks[options["task"]]
if "task" in options and options["task"]
else self.tasks[prev["task"]]
)
arch = (
self.archs[options["arch"]]
if "arch" in options and options["arch"]
else self.archs[prev["arch"]]
)
options["criterion"] = (
self.losses[options["criterion"]](reduction="none")
if "criterion" in options
else prev["criterion"]
)
self.model = self.factory(task, arch)(prev)
self.model = self.model.load(path, options, full=full)
else:
task = (
self.tasks[options["task"]]
if "task" in options and options["task"]
else FFN
)
arch = (
self.archs[options["arch"]]
if "arch" in options and options["arch"]
else Regression
)
options["criterion"] = (
self.losses[options["criterion"]](reduction="none")
if "criterion" in options
else nn.MSELoss(reduction="none")
)
self.model = self.factory(task, arch)(options)
@staticmethod
def factory(task, arch):
class Instance(task, arch):
def __init__(self, options={}):
super().__init__()
self.configure(options)
return Instance
def fit(self, data):
df = data.copy()
options = self.model.options
if 1 <= options["sample_size"] < len(df) or options["timeseries"]:
if options["sample_size"] >= len(df):
options["sample_size"] = len(df) - 1
if options["sample_size"] < 1:
options["sample_size"] = np.rint(options["sample_size"] * len(df))
rg = np.random.default_rng(42)
n = rg.choice(len(df) - 1 - np.rint(options["sample_size"]))
df = df.iloc[n : n + options["sample_size"], :].copy()
elif 0 < options["sample_size"] < 1:
df = df.sample(frac=options["sample_size"], random_state=42)
for col in df.columns:
if df[col].dtype == "object":
try:
df[col] = pd.to_numeric(df[col])
except ValueError:
pass
data = pd.get_dummies(df, dtype=np.int8)
if not options["timeseries"]:
X = data.drop(columns=options["targets"])
y = data[options["targets"]]
else:
X = data.iloc[: -options["num_out"], :]
y = data.iloc[-options["num_out"] :, :][options["targets"]]
# try:
self.model = self.model.fit(X, y)
# except:
# if options["verbose"]:
# traceback.print_exc()
# self.model = None
# finally:
return 0 if self.model is not None else 1
def predict(self, X):
return self.model.predict(X)
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
if colab:
drive.mount("/content/drive", force_remount=True)
drive_dir = "bronte" # @param {type:"string"}
path = "/".join(["drive", "MyDrive", drive_dir])
os.chdir(path)
else:
os.chdir(os.path.dirname(os.path.realpath(__file__)))
# set seeds for reproducibility
torch.manual_seed(42)
np.random.seed(42)
app = Flask(__name__)
engine = create_engine(
"sqlite:///data/data.db",
connect_args={"check_same_thread": False},
poolclass=pool.SingletonThreadPool,
echo=verbose > 2,
)
prefix = "bronte_"
idx = 0
tensorboard = None
@app.route("/flush", methods=["GET"])
def flush():
cursor = engine.raw_connection().cursor()
for row in cursor.execute("SELECT name FROM sqlite_master WHERE type='table';"):
cursor.execute(f"DROP TABLE {row[0]}")
try:
return "Database flushed"
except:
pass
@app.route("/load", methods=["POST"])
def load(data=None):
if data is None:
data = request.get_json()["data"]
df = pd.read_json(data)
global idx
df.to_sql(f"{prefix}{idx}", engine, if_exists="replace", index=False)
idx += 1
try:
return jsonify({"message": f"Data loaded into table {prefix}{idx}"}), 200
except:
pass
@app.route("/fit", methods=["POST"])
def fit(models=None):
if models is None:
models = request.get_json()
trainers = []
for task_type in models["tasks"]:
for task in models["tasks"][task_type]:
tables = pd.read_sql(
"SELECT name FROM sqlite_master WHERE type='table';", engine
)
for arch in models["archs"]:
options = task | arch
path = ""
if "fine" in task_type:
path = (
f"models/{options['task']} {options['arch']}/{options['prev']}/"
)
path += f"{sorted(os.listdir(path))[-1]}/model.pt"
trainer = Bronte(options, path)
for i, table in enumerate(tables["name"].tolist()):
if options["table_prefix"] in table:
data = pd.read_sql(f"SELECT * FROM {table}", engine)
if trainer.fit(data) != 0 or i >= options["samples"] - 1:
break
trainers.append(trainer)
try:
return jsonify({"message": "Models trained successfully"}), 200
except:
return trainers
@app.route("/logs", methods=["GET"])
def track():
global tensorboard
if tensorboard is not None:
tensorboard.terminate()
tensorboard = None
try:
return jsonify({"message": "Tensorboard server stopped"}), 200
except:
pass
if not colab:
tensorboard = subprocess.Popen(["tensorboard", "--logdir=models", "--bind_all"])
try:
return jsonify({"message": "Tensorboard server started"}), 200
except:
pass
@app.route("/predict", methods=["POST"])
def predict(XX=None, paths=[]):
if XX is None:
try:
XX = request.get_json() # Get new data from the request
except:
pass
if not isinstance(XX, list):
XX = [XX]
if not paths:
for root, _, files in os.walk("models"):
for file in files:
if (
file.endswith(".pt")
and not file.split(".")[0].lstrip("-").isdigit()
):
paths.append(os.path.join(root, file))
predictions = {}
for path in paths:
model = Bronte(path=path).model
targets = model.target_var
for i, X in enumerate(XX):
if verbose:
print(f"Predicting {targets} with {path}...\n")
preds = model.predict(X)
if not isinstance(preds, list):
preds = [preds.squeeze()]
predictions[path] = {str(i): preds}
if verbose:
for pred, target in zip(preds, targets):
print(f"Head of {target} predictions:\n{pred.squeeze()[:10]}\n")
try:
return jsonify(predictions), 200
except:
return predictions
if __name__ == "__main__":
app.run(debug=True)