質問編集履歴

3

test

2018/11/25 15:54

投稿

tensor
tensor

スコア10

test CHANGED
@@ -1 +1 @@
1
- Tensorflow でのプロトコルバッファの出力
1
+ aaaaaaaaaa
test CHANGED
File without changes

2

sakuzyo

2018/11/25 15:54

投稿

tensor
tensor

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,175 +1,5 @@
1
- ### 前提・実現したいこと
1
+ aaaaaaaaaa
2
2
 
3
+ aaaaaaaaaa
3
4
 
4
-
5
- Tensorflowで学習させたパラメータをチェックポイントで保存したので、
6
-
7
- チェックポイントをrestoreした推論だけを行うグラフをプロトコルバッファ形式で出力したいです。
8
-
9
-
10
-
11
- ### 発生している問題・エラーメッセージ
12
-
13
-
14
-
15
- ```
16
-
17
- AssertionError: output is not in graph
18
-
19
- ```
20
-
21
-
22
-
23
- ### 該当のソースコード
24
-
25
-
26
-
27
- ```python
28
-
29
- #cnn2_save で保存したcheckpoint形式で保存された学習させたパラメータ(変数)を読み込み推論だけのグラフを作成
30
-
31
- #pb形式で保存
32
-
33
-
34
-
35
- import tensorflow as tf
36
-
37
- import time
38
-
39
-
40
-
41
- import numpy as np
42
-
43
- import cv2
5
+ aaaaaaaaaa
44
-
45
- import matplotlib.pyplot as plt
46
-
47
- import readData as rd
48
-
49
-
50
-
51
- if __name__ == '__main__':
52
-
53
- with tf.device("/gpu:0"):
54
-
55
- gr = tf.Graph()
56
-
57
- with gr.as_default():
58
-
59
- with tf.variable_scope("cnn_model"):
60
-
61
- x = tf.placeholder("float", [None, 4096],name="x")
62
-
63
- y = tf.placeholder("float", [None, 2],name="y")
64
-
65
- keep_prob = tf.placeholder(tf.float32,name="dropout")
66
-
67
-
68
-
69
- #outputの中身
70
-
71
- #hidden1
72
-
73
- x1 = tf.reshape(x, shape=[-1, 64, 64, 1],name="x1")
74
-
75
- incoming1 = 5 * 5 * 1
76
-
77
- W1 = tf.get_variable("W1", [5, 5, 1, 32], initializer=tf.random_normal_initializer(stddev=(2.0/incoming1)**0.5))
78
-
79
- b1 = tf.get_variable("b1", [32], initializer=tf.constant_initializer(value=0))
80
-
81
- 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")
82
-
83
- pool_1 = tf.nn.max_pool(conv_1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME',name="pool_1")
84
-
85
- #hidden2
86
-
87
- incoming2 = 5 * 5 * 32
88
-
89
- W2 = tf.get_variable("W2", [5, 5, 32, 64], initializer=tf.random_normal_initializer(stddev=(2.0/incoming2)**0.5))
90
-
91
- b2 = tf.get_variable("b2", [64], initializer=tf.constant_initializer(value=0))
92
-
93
- 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")
94
-
95
- pool_2 = tf.nn.max_pool(conv_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME',name="pool_2")
96
-
97
- #fc
98
-
99
- pool_2_flat = tf.reshape(pool_2, [-1, 16 * 16 * 64])
100
-
101
- W3 = tf.get_variable("W3", [16*16*64, 1024], initializer=tf.random_normal_initializer(stddev=(2.0/(16*16*64))**0.5))
102
-
103
- b3 = tf.get_variable("b3", [1024], initializer=tf.constant_initializer(value=0))
104
-
105
- fc_1 = tf.nn.relu(tf.matmul(pool_2_flat, W3) + b3,name="fc_1")
106
-
107
- # apply dropout
108
-
109
- fc_1_drop = tf.nn.dropout(fc_1, keep_prob)
110
-
111
- #output
112
-
113
- W4 = tf.get_variable("W4", [1024,2], initializer=tf.random_normal_initializer(stddev=(2.0/1024)**0.5))
114
-
115
- b4 = tf.get_variable("b4", [2], initializer=tf.constant_initializer(value=0))
116
-
117
- output = tf.nn.relu(tf.matmul(fc_1_drop, W4) + b4,name="output")
118
-
119
-
120
-
121
- init_op = tf.global_variables_initializer()
122
-
123
-
124
-
125
- #restore
126
-
127
- sess = tf.Session()
128
-
129
- sess.run(init_op)
130
-
131
- saver = tf.train.Saver()
132
-
133
- saver.restore(sess, tf.train.latest_checkpoint('cnn2_save2'))
134
-
135
- print('---Restored a model')
136
-
137
-
138
-
139
- #vari => const に変換
140
-
141
- from tensorflow.python.framework import graph_util
142
-
143
- converted_graph = graph_util.convert_variables_to_constants(sess, gr.as_graph_def(), ["output"])
144
-
145
- #プロトコルバッファ形式でグラフ保存
146
-
147
- tf.train.write_graph(converted_graph, "cnn2_save3", 'const_model.pb', as_text=False)
148
-
149
-
150
-
151
- ```
152
-
153
-
154
-
155
- ### 試したこと
156
-
157
-
158
-
159
- checkpointをrestoreし、実際に推論はできています。
160
-
161
- ただ最後のgraph_util.convert_variables_to_constantsのところでoutputがないとのエラーが生じてしまいます。
162
-
163
- ```
164
-
165
- #vari => const に変換
166
-
167
- from tensorflow.python.framework import graph_util
168
-
169
- converted_graph = graph_util.convert_variables_to_constants(sess, gr.as_graph_def(), ["output"])
170
-
171
- #プロトコルバッファ形式でグラフ保存
172
-
173
- tf.train.write_graph(converted_graph, "cnn2_save3", 'const_model.pb', as_text=False)
174
-
175
- ```

1

誤字修正

2018/11/25 15:53

投稿

tensor
tensor

スコア10

test CHANGED
File without changes
test CHANGED
@@ -58,11 +58,11 @@
58
58
 
59
59
  with tf.variable_scope("cnn_model"):
60
60
 
61
- x = tf.placeholder("float", [None, 4096],name="x") # mnist data image of shape 28*28=784
61
+ x = tf.placeholder("float", [None, 4096],name="x")
62
62
 
63
- y = tf.placeholder("float", [None, 2],name="y") # 0-9 digits recognition => 10 classes
63
+ y = tf.placeholder("float", [None, 2],name="y")
64
64
 
65
- keep_prob = tf.placeholder(tf.float32,name="dropout") # dropout probability
65
+ keep_prob = tf.placeholder(tf.float32,name="dropout")
66
66
 
67
67
 
68
68