Я использую одноканальные изображения PNG для прогнозирования. Я использую код ниже для вывода, но я получаю FailedPreconditionError: Попытка использовать неинициализированное значение InceptionV3 / Mixed_7c / Branch_0 / Conv2d_0a_1x1 / weights . Я пытался решить с помощью существующих решений, упомянутых в StackOverflow, я должен инициализировать глобальные переменные tf.global_variables_initializer()
. Любой вид помощи будет очень признателен. Спасибо
Фрагмент кода
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import re
import sys
import tarfile
import argparse
import datetime
from six.moves import urllib
import tensorflow as tf
import os
from tensorflow.python.platform import gfile
from PIL import Image
import numpy as np
import scipy
from scipy import misc
import matplotlib.pyplot as plt
import cv2
import pdb
import imageio
with tf.Graph().as_default() as graph: # Set default graph as graph
with tf.Session() as sess:
# Load the graph in graph_def
print("load graph")
# We load the protobuf file from the disk and parse it to retrive the unserialized graph_drf
with gfile.FastGFile("/path_to_pb_file/mobilenet_graph.pb",'rb') as f:
print("Load Image...")
# Read the image & get statstics
image = imageio.imread('/path_to_the_image_file_for_prediction/image.png')
#pdb.set_trace()
image = image.astype(float)
image=image.reshape(-1, *image.shape, 1)
print(image.shape)
#image = image.reshape(-1, 224, 224, 1)
#Input_image_shape=image.shape
#height,width,channels = Input_image_shape
print("Plot image...")
#scipy.misc.imshow(image)
# Set FCN graph to the default graph
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
sess.graph.as_default()
# Import a graph_def into the current default Graph (In this case, the weights are (typically) embedded in the graph)
tf.import_graph_def(
graph_def,
input_map=None,
return_elements=None,
name="",
op_dict=None,
producer_op_list=None
)
# Print the name of operations in the session
#for op in graph.get_operations():
#print ("Operation Name :",op.name) # Operation name
#print ("Tensor Stats :",str(op.values())) # Tensor name
# INFERENCE Here
l_input = graph.get_tensor_by_name('input:0') # Input Tensor
l_output = graph.get_tensor_by_name('InceptionV3/Predictions/Reshape_1:0') # Output Tensor
print ("Shape of input : ", tf.shape(l_input))
#initialize_all_variables
tf.global_variables_initializer()
# Run Kitty model on single image
Session_out = sess.run(l_output, feed_dict = {l_input:image} )
Ошибка
Shape of input : Tensor("Shape:0", shape=(4,), dtype=int32)
Traceback (most recent call last):
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call
return fn(*args)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/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 InceptionV3/Mixed_6b/Branch_1/Conv2d_0b_1x7/BatchNorm/beta
[[{{node InceptionV3/Mixed_6b/Branch_1/Conv2d_0b_1x7/BatchNorm/beta/read}} = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](InceptionV3/Mixed_6b/Branch_1/Conv2d_0b_1x7/BatchNorm/beta)]]
[[{{node InceptionV3/Predictions/Reshape_1/_3}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_1579_InceptionV3/Predictions/Reshape_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test_inference_monjoy.py", line 86, in <module>
Session_out = sess.run(l_output, feed_dict = {l_input:image} )
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run
run_metadata_ptr)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1152, in _run
feed_dict_tensor, options, run_metadata)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1328, in _do_run
run_metadata)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/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 InceptionV3/Mixed_6b/Branch_1/Conv2d_0b_1x7/BatchNorm/beta
[[node InceptionV3/Mixed_6b/Branch_1/Conv2d_0b_1x7/BatchNorm/beta/read (defined at test_inference_monjoy.py:63) = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](InceptionV3/Mixed_6b/Branch_1/Conv2d_0b_1x7/BatchNorm/beta)]]
[[{{node InceptionV3/Predictions/Reshape_1/_3}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_1579_InceptionV3/Predictions/Reshape_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'InceptionV3/Mixed_6b/Branch_1/Conv2d_0b_1x7/BatchNorm/beta/read', defined at:
File "test_inference_monjoy.py", line 63, in <module>
producer_op_list=None
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 442, in import_graph_def
_ProcessNewOps(graph)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 234, in _ProcessNewOps
for new_op in graph._add_new_tf_operations(compute_devices=False): # pylint: disable=protected-access
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3440, in _add_new_tf_operations
for c_op in c_api_util.new_tf_operations(self)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3440, in <listcomp>
for c_op in c_api_util.new_tf_operations(self)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3299, in _create_op_from_tf_operation
ret = Operation(c_op, self)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1770, in __init__
self._traceback = tf_stack.extract_stack()
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value InceptionV3/Mixed_6b/Branch_1/Conv2d_0b_1x7/BatchNorm/beta
[[node InceptionV3/Mixed_6b/Branch_1/Conv2d_0b_1x7/BatchNorm/beta/read (defined at test_inference_monjoy.py:63) = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](InceptionV3/Mixed_6b/Branch_1/Conv2d_0b_1x7/BatchNorm/beta)]]
[[{{node InceptionV3/Predictions/Reshape_1/_3}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_1579_InceptionV3/Predictions/Reshape_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]