How to convert a serialized tensorflow::Example into an input to a session - tensorflow

I've used the following tutorial to export a saved model created by an estimator:
https://github.com/MtDersvan/tf_playground/blob/master/wide_and_deep_tutorial/wide_and_deep_basic_serving.md
I'm trying to load this model in c++. I've managed to create a serialized tensorflow::Example using c++. How do I convert this into a single Tensor?
The tutorial uses tf.contrib.util.make_tensor_proto(serialized, shape=[1])). What is the equivalent C++ API?

It's complicated a little than write in python, here's code example may helps:
#include <gtest/gtest.h>
#include <grpc/grpc.h>
#include <grpc++/channel.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/security/credentials.h>
#include <grpc++/security/credentials.h>
#include <iostream>
#include "tensorflow/core/framework/tensor.pb.h"
#include "tensorflow/core/example/example.pb.h"
#include "tensorflow/core/framework/tensor_shape.pb.h"
#include "tensorflow/core/framework/types.pb.h"
#include "tensorflow_serving/apis/prediction_service.grpc.pb.h"
#include "tensorflow_serving/apis/prediction_service.pb.h"
using namespace std;
namespace tensorflow {
namespace serving {
TEST(GRPCCppClient, TestPredict) {
shared_ptr<grpc::Channel> channel = grpc::CreateChannel("127.0.0.1:30355", grpc::InsecureChannelCredentials());
shared_ptr<PredictionService::Stub> stub_ = PredictionService::NewStub(channel);
tensorflow::TensorProto tensorProto;
tensorflow::TensorShapeProto tensorShapeProto;
PredictRequest predictRequest;
PredictResponse predictResponse;
tensorflow::TensorShapeProto_Dim* dim_0 = tensorShapeProto.add_dim();
dim_0->set_size(1);
tensorProto.mutable_tensor_shape()->CopyFrom(tensorShapeProto);
Example example;
/*Construct example..*/
std::string test_str;
example.SerializeToString(&test_str);
tensorProto.set_dtype(DT_STRING);
tensorProto.add_string_val(test_str);
(*predictRequest.mutable_inputs())["inputs"] = tensorProto;
predictRequest.mutable_model_spec()->set_name("default"); /*set your own model name*/
grpc::ClientContext context;
grpc::Status status = stub_->Predict(&context, predictRequest, &predictResponse);
if (!status.ok()) {
std::cout << "GetFeature rpc failed." << std::endl;
ASSERT_TRUE(false);
}
cout<<"------------\n"<<predictResponse.DebugString()<<endl;
}
}
}

Related

g++ file.cpp -o file -l wolfssl:undefined reference to 'sp_init' collect2: error :ld returned 1 exit status

#include <iostream>
#include <string>
#include <unistd.h>
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/ssl.h>
#include <wolfssl/wolfcrypt/types.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/sp_int.h>
#include <wolfssl/wolfcrypt/integer.h>
#include <wolfssl/wolfcrypt/wolfmath.h>
using namespace std;
int main(){
ecc_key key;
WC_RNG rng;
wc_ecc_init(&key);
wc_InitGng(&rng);
int curveId = ECC_SECP521R1;
const ecc_set_type* ecc_params;
ecc_params = wc_ecc_get_curve_params(curveId);
mp_int ord; //order of ecc
mp_int priv; //privatekey
int err;
err = mp_init(&ord);
cout<<err<<endl;
err = mp_init(&priv);
cout<<err<<endl;
//err = mp_read_radix(&ord,ecc_params->order,MP_RADIX_HEX); //
//cout<<err<<endl;
//err = wc_ecc_gen_k(&rng,120,&priv,&ord)
return 0 ;
}
enter image description here
i have include <wolfssl/wolfcrypt/sp_int.h>,but it told me undefined reference to 'sp_init',one solution maybe work according tohttps://github.com/wolfSSL/wolfssl/pull/5328,but i don't quite understand .how to solve this problem

why CGAL mesh simplification takes so long

I try to use triangulated surface mesh simplification of cgal. I used Garland&Heckbert Simplification to simplify my mesh, but it tooks so long. My data has 50000 nv, 165883 ne and 109521 nf, which needs almost 30mins with 0.2 stop_ratio. Is the runtime cost reasonable?
I also check whether the input mesh is valid by using CGAL::is_valid_polygon_mesh. It is valid.
how can i solve this problem?
Thank you so much...
the runtime cost of mesh with 165883 edges
the runtime cost of mesh with 8282 edges
enter code here
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
#include "gltf-loader.h"
#define TINYGLTF_NO_STB_IMAGE_WRITE
#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "tiny_gltf.h"
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/draw_surface_mesh.h>
#include <CGAL/draw_polygon_2.h>
#include <CGAL/Polygon_mesh_processing/repair.h>
#include <CGAL/Polygon_mesh_processing/IO/polygon_mesh_io.h>
#include <CGAL/boost/graph/iterator.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Surface_mesh_simplification/edge_collapse.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/Count_ratio_stop_predicate.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/Bounded_normal_change_placement.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/GarlandHeckbert_policies.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Polygon_mesh_processing/stitch_borders.h>
#include <chrono>
#include <vector>
using namespace example;
namespace PMP = CGAL::Polygon_mesh_processing;
namespace NP = CGAL::parameters;
//typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Simple_cartesian<double> K;
typedef CGAL::Surface_mesh<K::Point_3> CGAL_Mesh;
typedef CGAL_Mesh::Vertex_index vertex_descriptor;
typedef CGAL_Mesh::Face_index face_descriptor;
namespace SMS = CGAL::Surface_mesh_simplification;
CGAL_Mesh inital_mesh;
const char* filename = "C:\\Users\\Administrator\\source\\repos\\CMakeProject4\\CMakeProject4\\input.off";
if (!PMP::IO::read_polygon_mesh(filename, inital_mesh) || CGAL::is_empty(inital_mesh))
{
std::cerr << "Invalid input." << std::endl;
return 1;
}
std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
double stop_ratio = 0.2;
std::cout << "start edge collages." << std::endl;
SMS::Count_ratio_stop_predicate<CGAL_Mesh> stop(stop_ratio);
typedef typename SMS::GarlandHeckbert_policies<CGAL_Mesh, K> GH_policies;
typedef typename GH_policies::Get_cost GH_cost;
typedef typename GH_policies::Get_placement GH_placement;
typedef SMS::Bounded_normal_change_placement<GH_placement> Bounded_GH_placement;
bool check_mesh = CGAL::is_valid_polygon_mesh(inital_mesh);
std::cout << "vaild or in valid" <<check_mesh<< std::endl;
GH_policies gh_policies(inital_mesh);
const GH_cost& gh_cost = gh_policies.get_cost();
const GH_placement& gh_placement = gh_policies.get_placement();
Bounded_GH_placement placement(gh_placement);
std::cout << "Input mesh has " << num_vertices(inital_mesh) << " nv "<< num_edges(inital_mesh) << " ne "
<< num_faces(inital_mesh) << " nf" << std::endl;
/*internal::cgal_enable_sms_trace = true;*/
int r = SMS::edge_collapse(inital_mesh, stop,
CGAL::parameters::get_cost(gh_cost)
.get_placement(placement));

g++ Static analysis: false positive with -fanalyzer?

Running this very little snippet, to show a problem I have with a much larger code:
// Type your code here, or load an example.
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
int main() {
auto res = make_unique<int>();
auto ptr = res.get();
if (ptr) {
*ptr = 5;
cout << *ptr << endl;
}
return 0;
}
with the -fanalyzer switch, I get a warning
warning: dereference of possibly-NULL 'operator new(4)' [CWE-690] [-Wanalyzer-possible-null-dereference]
But clearly I made all I could do to avoid this warning, but it is buried in the STL, which returns a unique_ptr with no validity control..
I understand the word "possibly" though..
Anyway to correct this on my side?
Update:
I made a mistake in the first go, now corrected
Update 2:
Even that code is refused
// Type your code here, or load an example.
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
int main() {
auto i = new int(3);
if (!i) {
return 1;
}
unique_ptr<int> res(i);
auto ptr = res.get();
if (!ptr) {
return 1;
}
*ptr = 5;
cout << *ptr << endl;
return 0;
}
Please, see here
As for now (gcc-12), the analyzer is not recommended for C++ code although work is underway to support it.
https://developers.redhat.com/articles/2022/04/12/state-static-analysis-gcc-12-compiler#toward_support_for_c__

C++ C2676 error when using "+" to appened 2 strings

Error C2676 binary '+': 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
I am getting the C2676 error when trying to append two strings. I have tried everything I can think of, but no luck.
The include and using code is as follows.
#pragma once
#include <string>
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <tchar.h>
#include <stream>
#include <cstdlib>
#include <conio.h>
//#include <winnt.h>
//#include <WinUser.h>
using namespace std;
using std::string;
using std::wstring;
namespace CppCLRWinformsProjekt {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;
The code that is giving me trouble is.
private: System::Void bntSelected_Click(System::Object^ sender, System::EventArgs^ e) {
int numSelected = this->checkedListBox1->CheckedItems->Count;
if (numSelected != 0)
{
// Set input focus to the list box.
//SetFocus(lstReceiver);
string s = "";
// If so, loop through all checked items and print results.
for (int x = 0; x < numSelected; x++)
{
// it appears that the first "+" sign is what is giving me trouble
s = s + lstReceiver + (x + 1).ToString() + " = " + this->checkedListBox1->CheckedItems[x]->ToString() + "\n";
}
this.lstReceiver.Items.Add(s);
}

Converting Surface_mesh to Nef_polyhedron_3

I'm trying to use CGAL to do some boolean operations on meshes.
How do I convert from Surface_mesh to Nef_polyhedron_3?
EDIT:
I've tried with this code, but I don't know how to continue...
#include <iostream>
#include <CGAL/Nef_polyhedron_3.h>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
using namespace std;
typedef CGAL::Simple_cartesian<double> K;
typedef CGAL::Surface_mesh<K::Point_3> Mesh;
int main()
{
Mesh m;
auto a = m.add_vertex(K::Point_3(0,0,0));
auto b = m.add_vertex(K::Point_3(0,0,0));
auto c = m.add_vertex(K::Point_3(0,0,0));
m.add_face(a,b,c);
Mesh::Halfedge_range range = m.halfedges();
for(Mesh::Halfedge_index hei : range)
{
// ??? <<--
std::cout << hei << std::endl;
}
return 0;
}
Thanks
I think the suggested way to do this is to use the 3d polyhedral surface package instead. The Nef 3 documentation describes the conversion between Polyhedron_3 and Nef_3. The only difference between the 3d polyhedral surface package and the surface mesh package is that, it is pointer based rather than index based.