Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while reading resource variable dense_2/bias from Container: localhost. This could mean that the variable was uninitialized. #1117

Closed
maithal opened this issue Oct 1, 2019 · 7 comments

Comments

@maithal
Copy link

maithal commented Oct 1, 2019

Add any other context or screenshots about the feature request here.
Hello everyone,

I tried changing my model from keras to tensorflow based convnet. Now I am getting a new error.
Following is my code:

import tensorflow as tf
import numpy as np
import keras
from tensorflow.keras import layers
from keras import backend
from cleverhans.attacks import FastGradientMethod
#from cleverhans.attacks import BasicIterativeMethod
from cleverhans.utils_keras import KerasModelWrapper

from matplotlib import pyplot as plt
#import imageio

Set the matplotlib figure size

plt.rc('figure', figsize = (12.0, 12.0))

Set the learning phase to false, the model is pre-trained.

backend.set_learning_phase(False)

'''
Splitting the provided training data to create new training
data set and new validation data set. These will be used in
hyperparameter tuning
'''

Use the same seed to get same validation set

seed = 27

raw_data = pd.read_csv('train.csv')
train, validation = train_test_split(raw_data,
test_size = 0.1,
random_state= seed,
stratify= raw_data['label'])
#Split into input (X) and output (y) variable
x_validation = validation.values[:,1:].reshape(4200,28,28,1)
y_validation = validation.values[:,0]

#Set TF random seed to improve reproducibility
tf.set_random_seed(1234)

if not hasattr(backend, "tf"):
raise RuntimeError("This tutorial requires keras to be configured to use the TensorFlow backend.")

if keras.backend.image_dim_ordering() != 'tf':
keras.backend.image_dim_ordering('tf')
print("INFO: '-/.keras' sets 'image_dim_ordering' to 'th', temprorarily settilng to 'tf'")

#Retrieve the tensorflow session
sess = backend.get_session()

#Define input TF placeholder
x= tf.placeholder(tf.float32, shape= (None, 28, 28,1))
y= tf.placeholder(tf.float32, shape= (None, 10))

#Evaluate the model's accuracy on the validation data used in training
x_validation = x_validation.astype('float32')
x_validation /= 255

pred = np.argmax(model.predict(x_validation), axis=1)
acc = np.mean(np.equal(pred, y_validation))

print("The normal validation accuracy is : {}".format(acc))

#Initialize the Fast Gradient Sign Method (FGSM) attack object and
#use it to create adversarials example as numpy arrays
wrap = KerasModelWrapper(model)
fgsm = FastGradientMethod(wrap, sess= sess)
fgsm_params = {'eps': 0.3,
'clip_min': 0.,
'clip_max': 1.}
adv_x = fgsm.generate_np(x_validation, **fgsm_params)

adv_pred = np.argmax(model.predict(adv_x), axis=1)
adv_acc = np.mean(np.equal(adv_pred, y_validation))

print("The adversarial validation acuracy is: {}".format(adv_acc))

The error I am getting is as follows:
Traceback (most recent call last):

File "", line 1, in
adv_x = fgsm.generate_np(x_validation, **fgsm_params)

File "c:\users\maithal1\src\cleverhans\cleverhans\attacks\attack.py", line 200, in generate_np
return self.sess.run(x_adv, feed_dict)

File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 877, in run
run_metadata_ptr)

File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1100, in _run
feed_dict_tensor, options, run_metadata)

File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1272, in _do_run
run_metadata)

File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1291, in _do_call
raise type(e)(node_def, op, message)

FailedPreconditionError: Error while reading resource variable dense_2/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/dense_2/bias)
[[Node: model/dense_2/BiasAdd/ReadVariableOp = ReadVariableOpdtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]]
[[Node: Cast_4/_11 = _Recvclient_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_18_Cast_4", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

Caused by op 'model/dense_2/BiasAdd/ReadVariableOp', defined at:
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\runpy.py", line 193, in _run_module_as_main
"main", mod_spec)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\runpy.py", line 85, in run_code
exec(code, run_globals)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\spyder_kernels\console_main
.py", line 11, in
start.main()
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\spyder_kernels\console\start.py", line 296, in main
kernel.start()
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\ipykernel\kernelapp.py", line 499, in start
self.io_loop.start()
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tornado\platform\asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\asyncio\base_events.py", line 421, in run_forever
self._run_once()
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\asyncio\base_events.py", line 1425, in _run_once
handle._run()
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\asyncio\events.py", line 127, in _run
self._callback(*self._args)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tornado\platform\asyncio.py", line 122, in _handle_events
handler_func(fileobj, events)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tornado\stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\zmq\eventloop\zmqstream.py", line 450, in _handle_events
self._handle_recv()
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\zmq\eventloop\zmqstream.py", line 480, in _handle_recv
self._run_callback(callback, msg)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\zmq\eventloop\zmqstream.py", line 432, in _run_callback
callback(*args, **kwargs)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tornado\stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\ipykernel\kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\ipykernel\ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\ipykernel\zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 2662, in run_cell
raw_cell, store_history, silent, shell_futures)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 2785, in _run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 2901, in run_ast_nodes
if self.run_code(code, result):
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in
adv_x = fgsm.generate_np(x_validation, **fgsm_params)
File "c:\users\maithal1\src\cleverhans\cleverhans\attacks\attack.py", line 186, in generate_np
self.construct_graph(fixed, feedable, x_val, hash_key)
File "c:\users\maithal1\src\cleverhans\cleverhans\attacks\attack.py", line 158, in construct_graph
x_adv = self.generate(x, **new_kwargs)
File "c:\users\maithal1\src\cleverhans\cleverhans\attacks\fast_gradient_method.py", line 50, in generate
labels, _nb_classes = self.get_or_guess_labels(x, kwargs)
File "c:\users\maithal1\src\cleverhans\cleverhans\attacks\attack.py", line 278, in get_or_guess_labels
preds = self.model.get_probs(x)
File "c:\users\maithal1\src\cleverhans\cleverhans\utils_keras.py", line 188, in get_probs
return self.get_layer(x, name)
File "c:\users\maithal1\src\cleverhans\cleverhans\utils_keras.py", line 249, in get_layer
output = self.fprop(x)
File "c:\users\maithal1\src\cleverhans\cleverhans\utils_keras.py", line 228, in fprop
outputs = self.keras_model(x)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 736, in call
outputs = self.call(inputs, *args, **kwargs)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\network.py", line 777, in call
mask=masks)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\network.py", line 948, in _run_internal_graph
layer.call(computed_tensor, **kwargs))
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\keras\layers\core.py", line 945, in call
outputs = nn.bias_add(outputs, self.bias)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1505, in bias_add
bias = ops.convert_to_tensor(bias, dtype=value.dtype, name="bias")
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 998, in convert_to_tensor
as_ref=False)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 1094, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 1045, in _dense_var_to_tensor
return var._dense_var_to_tensor(dtype=dtype, name=name, as_ref=as_ref) # pylint: disable=protected-access
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 1000, in _dense_var_to_tensor
return self.value()
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 662, in value
return self._read_variable_op()
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 745, in _read_variable_op
self._dtype)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\ops\gen_resource_variable_ops.py", line 507, in read_variable_op
"ReadVariableOp", resource=resource, dtype=dtype, name=name)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\util\deprecation.py", line 454, in new_func
return func(*args, **kwargs)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 3155, in create_op
op_def=op_def)
File "C:\Users\maithal1\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 1717, in init
self._traceback = tf_stack.extract_stack()

FailedPreconditionError (see above for traceback): Error while reading resource variable dense_2/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/dense_2/bias)
[[Node: model/dense_2/BiasAdd/ReadVariableOp = ReadVariableOpdtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]]
[[Node: Cast_4/_11 = _Recvclient_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_18_Cast_4", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

Can someone please guide me through this issue.

Thanks!

@iamgroot42
Copy link
Contributor

Just add the following before creating the keras-model wrapper:

session = keras.backend.get_session()
init = tf.global_variables_initializer()
session.run(init)

@amitnjha
Copy link

I am getting this error as well.
I am running a MNIST based digit recognition app on a raspberry pi.

print(tensorflow.version)
1.13.1
print(keras.version)
2.3.1

@amitnjha
Copy link

Solution posted below worked.

tensorflow/tensorflow#28287 (comment)

@npapernot
Copy link
Member

Could you make a PR to add the fix to any relevant tutorials?

@soanduong
Copy link

Just add the following before creating the keras-model wrapper:

session = keras.backend.get_session()
init = tf.global_variables_initializer()
session.run(init)

Thanks. It works for my case (optimizing the hyper-parameters using hyperopt).

@juan-csv
Copy link

juan-csv commented Jul 2, 2020

Could you make a PR to add the fix to any relevant tutorials?

thanks!

@Sheng-Kai-IBM
Copy link

Just add the following before creating the keras-model wrapper:

session = keras.backend.get_session()
init = tf.global_variables_initializer()
session.run(init)

Thank you so much!! You just save my day 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants