Reputation: 147
After training my 'parameters' (w1,w2, weights of filters in Conv net), saving them as parameters=sess.run(parameters)
I take an image img=[1,64,64,3], and pass it to mypredict(x,parameters) function to predict but it gives error. The functions are given below. Any advice of whats going wrong.
def forward_propagation(X, parameters):
W1 = parameters['W1']
W2 = parameters['W2']
Z1 = tf.nn.conv2d(X,W1,strides=[1,1,1,1],padding='SAME')
A1 = tf.nn.relu(Z1)
P1 = tf.nn.max_pool(A1,ksize=[1,8,8,1],strides=[1,8,8,1],padding='SAME')
Z2 = tf.nn.conv2d(P1,W2,strides=[1,1,1,1],padding='SAME')
A2 = tf.nn.relu(Z2)
P2 = tf.nn.max_pool(A2,ksize=[1,4,4,1],strides=[1,4,4,1],padding='SAME')
P2 = tf.contrib.layers.flatten(P2)
Z3 = tf.contrib.layers.fully_connected(P2,num_outputs=6,activation_fn=None)
return Z3
def mypredict(X,par):
W1 = tf.convert_to_tensor(par["W1"])
W2 = tf.convert_to_tensor(par["W2"])
params = {"W1": W1,
"W2": W2}
x = tf.placeholder("float", [1,64,64,3])
z3 = forward_propagation_for_predict(x, params)
p = tf.argmax(z3)
sess = tf.Session()
prediction = sess.run(p, feed_dict = {x:X})
return prediction
I used the same function "forward_propagation" for training the weights, but when i pass a single image, it doesn't work.
Error:
FailedPreconditionError Traceback (most recent call last) /opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 1138 try: -> 1139 return fn(*args) 1140 except errors.OpError as e:
/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata) 1120 feed_dict, fetch_list, target_list, -> 1121 status, run_metadata) 1122
/opt/conda/lib/python3.6/contextlib.py in exit(self, type, value, traceback) 88 try: ---> 89 next(self.gen) 90 except StopIteration:
/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status() 465 compat.as_text(pywrap_tensorflow.TF_Message(status)), --> 466 pywrap_tensorflow.TF_GetCode(status)) 467 finally:
FailedPreconditionError: Attempting to use uninitialized value fully_connected_1/biases [[Node: fully_connected_1/biases/read = IdentityT=DT_FLOAT, _class=["loc:@fully_connected_1/biases"], _device="/job:localhost/replica:0/task:0/cpu:0"]]
During handling of the above exception, another exception occurred:
FailedPreconditionError Traceback (most recent call last) in () ----> 1 pred=mypredict(t,pp) 2
in mypredict(X, par) 49 50 sess = tf.Session() ---> 51 prediction = sess.run(p, feed_dict = {x:X}) 52 53 return prediction
/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 787 try: 788 result = self._run(None, fetches, feed_dict, options_ptr, --> 789 run_metadata_ptr) 790 if run_metadata: 791 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 995 if final_fetches or final_targets: 996 results = self._do_run(handle, final_targets, final_fetches, --> 997 feed_dict_string, options, run_metadata) 998 else: 999 results = []
/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 1130 if handle is None: 1131 return self._do_call(_run_fn, self._session, feed_dict, fetch_list, -> 1132 target_list, options, run_metadata) 1133 else: 1134 return self._do_call(_prun_fn, self._session, handle, feed_dict,
/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 1150 except KeyError: 1151 pass -> 1152 raise type(e)(node_def, op, message) 1153 1154 def _extend_graph(self):
FailedPreconditionError: Attempting to use uninitialized value fully_connected_1/biases
Upvotes: 0
Views: 108
Reputation: 804
You must also load the parameters from the fully connected layer.
However, I would suggest using TensorFlow's Saver and Restore functions anyhow.
For saving, here is a toy example:
import tensorflow as tf
w1 = tf.Variable(tf.random_normal(shape=[2]), name='w1')
w2 = tf.Variable(tf.random_normal(shape=[5]), name='w2')
saver = tf.train.Saver()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
saver.save(sess, 'my_test_model',global_step=1000) # saving model after 1000 steps
The following files are stored:
my_test_model-1000.index
my_test_model-1000.meta
my_test_model-1000.data-00000-of-00001
checkpoint
So for restoring, you can first recreate the net and then load the parameters:
with tf.Session() as sess:
recreated_net = tf.train.import_meta_graph('my_test_model-1000.meta')
recreated_net.restore(sess, tf.train.latest_checkpoint('./'))
Upvotes: 0