質問編集履歴
3
test
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
aaaaaaaaaa
|
body
CHANGED
File without changes
|
2
sakuzyo
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,88 +1,3 @@
|
|
1
|
-
### 前提・実現したいこと
|
2
|
-
|
3
|
-
Tensorflowで学習させたパラメータをチェックポイントで保存したので、
|
4
|
-
チェックポイントをrestoreした推論だけを行うグラフをプロトコルバッファ形式で出力したいです。
|
5
|
-
|
6
|
-
### 発生している問題・エラーメッセージ
|
7
|
-
|
8
|
-
```
|
9
|
-
AssertionError: output is not in graph
|
10
|
-
```
|
11
|
-
|
12
|
-
### 該当のソースコード
|
13
|
-
|
14
|
-
```python
|
15
|
-
#cnn2_save で保存したcheckpoint形式で保存された学習させたパラメータ(変数)を読み込み推論だけのグラフを作成
|
16
|
-
#pb形式で保存
|
17
|
-
|
18
|
-
import tensorflow as tf
|
19
|
-
import time
|
20
|
-
|
21
|
-
import numpy as np
|
22
|
-
|
1
|
+
aaaaaaaaaa
|
23
|
-
import matplotlib.pyplot as plt
|
24
|
-
import readData as rd
|
25
|
-
|
26
|
-
if __name__ == '__main__':
|
27
|
-
with tf.device("/gpu:0"):
|
28
|
-
gr = tf.Graph()
|
29
|
-
with gr.as_default():
|
30
|
-
with tf.variable_scope("cnn_model"):
|
31
|
-
x = tf.placeholder("float", [None, 4096],name="x")
|
32
|
-
y = tf.placeholder("float", [None, 2],name="y")
|
33
|
-
keep_prob = tf.placeholder(tf.float32,name="dropout")
|
34
|
-
|
35
|
-
|
2
|
+
aaaaaaaaaa
|
36
|
-
#hidden1
|
37
|
-
x1 = tf.reshape(x, shape=[-1, 64, 64, 1],name="x1")
|
38
|
-
incoming1 = 5 * 5 * 1
|
39
|
-
W1 = tf.get_variable("W1", [5, 5, 1, 32], initializer=tf.random_normal_initializer(stddev=(2.0/incoming1)**0.5))
|
40
|
-
b1 = tf.get_variable("b1", [32], initializer=tf.constant_initializer(value=0))
|
41
|
-
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")
|
42
|
-
pool_1 = tf.nn.max_pool(conv_1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME',name="pool_1")
|
43
|
-
#hidden2
|
44
|
-
incoming2 = 5 * 5 * 32
|
45
|
-
W2 = tf.get_variable("W2", [5, 5, 32, 64], initializer=tf.random_normal_initializer(stddev=(2.0/incoming2)**0.5))
|
46
|
-
b2 = tf.get_variable("b2", [64], initializer=tf.constant_initializer(value=0))
|
47
|
-
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")
|
48
|
-
pool_2 = tf.nn.max_pool(conv_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME',name="pool_2")
|
49
|
-
#fc
|
50
|
-
pool_2_flat = tf.reshape(pool_2, [-1, 16 * 16 * 64])
|
51
|
-
W3 = tf.get_variable("W3", [16*16*64, 1024], initializer=tf.random_normal_initializer(stddev=(2.0/(16*16*64))**0.5))
|
52
|
-
b3 = tf.get_variable("b3", [1024], initializer=tf.constant_initializer(value=0))
|
53
|
-
fc_1 = tf.nn.relu(tf.matmul(pool_2_flat, W3) + b3,name="fc_1")
|
54
|
-
# apply dropout
|
55
|
-
fc_1_drop = tf.nn.dropout(fc_1, keep_prob)
|
56
|
-
#output
|
57
|
-
W4 = tf.get_variable("W4", [1024,2], initializer=tf.random_normal_initializer(stddev=(2.0/1024)**0.5))
|
58
|
-
b4 = tf.get_variable("b4", [2], initializer=tf.constant_initializer(value=0))
|
59
|
-
output = tf.nn.relu(tf.matmul(fc_1_drop, W4) + b4,name="output")
|
60
|
-
|
61
|
-
init_op = tf.global_variables_initializer()
|
62
|
-
|
63
|
-
#restore
|
64
|
-
sess = tf.Session()
|
65
|
-
sess.run(init_op)
|
66
|
-
saver = tf.train.Saver()
|
67
|
-
saver.restore(sess, tf.train.latest_checkpoint('cnn2_save2'))
|
68
|
-
print('---Restored a model')
|
69
|
-
|
70
|
-
#vari => const に変換
|
71
|
-
from tensorflow.python.framework import graph_util
|
72
|
-
converted_graph = graph_util.convert_variables_to_constants(sess, gr.as_graph_def(), ["output"])
|
73
|
-
#プロトコルバッファ形式でグラフ保存
|
74
|
-
tf.train.write_graph(converted_graph, "cnn2_save3", 'const_model.pb', as_text=False)
|
75
|
-
|
76
|
-
```
|
77
|
-
|
78
|
-
|
3
|
+
aaaaaaaaaa
|
79
|
-
|
80
|
-
checkpointをrestoreし、実際に推論はできています。
|
81
|
-
ただ最後のgraph_util.convert_variables_to_constantsのところでoutputがないとのエラーが生じてしまいます。
|
82
|
-
```
|
83
|
-
#vari => const に変換
|
84
|
-
from tensorflow.python.framework import graph_util
|
85
|
-
converted_graph = graph_util.convert_variables_to_constants(sess, gr.as_graph_def(), ["output"])
|
86
|
-
#プロトコルバッファ形式でグラフ保存
|
87
|
-
tf.train.write_graph(converted_graph, "cnn2_save3", 'const_model.pb', as_text=False)
|
88
|
-
```
|
1
誤字修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -28,9 +28,9 @@
|
|
28
28
|
gr = tf.Graph()
|
29
29
|
with gr.as_default():
|
30
30
|
with tf.variable_scope("cnn_model"):
|
31
|
-
x = tf.placeholder("float", [None, 4096],name="x")
|
31
|
+
x = tf.placeholder("float", [None, 4096],name="x")
|
32
|
-
y = tf.placeholder("float", [None, 2],name="y")
|
32
|
+
y = tf.placeholder("float", [None, 2],name="y")
|
33
|
-
keep_prob = tf.placeholder(tf.float32,name="dropout")
|
33
|
+
keep_prob = tf.placeholder(tf.float32,name="dropout")
|
34
34
|
|
35
35
|
#outputの中身
|
36
36
|
#hidden1
|