Tensorflow пытается использовать неинициализированное значение после инициализации
Я получаю сообщение об ошибке "Попытка использовать неинициализированное значение", когда пытаюсь написать что-то, что выполнит один прямой проход (для прогнозирования), без изменений в коде из https://github.com/ry/tensorflow-resnet. Это код, который должен прочитать файл tfrecords и сделать для него прогноз:
import numpy as np
from resnet import *
import tensorflow as tf
sess = tf.Session()
saver = tf.train.import_meta_graph('/tmp/resnet_train/model.ckpt-85801.meta')
saver.restore(sess, "/tmp/resnet_train/model.ckpt-85801")
init = tf.global_variables_initializer()
init_l = tf.local_variables_initializer()
sess.run(init)
sess.run(init_l)
print("graph restored")
feature = {'train/video': tf.FixedLenFeature([], tf.string),
'train/label': tf.FixedLenFeature([], tf.string)}
def mv2(name, start, end):
avgfgpct = 0.
avgthrpct = 0.
avgftpct = 0.
avgefgpct = 0.
for i in range(start, end):
tf_record = '/home/ubuntu/NBANet-test/%s-%03d.tfrecords' %(name, i)
s = next(tf.python_io.tf_record_iterator(tf_record))
features = tf.parse_single_example(s, features=feature)
video = tf.decode_raw(features['train/video'], tf.float32)
label = tf.decode_raw(features['train/label'], tf.float32)
video = tf.cond(tf.size(video) < tf.constant(5358240), lambda: tf.convert_to_tensor(np.zeros([244,244,90]).astype(np.float32), np.float32), lambda: video)
video = tf.reshape(video, [244, 244, 90, 1])
videos = tf.stack([video, video, video, video])
label = tf.reshape(label, [4])
is_training = tf.placeholder('bool', [], name='is_training')
predictions = inference(videos, num_classes=4,is_training=is_training)
_loss = loss(predictions, label)
sess.run([_loss], {is_training: False})
У меня есть куча звонков в mv2, и все имена файлов действительны. Ошибка, которую я получаю, такова:
(tensorflow_p36) ubuntu@ip-172-31-14-68:~/NBANet-py/NBANet$ python NBANet-test.py
/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
return f(*args, **kwds)
2017-12-05 21:30:05.774989: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2017-12-05 21:30:05.864591: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2017-12-05 21:30:05.864929: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: Tesla K80 major: 3 minor: 7 memoryClockRate(GHz): 0.8235
pciBusID: 0000:00:1e.0
totalMemory: 11.17GiB freeMemory: 11.10GiB
2017-12-05 21:30:05.864962: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: Tesla K80, pci bus id: 0000:00:1e.0, compute capability: 3.7)
graph restored
2017-12-05 21:30:15.707131: W tensorflow/core/framework/op_kernel.cc:1192] Failed precondition: Attempting to use uninitialized value scale5/block1/B/beta_1
[[Node: scale5/block1/B/beta_1/read = Identity[T=DT_FLOAT, _class=["loc:@scale5/block1/B/beta_1"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](scale5/block1/B/beta_1)]]
Traceback (most recent call last):
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1323, in _do_call
return fn(*args)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1302, in _run_fn
status, run_metadata)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value scale5/block1/B/beta_1
[[Node: scale5/block1/B/beta_1/read = Identity[T=DT_FLOAT, _class=["loc:@scale5/block1/B/beta_1"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](scale5/block1/B/beta_1)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "NBANet-test.py", line 40, in <module>
mv2("Knicks", 412, 463)
File "NBANet-test.py", line 37, in mv2
sess.run([_loss], {is_training: False})
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 889, in run
run_metadata_ptr)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1120, in _run
feed_dict_tensor, options, run_metadata)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1317, in _do_run
options, run_metadata)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1336, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value scale5/block1/B/beta_1
[[Node: scale5/block1/B/beta_1/read = Identity[T=DT_FLOAT, _class=["loc:@scale5/block1/B/beta_1"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](scale5/block1/B/beta_1)]]
Caused by op 'scale5/block1/B/beta_1/read', defined at:
File "NBANet-test.py", line 40, in <module>
mv2("Knicks", 412, 463)
File "NBANet-test.py", line 35, in mv2
predictions = inference(videos, num_classes=4,is_training=is_training)
File "/home/ubuntu/NBANet-py/NBANet/resnet.py", line 78, in inference
x = stack(x, c)
File "/home/ubuntu/NBANet-py/NBANet/resnet.py", line 165, in stack
x = block(x, c)
File "/home/ubuntu/NBANet-py/NBANet/resnet.py", line 213, in block
x = bn(x, c)
File "/home/ubuntu/NBANet-py/NBANet/resnet.py", line 232, in bn
initializer=tf.zeros_initializer)
File "/home/ubuntu/NBANet-py/NBANet/resnet.py", line 301, in _get_variable
trainable=trainable)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 1203, in get_variable
constraint=constraint)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 1092, in get_variable
constraint=constraint)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 425, in get_variable
constraint=constraint)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 394, in _true_getter
use_resource=use_resource, constraint=constraint)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 805, in _get_single_variable
constraint=constraint)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 213, in __init__
constraint=constraint)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 356, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 125, in identity
return gen_array_ops.identity(input, name=name)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2071, in identity
"Identity", input=input, name=name)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2956, in create_op
op_def=op_def)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value scale5/block1/B/beta_1
[[Node: scale5/block1/B/beta_1/read = Identity[T=DT_FLOAT, _class=["loc:@scale5/block1/B/beta_1"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](scale5/block1/B/beta_1)]]
Итак, я позвонил моим инициализаторам. Кроме того, в какой-то момент в код были включены строки для этого:
graph = tf.get_default_graph()
f = open('/tmp/resnet_train/ops.txt', 'w')
for op in graph.get_operations():
f.write('%s\n' %(op.name))
f.close()
так что я мог видеть, какие у меня были тензоры, и после проверки как кода (который получен из https://github.com/ry/tensorflow-resnet и модифицированного для 3D-тензоров и более короткого графика), так и файла ops, тензора с именем scale5/block1/B/beta_1 даже не должно существовать; это должен быть scale5/block1/B/beta (который существует в файле ops), который должен быть найден. Сейчас я в растерянности из-за того, что я делаю неправильно.