tensor score 10
2018/11/26 00:54 投稿
Tensorflow でのプロトコルバッファの出力 |
aaaaaaaaaa |
aaaaaaaaaa |
aaaaaaaaaa |
aaaaaaaaaa |
tensor score 10
2018/11/26 00:53 投稿
Tensorflow でのプロトコルバッファの出力 |
### 前提・実現したいこと |
Tensorflowで学習させたパラメータをチェックポイントで保存したので、 |
チェックポイントをrestoreした推論だけを行うグラフをプロトコルバッファ形式で出力したいです。 |
### 発生している問題・エラーメッセージ |
``` |
AssertionError: output is not in graph |
``` |
### 該当のソースコード |
```python |
#cnn2_save で保存したcheckpoint形式で保存された学習させたパラメータ(変数)を読み込み推論だけのグラフを作成 |
#pb形式で保存 |
import tensorflow as tf |
import time |
import numpy as np |
import cv2 |
import matplotlib.pyplot as plt |
import readData as rd |
if __name__ == '__main__': |
with tf.device("/gpu:0"): |
gr = tf.Graph() |
with gr.as_default(): |
with tf.variable_scope("cnn_model"): |
x = tf.placeholder("float", [None, 4096],name="x") |
y = tf.placeholder("float", [None, 2],name="y") |
keep_prob = tf.placeholder(tf.float32,name="dropout") |
#outputの中身 |
#hidden1 |
x1 = tf.reshape(x, shape=[-1, 64, 64, 1],name="x1") |
incoming1 = 5 * 5 * 1 |
W1 = tf.get_variable("W1", [5, 5, 1, 32], initializer=tf.random_normal_initializer(stddev=(2.0/incoming1)**0.5)) |
b1 = tf.get_variable("b1", [32], initializer=tf.constant_initializer(value=0)) |
conv_1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x1, W1, strides=[1, 1, 1, 1], padding='SAME'), b1),name="conv_1") |
pool_1 = tf.nn.max_pool(conv_1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME',name="pool_1") |
#hidden2 |
incoming2 = 5 * 5 * 32 |
W2 = tf.get_variable("W2", [5, 5, 32, 64], initializer=tf.random_normal_initializer(stddev=(2.0/incoming2)**0.5)) |
b2 = tf.get_variable("b2", [64], initializer=tf.constant_initializer(value=0)) |
conv_2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(pool_1, W2, strides=[1, 1, 1, 1], padding='SAME'), b2),name="conv_2") |
pool_2 = tf.nn.max_pool(conv_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME',name="pool_2") |
#fc |
pool_2_flat = tf.reshape(pool_2, [-1, 16 * 16 * 64]) |
W3 = tf.get_variable("W3", [16*16*64, 1024], initializer=tf.random_normal_initializer(stddev=(2.0/(16*16*64))**0.5)) |
b3 = tf.get_variable("b3", [1024], initializer=tf.constant_initializer(value=0)) |
fc_1 = tf.nn.relu(tf.matmul(pool_2_flat, W3) + b3,name="fc_1") |
# apply dropout |
fc_1_drop = tf.nn.dropout(fc_1, keep_prob) |
#output |
W4 = tf.get_variable("W4", [1024,2], initializer=tf.random_normal_initializer(stddev=(2.0/1024)**0.5)) |
b4 = tf.get_variable("b4", [2], initializer=tf.constant_initializer(value=0)) |
output = tf.nn.relu(tf.matmul(fc_1_drop, W4) + b4,name="output") |
init_op = tf.global_variables_initializer() |
#restore |
sess = tf.Session() |
sess.run(init_op) |
saver = tf.train.Saver() |
saver.restore(sess, tf.train.latest_checkpoint('cnn2_save2')) |
print('---Restored a model') |
#vari => const に変換 |
from tensorflow.python.framework import graph_util |
converted_graph = graph_util.convert_variables_to_constants(sess, gr.as_graph_def(), ["output"]) |
#プロトコルバッファ形式でグラフ保存 |
tf.train.write_graph(converted_graph, "cnn2_save3", 'const_model.pb', as_text=False) |
``` |
### 試したこと |
checkpointをrestoreし、実際に推論はできています。 |
ただ最後のgraph_util.convert_variables_to_constantsのところでoutputがないとのエラーが生じてしまいます。 |
``` |
#vari => const に変換 |
from tensorflow.python.framework import graph_util |
converted_graph = graph_util.convert_variables_to_constants(sess, gr.as_graph_def(), ["output"]) |
#プロトコルバッファ形式でグラフ保存 |
tf.train.write_graph(converted_graph, "cnn2_save3", 'const_model.pb', as_text=False) |
``` |
aaaaaaaaaa |
aaaaaaaaaa |
aaaaaaaaaa |
tensor score 10
2018/11/25 15:55 投稿
Tensorflow でのプロトコルバッファの出力 |
### 前提・実現したいこと |
Tensorflowで学習させたパラメータをチェックポイントで保存したので、 |
チェックポイントをrestoreした推論だけを行うグラフをプロトコルバッファ形式で出力したいです。 |
### 発生している問題・エラーメッセージ |
``` |
AssertionError: output is not in graph |
``` |
### 該当のソースコード |
```python |
#cnn2_save で保存したcheckpoint形式で保存された学習させたパラメータ(変数)を読み込み推論だけのグラフを作成 |
#pb形式で保存 |
import tensorflow as tf |
import time |
import numpy as np |
import cv2 |
import matplotlib.pyplot as plt |
import readData as rd |
if __name__ == '__main__': |
with tf.device("/gpu:0"): |
gr = tf.Graph() |
with gr.as_default(): |
with tf.variable_scope("cnn_model"): |
x = tf.placeholder("float", [None, 4096],name="x") |
y = tf.placeholder("float", [None, 2],name="y") |
keep_prob = tf.placeholder(tf.float32,name="dropout") |
x = tf.placeholder("float", [None, 4096],name="x") |
y = tf.placeholder("float", [None, 2],name="y") |
keep_prob = tf.placeholder(tf.float32,name="dropout") |
#outputの中身 |
#hidden1 |
x1 = tf.reshape(x, shape=[-1, 64, 64, 1],name="x1") |
incoming1 = 5 * 5 * 1 |
W1 = tf.get_variable("W1", [5, 5, 1, 32], initializer=tf.random_normal_initializer(stddev=(2.0/incoming1)**0.5)) |
b1 = tf.get_variable("b1", [32], initializer=tf.constant_initializer(value=0)) |
conv_1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x1, W1, strides=[1, 1, 1, 1], padding='SAME'), b1),name="conv_1") |
pool_1 = tf.nn.max_pool(conv_1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME',name="pool_1") |
#hidden2 |
incoming2 = 5 * 5 * 32 |
W2 = tf.get_variable("W2", [5, 5, 32, 64], initializer=tf.random_normal_initializer(stddev=(2.0/incoming2)**0.5)) |
b2 = tf.get_variable("b2", [64], initializer=tf.constant_initializer(value=0)) |
conv_2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(pool_1, W2, strides=[1, 1, 1, 1], padding='SAME'), b2),name="conv_2") |
pool_2 = tf.nn.max_pool(conv_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME',name="pool_2") |
#fc |
pool_2_flat = tf.reshape(pool_2, [-1, 16 * 16 * 64]) |
W3 = tf.get_variable("W3", [16*16*64, 1024], initializer=tf.random_normal_initializer(stddev=(2.0/(16*16*64))**0.5)) |
b3 = tf.get_variable("b3", [1024], initializer=tf.constant_initializer(value=0)) |
fc_1 = tf.nn.relu(tf.matmul(pool_2_flat, W3) + b3,name="fc_1") |
# apply dropout |
fc_1_drop = tf.nn.dropout(fc_1, keep_prob) |
#output |
W4 = tf.get_variable("W4", [1024,2], initializer=tf.random_normal_initializer(stddev=(2.0/1024)**0.5)) |
b4 = tf.get_variable("b4", [2], initializer=tf.constant_initializer(value=0)) |
output = tf.nn.relu(tf.matmul(fc_1_drop, W4) + b4,name="output") |
init_op = tf.global_variables_initializer() |
#restore |
sess = tf.Session() |
sess.run(init_op) |
saver = tf.train.Saver() |
saver.restore(sess, tf.train.latest_checkpoint('cnn2_save2')) |
print('---Restored a model') |
#vari => const に変換 |
from tensorflow.python.framework import graph_util |
converted_graph = graph_util.convert_variables_to_constants(sess, gr.as_graph_def(), ["output"]) |
#プロトコルバッファ形式でグラフ保存 |
tf.train.write_graph(converted_graph, "cnn2_save3", 'const_model.pb', as_text=False) |
``` |
### 試したこと |
checkpointをrestoreし、実際に推論はできています。 |
ただ最後のgraph_util.convert_variables_to_constantsのところでoutputがないとのエラーが生じてしまいます。 |
``` |
#vari => const に変換 |
from tensorflow.python.framework import graph_util |
converted_graph = graph_util.convert_variables_to_constants(sess, gr.as_graph_def(), ["output"]) |
#プロトコルバッファ形式でグラフ保存 |
tf.train.write_graph(converted_graph, "cnn2_save3", 'const_model.pb', as_text=False) |
``` |