Skip to content

Commit

Permalink
models
Browse files Browse the repository at this point in the history
  • Loading branch information
laitassou committed Apr 22, 2020
1 parent ef770a9 commit d87eade
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 46 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions regression/ml1.py
Original file line number Diff line number Diff line change
@@ -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)

4 changes: 0 additions & 4 deletions service/examples/coco/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Created by sergio on 16/05/19.
//

#include "../../include/Model.h"
#include "../../include/Tensor.h"
#include <opencv2/opencv.hpp>
Expand Down
4 changes: 0 additions & 4 deletions service/examples/load_model/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Created by sergio on 16/05/19.
//

#include "../../include/Model.h"
#include "../../include/Tensor.h"

Expand Down
3 changes: 0 additions & 3 deletions service/examples/mnist/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//
// Created by sergio on 12/05/19.
//

#include "../../include/Model.h"
#include "../../include/Tensor.h"
Expand Down
19 changes: 14 additions & 5 deletions service/include/Model.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//
// Created by sergio on 12/05/19.
// Model
//

#ifndef CPPFLOW_MODEL_H
#define CPPFLOW_MODEL_H
#pragma once

#include <cstring>
#include <algorithm>
Expand All @@ -12,11 +11,15 @@
#include <iostream>
#include <fstream>
#include <tuple>
#include <memory>
#include <tensorflow/c/c_api.h>
#include "Tensor.h"

class Tensor;

//using GraphResourcePtr = std::unique_ptr<TF_Graph> ;


class Model {
public:
explicit Model(const std::string&);
Expand Down Expand Up @@ -48,8 +51,15 @@ class Model {
void run(const std::vector<Tensor*>& 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<TF_Graph, GraphDeleter> graph;
TF_Session* session;
TF_Status* status;

Expand All @@ -64,4 +74,3 @@ class Model {
};


#endif //CPPFLOW_MODEL_H
8 changes: 1 addition & 7 deletions service/include/Tensor.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
//
// Created by sergio on 13/05/19.
//

#ifndef CPPFLOW_TENSOR_H
#define CPPFLOW_TENSOR_H
#pragma once

#include <vector>
#include <memory>
Expand Down Expand Up @@ -63,4 +58,3 @@ class Tensor {
friend class Model;
};

#endif //CPPFLOW_TENSOR_H
28 changes: 12 additions & 16 deletions service/src/Model.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
//
// 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<TF_Graph>(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
TF_Buffer* def = read(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);

Expand All @@ -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");

Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -168,7 +164,7 @@ std::vector<std::string> 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));
}

Expand Down
10 changes: 3 additions & 7 deletions service/src/Tensor.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
//
// Created by sergio on 13/05/19.
//

#include "../include/Tensor.h"

#include <utility>

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
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit d87eade

Please sign in to comment.