1. 저장하기

import tensorflow.compat.v1 as tf
import numpy as np
import gym
from collections import deque
import random
import os

tf.disable_v2_behavior()


def createFolder(directory):
    try:
        if not os.path.exists(directory):
            os.makedirs(directory)
    except OSError:
        print ('Error: Creating directory. ' +  directory)


sess = tf.Session()


DQNmain = DQNet(sess, input_size, output_size, "DQNMain")
modelSaver= tf.train.Saver(var_list= tf.global_variables(), allow_empty=False)
sess.run( tf.global_variables_initializer())

.....
.....

여러 코드 구현

.....
.....


createFolder('my_test_model')
modelSaver.save(sess, 'my_test_model/mymodel', write_meta_graph = False, global_step=global_step)

 

 

 

2. 불러다 쓰기

sess = tf.Session()

# 네트워크 구성
DQNmain = DQNet(sess, input_size, output_size, "DQNMain")

# modelsaver
modelSaver = tf.train.Saver(var_list = tf.global_variables(), allow_empty=False)

# 저장된 checkpoint 있는지 체크..
# 있으면 load.. 없으면 에러
ckpt = tf.train.get_checkpoint_state('my_test_model/')
if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
    modelSaver.restore(sess, ckpt.model_checkpoint_path)
else:
    print("Model data not found...")
    exit()

 

1. Tensorflow 2.X Ver#1

import gym
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

env = gym.make('FrozenLake-v0')


learning_rate = 0.1
input_size = env.observation_space.n
output_size = env.action_space.n
dis=.99
num_episodes=2000


model = tf.keras.Sequential([
    tf.keras.layers.Dense(output_size, input_shape=[input_size],
                         kernel_initializer=tf.random_uniform_initializer(minval=0, maxval=0.01))
])
model.compile(optimizer=tf.train.GradientDescentOptimizer(learning_rate=learning_rate), loss='mse')
model.summary()


rList=[]
for i in range(num_episodes):
    s = env.reset()
    e = 1.0 / ((i/50)+10)
    rAll = 0
    done = False

    while not done:
        Qs = model.predict(one_hot(s))
        if np.random.rand(1) < e:
            a = env.action_space.sample()
        else:
            a = np.argmax(Qs)

        s1, reward, done, _ = env.step(a)
        if done:
            Qs[0, a] = reward
        else:
            Qs1 = model.predict(one_hot(s1))
            Qs[0, a] = reward + dis*np.max(Qs1)

        model.fit(x=one_hot(s), y=Qs)

        rAll += reward
        s= s1

    rList.append(rAll)

print("Percent of successful episode: "+str(sum(rList)/num_episodes)+"%")
plt.bar(range(len(rList)), rList, color='blue')
plt.show()

2. Tensorflow 2.X Ver2

import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
import gym
import matplotlib.pyplot as plt



def SumSqureError(y_true, y_pred):
    return tf.reduce_sum( tf.square(y_true - y_pred) )


def my_init(shape, dtype=None):
    return np.random.uniform(0, 0.01, shape)



env = gym.make("FrozenLake-v1")
INPUT_ONE_HOT = np.identity(env.observation_space.n)

model = keras.Sequential()
model.add(layers.Dense(env.action_space.n, input_shape=(None, env.observation_space.n), kernel_initializer=my_init))
model.summary()
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.1), loss=SumSqureError, metrics=['accuracy'])


dis = 0.99
episodes = 2000
rList = []


def one_hot(x):
    return INPUT_ONE_HOT[x:x + 1]


rAll = 0

for i in range(episodes):
    state = env.reset()

    e = 1.0 / ((i / 50) + 10)
    Done = False

    while not Done :
        Qs = model.predict(one_hot(state))
        
        if np.random.rand(1) < e:
            action = env.action_space.sample()
        else:
            action = np.argmax( Qs )

        new_state, reward, Done, _ = env.step(action)
        #env.render()


        if Done:
            rList.append(reward)
            rAll += reward
            Qs[0,action] = reward
        else :
            Qs[0,action] = reward + dis * np.max(model.predict(one_hot(new_state)))
 
        
        model.fit(one_hot(state), Qs)
        state = new_state

    if (i % 200 == 0) and (i > 0):
        print("Prgress : ", (i/episodes), "  ==> ", (rAll / i))



print("Success rate: " + str(rAll / episodes))
plt.bar(range(len(rList)), rList, color="blue")
plt.show()

env.close()

 

 

2. Tensorflow 1.X

import tensorflow.compat.v1 as tf
import numpy as np
import gym
import matplotlib.pyplot as plt

tf.disable_v2_behavior()




env = gym.make('FrozenLake-v1')
input_size = env.observation_space.n
output_size = env.action_space.n

episodes = 2000
rAll = 0
rList = []
dis = 0.99
Done = False
ONE_HOT = np.identity(input_size)


X = tf.placeholder(shape=[1, input_size], dtype=tf.float32)
#W = tf.Variable(tf.random_normal([input_size, output_size]))
W = tf.Variable(tf.random_uniform([input_size, output_size], 0, 0.01))
Y = tf.placeholder(shape=[1, output_size], dtype=tf.float32)

Qpred = tf.matmul(X, W)
cost = tf.reduce_sum(tf.square(Y - Qpred))
train = tf.train.GradientDescentOptimizer(learning_rate = 0.1).minimize(cost)

sess = tf.Session();
sess.run(tf.global_variables_initializer())

def one_hot(x):
    return ONE_HOT[x:x + 1]

for i in range(episodes):
    
    state = env.reset()
    Done = False
    #e = 1. / ((i / 50) + 10)
    e =  1. / ((i / 50) + 10)
    
    while not Done :
        Qs = sess.run(Qpred, feed_dict={X: one_hot(state)})
        
        if np.random.rand(1) < e:
            action = env.action_space.sample()
        else:
            action = np.argmax(Qs)
            
        new_state, reward, Done, _ = env.step(action)
        #env.render()
        
        if Done:
            rAll += reward
            rList.append(reward)
            # Update Q, and no Qs+1, since it's a terminal state
            Qs[0, action] = reward
        else:
            # Obtain the Q_s1 values by feeding the new state through our
            # network
            Qs1 = sess.run(Qpred, feed_dict={X: one_hot(new_state)})
            # Update Q
            Qs[0, action] = reward + dis * np.max(Qs1)
            
        sess.run(train, feed_dict={X: one_hot(state), Y: Qs})
        state = new_state
    
    if (i % 200 == 0) and (i > 0):
        print("Prgress : ", (i/episodes), "  ==> ", (rAll / i))

print("Percent of success of episodes : ", (rAll / episodes))
plt.bar(range(len(rList)), rList, color="blue")
plt.show()

env.close()

https://www.youtube.com/watch?v=ZYX0FaqUeN4 

import numpy as np

t = np.array([[[0,1,2], [3,4,5]], [[6,7,8],[9,10,11]]])
print(t)
print("shape=",t.shape)
print("----------------------")
print("reshape#1")
ar = np.reshape(t, (-1,3))
print(ar)
print("shape=",ar.shape)
print("----------------------")
print("reshape#2")
ar = np.reshape(ar, (-1,1,3))
print(ar)
print("shape=",ar.shape)
print("----------------------")
print("squeeze")
ar = np.squeeze([[0],[1],[2],[3]])
print( ar )
print( "shape=",ar.shape )
print("----------------------")
print("expand_dims")
ar = np.expand_dims([0, 1, 2], 1)
print( ar )
print( "shape=",ar.shape )
In [1]: 
[[[ 0  1  2]
  [ 3  4  5]]

 [[ 6  7  8]
  [ 9 10 11]]]
shape= (2, 2, 3)
----------------------
reshape#1
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
shape= (4, 3)
----------------------
reshape#2
[[[ 0  1  2]]

 [[ 3  4  5]]

 [[ 6  7  8]]

 [[ 9 10 11]]]
shape= (4, 1, 3)
----------------------
squeeze
[0 1 2 3]
shape= (4,)
----------------------
expand_dims
[[0]
 [1]
 [2]]
shape= (3, 1)

 

 

 

 

import tensorflow as tf
import numpy as np
import random
import time
import matplotlib.pyplot as plt


sizeof_x = 784
nb_classes = 10
nb_branch = 256
epoch = 15
batch_size = 100
learning_rate=0.001
alg = 0
val_keep_prob = 1.0

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
print( "x_train.shape=", x_train.shape )
print( "x_train.shape=", x_train.shape[0] )

num_iterations = int(x_train.shape[0] / batch_size)

x_train = x_train.reshape(x_train.shape[0], -1).astype('float32') / 255
x_test = x_test.reshape(x_test.shape[0], -1).astype('float32') / 255
print( "x_train.shape=", x_train.shape )
print( "x_test.shape=", x_test.shape )

y_train = tf.keras.utils.to_categorical(y_train, nb_classes)
y_test = tf.keras.utils.to_categorical(y_test, nb_classes)

print( "y_train.shape=", y_train.shape )
print( "y_test.shape=", y_test.shape )


model = tf.keras.Sequential()

model.add(tf.keras.layers.Dense(nb_classes, activation="softmax"))
model.compile(optimizer='adam', loss=tf.keras.losses.categorical_crossentropy, metrics=['accuracy'])

print( "Now training..." )
model.fit(x=x_train, y=y_train, verbose=1, epochs = epoch, batch_size=batch_size)



print( "Now evaluating..." )
result = model.predict(x_test)

v_result = np.argmax(result, 1)
v_y_test= np.argmax(y_test,1)

is_correct = np.equal(v_result, v_y_test).astype('float32')
accuracy = np.mean(is_correct)
print( "accuracy=", accuracy )

for i in range(100):
    r = random.randint(0, x_test.shape[0] - 1)
    image= x_test[r:r+1]
    predict = v_result[r:r+1]
    label = v_y_test[r:r+1]
    if label != predict :
      print("{} {} Label={} Predict={}".format(i, label==predict, label, predict))
      plt.imshow(image.reshape(28, 28),        cmap="Greys",
          interpolation="nearest"
          )
      plt.show()

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

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();
}

 

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

출처: https://seanpaulskin.tistory.com/entry/부스트-라이브러리Boost-Library-VC과-MinGW-컴파일Compile-하기 [:: 스킨 테스트 블로그 :: HTML5 / XHTML / HTML / CSS]

 

 

1. 부스트 라이브러리 최신 버전이나 필요한 버전을 다운 받는다.

- 이 글 작성시 부스트 최신 버전 : 1_58_0 

부스트 공식 홈( http://www.boost.org ) 또는 

SourceForge 다운로드  http://sourceforge.net/projects/boost/files/

 

컴파일을 하지 않고 바로 사용 할려면 컴파일된 x32, 또는 x64 파일을 다운
http://sourceforge.net/projects/boost/files/boost-binaries/1.58.0/

Boost_1_58_0-bin-msvc-all-32-64.7z 1.3GB (x32, x64 비주얼 스튜디오 모든 버전 포함)

 

 

 

Visual Studio 2013 크로스 컴파일러를 설치하면  

여러 가지 커맨드 프롬프트 제공 합니다. ( Itanium 버전은 인텔의 개발 중지 발표로 생략)

 

x64 크로스 Tools 명령 프롬프트에서는 x86환경에서 x64 컴파일 가능.

x86, x64 네이티브에서는 각 각  x32 , x64 컴파일 가능.

 

*** 미리 컴파일된 버전을 사용하지 않고 직접 컴파일해서 사용 하고자 한다면 ***

Boost 라이브러리는 두가지 방법으로 컴파일 할 수 있는데,

1. 헤더 파일과 라이브러리를 생성하는 방법
2. 라이브 러리를 생성 하는 방법

처음 설치 한다면 헤더(Header)와 라이브러리(Library)가 필요 하므로 install 옵션을 쓰고 

추가적으로 라이브러리(Library)가 필요하다면 stage를 쓰면 됩니다.

 

 

부스트 라이브러리 루트 폴더에 boostrap.bat 실행해서 b2.exe와 bjam.exe를 생성 합니다.

2개다 같은 일을 하지만 여기서는 b2.exe으로 설명 합니다.

b2 [options] [properties] [install|stage]

VS 2013 기준(msvc-12.0), VS 2012 : msvc-11.0)

 

--stagedir (라이브러리 파일이 있는 stage 폴더내에 복사

--libdir을 지정 해주면 원하는 폴더로 복사( --libdir="C:\Boost\lib" )

--includedir 위 와 마찬가지( --includedir="C:\Boost\include" )

-j n - CPU 멀티 코어 사용 ( n : 코어 갯수 지정 )

 

x32 라이브러리

/MT, /MTd (멀티 스레드 릴리즈, 멀티 스레드 디버그) : 정적 라이브러리 생성( .LIB )

b2  --stagedir=stage32 --toolset=msvc-12.0 -j 4 runtime-link=static

 

/MD, /MDd (멀티 다이나믹, 멀티 다이나믹 디버그  ): 동적 라이브러리 생성( .DLL )

b2  --stagedir=stage32 --toolset=msvc-12.0 -j 4 runtime-link=shared

 

x64 라이브러리

/MT, /MTd (멀티 스레드 릴리즈, 멀티 스레드 디버그) : 정적 라이브러리 생성

b2  --stagedir=stage64 address-model=64 --toolset=msvc-12.0 -j 4 runtime-link=static

 

/MD, /MDd

b2  --stagedir=stage64 address-model=64 --toolset=msvc-12.0 -j 4 runtime-link=shared

 

컴파일 결과파일 이름(Naming) 규칙

- lib 접두사 - Win32에서 Static 라이브러리에 붙음.

- boost - 접두사 모든 파일 앞에 붙음.

- vc120 - 비주얼 스튜디오 버전별로( 2012 : vc110, 2010 : vc100 )

 

- mt - Shared 멀티 스레드용(multi-threading) 릴리즈 버전 

- mt-gd Shared 방식으로 멀티 스레드용(multi-threading) 디버그 버전

- mt-s Static 방식으로 릴리즈

- mt-sgd  Static multi-threading 디버그 버전

- 1_58 - 부스트 버전.

- .lib - 확장자

 


 

컴파일 예제 :

C:\Boost에 Visual Studio 2013버전 x32, x64 디버그, 릴리즈 

싱글, 멀티스레딩(정적, 동적 라이브러리)를 멀티코어 4개로 컴파일

 

b2  --libdir="C:\Boost\lib" --libinclude="C:\Boost\include"

 --toolset=msvc-12.0 variant=debug,release address-model=32,64 

threading=single,multi -j 4 runtime-link=static,shared

컴파일 결과 : C:\Boost\Lib, C:\Boost\Include 생성 lib 폴더내에 .lib, .dll 파일 생성




import tensorflow.compat.v1 as tf
import numpy as np
import random
import time
import matplotlib.pyplot as plt


tf.disable_v2_behavior()
 

tf.set_random_seed(777)  # for reproducibility

from tensorflow.examples.tutorials.mnist import input_data


# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset
mnist = input_data.read_data_sets("D:/Downloads/", one_hot=True)

sizeof_x = 784
nb_classes = 10
nb_branch = 256
epoch = 15
batch_size = 100
alg = 4
val_keep_prob = 1.0


num_iterations = int(mnist.train.num_examples / batch_size)



x_data = tf.placeholder(tf.float32, [None, sizeof_x])
y_data = tf.placeholder(tf.float32, [None, nb_classes])
keep_prob = tf.placeholder(tf.float32)



if alg == 0 :
    print("Using single network.. softmax + GradientOptimizer")
    W1 = tf.Variable(tf.random_normal([sizeof_x, nb_classes]), name="weight1")
    B1 = tf.Variable(tf.random_normal([nb_classes]), name="bias1")
    logits = tf.matmul(x_data, W1) + B1
    H = tf.nn.softmax(logits)
     
    cost = tf.reduce_mean(-tf.reduce_sum(y_data * tf.log(H), axis=1))
    train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)

if alg == 1 :
    print("Using single network.. softmax + AdamOptimizer1")
    W1 = tf.Variable(tf.random_normal([sizeof_x, nb_classes]), name="weight1")
    B1 = tf.Variable(tf.random_normal([nb_classes]), name="bias1")
    
    logits = tf.matmul(x_data, W1) + B1
    H = tf.nn.softmax(logits)
 
    cost = tf.reduce_mean(-tf.reduce_sum(y_data * tf.log(H), axis=1))
    train = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

if alg == 2 :
    print("Using single network.. softmax + AdamOptimizer2")
    W1 = tf.Variable(tf.random_normal([sizeof_x, nb_classes]), name="weight1")
    B1 = tf.Variable(tf.random_normal([nb_classes]), name="bias1")

    H = tf.matmul(x_data, W1) + B1
    
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=H, labels=y_data))
    train = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)

elif alg == 3:
    print("Using neural network.. relu + AdamOptimizer")
    W1 = tf.Variable(tf.random_normal([sizeof_x, nb_branch]), name="weight1")
    B1 = tf.Variable(tf.random_normal([nb_branch]), name="bias1")
    
    W2 = tf.Variable(tf.random_normal([nb_branch, nb_branch]), name="weight2")
    B2 = tf.Variable(tf.random_normal([nb_branch]), name="bias2")
    
    W3 = tf.Variable(tf.random_normal([nb_branch, nb_classes]), name="weight3")
    B3 = tf.Variable(tf.random_normal([nb_classes]), name="bias3")
    
    L1 = tf.nn.relu(tf.matmul(x_data, W1) + B1)
    L2 = tf.nn.relu(tf.matmul(L1, W2) + B2)
    H = tf.matmul(L2, W3) + B3
    
    # cross entropy
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=H, labels=tf.stop_gradient(y_data)))
    train = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
  

elif alg == 4:
    nb_branch = 512
    val_keep_prob = 0.7
    
    print("Using Deep neural network.. dropout")
    W1 = tf.Variable(tf.random_normal([sizeof_x, nb_branch]), name="weight1")
    B1 = tf.Variable(tf.random_normal([nb_branch]), name="bias1")
    W2 = tf.Variable(tf.random_normal([nb_branch, nb_branch]), name="weight2")
    B2 = tf.Variable(tf.random_normal([nb_branch]), name="bias2")
    W3 = tf.Variable(tf.random_normal([nb_branch, nb_branch]), name="weight3")
    B3 = tf.Variable(tf.random_normal([nb_branch]), name="bias3")
    W4 = tf.Variable(tf.random_normal([nb_branch, nb_branch]), name="weight4")
    B4 = tf.Variable(tf.random_normal([nb_branch]), name="bias4")    
    W5 = tf.Variable(tf.random_normal([nb_branch, nb_classes]), name="weight5")
    B5 = tf.Variable(tf.random_normal([nb_classes]), name="bias5")
    
    L1 = tf.nn.relu(tf.matmul(x_data, W1) + B1)
    L1= tf.nn.dropout(L1, keep_prob=keep_prob)
    L2 = tf.nn.relu(tf.matmul(L1, W2) + B2)
    L2= tf.nn.dropout(L2, keep_prob=keep_prob)
    L3 = tf.nn.relu(tf.matmul(L2, W3) + B3)
    L3= tf.nn.dropout(L3, keep_prob=keep_prob)
    L4 = tf.nn.relu(tf.matmul(L3, W4) + B4)
    L4 = tf.nn.dropout(L4, keep_prob=keep_prob)
    H = tf.matmul(L4, W5) + B5
    
    # cross entropy
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=H, labels=tf.stop_gradient(y_data)))
    train = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
    

is_correct = tf.equal(tf.argmax(y_data, 1), tf.argmax(H, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))


sess = tf.Session()
sess.run(tf.global_variables_initializer())

for e in range(epoch):
    avg_cost = 0
    for it in range(num_iterations):
        x_batch, y_batch = mnist.train.next_batch(batch_size)
        val_cost, acc, _ = sess.run([cost, accuracy, train], feed_dict={x_data:x_batch, y_data:y_batch, keep_prob:val_keep_prob})
        avg_cost = avg_cost + val_cost / num_iterations
        
    print("Epoch: {:04d}, Cost: {:.9f}".format(e + 1, avg_cost))
    
print("training finished")


val_keep_prob = 1.0
print("Accuracy: ", accuracy.eval(session=sess, feed_dict={x_data: mnist.test.images, y_data: mnist.test.labels, keep_prob:val_keep_prob}))

for i in range(1):
    r = random.randint(0, mnist.test.num_examples - 1)
    label= sess.run( tf.argmax( mnist.test.labels[r:r+1], 1 ))
    image= mnist.test.images[r:r+1]
    predict= sess.run(tf.argmax(H, 1), feed_dict={x_data: image, keep_prob:val_keep_prob})
    print("{} {} Label={} Predict={}".format(i, label==predict, label, predict))
    plt.imshow(image.reshape(28, 28),        cmap="Greys",
        interpolation="nearest"
        )
    plt.show()
    time.sleep(3)

+ Recent posts