python&C++python tensorflowモデルパラメータを読み込み、txtファイルに書き込み、C++txtファイルを読み込む
5616 ワード
発見はC++で読む.matファイルにはmatlabの依存が必要で、本機はmatlabをインストールしていないので、最も素朴な方法でデータコアの読み込みデータを格納するしかありません.
1.tensorflowのパラメータをtxtファイルに格納する
保存したモデルは前のブログと一致していますが、今回はtxtに変更しました.numpy独自の書き込みファイル関数は1次元配列と2次元配列の書き込みをサポートしているが、ボリュームコアはこれが4次元でshapeは[ボリュームコア高、ボリュームコア幅、入力チャネル数、出力チャネル数]であり、ここでshapeを[出力チャネル数、入力チャネル数、ボリュームコア高、ボリュームコア幅]に変換して格納し、データを1000倍に拡大して整数に格納する.
コードは次のとおりです.
2.C++txtのデータを読み込む
上記のプロセスの逆プロセスは、すべて行ごとに格納されているので、考え方は簡単で、コードをつけます.
1.tensorflowのパラメータをtxtファイルに格納する
保存したモデルは前のブログと一致していますが、今回はtxtに変更しました.numpy独自の書き込みファイル関数は1次元配列と2次元配列の書き込みをサポートしているが、ボリュームコアはこれが4次元でshapeは[ボリュームコア高、ボリュームコア幅、入力チャネル数、出力チャネル数]であり、ここでshapeを[出力チャネル数、入力チャネル数、ボリュームコア高、ボリュームコア幅]に変換して格納し、データを1000倍に拡大して整数に格納する.
コードは次のとおりです.
import numpy as np
import tensorflow as tf
def store_4d_array(kernel, filename):
# store the kernel
f = open(filename, 'w+')
shape = kernel.shape
num_out_channel = shape[3]
num_in_channel = shape[2]
num_width = shape[0]
f.write(str(num_out_channel) + ',' + str(num_in_channel) + ',' + str(num_width) + ',' + str(num_width) + '
')
for index_out_channel in range(num_out_channel):
for index_in_channel in range(num_in_channel):
for index_row in range(num_width):
for index_col in range(num_width):
f.write(str(int(kernel[index_row][index_col][index_in_channel][index_out_channel] * 1000)))
if index_col == num_width - 1:
f.write('
')
else:
f.write(',')
f.close()
def store_1d_2d_array(bias, filename):
# store the bias
bias = bias * 1000
bias = bias.astype(int)
np.savetxt(filename, bias, delimiter=',', fmt="%d")
if __name__ == "__main__":
with tf.Session() as sess:
# load the meta graph and weights
saver = tf.train.import_meta_graph('model_2\minist.ckpt-70.meta')
saver.restore(sess, tf.train.latest_checkpoint('model_2/'))
# get weighs
graph = tf.get_default_graph()
conv1_w = sess.run(graph.get_tensor_by_name('conv1/w:0'))
np.save("conv1_w", conv1_w)
store_4d_array(conv1_w, "weights/conv1_w.txt")
conv1_b = sess.run(graph.get_tensor_by_name('conv1/b:0'))
store_1d_2d_array(conv1_b, "weights/conv1_b.txt")
conv2_w = sess.run(graph.get_tensor_by_name('conv2/w:0'))
store_4d_array(conv2_w, "weights/conv2_w.txt")
conv2_b = sess.run(graph.get_tensor_by_name('conv2/b:0'))
store_1d_2d_array(conv2_b, "weights/conv2_b.txt")
fc1_w = sess.run(graph.get_tensor_by_name('fc1/w:0'))
store_1d_2d_array(fc1_w, "weights/fc1_w.txt")
fc1_b = sess.run(graph.get_tensor_by_name('fc1/b:0'))
store_1d_2d_array(fc1_b, "weights/fc1_b.txt")
fc2_w = sess.run(graph.get_tensor_by_name('fc2/w:0'))
store_1d_2d_array(fc2_w, "weights/fc2_w.txt")
fc2_b = sess.run(graph.get_tensor_by_name('fc2/b:0'))
store_1d_2d_array(fc2_b, "weights/fc2_b.txt")
2.C++txtのデータを読み込む
上記のプロセスの逆プロセスは、すべて行ごとに格納されているので、考え方は簡単で、コードをつけます.
#include
#include
#include
#include
#include
using namespace std;
void read_4d_array(string filename, vector>>>& kernel)
{
//
//
ifstream infile(filename, ios::in);
string line;
getline(infile, line);
cout << " :" << line << endl;
//
stringstream ss_line(line);
string str;
vector line_array;
// ,
while (getline(ss_line, str, ','))
{
stringstream str_temp(str);
int int_temp;
str_temp >> int_temp;
line_array.push_back(int_temp);
}
int num_out_channel = line_array[0];
int num_in_channel = line_array[1];
int num_width = line_array[2];
// ,
for (int index_out_channel = 0; index_out_channel < num_out_channel; index_out_channel++)
{
// in_channel
vector>> one_in_kernel;
for (int index_in_channel = 0; index_in_channel < num_in_channel; index_in_channel++)
{
//
vector> one_kernel;
for (int index_row = 0; index_row < num_width; index_row++)
{
getline(infile, line);
stringstream tmp_line(line);
//
vector tmp_int_line;
while (getline(tmp_line, str, ','))
{
stringstream str_temp(str);
int int_temp;
str_temp >> int_temp;
tmp_int_line.push_back(int_temp);
}
one_kernel.push_back(tmp_int_line);
}
one_in_kernel.push_back(one_kernel);
}
kernel.push_back(one_in_kernel);
}
}
void read_1d_array(string filename, vector& bias)
{
// ,
//
ifstream infile(filename, ios::in);
string line;
while (getline(infile, line))
{
stringstream tmp_line(line);
int int_tmp;
tmp_line >> int_tmp;
bias.push_back(int_tmp);
}
}
void read_2d_array(string filename, vector>& weights)
{
// , 、
ifstream infile(filename, ios::in);
string line;
while (getline(infile, line))
{
stringstream ss_line(line);
string str;
vector line_array;
// ,
while (getline(ss_line, str, ','))
{
stringstream str_temp(str);
int int_temp;
str_temp >> int_temp;
line_array.push_back(int_temp);
}
weights.push_back(line_array);
}
}