Skip to content

Commit

Permalink
ModelInfos.txt parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
laitassou committed Apr 29, 2020
1 parent 36fe501 commit 60fe0bc
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 105 deletions.
3 changes: 2 additions & 1 deletion service/examples/mpg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions service/examples/mpg/ModelInfo.txt
Original file line number Diff line number Diff line change
@@ -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}}
13 changes: 9 additions & 4 deletions service/examples/mpg/main.cpp
Original file line number Diff line number Diff line change
@@ -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 <algorithm>
#include <iterator>
Expand All @@ -16,10 +16,15 @@ int main() {
//m.restore("../checkpoint/train.ckpt");

ModelInfos<float> mInfos("../ModelInfo.txt");
mInfos.show();

mInfos.parse();

mInfos.show();


mInfos.compute_columns();

std::cout << "depth:" <<mInfos.get_depth() <<std::endl;
// Create Tensors
Tensor input(m, "x");
Tensor prediction(m, "Identity");
Expand Down
2 changes: 1 addition & 1 deletion service/include/Model.h → service/include/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <tuple>
#include <memory>
#include <tensorflow/c/c_api.h>
#include "Tensor.h"
#include "Tensor.hpp"

class Tensor;

Expand Down
96 changes: 0 additions & 96 deletions service/include/ModelInfos.h

This file was deleted.

46 changes: 46 additions & 0 deletions service/include/ModelInfos.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* class ModelInfos.h
* user: laitassou
* description
*/

#pragma once


#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <cassert>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>
#include <map>


template <class T> 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<std::string,struct Stats> labels;
json_ptree pt_json;
};
2 changes: 1 addition & 1 deletion service/include/Tensor.h → service/include/Tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <numeric>
#include <cstring>
#include <tensorflow/c/c_api.h>
#include "Model.h"
#include "Model.hpp"

class Model;

Expand Down
2 changes: 1 addition & 1 deletion service/src/Model.cpp
Original file line number Diff line number Diff line change
@@ -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()){

Expand Down
148 changes: 148 additions & 0 deletions service/src/ModelInfos.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include "../include/ModelInfos.hpp"


template <class T> ModelInfos<T>::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 <class T> const T & ModelInfos<T>::get_mean(const std::string &label) const
{
auto it = labels.find(label);
if(it == labels.end())
{
std::cout <<"label: "<< label << "not found" <<std::endl;
return T(0);
}
else{
return it->second.mean;
}
}

template <class T> const T & ModelInfos<T>::get_std(const std::string &label) const
{
auto it = labels.find(label);
if(it == labels.end())
{
std::cout <<"label: "<< label << "not found" <<std::endl;
return T(0);
}
else{
return it->second.std;
}
}

template <class T> void ModelInfos<T>::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 << " :" <<std::endl;
auto row = it->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: " <<itr->first << "\n";
auto row = itr->second;

std::cout << "stats: "<< itr->second.std << ","<< itr->second.mean << std::endl;
}
}


template <class T> void ModelInfos<T>::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 << " :" <<std::endl;

auto row = it->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<std::string, struct Stats>(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<std::string, struct Stats>(rr.first,stat));
}
else
{
stat = labels[rr.first] ;
stat.std = std::stod(rr.second.data());
labels[rr.first] = stat;
}
}
}

}
}


template <class T> void ModelInfos<T>::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 <<std::endl;
}
break;

}

}


template class ModelInfos<float>;
template class ModelInfos<double>;
template class ModelInfos<int>;
template class ModelInfos<std::string>;
2 changes: 1 addition & 1 deletion service/src/Tensor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../include/Tensor.h"
#include "../include/Tensor.hpp"

#include <utility>

Expand Down

0 comments on commit 60fe0bc

Please sign in to comment.