Error on my neural network fully connected kernel initialisation - python

I'm trying ot use a neural network with the SUMO trafic simulator and TracI (SUMO tool) but i have an error on my network initialisation
I currently added :
with tf.Session() as sess:
# Initialize the variables or load a network
sess.run(tf.global_variables_initializer())
After the creation of my network with this line :
DQNetwork = DQNetwork(state_size, action_size, learning_rate)
Here the code of my DQNetwork :
class DQNetwork:
def __init__(self, state_size, action_size, learning_rate, name='DQNetwork'):
self.state_size = state_size
self.action_size = action_size
self.learning_rate = learning_rate
with tf.variable_scope(name):
# We create the placeholders
# *state_size means that we take each elements of state_size in tuple hence is like if we wrote
# [None, 84, 84, 4]
self.inputs_ = tf.placeholder(tf.float32, shape=[state_size[1], state_size[0]], name="inputs")
self.actions_ = tf.placeholder(tf.float32, [None, self.action_size], name="actions_")
# Remember that target_Q is the R(s,a) + ymax Qhat(s', a')
self.target_Q = tf.placeholder(tf.float32, [None], name="target")
self.fc = tf.layers.dense(inputs = self.inputs_,
units = 100,
activation = tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(),
name="fc1")
self.output = tf.layers.dense(inputs = self.fc,
kernel_initializer=tf.contrib.layers.xavier_initializer(),
units = self.action_size,
activation=None)
# Q is our predicted Q value.
self.Q = tf.reduce_sum(tf.multiply(self.output, self.actions_))
# The loss is the difference between our predicted Q_values and the Q_target
# Sum(Qtarget - Q)^2
self.loss = tf.reduce_mean(tf.square(self.target_Q - self.Q))
self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss)
The Error which is the problem is :
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value DQNetwork/fc1/kernel
[[{{node DQNetwork/fc1/kernel/read}}]]
And here the error log : (A bit long i'm sorry, i included the warnings)
WARNING:tensorflow:From C:\Users\Odeven poste 1\Documents\WorkSpace\V1.0 SumoDQL\DQNet.py:33: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.
Instructions for updating:
Use keras.layers.dense instead.
WARNING:tensorflow:From C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2019-04-12 10:08:47.635411: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
$$$ TracI tools loaded $$$
Loading configuration... done.
Step #45.00Traceback (most recent call last):icles TOT 26 ACT 26 BUF 84)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1334, in _do_call
return fn(*args)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1319, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1407, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value DQNetwork/fc1/kernel
[[{{node DQNetwork/fc1/kernel/read}}]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\Train.py", line 350, in <module>
train()
File ".\Train.py", line 301, in train
action, explore_probability = predict_action(explore_start, explore_stop, decay_rate, decay_step, state)
File ".\Train.py", line 155, in predict_action
Qs = sess.run(DQNetwork.output, feed_dict = {DQNetwork.inputs_: state})
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
run_metadata_ptr)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1328, in _do_run
run_metadata)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1348, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value DQNetwork/fc1/kernel
[[node DQNetwork/fc1/kernel/read (defined at C:\Users\Odeven poste 1\Documents\WorkSpace\V1.0 SumoDQL\DQNet.py:33) ]]
Caused by op 'DQNetwork/fc1/kernel/read', defined at:
File ".\Train.py", line 111, in <module>
DQNetwork = DQNetwork(state_size, action_size, learning_rate)
File "C:\Users\Odeven poste 1\Documents\WorkSpace\V1.0 SumoDQL\DQNet.py", line 33, in __init__
name="fc1")
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\layers\core.py", line 188, in dense
return layer.apply(inputs)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1227, in apply
return self.__call__(inputs, *args, **kwargs)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\layers\base.py", line 530, in __call__
outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 538, in __call__
self._maybe_build(inputs)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1603, in _maybe_build
self.build(input_shapes)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\keras\layers\core.py", line 949, in build
trainable=True)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\layers\base.py", line 435, in add_weight
getter=vs.get_variable)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 349, in add_weight
aggregation=aggregation)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\training\checkpointable\base.py", line 607, in _add_variable_with_custom_getter
**kwargs_for_getter)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1479, in get_variable
aggregation=aggregation)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1220, in get_variable
aggregation=aggregation)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 547, in get_variable
aggregation=aggregation)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 499, in _true_getter
aggregation=aggregation)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 911, in _get_single_variable
aggregation=aggregation)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 213, in __call__
return cls._variable_v1_call(*args, **kwargs)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 176, in _variable_v1_call
aggregation=aggregation)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 155, in <lambda>
previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 2495, in default_variable_creator
expected_shape=expected_shape, import_scope=import_scope)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 217, in __call__
return super(VariableMetaclass, cls).__call__(*args, **kwargs)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 1395, in __init__
constraint=constraint)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 1557, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\util\dispatch.py", line 180, in wrapper
return target(*args, **kwargs)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\array_ops.py", line 81, in identity
ret = gen_array_ops.identity(input, name=name)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 4537, in identity
"Identity", input=input, name=name)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\framework\ops.py", line 3300, in create_op
op_def=op_def)
File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\framework\ops.py", line 1801, in __init__
self._traceback = tf_stack.extract_stack()
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value DQNetwork/fc1/kernel
[[node DQNetwork/fc1/kernel/read (defined at C:\Users\Odeven poste 1\Documents\WorkSpace\V1.0 SumoDQL\DQNet.py:33) ]]
Error: tcpip::Socket::recvAndCheck # recv: peer shutdown
Quitting (on error).
PS C:\Users\Odeven poste 1\Documents\WorkSpace\V1.0 SumoDQL>

I found the solution, as i said i was using :
with tf.Session() as sess:
# Initialize the variables or load a network
sess.run(tf.global_variables_initializer())
And i also have a function which determined the Q values of my network for a given input set which have :
with tf.Session() as sess:
I just removed it and now everything is functional.

Related

Cannot classify with my TensorFlow model: FailedPreconditionError

I am trying to classify an input image with a TensorFlow model in a multi-class classification problem. I would like to plot the probabilities of the top-10 highest predicted class. I do that with the following steps:
1. Load the model
import tensorflow as tf
import tensorflow.compat.v1 as tfc
import PIL
import numpy as np
import cv2
from tensorflow.keras.models import model_from_json
import warnings
import matplotlib.pyplot as plt
warnings.filterwarnings('ignore')
tf.compat.v1.disable_eager_execution()
json_file = open('resnet-model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model_json = loaded_model_json.replace('"activation":"softmax"', '"activation":"linear"')
model = tf.keras.models.model_from_json(loaded_model_json)
2. Prepare the function to return logits and probabilities
#Sets the threshold for what messages will be logged
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
#Starts the Interactive Session
sess=tfc.InteractiveSession()
#Size of the image that will be classified
image=tf.Variable(tf.zeros((1,224,224,3)))
def resnet2(image,model):
logits = model(image)
probs = tf.nn.softmax(logits)
return logits, probs
#Returns logits and probabilities from the network
logits, probs = resnet2(image, model)
3. Function to classify and return top10 probabilities
def classify(img, correct_class=None, target_class=None):
#Get probabilities given the input image
p = sess.run(probs, feed_dict={image: img})[0]
#get top-10 probablilities
topk = list(p.argsort()[-10:][::-1])
#Select top10 probabilities
topprobs = p[topk]
print(topprobs)
4. Prepare Image to be classified and classify it
#Image class is 11
img_class = 11
#Open the image
img = cv2.imread("sample-image.jpg")
img= img.reshape(1,224,224,3)
img = (np.asarray(img) / 255.0).astype(np.float32)
classify(img, correct_class=img_class)
However, whenever I run it I have the following error:
---------------------------------------------------------------------------
FailedPreconditionError Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1364 try:
-> 1365 return fn(*args)
1366 except errors.OpError as e:
/usr/local/lib/python3.8/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1348 self._extend_graph()
-> 1349 return self._call_tf_sessionrun(options, feed_dict, fetch_list,
1350 target_list, run_metadata)
/usr/local/lib/python3.8/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1440 run_metadata):
-> 1441 return tf_session.TF_SessionRun_wrapper(self._session, options, feed_dict,
1442 fetch_list, target_list,
FailedPreconditionError: 2 root error(s) found.
(0) Failed precondition: Error while reading resource variable conv4_block4_3_bn_9/beta from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/conv4_block4_3_bn_9/beta)
[[{{node functional_1_5/conv4_block4_3_bn/ReadVariableOp_1}}]]
[[functional_1_5/dropout/cond/then/_0/dropout/GreaterEqual/y/_41]]
(1) Failed precondition: Error while reading resource variable conv4_block4_3_bn_9/beta from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/conv4_block4_3_bn_9/beta)
[[{{node functional_1_5/conv4_block4_3_bn/ReadVariableOp_1}}]]
0 successful operations.
0 derived errors ignored.
During handling of the above exception, another exception occurred:
FailedPreconditionError Traceback (most recent call last)
<ipython-input-116-291671c55c1a> in <module>
----> 1 classify(img, correct_class=img_class)
<ipython-input-114-f609e06291c4> in classify(img, correct_class, target_class)
6
7 #Get probabilities given the input image
----> 8 p = sess.run(probs, feed_dict={image: img})[0]
9 #ax1.imshow(img)
10 #fig.sca(ax1)
/usr/local/lib/python3.8/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
955
956 try:
--> 957 result = self._run(None, fetches, feed_dict, options_ptr,
958 run_metadata_ptr)
959 if run_metadata:
/usr/local/lib/python3.8/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1178 # or if the call is a partial run that specifies feeds.
1179 if final_fetches or final_targets or (handle and feed_dict_tensor):
-> 1180 results = self._do_run(handle, final_targets, final_fetches,
1181 feed_dict_tensor, options, run_metadata)
1182 else:
/usr/local/lib/python3.8/dist-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1356
1357 if handle is None:
-> 1358 return self._do_call(_run_fn, feeds, fetches, targets, options,
1359 run_metadata)
1360 else:
/usr/local/lib/python3.8/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1382 '\nsession_config.graph_options.rewrite_options.'
1383 'disable_meta_optimizer = True')
-> 1384 raise type(e)(node_def, op, message)
1385
1386 def _extend_graph(self):
FailedPreconditionError: 2 root error(s) found.
(0) Failed precondition: Error while reading resource variable conv4_block4_3_bn_9/beta from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/conv4_block4_3_bn_9/beta)
[[node functional_1_5/conv4_block4_3_bn/ReadVariableOp_1 (defined at <ipython-input-113-a25550204dc5>:3) ]]
[[functional_1_5/dropout/cond/then/_0/dropout/GreaterEqual/y/_41]]
(1) Failed precondition: Error while reading resource variable conv4_block4_3_bn_9/beta from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/conv4_block4_3_bn_9/beta)
[[node functional_1_5/conv4_block4_3_bn/ReadVariableOp_1 (defined at <ipython-input-113-a25550204dc5>:3) ]]
0 successful operations.
0 derived errors ignored.
Errors may have originated from an input operation.
Input Source operations connected to node functional_1_5/conv4_block4_3_bn/ReadVariableOp_1:
conv4_block4_3_bn_9/beta (defined at <ipython-input-111-e21a4c41d093>:12)
Input Source operations connected to node functional_1_5/conv4_block4_3_bn/ReadVariableOp_1:
conv4_block4_3_bn_9/beta (defined at <ipython-input-111-e21a4c41d093>:12)
Original stack trace for 'functional_1_5/conv4_block4_3_bn/ReadVariableOp_1':
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.8/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/home/anselmo/.local/lib/python3.8/site-packages/traitlets/config/application.py", line 845, in launch_instance
app.start()
File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelapp.py", line 612, in start
self.io_loop.start()
File "/home/anselmo/.local/lib/python3.8/site-packages/tornado/platform/asyncio.py", line 149, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
self._run_once()
File "/usr/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once
handle._run()
File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "/home/anselmo/.local/lib/python3.8/site-packages/tornado/ioloop.py", line 690, in <lambda>
lambda f: self._run_callback(functools.partial(callback, future))
File "/home/anselmo/.local/lib/python3.8/site-packages/tornado/ioloop.py", line 743, in _run_callback
ret = callback()
File "/home/anselmo/.local/lib/python3.8/site-packages/tornado/gen.py", line 787, in inner
self.run()
File "/home/anselmo/.local/lib/python3.8/site-packages/tornado/gen.py", line 748, in run
yielded = self.gen.send(value)
File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 365, in process_one
yield gen.maybe_future(dispatch(*args))
File "/home/anselmo/.local/lib/python3.8/site-packages/tornado/gen.py", line 209, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "/home/anselmo/.local/lib/python3.8/site-packages/tornado/gen.py", line 209, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 543, in execute_request
self.do_execute(
File "/home/anselmo/.local/lib/python3.8/site-packages/tornado/gen.py", line 209, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.8/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.8/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/home/anselmo/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 2876, in run_cell
result = self._run_cell(
File "/home/anselmo/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 2922, in _run_cell
return runner(coro)
File "/home/anselmo/.local/lib/python3.8/site-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
coro.send(None)
File "/home/anselmo/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3145, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "/home/anselmo/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3337, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "/home/anselmo/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3417, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-113-a25550204dc5>", line 8, in <module>
logits, probs = resnet2(image, model)
File "<ipython-input-113-a25550204dc5>", line 3, in resnet2
logits = model(image)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 776, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/functional.py", line 385, in call
return self._run_internal_graph(
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/functional.py", line 508, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 776, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/layers/normalization.py", line 720, in call
outputs = self._fused_batch_norm(inputs, training=training)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/layers/normalization.py", line 576, in _fused_batch_norm
output, mean, variance = tf_utils.smart_cond(training, train_op,
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/utils/tf_utils.py", line 64, in smart_cond
return smart_module.smart_cond(
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/smart_cond.py", line 56, in smart_cond
return false_fn()
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/layers/normalization.py", line 558, in _fused_batch_norm_inference
return nn.fused_batch_norm(
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
return target(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/nn_impl.py", line 1626, in fused_batch_norm
offset = ops.convert_to_tensor(offset, name="offset")
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py", line 1499, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/resource_variable_ops.py", line 1909, in _dense_var_to_tensor
return var._dense_var_to_tensor(dtype=dtype, name=name, as_ref=as_ref) # pylint: disable=protected-access
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/resource_variable_ops.py", line 1326, in _dense_var_to_tensor
return self.value()
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/resource_variable_ops.py", line 555, in value
return self._read_variable_op()
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/resource_variable_ops.py", line 657, in _read_variable_op
result = read_and_set_handle()
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/resource_variable_ops.py", line 647, in read_and_set_handle
result = gen_resource_variable_ops.read_variable_op(self._handle,
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/gen_resource_variable_ops.py", line 490, in read_variable_op
_, _, _op, _outputs = _op_def_library._apply_op_helper(
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/op_def_library.py", line 742, in _apply_op_helper
op = g._create_op_internal(op_type_name, inputs, dtypes=None,
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py", line 3477, in _create_op_internal
ret = Operation(
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py", line 1949, in __init__
self._traceback = tf_stack.extract_stack()
What is wrong with my code? is the model not initialized? how to fix it?
P.s1= I can call the function resnet2 normally with an input image and it returns both logits and probs. The problem is when I run sess.run()
P.s2= the model and image used can be found HERE
​
Since you are initializing a tensorflow (version < 2) session with tfc.InteractiveSession() you might need to initialize all values before running your session by calling:
tfc.initialize_all_variables().run()
This should initialize all variables. This would also back your case, that you can run Resnet alone but not with sess.run().

FailedPreconditionError when running output layer in session?

In tensorflow, I am experiencing an issue where I try to get the output of my last neuron by running it in a session but it gives me an error. I am running Python 3.8.5 and TensorFlow 1.15 (because of some hardware issues)
Here is my code:
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import tensorflow as tf
def main():
x_inputs = tf.Variable(np.array([[1, 2 ,3], [1, 2, 3]]), dtype=tf.float32)
y_outputs = tf.Variable(np.array([[1, 2, 4], [2, 3, 4]]), dtype=tf.float32)
model = create_model(x_inputs, y_outputs)
def create_model(x: np.array, y: np.array):
var_init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(var_init)
x_val = sess.run(x).flatten()
y_val = sess.run(y).flatten()
# neural network architecture
input_layer = tf.layers.dense(x, units=len(x_val), activation=tf.nn.sigmoid)
hidden_layer = tf.layers.dense(input_layer, units=5, activation=tf.nn.relu)
output_layer = tf.layers.dense(hidden_layer, units=2, activation=tf.nn.softmax)
print(sess.run(output_layer))
if __name__ == "__main__":
main()
Here's the error:
Traceback (most recent call last):
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1350, in _do_call
return fn(*args)
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1327, in _run_fn
return tf_session.TF_Run(session, options,
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/errors_impl.py", line 470, in __exit__
raise _make_specific_exception(
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value dense_2/bias
[[Node: dense_2/bias/read = Identity[T=DT_FLOAT, _class=["loc:#dense_2/bias"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](dense_2/bias)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "learning.py", line 29, in <module>
main()
File "learning.py", line 10, in main
model = create_model(x_inputs, y_outputs)
File "learning.py", line 26, in create_model
print(sess.run(output_layer))
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 894, in run
result = self._run(None, fetches, feed_dict, options_ptr,
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1127, in _run
results = self._do_run(handle, final_targets, final_fetches,
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1343, in _do_run
return self._do_call(_run_fn, self._session, feeds, fetches, targets,
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1363, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value dense_2/bias
[[Node: dense_2/bias/read = Identity[T=DT_FLOAT, _class=["loc:#dense_2/bias"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](dense_2/bias)]]
Caused by op 'dense_2/bias/read', defined at:
File "learning.py", line 29, in <module>
main()
File "learning.py", line 10, in main
model = create_model(x_inputs, y_outputs)
File "learning.py", line 24, in create_model
output_layer = tf.layers.dense(hidden_layer, units=2, activation=tf.nn.softmax)
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/layers/core.py", line 253, in dense
return layer.apply(inputs)
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/layers/base.py", line 762, in apply
return self.__call__(inputs, *args, **kwargs)
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/layers/base.py", line 636, in __call__
self.build(input_shapes)
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/layers/core.py", line 139, in build
self.bias = self.add_variable('bias',
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/layers/base.py", line 498, in add_variable
variable = vs.get_variable(name,
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py", line 1256, in get_variable
return get_variable_scope().get_variable(
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py", line 1091, in get_variable
return var_store.get_variable(
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py", line 429, in get_variable
return _true_getter(
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py", line 399, in _true_getter
return self._get_single_variable(
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py", line 798, in _get_single_variable
v = variables.Variable(
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variables.py", line 220, in __init__
self._init_from_args(
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variables.py", line 376, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py", line 127, in identity
return gen_array_ops.identity(input, name=name)
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2133, in identity
_, _, _op = _op_def_lib._apply_op_helper(
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py", line 785, in _apply_op_helper
op = g.create_op(op_type_name, inputs, output_types, name=scope,
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 3152, in create_op
ret = Operation(
File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1625, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value dense_2/bias
[[Node: dense_2/bias/read = Identity[T=DT_FLOAT, _class=["loc:#dense_2/bias"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](dense_2/bias)]]
Please help me I'm really struggling.
Thanks in advance!

Tensorflow : FailedPreconditionError: Attempting to use uninitialized value conv2d_transpose/bias

I am trying to build a model that uses transposed convolution operation ,But when I try to pass the weights and bias as a parameter to the model function it gives an error.
import tensorflow as tf
import cv2
class WeighsTest:
def __model_1(self, plh_var1,weights,bias):
conv = tf.nn.conv2d(plh_var1, weights["v1"], [1, 1, 1, 1], padding="SAME")
conv = tf.add(conv, bias["b1"])
conv = tf.nn.relu(conv)
tran_conv = tf.layers.conv2d_transpose(conv,32, 4, 3, padding="valid")
return tran_conv
def train(self, input_img):
plh = tf.placeholder(dtype=tf.float32, shape=(None, 84, 150, 3), name="input_img")
with tf.variable_scope("test", reuse=tf.AUTO_REUSE):
var_dict_1 = {
"v1": tf.get_variable("v1", shape=(2, 2, 3, 32), initializer=tf.contrib.layers.xavier_initializer())
}
bias_1 = {
"b1": tf.get_variable("b1", shape=32, initializer=tf.contrib.layers.xavier_initializer())
}
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
out_p = sess.run([self.__model_1(plh, var_dict_1, bias_1)], feed_dict={plh: [input_img]})
return out_p
if __name__ == '__main__':
obj = WeighsTest()
img = cv2.imread('./1.jpg')
output = obj.train(img)
traceback is given below
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py", line 1365, in _do_call
return fn(*args)
File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py", line 1350, in _run_fn
target_list, run_metadata)
File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py", line 1443, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value conv2d_transpose/bias
[[{{node conv2d_transpose/bias/read}}]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/strange/DEV/Python_Projects/slmv/testing_unit.py", line 43, in <module>
output = obj.train(img)
File "/home/strange/DEV/Python_Projects/slmv/testing_unit.py", line 36, in train
out_p = sess.run([self.__model_1(x1, var_dict_1, bias_1)], feed_dict={x1: [input_img]})
File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py", line 956, in run
run_metadata_ptr)
File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py", line 1180, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py", line 1359, in _do_run
run_metadata)
File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py", line 1384, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value conv2d_transpose/bias
[[node conv2d_transpose/bias/read (defined at usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py:1748) ]]
Original stack trace for 'conv2d_transpose/bias/read':
File "home/strange/DEV/Python_Projects/slmv/testing_unit.py", line 43, in <module>
output = obj.train(img)
File "home/strange/DEV/Python_Projects/slmv/testing_unit.py", line 36, in train
out_p = sess.run([self.__model_1(x1, var_dict_1, bias_1)], feed_dict={x1: [input_img]})
File "home/strange/DEV/Python_Projects/slmv/testing_unit.py", line 10, in __model_1
tran_conv = tf.layers.conv2d_transpose(conv,32, 4, 3, padding="valid")
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/util/deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/layers/convolutional.py", line 1279, in conv2d_transpose
return layer.apply(inputs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/util/deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/base_layer.py", line 1700, in apply
return self.__call__(inputs, *args, **kwargs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/layers/base.py", line 548, in __call__
outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/base_layer.py", line 824, in __call__
self._maybe_build(inputs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/base_layer.py", line 2146, in _maybe_build
self.build(input_shapes)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/layers/convolutional.py", line 787, in build
dtype=self.dtype)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/layers/base.py", line 461, in add_weight
**kwargs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/base_layer.py", line 529, in add_weight
aggregation=aggregation)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/training/tracking/base.py", line 712, in _add_variable_with_custom_getter
**kwargs_for_getter)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variable_scope.py", line 1500, in get_variable
aggregation=aggregation)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variable_scope.py", line 1243, in get_variable
aggregation=aggregation)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variable_scope.py", line 567, in get_variable
aggregation=aggregation)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variable_scope.py", line 519, in _true_getter
aggregation=aggregation)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variable_scope.py", line 933, in _get_single_variable
aggregation=aggregation)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variables.py", line 258, in __call__
return cls._variable_v1_call(*args, **kwargs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variables.py", line 219, in _variable_v1_call
shape=shape)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variables.py", line 197, in <lambda>
previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variable_scope.py", line 2519, in default_variable_creator
shape=shape)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variables.py", line 262, in __call__
return super(VariableMetaclass, cls).__call__(*args, **kwargs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variables.py", line 1688, in __init__
shape=shape)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/variables.py", line 1872, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/util/dispatch.py", line 180, in wrapper
return target(*args, **kwargs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/array_ops.py", line 203, in identity
ret = gen_array_ops.identity(input, name=name)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/gen_array_ops.py", line 4239, in identity
"Identity", input=input, name=name)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/op_def_library.py", line 794, in _apply_op_helper
op_def=op_def)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 3357, in create_op
attrs, op_def, compute_device)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 3426, in _create_op_internal
op_def=op_def)
File "usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 1748, in __init__
self._traceback = tf_stack.extract_stack()
It is important to pass the bias and weights as parameters to the model.
I used tensorflow-cpu 1.15.2 for the model.
Any idea how to solve this ?
Thank you
The model needed to be called before the tf.global_variabels_initializer() is used
ie. the train function is changed as below
def train(self, input_img):
plh = tf.placeholder(dtype=tf.float32, shape=(None, 84, 150, 3), name="input_img")
with tf.variable_scope("test", reuse=tf.AUTO_REUSE):
var_dict_1 = {
"v1": tf.get_variable("v1", shape=(2, 2, 3, 32), initializer=tf.contrib.layers.xavier_initializer())
}
bias_1 = {
"b1": tf.get_variable("b1", shape=32, initializer=tf.contrib.layers.xavier_initializer())
}
"""model is called before variable initialization"""
model = self.__model_1(plh, var_dict_1, bias_1)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
out_p = sess.run([model], feed_dict={plh: [input_img]})
return out_p
the line given below:
out_p = sess.run([self.__model_1(plh, var_dict_1, bias_1)], feed_dict={plh: [input_img]})
is changed into
out_p = sess.run([model], feed_dict={plh: [input_img]})

How to access/manipulate elements of tensor in keras model?

I want to test a new network structure which requires changing some of the elements of a tensor in a keras model. If I could find a way to convert/copy the tensor to a numpy array and then later transform it back into a tensor, then I should be able to make the model work.
I tried using the .eval() method to convert the tensor to a numpy array, but it gives me errors. I am also using this model with a DQN Agent from keras-rl, so it is possible the error is from how keras-rl uses the model. Here is my code:
def Filter_Features(F):
sess = Session()
with sess.as_default():
F_np = F.eval()
min_pos = np.argmin(F_np)
F_np[min_pos] = 0
return convert_to_tensor(F_np)
def create_model(nb_actions, num_frames=4):
inputs = Input(shape = (num_frames,84,84))
F = Conv2D(16,(8,8), activation='relu', strides=(4,4), data_format = "channels_first")(inputs)
...
F_k = Lambda(Filter_Features)(F)
actions = Dense(nb_actions, activation = 'linear')(F_k)
nnf_model = Model(inputs = inputs, outputs = actions)
return nnf_model
Note that the code runs if I remove the lambda layer, so the issue must be originating there. I received the error:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1356, in _do_call
return fn(*args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1341, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1429, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value conv2d_1/bias
[[{{node conv2d_1/bias/read}}]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Atari_Test.py", line 32, in <module>
model = Model_StackExchange.create_model(nb_actions = nb_actions)
File "/Users/j/deep-rl/Model_StackExchange.py", line 26, in create_model
F_k = Lambda(Filter_Features)(F)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/base_layer.py", line 457, in __call__
output = self.call(inputs, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/layers/core.py", line 687, in call
return self.function(inputs, **arguments)
File "/Users/j/deep-rl/Model_StackExchange.py", line 11, in Filter_Features
F_np = F.eval()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 731, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 5579, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 950, in run
run_metadata_ptr)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1173, in _run
feed_dict_tensor, options, run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1350, in _do_run
run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1370, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value conv2d_1/bias
[[node conv2d_1/bias/read (defined at /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:402) ]]
Original stack trace for 'conv2d_1/bias/read':
File "Atari_Test.py", line 32, in <module>
model = Model_StackExchange.create_model(nb_actions = nb_actions)
File "/Users/j/deep-rl/Model_StackExchange.py", line 21, in create_model
F = Conv2D(16,(8,8), activation='relu', strides=(4,4), data_format = "channels_first")(inputs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/base_layer.py", line 431, in __call__
self.build(unpack_singleton(input_shapes))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/layers/convolutional.py", line 147, in build
constraint=self.bias_constraint)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/base_layer.py", line 252, in add_weight
constraint=constraint)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 402, in variable
v = tf.Variable(value, dtype=tf.as_dtype(dtype), name=name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 259, in __call__
return cls._variable_v1_call(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 220, in _variable_v1_call
shape=shape)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 198, in <lambda>
previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/ops/variable_scope.py", line 2511, in default_variable_creator
shape=shape)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 263, in __call__
return super(VariableMetaclass, cls).__call__(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 1568, in __init__
shape=shape)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 1755, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper
return target(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py", line 86, in identity
ret = gen_array_ops.identity(input, name=name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 4253, in identity
"Identity", input=input, name=name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3616, in create_op
op_def=op_def)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 2005, in __init__
self._traceback = tf_stack.extract_stack()
Please let me know if you know how to access and change the elements of a tensor within a keras model. Thank you.

Tensorflow: Graph is finalized and cannot be modified

I am trying to build a custom object detection model using the Tensorflow Object Detection API. Specifically, I want to add a distance estimation to each detected object. So, I extended the SSDMetaArch class and adapted/added some crucial parts. Among other things:
added distance to the input
adapted protos to handle distance
created a new prediction head for distance estimations
created new loss taking distances into account
adapted postprocessing step to output distances
After all these steps the training seems to work and the losses (including the distance loss) go down.
But when I want to evaluate the model I get a
RuntimeError: Graph is finalized and cannot be modified.
Specifically the following line in the (adapted) visualize_boxes_and_labels_on_image_array() method in visualization_utils.py (around line 700) causes the error
#the follwing line throws the error
display_str = '{}: {}% -> {}'.format(display_str, int(100 * scores[i]), int(distances[i]))
#No error is thrown using the original code
#display_str = '{}: {}%'.format(display_str, int(100 * scores[i]))
It seems that just by printing the value the above exception is thrown.
My Question: I do not understand why printing a value will modify the graph. Could you help me understand why this is the case and if possible give me hint how I can avoid this modification to the graph.
Find the complete error below
INFO:tensorflow:Restoring parameters from E:/Data/OD_DE/tensorflow/models/model\model.ckpt-5103
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
2019-04-01 13:14:30.795741: W tensorflow/core/framework/op_kernel.cc:1261]
Unknown: RuntimeError: Graph is finalized and cannot be modified.
Traceback (most recent call last):
File "..\lib\site-packages\tensorflow\python\ops\script_ops.py", line 206, in __call__
ret = func(*args)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 271, in _visualize_boxes
image, boxes, classes, scores, category_index=category_index, **kwargs)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 730, in visualize_boxes_and_labels_on_image_array
print("We were definitely here:" + str(int(distances[i])))
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 525, in _slice_helper
name=name)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 6037, in __exit__
self._name_scope.__exit__(type_arg, value_arg, traceback_arg)
File "C:\Users\JD\AppData\Local\Continuum\Anaconda3\Lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 4016, in name_scope
yield "" if new_stack is None else new_stack + "/"
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 500, in _slice_helper
packed_begin, packed_end, packed_strides = (stack(begin), stack(end),
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 863, in stack
return ops.convert_to_tensor(values, name=name)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 1050, in convert_to_tensor
as_ref=False)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 1146, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "..\lib\site-packages\tensorflow\python\framework\constant_op.py", line 229, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "..\lib\site-packages\tensorflow\python\framework\constant_op.py", line 214, in constant
name=name).outputs[0]
File "..\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 3246, in create_op
self._check_not_finalized()
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 2919, in _check_not_finalized
raise RuntimeError("Graph is finalized and cannot be modified.")
RuntimeError: Graph is finalized and cannot be modified.
Traceback (most recent call last):
File "..\lib\site-packages\tensorflow\python\client\session.py", line 1334, in _do_call
return fn(*args)
File "..\lib\site-packages\tensorflow\python\client\session.py", line 1319, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "..\lib\site-packages\tensorflow\python\client\session.py", line 1407, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.UnknownError: RuntimeError: Graph is finalized and cannot be modified.
Traceback (most recent call last):
File "..\lib\site-packages\tensorflow\python\ops\script_ops.py", line 206, in __call__
ret = func(*args)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 271, in _visualize_boxes
image, boxes, classes, scores, category_index=category_index, **kwargs)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 730, in visualize_boxes_and_labels_on_image_array
print("We were definitely here:" + str(int(distances[i])))
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 525, in _slice_helper
name=name)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 6037, in __exit__
self._name_scope.__exit__(type_arg, value_arg, traceback_arg)
File "C:\Users\JD\AppData\Local\Continuum\Anaconda3\Lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 4016, in name_scope
yield "" if new_stack is None else new_stack + "/"
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 500, in _slice_helper
packed_begin, packed_end, packed_strides = (stack(begin), stack(end),
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 863, in stack
return ops.convert_to_tensor(values, name=name)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 1050, in convert_to_tensor
as_ref=False)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 1146, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "..\lib\site-packages\tensorflow\python\framework\constant_op.py", line 229, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "..\lib\site-packages\tensorflow\python\framework\constant_op.py", line 214, in constant
name=name).outputs[0]
File "..\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 3246, in create_op
self._check_not_finalized()
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 2919, in _check_not_finalized
raise RuntimeError("Graph is finalized and cannot be modified.")
RuntimeError: Graph is finalized and cannot be modified.
[[{{node map_1/while/PyFunc}} = PyFunc[Tin=[DT_UINT8, DT_FLOAT, DT_INT64, DT_FLOAT], Tout=[DT_UINT8], _class=["loc:#map_1/while/TensorArrayWrite/TensorArrayWriteV3"], token="pyfunc_1", _device="/job:localhost/replica:0/task:0/device:CPU:0"](map_1/while/Squeeze/_2907, map_1/while/TensorArrayReadV3_3/_2909, map_1/while/TensorArrayReadV3_4/_2911, map_1/while/TensorArrayReadV3_5/_2913)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\pydevd.py", line 1596, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\pydevd.py", line 1023, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/JD/tensorflowod/Lib/site-packages/tensorflow/models/research/object_detection/model_main.py", line 110, in <module>
tf.app.run()
File "..\lib\site-packages\tensorflow\python\platform\app.py", line 125, in run
_sys.exit(main(argv))
File "C:/Users/JD/tensorflowod/Lib/site-packages/tensorflow/models/research/object_detection/model_main.py", line 105, in main
tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 471, in train_and_evaluate
return executor.run()
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 610, in run
return self.run_local()
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 711, in run_local
saving_listeners=saving_listeners)
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 354, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1207, in _train_model
return self._train_model_default(input_fn, hooks, saving_listeners)
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1241, in _train_model_default
saving_listeners)
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1471, in _train_with_estimator_spec
_, loss = mon_sess.run([estimator_spec.train_op, estimator_spec.loss])
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 671, in run
run_metadata=run_metadata)
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1156, in run
run_metadata=run_metadata)
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1255, in run
raise six.reraise(*original_exc_info)
File "c:\users\JD\appdata\local\continuum\anaconda3\lib\site-packages\six.py", line 686, in reraise
raise value
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1240, in run
return self._sess.run(*args, **kwargs)
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1320, in run
run_metadata=run_metadata))
File "..\lib\site-packages\tensorflow\python\training\basic_session_run_hooks.py", line 582, in after_run
if self._save(run_context.session, global_step):
File "..\lib\site-packages\tensorflow\python\training\basic_session_run_hooks.py", line 607, in _save
if l.after_save(session, step):
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 517, in after_save
self._evaluate(global_step_value) # updates self.eval_result
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 537, in _evaluate
self._evaluator.evaluate_and_export())
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 912, in evaluate_and_export
hooks=self._eval_spec.hooks)
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 478, in evaluate
return _evaluate()
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 467, in _evaluate
output_dir=self.eval_dir(name))
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1591, in _evaluate_run
config=self._session_config)
File "..\lib\site-packages\tensorflow\python\training\evaluation.py", line 274, in _evaluate_once
session.run(eval_ops, feed_dict)
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 671, in run
run_metadata=run_metadata)
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1156, in run
run_metadata=run_metadata)
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1255, in run
raise six.reraise(*original_exc_info)
File "c:\users\JD\appdata\local\continuum\anaconda3\lib\site-packages\six.py", line 686, in reraise
raise value
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1240, in run
return self._sess.run(*args, **kwargs)
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1312, in run
run_metadata=run_metadata)
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1076, in run
return self._sess.run(*args, **kwargs)
File "..\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
run_metadata_ptr)
File "..\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _run
feed_dict_tensor, options, run_metadata)
File "..\lib\site-packages\tensorflow\python\client\session.py", line 1328, in _do_run
run_metadata)
File "..\lib\site-packages\tensorflow\python\client\session.py", line 1348, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.UnknownError: RuntimeError: Graph is finalized and cannot be modified.
Traceback (most recent call last):
File "..\lib\site-packages\tensorflow\python\ops\script_ops.py", line 206, in __call__
ret = func(*args)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 271, in _visualize_boxes
image, boxes, classes, scores, category_index=category_index, **kwargs)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 730, in visualize_boxes_and_labels_on_image_array
print("We were definitely here:" + str(int(distances[i])))
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 525, in _slice_helper
name=name)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 6037, in __exit__
self._name_scope.__exit__(type_arg, value_arg, traceback_arg)
File "C:\Users\JD\AppData\Local\Continuum\Anaconda3\Lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 4016, in name_scope
yield "" if new_stack is None else new_stack + "/"
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 500, in _slice_helper
packed_begin, packed_end, packed_strides = (stack(begin), stack(end),
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 863, in stack
return ops.convert_to_tensor(values, name=name)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 1050, in convert_to_tensor
as_ref=False)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 1146, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "..\lib\site-packages\tensorflow\python\framework\constant_op.py", line 229, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "..\lib\site-packages\tensorflow\python\framework\constant_op.py", line 214, in constant
name=name).outputs[0]
File "..\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 3246, in create_op
self._check_not_finalized()
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 2919, in _check_not_finalized
raise RuntimeError("Graph is finalized and cannot be modified.")
RuntimeError: Graph is finalized and cannot be modified.
[[node map_1/while/PyFunc (defined at ..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py:439) = PyFunc[Tin=[DT_UINT8, DT_FLOAT, DT_INT64, DT_FLOAT], Tout=[DT_UINT8], _class=["loc:#map_1/while/TensorArrayWrite/TensorArrayWriteV3"], token="pyfunc_1", _device="/job:localhost/replica:0/task:0/device:CPU:0"](map_1/while/Squeeze/_2907, map_1/while/TensorArrayReadV3_3/_2909, map_1/while/TensorArrayReadV3_4/_2911, map_1/while/TensorArrayReadV3_5/_2913)]]
Caused by op 'map_1/while/PyFunc', defined at:
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\pydevd.py", line 1596, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\pydevd.py", line 1023, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/JD/tensorflowod/Lib/site-packages/tensorflow/models/research/object_detection/model_main.py", line 110, in <module>
tf.app.run()
File "..\lib\site-packages\tensorflow\python\platform\app.py", line 125, in run
_sys.exit(main(argv))
File "C:/Users/JD/tensorflowod/Lib/site-packages/tensorflow/models/research/object_detection/model_main.py", line 105, in main
tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 471, in train_and_evaluate
return executor.run()
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 610, in run
return self.run_local()
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 711, in run_local
saving_listeners=saving_listeners)
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 354, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1207, in _train_model
return self._train_model_default(input_fn, hooks, saving_listeners)
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1241, in _train_model_default
saving_listeners)
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1471, in _train_with_estimator_spec
_, loss = mon_sess.run([estimator_spec.train_op, estimator_spec.loss])
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 671, in run
run_metadata=run_metadata)
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1156, in run
run_metadata=run_metadata)
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1240, in run
return self._sess.run(*args, **kwargs)
File "..\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1320, in run
run_metadata=run_metadata))
File "..\lib\site-packages\tensorflow\python\training\basic_session_run_hooks.py", line 582, in after_run
if self._save(run_context.session, global_step):
File "..\lib\site-packages\tensorflow\python\training\basic_session_run_hooks.py", line 607, in _save
if l.after_save(session, step):
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 517, in after_save
self._evaluate(global_step_value) # updates self.eval_result
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 537, in _evaluate
self._evaluator.evaluate_and_export())
File "..\lib\site-packages\tensorflow\python\estimator\training.py", line 912, in evaluate_and_export
hooks=self._eval_spec.hooks)
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 478, in evaluate
return _evaluate()
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 460, in _evaluate
self._evaluate_build_graph(input_fn, hooks, checkpoint_path))
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1484, in _evaluate_build_graph
self._call_model_fn_eval(input_fn, self.config))
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1520, in _call_model_fn_eval
features, labels, model_fn_lib.ModeKeys.EVAL, config)
File "..\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1195, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\model_lib.py", line 443, in model_fn
eval_dict)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 931, in get_estimator_eval_metric_ops
images = self.images_from_evaluation_dict(eval_dict)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 995, in images_from_evaluation_dict
self._min_score_thresh, self._use_normalized_coordinates)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 547, in draw_side_by_side_evaluation_image
eval_dict[input_data_fields.groundtruth_distances][indx], axis=0) if input_data_fields.groundtruth_distances in eval_dict else None
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 442, in draw_bounding_boxes_on_image_tensors
images = tf.map_fn(draw_boxes, elems, dtype=tf.uint8, back_prop=False)
File "..\lib\site-packages\tensorflow\python\ops\functional_ops.py", line 494, in map_fn
maximum_iterations=n)
File "..\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 3291, in while_loop
return_same_structure)
File "..\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 3004, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
File "..\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2939, in _BuildLoop
body_result = body(*packed_vars_for_body)
File "..\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 3260, in <lambda>
body = lambda i, lv: (i + 1, orig_body(*lv))
File "..\lib\site-packages\tensorflow\python\ops\functional_ops.py", line 483, in compute
packed_fn_values = fn(packed_values)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 439, in draw_boxes
tf.uint8)
File "..\lib\site-packages\tensorflow\python\ops\script_ops.py", line 457, in py_func
func=func, inp=inp, Tout=Tout, stateful=stateful, eager=False, name=name)
File "..\lib\site-packages\tensorflow\python\ops\script_ops.py", line 281, in _internal_py_func
input=inp, token=token, Tout=Tout, name=name)
File "..\lib\site-packages\tensorflow\python\ops\gen_script_ops.py", line 132, in py_func
"PyFunc", input=input, token=token, Tout=Tout, name=name)
File "..\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "..\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 3274, in create_op
op_def=op_def)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 1770, in __init__
self._traceback = tf_stack.extract_stack()
UnknownError (see above for traceback): RuntimeError: Graph is finalized and cannot be modified.
Traceback (most recent call last):
File "..\lib\site-packages\tensorflow\python\ops\script_ops.py", line 206, in __call__
ret = func(*args)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 271, in _visualize_boxes
image, boxes, classes, scores, category_index=category_index, **kwargs)
File "..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py", line 730, in visualize_boxes_and_labels_on_image_array
print("We were definitely here:" + str(int(distances[i])))
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 525, in _slice_helper
name=name)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 6037, in __exit__
self._name_scope.__exit__(type_arg, value_arg, traceback_arg)
File "C:\Users\JD\AppData\Local\Continuum\Anaconda3\Lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 4016, in name_scope
yield "" if new_stack is None else new_stack + "/"
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 500, in _slice_helper
packed_begin, packed_end, packed_strides = (stack(begin), stack(end),
File "..\lib\site-packages\tensorflow\python\ops\array_ops.py", line 863, in stack
return ops.convert_to_tensor(values, name=name)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 1050, in convert_to_tensor
as_ref=False)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 1146, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "..\lib\site-packages\tensorflow\python\framework\constant_op.py", line 229, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "..\lib\site-packages\tensorflow\python\framework\constant_op.py", line 214, in constant
name=name).outputs[0]
File "..\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 3246, in create_op
self._check_not_finalized()
File "..\lib\site-packages\tensorflow\python\framework\ops.py", line 2919, in _check_not_finalized
raise RuntimeError("Graph is finalized and cannot be modified.")
RuntimeError: Graph is finalized and cannot be modified.
[[node map_1/while/PyFunc (defined at ..\Lib\site-packages\tensorflow\models\research\object_detection\utils\visualization_utils.py:439) = PyFunc[Tin=[DT_UINT8, DT_FLOAT, DT_INT64, DT_FLOAT], Tout=[DT_UINT8], _class=["loc:#map_1/while/TensorArrayWrite/TensorArrayWriteV3"], token="pyfunc_1", _device="/job:localhost/replica:0/task:0/device:CPU:0"](map_1/while/Squeeze/_2907, map_1/while/TensorArrayReadV3_3/_2909, map_1/while/TensorArrayReadV3_4/_2911, map_1/while/TensorArrayReadV3_5/_2913)]]
It appears that in your case distances is a tensor and calling distances[0] attempts to create an array_slice operation on a finalized graph. The right way of handling that would be to feed distances tensor to your distances_np = session.run(distances) or call distances_np = distances.eval() providing necessary parameters to acquire some sort of numpy array and only then do indexing and/or printing on distances_np.

Categories

Resources