-
Notifications
You must be signed in to change notification settings - Fork 69
/
train.py
executable file
·66 lines (54 loc) · 1.98 KB
/
train.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import h5py
import numpy as np
import sys, deps
import glob, os
import keras.models
print("---- FASTEN YOUR SEATBELTS -----") # FIXME
print("If it's slow, compile protobuf and tensorflow from source!")
NC_PATH_MODELS = 'data/models/'
NC_PATH_DATASET = 'data/train/'
NC_MODELS = {
'LAPS': {'network': deps.laps.model, 'labels': None},
'MAIN': {'network': None, 'labels': None}
}
################################################################################
def read_dataset(name):
global NC_PATH_DATASET
path = NC_PATH_DATASET + "{}.h5".format(name)
h5f = h5py.File(path, 'r', driver='core')
X, Y = h5f['data'], h5f['labels']
X = X[()].reshape([-1, 21, 21, 1])
Y = Y[()].reshape([-1, 2])
return (X, Y)
def train_network(model, X, Y, n=50):
if n == 0: return model
model.fit(X, Y, epochs=n, batch_size=64, shuffle="batch")
pred = model.predict(X); print("FINAL", np.mean(np.square(pred - Y)))
return model
def load_model(name, best=False):
global NC_MODELS, NC_PATH_MODELS
model = NC_MODELS[name]['network']
#best_path = NC_PATH_MODELS + '{}.weights.h5'.format(name.lower())
#if best and os.path.isfile(best_path):
# model.load_weights(best_path)
best_path = NC_PATH_MODELS + '{}.h5'.format(name.lower())
if best and os.path.isfile(best_path):
model = keras.models.load_model(best_path)
return model
def save_model(name):
global NC_PATH_MODELS
save_path = NC_PATH_MODELS + '{}.h5'.format(name.lower())
model.save(save_path)
save_model = NC_PATH_MODELS + '{}.model.json'.format(name.lower())
with open(save_model, "w") as json_file:
json_file.write(model.to_json())
save_weights = NC_PATH_MODELS + '{}.weights.h5'.format(name.lower())
model.save_weights(save_weights)
################################################################################
print("[FIXME]: only LAPS model is supported")
NAME = 'LAPS'.upper()
model = train_network(load_model(NAME, best=True),
*read_dataset(NAME), n=int(sys.argv[1]))
save_model(NAME)