Jacke Dow
Jacke Dow

Reputation: 199

TensorFlow saving model - Paradoxical exception

I've tried to save a basic MNIST model:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

saver = tf.train.Saver()

sess.run(tf.global_variables_initializer())

y = tf.matmul(x, W) + b
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_, logits = y))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

saver.save(sess, './mnist_to-save-saved')

for _ in range(1000):
    batch = mnist.train.next_batch(100)
    train_step.run(feed_dict={x: batch[0], y_: batch[1]})

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver.save(sess, '/mnist-to-save-saved', global_step=1000)

print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

It seems to partially function because, in the same directory that this model .py file some files are generated:

mnist_to-save-daved.index
mnist_to-save_saved.meta
mnist_to-save.saved.data-00000-of-00001
checkpoint

I can only access to the content of the "checkpoint" file, that is:

model_checkpoint_path: "mnist_to-save-saved"
all_model_checkpoint_paths: "mnist_to-save-saved"

My question is, when executing the training model file, why do I get at the same time this error trace, and how can I fix it?

C:\Users\username\Anaconda3\envs\tensorflowenv\python.exe C:/Users/username/PycharmProjects/estimator/mnist-to-save.py
Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
2018-04-15 18:22:39.959614: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1192] Permission denied: Failed to create a directory: /; Permission denied
Traceback (most recent call last):
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 1323, in _do_call
    return fn(*args)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 1302, in _run_fn
    status, run_metadata)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.PermissionDeniedError: Failed to create a directory: /; Permission denied
     [[Node: save/SaveV2 = SaveV2[dtypes=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/SaveV2/tensor_names, save/SaveV2/shape_and_slices, Variable, Variable_1)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/username/PycharmProjects/estimator/mnist-to-save.py", line 28, in <module>
    saver.save(sess, '/mnist-to-save-saved', global_step=1000)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 1573, in save
    {self.saver_def.filename_tensor_name: checkpoint_file})
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 889, in run
    run_metadata_ptr)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 1120, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 1317, in _do_run
    options, run_metadata)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 1336, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.PermissionDeniedError: Failed to create a directory: /; Permission denied
     [[Node: save/SaveV2 = SaveV2[dtypes=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/SaveV2/tensor_names, save/SaveV2/shape_and_slices, Variable, Variable_1)]]

Caused by op 'save/SaveV2', defined at:
  File "C:/Users/username/PycharmProjects/estimator/mnist-to-save.py", line 11, in <module>
    saver = tf.train.Saver()
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 1218, in __init__
    self.build()
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 1227, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 1263, in _build
    build_save=build_save, build_restore=build_restore)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 748, in _build_internal
    save_tensor = self._AddSaveOps(filename_tensor, saveables)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 296, in _AddSaveOps
    save = self.save_op(filename_tensor, saveables)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 239, in save_op
    tensors)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\ops\gen_io_ops.py", line 1162, in save_v2
    shape_and_slices=shape_and_slices, tensors=tensors, name=name)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\framework\ops.py", line 2956, in create_op
    op_def=op_def)
  File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\framework\ops.py", line 1470, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

PermissionDeniedError (see above for traceback): Failed to create a directory: /; Permission denied
     [[Node: save/SaveV2 = SaveV2[dtypes=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/SaveV2/tensor_names, save/SaveV2/shape_and_slices, Variable, Variable_1)]]


Process finished with exit code 1

It seems to save the pre-trained mdel, but not works well with the training.

Upvotes: 3

Views: 3534

Answers (1)

Anton Panchishin
Anton Panchishin

Reputation: 3763

Here is an extract of your code

saver.save(sess, './mnist_to-save-saved')

for _ in range(1000):
    batch = mnist.train.next_batch(100)
    train_step.run(feed_dict={x: batch[0], y_: batch[1]})

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver.save(sess, '/mnist-to-save-saved', global_step=1000)

Problem

The last line has saver.save(sess, '/mnist-to-save-saved' , which should probably be saver.save(sess, './mnist-to-save-saved'

This is most likely causing the issue W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1192] Permission denied: Failed to create a directory: /; Permission denied because your path is using root "/" instead of relative "./"

Solution

Replace /mnist-to-save-saved with ./mnist-to-save-saved or better yet define a variable at the top of your code like

TF_SAVE_FILE = './mnist-to-save-saved'

Then use that variable throughout your code instead of copying it.

Upvotes: 9

Related Questions