Keras witf tensorflow optimizer - tensorflow

I am trying to optimize a Keras model with a Tensorflow backend optimizer.
I can run the model, calculate a loss, and print all the weights and biases, but when I try to apply the gradient the model crashes.
PS: I am using a tensorflow Cuda docker container and the host system is Ubuntu 18.04.2 LTS.
Code:
import numpy as np
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Dense, Input
from tensorflow.python.keras.optimizers import Adam
from tensorflow.python.keras import backend as K
import tensorflow as tf
data = np.random.uniform(low=0, high=5, size=50)
Y_Data = np.random.randint(low=0, high=3, size=(50, 3))
config = tf.ConfigProto(allow_soft_placement=True)
sess = tf.Session(config=config)
K.set_session(sess)
input = Input(shape=(1, ))
dens_1 = Dense(128, activation='relu')(input)
output = Dense(3, activation='linear')(dens_1)
Y_data_placeholder = tf.placeholder(tf.float32, shape=[None, 3])
loss = tf.losses.absolute_difference(Y_data_placeholder, output)
params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
grads = tf.gradients(loss, params)
grads = list(zip(grads, params))
trainer = tf.train.RMSPropOptimizer(learning_rate=0.003, decay=0.99, epsilon=1e-5)
_train = trainer.apply_gradients(grads)
model = Model(inputs=input, outputs=output)
adam = Adam(lr=0.001)
model.compile(loss="mse", optimizer=adam)
model.summary()
print('')
print(model.predict(data)) # Runs fine.
print('')
print(sess.run(output, feed_dict={input: data.reshape(50, 1)})) # Runs fine.
out_data, out_loss, out_params = sess.run([output, loss, params], feed_dict={input: data.reshape(50, 1), Y_data_placeholder: Y_Data}) # Runs fine.
print('')
print(out_params)
print('')
print(out_data)
print('')
print(out_loss)
out_data, out_loss, out_train = sess.run([output, loss, _train], feed_dict={input: data.reshape(50, 1), Y_data_placeholder: Y_Data}) # Runs Error.
print('')
print(out_train)
Error:
2019-03-12 21:59:20.737232: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737326: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737359: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737436: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at training_ops.cc:2933 : Not found: Resource localhost/dense_1/bias/RMSProp/N10tensorflow3VarE does not exist.
2019-03-12 21:59:20.737524: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737569: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737605: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737647: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at training_ops.cc:2933 : Not found: Resource localhost/dense_1/kernel/RMSProp/N10tensorflow3VarE does not exist.
2019-03-12 21:59:20.737896: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737944: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737983: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.738050: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at training_ops.cc:2933 : Not found: Resource localhost/dense/bias/RMSProp/N10tensorflow3VarE does not exist.
2019-03-12 21:59:20.738110: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.738149: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.738186: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.738231: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at training_ops.cc:2933 : Not found: Resource localhost/dense/kernel/RMSProp/N10tensorflow3VarE does not exist.
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1334, in _do_call
return fn(*args)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.InternalError: Invalid variable reference.
[[{{node RMSProp/update_dense_1/bias/ResourceApplyRMSProp}} = ResourceApplyRMSProp[T=DT_FLOAT, use_locking=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](dense_1/bias, dense_1/bias/RMSProp, dense_1/bias/RMSProp_1, RMSProp/learning_rate, RMSProp/decay, RMSProp/momentum, RMSProp/epsilon, gradients/dense_1/BiasAdd_grad/BiasAddGrad)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/project/test6.py", line 50, in <module>
out_data, out_loss, out_train = sess.run([output, loss, _train], feed_dict={input: data.reshape(50, 1), Y_data_placeholder: Y_Data}) # Runs Error.
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 929, in run
run_metadata_ptr)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1152, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1328, in _do_run
run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1348, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InternalError: Invalid variable reference.
[[node RMSProp/update_dense_1/bias/ResourceApplyRMSProp (defined at /opt/project/test6.py:31) = ResourceApplyRMSProp[T=DT_FLOAT, use_locking=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](dense_1/bias, dense_1/bias/RMSProp, dense_1/bias/RMSProp_1, RMSProp/learning_rate, RMSProp/decay, RMSProp/momentum, RMSProp/epsilon, gradients/dense_1/BiasAdd_grad/BiasAddGrad)]]
Caused by op 'RMSProp/update_dense_1/bias/ResourceApplyRMSProp', defined at:
File "/opt/project/test6.py", line 31, in <module>
_train = trainer.apply_gradients(grads)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 610, in apply_gradients
update_ops.append(processor.update_op(self, grad))
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 167, in update_op
update_op = optimizer._resource_apply_dense(g, self._v)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/rmsprop.py", line 194, in _resource_apply_dense
use_locking=self._use_locking)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/gen_training_ops.py", line 2073, in resource_apply_rms_prop
use_locking=use_locking, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3274, in create_op
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1770, in __init__
self._traceback = tf_stack.extract_stack()
InternalError (see above for traceback): Invalid variable reference.
[[node RMSProp/update_dense_1/bias/ResourceApplyRMSProp (defined at /opt/project/test6.py:31) = ResourceApplyRMSProp[T=DT_FLOAT, use_locking=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](dense_1/bias, dense_1/bias/RMSProp, dense_1/bias/RMSProp_1, RMSProp/learning_rate, RMSProp/decay, RMSProp/momentum, RMSProp/epsilon, gradients/dense_1/BiasAdd_grad/BiasAddGrad)]]

I fight it out...
After defining the model, loss and Tensorflow optimizer, I need to initialize all variables first.
sess.run(tf.global_variables_initializer())

Related

Tensorflow not finding files

So as I try to run my code it keeps saying that it can't find an image but it in fact is there. I've tried a lot of things but nothing works. Can someone please help me? This is code is trying to read images from the nist dataset and then i am trying to train a model. Here's my code and error code:
import os
import cv2
import tensorflow as tf
upper_level_dirs = open("/Users/cam/reader/top_level_dirs")
upper_level_dirs = upper_level_dirs.read().split()
print(upper_level_dirs)
file_names = []
labels = []
for folder in upper_level_dirs:
for filename in os.listdir("./train/" + folder + "/"):
file_names.append("./train/" + folder + "/" + filename)
labels.append(folder)
# Use a custom OpenCV function to read the image, instead of the standard
# TensorFlow `tf.read_file()` operation.
def _read_py_function(file_name, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_image(image_string, channels=3)
image_resized = tf.image.resize_image_with_crop_or_pad(image_decoded, 28, 28)
return image_resized, label
# image_decoded = cv2.imread(file_name.decode(), cv2.IMREAD_GRAYSCALE)
# return image_decoded, label
dataset = tf.data.Dataset.from_tensor_slices((file_names, labels))
dataset = dataset.map(_read_py_function)
sess = tf.InteractiveSession()
dataset = dataset.batch(32)
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()
for _ in range(100):
sess.run(iterator.initializer)
while True:
try:
sess.run(next_element)
except tf.errors.OutOfRangeError:
break
Errors:
2018-03-27 12:59:58.001455: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-03-27 13:00:01.574038: W tensorflow/core/framework/op_kernel.cc:1278] OP_REQUIRES failed at whole_file_read_ops.cc:114 : Not found: train_7a_02017.png; No such file or directory
2018-03-27 13:00:01.576585: W tensorflow/core/framework/op_kernel.cc:1278] OP_REQUIRES failed at whole_file_read_ops.cc:114 : Not found: train_7a_02017.png; No such file or directory
2018-03-27 13:00:01.578373: W tensorflow/core/framework/op_kernel.cc:1278] OP_REQUIRES failed at whole_file_read_ops.cc:114 : Not found: train_7a_02017.png; No such file or directory
Traceback (most recent call last):
File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1330, in _do_call
return fn(*args)
File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1315, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1423, in _call_tf_sessionrun
status, run_metadata)
File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 516, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.NotFoundError: train_7a_02017.png; No such file or directory
[[Node: ReadFile = ReadFile[](ReadFile/filename)]]
[[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,28,28,?], [?]], output_types=[DT_UINT8, DT_STRING], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "split2.py", line 46, in <module>
sess.run(next_element)
File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 908, in run
run_metadata_ptr)
File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1143, in _run
feed_dict_tensor, options, run_metadata)
File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1324, in _do_run
run_metadata)
File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1343, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: train_7a_02017.png; No such file or directory
[[Node: ReadFile = ReadFile[](ReadFile/filename)]]
[[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,28,28,?], [?]], output_types=[DT_UINT8, DT_STRING], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator)]]
I also find that error and my solution is:
Maybe you create the path by windows and the path end with 'CR' when you view the txt with notepad++, which cant read by linux.
You can replace 'CR' with '' by notepad++. Then it works.
I had the same problem and using tf.as_str_any() solved the problem for me.
This because the file is an object and not a string, so you have to decode it (generally UTF-8) to a string.
So your code would look like this:
def _read_py_function(file_name, label):
image_string = tf.as_str_any(file_name)
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_image(image_string, channels=3)
image_resized = tf.image.resize_image_with_crop_or_pad(image_decoded, 28, 28)
return image_resized, label

Prettytensor: Attempting to use uninitialized value

I'm following these tutorials:
https://www.youtube.com/watch?v=wuo4JdG3SvU&list=PL9Hr9sNUjfsmEu1ZniY0XpHSzl5uihcXZ
and prettytensor is introduced in tutorial 4.
Following the tutorial, i wrote this code to run a small neural network:
import tensorflow as tf
# Use PrettyTensor to simplify Neural Network construction.
import prettytensor as pt
from tensorflow.examples.tutorials.mnist import input_data
data = input_data.read_data_sets('../data/MNIST/', one_hot=True)
# We know that MNIST images are 28 pixels in each dimension.
img_size = 28
# Images are stored in one-dimensional arrays of this length.
img_size_flat = img_size * img_size
# Tuple with height and width of images used to reshape arrays.
img_shape = (img_size, img_size)
# Number of colour channels for the images: 1 channel for gray-scale.
num_channels = 1
# Number of classes, one class for each of 10 digits.
num_classes = 10
# the placeholders
x = tf.placeholder(tf.float32, shape=[None, img_size_flat], name='x')
x_image = tf.reshape(x, [-1, img_size, img_size, num_channels])
y_true = tf.placeholder(tf.float32, shape=[None, 10], name='y_true')
# use prettyTensor to build the model
# this will give us the predictions and the loss functions
x_pretty = pt.wrap(x_image)
with pt.defaults_scope(activation_fn=tf.nn.relu):
y_pred, loss = x_pretty.\
conv2d(kernel=5, depth=16, name='layer_conv1').\
max_pool(kernel=2, stride=2).\
conv2d(kernel=5, depth=36, name='layer_conv2').\
max_pool(kernel=2, stride=2).\
flatten().\
fully_connected(size=128, name='layer_fc1').\
softmax_classifier(class_count=10, labels=y_true)
# the model optimizer
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(loss)
# the model testing
correct_prediction = tf.equal(tf.argmax(y_pred,1), tf.argmax(y_true,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# start the session
session = tf.InteractiveSession()
# Start the training
tf.global_variables_initializer().run(session = session)
train_batch_size = 64
for i in range(1000):
print("training batch ",i)
x_batch, y_true_batch = data.train.next_batch(train_batch_size)
session.run(optimizer, feed_dict={x:x_batch, y_true:y_true_batch})
When i tried to run it, I got the following error:
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value layer_conv1/bias
[[Node: layer_conv1/bias/read = Identity[T=DT_FLOAT, _class=["loc:#layer_conv1/bias"], _device="/job:localhost/replica:0/task:0/cpu:0"](layer_conv1/bias)]]
Caused by op u'layer_conv1/bias/read', defined at:
File "/home/gal/Documents/Workspace/EclipseWorkspace/Melanoma Classification!/tutorial4/tutorial4Test.py", line 31, in <module>
the full error trace:
Traceback (most recent call last):
File "/home/gal/Documents/Workspace/EclipseWorkspace/Melanoma Classification!/tutorial4/tutorial4Test.py", line 55, in <module>
session.run(optimizer, feed_dict={x:x_batch, y_true:y_true_batch})
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 766, in run
run_metadata_ptr)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 964, in _run
feed_dict_string, options, run_metadata)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1014, in _do_run
target_list, options, run_metadata)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1034, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value layer_conv1/bias
[[Node: layer_conv1/bias/read = Identity[T=DT_FLOAT, _class=["loc:#layer_conv1/bias"], _device="/job:localhost/replica:0/task:0/cpu:0"](layer_conv1/bias)]]
Caused by op u'layer_conv1/bias/read', defined at:
File "/home/gal/Documents/Workspace/EclipseWorkspace/Melanoma Classification!/tutorial4/tutorial4Test.py", line 31, in <module>
conv2d(kernel=5, depth=16, name='layer_conv1').\
File "/home/gal/anaconda2/lib/python2.7/site-packages/prettytensor/pretty_tensor_class.py", line 1981, in method
result = func(non_seq_layer, *args, **kwargs)
File "/home/gal/anaconda2/lib/python2.7/site-packages/prettytensor/pretty_tensor_image_methods.py", line 163, in __call__
y += self.variable('bias', [size[-1]], bias_init, dt=dtype)
File "/home/gal/anaconda2/lib/python2.7/site-packages/prettytensor/pretty_tensor_class.py", line 1695, in variable
collections=variable_collections)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1024, in get_variable
custom_getter=custom_getter)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 850, in get_variable
custom_getter=custom_getter)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 346, in get_variable
validate_shape=validate_shape)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 331, in _true_getter
caching_device=caching_device, validate_shape=validate_shape)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 677, in _get_single_variable
expected_shape=shape)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 224, in __init__
expected_shape=expected_shape)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 370, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1424, in identity
result = _op_def_lib.apply_op("Identity", input=input, name=name)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
op_def=op_def)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2240, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/gal/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1128, in __init__
self._traceback = _extract_stack()
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value layer_conv1/bias
[[Node: layer_conv1/bias/read = Identity[T=DT_FLOAT, _class=["loc:#layer_conv1/bias"], _device="/job:localhost/replica:0/task:0/cpu:0"](layer_conv1/bias)]]
So my question is, How can i solve this error?
This problem is caused by a bug in the 0.12rc0 release candidate of TensorFlow, and the fact that Pretty Tensor uses a deprecated TensorFlow API (for which I've opened an issue).
Until this bug is fixed, the best workaround I can think of is a hack. Add the following line at the top of your program, after import tensorflow as tf:
tf.GraphKeys.VARIABLES = tf.GraphKeys.GLOBAL_VARIABLES

Unable to freeze inception v1 (tf-slim) graph

After successfully running all examples from slim walkthrough notebook, I wanted to freeze the graph. In order to do that, I ran the following (copy from original notebook):
import os
from datasets import flowers
from nets import inception
from preprocessing import inception_preprocessing
slim = tf.contrib.slim
image_size = inception.inception_v1.default_image_size
def get_init_fn():
"""Returns a function run by the chief worker to warm-start the training."""
checkpoint_exclude_scopes=["InceptionV1/Logits", "InceptionV1/AuxLogits"]
exclusions = [scope.strip() for scope in checkpoint_exclude_scopes]
variables_to_restore = []
for var in slim.get_model_variables():
excluded = False
for exclusion in exclusions:
if var.op.name.startswith(exclusion):
excluded = True
break
if not excluded:
variables_to_restore.append(var)
return slim.assign_from_checkpoint_fn(
os.path.join(checkpoints_dir, 'inception_v1.ckpt'),
variables_to_restore)
train_dir = '/tmp/inception_finetuned/'
with tf.Graph().as_default():
tf.logging.set_verbosity(tf.logging.INFO)
dataset = flowers.get_split('train', flowers_data_dir)
images, _, labels = load_batch(dataset, height=image_size, width=image_size)
# Create the model, use the default arg scope to configure the batch norm parameters.
with slim.arg_scope(inception.inception_v1_arg_scope()):
logits, _ = inception.inception_v1(images, num_classes=dataset.num_classes, is_training=True)
# Specify the loss function:
one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes)
slim.losses.softmax_cross_entropy(logits, one_hot_labels)
total_loss = slim.losses.get_total_loss()
# Create some summaries to visualize the training process:
tf.scalar_summary('losses/Total Loss', total_loss)
# Specify the optimizer and create the train op:
optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
train_op = slim.learning.create_train_op(total_loss, optimizer)
# Run the training:
final_loss = slim.learning.train(
train_op,
logdir=train_dir,
init_fn=get_init_fn(),
number_of_steps=2)
print('Finished training. Last batch loss %f' % final_loss)
The code above produced the following files in /tmp/inception_finetuned folder:
checkpoint
model.ckpt-0.meta
events.out.tfevents.1478081437.Nikos-MacBook-Pro.local
model.ckpt-2 graph.pbtxt
model.ckpt-2.meta model.ckpt-0
Then, in order to freeze the graph, I ran the following command:
bazel-bin/tensorflow/python/tools/freeze_graph --input_graph=/tmp/inception_finetuned/graph.pbtxt --input_checkpoint=/tmp/inception_finetuned/model.ckpt-2 --output_graph=/tmp/freeze.pb --output_node_names=InceptionV1/Logits/Predictions/Softmax
The command, however, produced the following error:
W tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1
[[Node: _send_InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1_0 = _Send[T=DT_FLOAT, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=6007788667487390928, tensor_name="InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1:0", _device="/job:localhost/replica:0/task:0/cpu:0"](InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1)]]
...
W tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1
[[Node: _send_InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1_0 = _Send[T=DT_FLOAT, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=6007788667487390928, tensor_name="InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1:0", _device="/job:localhost/replica:0/task:0/cpu:0"](InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1)]]
Traceback (most recent call last):
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 135, in <module>
tf.app.run()
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/platform/app.py", line 32, in run
sys.exit(main(sys.argv[:1] + flags_passthrough))
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 132, in main
FLAGS.output_graph, FLAGS.clear_devices, FLAGS.initializer_nodes)
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 121, in freeze_graph
sess, input_graph_def, output_node_names.split(","))
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/framework/graph_util.py", line 226, in convert_variables_to_constants
returned_variables = sess.run(variable_names)
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 717, in run
run_metadata_ptr)
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 915, in _run
feed_dict_string, options, run_metadata)
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 965, in _do_run
target_list, options, run_metadata)
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 985, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1
[[Node: _send_InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1_0 = _Send[T=DT_FLOAT, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=6007788667487390928, tensor_name="InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1:0", _device="/job:localhost/replica:0/task:0/cpu:0"](InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1)]]
Then I tried to use different optimizer:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
and got the following error:
W tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value global_step
[[Node: _send_global_step_0 = _Send[T=DT_INT64, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=8900174487477528080, tensor_name="global_step:0", _device="/job:localhost/replica:0/task:0/cpu:0"](global_step)]]
...
[[Node: _send_global_step_0 = _Send[T=DT_INT64, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=8900174487477528080, tensor_name="global_step:0", _device="/job:localhost/replica:0/task:0/cpu:0"](global_step)]]
W tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value global_step
[[Node: _send_global_step_0 = _Send[T=DT_INT64, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=8900174487477528080, tensor_name="global_step:0", _device="/job:localhost/replica:0/task:0/cpu:0"](global_step)]]
Traceback (most recent call last):
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 135, in <module>
tf.app.run()
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/platform/app.py", line 32, in run
sys.exit(main(sys.argv[:1] + flags_passthrough))
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 132, in main
FLAGS.output_graph, FLAGS.clear_devices, FLAGS.initializer_nodes)
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 121, in freeze_graph
sess, input_graph_def, output_node_names.split(","))
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/framework/graph_util.py", line 226, in convert_variables_to_constants
returned_variables = sess.run(variable_names)
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 717, in run
run_metadata_ptr)
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 915, in _run
feed_dict_string, options, run_metadata)
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 965, in _do_run
target_list, options, run_metadata)
File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 985, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value global_step
[[Node: _send_global_step_0 = _Send[T=DT_INT64, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=8900174487477528080, tensor_name="global_step:0", _device="/job:localhost/replica:0/task:0/cpu:0"](global_step)]]
Similarly, if I retrain the model running the following command:
python train_image_classifier.py \
--train_dir=${TRAIN_DIR} \
--dataset_dir=${DATASET_DIR} \
--dataset_name=flowers \
--dataset_split_name=train \
--model_name=inception_v1 \
--checkpoint_path=${CHECKPOINT_PATH} \
--checkpoint_exclude_scopes=InceptionV1/Logits,InceptionV1/AuxLogits/Logits \
--trainable_scopes=InceptionV1/Logits,InceptionV1/AuxLogits/Logits
and try to freeze the graph, get the errors related to
global_step
Does anyone know why the above errors occur and how to solve them? If anyone managed to freeze inception v1 (tf-slim) graph, I would be thankful for any suggestions that might solve the issue.

FailedPreconditionError while trying to use RMSPropOptimizer on tensorflow

I am trying to use the RMSPropOptimizer for minimizing loss. Here's the part of the code that is relevant:
import tensorflow as tf
#build large convnet...
#...
opt = tf.train.RMSPropOptimizer(learning_rate=0.0025, decay=0.95)
#do stuff to get targets and loss...
#...
grads_and_vars = opt.compute_gradients(loss)
capped_grads_and_vars = [(tf.clip_by_value(g, -1, 1), v) for g, v in grads_and_vars]
opt_op = self.opt.apply_gradients(capped_grads_and_vars)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
while(1):
sess.run(opt_op)
Problem is as soon as I run this I get the following error:
W tensorflow/core/common_runtime/executor.cc:1091] 0x10a0bba40 Compute status: Failed precondition: Attempting to use uninitialized value train/output/bias/RMSProp
[[Node: RMSProp/update_train/output/bias/ApplyRMSProp = ApplyRMSProp[T=DT_FLOAT, use_locking=false, _device="/job:localhost/replica:0/task:0/cpu:0"](train/output/bias, train/output/bias/RMSProp, train/output/bias/RMSProp_1, RMSProp/learning_rate, RMSProp/decay, RMSProp/momentum, RMSProp/epsilon, clip_by_value_9)]]
[[Node: _send_MergeSummary/MergeSummary_0 = _Send[T=DT_STRING, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=-6901001318975381332, tensor_name="MergeSummary/MergeSummary:0", _device="/job:localhost/replica:0/task:0/cpu:0"](MergeSummary/MergeSummary)]]
Traceback (most recent call last):
File "dqn.py", line 213, in <module>
result = sess.run(opt_op)
File "/Users/home/miniconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 385, in run
results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
File "/Users/home/miniconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 461, in _do_run
e.code)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value train/output/bias/RMSProp
[[Node: RMSProp/update_train/output/bias/ApplyRMSProp = ApplyRMSProp[T=DT_FLOAT, use_locking=false, _device="/job:localhost/replica:0/task:0/cpu:0"](train/output/bias, train/output/bias/RMSProp, train/output/bias/RMSProp_1, RMSProp/learning_rate, RMSProp/decay, RMSProp/momentum, RMSProp/epsilon, clip_by_value_9)]]
Caused by op u'RMSProp/update_train/output/bias/ApplyRMSProp', defined at:
File "dqn.py", line 159, in qLearnMinibatch
opt_op = self.opt.apply_gradients(capped_grads_and_vars)
File "/Users/home/miniconda2/lib/python2.7/site-packages/tensorflow/python/training/optimizer.py", line 288, in apply_gradients
update_ops.append(self._apply_dense(grad, var))
File "/Users/home/miniconda2/lib/python2.7/site-packages/tensorflow/python/training/rmsprop.py", line 103, in _apply_dense
grad, use_locking=self._use_locking).op
File "/Users/home/miniconda2/lib/python2.7/site-packages/tensorflow/python/training/gen_training_ops.py", line 171, in apply_rms_prop
grad=grad, use_locking=use_locking, name=name)
File "/Users/home/miniconda2/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 659, in apply_op
op_def=op_def)
File "/Users/home/miniconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1904, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Users/home/miniconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1083, in __init__
self._traceback = _extract_stack()
Note that I don't get this error If am using the usual GradientDescentOptimizer. I am initializing my variables as you can see above but I don't know what 'train/output/bias/RMSProp' is because I don't create any such variable. I only have 'train/output/bias/' which does get initialized above.
Thanks!
So for people from the future running into similar trouble, I found this post helpful:
Tensorflow: Using Adam optimizer
Basically, I was running
sess.run(tf.initialize_all_variables())
before I had defined my loss minimization op
loss = tf.square(targets)
#create the gradient descent op
grads_and_vars = opt.compute_gradients(loss)
capped_grads_and_vars = [(tf.clip_by_value(g, -self.clip_delta, self.clip_delta), v) for g, v in grads_and_vars] #gradient capping
self.opt_op = self.opt.apply_gradients(capped_grads_and_vars)
This needs to be done before running the initialization op!

Tensorflow complaining about placeholder after model restore

I am having a problem with Tensorflow restoring models. I have a script that generates several models, based on a set of training files. These models are created with their own variable scopings, using
tf.variable_scope(myPrefix).
After training, I am able to restore the models using
tf.train.Saver(model_vars).restore(sess, model)
with model_vars computed as
all_vars = tf.all_variables()
model_vars=[k for k in all_vars if k.name.startswith(myPrefix)]
While the models do seem to load, running them produces a Placeholder-error (see below, 85314_tr_10 is my prefix).
I am pretty sure I do not skip any placeholders. The model just has two (x and y) and these are used by the eval call I make:
predictions = sess.run(pred, feed_dict={x: test_data, y:test_labels})
Here is the error trace:
W tensorflow/core/common_runtime/executor.cc:1076] 0x2ea0e60 Compute status: Invalid argument: You must feed a value for placeholder tensor '85314_tr_10/Placeholder' with dtype float
[[Node: 85314_tr_10/Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Traceback (most recent call last):
File "../prediction/views.py", line 507, in <module>
predict("","85314","True","2015-11-12T09:08:00Z","2015-11-12T10:08:00Z")
File "../prediction/views.py", line 472, in predict
predictions= prediction.eval(feed_dict={x: test_data,y:test_labels}, session=sess)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 460, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2910, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 368, in run
results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 444, in _do_run
e.code)
Any help very much appreciated!