无法使用我的 TensorFlow 模型进行分类:FailedPreconditionError
Cannot classify with my TensorFlow model: FailedPreconditionError
我正尝试在多 class class 化问题中使用 TensorFlow 模型 class 化输入图像。我想绘制前 10 个最高预测 class 的概率。我通过以下步骤进行操作:
1.加载模型
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。准备函数 return logits 和概率
#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。 classify 和 return top10 概率
的函数
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。准备要class化的图像并class化它
#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)
但是,每当我 运行 它时,我都会遇到以下错误:
---------------------------------------------------------------------------
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()
我的代码有什么问题?模型没有初始化?如何解决?
P.s1= 我可以用输入图像正常调用函数 resnet2,它 return 既有 logits 又有 probs。问题是当我 运行 sess.run()
P.s2=使用的模型和图片可以找到HERE
由于您正在使用 tfc.InteractiveSession()
初始化 tensorflow(版本 < 2)会话,因此您可能需要在 运行 通过调用以下方式初始化会话之前初始化所有值:
tfc.initialize_all_variables().run()
这应该初始化所有变量。这也支持你的情况,你可以单独使用 运行 Resnet,但不能使用 sess.run().
我正尝试在多 class class 化问题中使用 TensorFlow 模型 class 化输入图像。我想绘制前 10 个最高预测 class 的概率。我通过以下步骤进行操作:
1.加载模型
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。准备函数 return logits 和概率
#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。 classify 和 return top10 概率
的函数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。准备要class化的图像并class化它
#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)
但是,每当我 运行 它时,我都会遇到以下错误:
---------------------------------------------------------------------------
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()
我的代码有什么问题?模型没有初始化?如何解决?
P.s1= 我可以用输入图像正常调用函数 resnet2,它 return 既有 logits 又有 probs。问题是当我 运行 sess.run()
P.s2=使用的模型和图片可以找到HERE
由于您正在使用 tfc.InteractiveSession()
初始化 tensorflow(版本 < 2)会话,因此您可能需要在 运行 通过调用以下方式初始化会话之前初始化所有值:
tfc.initialize_all_variables().run()
这应该初始化所有变量。这也支持你的情况,你可以单独使用 运行 Resnet,但不能使用 sess.run().