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
CodePudding user response:
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().