-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_param.py
161 lines (124 loc) · 5.33 KB
/
plot_param.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
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from scipy.optimize import curve_fit
def regression(x,y1):
# Assuming X contains ground truth distances covered and y contains errors
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(x.reshape(-1, 1), y1, test_size=0.2, random_state=42)
# Create a linear regression model
model = LinearRegression()
# Train the model
model.fit(X_train, y_train)
# Make predictions on the testing set
y_pred = model.predict(X_test)
# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
# print("Mean Squared Error:", mse)
# Predict expected error for new ground truth distances covered
new_distances = x.reshape(-1, 1) # New data points
expected_errors = model.predict(new_distances)
# print(expected_errors, y2[-5:-1], x[-5:-1])
# Assuming model is your trained linear regression model
slope = model.coef_[0]
intercept = model.intercept_
return slope, intercept, expected_errors
def main():
# Generate some sample data
base_dir = "Monocular-Visual-Odometry/data/Tello_dataset/line/consitancy/"
slope = []
intercept = []
for i in range(1,11):
# k = np.load(base_dir+f"dynamics_error_{i}.npy")
k = np.load(base_dir + f"dynamics_error_{1}.npy")
x = np.load(base_dir+"gt.npy")
m, c = regression(x,k)
slope.append(m)
intercept.append(c)
grad = np.mean(slope)
interc = np.mean(intercept)
gra_de = np.std(slope)
intec_de = np.std(intercept)
print(grad, interc, gra_de, intec_de)
# y1 = np.load(base_dir+"500_features_statics_error_.npy")
y1 = np.load(base_dir+"750dynamics_error_.npy")
y2 = np.load(base_dir+"500dynamics_error_.npy")
y3 = np.load(base_dir+"300dynamics_error_.npy")
y4 = np.load(base_dir+"100dynamics_error_.npy")
z1 = np.load(base_dir+"750dynamics_%error_.npy")
z2 = np.load(base_dir+"500dynamics_%error_.npy")
z3 = np.load(base_dir+"300dynamics_%error_.npy")
z4 = np.load(base_dir+"100dynamics_%error_.npy")
# print(np.mean(y2), np.std(y2))
rmse1 = np.sqrt(np.mean(np.square(y1)))
rmse2 = np.sqrt(np.mean(np.square(y2)))
rmse3 = np.sqrt(np.mean(np.square(y3)))
rmse4 = np.sqrt(np.mean(np.square(y4)))
x = np.load(base_dir+"gt.npy")
# Plot the three arrays on one graph
plt.plot(x, y1, label='750 Features,' +' RMSE = ' + "{: .2f}".format(rmse1)+'m' + ', 1.58 frames/s')
plt.plot(x, y2, label='500 Features,' +' RMSE = ' + "{: .2f}".format(rmse2)+'m' + ', 1.54 frames/s')
plt.plot(x, y3, label='300 Features,' +' RMSE = ' + "{: .2f}".format(rmse3)+'m' + ', 1.64 frames/s')
plt.plot(x, y4, label='100 Features,' +' RMSE = ' + "{: .2f}".format(rmse4)+'m' + ', 2.58 frames/s')
# # Add labels and legend
plt.xlabel('Distance covered in m')
plt.ylabel('Error in m')
plt.title('Error measurement of varying Optical flow quality')
plt.legend()
plt.grid()
# Show the plot
plt.show()
y1 = y2
# Assuming X contains ground truth distances covered and y contains errors
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(x[:-5].reshape(-1, 1), y1[:-5], test_size=0.2, random_state=42)
# Create a linear regression model
model = LinearRegression()
# Train the model
model.fit(X_train, y_train)
# Make predictions on the testing set
y_pred = model.predict(X_test)
# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
# print("Mean Squared Error:", mse)
# Predict expected error for new ground truth distances covered
new_distances = x.reshape(-1, 1) # New data points
expected_errors = model.predict(new_distances)
# print(expected_errors, y2[-5:-1], x[-5:-1])
# Assuming model is your trained linear regression model
slope = model.coef_[0]
intercept = model.intercept_
print("Slope (m):", slope)
print("Intercept (c):", intercept)
print(slope*x[-1]+intercept)
# Define the exponential function
def exponential_func(x, a, b):
return a * np.exp(b * x)
# Sample data (replace with your actual data)
x_data = np.array([1, 2, 3, 4, 5])
y_data = np.array([2.5, 3.5, 6.5, 10.5, 20.5])
x_data = x
y_data = y1
# Fit the data to the exponential function
popt, pcov = curve_fit(exponential_func, x_data, y_data)
# Extract the coefficients
a = popt[0] # Coefficient representing the initial value
b = popt[1] # Coefficient representing the rate of change
# Predicted values using the exponential model
y_pred = exponential_func(x_data, a, b)
# Print the coefficients
print(f'Coefficient a (initial value): {a}')
print(f'Coefficient b (rate of change): {b}')
# Plot the original data and the exponential fit
plt.plot(x, y1, label='Visual Odometry error in a dynamic environment')
plt.plot(x, y_pred, color='red', label='Linear model of the error')
plt.xlabel('Distance covered in m')
plt.ylabel('Error in m')
# plt.title('Error measurement of varying number of features')
plt.legend()
plt.grid()
plt.show()
if __name__ == "__main__":
main()