aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHongmei Gou2019-04-30 14:28:55 -0500
committerHongmei Gou2019-04-30 14:28:55 -0500
commitd562ac54800e5bb13fe73636f34b283442faacc0 (patch)
treee8776652735ed436493c2e541808dfe410f2397d
parent7c0aa20c966fa156076a40d20df389955949259d (diff)
parent7439abc642c7a7faf8c9262ebdc26a311a82027c (diff)
downloadpdm-anomaly-detection-d562ac54800e5bb13fe73636f34b283442faacc0.tar.gz
pdm-anomaly-detection-d562ac54800e5bb13fe73636f34b283442faacc0.tar.xz
pdm-anomaly-detection-d562ac54800e5bb13fe73636f34b283442faacc0.zip
Merge pull request #1 in PROCESSOR-SDK/pdm-anomaly-detection from pdm-demo-bringup to master
* commit '7439abc642c7a7faf8c9262ebdc26a311a82027c': Add ONNX model and python scripts for converting it to LSTM_model.h Add PdM anomaly detection code
-rw-r--r--LICENSE19
-rw-r--r--LSTM_model.h77
-rw-r--r--coeff.binbin0 -> 8008 bytes
-rw-r--r--config.cpp19
-rw-r--r--config.h26
-rw-r--r--logs/normal45-270-v100-with-friction2-iter10-15.logbin0 -> 8175616 bytes
-rw-r--r--lstm_infer.cpp32
-rwxr-xr-xmk.sh1
-rw-r--r--models/configheader.py66
-rw-r--r--models/make_header.py305
-rw-r--r--models/preprocessingheader.py108
-rw-r--r--models/readme.txt14
-rw-r--r--models/timotor20190227raw.onnxbin0 -> 18699 bytes
-rw-r--r--motor-pdm.cpp187
-rw-r--r--onnx_model.cpp389
-rw-r--r--onnx_model.h397
-rw-r--r--psensors.cpp144
-rw-r--r--readme.txt17
-rwxr-xr-xrun.sh1
19 files changed, 1802 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..f6cb9de
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,19 @@
1Copyright (c) 2016 Robert W. Rose
2
3Permission is hereby granted, free of charge, to any person obtaining a copy
4of this software and associated documentation files (the "Software"), to deal
5in the Software without restriction, including without limitation the rights
6to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7copies of the Software, and to permit persons to whom the Software is
8furnished to do so, subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all
11copies or substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19SOFTWARE.
diff --git a/LSTM_model.h b/LSTM_model.h
new file mode 100644
index 0000000..2d42757
--- /dev/null
+++ b/LSTM_model.h
@@ -0,0 +1,77 @@
1/*This is the header file generated from the ONNX model*/
2
3#include <string>
4#include <vector>
5
6unsigned int NUM_LAYERS = 2;
7unsigned int DENSE_WEIGHTS_ROWS = 31;
8unsigned int DENSE_WEIGHTS_COLS = 2;
9unsigned int DENSE_BIASES_SHAPE = 2;
10unsigned int DENSE_ACTIVATION = 1;
11
12std::vector<float> DENSE_WEIGHTS = {
130.210532277822, -0.846990406513, 0.55079549551, 0.0386005304754, 0.460019826889, -0.389443337917, -0.422828495502, -0.658000648022, -0.411930412054, -0.0299967080355, 0.0646565034986, -0.127355322242, -0.136991396546, -0.19303496182, 0.537825167179, -0.346626967192, -0.474057734013, 0.453149110079, 0.228727906942, 0.493378400803, 0.214479982853, 0.577478289604, 0.506602048874, 0.0788790583611, 0.271695792675, -0.262315154076, -0.65589261055, 0.1702619344, -0.455700933933, 0.282108694315, 0.377390474081, 0.149042129517, -0.33547013998, 0.464447796345, 0.19582682848, 0.535690009594, 0.368645846844, -0.560067415237, -0.268278330564, 0.503697216511, -0.222755014896, 0.405264765024, 0.240972399712, -0.269863516092, 0.299161523581, 0.103564433753, 0.0878492891788, -0.223425522447, -0.245210498571, 0.0221647024155, -0.532086789608, -0.0356784388423, 0.34632819891, -0.265819728374, -0.215275481343, -0.961182057858, -0.611859202385, -0.143221437931, 0.05197237432, -0.196741729975, 0.404271095991, -0.517124414444};
14
15std::vector<float> DENSE_BIASES = {
16-0.279018938541, 0.107939668};
17
18std::vector<unsigned int> LSTM_RECURRENT_ACTIVATION = {4};
19std::vector<unsigned int> LSTM_ACTIVATION = {4};
20std::vector<unsigned int> RETURN_SEQUENCES = {0};
21std::vector<unsigned int> N_HIDDEN_NEURONS = {31};
22std::vector<unsigned int> W_i_ROWS = {2};
23std::vector<unsigned int> W_i_COLS = {31};
24std::vector<unsigned int> W_f_ROWS = {2};
25std::vector<unsigned int> W_f_COLS = {31};
26std::vector<unsigned int> W_c_ROWS = {2};
27std::vector<unsigned int> W_c_COLS = {31};
28std::vector<unsigned int> W_o_ROWS = {2};
29std::vector<unsigned int> W_o_COLS = {31};
30std::vector<unsigned int> U_i_ROWS = {31};
31std::vector<unsigned int> U_i_COLS = {31};
32std::vector<unsigned int> U_f_ROWS = {31};
33std::vector<unsigned int> U_f_COLS = {31};
34std::vector<unsigned int> U_c_ROWS = {31};
35std::vector<unsigned int> U_c_COLS = {31};
36std::vector<unsigned int> U_o_ROWS = {31};
37std::vector<unsigned int> U_o_COLS = {31};
38std::vector<unsigned int> b_i_SHAPE = {31};
39std::vector<unsigned int> b_f_SHAPE = {31};
40std::vector<unsigned int> b_c_SHAPE = {31};
41std::vector<unsigned int> b_o_SHAPE = {31};
42std::vector< std::vector<float> > W_i = {
43{0.185322135687, -0.824516355991, -0.642393827438, -0.425822228193, 0.293285936117, -0.0121912341565, -0.324504047632, 0.597941279411, 0.311987578869, -0.136105984449, -0.0732097104192, 0.770671188831, -0.205151304603, 0.176611080766, 0.0811026617885, 0.845845758915, 0.771258175373, 0.199620261788, -0.151814773679, 0.265885561705, 0.608966231346, 1.08486700058, -1.02740645409, -0.0338779725134, 0.0174377560616, -0.676254630089, 0.171720176935, -0.744176924229, -0.0273252110928, 0.208006575704, 0.110072128475, -0.401547402143, -0.515101671219, -0.118443764746, -0.337929040194, 0.159724146128, -0.050756521523, 0.907452821732, -0.162338942289, -0.555124700069, -0.620603978634, -0.0859199911356, -0.0510654933751, -0.0102122807875, -0.30154183507, 0.0126685164869, -0.079158090055, -0.00292449118569, 0.237848713994, 0.0574446730316, -0.263453274965, 0.441671043634, 0.200348317623, 0.187376976013, -0.218234792352, 0.103939890862, -0.317922919989, 0.378643244505, 0.329482734203, -0.473105251789, -0.825138568878, 0.0711314529181}
44};
45std::vector< std::vector<float> > W_f = {
46{0.332748025656, 0.0523226633668, 0.110590398312, 0.526660919189, 0.791073739529, 0.815366089344, 0.456697463989, 0.704801619053, 0.796210944653, 0.0887657403946, 1.12468755245, 0.268723905087, -0.327608138323, -0.0266997776926, 0.481298834085, 0.605446517467, 0.723925292492, 1.04179382324, -0.0929877460003, 0.153678372502, 0.653793334961, 0.59051245451, 0.0277767088264, -0.231102436781, -0.232954755425, -0.25073248148, 0.420234024525, 0.370259165764, -0.139870449901, -0.390542477369, 0.44000634551, -0.620040655136, -0.268983364105, 0.658930122852, 0.209272459149, -0.0116781275719, 0.718103706837, 0.523001134396, -0.616794168949, -0.12540872395, -0.816522777081, 0.23254366219, 0.433318346739, 0.840964078903, -0.397520244122, -0.773413360119, -0.227990701795, -0.188090503216, 0.0812743529677, -0.217632204294, -0.710024833679, -0.229106917977, 0.0362048633397, 0.205409690738, 0.39506110549, 0.904763877392, -0.0510291755199, -0.236016601324, 0.0760101228952, -0.151532202959, -0.861478626728, 0.700033843517}
47};
48std::vector< std::vector<float> > W_c = {
49{0.0956307649612, 0.613787412643, 1.42434096336, -0.465489715338, -0.885539829731, 0.331036388874, -0.463429629803, 0.559589505196, -0.934589862823, 0.663712322712, 0.298835992813, 1.22075688839, 0.483471244574, -0.574919819832, -0.423118859529, 0.714640617371, -0.599209070206, 0.239795416594, 0.3150434196, -0.357135206461, -0.994406998158, 0.951134383678, 0.22748029232, 0.156485080719, -0.0671564117074, -0.834566652775, 0.784602880478, -0.389289140701, -0.446090251207, 0.0358697026968, 0.446910351515, -0.699675858021, 0.750261664391, 0.343975454569, -0.624528586864, -0.0853450447321, -0.350698500872, -0.797592699528, -0.309429317713, 0.849446773529, 1.02368044853, 0.415024578571, 0.439539432526, -0.143217951059, -0.0407542400062, 0.360431134701, 0.38262706995, 0.5365011096, 0.70000731945, -0.644960641861, 0.839984714985, 0.877264797688, -0.265248954296, 0.658727347851, -0.200940534472, 0.212469920516, 0.0511242486537, -0.259432256222, -1.01890397072, -0.219125330448, -0.0610960572958, -0.687466144562}
50};
51std::vector< std::vector<float> > W_o = {
52{-0.107104726136, -0.0896070674062, 0.802965939045, 0.352801054716, 0.350219219923, -0.168809011579, 0.648496985435, 0.746658265591, -0.248712971807, -0.24700962007, 0.370593160391, 0.572165369987, -0.42929688096, -0.421578377485, 1.03107631207, 1.03904819489, 0.523328661919, 1.42379152775, 0.151075184345, -0.186686187983, 0.324157714844, -0.154081508517, -0.0407108552754, -0.32866358757, -0.201205551624, -0.466150105, -0.0200563259423, 0.190965130925, -1.13164412975, 0.0166868362576, -0.308687716722, -0.514899194241, -0.0124524720013, -0.122631326318, 0.785848140717, 0.213736400008, 0.17377141118, 0.0467304028571, 0.0383704453707, 0.731132745743, -1.19046068192, 1.179885149, -0.22209353745, 0.0215659141541, -0.905560016632, -0.0975127071142, -0.0893698260188, -0.245528966188, 1.36968410015, -0.545720994473, -0.177812322974, -0.600349009037, 0.52797806263, 0.148664355278, 0.844377994537, -0.188374742866, -0.306566596031, 0.277793824673, 0.39385086298, -0.360828429461, -1.31102252007, 1.04591810703}
53};
54std::vector< std::vector<float> > U_i = {
55{-0.164487153292, -0.407542705536, 0.159912630916, 0.648254752159, -0.613477647305, -0.0732728615403, -0.274545997381, -0.260433137417, 0.280366152525, 0.474827498198, 0.280240088701, 0.395428329706, -0.113590732217, -1.12173962593, -0.329467684031, -0.350164711475, 0.160477310419, 0.403092175722, -0.402937591076, 0.00753971841186, 0.394124239683, -0.5355052948, -0.237472698092, 0.467440456152, -0.510356009007, 0.242923140526, 0.142857924104, 0.19709187746, -0.109128497541, -0.993200421333, 0.140478551388, -0.35166734457, 0.0562370121479, -0.639412999153, -0.167009696364, -0.106686957181, -0.126806974411, -0.571713209152, 0.450123429298, -0.622551620007, -0.28208220005, -0.0408553704619, 0.302280783653, 0.480019360781, 0.260600477457, 0.245027571917, -0.231700494885, 0.292674720287, -0.446479588747, -0.0386877022684, -0.029840502888, 0.241011783481, 0.704230546951, -0.28801497817, -0.31289306283, 0.0571827180684, -0.372752815485, 0.246893778443, 0.0210091192275, -0.0565862804651, 0.347900778055, -0.900236427784, -0.242373347282, 0.134584665298, -0.263310015202, 0.182442843914, -0.0910202264786, 0.304554104805, -0.795127093792, -0.428028792143, 0.146787345409, 0.192136183381, 0.0959995016456, 0.0966757833958, 0.200424581766, -0.113789923489, -1.16422116756, -0.24228258431, -0.256587892771, -0.274459004402, -0.803272068501, 0.0119419684634, 1.09562063217, 0.0718587115407, -1.26335823536, 0.733490884304, 0.265326201916, 0.0797169804573, -0.299673199654, -0.466012209654, 0.634292542934, 0.597472548485, 0.463436990976, -0.0922856032848, 1.16615080833, 1.0626142025, 0.1977789253, -0.00269439537078, -0.285869479179, -0.0248880460858, 0.286503612995, 0.135255530477, -0.621304273605, -0.206383794546, 0.179388433695, -0.30302387476, -0.240797460079, 0.717433273792, -0.262774467468, 0.187226936221, -0.422373026609, 0.0253499988467, -0.212347686291, -0.357483774424, 0.0767034813762, 0.543270349503, -0.218572050333, -0.48947134614, 0.343642055988, 0.767906725407, 0.130830034614, -0.134268254042, -0.549683153629, 0.548970878124, 0.624590337276, 0.480495393276, 0.445949167013, 0.522306740284, 0.00411103619263, 0.248325422406, 0.00594625947997, -0.475243955851, 0.2216501683, -0.0844806209207, 0.388139039278, -0.572655200958, -0.666153311729, -0.0269224829972, -0.415426880121, -0.203630030155, -0.0548370666802, 0.207273036242, 0.630436182022, 0.588027536869, 0.088676713407, -0.766153216362, 0.040290530771, 0.57573735714, -0.107092857361, 0.441415220499, -0.410136133432, 0.891629874706, -0.327789664268, 0.0766619220376, 0.265039891005, 0.929680109024, 1.94677746296, 0.775543987751, 1.72443604469, -0.148989260197, -0.132808059454, -0.564696550369, -0.528271377087, 0.00811330694705, 0.682681143284, 1.41143941879, -0.357699811459, 0.0670791938901, 0.297949612141, -0.261132359505, 0.302658081055, 1.06131291389, 0.546736717224, 0.40866419673, 0.106745764613, -0.608365654945, 0.563972592354, 0.6762560606, 0.814812242985, 0.812520265579, 0.149787783623, 0.602243185043, 0.0675226300955, 0.473846673965, 0.0268353205174, 0.722383499146, 1.390671134, 0.655241549015, 0.281917184591, 1.06690776348, -0.767260313034, -0.0510017499328, -0.585513174534, 0.138773754239, -0.312110543251, 0.418485760689, 0.860759079456, -0.55674302578, 0.204360798001, -0.804131507874, 0.584833920002, 0.0693883597851, 1.15516602993, 1.04773974419, 0.760290145874, 0.888959050179, 0.149687379599, 0.47254306078, -0.0609300397336, 0.445295840502, -0.311582297087, -0.19494214654, 0.386239141226, -0.165093749762, -0.165460571647, -0.56527364254, -0.374315232038, -0.138946682215, -0.800044417381, -0.114970937371, -0.0521705858409, 0.305533736944, -0.066392108798, 0.000597951002419, -0.0874828025699, -0.154430449009, 0.115694373846, -0.150818437338, 0.383218079805, -0.511324822903, -0.344857662916, -0.433309078217, 0.263184338808, -0.121228545904, 0.170186474919, -0.236393168569, 0.21826466918, 0.756543636322, 0.264278173447, -0.633802652359, -0.147351667285, -0.207492679358, -0.0609193146229, 0.00532165169716, -0.355129539967, 0.390977740288, -0.58093214035, 0.45332044363, 0.479072213173, 0.630649805069, -0.0380194149911, 0.274149060249, 0.373511403799, 0.049011003226, -0.37916713953, 0.164433777332, 0.279291093349, 1.19503068924, 0.0742072761059, -0.430000394583, -0.21801880002, 0.851600646973, 0.524434328079, 0.265210330486, 0.419434785843, -0.722075998783, 0.582435309887, 0.147055476904, -0.319798052311, 0.434719651937, 1.38389933109, -0.20397682488, -0.823963522911, 0.411769390106, 0.465590059757, 0.960044682026, -0.51882314682, 0.939898014069, -0.163087695837, 0.485007911921, 0.29119476676, -0.48755633831, -1.22392225266, 0.120499685407, -0.00624776491895, -0.153248459101, 0.451457649469, -1.25283944607, 1.03268873692, -0.569376468658, 0.616575062275, -0.339915305376, 0.600001573563, 0.690176486969, 0.353133827448, 0.882952451706, -1.48962819576, 0.670821905136, 0.224207192659, 0.514059305191, 0.843888282776, -0.15627284348, -0.395652532578, 0.539095163345, 0.696803152561, -0.298561513424, -0.172457501292, -0.0899025052786, 0.390438437462, -0.114717833698, 0.294596195221, -0.45960226655, 0.141172647476, 0.750059485435, 0.0719481036067, 0.420206606388, 0.222784742713, -0.44137391448, 0.244588196278, 0.670756518841, 0.0992781817913, 0.262624591589, 0.707923710346, 0.147759243846, -0.749383866787, 0.882630169392, -0.397331625223, 0.808132231236, -0.251220315695, 0.449114501476, -0.111033506691, -0.631867706776, -0.601980805397, 0.190706953406, 0.671102404594, -0.0186868663877, -0.576015949249, 0.114028081298, 0.398586988449, 0.18180270493, 0.119044058025, -0.0966572165489, -0.711808145046, -0.0299291517586, 0.061357807368, 0.212572187185, 0.0517120175064, 0.000841927016154, -0.0779775232077, 0.0772511437535, 0.164951711893, -0.0630072057247, 0.350497245789, -0.222510799766, -0.309259176254, -0.45270383358, 0.11665610224, 0.114547811449, -0.0482596047223, -0.294239163399, -0.336119443178, 0.807297766209, 0.725023388863, -0.471796900034, 0.182807207108, 0.0225853677839, -0.425974756479, 0.431024640799, -0.654430389404, 0.629156529903, 0.0387883260846, 0.513399243355, 0.43688544631, 0.397288948298, 0.169491678476, 0.288903951645, 0.167713269591, -0.821794748306, -0.414580583572, 0.14986024797, -0.476276963949, -1.17544054985, 0.107744380832, -0.326127499342, -0.514605522156, -0.0699354335666, 0.118005059659, -0.376910716295, 0.188896372914, 0.61270070076, 0.620187163353, 1.04264950752, 0.54366350174, 0.333110958338, -0.30417072773, 0.15594497323, 0.744870364666, -0.912702798843, 0.300396263599, 0.588330388069, 0.855659484863, -0.558107972145, 0.6105286479, -0.0117312464863, -0.434222072363, 0.0880842134356, -0.026462899521, 0.164516434073, 0.523595333099, 0.262407064438, -0.00614183349535, -0.504784703255, -0.220052748919, -0.1333951056, 0.901484549046, -0.409719914198, -0.1779910326, -0.0240657329559, 0.642626345158, -1.00084853172, 0.0544602125883, -0.0153825329617, -0.217744216323, 0.320210903883, -0.850863873959, 0.887492418289, -0.444515943527, -0.19537115097, -0.0819171667099, -0.747580111027, 0.150522857904, -0.0809876769781, 0.306356102228, -0.817374527454, -0.0831496715546, 0.683626651764, -0.104358166456, -0.215784758329, -0.364929646254, 0.0581333190203, 0.22409068048, 0.0140627184883, 0.0120426444337, 0.0403446182609, -0.0839538946748, -0.274827152491, 0.432591974735, 0.267429351807, 0.301961421967, -0.52459281683, 0.112877570093, -0.395714133978, 0.157957434654, -0.202919214964, -0.447013407946, -0.347543030977, 0.541160345078, 0.147864580154, 0.571336686611, -0.0288989376277, -0.277266472578, 0.596128940582, -0.244217306376, 0.818109214306, -0.374582409859, -0.503960847855, -1.12998652458, -0.298067510128, -0.374991089106, -0.327114880085, -0.364645600319, 0.0865003690124, 0.492246687412, 0.408382177353, 0.201397910714, -0.50353628397, 0.284086823463, 0.283284932375, -0.226132124662, 0.0656722337008, -0.43092417717, 0.0277040228248, -0.536834657192, -0.732004404068, -0.416810959578, 0.0975241810083, 0.936955571175, -0.794023096561, -0.172677233815, -0.0384489707649, 0.0177555419505, 0.440599888563, -0.268689066172, 0.542522311211, 0.183660060167, 0.397090375423, -0.0910913571715, 0.402351111174, -0.0908357128501, -0.174148067832, 0.0244021248072, 0.151753664017, 0.400920420885, 0.149080857635, -0.300013065338, -0.475381612778, -0.028100065887, -0.356042742729, 0.484075576067, 0.0740654245019, 0.378352046013, -0.315879017115, -0.126948535442, -0.12522906065, 0.175269782543, -0.0752810761333, -0.350499600172, -0.233585223556, 0.538995742798, -0.0347191430628, 0.443047851324, -0.300464123487, -0.337638616562, 0.556284606457, -0.256481736898, 0.12988114357, -0.478183090687, -0.0753780826926, -0.708748221397, 0.171034634113, -0.420573532581, 0.315279096365, 0.63263964653, 0.450077265501, 0.0206140894443, 0.286801755428, -0.249289557338, -0.525831580162, 0.517132937908, -0.126471325755, -0.0833186134696, -0.382867485285, -0.0735524222255, -0.494721859694, -0.324033051729, -0.0452652797103, 0.210131719708, 0.514053940773, -0.0110263107345, -0.956570386887, -0.339703917503, 0.208228453994, 0.0264142621309, 0.077341362834, 0.539237558842, 0.758221745491, -0.311438530684, 0.522510647774, -0.0517337098718, -0.708975791931, 0.435691028833, 0.287285894156, -0.549819648266, 0.0489376075566, -0.345976322889, -0.138600617647, 0.417212039232, 0.0213532391936, 0.0622787289321, 0.146891266108, -0.364064216614, -0.74403744936, -0.208205148578, 0.345515042543, 0.210370749235, 0.387875795364, -0.249161601067, 0.447263747454, 0.590879023075, -0.102956019342, -0.30993527174, 0.0979671478271, -0.462234795094, 0.0526035167277, 0.156474024057, -0.0577186346054, 0.100531190634, -1.19445288181, 0.126113474369, -0.0557793565094, 0.00289810402319, -0.300669878721, -0.523862719536, 0.547021925449, 0.0875920131803, 0.652453899384, 0.0691835731268, -0.207231938839, -0.771884024143, -0.309908807278, -0.777642190456, 0.246078327298, 0.211340993643, 0.189431622624, -0.0524844750762, -0.545048654079, -0.11482847482, 0.407315611839, 0.376557826996, -0.339429050684, -0.259641855955, 0.145675361156, -0.515954613686, 0.707189023495, -0.302177757025, -0.665942549706, 1.4158103466, -0.0196849759668, 0.380451321602, -0.056689646095, -0.51342433691, 0.102460436523, 0.533276855946, -0.661154747009, -0.1155404374, 0.214141726494, 0.788060307503, 0.309116214514, -0.347334384918, -0.757768213749, -0.285526365042, -0.321270138025, 0.517450809479, -0.279202580452, 0.507998287678, -0.475096255541, -0.33223977685, -0.228368580341, 0.0197465848178, -0.192769706249, -0.52666670084, 0.205842882395, 0.49493008852, -0.163632556796, 0.452393203974, -0.0329611040652, -0.181181535125, 0.181062892079, -0.305417060852, -0.485239565372, -0.819802105427, -0.336250424385, -0.404516607523, 0.0112056937069, -0.548414349556, 0.384314745665, -0.00844592228532, -0.176367342472, -0.166681304574, -0.536572158337, -0.455058038235, -0.127014622092, 0.749053537846, 0.00158268236555, -0.0975999906659, -0.601346969604, 0.30337318778, -0.220618084073, 0.153511270881, -0.400322914124, -0.494821369648, 0.589502334595, 0.793783426285, -0.167749494314, 0.358410328627, -0.0771593675017, -0.605319261551, 0.556215465069, -2.08747410774, 0.342077553272, 0.353174626827, 0.333294332027, -0.172520130873, -0.165387749672, -0.213544026017, -0.953555047512, 0.127406224608, 0.466204047203, -0.660779416561, 0.0761788710952, -0.0531936846673, 0.23602540791, -0.139079079032, 0.510132908821, -0.927884936333, 0.477215826511, 0.15670542419, -0.464307159185, 0.405231356621, 0.924576461315, -0.126855760813, 0.886300623417, 0.22820314765, 0.424698889256, 0.637558281422, -0.660719752312, 0.414879769087, 1.09232091904, -0.379344254732, 0.893301188946, -0.122210688889, 0.0453483648598, 0.824729979038, 0.0207926835865, 0.0403792373836, -0.0680488795042, -0.0480768457055, -0.319194704294, -0.112287148833, -0.369646340609, -0.0192338526249, -0.987628757954, -0.446307122707, 0.424960017204, -0.0494486391544, -0.270858675241, -0.414820849895, 0.309547901154, 0.0295022111386, 0.307085543871, 0.778900742531, -0.107606820762, -0.00586908869445, 0.113031215966, 0.0291796531528, 0.344350993633, 0.291163057089, -0.458524852991, -0.918518900871, -0.194868296385, -0.809211075306, -0.352026432753, 0.212357789278, -0.0104480003938, -0.877216100693, -0.286692887545, -0.24143640697, -0.496465653181, 0.228827640414, 0.100610949099, 0.233085379004, 0.314258515835, 0.41614151001, 0.376080274582, -0.478030204773, 0.410880923271, 0.12531080842, 0.42834675312, 0.0817243382335, -0.00883969664574, -0.881691634655, -0.570351064205, -0.637523770332, -0.495629072189, 0.291542351246, -0.435092538595, 0.221673086286, -0.366768181324, -0.160927116871, 0.67035150528, -0.615176916122, 0.267816901207, -0.190832167864, 0.447455942631, -0.224486753345, 0.675238370895, -0.683622658253, 0.247054189444, -0.0335351787508, -0.588586568832, 0.0193692930043, 0.294937759638, -0.0841782838106, -0.418257504702, -0.141904041171, 0.112735651433, -0.302489161491, -0.822585761547, 1.12332069874, 0.810719013214, 0.92630815506, 0.553161740303, 0.669064104557, 0.789175450802, 0.0595295540988, 0.328040480614, 0.142672494054, 1.26204276085, 0.703191518784, -0.319422125816, 1.00696074963, 1.56500065327, -0.180914118886, -0.27977013588, 0.212531447411, 0.922577500343, -0.0477373450994, -0.57782959938, 0.234480381012, -0.133011013269, 0.0124455792829, -0.0202074460685, -0.55104881525, 0.0753744319081, -0.0395135879517, -0.0474609397352, 0.0854560360312, 0.34555748105, -0.0113620730117, 0.108251556754, -0.0206192489713, 0.359737843275, 0.314718127251, 0.271229833364, -0.110280051827, -0.0929397493601, 0.277293533087, 1.07226550579, -0.282710403204, 0.23233217001, -0.168234527111, 0.109317548573, 0.968984484673, -1.5862557888, -0.162628695369, 0.0277825389057, -0.00271279597655, 0.632942199707, 0.546580553055, 0.143725767732, 0.41021296382, 1.04510653019, -0.323831290007, -0.496476531029, 0.293700605631, -0.27276724577, -0.304866164923, 0.223264828324, 0.315074622631, 0.106523774564, -1.04497349262, 0.640197455883, 0.410274893045, 0.6852222085, 0.322529107332, 0.651565909386, 0.739228010178, -0.047073662281, 0.272879123688, -0.150955498219, -0.236635446548, -0.168399587274, -0.268863230944, 0.255556851625, 0.035828627646, -0.179071620107, -0.343835622072, -0.0665528029203, -0.660620927811, 0.320234775543, -0.219391807914, -0.10191975534, 0.609591126442, 0.615102827549, 0.109470307827, -0.396675527096, 0.757627129555, -0.241236165166, -0.298540443182, -0.0442315675318, 0.313224673271, -0.024803051725, -0.839801967144, 0.129486396909, -1.34405696392, -0.416842848063, -0.511329770088, -0.320518761873, -0.162213683128, -0.56982165575, 0.0866421014071, -0.52089881897, 0.283288031816, 0.181297704577, -0.816463708878, 0.0414585545659, -0.168896883726, -0.131179049611, 0.252290189266, -0.354806959629, -0.0773894041777, 0.0907651484013, 0.803178548813, 0.123843699694, 0.17028132081, 0.226506382227, 0.314169317484, 0.175540968776, 0.0923963412642, -0.142761036754, -0.418033361435, 0.46586728096, -0.668575704098, 0.804650187492, 0.354451835155, -0.69633024931, 0.120617717505, 0.146716445684, -0.213579460979, -0.324909329414, -0.652023494244, -0.339829713106, -0.529457092285, -0.392566502094, -0.458715587854, -0.674575984478, 0.505500972271, 0.0594071224332, -0.105159506202, 0.347018748522, -0.413006663322, 0.828626155853, -1.09643268585, 0.0396186038852, -0.216222405434, -0.242173328996, -0.71623903513, 0.313321501017, 0.522072970867, 0.0715973451734, 0.330850750208, -0.662272572517, 0.379842311144, -0.00743951508775, 0.268450319767, -0.211613088846, 0.645617008209, -0.629408299923, -0.618042111397, -0.136103719473, -0.667649209499, 0.303324460983, -0.500855207443, 0.0376387983561, 0.198482051492, -0.310838550329, -0.689716219902, -0.496901482344, -0.100248679519, -0.291292279959, -0.95583409071}
56};
57std::vector< std::vector<float> > U_f = {
58{-0.579729378223, -0.240697517991, -0.391773641109, -0.0240022800863, -0.411112308502, 0.415624171495, -0.0047526913695, -0.269419491291, -0.368260592222, -0.812921464443, -0.109384700656, 0.0500135831535, -0.320259720087, -0.797238886356, -0.054978325963, -0.50250607729, -0.292500197887, 0.369918167591, -0.613622844219, -0.0181589126587, -0.188018590212, -0.582842886448, 0.0219135191292, 0.501310229301, -0.625574946404, -0.0332837402821, -0.290030449629, -0.0438743419945, -0.423029005527, -0.434717297554, -0.152828618884, 0.0307667832822, 0.187059700489, 0.143562138081, 0.467153996229, -0.200320243835, 0.324830114841, -0.187931135297, 0.0407067835331, -0.28892570734, 0.291180491447, 0.522511601448, -0.107481434941, -0.166256844997, 0.145582005382, -0.0777871981263, -0.579501390457, 0.0576013736427, 0.331325441599, 0.0442015863955, -0.237542748451, 0.125098735094, 0.146750941873, -0.0388164408505, 0.382148653269, 0.678474605083, 0.0561255514622, -0.249962866306, 0.49927714467, 0.490476608276, 0.18875233829, -0.152198985219, -0.189808696508, 0.272002488375, 0.223152041435, 0.114646427333, -0.45322740078, 0.219066694379, -0.386645436287, -0.423029690981, -0.12091396004, 0.0288026742637, 0.44174939394, -0.483458280563, -0.134082078934, -0.0279557500035, -0.704384088516, 0.12814360857, -0.254986315966, 0.433679521084, -0.570041418076, -0.0974168106914, 0.0302363745868, -0.433956056833, -0.789570391178, 0.99357765913, 0.738486230373, 0.162031829357, -0.832058727741, -0.414377868176, 0.439675629139, 0.107937663794, -0.343572825193, 0.0125817749649, -0.135692805052, -0.605703413486, 0.0514758676291, -0.210153132677, -0.572701394558, 0.617621719837, 0.279402196407, 0.286285996437, -0.08839494735, -0.436336070299, 0.266761064529, -0.137363597751, -0.324332386255, 0.431760847569, 0.911524832249, 0.288351804018, -0.608948409557, 0.184108152986, -0.0395081788301, 0.148599147797, 0.32482033968, 0.0214601289481, -0.499597609043, -0.504007339478, 0.531947016716, 1.00183415413, 0.244060441852, -0.234654963017, -0.284466505051, 0.92964053154, 0.146770775318, 0.366336911917, -0.0249889716506, -0.0122079923749, 0.217332869768, -0.110673725605, -0.117202416062, -0.241526171565, -0.167616009712, -0.891405284405, -0.31890463829, -0.27149784565, -0.09813477844, 0.264856249094, -0.346463918686, 0.659840703011, -0.228288516402, 0.235374420881, 0.370430916548, 0.167681753635, -0.342733711004, -0.387056231499, -0.925467073917, 0.344034463167, -0.341795682907, -0.108376920223, -0.709555804729, -0.311818391085, -0.0762023329735, -0.202133536339, 0.330305397511, -0.325266867876, 0.607773900032, -0.106952250004, 0.499734938145, -0.474227011204, -0.0895734354854, -0.141600623727, -0.501813828945, -0.298272758722, -0.13137434423, 0.610173463821, -0.0960054770112, -0.049923799932, -0.688569188118, -0.156922519207, -0.0889091566205, 0.176287263632, 2.88600850105, -0.314901798964, -0.573954343796, -0.768349409103, -0.299445867538, 0.0890558734536, 1.5026512146, -0.0780149847269, -0.628274023533, -0.641145825386, 1.57606589794, 0.726400136948, -0.664666235447, 0.276382386684, 0.551085412502, 0.290566623211, -0.366790264845, 1.15117657185, -0.444707900286, 0.0951429829001, -0.15641708672, 0.224048420787, -0.368727147579, -0.476319015026, 0.865709364414, -0.137042358518, -0.214546844363, -0.624815285206, 0.45021507144, 0.0785279050469, 0.0835598781705, 0.760127067566, 0.420478850603, 0.392497837543, -0.619300127029, -0.26378428936, -0.157186418772, 0.485694676638, -0.452785879374, 0.114374749362, -0.252173870802, 0.461731642485, -0.0714897140861, -0.13897703588, 0.188868910074, -0.128385365009, -0.657074272633, -0.243392631412, -0.0710246786475, -0.517055511475, 0.385283738375, 0.120848491788, -0.204280465841, -0.522168755531, -0.393322139978, 0.3502202034, -0.123526722193, -0.0982557758689, -0.40042924881, -0.229914024472, -0.494513064623, -0.321847587824, 0.194977909327, -0.397868782282, 0.067718654871, 0.131063088775, -0.0288040582091, -0.206563830376, 0.0182079095393, 0.271297633648, -0.179054841399, -0.264207214117, -0.69286519289, -0.418661177158, -0.262594968081, -0.568449556828, 0.269163787365, 0.137929677963, 0.0548856109381, 0.0584674961865, 0.41508808732, -0.875878870487, -0.586689352989, 0.0709109678864, 0.155825406313, 0.772737443447, -0.559619009495, -0.284445643425, 0.350556284189, 0.622111678123, 0.135816544294, 0.142503634095, 0.306301683187, -0.650290310383, 0.507817208767, -0.0417640991509, -0.234705746174, 0.188104748726, 0.905066311359, -0.239322468638, -0.0117977950722, 0.218682900071, 0.182186245918, 0.518645942211, 0.220537647605, 0.645013213158, -0.0675756558776, 0.46430426836, 0.206398323178, -0.562358379364, 0.313486456871, -0.798752069473, 0.489626199007, 0.0342953205109, 0.341822475195, -0.158351302147, 0.649500489235, 0.517660617828, 0.709735572338, 0.191699832678, 0.343735873699, 0.181281149387, 0.00268616294488, 0.755567312241, -0.478244155645, 0.617243826389, 0.253449887037, 0.963568270206, 0.397872716188, 0.273138165474, -0.261880934238, 0.60852009058, 0.535265922546, -0.69650042057, 0.858114480972, 0.744390308857, 0.233539000154, -0.229464411736, -0.647045850754, -0.0832387804985, 0.340829253197, -0.303880572319, -0.315676033497, 0.689193665981, -0.477797985077, -0.479350298643, -0.674902379513, -0.333230465651, -0.167654871941, 0.136966109276, 0.417031228542, -0.495346069336, -0.478774696589, -0.265086233616, -0.389864444733, 0.221837043762, -0.619465172291, -0.182093203068, -0.0861630067229, -0.60737067461, -0.784800291061, 0.170872405171, -0.111186757684, -0.870047688484, -0.866347312927, -0.551609218121, 0.242425218225, -0.242621302605, -0.725639164448, -0.246908560395, -0.376547545195, -0.680213928223, 0.184919148684, -0.408092558384, 0.476654082537, 0.21163316071, -0.109749421477, -0.247667014599, 0.102044299245, 0.65992307663, -0.322650372982, -0.164773181081, -0.798241734505, -0.296254038811, -0.382144272327, -0.165147379041, 0.603541791439, -0.427700132132, -0.123156808317, 0.190040752292, 0.300995379686, 0.232947990298, 0.483257859945, 0.230173125863, -0.676247477531, -0.159243002534, -0.414438933134, -0.0480983704329, -0.42405128479, -0.259621202946, -0.144406691194, 0.103743590415, 0.0430429130793, 0.616546213627, -1.23698651791, 0.558896720409, 0.268420279026, 0.0616095997393, -0.369732618332, -0.841801345348, 0.357456415892, 0.167683184147, 0.152035757899, -0.607366681099, -0.16161262989, -0.464338213205, -0.0268882736564, 1.47340667248, -0.36528813839, 0.36539876461, 0.395767390728, -0.0035567691084, -0.250310361385, 0.684129118919, 0.0685274973512, -0.180814534426, 0.00899717584252, 0.200045615435, 0.227090999484, -0.757442772388, -0.328870713711, 0.609381973743, -0.686259329319, 0.0379131250083, -0.251714229584, 0.341968119144, -0.419956296682, -0.183063656092, 0.0513952076435, -0.3560090065, -0.0566507615149, 0.0211090072989, 0.289371162653, -0.717945158482, 0.112896434963, 0.141625061631, 0.0602362863719, -0.308017522097, -0.646728754044, 0.609350085258, 0.544661879539, 0.252454012632, -0.200253546238, -0.491183161736, -0.376882940531, 0.649139404297, 0.412702530622, -0.128171145916, -0.255522102118, -0.344611078501, 0.669388175011, 0.0921176150441, 0.0969089344144, 0.655250191689, 0.415021896362, -0.0976783111691, 0.406340152025, -0.468279898167, -0.307071685791, 0.0665305256844, 0.287617266178, 0.81674182415, -0.390051871538, -0.0387920103967, -0.0731227695942, 0.473023176193, 0.0558998435736, 0.412459939718, 0.31103181839, -0.675665318966, 0.376408517361, 0.0329382121563, 0.0427423231304, -0.0929640829563, 0.225189611316, -0.027886737138, 0.124357715249, 0.236565813422, 0.304815322161, 0.615557909012, 0.448634266853, 0.454819411039, 0.389647275209, -0.033180937171, -0.701718509197, 0.096556827426, -0.131517618895, -0.42744666338, 0.196032688022, 0.0638964399695, 0.245661273599, 0.371226906776, -0.259907871485, 0.255794912577, -0.319425225258, 0.19839258492, -0.248832300305, 0.212510317564, -0.646145403385, 0.0419677756727, -0.183626249433, -0.51517701149, -0.117576546967, -0.198912248015, 0.526692569256, -0.00695317704231, -0.0801942497492, 0.41096329689, -0.135086968541, 0.569498598576, -0.423445791006, -0.0831989720464, -0.647928774357, -0.0476304143667, 0.211213365197, 0.574036061764, 0.0974889919162, 0.0040074181743, 0.340808212757, -0.28969591856, 0.22261941433, 0.194737628102, 0.209574952722, 0.441866368055, -0.274214893579, 0.220092102885, -0.0807638987899, 0.426905781031, 0.145819187164, 0.471180081367, 0.263562709093, -0.59473413229, 0.377283275127, 0.0465606302023, 0.00822502747178, 0.181930109859, 0.148309499025, -0.352126419544, 0.00746462587267, 0.301979899406, 0.2656917274, 0.715158879757, 0.304514884949, 0.382809013128, 0.293740808964, -0.661802053452, -0.617960214615, 0.401422053576, -0.617265880108, -0.369054377079, 0.369053930044, 0.309769362211, -0.353882044554, -0.140615493059, -1.03740394115, -0.355007350445, 0.352682679892, 0.40452259779, -0.223840564489, -0.439689874649, -0.567243874073, -0.149962797761, 0.0844186022878, -0.474122345448, 0.0611325576901, -0.0570296421647, -0.307366907597, -0.683964073658, 0.069020435214, 0.423344314098, -0.329758435488, -0.371612131596, -0.480321705341, 0.461175382137, -0.333911120892, -0.543587982655, -0.578899264336, -0.50201433897, -0.210972383618, -0.216104641557, -0.594712853432, 0.520093858242, 0.152158617973, -0.201858177781, -0.367576986551, -0.908140420914, 0.211481839418, -0.173844143748, -0.615172982216, -0.708517968655, -0.0951965451241, -0.273550033569, -0.275654107332, 0.512732207775, -0.566919386387, -0.0318128205836, 0.00217428128235, -0.211867466569, 0.129690721631, 0.269317597151, -0.417693078518, 0.00703907804564, -0.180487573147, -0.123906612396, -0.551772534847, -0.271103143692, -0.354702323675, 0.128252461553, -0.0385474413633, 0.106414414942, -0.247589573264, 0.00698315678164, -0.0887186080217, 0.294528365135, 0.180485606194, -0.274272561073, -0.0821488201618, -0.241977900267, 0.163563087583, 0.0889354273677, 0.394556999207, -0.0161370523274, 0.320505410433, 0.169003814459, -0.376825511456, 0.541992604733, 0.193778082728, 0.026314239949, 0.208189100027, 0.187958478928, -0.820899426937, 0.118026301265, -0.10349791497, 0.0394578352571, 0.0707554370165, -0.113599389791, 0.672230064869, -0.310120284557, 0.0898140221834, 0.350852161646, -0.203631088138, -0.123907469213, 0.203928560019, -0.251981019974, 0.281952947378, 0.243859305978, 0.121276192367, 0.445794135332, -0.314056575298, 0.0961595624685, -0.225795194507, 0.377101629972, 0.215090364218, 0.366714507341, 0.246535301208, -0.572864532471, 0.288375556469, 0.322296857834, 0.0266691055149, 0.31304949522, 0.042199190706, -0.455860853195, 0.334904879332, 0.232869058847, 0.287036687136, 0.312692373991, 0.0659090206027, 0.193059056997, 0.0881818458438, -0.0480946004391, -0.355062365532, -0.550945341587, 0.0731179788709, -0.354915201664, 0.27765712142, -0.0132603757083, -0.225266650319, -0.102469801903, -0.275519639254, 0.731525957584, -0.0246590003371, -0.299534469843, -0.225920438766, -0.228455320001, -0.327942460775, -0.278077065945, 0.54693543911, -0.194143459201, 0.0255733206868, -0.0270867347717, 0.503746747971, -0.0369442403316, 0.338712483644, 0.240481674671, -0.230860322714, 0.111184045672, -0.559044420719, 0.130134448409, -0.0424872450531, 0.00220347498544, 0.384383231401, -0.0701170191169, 0.622472584248, 0.185327991843, 0.360326111317, 0.0586520321667, 0.153322279453, -0.0570598803461, -0.504354476929, 0.692774772644, 0.39702039957, -0.155441090465, -0.456518620253, 0.034941226244, 0.0588169060647, -0.464926213026, 0.184596061707, 1.0513368845, -0.152978569269, -0.436923295259, 0.473860144615, 0.216794729233, 0.108939379454, 0.441177546978, 0.902628719807, 1.05245101452, -0.126265078783, 0.370298057795, -0.227412566543, -0.185615718365, 0.660686016083, 0.71403336525, 0.264653533697, -0.373310565948, 0.617018282413, 0.489990532398, -0.0935362055898, 0.0462726131082, 0.295728296041, -0.101450890303, 0.474373489618, 0.573818743229, -0.161887004972, -0.754527449608, 0.290287226439, 0.313316971064, 0.656889140606, 0.0972219333053, -0.0716303512454, 0.17260055244, 0.39352735877, 0.431635290384, 0.538031101227, 0.561284661293, -0.204435154796, -0.410260230303, -0.145853236318, 0.189406499267, 0.517140686512, -0.222079068422, 0.385745227337, 0.421404510736, 0.0715472251177, 0.10265763104, 0.419802725315, -0.473630845547, 0.254060775042, -0.632694244385, -0.208805173635, 0.0177669860423, -0.19705748558, 0.589730679989, -0.506344497204, 0.047658007592, -0.192752793431, 0.566913366318, 0.0692817866802, -0.246583238244, 0.0162540003657, -1.49162375927, 0.0491413548589, 0.150106698275, 0.399000257254, -0.152639985085, -0.246186479926, -0.59215515852, 0.437586903572, 0.373394668102, 0.305503159761, -0.61251950264, -0.241916432977, 0.390638768673, -0.624098658562, 0.0438898727298, -0.863707661629, -0.577601790428, -0.199276790023, 0.147946894169, -0.134792327881, -0.333698660135, 0.120271444321, 0.655990421772, -1.20608663559, -0.68566775322, 0.325098931789, -0.576812565327, -0.121651135385, 0.700207591057, -0.358716547489, 0.138019502163, 0.803461194038, 0.423798233271, 0.518534779549, -0.067420206964, -0.262941598892, 0.724140644073, 0.497597664595, -0.865565896034, -0.569445073605, 0.153895184398, 0.880490005016, -0.277198791504, 0.420099645853, -0.102995172143, 0.0871484652162, -0.53413105011, -0.212427526712, 0.486138731241, -0.174877434969, 0.0187892206013, 0.067981377244, 0.0417774654925, 0.182296946645, -0.592497587204, 0.556780397892, -0.175521165133, -0.264682203531, -0.0488861650229, 0.195661947131, -0.346799075603, -0.114110477269, 0.907555878162, -0.165068402886, -0.271177262068, -0.13010032475, 0.414531469345, -0.159666135907, 0.279220640659, 0.27878883481, -0.0521079413593, -0.00908483099192, 0.0546137131751, -0.221106931567, -0.259886592627, 0.247681424022, 0.36287266016, 0.245052456856, -0.810599148273, 0.745921313763, 0.208042383194, 0.0279369708151, 0.16183103621, 0.416752308607, -0.0265147443861, -0.268208086491, 0.49791431427, 0.455762803555, 0.0142332334071, -0.675378441811, 0.533760666847, 0.515494406223, 0.0783475190401, 0.433628559113, 0.558027625084, 0.366834670305, 0.354204088449, 0.115862496197, -0.0876316800714, -0.0248375348747, -0.504761517048, 0.48023557663, -0.141204029322, 0.663755238056, -0.0274682343006, 0.00642060115933, 0.0727510601282, -0.545299351215, -0.186217948794, -0.270892113447, -0.464565783739, -0.351237475872, -0.0882039666176, -0.181781664491, -0.564282953739, -0.573221623898, -0.320227593184, 0.250055462122, -0.332153171301, 0.000218620203668, -0.615575492382, -0.478811681271, -0.490893781185, -0.599205315113, -0.0468643195927, -0.37717962265, -0.465682476759, -0.476660132408, -0.803482353687, -0.232321605086, 0.0462054684758, 0.394775122404, 0.036762021482, -0.798236966133, -0.428599894047, -0.69172424078, -0.0666895136237, -0.539053976536, -1.49004662037, -0.280903965235, 0.149694919586, -0.526975214481, -1.11851418018, 0.148219779134, -0.267227470875, -0.404032796621, -1.52894961834, -0.54094350338, 0.0644419714808, -0.582399904728, -0.287099629641, -0.365315794945, 0.120949052274, -0.704033732414, -0.568006157875, -0.237801223993, -0.463440209627, -0.458468914032, -0.431300491095, -0.613944768906, -0.951498150826, -0.190325140953, -0.517063379288, -0.795393884182, -0.47432744503, -0.0943014621735, -0.278003245592, -0.0407003760338, -0.69560354948, -0.0227578505874, 1.11235868931, -0.207517802715, 0.463001102209, 0.103660002351, 0.060673866421, 0.38691174984, 0.234788775444, 0.143564611673, 1.44329452515, 1.06302022934, -0.469094455242, -0.132172107697, -0.0948769599199, 0.0846410617232, 0.385747849941, 0.351442813873, 0.0698994845152, -0.222074016929, -0.469291031361, -0.265184521675, 0.0497683249414, 0.35973444581, 0.30969157815, 0.111434161663, 0.523951888084, 0.092773668468, 0.280131906271, 0.386053591967, -0.415138632059, 0.108068540692}
59};
60std::vector< std::vector<float> > U_c = {
61{-0.476260900497, 0.363233804703, -0.0833832472563, -0.485053002834, -0.342705756426, 0.644412219524, -0.141952365637, -0.330390781164, -0.183823272586, 0.380058825016, 0.291497766972, -0.297042936087, 0.352544337511, -0.400815606117, 0.219869077206, 0.67021638155, -0.0782335475087, 0.460703641176, -0.603317856789, -0.0727846696973, -0.293882846832, -0.00808594282717, 0.192287102342, -0.636183381081, -0.295784294605, 0.18799290061, 0.219009041786, -0.14994302392, -0.113345332444, -0.545362830162, -0.296479672194, -0.0767128616571, -0.418924570084, 0.171866297722, 0.280353754759, 0.61053955555, -0.365708380938, 0.203083172441, -0.00563837448135, 0.399824887514, -0.256104558706, -0.0600364357233, -0.296908169985, -0.295463413, 0.260764271021, 0.0995672866702, -0.838992238045, -0.0102459685877, -0.140429675579, -0.00307480944321, 0.130755335093, -0.090179041028, 0.0287466980517, 0.244353726506, 0.0511544980109, 0.119147732854, 0.175830990076, 0.0809949710965, 0.665032565594, 0.599435567856, 0.526889681816, -0.318579733372, -0.088525891304, -0.378526329994, 0.480689913034, -0.302897900343, 0.873670756817, 0.439698904753, 0.141936421394, -0.236772105098, -0.336209595203, -0.2377910465, 0.0137182036415, -0.22522161901, 0.206687599421, 0.0298010352999, 0.244848906994, -0.174718618393, -0.0519334189594, 0.0680439993739, -0.232676193118, -0.304009646177, -0.549442529678, -0.301269084215, -0.221348389983, -0.684548377991, 0.0316169448197, -0.217143908143, -0.569811403751, 0.806551873684, 0.469743132591, -0.0124996053055, 0.0423225536942, -0.125226944685, 0.224116638303, -0.976225554943, -0.137934699655, -0.348645567894, -0.188023731112, -0.389280080795, 0.151754468679, -0.46952727437, -0.835674881935, 0.143766060472, 0.145547404885, -0.198087438941, -0.719173371792, -0.101115211844, 0.813134789467, -0.0540882870555, 0.0419979989529, -0.0675243362784, 0.357155710459, -0.0141962738708, 0.144741997123, -0.666016221046, 0.0315781757236, 0.417047381401, -0.37798666954, 0.382874697447, 0.713350594044, -0.256401181221, 0.182566106319, 0.215668484569, 0.62120449543, -0.0181763861328, -0.451997071505, 0.285670787096, 0.00353050790727, -0.14182458818, -0.699721038342, 0.0563862249255, -0.165772095323, 0.555769383907, -0.0757899656892, 0.248194664717, -0.419338285923, -0.299710541964, 0.072909116745, -0.0879716128111, 0.0628807023168, 0.101744689047, 0.21888923645, -0.361379235983, 0.245139867067, -0.304993212223, -0.525156974792, 0.000873345532455, 0.480335444212, -0.439277261496, -0.940974533558, -0.2945522964, -0.589957952499, -0.0413432493806, 0.349855303764, 0.0232693050057, -0.584975838661, 0.0588083229959, -0.153540045023, 0.468829095364, 0.615237414837, -0.470928817987, -0.449805498123, 0.374449640512, -0.28412887454, 0.12635101378, -0.116223283112, 0.0188229270279, -0.0936760380864, 0.372985363007, -0.165058523417, 0.104007296264, 0.577144622803, -0.309218585491, -0.0728223621845, -0.0823996290565, -0.514923930168, 0.102164700627, -0.799597203732, 0.0363731980324, 0.550311565399, -0.0905897021294, -0.550099670887, 0.0831759348512, -0.309256374836, -0.323443651199, -0.11093480885, 0.0276359412819, -0.272071838379, -0.100034013391, 0.823453009129, 0.545949459076, -0.0677605867386, -0.160879194736, 0.521573483944, 0.320608735085, -0.112215302885, -0.0835302919149, -0.0640911534429, 0.0208857357502, 0.342691481113, -0.461957722902, 0.43108317256, -0.223896756768, -0.344851404428, -0.593664228916, -0.374976158142, -0.529339671135, -0.340695679188, -0.195017486811, -0.0974618121982, 0.301549136639, -0.333785295486, -0.241046234965, -0.277208000422, -0.297574132681, -0.889755189419, -0.128550425172, -0.180047512054, 0.395120829344, -0.26941677928, 0.0909226536751, 0.201723411679, 0.114779748023, -0.128941982985, -0.336668938398, -0.165393739939, 0.195176973939, -0.115624532104, 0.546708703041, 0.209586113691, -0.0140945501626, -0.00315453624353, -0.205053970218, 0.230554044247, 0.0213234517723, -0.104124419391, -0.301980614662, 0.148923546076, 0.392650425434, -0.251561999321, -0.785662829876, 0.0589303523302, 0.241800203919, 0.0649228617549, 0.128587007523, -0.324615091085, -0.258906751871, 0.473769158125, -0.201317116618, -0.30278018117, 0.459508240223, -0.551006257534, -0.493169516325, -0.566304624081, 0.236484885216, -0.148979380727, -0.389608681202, -0.404490768909, 0.0119635472074, -1.31970310211, -0.556810379028, -0.264738619328, 0.379410684109, -0.0107369152829, -0.356728494167, 0.328995794058, -0.39622810483, 0.538003504276, 0.258066534996, -0.556933164597, 0.346493691206, 1.00652253628, -0.244926318526, -0.139648661017, -0.704957127571, -0.127299875021, 0.951733648777, 0.87039411068, -0.0942110344768, -0.703218221664, 0.391133487225, 0.147731512785, -0.0410939380527, -0.185838386416, -0.0311698466539, 0.242173612118, 0.241510942578, -0.051217790693, -0.319134384394, 0.0297412499785, -0.036984115839, -0.227399155498, -0.169823780656, -0.369953095913, -0.0721863731742, -0.644687533379, -0.16028419137, 0.0497789382935, -0.29839065671, 0.704463064671, -0.227847725153, 0.404548168182, 0.523773372173, -0.294720172882, 0.307150661945, 0.608956992626, 0.521479964256, 0.545867919922, 0.291637718678, -0.0610913224518, -0.49217441678, 0.18885949254, 0.372742950916, 0.180887162685, -0.183727517724, -0.401921272278, -0.194158658385, 0.510446310043, 0.496043473482, -0.456822991371, -0.109792202711, -0.148828849196, 0.471700459719, 0.196458950639, -0.635698974133, 0.11160877347, -0.279748171568, -0.128322467208, 0.136973157525, 0.152544185519, -0.236385956407, -0.503781616688, 0.382612198591, 0.323580741882, 0.142536506057, -0.198670223355, -0.747487068176, 0.261089384556, 0.359869539738, -0.18870177865, -0.249920725822, -0.339892119169, 0.898423433304, -0.259360045195, -0.370633035898, -0.148609548807, -0.52971470356, -0.102137736976, -0.299098163843, -0.411450147629, -0.0975743830204, -0.101265944541, 0.165021672845, -0.182647541165, -0.106156222522, 0.119657792151, -0.208211377263, -0.314904481173, 0.00797762628645, 0.0112475622445, -0.357670992613, 0.404539048672, -0.181079149246, 0.285740882158, 0.202332496643, 0.0608167313039, 0.287276834249, -0.457862734795, 0.309880703688, 0.220650374889, -0.220054715872, 0.138211980462, -0.699477553368, 0.200755447149, 0.589909553528, 0.5602029562, 0.723262965679, -0.55708104372, -0.214356705546, 0.183191120625, -0.233613148332, 0.303535014391, 0.0857863500714, -0.135876402259, 0.0586606673896, 0.379069656134, -0.474004983902, 0.0476555936038, 0.294212818146, 0.119344219565, -0.0216223876923, -0.0461735762656, -0.364545404911, -0.589071631432, -0.912951231003, 0.252641409636, 0.11589153111, -0.159700453281, -0.326051563025, -0.0938815027475, -0.423280090094, -0.341553837061, 0.472398608923, 0.135508596897, 0.170394644141, 0.165682211518, 0.195007026196, -0.367504954338, -0.211165055633, 0.270498007536, -0.345787256956, 0.270992875099, -0.626690804958, 0.0325551852584, -0.188837721944, -0.389090031385, -0.166293501854, 0.0529769212008, -0.118561267853, -0.621582448483, 0.634092748165, 0.0746784806252, 0.143406122923, 0.0433976128697, -0.251817017794, 0.482971280813, 0.589926958084, -0.395296573639, 0.152519837022, -0.0756794661283, -0.442326337099, 0.258955031633, 0.034086432308, 0.148679450154, 0.194083034992, -0.303751885891, 0.347468614578, -0.219399750233, -0.360961973667, -0.101252168417, 0.0643278285861, 0.30460986495, -0.015496795997, -0.317508518696, -0.0715812370181, -0.753070294857, -0.28467848897, -0.0151374032721, 0.0739413350821, 0.173116534948, -0.368360966444, -0.0328403823078, -0.081761687994, 0.339223831892, -0.180474817753, -0.392735540867, 0.313722401857, 0.832724452019, -0.104680746794, -0.367193907499, 0.125167712569, -0.0288922246546, 0.380321443081, 0.355877846479, -0.342632472515, 0.0361616387963, 0.691120743752, -0.131167218089, -0.123408719897, 0.173001527786, 0.362556248903, 0.111326254904, 0.148095831275, 0.276494145393, -0.0422801114619, 0.145789191127, 0.456580221653, 0.687743604183, -0.0635361149907, -0.487026482821, -0.0536798760295, 0.290499329567, -0.257252365351, 0.284238129854, -0.138214468956, 0.271385014057, 0.484819829464, -0.256872206926, -0.447736144066, 0.512245833874, 0.756500661373, 0.411867111921, 0.338565587997, -0.674514770508, -0.305169910192, -0.0578949861228, 0.400421053171, -0.1137194857, 0.189531028271, -0.487585872412, -0.64809679985, -0.0162205863744, 0.134344518185, 0.0696172714233, 0.260535269976, -0.285728007555, -0.0316427461803, -0.670778751373, -0.337795048952, -0.0996651872993, -0.0476932935417, 0.0986134260893, -0.249471321702, -0.00814099423587, 0.0463192090392, 0.421680003405, 0.126967847347, -0.177553460002, 0.385054379702, 0.414351195097, -0.369618564844, -0.0579629801214, 0.021403664723, -0.033271830529, 0.275448292494, 0.374975293875, 0.167479857802, -0.218379050493, 0.279372572899, 0.645205140114, 0.0896425694227, -0.404523164034, -0.352546989918, -0.0341688767076, -0.0414383932948, 0.587391495705, -0.482531934977, 0.0543696880341, -0.331482619047, 0.316195726395, 0.0803859755397, -0.239632353187, -0.167191550136, -0.0157887861133, 0.0669859647751, 0.152309507132, 0.104764387012, 0.100833669305, -0.236839190125, 0.184243693948, 0.12041465193, -0.241180419922, 0.599929630756, -0.111249998212, 0.476123690605, 0.0275491196662, 0.569387614727, -0.434991449118, 0.129588022828, -0.133763894439, -0.3095895648, -0.298461914062, 0.436980426311, -0.113903388381, -0.171226501465, -0.285572528839, -0.150056272745, 0.153229266405, -0.378641486168, 0.668850421906, -0.30952295661, 0.079751804471, 0.434199899435, -0.0930515751243, 0.253315925598, -0.183142960072, -0.0819519758224, -0.293278932571, -0.116167038679, 0.0264510959387, -0.319119006395, -0.307311266661, 0.00481956452131, 0.237418532372, 0.0794789716601, -0.197824135423, -0.357526659966, -0.256382912397, 0.132777735591, 0.318279892206, -0.0531618222594, 0.233718603849, 0.0451786406338, -1.05805635452, -0.313412338495, 0.452723920345, -0.399793833494, -0.148460030556, -0.177501484752, 0.0179707873613, 0.0127085121349, -0.172698259354, -0.425784230232, -0.323390215635, -0.00596148846671, -0.251144081354, 0.675697743893, -0.0884841531515, 0.299249857664, 0.117154799402, -0.354994535446, 0.478085845709, 0.00279094441794, -0.432502776384, -0.0144447721541, 0.160884097219, -0.216258585453, 0.000426440092269, 0.661960244179, -0.242449179292, 0.596243798733, 0.135634630919, 0.0584105923772, -0.528117835522, -0.842608809471, 0.316155642271, 0.2716332376, -0.338095009327, 0.642013311386, -0.433895766735, -0.22602199018, -0.287353634834, -0.0706792771816, -0.186097919941, 0.0319171585143, 0.00208826502785, -0.131310775876, -0.0801445096731, 0.227182686329, 0.0650725141168, 0.310885101557, 0.214585527778, 0.412424534559, 0.226669490337, -0.234037399292, 0.251838862896, 0.369032859802, 0.0231499746442, 0.0203344989568, 0.228693440557, -0.184843987226, -0.223295599222, 0.779653549194, -0.370320439339, 0.25665307045, 0.269827008247, 0.376437872648, -0.404281556606, -0.092129483819, -0.368779033422, 0.483608454466, 0.173235580325, 0.425827860832, 0.232187747955, 0.18199492991, -0.356931865215, -0.0460466444492, 0.208427265286, -0.0528370328248, -0.0866274833679, -0.254204571247, -0.0155757078901, 0.20847697556, -0.44856569171, -0.61966252327, 0.473109811544, -0.196080774069, -0.109102070332, 0.306837946177, -0.484677553177, -0.751867175102, 0.0579593889415, -0.588668525219, 0.748274803162, 0.467081189156, 1.12538015842, -0.390274226665, -0.422624707222, -0.306598633528, 0.709831893444, 0.150826081634, -0.544201254845, 0.639782190323, 0.198120981455, 0.586140692234, 0.0408927612007, -0.264283806086, -0.0716437771916, -0.203621849418, 0.192969188094, 0.111010782421, -0.241321623325, -0.409180402756, -0.383242964745, -0.18724553287, 0.54532033205, 0.31904026866, 0.0199969820678, -0.224509179592, 0.244250103831, -0.0662090182304, -0.165152788162, -0.045413069427, 0.343041598797, -0.163938015699, -0.528438508511, -0.267112582922, 0.309718817472, 0.539167582989, 0.0311706736684, -0.159111067653, -0.742110669613, 0.566340386868, 0.0272132866085, 0.465217143297, -0.291708409786, -0.125376835465, 0.12600903213, 0.0662904083729, 0.233259633183, 0.17283192277, 0.0822020471096, 0.0778817385435, -0.000820057699457, 0.546179831028, -0.208696439862, -0.656307697296, 0.134253963828, -0.209758907557, 0.278120070696, -0.25758394599, 0.0547419860959, -0.372287690639, 0.302204281092, 0.407633006573, 0.205236673355, -0.275920569897, -0.507725775242, 0.124991588295, 0.676386356354, 0.360373944044, -0.529384315014, 0.499229520559, 0.0967811048031, 0.0783446803689, -0.0638069063425, 0.0171148944646, -0.398391872644, 0.532773792744, -0.171478495002, 0.27536419034, 0.188025057316, 0.224178776145, 0.299687176943, 0.425920933485, 0.201258227229, 0.0184365585446, -0.14245171845, -0.114034496248, 0.481977254152, 0.649473726749, 0.313895821571, 0.113943733275, 0.675558567047, -0.00417251931503, -0.461911857128, -0.367138504982, 0.185348391533, 0.522893369198, -0.36487314105, -0.411548554897, 0.21370986104, 0.410751491785, -0.835658490658, -0.666639745235, -0.406063914299, -0.438852399588, -0.281861245632, 0.111891634762, -0.0303278062493, 0.160539522767, -0.234898611903, 0.0998264029622, -0.0865570083261, 0.504289090633, -0.155623584986, -0.631342113018, 0.332029283047, 0.349530756474, -0.618217110634, 0.222039744258, -0.82423555851, -0.828057467937, 0.295978248119, -0.827246844769, 0.0338012427092, -0.240551695228, 0.722697257996, -0.316399693489, 0.613618195057, 0.459160745144, 0.178954139352, -0.214243292809, 0.0607495345175, -0.244216859341, 0.20762732625, 0.241332963109, 0.653276145458, 0.299086630344, 0.106622710824, -0.204596489668, -0.062441252172, 0.15427698195, 0.133646249771, 0.148002400994, -0.373497158289, -0.290177345276, 0.126465275884, -0.412142604589, -0.47807008028, 0.628624022007, 0.0914865732193, 0.0323539301753, -0.173378914595, -0.469193726778, -0.828047573566, -0.361548185349, -0.168318152428, -0.323063850403, -0.124064549804, 0.0582916922867, 0.438749343157, 0.68442940712, -0.300360709429, 0.294909328222, 0.128743678331, 0.348173558712, -0.228440105915, -0.0270835030824, -0.433656573296, 0.369668841362, -0.308600515127, 0.365284055471, -0.673052966595, -0.189704492688, -0.0530697032809, -0.0168927740306, -0.274845063686, -0.199872329831, 0.182394474745, -0.819949150085, 0.226003870368, -0.227072358131, -0.361823171377, -0.160746246576, -0.223619773984, -0.670349121094, -0.156297490001, -0.573216974735, 0.248604759574, -0.542316496372, 0.368392884731, 0.142644420266, 0.147660300136, -0.0473959781229, -0.142904952168, -0.497010856867, 0.157088115811, 0.330286502838, 0.183208569884, -0.0248327944428, -0.1519010216, 0.088474586606, 0.205937057734, -0.136989936233, 0.27269411087, 0.508547544479, 0.135484799743, -0.293869286776, -0.041024826467, -0.00393711589277, -0.220801159739, 0.309434950352, -0.520574510098, -0.299849301577, -0.56118786335, -0.691200375557, -0.23916245997, -0.105003371835, 0.238450333476, -0.321029871702, -0.172825857997, -0.973243355751, 0.0147989718243, 0.673430562019, 0.561264395714, -0.66096585989, -0.0373016521335, 0.240049481392, 0.147330850363, 0.255987077951, 0.290556788445, -0.663549125195, 0.262611269951, -0.233406290412, 0.2513474226, 0.253098130226, 0.136670470238, -0.144896656275, 0.364002764225, 0.975185513496, -0.0405109785497, -0.448907256126, 0.460708618164, 0.272006630898, 0.277762204409, 0.0219166465104, -0.448736727238, -0.0534540005028, -0.784090459347, 0.470430612564, -0.0264166090637, -0.904743492603, -0.350075691938, 0.41157707572, 0.150189146399, -0.117259636521, 0.17916546762, -0.695175230503, 0.379684358835, -0.0881545096636, 0.373303085566, 0.0430721864104, 0.0381501764059, 0.477151572704, 0.00988717470318, -0.0436263419688, -0.422077775002, -0.0866391733289, -0.603933274746, 0.148453131318, 0.221070036292, -0.108046241105, -0.303051143885, 0.441788882017, -0.00440698349848, 0.365677624941, 0.293478250504, -0.270659476519, -0.319292187691}
62};
63std::vector< std::vector<float> > U_o = {
64{-0.230232179165, 0.0205486081541, -0.401903271675, 0.109920702875, -0.691217064857, 0.0470181033015, 0.133465856314, -0.173473194242, -0.0576262511313, 0.183640822768, 0.405053257942, 0.24936440587, -0.594521760941, -0.718641936779, -0.226488083601, -0.299191623926, -0.39187514782, 0.345947921276, -0.218880310655, 0.285045921803, -0.436422526836, -0.0438544116914, -0.0655047297478, -0.130614161491, 0.301144987345, 0.572843015194, 0.159976229072, -0.451845765114, -0.365841031075, -0.810667037964, -0.140651345253, 0.00223093712702, -0.0169935990125, 0.716104567051, 0.487425595522, -0.140877991915, 0.646854639053, -0.339587509632, 0.174669191241, -0.483658313751, 0.2889328897, -0.132459461689, 0.220022514462, 0.555271863937, -0.141440182924, 0.0555354841053, -0.179256826639, 0.289142340422, -0.434483736753, -0.10009033978, -0.495820224285, 0.512155056, -0.356727391481, -0.211471363902, 0.421799659729, 0.234898149967, -0.706707239151, -0.448694169521, 0.659977853298, -0.0469280704856, 0.0946899652481, -0.586544394493, 0.139435768127, 0.38280826807, 0.0721923559904, 0.186812922359, -0.185512423515, 0.72023499012, -0.485877066851, -0.0822856649756, 0.212789356709, 0.218568772078, -0.266736507416, -0.607727885246, 0.000461816671304, -0.0694766789675, 0.0195119883865, -0.264453053474, -0.298973470926, -0.93114644289, -0.130476161838, 0.056644808501, -0.169811531901, -0.146569475532, 0.423955261707, -0.457307547331, 0.142826199532, 0.321131616831, -0.180789917707, -0.0959079638124, 0.22587376833, -0.0663760453463, -0.164583325386, -0.140932574868, -0.199941024184, -0.181526333094, -0.0203421991318, 0.195820048451, -0.157560244203, 0.446546345949, 0.0922764390707, 0.28920724988, -0.0416567362845, 0.288586825132, 0.784974098206, -0.604826271534, 0.00307735870592, -0.00698337610811, -0.303714960814, 0.0230147317052, -0.486879169941, -0.162032619119, 0.368861049414, -0.259443819523, 0.407054841518, 0.148995965719, -0.416047304869, 0.259648382664, 0.28815099597, 0.762510418892, -0.902653813362, 0.440671890974, 0.629763305187, 0.335902243853, 0.835532844067, 0.0369899496436, 0.17930842936, 0.0786409452558, 0.106598071754, 1.23513257504, 0.616916179657, 0.29204210639, -0.0242422111332, -0.153707757592, 0.127918958664, -0.119772262871, 0.544666051865, 0.324779838324, 0.239006921649, -0.0924495905638, -0.607812285423, 0.769949734211, 0.601655304432, -0.0130537552759, -0.583716511726, -0.686632812023, 0.483102440834, -0.0151197090745, 0.709682703018, 0.758449852467, -0.140919730067, 0.293156296015, -0.537789642811, 0.63957130909, -0.200582161546, 0.133771359921, 0.4561406672, -0.0509933903813, 0.199158996344, -0.603383898735, 0.13383051753, -0.958775579929, 0.19699332118, -0.0974606499076, 0.317563951015, 0.680873453617, 0.212780579925, -0.0407421551645, -0.425788760185, 0.396682858467, 0.385065585375, 0.283968031406, -0.964271366596, 0.218730434775, 0.482916682959, 0.332988202572, -0.223672240973, -0.032158844173, -0.260298401117, -1.68755960464, 0.441846311092, 0.0651218891144, -0.226209715009, -0.291343122721, 0.260928273201, 0.0178945269436, 0.823356032372, 0.00574315525591, 0.00569802382961, 0.332162946463, -0.484819918871, 0.268787294626, 0.21328894794, 0.053284380585, -1.03840112686, 0.158958375454, 0.624661266804, 0.436532795429, 0.038346990943, -0.197258070111, 0.108011722565, 0.334964424372, 0.452759444714, -0.0072251195088, 0.14552937448, 1.26336550713, 0.434452921152, -0.25795352459, -0.0116482712328, 0.085065074265, -0.738467156887, 0.120552845299, 0.115385010839, -0.215058803558, -0.497080981731, -0.138640195131, -0.924027681351, -0.048629976809, 0.269809126854, 0.211867496371, 0.230882197618, 0.0495937131345, -0.358516395092, -0.371314316988, -0.188390687108, -0.115469604731, 0.486744374037, -0.210745409131, 0.0571626313031, -0.405347138643, -0.0724542587996, -0.125774487853, 0.214891970158, -0.182782143354, -0.497832238674, -0.15929967165, -0.0369414910674, -0.108604483306, 0.111244261265, -0.489217430353, -0.528547942638, 0.186059564352, -0.00850142166018, 0.0436340086162, 0.335779368877, 0.145701736212, -0.685704827309, 0.378927499056, 0.814988493919, 0.0138626499102, -0.371524035931, 0.274067401886, 0.679609954357, 0.483737826347, 0.131814718246, 0.351422905922, 0.537636816502, 0.174367129803, 0.0121098076925, 0.246450707316, 0.17421156168, 0.284833520651, 0.142588481307, 0.407764256001, 0.307735145092, 0.337948232889, 0.520263254642, 0.479906588793, 0.417292118073, 0.317961573601, 0.369405925274, 0.383081316948, -0.141126438975, 0.269783645868, 0.324794441462, 0.174264892936, -0.150778159499, 0.660377502441, -0.393305420876, 0.484085559845, -0.937421798706, -0.156022235751, -0.31157925725, 0.00634649256244, 0.114026561379, -0.0178149919957, 0.306646317244, -0.52957957983, 1.02926778793, -0.50990998745, 0.968846261501, 0.0998627468944, -0.322920173407, 0.189471021295, -0.117565631866, 0.455198228359, -0.0231772474945, 0.0894395112991, -0.105348929763, 0.61057806015, -0.236860319972, -1.05028641224, 0.395838499069, -0.662855446339, -0.0940272808075, -1.11867642403, 0.390377759933, -0.0936558395624, -0.556634426117, -0.666942358017, -0.466880619526, 0.3956579566, 0.499339640141, 0.280326336622, 0.386395812035, 2.36877240241e-05, -0.497093200684, 0.075920149684, 0.120664410293, 0.102253749967, -0.0604435428977, -0.29989272356, 0.192053884268, 0.0875131040812, 0.184831604362, 0.837235987186, -0.137301489711, 0.510612308979, -0.190522491932, -0.0028200247325, 0.0143071198836, -0.0160747561604, -0.274926126003, 0.236399829388, -0.602124333382, 0.336574524641, -0.302964925766, 0.800043821335, -0.154450595379, 0.317300826311, 0.488817811012, 0.108004659414, 0.375342041254, -0.172089263797, 0.386206269264, -0.219486773014, 0.310803622007, 0.12701100111, -0.0349657200277, 0.621266067028, 0.596777200699, -0.078187443316, -0.355752080679, -0.0467068068683, 0.0521030016243, 0.114057518542, -0.0252706017345, 0.150653958321, -0.0470893755555, 0.0066180867143, -0.0278404839337, 0.237126126885, 0.810919880867, -0.0553170219064, -0.146957442164, 0.489920407534, -0.301968842745, 0.598005890846, 0.113271549344, 0.436753541231, -0.714772462845, 0.345493525267, -0.0359437949955, 0.174665823579, 0.378943115473, 0.021457279101, -0.426008880138, 0.0124987950549, -0.526060521603, 0.200031235814, -1.24587607384, -0.597509741783, 0.714687824249, 0.871401548386, -0.231417968869, 0.0194307602942, 0.33912011981, -0.290394037962, 0.136520594358, 0.362242549658, -0.245033979416, 1.09198760986, 0.323082149029, 0.196922659874, -0.474113762379, -0.154468342662, -0.441907376051, -0.59861344099, 0.0561420880258, 0.380189597607, 0.377747148275, -0.255721896887, 0.180880144238, 0.362433642149, 0.721752762794, 0.838150918484, 0.182588756084, 0.0935470685363, 0.305843383074, -0.324756771326, -0.310819387436, -1.16464281082, -0.290249615908, -0.50999546051, 0.283817321062, -0.434470951557, -0.126545652747, -0.295378774405, 0.565843939781, -0.807359278202, -1.00800383091, 0.197524830699, -0.442735254765, -0.749307096004, -0.769624471664, 0.456985831261, -1.21000897884, 0.0859573632479, -0.0873735621572, -0.112147927284, -0.301290512085, -0.171894684434, 0.281311690807, -1.19254004955, 0.0904107093811, -0.290790945292, -0.145752802491, -0.19665145874, -0.284464478493, 0.771230578423, 0.373421400785, 0.182227656245, 0.180574789643, -0.132402479649, 0.246747225523, 0.0957783013582, 0.390556126833, -0.0179129913449, -0.0157641544938, -0.394919961691, 0.132176667452, 0.350833982229, 0.152620702982, 0.100988700986, 0.0881125405431, -0.156632050872, 0.400717109442, 0.521214842796, 0.470715820789, -0.220873698592, -0.294844329357, -0.205065280199, -0.196818709373, 0.795477926731, -0.384722739458, -1.06169712543, -0.167890742421, 0.49599763751, -0.0101557439193, -0.558584392071, -0.702517211437, -0.179195135832, -0.0889069065452, 1.24287176132, 0.386941492558, -0.377963095903, -0.740742206573, 0.127455756068, -0.230323076248, -0.163900479674, -0.395755589008, 0.43536195159, 0.738106548786, -0.799264013767, -0.20603042841, 0.710767865181, 0.523515284061, -0.907929956913, 0.0788556262851, 0.111601270735, -0.0745002478361, 0.346990317106, 0.32616391778, -0.23171889782, -0.473845392466, 0.664731264114, 0.0312324296683, -0.293554157019, -0.0703734606504, -0.184768974781, -0.0756682902575, 0.371084541082, 0.0839636474848, 0.0920626595616, -0.516082465649, -0.563040733337, 0.143838390708, 0.293257385492, 0.390136986971, -0.088386811316, 0.0194950494915, -0.309329986572, 0.0987806171179, 0.255824953318, -0.00011260494648, -0.172500222921, 0.112487077713, -0.129405960441, 0.31120377779, 0.322653442621, 0.397172451019, -0.347260117531, -0.409591943026, -0.172383740544, -0.172353431582, -0.104770876467, -0.502486109734, -0.258416891098, -0.239610090852, 0.695222616196, -0.130661696196, 0.806845724583, -0.206436693668, -0.744152188301, 0.464515298605, 0.818568170071, -0.438517779112, -0.752291917801, -0.104600019753, 0.0779007822275, -0.0495877750218, 0.341687679291, 0.0815256163478, -0.0824779421091, 0.3301846385, -0.296553939581, -0.439097911119, 0.135820060968, 0.797622740269, -0.219913393259, -0.220496103168, -0.109801113605, 0.599159359932, 0.102990858257, 0.608977615833, 0.0504399836063, -0.493425905704, 0.0287420041859, -0.0884666144848, 0.184674471617, -0.197273418307, 0.159897565842, -0.428218573332, -0.0391625054181, 0.115218162537, -0.210350200534, -0.492515921593, -0.2302210778, 0.249826833606, 0.0847941711545, -0.356171488762, -0.401976972818, -0.254000753164, 0.217901006341, -0.231287255883, 0.341797351837, -0.154923707247, 0.372051864862, -0.210665076971, 0.115214712918, -0.1160717085, -0.352732926607, 0.238445043564, 0.320423394442, 0.0782100856304, -0.153607711196, -0.021156443283, -0.86895608902, -0.0472883842885, 0.405406236649, -0.43440887332, -0.541340589523, -0.205899387598, 0.571096301079, 0.527008593082, 0.229026332498, -0.0568662919104, -0.521931231022, -0.756743788719, -0.060257948935, 0.454650580883, 0.392847001553, 0.514076173306, -0.195735380054, -0.149251639843, 0.0240337196738, -0.32173460722, -0.00516948476434, 0.149106442928, 0.327200859785, -0.116163298488, 0.0189112257212, -0.192452773452, 0.389188468456, -0.52135938406, -0.404690414667, 0.603824496269, 0.219251781702, 0.243884593248, -0.28382691741, -0.290861874819, -0.477886646986, 0.0771175771952, -0.310908734798, -0.325133025646, 0.464059889317, 0.328977435827, 0.129672557116, -0.553050339222, -0.507432222366, 0.115787640214, 0.405775815248, -0.0289540793747, -0.214790910482, 0.00926195364445, -0.401621758938, 0.0141945555806, 0.695082128048, -0.306836396456, -0.280148535967, 0.191328331828, -0.0740591660142, 0.237794160843, 0.521210551262, 0.507326960564, -0.326399624348, -0.282526016235, -0.453115671873, -0.323135435581, -0.684462785721, -0.659346997738, -0.275617092848, 0.151040226221, 0.233532860875, -0.0550286248326, 0.241681188345, -0.240301191807, -0.274218916893, -0.350826144218, -0.604172348976, 0.211623609066, -0.360034793615, -0.295849204063, -0.0619548112154, -0.042968723923, -0.0480784736574, 0.299180865288, -0.148937031627, -0.901235699654, -0.101308859885, -0.394019365311, -0.209546416998, 0.495187491179, -0.454454779625, -0.249904125929, -0.13583612442, -0.555691540241, 0.370698779821, -0.5843744874, 0.251811653376, 0.0172328464687, 0.457344740629, -0.283511608839, -0.0333109535277, 1.56934905052, -0.00757428444922, -0.0676159262657, 0.0920815989375, -0.482059985399, 0.372551232576, -0.43467310071, 0.742170333862, -0.0253379456699, 1.08074021339, -0.698093533516, -0.619763255119, 0.420928686857, -0.159713298082, 0.677888333797, 0.957087218761, -0.198817029595, -0.216832309961, 0.721533238888, 0.408280283213, -0.17367580533, 0.637643396854, 0.246913656592, 0.927109181881, -0.452713102102, 1.52946841717, 0.0325671993196, -0.219425037503, 1.21932530403, 0.0897421017289, -0.216693207622, 0.166701167822, -0.117182388902, -0.317105710506, -0.33998015523, 0.203328862786, -0.15231962502, -0.0514479577541, 0.385309845209, 0.737704575062, 0.353906542063, -0.291469246149, -0.510806381702, -0.0358709841967, 0.379145056009, 0.120908439159, -0.053712785244, -0.215732142329, -0.32273119688, -0.391137242317, -0.096145927906, -0.0706685110927, 0.439254701138, 0.144891545177, -1.15791594982, -0.476177155972, -0.386640161276, -0.0696934089065, 0.00766070419922, 0.0915936976671, -0.194772407413, 0.0288319084793, 0.807722628117, -0.312456071377, 0.024683419615, -0.134210512042, -0.658850967884, -0.444511920214, 1.16682302952, 0.412955373526, -0.86106300354, -0.843534350395, -0.0756809189916, 0.249314174056, -0.768764734268, -0.18570099771, -0.64992171526, -1.06809818745, -0.241304412484, -1.09838747978, -0.408396184444, -0.448166787624, 0.306912541389, -0.644407331944, 0.264287084341, 0.352415382862, -0.32395285368, -0.11422291398, 0.169177040458, -0.171893030405, -0.256403177977, -0.151077628136, 0.51526093483, -0.780808508396, -0.556867837906, 0.540703356266, -0.94756937027, 0.518447637558, -0.379737138748, 0.325614154339, -0.589700877666, -0.121931925416, -0.376903027296, 0.674576640129, 1.0511289835, -0.200893417001, 0.866664469242, -0.31772223115, 0.389687240124, 0.329201370478, 0.14609798789, -0.0465747490525, -0.130747646093, 1.11257493496, -0.171550154686, -1.10172557831, 0.783048510551, 0.654971837997, 0.430614262819, 0.0185522232205, 0.533750295639, 0.957171976566, -0.411320418119, 0.0443305559456, 0.879363298416, 0.0413797199726, 0.147208213806, -0.547723352909, -0.380599319935, 0.0328748337924, 0.162938177586, 0.309380620718, -0.144598171115, -0.365523636341, 0.12974075973, 0.051276486367, 0.318579405546, 0.301273941994, -0.0293224602938, -0.539349555969, -0.0128162642941, -0.396678030491, -0.154957085848, 0.207027703524, -0.271222531796, -0.338817328215, -0.353360652924, 0.0440826974809, 0.557435572147, -0.364424318075, -0.408349364996, -0.255339831114, 0.610197246075, 0.3794259727, -0.00990682188421, -0.0818183645606, 0.212990865111, 0.405529648066, -0.532237768173, 0.572834432125, 0.183916419744, -0.864466190338, -0.089433401823, 0.35382142663, 1.23338615894, -0.138091757894, -0.385909616947, 0.218497782946, 0.603306472301, 0.558208048344, -0.237068578601, 0.642093062401, 0.765398025513, 0.238009110093, 0.127825573087, -0.487903863192, 0.0951134040952, -0.262359976768, -0.606513559818, 0.107818476856, 0.220998287201, 0.115167543292, -0.346906840801, -0.308289676905, -0.196890816092, 1.19753932953, -0.632979691029, 0.350641727448, 0.118041612208, 0.115423731506, -0.767681896687, -1.1139986515, -0.168996348977, 0.203874543309, -0.0669453367591, -0.168869003654, -0.289925992489, -0.0373433195055, -0.785607755184, -0.120450884104, -0.991163671017, -1.64675581455, -0.0369265303016, -0.15207824111, -0.585144102573, -0.652086734772, -0.156884670258, -1.01922428608, -0.558863282204, -0.0643538758159, 0.0412841588259, 0.339178562164, 0.303961127996, -0.376203209162, 0.448310941458, -0.186875894666, 0.181232169271, -0.402108848095, 0.672553420067, -0.200394809246, 0.117735303938, 0.0478924252093, -1.02634394169, -0.210108563304, -0.256260812283, 0.0259675178677, -0.355624139309, 0.039109505713, -0.247299253941, -1.17434430122, 0.278623580933, -1.04232430458, 0.0840487703681, 0.240674853325, -0.695171833038, -0.736045897007, -1.0585244894, -0.282407969236, -0.0547968931496, -0.0205924920738, -0.454886257648, -0.783143937588, 0.0938883125782, 0.0397834517062, -0.166149884462, -0.0343275517225, 0.0802892446518, -0.211236014962, 0.200299948454, 0.164131179452, -1.26071095467, 0.380297392607, -0.10085054487, -0.0734983608127, 0.428060978651, 0.474595427513, 0.70434564352, 0.207352936268, -0.149105146527, -0.274008482695, -0.405232489109, -0.099853143096, 0.637039959431, -0.853150248528, 0.143071815372, 0.891714990139, 0.745768725872, 0.383694887161, -0.741264939308, 0.061810079962, 0.451800614595, -0.658157885075, -0.523318350315, -0.301910966635, 0.422813117504, -0.749253630638, -0.375865846872}
65};
66std::vector< std::vector<float> > b_i = {
67{-0.0646453052759, -0.833576738834, -0.0266484506428, 0.191880121827, 0.17440316081, 0.258504778147, 0.304691702127, -0.135863363743, 0.421445906162, -0.235500946641, -0.360742539167, -0.154070556164, -0.435084909201, -0.0338653549552, -0.240953952074, 0.0292105004191, -0.378072559834, -0.146301329136, -0.268983244896, -0.142332673073, 0.277948409319, -0.192356497049, -0.415451616049, -0.453225821257, -0.351215183735, -0.100444279611, -0.369603067636, 0.113715209067, 0.300046652555, -0.408928126097, 0.599760174751}
68};
69std::vector< std::vector<float> > b_f = {
70{0.700906693935, 0.563576161861, 1.37404668331, 0.664588212967, 0.655001282692, 1.50752604008, 0.901046395302, 0.908896207809, 0.531699061394, -0.175342053175, 0.622060060501, 0.766630768776, 0.517459094524, 0.655570209026, 0.967856347561, 0.531598210335, 0.995056331158, 0.863079667091, 0.805407762527, 1.07394945621, 1.33595705032, 0.889388442039, 0.709481477737, 0.916946470737, 0.724635124207, 0.30798125267, 0.742200016975, 0.249754697084, 0.470162123442, 0.843433737755, 0.515080571175}
71};
72std::vector< std::vector<float> > b_c = {
73{0.111251153052, -0.318186342716, -0.178570881486, -0.00616357009858, 0.0236715469509, -0.0270600337535, -0.447214305401, 0.350495159626, -0.597518026829, -0.242235258222, -0.0601634345949, 0.00118016276974, 0.448368012905, -0.0442144423723, -0.406291365623, 0.229264989495, -0.302716135979, 0.554887712002, 0.35920727253, 0.304902613163, -0.0852948501706, -0.0758324190974, -0.235283449292, -0.319113492966, -0.173131242394, -0.12002632767, -0.052010346204, -0.0521944798529, -0.00280740624294, 0.175736755133, -0.111001990736}
74};
75std::vector< std::vector<float> > b_o = {
76{-0.0560160428286, 0.0134335802868, -0.212648883462, 0.247949317098, 0.192606985569, -0.326273173094, -0.41331744194, -0.304275363684, 0.279461324215, -0.530653178692, -0.315937668085, -0.477790594101, -0.590563416481, 0.314803212881, -0.128587067127, -0.0400473587215, -0.361676484346, -0.267935723066, -0.186445727944, -0.36678853631, -0.281785964966, -0.122770354152, -0.110378704965, -0.724780499935, -0.183713123202, 0.106771074235, -0.233618751168, 0.157023027539, 0.219069510698, -0.396034121513, 0.614786446095}
77};
diff --git a/coeff.bin b/coeff.bin
new file mode 100644
index 0000000..eb40001
--- /dev/null
+++ b/coeff.bin
Binary files differ
diff --git a/config.cpp b/config.cpp
new file mode 100644
index 0000000..ac0f1d3
--- /dev/null
+++ b/config.cpp
@@ -0,0 +1,19 @@
1#include <vector>
2#include <string>
3
4#include "config.h"
5
6namespace config{
7 std::vector<std::string> LABELS_NAMES = {
8 "Pushup",
9 "Pushup_Incorrect",
10 "Squat",
11 "Situp",
12 "Situp_Incorrect",
13 "Jumping",
14 "Lunge"
15 };
16
17 int N_FEATURES = 2;
18 int SEGMENT_TIME_SIZE = 1;
19}
diff --git a/config.h b/config.h
new file mode 100644
index 0000000..2ab3ac1
--- /dev/null
+++ b/config.h
@@ -0,0 +1,26 @@
1/*
2Config file with global constants.
3*/
4
5#ifndef CONFIG_H_
6#define CONFIG_H_
7
8#include <vector>
9#include <string>
10
11namespace config{
12 // Labels (activities) that are considered by the model
13 extern std::vector<std::string> LABELS_NAMES;
14
15 // Number of features (3 if only acceleration is used)
16 // 9 if acceleration + gyro + magnetometer readings are used
17 extern int N_FEATURES;
18
19 // Size of a sliding window (how many samples does it contain)
20 extern int SEGMENT_TIME_SIZE;
21
22 // Sliding window shift (number of samples)
23 extern int TIME_STEP;
24}
25
26#endif
diff --git a/logs/normal45-270-v100-with-friction2-iter10-15.log b/logs/normal45-270-v100-with-friction2-iter10-15.log
new file mode 100644
index 0000000..13a886e
--- /dev/null
+++ b/logs/normal45-270-v100-with-friction2-iter10-15.log
Binary files differ
diff --git a/lstm_infer.cpp b/lstm_infer.cpp
new file mode 100644
index 0000000..2ad5d60
--- /dev/null
+++ b/lstm_infer.cpp
@@ -0,0 +1,32 @@
1/*
2Compile with:
3g++ --std=c++11 -Wall -O3 test_cpp_model.cpp onnx_model.cpp utils.cpp config.cpp -o output
4*/
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <malloc.h>
9
10#include <iostream>
11#include "onnx_model.h"
12#include "config.h"
13
14OnnxModel *model;
15void lstmSetup(void)
16{
17 model = new OnnxModel;
18 // Initialize model.
19 model->LoadModel();
20}
21
22void runLstm(double in1, double in2, double *out1, double *out2)
23{
24 Tensor in(config::SEGMENT_TIME_SIZE, config::N_FEATURES);
25 Tensor out;
26
27 in.data_.at(0) = in1;
28 in.data_.at(1) = in2;
29 model->Apply(&in, &out);
30 *out1 = out.data_.at(0);
31 *out2 = out.data_.at(1);
32}
diff --git a/mk.sh b/mk.sh
new file mode 100755
index 0000000..79d6365
--- /dev/null
+++ b/mk.sh
@@ -0,0 +1 @@
g++ --std=c++11 -Wall -O3 motor-pdm.cpp psensors.cpp lstm_infer.cpp onnx_model.cpp config.cpp -lpthread -o motpdm
diff --git a/models/configheader.py b/models/configheader.py
new file mode 100644
index 0000000..21876f4
--- /dev/null
+++ b/models/configheader.py
@@ -0,0 +1,66 @@
1"""
2Config file.
3All other scripts import variables from this config
4to their global namespace.
5"""
6##################################################
7### GLOBAL VARIABLES
8##################################################
9COLUMN_NAMES = [
10 'activity',
11 'acc-x-axis',
12 'acc-y-axis',
13 'acc-z-axis',
14 'gyro-x-axis',
15 'gyro-y-axis',
16 'gyro-z-axis',
17 'mag-x-axis',
18 'mag-y-axis',
19 'mag-z-axis'
20]
21
22LABELS_NAMES = [
23 'Pushup',
24 'Pushup_Incorrect',
25 'Squat',
26 'Situp',
27 'Situp_Incorrect',
28 'Jumping',
29 'Lunge'
30]
31
32# Data directories
33DATA_PATH = 'data/data.pckl'
34
35# Model directories
36MODEL_PATH = 'models/model.h5'
37MODEL_PATH_DIR = 'models/'
38HEADERFILE_NAME = 'LSTM_model.h'
39
40##################################################
41### MODEL
42##################################################
43# Used for shuffling data
44RANDOM_SEED = 13
45
46# Model
47N_CLASSES = len(LABELS_NAMES)
48N_FEATURES = 3
49
50# Hyperparameters
51N_EPOCHS = 30
52LEARNING_RATE = 0.0005
53N_HIDDEN_NEURONS = 30
54BATCH_SIZE = 30
55DROPOUT_RATE = 0.2
56
57##################################################
58### DATA COLLECTION/PREPROCESSING
59##################################################
60
61# Data preprocessing
62TIME_STEP = 5
63SEGMENT_TIME_SIZE = 40
64
65# Train/test proportion
66TEST_SIZE = 0.2
diff --git a/models/make_header.py b/models/make_header.py
new file mode 100644
index 0000000..36cb0ee
--- /dev/null
+++ b/models/make_header.py
@@ -0,0 +1,305 @@
1"""
2Script for making a C++ headerfile with LSTM network
3based on an ONNX model placed in MODEL_PATH
4(see config file).
5"""
6
7import sys
8_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
9import os
10import numpy as np
11import pandas as pd
12from configheader import *
13from preprocessingheader import get_convoluted_data
14import onnx
15from onnx import numpy_helper
16import argparse
17
18LAYER_DENSE = 1
19LAYER_ACTIVATION = 2
20LAYER_LSTM = 3
21
22ACTIVATION_LINEAR = 1
23ACTIVATION_RELU = 2
24ACTIVATION_SOFTPLUS = 3
25ACTIVATION_SIGMOID = 4
26ACTIVATION_TANH = 5
27ACTIVATION_HARD_SIGMOID = 6
28
29"""
30Given activation return a number assigned to that activation.
31"""
32def activation_type(activation):
33 if activation == 'linear':
34 return ACTIVATION_LINEAR
35 elif activation == 'relu':
36 return ACTIVATION_RELU
37 elif activation == 'softplus':
38 return ACTIVATION_SOFTPLUS
39 elif activation == 'tanh':
40 return ACTIVATION_TANH
41 elif activation == 'sigmoid':
42 return ACTIVATION_SIGMOID
43 elif activation == 'hard_sigmoid':
44 return ACTIVATION_HARD_SIGMOID
45
46"""
47Append functions are used to append strings to the "file",
48so that it has a C/C++ headerfile style.
49"""
50def append_vector(file, type, name, vector_object):
51 file.write("std::vector<{}> {} = {{\n".format(type, name))
52 file.write(', '.join(map(str, vector_object.tolist())))
53 file.write("};\n\n")
54
55def append_list(file, type, name, list):
56 file.write("std::vector<{}> {} = {{".format(type, name))
57 file.write(', '.join(map(str, list)))
58 file.write("};\n")
59
60def append_vector_of_vectors(file, n, type, name, array):
61 file.write("std::vector< std::vector<{}> > {} = {{\n".format(type, name))
62 for i in range(n):
63 append_numbers(file, array[i].flatten())
64 if(i != n - 1):
65 file.write(",\n")
66 file.write("\n};\n")
67
68def append_numbers(file, array):
69 file.write("{")
70 file.write(', '.join(map(str, array.tolist())))
71 file.write("}")
72
73def append_includes(file):
74 file.write("/*This is the header file generated from the ONNX model*/\n\n")
75 file.write("#include <string>\n")
76 file.write("#include <vector>\n")
77 file.write("\n")
78
79"""
80Function used for creating and saving to disk a headerfile.
81Input: ONNX model
82Output: None (saves file to disk)
83"""
84def make_header(model, lstm_layers_count):
85 # Count number of each layer type
86 dense_layers_count = 1
87 num_layers = dense_layers_count + lstm_layers_count
88 INIT_Weights = model.graph.initializer
89
90 if os.path.exists(HEADERFILE_NAME):
91 os.remove(HEADERFILE_NAME)
92
93 with open(HEADERFILE_NAME, "a") as file:
94 append_includes(file)
95 file.write("unsigned int NUM_LAYERS = " + str(num_layers) + ";\n")
96 # LSTM layer
97 recurrent_activation_list = []
98 activation_list = []
99 return_sequences_list = []
100
101 num_units_list = []
102
103 W_i_list = []
104 W_f_list = []
105 W_c_list = []
106 W_o_list = []
107
108 U_i_list = []
109 U_f_list = []
110 U_c_list = []
111 U_o_list = []
112
113 b_i_list = []
114 b_f_list = []
115 b_c_list = []
116 b_o_list = []
117
118 W_i_ROWS_list = []
119 W_i_COLS_list = []
120 W_f_ROWS_list = []
121 W_f_COLS_list = []
122 W_c_ROWS_list = []
123 W_c_COLS_list = []
124 W_o_ROWS_list = []
125 W_o_COLS_list = []
126 U_i_ROWS_list = []
127 U_i_COLS_list = []
128 U_f_ROWS_list = []
129 U_f_COLS_list = []
130 U_c_ROWS_list = []
131 U_c_COLS_list = []
132 U_o_ROWS_list = []
133 U_o_COLS_list = []
134 b_i_SHAPE_list = []
135 b_f_SHAPE_list = []
136 b_c_SHAPE_list = []
137 b_o_SHAPE_list = []
138
139 # Fully connected
140 weights = np.transpose(numpy_helper.to_array(INIT_Weights[5]))
141 biases = np.transpose(numpy_helper.to_array(INIT_Weights[6]))
142 activation = 'linear'
143 print "FC weights:" + str(weights.shape)
144 print "FC biases :" + str(biases.shape)
145
146 file.write("unsigned int DENSE_WEIGHTS_ROWS = " + str(weights.shape[0]) + ";\n")
147 file.write("unsigned int DENSE_WEIGHTS_COLS = " + str(weights.shape[1]) + ";\n")
148 file.write("unsigned int DENSE_BIASES_SHAPE = " + str(biases.shape[0]) + ";\n")
149 file.write("unsigned int DENSE_ACTIVATION = " + str(activation_type(activation)) + ";\n\n")
150
151 append_vector(file, "float", "DENSE_WEIGHTS", weights.flatten())
152 append_vector(file, "float", "DENSE_BIASES", biases)
153
154 # LSTM layer attriutes
155 recurrent_activation = 'sigmoid'
156 activation = 'sigmoid'
157 return_sequences = 0
158
159 recurrent_activation_list += str(activation_type(recurrent_activation))
160 activation_list += str(activation_type(activation))
161 return_sequences_list += str(return_sequences)
162
163 Wonnx = numpy_helper.to_array(INIT_Weights[0])
164 Uonnx = numpy_helper.to_array(INIT_Weights[1])
165 bonnx = numpy_helper.to_array(INIT_Weights[2])
166 W = np.transpose(Wonnx[0,:])
167 U = np.transpose(Uonnx[0,:])
168 bIN = np.transpose(bonnx[0,:])
169
170 b = bIN[:len(bIN)/2]
171 print "bIN shape " + str(bIN.shape)
172
173 num_units = int(int(W.shape[1])/4)
174 num_units_list.append(num_units)
175 # file.write("unsigned int N_HIDDEN_NEURONS = " + str(num_units) + ";\n\n")
176 print "W shape:" + str(W.shape)
177 print "U shape:" + str(U.shape)
178 print "b shape:" + str(b.shape)
179 W_i = W[:, :num_units]
180 W_o = W[:, num_units: num_units * 2]
181 W_f = W[:, num_units * 2: num_units * 3]
182 W_c = W[:, num_units * 3:]
183 print "Wi shape:" + str(W_i.shape)
184 print "Wf shape:" + str(W_f.shape)
185 print "Wc shape:" + str(W_c.shape)
186 print "Wo shape:" + str(W_o.shape)
187
188 U_i = U[:, :num_units]
189 U_o = U[:, num_units: num_units * 2]
190 U_f = U[:, num_units * 2: num_units * 3]
191 U_c = U[:, num_units * 3:]
192 print "Ui shape:" + str(U_i.shape)
193 print "Uf shape:" + str(U_f.shape)
194 print "Uc shape:" + str(U_c.shape)
195 print "Uo shape:" + str(U_o.shape)
196
197 b_i = b[:num_units]
198 b_o = b[num_units: num_units * 2]
199 b_f = b[num_units * 2: num_units * 3]
200 b_c = b[num_units * 3:]
201 print "Bi shape:" + str(b_i.shape)
202 print "Bf shape:" + str(b_f.shape)
203 print "Bc shape:" + str(b_c.shape)
204 print "Bo shape:" + str(b_o.shape)
205
206 W_i_list.append(W_i)
207 W_f_list.append(W_f)
208 W_c_list.append(W_c)
209 W_o_list.append(W_o)
210
211 U_i_list.append(U_i)
212 U_f_list.append(U_f)
213 U_c_list.append(U_c)
214 U_o_list.append(U_o)
215
216 b_i_list.append(b_i)
217 b_f_list.append(b_f)
218 b_c_list.append(b_c)
219 b_o_list.append(b_o)
220
221 print "W_i_ROWS:" + str(W_i.shape[0])
222 print "W_i_COLS:" + str(W_i.shape[1])
223 W_i_ROWS_list.append(str(W_i.shape[0]))
224 W_i_COLS_list.append(str(W_i.shape[1]))
225 print "W_i_ROWS list:" + str(W_i_ROWS_list)
226 print "W_i_COLS list:" + str(W_i_COLS_list)
227
228 W_f_ROWS_list.append(str(W_f.shape[0]))
229 W_f_COLS_list.append(str(W_f.shape[1]))
230 W_c_ROWS_list.append(str(W_c.shape[0]))
231 W_c_COLS_list.append(str(W_c.shape[1]))
232 W_o_ROWS_list.append(str(W_o.shape[0]))
233 W_o_COLS_list.append(str(W_o.shape[1]))
234 U_i_ROWS_list.append(str(U_i.shape[0]))
235 U_i_COLS_list.append(str(U_i.shape[1]))
236 U_f_ROWS_list.append(str(U_f.shape[0]))
237 U_f_COLS_list.append(str(U_f.shape[1]))
238 U_c_ROWS_list.append(str(U_c.shape[0]))
239 U_c_COLS_list.append(str(U_c.shape[1]))
240 U_o_ROWS_list.append(str(U_o.shape[0]))
241 U_o_COLS_list.append(str(U_o.shape[1]))
242 b_i_SHAPE_list.append(str(b_i.shape[0]))
243 b_f_SHAPE_list.append(str(b_f.shape[0]))
244 b_c_SHAPE_list.append(str(b_c.shape[0]))
245 b_o_SHAPE_list.append(str(b_o.shape[0]))
246
247 append_list(file, "unsigned int", 'LSTM_RECURRENT_ACTIVATION', recurrent_activation_list)
248 append_list(file, "unsigned int", 'LSTM_ACTIVATION', activation_list)
249 append_list(file, "unsigned int", 'RETURN_SEQUENCES', return_sequences_list)
250 append_list(file, "unsigned int", 'N_HIDDEN_NEURONS', num_units_list)
251 append_list(file, "unsigned int", 'W_i_ROWS', W_i_ROWS_list)
252 append_list(file, "unsigned int", 'W_i_COLS', W_i_COLS_list)
253 append_list(file, "unsigned int", 'W_f_ROWS', W_f_ROWS_list)
254 append_list(file, "unsigned int", 'W_f_COLS', W_f_COLS_list)
255 append_list(file, "unsigned int", 'W_c_ROWS', W_c_ROWS_list)
256 append_list(file, "unsigned int", 'W_c_COLS', W_c_COLS_list)
257 append_list(file, "unsigned int", 'W_o_ROWS', W_o_ROWS_list)
258 append_list(file, "unsigned int", 'W_o_COLS', W_o_COLS_list)
259 append_list(file, "unsigned int", 'U_i_ROWS', U_i_ROWS_list)
260 append_list(file, "unsigned int", 'U_i_COLS', U_i_COLS_list)
261 append_list(file, "unsigned int", 'U_f_ROWS', U_f_ROWS_list)
262 append_list(file, "unsigned int", 'U_f_COLS', U_f_COLS_list)
263 append_list(file, "unsigned int", 'U_c_ROWS', U_c_ROWS_list)
264 append_list(file, "unsigned int", 'U_c_COLS', U_c_COLS_list)
265 append_list(file, "unsigned int", 'U_o_ROWS', U_o_ROWS_list)
266 append_list(file, "unsigned int", 'U_o_COLS', U_o_COLS_list)
267 append_list(file, "unsigned int", 'b_i_SHAPE', b_i_SHAPE_list)
268 append_list(file, "unsigned int", 'b_f_SHAPE', b_f_SHAPE_list)
269 append_list(file, "unsigned int", 'b_c_SHAPE', b_c_SHAPE_list)
270 append_list(file, "unsigned int", 'b_o_SHAPE', b_o_SHAPE_list)
271
272 append_vector_of_vectors(file, lstm_layers_count ,'float', 'W_i', W_i_list)
273 append_vector_of_vectors(file, lstm_layers_count ,'float', 'W_f', W_f_list)
274 append_vector_of_vectors(file, lstm_layers_count ,'float', 'W_c', W_c_list)
275 append_vector_of_vectors(file, lstm_layers_count ,'float', 'W_o', W_o_list)
276 append_vector_of_vectors(file, lstm_layers_count ,'float', 'U_i', U_i_list)
277 append_vector_of_vectors(file, lstm_layers_count ,'float', 'U_f', U_f_list)
278 append_vector_of_vectors(file, lstm_layers_count ,'float', 'U_c', U_c_list)
279 append_vector_of_vectors(file, lstm_layers_count ,'float', 'U_o', U_o_list)
280 append_vector_of_vectors(file, lstm_layers_count ,'float', 'b_i', b_i_list)
281 append_vector_of_vectors(file, lstm_layers_count ,'float', 'b_f', b_f_list)
282 append_vector_of_vectors(file, lstm_layers_count ,'float', 'b_c', b_c_list)
283 append_vector_of_vectors(file, lstm_layers_count ,'float', 'b_o', b_o_list)
284
285 file.close()
286
287if __name__ == '__main__':
288 parser = argparse.ArgumentParser()
289 parser.add_argument('-m', '--model', help="onnx model filename")
290 parser.add_argument("-w", "--weights", help="show initializer weights", action="store_true")
291 parser.add_argument('-l', '--lstmlayer', help="the number of LSTM layers, 1-3 layers are supported")
292 args = parser.parse_args()
293 # check if the number of LSTM layers is set and in the right range
294 lstm_layers_count = 0
295 if args.lstmlayer is not None:
296 lstm_layers_count = int(args.lstmlayer)
297 if lstm_layers_count > 3 or lstm_layers_count < 1:
298 print("1-3 LSTM layers are supported. Please set -l or --lstmlayer accordingly.")
299 sys.exit()
300 # Load the ONNX model
301 model = onnx.load(args.model)
302 # Check that the IR is well formed
303 onnx.checker.check_model(model)
304 print("================")
305 make_header(model, lstm_layers_count)
diff --git a/models/preprocessingheader.py b/models/preprocessingheader.py
new file mode 100644
index 0000000..c23c732
--- /dev/null
+++ b/models/preprocessingheader.py
@@ -0,0 +1,108 @@
1"""
2Library for data preprocessing.
3"""
4
5import numpy as np
6import pandas as pd
7
8from scipy import stats
9
10from configheader import * # Global variables
11
12def get_convoluted_data(data,
13 segment_time_size=SEGMENT_TIME_SIZE,
14 time_step=TIME_STEP):
15 """
16 Take as input pandas dataframe and return a tuple: convoluted data and labels.
17 Convoluted data is basically the data extracted by sliding a window of size
18 SEGMENT_TIME_SIZE with stepsize TIME_STEP. Each window is assigned with one
19 label, which is the most frequently occuring label for in given window.
20 """
21 data_convoluted = []
22 labels = []
23
24 increment = 0
25 if(len(data) == segment_time_size):
26 increment = 1
27 elif(len(data) < segment_time_size):
28 raise ValueError
29
30 # Slide a "segment_time_size" wide window with a step size of "TIME_STEP"
31 # Increment allows the loop to run if there is just one sample, for which
32 # len(data) == segment_time_size
33 for i in range(0, len(data) - segment_time_size + increment, time_step):
34 ax = data[COLUMN_NAMES[1]].values[i: i + segment_time_size]
35 ay = data[COLUMN_NAMES[2]].values[i: i + segment_time_size]
36 az = data[COLUMN_NAMES[3]].values[i: i + segment_time_size]
37
38 gx = data[COLUMN_NAMES[4]].values[i: i + segment_time_size]
39 gy = data[COLUMN_NAMES[5]].values[i: i + segment_time_size]
40 gz = data[COLUMN_NAMES[6]].values[i: i + segment_time_size]
41
42 mx = data[COLUMN_NAMES[7]].values[i: i + segment_time_size]
43 my = data[COLUMN_NAMES[8]].values[i: i + segment_time_size]
44 mz = data[COLUMN_NAMES[9]].values[i: i + segment_time_size]
45
46 # When 9 features used
47 # data_convoluted.append([ax, ay, az, gx, gy, gz, mx, my, mz])
48
49 # When 3 features used
50 data_convoluted.append([ax, ay, az])
51
52 # Label for a data window is the label that appears most commonly
53 label = stats.mode(data[COLUMN_NAMES[0]][i: i + segment_time_size])[0][0]
54 labels.append(label)
55
56 data_convoluted = np.asarray(data_convoluted, dtype=np.float32)
57 data_convoluted = data_convoluted.transpose(0, 2, 1)
58
59 labels = one_hot_encode(labels)
60 return data_convoluted, labels
61
62def one_hot_encode(labels):
63 """
64 Take as input a list of labels (strings) and encode them using one_hot_encoding scheme,
65 i.e. assuming there are 3 activities: A, B in the LABELS_NAMES array,
66 if the input is [A, B] then return [[1, 0, 0].[0, 1, 0]].
67 If the label does not exist then raise a NameError.
68 """
69 encoded = []
70 for i, label in enumerate(labels):
71 array = np.zeros(N_CLASSES)
72 try:
73 label_position(label)
74 except NameError:
75 raise NameError
76 array[label_position(label)] = 1
77 encoded.append(array)
78
79 return np.asarray(encoded, dtype=np.float32)
80
81def label_position(label):
82 """
83 Take as input a label (string) and if it's in LABELS_NAMES array,
84 return its position, otherwise raise NameError.
85 """
86 for i in range(N_CLASSES):
87 if(LABELS_NAMES[i] == label):
88 return i
89 raise NameError('Label does not exist')
90
91def one_hot_to_label(array):
92 """
93 Take as input a one-hot encoded array and return a label corresponding to "one".
94 i.e for [0, 0, 1] return label at index 2.
95 """
96 i = np.argmax(array)
97 return LABELS_NAMES[i]
98
99def softmax_to_one_hot(array):
100 """
101 Take an array of numbers as input and return one-hot encoded version of the
102 array, where "one" corresponds to the highest number in the array.
103 i.e. for [0, 2, 5, 3] return [0, 0, 1, 0]
104 """
105 i = np.argmax(array)
106 one_hot = np.zeros(len(array))
107 one_hot[i] = 1
108 return one_hot
diff --git a/models/readme.txt b/models/readme.txt
new file mode 100644
index 0000000..64add3a
--- /dev/null
+++ b/models/readme.txt
@@ -0,0 +1,14 @@
1===================================================
2LSTM_model.h created from the ONNX model
3===================================================
4
5The ONNX model is converted with python scripts in this folder
6to create the CPP header file (LSTM_model.h) with initialized
7data structures, which is then compiled to establish the
8Recurrent Neural Network (RNN) inference libarary for anomaly
9detection.
10
11 python ./make_header.py -m ./timotor20190227raw.onnx -l 1
12
13 -m/--model: onnx model filename
14 -l/--lstmlayer: the number of LSTM layers, 1-3 layers are supported
diff --git a/models/timotor20190227raw.onnx b/models/timotor20190227raw.onnx
new file mode 100644
index 0000000..266a23d
--- /dev/null
+++ b/models/timotor20190227raw.onnx
Binary files differ
diff --git a/motor-pdm.cpp b/motor-pdm.cpp
new file mode 100644
index 0000000..5b30193
--- /dev/null
+++ b/motor-pdm.cpp
@@ -0,0 +1,187 @@
1#include <stdio.h>
2#include <stdint.h>
3#include <stdlib.h>
4#include <string.h>
5#include <memory.h>
6#include <fcntl.h>
7#include <sys/stat.h>
8#include <sys/types.h>
9#include <unistd.h>
10
11#include <iostream>
12#include <thread>
13#include <chrono>
14
15//////////////////////////////////////////////////////////////
16// Filter Code Definitions
17//////////////////////////////////////////////////////////////
18// maximum number of inputs that can be handled
19// in one function call
20#define SAMPLES 200
21// maximum length of filter than can be handled
22#define FIRFLT_LEN 1001
23// buffer to hold all of the input samples
24#define BUFFER_LEN (FIRFLT_LEN - 1 + SAMPLES)
25
26// delay line to hold input samples
27double insampPH1[ BUFFER_LEN ];
28double insampPH2[ BUFFER_LEN ];
29// LP filter coefficients
30#define DRATIO 200
31double coeffs[ FIRFLT_LEN ];
32
33// Relative prediction error threshold
34#define RELATIVE_THRESHOLD 2.0
35
36// Each sample tick is 20ms long
37#define START_FIND_THRESHOLD 150
38#define STOP_FIND_THRESHOLD 1500
39#define HANGOVER_TIME 100
40
41#define max(a,b) (a > b ? a : b)
42
43int uart_get_data(int sample_count, int op_mode, float *samples_array);
44void uart_stream_parser(int mode_op);
45void lstmSetup(void);
46void runLstm(double lstm_in1, double lstm_in2, double *lstm_out1, double *lstm_out2);
47
48// FIR init
49void firFloatInit( double *insamp, int len_delay_line)
50{
51 int i;
52 for (i = 0; i < len_delay_line; i ++) insamp[i] = 0.0;
53}
54
55// the FIR filter function
56void firFloat( double *coeffs, double *insamp, int filterLength,
57 double *input, int length,
58 double *output)
59{
60 double acc; // accumulator for MACs
61 double *coeffp; // pointer to coefficients
62 double *inputp; // pointer to input samples
63 int n,j=0;
64 int k;
65 // put the new samples at the high end of the buffer
66 memcpy( &insamp[filterLength - 1], input,
67 length * sizeof(double) );
68 // apply the filter to each input sample
69 for ( n = 0; n < length; n=n+DRATIO ) {
70 // calculate output n
71 coeffp = coeffs;
72 inputp = &insamp[filterLength - 1 + n];
73 acc = 0;
74 for ( k = 0; k < filterLength; k++ )
75 {
76 acc += (*coeffp++) * (*inputp--);
77 }
78 output[j++] = acc;
79 }
80 // shift input samples back in time for next time
81 memmove( &insamp[0], &insamp[length], (filterLength - 1) * sizeof(double) );
82}
83//////////////////////////////////////////////////////////////
84// Test program
85//////////////////////////////////////////////////////////////
86// number of samples to read per loop
87int main( int argc, char *argv[])
88{
89 int size;
90 double floatInput1[SAMPLES];
91 double floatInput2[SAMPLES];
92 double floatOutput1[SAMPLES/DRATIO];
93 double floatOutput2[SAMPLES/DRATIO];
94 double predict_threshold_err = 0.0;
95 double predict1 = 0.0;
96 double predict2 = 0.0;
97 double mu1, mu2, sig1, sig2;
98 FILE *coeff_fid;
99 int samples_counter = 0;
100 int restart_hangover_timer = 0;
101 float samples_array[3*SAMPLES];
102
103 //Pick up decimating FIR filter coefficients
104 coeff_fid = fopen( "coeff.bin", "rb" );
105 if ( coeff_fid == 0 ) {
106 printf("\n...couldn't open coeff.bin file (Decimating FIR filter coefficients)\n");
107 return -1;
108 }
109 fread(coeffs, sizeof(double), FIRFLT_LEN, coeff_fid );
110 fclose( coeff_fid );
111
112 //Get normalization parameters
113 if(argc < 5) {
114 printf("\nUsage:%s mu1 mu2 sig1 sig2\n", argv[0]);
115 return -1;
116 }
117 mu1 = atof(argv[1]);
118 mu2 = atof(argv[2]);
119 sig1 = atof(argv[3]);
120 sig2 = atof(argv[4]);
121
122 lstmSetup();
123 // initialize the filter delay lines
124 firFloatInit(insampPH1, BUFFER_LEN);
125 firFloatInit(insampPH2, BUFFER_LEN);
126
127 //Start UART reading, and parsing thread. Parsed data are logged into a buffer
128 std::thread sensorStream (uart_stream_parser, 0);
129 // Processs loop
130 while(1)
131 {
132 // read samples from UART pipe
133 size = uart_get_data(SAMPLES, 0, samples_array);
134 if(size == SAMPLES)
135 { // It means, at least this many SAMPLES are available
136 for(int i = 0; i < SAMPLES; i ++) {
137 floatInput1[i] = (double)samples_array[3*i + 0];
138 floatInput2[i] = (double)samples_array[3*i + 1];
139 // position is samples_array[3*i + 2]
140 }
141 // perform the filtering
142 // One decimated sample generated
143 firFloat( coeffs, insampPH1, FIRFLT_LEN, floatInput1, SAMPLES, floatOutput1 );
144 firFloat( coeffs, insampPH2, FIRFLT_LEN, floatInput2, SAMPLES, floatOutput2 );
145 //Calculate prediction error (using previously predicted samples)
146 double predict_error = (floatOutput1[0] - predict1) * (floatOutput1[0] - predict1);
147 predict_error += (floatOutput2[0] - predict2) * (floatOutput2[0] - predict2);
148 if(samples_counter > START_FIND_THRESHOLD)
149 {
150 if(samples_counter < STOP_FIND_THRESHOLD)
151 { // Find error threshold
152 predict_threshold_err = max(predict_error, predict_threshold_err);
153 std::cout << "Find error threshold:" << samples_counter << std::endl;
154 restart_hangover_timer = 0;
155 } else {
156 std::cout << "Look for anomaly:" << samples_counter << " err:" << predict_error << " thre:" << (RELATIVE_THRESHOLD * predict_threshold_err);
157 // Anomaly detection mode!
158 // ...implemented hangover
159 if(predict_error > RELATIVE_THRESHOLD * predict_threshold_err)
160 {
161 restart_hangover_timer = samples_counter;
162 }
163 std::cout << std::endl;
164 if((samples_counter - restart_hangover_timer) < HANGOVER_TIME)
165 {
166 std::cout << " [AD] ";
167 }
168 }
169 }
170 //Normalize
171 double lstm_out1, lstm_in1 = (floatOutput1[0] - mu1) / sig1;
172 double lstm_out2, lstm_in2 = (floatOutput2[0] - mu2) / sig2;
173 //Run LSTM network
174 runLstm(lstm_in1, lstm_in2, &lstm_out1, &lstm_out2);
175 //Denormalize
176 predict1 = lstm_out1 * sig1 + mu1;
177 predict2 = lstm_out2 * sig2 + mu2;
178
179 samples_counter ++; // Increamented at decimated rate!!!
180 } else {
181 std::this_thread::sleep_for(std::chrono::milliseconds(100));
182 std::cout << "sleep" << std::endl;
183 }
184 }
185
186 return 0;
187}
diff --git a/onnx_model.cpp b/onnx_model.cpp
new file mode 100644
index 0000000..5713708
--- /dev/null
+++ b/onnx_model.cpp
@@ -0,0 +1,389 @@
1/*
2 * Copyright (c) 2016 Robert W. Rose
3 *
4 * Edited by Tomasz Bartkowiak (July 2018)
5 * MIT License, see LICENSE file.
6 *
7 * Modified by Texas Instruments (2019) for ONNX import support
8 * MIT License.
9 */
10
11#include "onnx_model.h"
12
13#ifdef DBG_START
14#include "start_LSTM_model.h"
15#else
16#include "LSTM_model.h"
17#endif
18
19#include <iostream>
20#include <cmath>
21#include <limits>
22#include <stdio.h>
23
24bool OnnxLayerActivation::LoadActivation(ActivationType activation_) {
25 switch (activation_) {
26 case kLinear:
27 activation_type_ = kLinear;
28 break;
29 case kRelu:
30 activation_type_ = kRelu;
31 break;
32 case kSoftPlus:
33 activation_type_ = kSoftPlus;
34 break;
35 case kHardSigmoid:
36 activation_type_ = kHardSigmoid;
37 break;
38 case kSigmoid:
39 activation_type_ = kSigmoid;
40 break;
41 case kTanh:
42 activation_type_ = kTanh;
43 break;
44 default:
45 KASSERT(false, "Unsupported activation type %d", activation_);
46 }
47
48 return true;
49}
50
51bool OnnxLayerActivation::Apply(Tensor* in, Tensor* out) {
52 KASSERT(in, "Invalid input");
53 KASSERT(out, "Invalid output");
54
55 *out = *in;
56#ifdef DBG_VERBOSE
57 std::cout << "Activation Apply(" << activation_type_ << ")" << std::endl;
58#endif
59 switch (activation_type_) {
60 case kLinear:
61 break;
62 case kRelu:
63 for (size_t i = 0; i < out->data_.size(); i++) {
64 if (out->data_[i] < 0.0) {
65 out->data_[i] = 0.0;
66 }
67 }
68 break;
69 case kSoftPlus:
70 for (size_t i = 0; i < out->data_.size(); i++) {
71 out->data_[i] = std::log(1.0 + std::exp(out->data_[i]));
72 }
73 break;
74 case kHardSigmoid:
75 for (size_t i = 0; i < out->data_.size(); i++) {
76 float x = (out->data_[i] * 0.2) + 0.5;
77
78 if (x <= 0) {
79 out->data_[i] = 0.0;
80 } else if (x >= 1) {
81 out->data_[i] = 1.0;
82 } else {
83 out->data_[i] = x;
84 }
85 }
86 break;
87 case kSigmoid:
88 for (size_t i = 0; i < out->data_.size(); i++) {
89 float& x = out->data_[i];
90
91 if (x >= 0) {
92 out->data_[i] = 1.0 / (1.0 + std::exp(-x));
93 } else {
94 float z = std::exp(x);
95 out->data_[i] = z / (1.0 + z);
96 }
97 }
98 break;
99 case kTanh:
100 for (size_t i = 0; i < out->data_.size(); i++) {
101 out->data_[i] = std::tanh(out->data_[i]);
102 }
103 break;
104 default:
105 break;
106 }
107
108 return true;
109}
110
111bool OnnxLayerDense::LoadLayer(unsigned int dummy_int) {
112 unsigned int weights_rows = DENSE_WEIGHTS_ROWS;
113 unsigned int weights_cols = DENSE_WEIGHTS_COLS;
114 unsigned int biases_shape = DENSE_BIASES_SHAPE;
115
116 weights_.Resize(weights_rows, weights_cols);
117 weights_.data_ = DENSE_WEIGHTS;
118 biases_.Resize(biases_shape);
119 biases_.data_ = DENSE_BIASES;
120 activation_.LoadActivation(ActivationType::kLinear);
121 return true;
122}
123
124bool OnnxLayerDense::Apply(Tensor* in, Tensor* out) {
125 KASSERT(in, "Invalid input");
126 KASSERT(out, "Invalid output");
127 KASSERT(in->dims_.size() <= 2, "Invalid input dimensions");
128
129 if (in->dims_.size() == 2) {
130 KASSERT(in->dims_[1] == weights_.dims_[0], "Dimension mismatch %d %d",
131 in->dims_[1], weights_.dims_[0]);
132 }
133
134#ifdef DBG_VERBOSE
135 std::cout << "DenseLayer Weights Size:" << weights_.dims_[0] << " " << weights_.dims_[1] << std::endl;
136 std::cout << "DenseLayer Input Size :" << in->dims_[0] << " " << in->dims_[1] << std::endl << std::flush;
137#endif
138
139#if 1
140 Tensor tmp(weights_.dims_[1]);
141
142 for (int i = 0; i < weights_.dims_[0]; i++) {
143 for (int j = 0; j < weights_.dims_[1]; j++) {
144 tmp(j) += (*in)(i)*weights_(i, j);
145 }
146 }
147
148 for (int i = 0; i < biases_.dims_[0]; i++) {
149 tmp(i) += biases_(i);
150 }
151#else
152 std::cout << "BiasesDIM DenseLayer:" << biases_.dims_[0] << std::endl;
153 Tensor tmp = in->Dot(weights_) + biases_;
154 tmp.Print();
155#endif
156 KASSERT(activation_.Apply(&tmp, out), "Failed to apply activation");
157
158#ifdef DBG_VERBOSE
159 std::cout << "DenseLayer TMP Size:" << tmp.dims_[0] << std::endl;
160 std::cout << "DenseLayer Output Dimensions:" << out->dims_.size() << std::endl;
161 if(out->dims_.size() == 1)
162 std::cout << "DenseLayer Output Size :" << out->dims_[0] << std::endl;
163 if(out->dims_.size() == 2)
164 std::cout << "DenseLayer Output Size :" << out->dims_[0] << " " << out->dims_[1] << std::endl;
165#endif
166 return true;
167}
168
169bool OnnxLayerLSTM::LoadLayer(unsigned int i) {
170 unsigned int wi_rows = W_i_ROWS[i];
171 unsigned int wi_cols = W_i_COLS[i];
172 unsigned int ui_rows = U_i_ROWS[i];
173 unsigned int ui_cols = U_i_COLS[i];
174 unsigned int bi_shape = b_i_SHAPE[i];
175 unsigned int wf_rows = W_f_ROWS[i];
176 unsigned int wf_cols = W_f_COLS[i];
177 unsigned int uf_rows = U_f_ROWS[i];
178 unsigned int uf_cols = U_f_COLS[i];
179 unsigned int bf_shape = b_f_SHAPE[i];
180 unsigned int wc_rows = W_c_ROWS[i];
181 unsigned int wc_cols = W_c_COLS[i];
182 unsigned int uc_rows = U_c_ROWS[i];
183 unsigned int uc_cols = U_c_COLS[i];
184 unsigned int bc_shape = b_c_SHAPE[i];
185 unsigned int wo_rows = W_o_ROWS[i];
186 unsigned int wo_cols = W_o_COLS[i];
187 unsigned int uo_rows = U_o_ROWS[i];
188 unsigned int uo_cols = U_o_COLS[i];
189 unsigned int bo_shape = b_o_SHAPE[i];
190
191 // Load Input Weights and Biases
192 Wi_.Resize(wi_rows, wi_cols);
193 Ui_.Resize(ui_rows, ui_cols);
194 bi_.Resize(1, bi_shape);
195 Wi_.data_ = W_i[i];
196 Ui_.data_ = U_i[i];
197 bi_.data_ = b_i[i];
198
199 // Load Forget Weights and Biases
200 Wf_.Resize(wf_rows, wf_cols);
201 Uf_.Resize(uf_rows, uf_cols);
202 bf_.Resize(1, bf_shape);
203 Wf_.data_ = W_f[i];
204 Uf_.data_ = U_f[i];
205 bf_.data_ = b_f[i];
206
207 // Load State Weights and Biases
208 Wc_.Resize(wc_rows, wc_cols);
209 Uc_.Resize(uc_rows, uc_cols);
210 bc_.Resize(1, bc_shape);
211 Wc_.data_ = W_c[i];
212 Uc_.data_ = U_c[i];
213 bc_.data_ = b_c[i];
214
215 // Load Output Weights and Biases
216 Wo_.Resize(wo_rows, wo_cols);
217 Uo_.Resize(uo_rows, uo_cols);
218 bo_.Resize(1, bo_shape);
219 Wo_.data_ = W_o[i];
220 Uo_.data_ = U_o[i];
221 bo_.data_ = b_o[i];
222
223 innerActivation_.LoadActivation(ActivationType::kSigmoid);
224 activation_.LoadActivation(ActivationType::kTanh);
225 return_sequences_ = (bool)RETURN_SEQUENCES[i];
226
227#ifdef DO_LSTM_RESET
228 Reset();
229#endif
230
231 return true;
232}
233
234#ifdef DO_LSTM_RESET
235bool OnnxLayerLSTM::Reset() {
236 // Assume bo always keeps the output shape and we will always receive one
237 // single sample.
238#ifdef DBG_VERBOSE
239 std::cout << "Reset LSTM enter" << std::endl;
240#endif
241 int outputDim = bo_.dims_[1];
242 ht_1.Resize(1, outputDim);
243 ct_1.Resize(1, outputDim);
244
245 ht_1.Fill(0.0f);
246 ct_1.Fill(0.0f);
247#ifdef DBG_VERBOSE
248 std::cout << "Reset LSTM end:" << outputDim << std::endl;
249#endif
250 return true;
251}
252#endif
253
254bool OnnxLayerLSTM::Apply(Tensor* in, Tensor* out) {
255 // Assume bo always keeps the output shape and we will always receive one
256 // single sample.
257 int outputDim = bo_.dims_[1];
258#ifndef DO_LSTM_RESET
259 Tensor ht_1 = Tensor(1, outputDim);
260 Tensor ct_1 = Tensor(1, outputDim);
261
262 ht_1.Fill(0.0f);
263 ct_1.Fill(0.0f);
264#endif
265
266 int steps = in->dims_[0];
267
268 Tensor outputs, lastOutput;
269
270 if (return_sequences_) {
271 outputs.dims_ = {steps, outputDim};
272 outputs.data_.reserve(steps * outputDim);
273 }
274
275 for (int s = 0; s < steps; s++) {
276 Tensor x = in->Select(s);
277
278 KASSERT(Step(&x, &lastOutput, &ht_1, &ct_1), "Failed to execute step");
279
280 if (return_sequences_) {
281 outputs.data_.insert(outputs.data_.end(), lastOutput.data_.begin(),
282 lastOutput.data_.end());
283 }
284 }
285
286 if (return_sequences_) {
287 *out = outputs;
288 std::cout << "Return dim(" << outputs.dims_[0] << "," << outputs.dims_[1] << ")" << std::endl;
289 } else {
290 *out = lastOutput;
291 }
292
293 return true;
294}
295
296bool OnnxLayerLSTM::Step(Tensor* x, Tensor* out, Tensor* ht_1, Tensor* ct_1) {
297 Tensor xi = x->Dot(Wi_) + bi_;
298 Tensor xf = x->Dot(Wf_) + bf_;
299 Tensor xc = x->Dot(Wc_) + bc_;
300 Tensor xo = x->Dot(Wo_) + bo_;
301#ifdef DBG_VERBOSE
302 printf("\nXi[%d,%d]", xi.dims_[0], xi.dims_[1]);
303 printf("\nXf[%d,%d]", xf.dims_[0], xf.dims_[1]);
304 printf("\nXc[%d,%d]", xc.dims_[0], xc.dims_[1]);
305 printf("\nXo[%d,%d]", xo.dims_[0], xo.dims_[1]);
306#endif
307
308 Tensor i_ = xi + ht_1->Dot(Ui_);
309 Tensor f_ = xf + ht_1->Dot(Uf_);
310 Tensor c_ = xc + ht_1->Dot(Uc_);
311 Tensor o_ = xo + ht_1->Dot(Uo_);
312
313#ifdef DBG_VERBOSE
314 printf("\ni[%d,%d]", i_.dims_[0], i_.dims_[1]);
315 printf("\nf[%d,%d]", f_.dims_[0], f_.dims_[1]);
316 printf("\nc[%d,%d]", c_.dims_[0], c_.dims_[1]);
317 printf("\no[%d,%d]", o_.dims_[0], o_.dims_[1]);
318 std::cout << "\nLSTM step" << std::endl;
319#endif
320
321 Tensor i, f, cc, o;
322
323 KASSERT(innerActivation_.Apply(&i_, &i),
324 "Failed to apply inner activation on i");
325 KASSERT(innerActivation_.Apply(&f_, &f),
326 "Failed to apply inner activation on f");
327 KASSERT(activation_.Apply(&c_, &cc), "Failed to apply activation on c_");
328 KASSERT(innerActivation_.Apply(&o_, &o),
329 "Failed to apply inner activation on o");
330
331 *ct_1 = f.Multiply(*ct_1) + i.Multiply(cc);
332
333 KASSERT(activation_.Apply(ct_1, &cc), "Failed to apply activation on c");
334 *out = *ht_1 = o.Multiply(cc);
335
336 return true;
337}
338
339bool OnnxModel::LoadModel() {
340 unsigned int num_layers = NUM_LAYERS;
341
342 LayerType layer_types[num_layers] = {LayerType::kLSTM, LayerType::kDense};
343 for (unsigned int i = 0; i < num_layers; i++) {
344 LayerType layer_type = layer_types[i];
345 OnnxLayer* layer = NULL;
346
347 switch (layer_type) {
348 case kDense:
349 layer = new OnnxLayerDense();
350 break;
351 case kActivation:
352 layer = new OnnxLayerActivation();
353 break;
354 case kLSTM:
355 layer = new OnnxLayerLSTM();
356 break;
357 default:
358 break;
359 }
360
361 bool result = layer->LoadLayer(i);
362 if (!result) {
363 printf("Failed to load layer %d", i);
364 delete layer;
365 return false;
366 }
367 layers_.push_back(layer);
368 }
369
370 return true;
371}
372
373bool OnnxModel::Apply(Tensor* in, Tensor* out) {
374 Tensor temp_in, temp_out;
375
376 for (unsigned int i = 0; i < layers_.size(); i++) {
377 if (i == 0) {
378 temp_in = *in;
379 }
380 KASSERT(layers_[i]->Apply(&temp_in, &temp_out),
381 "Failed to apply layer %d", i);
382
383 temp_in = temp_out;
384 }
385
386 *out = temp_out;
387
388 return true;
389}
diff --git a/onnx_model.h b/onnx_model.h
new file mode 100644
index 0000000..3a3d165
--- /dev/null
+++ b/onnx_model.h
@@ -0,0 +1,397 @@
1/*
2 * Copyright (c) 2016 Robert W. Rose
3 *
4 * MIT License, see LICENSE file.
5 *
6 * Modified by Texas Instruments (2019) for ONNX import support
7 * MIT License.
8 */
9
10#ifndef ONNX_MODEL_H_
11#define ONNX_MODEL_H_
12
13#define DO_LSTM_RESET
14
15#include <algorithm>
16#include <numeric>
17#include <string>
18#include <vector>
19
20#define KASSERT(x, ...) \
21 if (!(x)) { \
22 printf("KASSERT: %s(%d): ", __FILE__, __LINE__); \
23 printf(__VA_ARGS__); \
24 printf("\n"); \
25 return false; \
26 }
27
28#define KASSERT_EQ(x, y, eps) \
29 if (fabs(x - y) > eps) { \
30 printf("KASSERT: Expected %f, got %f\n", y, x); \
31 return false; \
32 }
33
34#ifdef DEBUG
35#define KDEBUG(x, ...) \
36 if (!(x)) { \
37 printf("%s(%d): ", __FILE__, __LINE__); \
38 printf(__VA_ARGS__); \
39 printf("\n"); \
40 exit(-1); \
41 }
42#else
43#define KDEBUG(x, ...) ;
44#endif
45
46class Tensor {
47 public:
48 Tensor() {}
49
50 Tensor(int i) { Resize(i); }
51
52 Tensor(int i, int j) { Resize(i, j); }
53
54 Tensor(int i, int j, int k) { Resize(i, j, k); }
55
56 Tensor(int i, int j, int k, int l) { Resize(i, j, k, l); }
57
58 void Resize(int i) {
59 dims_ = {i};
60 data_.resize(i);
61 }
62
63 void Resize(int i, int j) {
64 dims_ = {i, j};
65 data_.resize(i * j);
66 }
67
68 void Resize(int i, int j, int k) {
69 dims_ = {i, j, k};
70 data_.resize(i * j * k);
71 }
72
73 void Resize(int i, int j, int k, int l) {
74 dims_ = {i, j, k, l};
75 data_.resize(i * j * k * l);
76 }
77
78 inline void Flatten() {
79 KDEBUG(dims_.size() > 0, "Invalid tensor");
80
81 int elements = dims_[0];
82 for (unsigned int i = 1; i < dims_.size(); i++) {
83 elements *= dims_[i];
84 }
85 dims_ = {elements};
86 }
87
88 inline float& operator()(int i) {
89 KDEBUG(dims_.size() == 1, "Invalid indexing for tensor");
90 KDEBUG(i < dims_[0] && i >= 0, "Invalid i: %d (max %d)", i, dims_[0]);
91
92 return data_[i];
93 }
94
95 inline float& operator()(int i, int j) {
96 KDEBUG(dims_.size() == 2, "Invalid indexing for tensor");
97 KDEBUG(i < dims_[0] && i >= 0, "Invalid i: %d (max %d)", i, dims_[0]);
98 KDEBUG(j < dims_[1] && j >= 0, "Invalid j: %d (max %d)", j, dims_[1]);
99
100 return data_[dims_[1] * i + j];
101 }
102
103 inline float operator()(int i, int j) const {
104 KDEBUG(dims_.size() == 2, "Invalid indexing for tensor");
105 KDEBUG(i < dims_[0] && i >= 0, "Invalid i: %d (max %d)", i, dims_[0]);
106 KDEBUG(j < dims_[1] && j >= 0, "Invalid j: %d (max %d)", j, dims_[1]);
107
108 return data_[dims_[1] * i + j];
109 }
110
111 inline float& operator()(int i, int j, int k) {
112 KDEBUG(dims_.size() == 3, "Invalid indexing for tensor");
113 KDEBUG(i < dims_[0] && i >= 0, "Invalid i: %d (max %d)", i, dims_[0]);
114 KDEBUG(j < dims_[1] && j >= 0, "Invalid j: %d (max %d)", j, dims_[1]);
115 KDEBUG(k < dims_[2] && k >= 0, "Invalid k: %d (max %d)", k, dims_[2]);
116
117 return data_[dims_[2] * (dims_[1] * i + j) + k];
118 }
119
120 inline float& operator()(int i, int j, int k, int l) {
121 KDEBUG(dims_.size() == 4, "Invalid indexing for tensor");
122 KDEBUG(i < dims_[0] && i >= 0, "Invalid i: %d (max %d)", i, dims_[0]);
123 KDEBUG(j < dims_[1] && j >= 0, "Invalid j: %d (max %d)", j, dims_[1]);
124 KDEBUG(k < dims_[2] && k >= 0, "Invalid k: %d (max %d)", k, dims_[2]);
125 KDEBUG(l < dims_[3] && l >= 0, "Invalid l: %d (max %d)", l, dims_[3]);
126
127 return data_[dims_[3] * (dims_[2] * (dims_[1] * i + j) + k) + l];
128 }
129
130 inline void Fill(float value) {
131 std::fill(data_.begin(), data_.end(), value);
132 }
133
134 Tensor Unpack(int row) const {
135 KASSERT(dims_.size() >= 2, "Invalid tensor");
136 std::vector<int> pack_dims =
137 std::vector<int>(dims_.begin() + 1, dims_.end());
138 int pack_size = std::accumulate(pack_dims.begin(), pack_dims.end(), 0);
139
140 std::vector<float>::const_iterator first =
141 data_.begin() + (row * pack_size);
142 std::vector<float>::const_iterator last =
143 data_.begin() + (row + 1) * pack_size;
144
145 Tensor x = Tensor();
146 x.dims_ = pack_dims;
147 x.data_ = std::vector<float>(first, last);
148
149 return x;
150 }
151
152 Tensor Select(int row) const {
153 Tensor x = Unpack(row);
154 x.dims_.insert(x.dims_.begin(), 1);
155
156 return x;
157 }
158
159 Tensor operator+(const Tensor& other) {
160 KASSERT(dims_ == other.dims_,
161 "Cannot add tensors with different dimensions");
162
163 Tensor result;
164 result.dims_ = dims_;
165 result.data_.reserve(data_.size());
166
167 std::transform(data_.begin(), data_.end(), other.data_.begin(),
168 std::back_inserter(result.data_),
169 [](float x, float y) { return x + y; });
170
171 return result;
172 }
173
174 Tensor Multiply(const Tensor& other) {
175 KASSERT(dims_ == other.dims_,
176 "Cannot multiply elements with different dimensions");
177
178 Tensor result;
179 result.dims_ = dims_;
180 result.data_.reserve(data_.size());
181
182 std::transform(data_.begin(), data_.end(), other.data_.begin(),
183 std::back_inserter(result.data_),
184 [](float x, float y) { return x * y; });
185
186 return result;
187 }
188
189 Tensor Dot(const Tensor& other) {
190 KDEBUG(dims_.size() == 2, "Invalid tensor dimensions");
191 KDEBUG(other.dims_.size() == 2, "Invalid tensor dimensions");
192 KASSERT(dims_[1] == other.dims_[0],
193 "Cannot multiply with different inner dimensions");
194
195 Tensor tmp(dims_[0], other.dims_[1]);
196
197 for (int i = 0; i < dims_[0]; i++) {
198 for (int j = 0; j < other.dims_[1]; j++) {
199 for (int k = 0; k < dims_[1]; k++) {
200 tmp(i, j) += (*this)(i, k) * other(k, j);
201 }
202 }
203 }
204
205 return tmp;
206 }
207
208 void Print() {
209 if (dims_.size() == 1) {
210 printf("[ ");
211 for (int i = 0; i < dims_[0]; i++) {
212 printf("%f ", (*this)(i));
213 }
214 printf("]\n");
215 } else if (dims_.size() == 2) {
216 printf("[\n");
217 for (int i = 0; i < dims_[0]; i++) {
218 printf(" [ ");
219 for (int j = 0; j < dims_[1]; j++) {
220 printf("%f ", (*this)(i, j));
221 }
222 printf("]\n");
223 }
224 printf("]\n");
225 } else if (dims_.size() == 3) {
226 printf("[\n");
227 for (int i = 0; i < dims_[0]; i++) {
228 printf(" [\n");
229 for (int j = 0; j < dims_[1]; j++) {
230 printf(" [ ");
231 for (int k = 0; k < dims_[2]; k++) {
232 printf("%f ", (*this)(i, j, k));
233 }
234 printf(" ]\n");
235 }
236 printf(" ]\n");
237 }
238 printf("]\n");
239 } else if (dims_.size() == 4) {
240 printf("[\n");
241 for (int i = 0; i < dims_[0]; i++) {
242 printf(" [\n");
243 for (int j = 0; j < dims_[1]; j++) {
244 printf(" [\n");
245 for (int k = 0; k < dims_[2]; k++) {
246 printf(" [");
247 for (int l = 0; l < dims_[3]; l++) {
248 printf("%f ", (*this)(i, j, k, l));
249 }
250 printf("]\n");
251 }
252 printf(" ]\n");
253 }
254 printf(" ]\n");
255 }
256 printf("]\n");
257 }
258 }
259
260 void PrintShape() {
261 printf("(");
262 for (unsigned int i = 0; i < dims_.size(); i++) {
263 printf("%d ", dims_[i]);
264 }
265 printf(")\n");
266 }
267
268 std::vector<int> dims_;
269 std::vector<float> data_;
270};
271
272enum ActivationType {
273 kLinear = 1,
274 kRelu = 2,
275 kSoftPlus = 3,
276 kSigmoid = 4,
277 kTanh = 5,
278 kHardSigmoid = 6
279};
280
281enum LayerType {
282 kDense = 1,
283 // kConvolution2d = 2,
284 // kFlatten = 3,
285 // kElu = 4,
286 kActivation = 2,
287 // kMaxPooling2D = 6,
288 kLSTM = 3,
289 // kEmbedding = 8
290};
291
292class OnnxLayer {
293 public:
294 OnnxLayer() {}
295
296 virtual ~OnnxLayer() {}
297
298 virtual bool LoadLayer(unsigned int i) = 0;
299
300 virtual bool Apply(Tensor* in, Tensor* out) = 0;
301
302 virtual bool Reset() = 0;
303};
304
305class OnnxLayerActivation : public OnnxLayer {
306 public:
307 OnnxLayerActivation() : activation_type_(ActivationType::kLinear) {}
308
309 virtual ~OnnxLayerActivation() {}
310
311 virtual bool LoadLayer(unsigned int i) { return true; }
312
313 bool LoadActivation(ActivationType activation_);
314
315 virtual bool Apply(Tensor* in, Tensor* out);
316
317 virtual bool Reset() { return true; }
318
319 private:
320 ActivationType activation_type_;
321};
322
323class OnnxLayerDense : public OnnxLayer {
324 public:
325 OnnxLayerDense() {}
326
327 virtual ~OnnxLayerDense() {}
328
329 virtual bool LoadLayer(unsigned int dummy_int);
330
331 virtual bool Apply(Tensor* in, Tensor* out);
332
333 virtual bool Reset() { return true; }
334
335 private:
336 Tensor weights_;
337 Tensor biases_;
338
339 OnnxLayerActivation activation_;
340};
341
342class OnnxLayerLSTM : public OnnxLayer {
343 public:
344 OnnxLayerLSTM() : return_sequences_(false) {}
345
346 virtual ~OnnxLayerLSTM() {}
347
348 virtual bool LoadLayer(unsigned int i);
349
350 virtual bool Apply(Tensor* in, Tensor* out);
351#ifdef DO_LSTM_RESET
352 virtual bool Reset();
353#else
354 virtual bool Reset() { return true; }
355#endif
356 private:
357 bool Step(Tensor* x, Tensor* out, Tensor* ht_1, Tensor* ct_1);
358
359#ifdef DO_LSTM_RESET
360 Tensor ht_1;
361 Tensor ct_1;
362#endif
363 Tensor Wi_;
364 Tensor Ui_;
365 Tensor bi_;
366 Tensor Wf_;
367 Tensor Uf_;
368 Tensor bf_;
369 Tensor Wc_;
370 Tensor Uc_;
371 Tensor bc_;
372 Tensor Wo_;
373 Tensor Uo_;
374 Tensor bo_;
375 OnnxLayerActivation innerActivation_;
376 OnnxLayerActivation activation_;
377 bool return_sequences_;
378};
379
380class OnnxModel {
381 public:
382 OnnxModel() {}
383 virtual ~OnnxModel() {
384 for (unsigned int i = 0; i < layers_.size(); i++) {
385 delete layers_[i];
386 }
387 }
388
389 virtual bool LoadModel();
390
391 virtual bool Apply(Tensor* in, Tensor* out);
392
393 private:
394 std::vector<OnnxLayer*> layers_;
395};
396
397#endif // ONNX_MODEL_H_
diff --git a/psensors.cpp b/psensors.cpp
new file mode 100644
index 0000000..8b333c9
--- /dev/null
+++ b/psensors.cpp
@@ -0,0 +1,144 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <memory.h>
5#include <fcntl.h>
6#include <sys/stat.h>
7#include <sys/types.h>
8#include <unistd.h>
9
10#include <iostream>
11#include <thread>
12#include <chrono>
13
14//#define VERBOSE
15
16#define MAX_BUFFER (3*64*16384)
17static int rd_data_cnt = 0;
18static int wr_data_cnt = 0;
19static float data_ready_to_use[MAX_BUFFER];
20
21int uart_get_data(int samples_count, int overlap_mode, float *samples_array)
22{
23 int rd_idx;
24#ifdef VERBOSE
25 printf ("\nwr=%d rd=%d cnt=%d ", wr_data_cnt, rd_data_cnt, samples_count);
26 printf ("\noverlap_mode=%d ", overlap_mode);
27#endif
28 if(wr_data_cnt >= (rd_data_cnt + samples_count))
29 {
30 for (int i = 0; i < samples_count; i ++)
31 {
32 rd_idx = rd_data_cnt % MAX_BUFFER;
33 samples_array[3*i + 0] = data_ready_to_use[rd_idx + 0];
34 samples_array[3*i + 1] = data_ready_to_use[rd_idx + 1];
35 samples_array[3*i + 2] = data_ready_to_use[rd_idx + 2];
36 rd_data_cnt += 3;
37 }
38 return samples_count;
39 } else return 0;
40}
41
42void uart_stream_parser(int mode_op) {
43const char *log_filename = mode_op & 0x01 ? "parser.txt" : NULL;
44FILE *fout;
45int wr_idx = 0;
46#ifdef VERBOSE
47int prev_sync = 0;
48#endif
49int prev_cnt = 0;
50unsigned int val1, val_phases, val_position;
51short int_phase1, int_phase2;
52float phase1, phase2, position;
53unsigned char all_zeros[256];
54unsigned char line_in[256];
55int sync_i = (1 << 31);
56int cnt, err_flag = 0;
57int fd;
58// FIFO file path
59const char * myfifo = "/tmp/myfifo";
60
61 memset(all_zeros, 0, 256);
62 memset(line_in, 0, 256);
63
64 // Creating the named file(FIFO)
65 mkfifo(myfifo, 0666);
66
67 // Open FIFO for Read only
68 fd = open(myfifo, O_RDONLY);
69
70 if(log_filename)
71 {
72 fout = fopen(log_filename, "wt");
73 }
74
75 for (int i = 0; ; i ++)
76 { // Shift delay line left for one byte
77 for(int j = 1; j < 256; j ++) line_in[j - 1] = line_in[j];
78 if(read(fd, &line_in[255], 1) != 1) {
79 line_in[255] = 0;
80 if(wr_data_cnt <= rd_data_cnt) { // Continue processing until all buffered samples are processed
81 // No unprocessed bytes left, sleep for some time as there is nothing on input
82 std::this_thread::sleep_for(std::chrono::milliseconds(20));
83 continue;
84 }
85 }
86
87 // Writting too fast, do write side throttling
88 if((mode_op & 0x02) && (wr_data_cnt > (rd_data_cnt + MAX_BUFFER - 16)))
89 std::this_thread::sleep_for(std::chrono::milliseconds(100));
90
91 if(line_in[0] == 0xce &&
92 line_in[1] == 0xfa &&
93 line_in[2] == 0xbe &&
94 line_in[3] == 0xba
95 )
96 {
97 cnt = *(unsigned int *)&line_in[1*4];
98 sync_i = i + 2*4;
99#ifdef VERBOSE
100 printf ("\n%08x, %8d:%8d ... %8d[%8d]\n", *(unsigned int *)&line_in[0], i, cnt,
101 sync_i - prev_sync, cnt - prev_cnt);
102 prev_sync = sync_i;
103#endif
104 if((i >= 4*1024) && (cnt - prev_cnt) != 1023) err_flag |= 1;
105 prev_cnt = cnt;
106 }
107 if((i - sync_i) >= 0)
108 {
109#ifdef VERBOSE
110 if(((i - sync_i) % (2*4)) == 0) printf ("\n[%8d, %8d] ... ", i, sync_i);
111#endif
112 if(((i - sync_i) % 4) == 0)
113 {
114 val1 = *(unsigned int *)&line_in[0];
115#ifdef VERBOSE
116 printf ("%08x ", val1);
117#endif
118 if(((i - sync_i) % 8) == 0) {
119 val_phases = val1;
120 } else {
121 val_position = (int)val1;
122 int_phase1 = (short)(val_phases >> 16);
123 int_phase2 = (short)((val_phases << 16) >> 16);
124 phase1 = (float)int_phase1 / 32767.0;
125 phase2 = (float)int_phase2 / 32767.0;
126 position = (float)val_position / 33554432.0f;
127 if(log_filename) fprintf(fout, "%8.5f %8.5f %8.5f\n", phase1, phase2, position);
128#ifdef VERBOSE
129 printf("... %8.5f %8.5f %8.5f", phase1, phase2, position);
130#endif
131 wr_idx = wr_data_cnt % MAX_BUFFER;
132 data_ready_to_use[wr_idx + 0] = phase1;
133 data_ready_to_use[wr_idx + 1] = phase2;
134 data_ready_to_use[wr_idx + 2] = position;
135 wr_data_cnt += 3;
136 }
137 }
138 }
139 }
140 printf("\nEnd of stream!!!\n"); fflush(stdout);
141 if(log_filename) fclose(fout);
142 close(fd);
143}
144/** nothing past this point **/
diff --git a/readme.txt b/readme.txt
new file mode 100644
index 0000000..162f32e
--- /dev/null
+++ b/readme.txt
@@ -0,0 +1,17 @@
1==============================
2Native compilation on target
3==============================
4Copy the folder pdm-anomaly-detection to target, and then run "./mk.sh"
5After the compilation is completed successfully, motpdm binary will be created.
6
7
8==============================
9Run motpdm on target
10==============================
11Terminal 1: ./run.sh > log.txt
12Terminal 2: cat ./logs/normal45-270-v100-with-friction2-iter10-15.log > /tmp/myfifo
13
14After the cat command in Terminal 2 is done, stop the process (CTRL-C) in Terminal 1.
15
16Then, check reports in log.txt, with time units being in 20ms ticks (decimated sample
17rate of 50Hz). AD (Anomaly detection) is reported along with the sample counter.
diff --git a/run.sh b/run.sh
new file mode 100755
index 0000000..f040753
--- /dev/null
+++ b/run.sh
@@ -0,0 +1 @@
./motpdm -0.0001841035315 0.004191935886 0.01918238488 0.01933241767