여기서 스크랩 해 왔습니다.

https://curiousseed.tistory.com/40

 

C#에서 tensorflow 사용하는 방법 알아보기

(*아래 사이트를 참고하여 작성하였습니다: (1) http://euhyeji.blogspot.com/2018/08/tensorflowsharp01.html (2) https://github.com/migueldeicaza/TensorFlowSharp) C#에서 Tensorflow로 만든 모델을 활용할..

curiousseed.tistory.com

 

C#에서 Tensorflow로 만든 모델을 활용할 수 있는 방법은 아래와 같습니다.

Tensorflow에서 제공하는 TensorFlowsharp(https://github.com/migueldeicaza/TensorFlowSharp )을 사용하면 C#에서 tensorflow를 사용할 수 있습니다.

Tensorflow, Keras로 학습시킨 모델을 .NET에 로드시켜 학습이나 참조를 시키는 응용 프로그램을 만들 수 있는 API TensorFlowSharp에서 제공합니다.

TensorFlowSharp C#에서 설치하는 방법은 아래와 같습니다.

 

        <TensorFlowSharp C#에서 설치하는 방법>

        1.        프로젝트 파일을 생성할  .NET Framework 4.6.1 이상으로 설정합니다.
        2.       [project]  [Manage NuGet Packages]  Browse에서 search Tensorflowsharp을 입력합니다. → 
                 TensorFlowSharp을 선택하여 현재 프로젝트에 설치합니다.
        3.       Solution explorer에서 References 보면 TensorFlowSharp 설치되어 있는 것을 확인할  있습니다.

        (참고 사이트:  http://euhyeji.blogspot.com/2018/08/visualstudio2017.html )



 

TensorFlowSharp에서는 두 가지 방식을 지원합니다.

(1) 이미 Tensorflow에서 만들어져 있는 모델을 가져오는 것입니다.

(2) TensorFlow의 문법을 C#에서 사용하는 방식입니다. 아래는 (1), (2)에 대한 각각의 설명입니다.

 

 

1.    이미 Tensorflow에서 만들어져 있는 모델을 가져오는 방식 
       Python에서 TensorFlow 또는 Keras 사용하여 프로토 타입을 생성한 다음 학습된 모델을 저장  다음 TensorFlowSharp 사용하여 .NET 결과를 로드할  있다. 그리고나서 가져온 모델을 가지고 C#에서 자체적으로 데이터를 학습 또는 피드할  있습니다.

 

<TensorFlowSharp Github에 있는 예제 코드>

(참고사이트: https://github.com/migueldeicaza/TensorFlowSharp)  

 

 

2.    TensorFlow의 문법을 C#에서 사용하는 방식 

        : 핵심은 TFSession()을 객체로 선언하고 C#에서 API를 사용하는 방식으로 TensorFlow 문법을 C#에서 사용하는 것입니다. 이것보다는 1번 방식으로 프로젝트를 진행해야 하지않을까 싶습니다.

 

<TensorFlowSharp Github에 있는 예제 코드>

(참고사이트: https://github.com/migueldeicaza/TensorFlowSharp)  

본 게시글은 "홍정모"님의 블로그 게시글을 참고해서 제작하였습니다.

(blog.naver.com/atelierjpro/220965203959)

 

Tensorflow를 C++에서 돌리고 싶을 때를 위해 미리 테스트를 했다.

Windows10 + Visual Studio 2017 (VC14.1)로 테스트를 했다.

Python 3.7 + tensorflow 2.2 버전이다.

boost는 1.73.0 버전에 python numpy 포함해서 직접 컴파일 하였다.

 

 

// ConsoleApplication1.cpp : 이 파일에는 'main' 함수가 포함됩니다. 거기서 프로그램 실행이 시작되고 종료됩니다.
//
#define BOOST_PYTHON_STATIC_LIB
#define BOOST_LIB_NAME "boost_numpy3"
#include <boost/config/auto_link.hpp>
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/python/extract.hpp>


namespace py = boost::python;
namespace np = boost::python::numpy;

using namespace boost::python;
using namespace boost::python::numpy;

#define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )


class MyTest
{
public:
	typedef boost::thread THREAD, *pTHREAD;

	int a_;

	MyTest(int a)
		: a_(a)
	{
	}

	int get()
	{
		return a_;
	}

	void set(int _a)
	{
		a_ = _a;
	}

	void threadjob(int tid)
	{
		std::cout << "Thread working! " << tid << std::endl;
	}

	void threadingtest()
	{
		const int num_threads_ = 10;
		pTHREAD *thread_list_;
		thread_list_ = new pTHREAD[num_threads_];
		for (int i = 0; i < num_threads_; i++) thread_list_[i] = 0;

		for (unsigned int thread_id = 0; thread_id < num_threads_; thread_id++)
			thread_list_[thread_id] = new THREAD(&MyTest::threadjob, this, thread_id);

		for (int i = 0; i < num_threads_; i++) thread_list_[i]->join();
	}

	void count()
	{
		a_++;

		std::cout << "A count " << std::endl;
	}

	void getArray(np::ndarray& np_array)
	{
		std::cout << "get array c++" << std::endl;

		double* ddd = (double*)np_array.get_data();

		std::cout << ddd[0] << std::endl;
		std::cout << ddd[1] << std::endl;
		std::cout << ddd[2] << std::endl;
	}

	void setArray(boost::python::numpy::ndarray data) {
		// Access a built-in type (an array)
		boost::python::numpy::ndarray a = data;
		// Need to <extract> array elements because their type is unknown
		std::cout << "First array item: " << extract<int>(a[0]) << std::endl;
	}

	void setPointer(double* ptr)
	{
		std::cout << "pointer " << ptr << std::endl;
	}

	void set_first_element(numpy::ndarray& y, float value)
	{
		y[0] = value;
	}

	void greet(boost::python::object& obj)
	{
		std::cout << "Greet C++" << std::endl;

		PyObject* pobj = obj.ptr();

		static Py_buffer pybuf;

		if (PyObject_GetBuffer(pobj, &pybuf, PyBUF_SIMPLE) != -1)
		{
			void *buf = pybuf.buf;
			double *p = (double*)buf;

			*p += 4.123;
			*(p + 1) += 5.12312;

			//PyBuffer_Release(&pybuf);
		}
	}

	void plusOne(np::ndarray& np_array)
	{
		std::cout << "get array c++" << std::endl;
		double* ddd = (double*)np_array.get_data();
		ddd[0] += 1.0;
	}
};


BOOST_PYTHON_MODULE(my_test)
{
	class_<MyTest>("MyTest", init<int>())
		.def("get", &MyTest::get)
		.def("threadingtest", &MyTest::threadingtest)
		.def("count", &MyTest::count)
		.def("setArray", &MyTest::setArray)
		.def("setPointer", &MyTest::setPointer)
		.def("set_first_element", &MyTest::set_first_element)
		.def("greet", &MyTest::greet)
		.def("getArray", &MyTest::getArray)
		.def("plusOne", &MyTest::plusOne)
		.add_property("value", &MyTest::get, &MyTest::set);
}



static const char * pycode = "import my_test\n"
"import numpy as np\n"
"import tensorflow.compat.v1 as tf\n"
"tf.disable_v2_behavior()\n"
"mt = my_test.MyTest(123)\n"
"mt.count()\n"
"print(mt.get())\n"
"\n"
"print(mt.value)\n"
"\n"
"mt.value = 12345\n"
"\n"
"print(mt.value)\n"
"\n"
"mt.threadingtest()\n"
"\n"
"b = np.array([10.0, 20.0, 30.0], dtype = np.float)\n"
"print(b)\n"
"mt.greet(b)\n"
"print(b)\n"
"mt.greet(b)\n"
"print(b)\n"
"mt.getArray(b)\n"
"#mt.setArray(b)\n"
"#my_arr = array('f', [1, 2, 3, 4, 5])\n"
"#mt.set_first_element(b.array, 1123.0)\n"
"#mt.setPointer(b.data)\n"
"\n"
"input1 = tf.placeholder(tf.float32)\n"
"input2 = tf.placeholder(tf.float32)\n"
"output = tf.multiply(input1, input2)\n"
"\n"
"x_input = np.array([2], dtype = np.float)\n"
"y_input = np.array([3], dtype = np.float)\n"
"\n"
"with tf.Session() as sess :\n"
"	print(\"Hello !\")\n"
"	print(sess.run([output], feed_dict = { input1:x_input, input2 : y_input }))\n"
"\n"
"mt.plusOne(x_input)\n"
"mt.plusOne(y_input)\n"
"\n"
"print(x_input)\n"
"print(y_input)\n"
"\n"
"with tf.Session() as sess :\n"
"	print(\"Hello !\")\n"
"	print(sess.run([output], feed_dict = { input1:x_input, input2 : y_input }))";
















void exec_test()
{
	// Retrieve the main module
	py::object main = py::import("__main__");

	// Retrieve the main module's namespace
	py::object global(main.attr("__dict__"));

	py::object result = py::exec(pycode,
		global, global);

	py::object print = py::import("__main__").attr("__builtins__").attr("print");
	print(result);
}




int main()
{
	using namespace std;
	//MyTest my_test(0);

	// 본인의 상황에 맞게 설정한다.
	// 나는 아나콘다 가상환경을 만들지 않았다.
	Py_SetPythonHome(L"C:\\ProgramData\\Anaconda3");

	// my_test 핸들러들을 파이썬에 등록한다.
	// Py_Initialize() 함수보다 먼저 와야한다.
	if (PyImport_AppendInittab("my_test", &PyInit_my_test) == -1)
		throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
			"builtin modules");

	Py_Initialize();
	np::initialize();

	if (py::handle_exception(exec_test))
	{
		if (PyErr_Occurred())
		{
			printf("Python Error detected");
			PyErr_Print();
		}
		else
		{
			printf("A C++ exception was thrown  for which "
				"there was no exception translator registered.");
		}
	}

	return 0;
}

 

 

 

위의 소스를 실행하면 아래와 같은 결과를 보게 된다.

 

Windows10 + Visual Studio 2017 (VC14.1)로 테스트를 했다.

Python 3.7 + tensorflow 2.2 버전이다.

boost는 1.73.0 버전에 python numpy 포함해서 직접 컴파일 하였다.

 

파이썬에서 C++ 코드를 실행하는 2가지 방법이다.

1. C++에서 Base 클래스를 만들고 이를 파이썬에서 상속받아 클래스를 구현할때 C++에서 코드가 실행되게 하는 방법.

2. C++ 클래스에서 함수들을 만들고 파이썬에서 이를 실행하는 방법.

 

본인은 Windows10에 아나콘다 가상환경을 설정하지 않았다.

그래서 아나콘다 파이썬 기본 경로가 "C:\\ProgramData\\Anaconda3" 이다.

혹시 아나콘다 가상환경을 구성한 경우 각자 상황에 맞게 아나콘다 경로를 변경한다.

 

// Copyright Stefan Seefeld 2005.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Boost 기본 예제 boost_1_73_0\libs\python\example\quickstart의 코드.
// 컴파일해서 실행하는 경우 제대로 동작하지 않아 직접수정하여 나중에 쓸려고 올려놓음.

#include <boost/python.hpp>

#include <boost/detail/lightweight_test.hpp>
#include <iostream>

namespace python = boost::python;

// An abstract base class
class Base : public boost::noncopyable
{
public:
  virtual ~Base() {};
  virtual std::string hello() = 0;
};

// C++ derived class
class CppDerived : public Base
{
public:
  virtual ~CppDerived() {}
  virtual std::string hello()
  { 
	  return "Hello from C++!";
  }
};

// Familiar Boost.Python wrapper class for Base
struct BaseWrap : Base, python::wrapper<Base>
{
  virtual std::string hello() 
  {
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
    // workaround for VC++ 6.x or 7.0, see
    // http://boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_virtual_functions
    return python::call<std::string>(this->get_override("hello").ptr());
#else
    return this->get_override("hello")();
#endif
  }
};


// Boost 기본 예제 boost_1_73_0\libs\python\example\quickstart의 코드
namespace { // Avoid cluttering the global namespace.

  // A friendly class.
	class hello
	{
	public:
		hello(const std::string& country) { this->country = country; }
		std::string greet() const { return "Hello from " + country; }
	private:
		std::string country;
	};

	// A function taking a hello object as an argument.
	std::string invite(const hello& w) {
		return w.greet() + "! Please come soon!";
	}
}


// Pack the Base class wrapper into a module
BOOST_PYTHON_MODULE(embedded_hello)
{
  python::class_<BaseWrap, boost::noncopyable> base("Base");
}


BOOST_PYTHON_MODULE(extending)
{
	using namespace boost::python;
	class_<hello>("hello", init<std::string>())
		// Add a regular member function.
		.def("greet", &hello::greet)
		// Add invite() as a member of hello!
		.def("invite", invite)
		;

	// Also add invite() as a regular function to the module.
	def("invite", invite);
}



void exec_test1()
{
	// Retrieve the main module
	python::object main = python::import("__main__");

	// Retrieve the main module's namespace
	python::object global(main.attr("__dict__"));

	// Define the derived class in Python.
	python::object result = python::exec(
		"from embedded_hello import *        \n"
		"class PythonDerived(Base):          \n"
		"    def hello(self):                \n"
		"        return 'Hello from Python!' \n",
		global, global);

	python::object PythonDerived = global["PythonDerived"];

	// Creating and using instances of the C++ class is as easy as always.
	CppDerived cpp;
	BOOST_TEST(cpp.hello() == "Hello from C++!");

	std::cout << "testing derived class from C++..." << std::endl;

	// But now creating and using instances of the Python class is almost
	// as easy!
	python::object py_base = PythonDerived();
	Base& py = python::extract<Base&>(py_base) BOOST_EXTRACT_WORKAROUND;

	// Make sure the right 'hello' method is called.
	BOOST_TEST(py.hello() == "Hello from Python!");

	std::cout << "success!" << std::endl;
}


void exec_test2()
{
	// Retrieve the main module
	python::object main = python::import("__main__");

	// Retrieve the main module's namespace
	python::object global(main.attr("__dict__"));

	python::object result = python::exec(
		"from extending import *        \n"
		"hi = hello('California')         \n"
		"hi.greet() \n",
		global, global);

	python::object print = python::import("__main__").attr("__builtins__").attr("print");
	print(result);
}

void exec_test_error()
{
    std::cout << "intentionally causing a python exception..." << std::endl;
    
    // Execute a statement that raises a python exception.
    python::dict global;
    python::object result = python::exec("print unknown \n", global, global);

	python::object print = python::import("__main__").attr("__builtins__").attr("print");
	print(result);
}

int main(int argc, char **argv)
{
	BOOST_TEST(argc == 2);
	std::string script = argv[1];


	// Register the module with the interpreter
	if (PyImport_AppendInittab("embedded_hello", &PyInit_embedded_hello) == -1)
		throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
			"builtin modules");

	if (PyImport_AppendInittab("extending", &PyInit_extending) == -1)
		throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
			"builtin modules");


	// Initialize the interpreter
	Py_SetPythonHome(L"C:\\ProgramData\\Anaconda3");
	Py_Initialize();

	bool error_expected = false;

	if (
		python::handle_exception(exec_test1)
		|| python::handle_exception(exec_test2)
		)
	{
		if (PyErr_Occurred())
		{
			if (!error_expected)
				BOOST_ERROR("Python Error detected");
			PyErr_Print();
		}
		else
		{
			BOOST_ERROR("A C++ exception was thrown  for which "
				"there was no exception translator registered.");
		}
	}

	// Boost.Python doesn't support Py_Finalize yet, so don't call it!
	return boost::report_errors();
}

 

위의 코드를 실행하면 아래와 같은 결과가 나타난다.

+ Recent posts