-
Notifications
You must be signed in to change notification settings - Fork 2
/
predict.py
executable file
·64 lines (48 loc) · 2.05 KB
/
predict.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
#!/usr/bin/env python3
from tensorflow.keras.models import load_model
import pickle
import numpy as np
from preprocessor import PreProcessor
import pandas as pd
import training
import model
pre = PreProcessor()
def predictions(data, model):
new_data = []
for idx in range(1, len(data.index)-1):
row_now = data.iloc[[idx]].reset_index()
row_prev = data.iloc[[idx - 1]].reset_index()
row_next = data.iloc[[idx + 1]].reset_index()
time_now = row_now[1].values[0]
time_prev = row_prev[1].values[0]
time_next = row_next[1].values[0]
if time_now - time_prev > 0 and 0.0000001 < time_now - time_prev < 0.58: # 0.578111 is highest diff i have seen
row1 = row_prev
row2 = row_now
elif time_next - time_now > 0 and 0.0000001 < time_next - time_now < 0.58:
row1 = row_now
row2 = row_next
x1, y1 = pre.preprocess_image_from_path(row1[0].values[0],row1[2].values[0])
x2, y2 = pre.preprocess_image_from_path(row2[0].values[0],row2[2].values[0])
img_diff = pre.optical_flow(x1, x2)
img_diff = img_diff.reshape(1, img_diff.shape[0], img_diff.shape[1], img_diff.shape[2])
y = np.mean([y1, y2])
prediction = model.predict(img_diff)
error = abs(prediction-y2)
new_data.append([prediction[0][0], y2, error[0][0], time_now, time_prev])
return pd.DataFrame(new_data)
def get_pred_mse(preds):
df = pd.read_pickle(preds)
avg = np.mean(df[2].values**2)
return avg
if __name__ == "__main__":
model = model.speed_model()
model.load_weights("./model-weights.h5")
df = pd.read_csv("./processed.csv", header = None)
pre = PreProcessor()
train, test = pre.shuffle_frame_pairs(df)
test_generator = training.generate_validation_data(test)
val_score = model.evaluate_generator(test_generator, steps=len(test))
data = predictions(test, model)
data.to_pickle("./predictions.pkl")
print(get_pred_mse("predictions.pkl"))