From 60fe0bcad75c0091e4bf802670702de55569857c Mon Sep 17 00:00:00 2001 From: atlas Date: Wed, 29 Apr 2020 15:11:00 +0200 Subject: [PATCH] ModelInfos.txt parsing --- service/examples/mpg/CMakeLists.txt | 3 +- service/examples/mpg/ModelInfo.txt | 1 + service/examples/mpg/main.cpp | 13 +- service/include/{Model.h => Model.hpp} | 2 +- service/include/ModelInfos.h | 96 --------------- service/include/ModelInfos.hpp | 46 +++++++ service/include/{Tensor.h => Tensor.hpp} | 2 +- service/src/Model.cpp | 2 +- service/src/ModelInfos.cpp | 148 +++++++++++++++++++++++ service/src/Tensor.cpp | 2 +- 10 files changed, 210 insertions(+), 105 deletions(-) create mode 100644 service/examples/mpg/ModelInfo.txt rename service/include/{Model.h => Model.hpp} (99%) delete mode 100644 service/include/ModelInfos.h create mode 100644 service/include/ModelInfos.hpp rename service/include/{Tensor.h => Tensor.hpp} (98%) create mode 100644 service/src/ModelInfos.cpp diff --git a/service/examples/mpg/CMakeLists.txt b/service/examples/mpg/CMakeLists.txt index 9f0b3e3..704fe1e 100644 --- a/service/examples/mpg/CMakeLists.txt +++ b/service/examples/mpg/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.10) project(mpg) set(CMAKE_CXX_STANDARD 17) -add_executable(mpg main.cpp ../../src/Model.cpp ../../src/Tensor.cpp) +#include_directories(../../include) +add_executable(mpg main.cpp ../../src/Model.cpp ../../src/Tensor.cpp ../../src/ModelInfos.cpp ) target_include_directories(mpg PRIVATE ../../include) target_link_libraries (mpg -ltensorflow) \ No newline at end of file diff --git a/service/examples/mpg/ModelInfo.txt b/service/examples/mpg/ModelInfo.txt new file mode 100644 index 0000000..a4c5c50 --- /dev/null +++ b/service/examples/mpg/ModelInfo.txt @@ -0,0 +1 @@ +{"count":{"Cylinders":314.0,"Displacement":314.0,"Horsepower":314.0,"Weight":314.0,"Acceleration":314.0,"Model Year":314.0,"Origin":314.0},"mean":{"Cylinders":5.4777070064,"Displacement":195.3184713376,"Horsepower":104.8694267516,"Weight":2990.2515923567,"Acceleration":15.5592356688,"Model Year":75.898089172,"Origin":1.5732484076},"std":{"Cylinders":1.6997875727,"Displacement":104.331588508,"Horsepower":38.0962144353,"Weight":843.8985961906,"Acceleration":2.7892297519,"Model Year":3.6756424982,"Origin":0.8009883803},"min":{"Cylinders":3.0,"Displacement":68.0,"Horsepower":46.0,"Weight":1649.0,"Acceleration":8.0,"Model Year":70.0,"Origin":1.0},"25%":{"Cylinders":4.0,"Displacement":105.5,"Horsepower":76.25,"Weight":2256.5,"Acceleration":13.8,"Model Year":73.0,"Origin":1.0},"50%":{"Cylinders":4.0,"Displacement":151.0,"Horsepower":94.5,"Weight":2822.5,"Acceleration":15.5,"Model Year":76.0,"Origin":1.0},"75%":{"Cylinders":8.0,"Displacement":265.75,"Horsepower":128.0,"Weight":3608.0,"Acceleration":17.2,"Model Year":79.0,"Origin":2.0},"max":{"Cylinders":8.0,"Displacement":455.0,"Horsepower":225.0,"Weight":5140.0,"Acceleration":24.8,"Model Year":82.0,"Origin":3.0}} \ No newline at end of file diff --git a/service/examples/mpg/main.cpp b/service/examples/mpg/main.cpp index ef21ffb..b8eaee9 100644 --- a/service/examples/mpg/main.cpp +++ b/service/examples/mpg/main.cpp @@ -1,6 +1,6 @@ -#include "../../include/Model.h" -#include "../../include/Tensor.h" -#include "../../include/ModelInfos.h" +#include "../../include/Model.hpp" +#include "../../include/Tensor.hpp" +#include "../../include/ModelInfos.hpp" #include #include @@ -16,10 +16,15 @@ int main() { //m.restore("../checkpoint/train.ckpt"); ModelInfos mInfos("../ModelInfo.txt"); - mInfos.show(); mInfos.parse(); + mInfos.show(); + + + mInfos.compute_columns(); + + std::cout << "depth:" < #include #include -#include "Tensor.h" +#include "Tensor.hpp" class Tensor; diff --git a/service/include/ModelInfos.h b/service/include/ModelInfos.h deleted file mode 100644 index 391f503..0000000 --- a/service/include/ModelInfos.h +++ /dev/null @@ -1,96 +0,0 @@ -/* -* class ModelInfos.h -* user: laitassou -* description -*/ - -#pragma once - - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -template class ModelInfos -{ - public: - struct Stats - { - T mean; - T std; - }; - - ModelInfos(const std::string & name):input_depth(0) - { - - try - { - //std::stringstream ss; - // send your JSON above to the parser below, but populate ss first - //boost::property_tree::ptree pt; - - std::ifstream jsonFile(name); - boost::property_tree::read_json(jsonFile, pt_json); - - } - catch (std::exception const& e) - { - std::cout<< e.what()<< std::endl; - } - } - const std::string & get(const std::string &label) const - { - auto it = labels.find(label); - if(it == labels.end()) - { - return std::string(); - } - else{ - return it->second; - } - } - - size_t get_depth()const - { - return input_depth; - } - - void show(void) const - { - using boost::property_tree::ptree; - ptree::const_iterator end = pt_json.end(); - for (ptree::const_iterator it = pt_json.begin(); it != end; ++it) { - std::cout << it->first << " :" <second; - for(auto & rr :row) - std::cout << rr.first << ":" << rr.second.data()<< std::endl; - } - } - void parse(void) - { - using boost::property_tree::ptree; - ptree::const_iterator end = pt_json.end(); - for (ptree::const_iterator it = pt_json.begin(); it != end; ++it) { - auto row = it->second; - for(auto & rr :row) - { - input_depth++; - std::cout << rr.first << ", input_depth :" << input_depth < labels; - boost::property_tree::ptree pt_json; -}; \ No newline at end of file diff --git a/service/include/ModelInfos.hpp b/service/include/ModelInfos.hpp new file mode 100644 index 0000000..d94c9e3 --- /dev/null +++ b/service/include/ModelInfos.hpp @@ -0,0 +1,46 @@ +/* +* class ModelInfos.h +* user: laitassou +* description +*/ + +#pragma once + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +template class ModelInfos +{ + public: + struct Stats + { + T mean; + T std; + }; + using json_ptree= boost::property_tree::ptree; + + ModelInfos(const std::string & name); + const T & get_mean(const std::string &label) const; + const T & get_std(const std::string &label) const; + void show(void) const; + void compute_columns(void); + void parse(void); + inline size_t get_depth()const + { + return input_depth; + }; + + private: + size_t input_depth; + std::map labels; + json_ptree pt_json; +}; \ No newline at end of file diff --git a/service/include/Tensor.h b/service/include/Tensor.hpp similarity index 98% rename from service/include/Tensor.h rename to service/include/Tensor.hpp index fb8ae28..cf8e5cb 100644 --- a/service/include/Tensor.h +++ b/service/include/Tensor.hpp @@ -13,7 +13,7 @@ #include #include #include -#include "Model.h" +#include "Model.hpp" class Model; diff --git a/service/src/Model.cpp b/service/src/Model.cpp index 7ae8a3b..e909af4 100644 --- a/service/src/Model.cpp +++ b/service/src/Model.cpp @@ -1,4 +1,4 @@ -#include "../include/Model.h" +#include "../include/Model.hpp" Model::Model(const std::string& model_filename):_graph(TF_NewGraph()),_status( TF_NewStatus()){ diff --git a/service/src/ModelInfos.cpp b/service/src/ModelInfos.cpp new file mode 100644 index 0000000..f2e7cff --- /dev/null +++ b/service/src/ModelInfos.cpp @@ -0,0 +1,148 @@ +#include "../include/ModelInfos.hpp" + + +template ModelInfos::ModelInfos(const std::string & name):input_depth(0) +{ + + try + { + //std::stringstream ss; + // send your JSON above to the parser below, but populate ss first + //boost::property_tree::ptree pt; + + std::ifstream jsonFile(name); + boost::property_tree::read_json(jsonFile, pt_json); + + } + catch (std::exception const& e) + { + std::cout<< e.what()<< std::endl; + } +} + + +template const T & ModelInfos::get_mean(const std::string &label) const +{ + auto it = labels.find(label); + if(it == labels.end()) + { + std::cout <<"label: "<< label << "not found" <second.mean; + } +} + +template const T & ModelInfos::get_std(const std::string &label) const +{ + auto it = labels.find(label); + if(it == labels.end()) + { + std::cout <<"label: "<< label << "not found" <second.std; + } +} + +template void ModelInfos::show(void) const +{ + using boost::property_tree::ptree; + ptree::const_iterator end = pt_json.end(); + for (ptree::const_iterator it = pt_json.begin(); it != end; ++it) { + std::cout << it->first << " :" <second; + for(auto & rr :row) + std::cout << rr.first << ":" << rr.second.data()<< std::endl; + } + + // show map + for( auto itr =labels.begin(); itr != labels.end(); ++itr) + { + std::cout << "label: " <first << "\n"; + auto row = itr->second; + + std::cout << "stats: "<< itr->second.std << ","<< itr->second.mean << std::endl; + } +} + + +template void ModelInfos::parse(void) +{ + using boost::property_tree::ptree; + ptree::const_iterator end = pt_json.end(); + for (ptree::const_iterator it = pt_json.begin(); it != end; ++it) + { + std::cout << it->first << " :" <second; + if ((it->first).compare("mean")==0) + { + + for(auto & rr :row) + { + struct Stats stat {0,0}; + std::cout << rr.first << ":" << rr.second.data()<< std::endl; + + if (labels.find(rr.first) == labels.end()) + { + stat.mean = std::stod(rr.second.data()); + labels.insert(std::pair(rr.first,stat)); + } + else + { + stat = labels[rr.first] ; + stat.std = std::stod(rr.second.data()); + labels[rr.first] = stat; + } + stat.mean = std::stod( rr.second.data()); + labels.insert({rr.first,stat}); + } + } + else if ((it->first).compare("std")==0) + { + for(auto & rr :row){ + struct Stats stat {0,0}; + std::cout << rr.first << ":" << rr.second.data()<< std::endl; + if (labels.find(rr.first) == labels.end()) + { + stat.std= std::stod(rr.second.data());; + labels.insert(std::pair(rr.first,stat)); + } + else + { + stat = labels[rr.first] ; + stat.std = std::stod(rr.second.data()); + labels[rr.first] = stat; + } + } + } + + } +} + + +template void ModelInfos::compute_columns(void) +{ + using boost::property_tree::ptree; + ptree::const_iterator end = pt_json.end(); + for (ptree::const_iterator it = pt_json.begin(); it != end; ++it) { + auto row = it->second; + for(auto & rr :row) + { + input_depth++; + std::cout << rr.first << ", input_depth :" << input_depth <; +template class ModelInfos; +template class ModelInfos; +template class ModelInfos; \ No newline at end of file diff --git a/service/src/Tensor.cpp b/service/src/Tensor.cpp index d30c02f..abf7273 100644 --- a/service/src/Tensor.cpp +++ b/service/src/Tensor.cpp @@ -1,4 +1,4 @@ -#include "../include/Tensor.h" +#include "../include/Tensor.hpp" #include