forked from hamuchiwa/AutoRCCar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
95 lines (75 loc) · 2.52 KB
/
model.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
__author__ = 'zhengwang'
import cv2
import numpy as np
import glob
import sys
import time
import os
from sklearn.model_selection import train_test_split
def load_data(input_size, path):
print("Loading training data...")
start = time.time()
# load training data
X = np.empty((0, input_size))
y = np.empty((0, 4))
training_data = glob.glob(path)
# if no data, exit
if not training_data:
print("Data not found, exit")
sys.exit()
for single_npz in training_data:
with np.load(single_npz) as data:
train = data['train']
train_labels = data['train_labels']
X = np.vstack((X, train))
y = np.vstack((y, train_labels))
print("Image array shape: ", X.shape)
print("Label array shape: ", y.shape)
end = time.time()
print("Loading data duration: %.2fs" % (end - start))
# normalize data
X = X / 255.
# train validation split, 7:3
return train_test_split(X, y, test_size=0.3)
class NeuralNetwork(object):
def __init__(self):
self.model = None
def create(self, layer_sizes):
# create neural network
self.model = cv2.ml.ANN_MLP_create()
self.model.setLayerSizes(np.int32(layer_sizes))
self.model.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP)
self.model.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM, 2, 1)
self.model.setTermCriteria((cv2.TERM_CRITERIA_COUNT, 100, 0.01))
def train(self, X, y):
# set start time
start = time.time()
print("Training ...")
self.model.train(np.float32(X), cv2.ml.ROW_SAMPLE, np.float32(y))
# set end time
end = time.time()
print("Training duration: %.2fs" % (end - start))
def evaluate(self, X, y):
ret, resp = self.model.predict(X)
prediction = resp.argmax(-1)
true_labels = y.argmax(-1)
accuracy = np.mean(prediction == true_labels)
return accuracy
def save_model(self, path):
directory = "saved_model"
if not os.path.exists(directory):
os.makedirs(directory)
self.model.save(path)
print("Model saved to: " + "'" + path + "'")
def load_model(self, path):
if not os.path.exists(path):
print("Model does not exist, exit")
sys.exit()
self.model = cv2.ml.ANN_MLP_load(path)
def predict(self, X):
resp = None
try:
ret, resp = self.model.predict(X)
except Exception as e:
print(e)
return resp.argmax(-1)