From d87eade4641b94c8eb23dd2248939c5153b90358 Mon Sep 17 00:00:00 2001 From: atlas Date: Wed, 22 Apr 2020 13:47:27 +0200 Subject: [PATCH] models --- README.md | 15 ++++++++++ regression/ml1.py | 43 ++++++++++++++++++++++++++++ service/examples/coco/main.cpp | 4 --- service/examples/load_model/main.cpp | 4 --- service/examples/mnist/main.cpp | 3 -- service/include/Model.h | 19 ++++++++---- service/include/Tensor.h | 8 +----- service/src/Model.cpp | 28 ++++++++---------- service/src/Tensor.cpp | 10 ++----- 9 files changed, 88 insertions(+), 46 deletions(-) create mode 100755 regression/ml1.py diff --git a/README.md b/README.md index 7db9053..25ad084 100644 --- a/README.md +++ b/README.md @@ -1 +1,16 @@ # ML + + +needed packages for venv + + 302 sudo apt install python3-dev python3-pip + 303 sudo pip3 install -U virtualenv + 331 pip list + 332 pip install --upgrade tensorflow + 382 pip3 --version + 383 sudo apt install python3-pip + 437 pip install --upgrade matplotlib + 439 pip install --upgrade pandas + 441 pip install --upgrade seaborn + 443 pip install --upgrade tensorflow_docs + 444 pip install git+https://github.com/tensorflow/docs diff --git a/regression/ml1.py b/regression/ml1.py new file mode 100755 index 0000000..847420f --- /dev/null +++ b/regression/ml1.py @@ -0,0 +1,43 @@ +import tensorflow as tf + +mnist = tf.keras.datasets.mnist + +(x_train, y_train), (x_test, y_test) = mnist.load_data() +x_train, x_test = x_train / 255.0, x_test / 255.0 + + +model = tf.keras.models.Sequential([ + tf.keras.layers.Flatten(input_shape=(28, 28)), + tf.keras.layers.Dense(128, activation='relu'), + tf.keras.layers.Dropout(0.2), + tf.keras.layers.Dense(10) +]) + + +predictions = model(x_train[:1]).numpy() +predictions + +print ("predictions:") +print(predictions) + + +tf.nn.softmax(predictions).numpy() + +print (" softmax:") +print(predictions) + + +loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) +loss_fn(y_train[:1], predictions).numpy() + + +model.compile(optimizer='adam', + loss=loss_fn, + metrics=['accuracy']) + + +model.fit(x_train, y_train, epochs=5) + + +model.evaluate(x_test, y_test, verbose=2) + diff --git a/service/examples/coco/main.cpp b/service/examples/coco/main.cpp index 5fa1225..3157349 100644 --- a/service/examples/coco/main.cpp +++ b/service/examples/coco/main.cpp @@ -1,7 +1,3 @@ -// -// Created by sergio on 16/05/19. -// - #include "../../include/Model.h" #include "../../include/Tensor.h" #include diff --git a/service/examples/load_model/main.cpp b/service/examples/load_model/main.cpp index ee59ddc..c8f5a7a 100644 --- a/service/examples/load_model/main.cpp +++ b/service/examples/load_model/main.cpp @@ -1,7 +1,3 @@ -// -// Created by sergio on 16/05/19. -// - #include "../../include/Model.h" #include "../../include/Tensor.h" diff --git a/service/examples/mnist/main.cpp b/service/examples/mnist/main.cpp index 432b947..acf94e9 100644 --- a/service/examples/mnist/main.cpp +++ b/service/examples/mnist/main.cpp @@ -1,6 +1,3 @@ -// -// Created by sergio on 12/05/19. -// #include "../../include/Model.h" #include "../../include/Tensor.h" diff --git a/service/include/Model.h b/service/include/Model.h index b55c774..106aff5 100644 --- a/service/include/Model.h +++ b/service/include/Model.h @@ -1,9 +1,8 @@ // -// Created by sergio on 12/05/19. +// Model // -#ifndef CPPFLOW_MODEL_H -#define CPPFLOW_MODEL_H +#pragma once #include #include @@ -12,11 +11,15 @@ #include #include #include +#include #include #include "Tensor.h" class Tensor; +//using GraphResourcePtr = std::unique_ptr ; + + class Model { public: explicit Model(const std::string&); @@ -48,8 +51,15 @@ class Model { void run(const std::vector& inputs, Tensor* output); void run(Tensor* input, Tensor* output); + struct GraphCreate { + TF_Graph * operator()() { return TF_NewGraph(); } + }; + struct GraphDeleter { + void operator()(TF_Graph* b) { TF_DeleteGraph(b); } + }; + private: - TF_Graph* graph; + std::unique_ptr graph; TF_Session* session; TF_Status* status; @@ -64,4 +74,3 @@ class Model { }; -#endif //CPPFLOW_MODEL_H diff --git a/service/include/Tensor.h b/service/include/Tensor.h index 501b78e..9d796bb 100644 --- a/service/include/Tensor.h +++ b/service/include/Tensor.h @@ -1,9 +1,4 @@ -// -// Created by sergio on 13/05/19. -// - -#ifndef CPPFLOW_TENSOR_H -#define CPPFLOW_TENSOR_H +#pragma once #include #include @@ -63,4 +58,3 @@ class Tensor { friend class Model; }; -#endif //CPPFLOW_TENSOR_H diff --git a/service/src/Model.cpp b/service/src/Model.cpp index d09fdfd..6957bb0 100644 --- a/service/src/Model.cpp +++ b/service/src/Model.cpp @@ -1,25 +1,21 @@ -// -// Created by sergio on 12/05/19. -// - #include "../include/Model.h" -Model::Model(const std::string& model_filename) { +Model::Model(const std::string& model_filename):graph(TF_NewGraph()){ this->status = TF_NewStatus(); - this->graph = TF_NewGraph(); + //this->graph = std::make_unique(GraphCreate(), GraphDeleter); //TF_NewGraph(); // Create the session. TF_SessionOptions* sess_opts = TF_NewSessionOptions(); - this->session = TF_NewSession(this->graph, sess_opts, this->status); + this->session = TF_NewSession(graph.get(), sess_opts, this->status); TF_DeleteSessionOptions(sess_opts); // Check the status this->status_check(true); // Create the graph - TF_Graph* g = this->graph; + ////TF_Graph* g = this->graph; // Import the graph definition @@ -27,7 +23,7 @@ Model::Model(const std::string& model_filename) { this->error_check(def != nullptr, "An error occurred reading the model"); TF_ImportGraphDefOptions* graph_opts = TF_NewImportGraphDefOptions(); - TF_GraphImportGraphDef(g, def, graph_opts, this->status); + TF_GraphImportGraphDef(graph.get(), def, graph_opts, this->status); TF_DeleteImportGraphDefOptions(graph_opts); TF_DeleteBuffer(def); @@ -37,14 +33,14 @@ Model::Model(const std::string& model_filename) { Model::~Model() { TF_DeleteSession(this->session, this->status); - TF_DeleteGraph(this->graph); + ///TF_DeleteGraph(this->graph); this->status_check(true); TF_DeleteStatus(this->status); } void Model::init() { - TF_Operation* init_op[1] = {TF_GraphOperationByName(this->graph, "init")}; + TF_Operation* init_op[1] = {TF_GraphOperationByName(this->graph.get(), "init")}; this->error_check(init_op[0]!= nullptr, "Error: No operation named \"init\" exists"); @@ -71,12 +67,12 @@ void Model::save(const std::string &ckpt) { } TF_Output output_file; - output_file.oper = TF_GraphOperationByName(this->graph, "save/Const"); + output_file.oper = TF_GraphOperationByName(this->graph.get(), "save/Const"); output_file.index = 0; TF_Output inputs[1] = {output_file}; TF_Tensor* input_values[1] = {t}; - const TF_Operation* restore_op[1] = {TF_GraphOperationByName(this->graph, "save/control_dependency")}; + const TF_Operation* restore_op[1] = {TF_GraphOperationByName(this->graph.get(), "save/control_dependency")}; if (!restore_op[0]) { TF_DeleteTensor(t); this->error_check(false, "Error: No operation named \"save/control_dependencyl\" exists"); @@ -106,12 +102,12 @@ void Model::restore(const std::string& ckpt) { } TF_Output output_file; - output_file.oper = TF_GraphOperationByName(this->graph, "save/Const"); + output_file.oper = TF_GraphOperationByName(this->graph.get(), "save/Const"); output_file.index = 0; TF_Output inputs[1] = {output_file}; TF_Tensor* input_values[1] = {t}; - const TF_Operation* restore_op[1] = {TF_GraphOperationByName(this->graph, "save/restore_all")}; + const TF_Operation* restore_op[1] = {TF_GraphOperationByName(this->graph.get(), "save/restore_all")}; if (!restore_op[0]) { TF_DeleteTensor(t); this->error_check(false, "Error: No operation named \"save/restore_all\" exists"); @@ -168,7 +164,7 @@ std::vector Model::get_operations() const { TF_Operation* oper; // Iterate through the operations of a graph - while ((oper = TF_GraphNextOperation(this->graph, &pos)) != nullptr) { + while ((oper = TF_GraphNextOperation(this->graph.get(), &pos)) != nullptr) { result.emplace_back(TF_OperationName(oper)); } diff --git a/service/src/Tensor.cpp b/service/src/Tensor.cpp index 26d0ea4..bdcc550 100644 --- a/service/src/Tensor.cpp +++ b/service/src/Tensor.cpp @@ -1,7 +1,3 @@ -// -// Created by sergio on 13/05/19. -// - #include "../include/Tensor.h" #include @@ -9,7 +5,7 @@ Tensor::Tensor(const Model& model, const std::string& operation) { // Get operation by the name - this->op.oper = TF_GraphOperationByName(model.graph, operation.c_str()); + this->op.oper = TF_GraphOperationByName(model.graph.get(), operation.c_str()); this->op.index = 0; // Operation did not exists @@ -18,7 +14,7 @@ Tensor::Tensor(const Model& model, const std::string& operation) { // DIMENSIONS // Get number of dimensions - int n_dims = TF_GraphGetTensorNumDims(model.graph, this->op, model.status); + int n_dims = TF_GraphGetTensorNumDims(model.graph.get(), this->op, model.status); // DataType this->type = TF_OperationOutputType(this->op); @@ -27,7 +23,7 @@ Tensor::Tensor(const Model& model, const std::string& operation) { if (n_dims > 0) { // Get dimensions auto *dims = new int64_t[n_dims]; - TF_GraphGetTensorShape(model.graph, this->op, dims, n_dims, model.status); + TF_GraphGetTensorShape(model.graph.get(), this->op, dims, n_dims, model.status); // Check error on Model Status model.status_check(true);