Skip to content

Commit

Permalink
clang format
Browse files Browse the repository at this point in the history
  • Loading branch information
PatWie committed Dec 11, 2018
1 parent aa82b14 commit 6759705
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 61 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
BasedOnStyle: Google

...
12 changes: 5 additions & 7 deletions examples/event_writer/write_events.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <tensorflow/core/util/events_writer.h>
#include <string>
#include <iostream>
#include <string>


void write_scalar(tensorflow::EventsWriter* writer, double wall_time, tensorflow::int64 step,
const std::string& tag, float simple_value) {
void write_scalar(tensorflow::EventsWriter* writer, double wall_time,
tensorflow::int64 step, const std::string& tag,
float simple_value) {
tensorflow::Event event;
event.set_wall_time(wall_time);
event.set_step(step);
Expand All @@ -14,9 +14,7 @@ void write_scalar(tensorflow::EventsWriter* writer, double wall_time, tensorflow
writer->WriteEvent(event);
}


int main(int argc, char const *argv[]) {

int main(int argc, char const* argv[]) {
std::string envent_file = "./events";
tensorflow::EventsWriter writer(envent_file);
for (int i = 0; i < 150; ++i)
Expand Down
45 changes: 23 additions & 22 deletions examples/keras/inference.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// 2018, Patrick Wieschollek <mail@patwie.com>
#include <tensorflow/core/protobuf/meta_graph.pb.h>
#include <tensorflow/core/public/session.h>
#include <tensorflow/core/public/session_options.h>
#include <tensorflow/core/protobuf/meta_graph.pb.h>
#include <string>
#include <iostream>
#include <string>

typedef std::vector<std::pair<std::string, tensorflow::Tensor>> tensor_dict;


/**
* @brief load a previous store model
* @details [long description]
Expand All @@ -18,53 +17,55 @@ typedef std::vector<std::pair<std::string, tensorflow::Tensor>> tensor_dict;
* saver.save(sess, './exported/my_model')
* tf.train.write_graph(sess.graph, '.', './exported/graph.pb, as_text=False)
*
* this relies on a graph which has an operation called `init` responsible to initialize all variables, eg.
* this relies on a graph which has an operation called `init` responsible to
* initialize all variables, eg.
*
* sess.run(tf.global_variables_initializer()) # somewhere in the python file
* sess.run(tf.global_variables_initializer()) # somewhere in the python
* file
*
* @param sess active tensorflow session
* @param graph_fn path to graph file (eg. "./exported/graph.pb")
* @param checkpoint_fn path to checkpoint file (eg. "./exported/my_model", optional)
* @param checkpoint_fn path to checkpoint file (eg. "./exported/my_model",
* optional)
* @return status of reloading
*/
tensorflow::Status LoadModel(tensorflow::Session *sess, std::string graph_fn, std::string checkpoint_fn = "") {
tensorflow::Status LoadModel(tensorflow::Session *sess, std::string graph_fn,
std::string checkpoint_fn = "") {
tensorflow::Status status;

// Read in the protobuf graph we exported
tensorflow::MetaGraphDef graph_def;
status = ReadBinaryProto(tensorflow::Env::Default(), graph_fn, &graph_def);
if (status != tensorflow::Status::OK())
return status;
if (status != tensorflow::Status::OK()) return status;

// create the graph
status = sess->Create(graph_def.graph_def());
if (status != tensorflow::Status::OK())
return status;
if (status != tensorflow::Status::OK()) return status;

// restore model from checkpoint, iff checkpoint is given
if (checkpoint_fn != "") {
tensorflow::Tensor checkpointPathTensor(tensorflow::DT_STRING, tensorflow::TensorShape());
tensorflow::Tensor checkpointPathTensor(tensorflow::DT_STRING,
tensorflow::TensorShape());
checkpointPathTensor.scalar<std::string>()() = checkpoint_fn;

tensor_dict feed_dict = {{graph_def.saver_def().filename_tensor_name(), checkpointPathTensor}};
status = sess->Run(feed_dict, {}, {graph_def.saver_def().restore_op_name()}, nullptr);
if (status != tensorflow::Status::OK())
return status;
tensor_dict feed_dict = {
{graph_def.saver_def().filename_tensor_name(), checkpointPathTensor}};
status = sess->Run(feed_dict, {}, {graph_def.saver_def().restore_op_name()},
nullptr);
if (status != tensorflow::Status::OK()) return status;
} else {
// virtual Status Run(const std::vector<std::pair<string, Tensor> >& inputs,
// const std::vector<string>& output_tensor_names,
// const std::vector<string>& target_node_names,
// std::vector<Tensor>* outputs) = 0;
status = sess->Run({}, {}, {"init"}, nullptr);
if (status != tensorflow::Status::OK())
return status;
if (status != tensorflow::Status::OK()) return status;
}

return tensorflow::Status::OK();
}

int main(int argc, char const *argv[]) {

const std::string graph_fn = "./exported/my_model.meta";
const std::string checkpoint_fn = "./exported/my_model";

Expand All @@ -84,15 +85,15 @@ int main(int argc, char const *argv[]) {
data_[1] = 43;

tensor_dict feed_dict = {
{ "input_plhdr", data },
{"input_plhdr", data},
};

std::vector<tensorflow::Tensor> outputs;
TF_CHECK_OK(sess->Run(feed_dict, {"sequential/Output_1/Softmax:0"}, {}, &outputs));
TF_CHECK_OK(
sess->Run(feed_dict, {"sequential/Output_1/Softmax:0"}, {}, &outputs));

std::cout << "input " << data.DebugString() << std::endl;
std::cout << "output " << outputs[0].DebugString() << std::endl;


return 0;
}
53 changes: 28 additions & 25 deletions examples/resize/opencv_version.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Patrick Wieschollek, <mail@patwie.com>

/*
This example loads an image using OpenCV and resizes this image by factor 2 using TensorFlow
before writing it again into a file.
This example loads an image using OpenCV and resizes this image by factor 2
using TensorFlow before writing it again into a file.
*/

#include "tensorflow/cc/client/client_session.h"
Expand All @@ -14,39 +14,43 @@ before writing it again into a file.
#include <opencv2/highgui/highgui.hpp>

int main() {

// read image in OpenCV
std::string fn = "Grace_Hopper.png";
cv::Mat image;
image = cv::imread(fn);
if (!image.data ) {
std::cerr << "Could not open or find the image" << std::endl ;
if (!image.data) {
std::cerr << "Could not open or find the image" << std::endl;
return -1;
}
std::cout << "read image " << fn << " with shape " << image.size() << ", "<< image.channels() << std::endl;
std::cout << "read image " << fn << " with shape " << image.size() << ", "
<< image.channels() << std::endl;

// convert byte to float image
cv::Mat image_float;
image.convertTo(image_float, CV_32FC3);
float *image_float_data = (float*)image_float.data;
float *image_float_data = (float *)image_float.data;

// create input shape
tensorflow::TensorShape image_shape = tensorflow::TensorShape{1, image.rows, image.cols, image.channels()};
std::cout << "Input TensorShape ["
<< image_shape.dim_size(0) << ", "
<< image_shape.dim_size(1) << ", "
<< image_shape.dim_size(2) << ", "
<< image_shape.dim_size(3) << "]" << std::endl;
tensorflow::TensorShape image_shape =
tensorflow::TensorShape{1, image.rows, image.cols, image.channels()};
std::cout << "Input TensorShape [" << image_shape.dim_size(0) << ", "
<< image_shape.dim_size(1) << ", " << image_shape.dim_size(2)
<< ", " << image_shape.dim_size(3) << "]" << std::endl;

// create input tensor
tensorflow::Tensor image_tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, image_shape);
tensorflow::Tensor image_tensor =
tensorflow::Tensor(tensorflow::DT_FLOAT, image_shape);
// copy data from OpenCv to TensorFlow Tensor
std::copy_n((char*) image_float_data, image_shape.num_elements() * sizeof(float),
const_cast<char*>(image_tensor.tensor_data().data()));
std::copy_n((char *)image_float_data,
image_shape.num_elements() * sizeof(float),
const_cast<char *>(image_tensor.tensor_data().data()));

// create graph for resizing images
tensorflow::Scope root = tensorflow::Scope::NewRootScope();
auto resized = tensorflow::ops::ResizeBicubic(root, image_tensor, tensorflow::ops::Const(root.WithOpName("size"), {2 * image.rows, 2 * image.cols}));
auto resized = tensorflow::ops::ResizeBicubic(
root, image_tensor,
tensorflow::ops::Const(root.WithOpName("size"),
{2 * image.rows, 2 * image.cols}));

// run graph and fetch outputs
std::vector<tensorflow::Tensor> outputs;
Expand All @@ -57,21 +61,20 @@ int main() {
tensorflow::Tensor output = outputs[0];
float *result_float_data = output.flat<float>().data();

std::cout << "Output TensorShape ["
<< output.shape().dim_size(0) << ", "
<< output.shape().dim_size(1) << ", "
<< output.shape().dim_size(2) << ", "
<< output.shape().dim_size(3) << "]" << std::endl;
std::cout << "Output TensorShape [" << output.shape().dim_size(0) << ", "
<< output.shape().dim_size(1) << ", " << output.shape().dim_size(2)
<< ", " << output.shape().dim_size(3) << "]" << std::endl;

cv::Mat resized_image;
resized_image.create(output.dim_size(1), output.dim_size(2), CV_32FC3);
std::copy_n((char*) result_float_data , output.shape().num_elements() * sizeof(float), (char*) resized_image.data);
std::copy_n((char *)result_float_data,
output.shape().num_elements() * sizeof(float),
(char *)resized_image.data);

//cv::imwrite("Grace_Hopper_resized.png", resized_image);
// cv::imwrite("Grace_Hopper_resized.png", resized_image);
cv::Mat resized_image_uint8;
resized_image.convertTo(resized_image_uint8, CV_8UC3);
cv::imwrite("Grace_Hopper_resized.png", resized_image_uint8);


return 0;
}
13 changes: 8 additions & 5 deletions examples/resize/tensorflow_version.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// tensorflow/cc/example/example.cc

#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/cc/ops/image_ops.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"

#include <string>
#include <fstream>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <string>

/*
Loads, resizes and saves an image using TensorFlow only.
Expand All @@ -24,8 +24,10 @@ int main() {
auto net2 = tensorflow::ops::DecodePng(root, net1);
auto net3 = tensorflow::ops::Cast(root, net2, tensorflow::DT_FLOAT);
auto net4 = tensorflow::ops::ExpandDims(root, net3, 0);
auto net5 = tensorflow::ops::ResizeBilinear(root, net4, tensorflow::ops::Const(root, {2 * 606, 2 * 517}));
auto net6 = tensorflow::ops::Reshape(root, net5, tensorflow::ops::Const(root, {2 * 606, 2 * 517, 3}));
auto net5 = tensorflow::ops::ResizeBilinear(
root, net4, tensorflow::ops::Const(root, {2 * 606, 2 * 517}));
auto net6 = tensorflow::ops::Reshape(
root, net5, tensorflow::ops::Const(root, {2 * 606, 2 * 517, 3}));
auto net7 = tensorflow::ops::Cast(root, net6, tensorflow::DT_UINT8);
auto net8 = tensorflow::ops::EncodeJpeg(root, net7);

Expand All @@ -34,7 +36,8 @@ int main() {

// Run and fetch v
TF_CHECK_OK(session.Run({net8}, &outputs));
std::ofstream("output.jpg", std::ios::binary) << outputs[0].scalar<std::string>()();
std::ofstream("output.jpg", std::ios::binary)
<< outputs[0].scalar<std::string>()();

return 0;
}
4 changes: 2 additions & 2 deletions examples/simple/example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ int main() {
using namespace tensorflow::ops;
Scope root = Scope::NewRootScope();
// Matrix A = [3 2; -1 0]
auto A = Const(root, { {3.f, 2.f}, { -1.f, 0.f} });
auto A = Const(root, {{3.f, 2.f}, {-1.f, 0.f}});
// Vector b = [3 5]
auto b = Const(root, { {3.f, 5.f} });
auto b = Const(root, {{3.f, 5.f}});
// v = Ab^T
auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
std::vector<Tensor> outputs;
Expand Down

0 comments on commit 6759705

Please sign in to comment.