C/C++でTensorFlowを使用して事前に訓練されたモデル--間接呼び出しPython実装
3784 ワード
現在の深さ学習フレームワークは一般的にPythonに基づいて実現されており,モデルの構築,訓練,保存,呼び出しは容易にPythonで行うことができる.しかし、実際にこれらのモデルを適用する場合、他のプログラミング言語で行う必要がある場合があります.本稿では、C/C++間接呼び出しPythonにより、C/C++プログラムでTensorFlowを呼び出して事前に訓練したモデルを実現します.
1.環境構成 C/C++でPythonを呼び出すには、ヘッダファイルとライブラリのパスを構成する必要があります.ここではCode::Blocksを例に説明します. Build->Project optionsにリンクライブラリlibpython 3を追加する.5m.ヘッドファイルとhの経路は、Pythonバージョンによって自分で状況に応じて調整できます.
2.Pythonモジュールおよび関連関数の初期化とインポートクリックしてPythonでTensorFlowプリトレーニングモデルを呼び出す方法を振り返る PyImport_を通過ImportModule呼び出しが必要なPythonファイルをインポートし、PyObject_を介してGetAttrStringはモジュール内の関数を得て,最後に事前訓練モデルを導入し,パラメータとして新しいメタグループを作成した.
3.CからPythonへのパラメータの構築テストデータを読み込み、リストに入力します.
4.リストをタプルに入力し、パラメータとしてPythonに入力し、戻り値を解析するは、Pythonプログラムにタプルを入力し、predict関数を呼び出して戻り値を求め、解析を行う.
5.いくつかのパラメータと主関数
もっと素晴らしいものを手に入れて、「seniusen」に注目してください!
1.環境構成
2.Pythonモジュールおよび関連関数の初期化とインポート
void Initialize()
{
Py_Initialize();
if ( !Py_IsInitialized() )
{
printf("Initialize failed!");
}
// Path of the python file. python
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('/home/senius/python/c_python/test/')");
const char* modulName = "forward"; // Module name of python file.
pMod = PyImport_ImportModule(modulName);
if(!pMod)
{
printf("Import Module failed!
");
}
const char* funcName = "load_model"; // Function name in the python file.
load_model = PyObject_GetAttrString(pMod, funcName);
if(!load_model)
{
printf("Import load_model Function failed!
");
}
funcName = "predict"; // Function name in the python file.
predict = PyObject_GetAttrString(pMod, funcName);
if(!predict)
{
printf("Import predict Function failed!
");
}
PyEval_CallObject(load_model, NULL); //
pParm = PyTuple_New(1); // , Python
}
3.CからPythonへのパラメータの構築
void Read_data()
{
const char* txtdata_path = "/home/senius/python/c_python/test/04t30t00.npy";
//Path of the TXT file. txt
FILE *fp;
fp = fopen(txtdata_path, "rb");
if(fp == NULL)
{
printf("Unable to open the file!");
}
fread(data, num*SIZE, sizeof(float), fp);
fclose(fp);
// copying the data to the list
int j = 0;
pArgs = PyList_New(num * SIZE); // ,
while(j < num * SIZE)
{
PyList_SET_ITEM(pArgs, j, Py_BuildValue("f", data[j]));
j++;
}
}
4.リストをタプルに入力し、パラメータとしてPythonに入力し、戻り値を解析する
void Test()
{
PyTuple_SetItem(pParm, 0, pArgs);
pRetVal = PyEval_CallObject(predict, pParm);
int list_len = PyList_Size(pRetVal);
PyObject *list_item = NULL;
PyObject *tuple_item = NULL;
for (int i = 0; i < list_len; i++)
{
list_item = PyList_GetItem(pRetVal, i);
tuple_item = PyList_AsTuple(list_item);
PyArg_ParseTuple(tuple_item, "f", &iRetVal[i]);
}
}
5.いくつかのパラメータと主関数
#include
#include
#define SIZE 41*41*41*3
#define NUM 100
PyObject* pMod = NULL;
PyObject* load_model = NULL;
PyObject* predict = NULL;
PyObject* pParm = NULL;
PyObject* pArgs = NULL;
PyObject* pRetVal = NULL;
float iRetVal[NUM*3] = {0};
float data[NUM * SIZE] = {0};
int num = 1; // 100
void Initialize();
void Read_data();
void Test();
int main(int argc, char **argv)
{
Initialize(); //
Read_data(); //
Test(); //
int j = 0;
while(j < num*3)
{
printf("%f
", iRetVal[j]);
j++;
}
printf("Done!
");
Py_Finalize();
return 0;
}
もっと素晴らしいものを手に入れて、「seniusen」に注目してください!