infocusp / tf_cnnvis

CNN visualization tool in TensorFlow
MIT License
781 stars 208 forks source link

PermissionDeniedError when importing frozen graph #64

Closed tm2313 closed 6 years ago

tm2313 commented 6 years ago

Hi there,

I'm trying to use the library with a pretrained model to visualise features. I think where I'm coming stuck is providing the graph to the activation_visualisation function (i.e. I don't think this is a problem with Tensorflow - I'm running other scripts that seem to work okay).

import os
import tensorflow as tf
import numpy as np
import cv2
import tf_cnnvis as tfv
from tensorflow.python.platform import gfile

X = tf.placeholder(tf.float32, shape = [None, 48, 64, 3]) # placeholder for input images
im = np.array(cv2.imread("test.jpg"))

with tf.Session() as sess:
    model_filename = "saved_model.pb"
    with gfile.FastGFile(model_filename, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        tf.import_graph_def(graph_def)

    is_success = tfv.activation_visualization(sess_graph_path = tf.get_default_graph(), value_feed_dict = {X : im})

sess.close()

I'm still fairly new to Tensorflow, so it may be an issue with where I'm working in the session, but I've tried jostling the activation_visualization function around to no avail.

This is the error message I get.

2018-08-13 14:43:16.446408: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1098] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 7534 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080, pci bus id: 0000:02:00.0, compute capability: 6.1)
2018-08-13 14:43:16.446561: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1098] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:1 with 7534 MB memory) -> physical GPU (device: 1, name: GeForce GTX 1080, pci bus id: 0000:03:00.0, compute capability: 6.1)
2018-08-13 14:43:16.446685: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1098] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:2 with 7534 MB memory) -> physical GPU (device: 2, name: GeForce GTX 1080, pci bus id: 0000:81:00.0, compute capability: 6.1)
2018-08-13 14:43:16.447510: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1098] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:3 with 7534 MB memory) -> physical GPU (device: 3, name: GeForce GTX 1080, pci bus id: 0000:82:00.0, compute capability: 6.1)
2018-08-13 14:43:16.475137: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at save_restore_v2_ops.cc:109 : Permission denied: model; Permission denied
Traceback (most recent call last):
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1292, in _do_call
    return fn(*args)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1277, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1367, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.PermissionDeniedError: model; Permission denied
     [[{{node save/SaveV2}} = SaveV2[dtypes=[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, fake_var/_1)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "visualise_testv2.py", line 24, in <module>
    is_success = tfv.activation_visualization(sess_graph_path = tf.get_default_graph(), value_feed_dict = {X : im})
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tf_cnnvis-1.0.0-py3.6.egg/tf_cnnvis/tf_cnnvis.py", line 406, in activation_visualization
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tf_cnnvis-1.0.0-py3.6.egg/tf_cnnvis/tf_cnnvis.py", line 131, in _get_visualization
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tf_cnnvis-1.0.0-py3.6.egg/tf_cnnvis/tf_cnnvis.py", line 79, in _save_model
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1417, in save
    {self.saver_def.filename_tensor_name: checkpoint_file})
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 887, in run
    run_metadata_ptr)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1110, in _run
    feed_dict_tensor, options, run_metadata)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1286, in _do_run
    run_metadata)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1308, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.PermissionDeniedError: model; Permission denied
     [[{{node save/SaveV2}} = SaveV2[dtypes=[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, fake_var/_1)]]

Caused by op 'save/SaveV2', defined at:
  File "visualise_testv2.py", line 24, in <module>
    is_success = tfv.activation_visualization(sess_graph_path = tf.get_default_graph(), value_feed_dict = {X : im})
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tf_cnnvis-1.0.0-py3.6.egg/tf_cnnvis/tf_cnnvis.py", line 406, in activation_visualization
    path_logdir = path_logdir, path_outdir = path_outdir)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tf_cnnvis-1.0.0-py3.6.egg/tf_cnnvis/tf_cnnvis.py", line 131, in _get_visualization
    PATH = _save_model(sess_graph_path)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tf_cnnvis-1.0.0-py3.6.egg/tf_cnnvis/tf_cnnvis.py", line 77, in _save_model
    saver = tf.train.Saver()
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1078, in __init__
    self.build()
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1090, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1127, in _build
    build_save=build_save, build_restore=build_restore)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 784, in _build_internal
    save_tensor = self._AddSaveOps(filename_tensor, saveables)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 284, in _AddSaveOps
    save = self.save_op(filename_tensor, saveables)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 202, in save_op
    tensors)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_io_ops.py", line 1690, in save_v2
    shape_and_slices=shape_and_slices, tensors=tensors, name=name)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3260, in create_op
    op_def=op_def)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1748, in __init__
    self._traceback = tf_stack.extract_stack()

PermissionDeniedError (see above for traceback): model; Permission denied
     [[{{node save/SaveV2}} = SaveV2[dtypes=[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, fake_var/_1)]]

As an aside, would it be possible in future to provide protobuf files directly as input to the function? As I said, I'm new to Tensorflow, so I'm not sure how easy it would be.

falaktheoptimist commented 6 years ago

This would be caused if you are running the script above from a folder where you do not have write permissions. So, when it tries to save the mdoel, it does not have the necessary permissions to write the models folder in the current directory. Try running from a different directory where you have permissions - that should solve your problem.

tm2313 commented 6 years ago

Hmm, that's probably it - I wasn't sure if it was the input for the function or if it was when it was trying to write something. I've been having problems with the server that work has set me up with recently, but I thought they'd been fixed. Unfortunately, this probably means that I'll be having the same issue no matter where I run the script. Thanks very much for the help, at any rate.

falaktheoptimist commented 6 years ago

You could run it locally on your machine if you want.

tm2313 commented 6 years ago

That's true. I might ask our system admin about it when he has the time. Cheers.

tm2313 commented 6 years ago

Okay, so I've tried running the script locally and I've run into another issue. At the moment, I'm importing a Keras model with BatchNormalization layers. Normally when you try and do this, you need to specify a learning phase variable and pass it to feed_dict as a seperate tensor. For example, with the script above, I am assuming I provide the input like so:

value_feed_dict = {X: im, K.learning_phase(): 0}

However, this is giving me the following error:

Traceback (most recent call last):
  File "vis2.py", line 36, in <module>
    is_success = tfv.activation_visualization(sess_graph_path=tf.get_default_graph(), value_feed_dict = {X : im, K.learning_phase(): 0}, layers=layers)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tf_cnnvis-1.0.0-py3.6.egg/tf_cnnvis/tf_cnnvis.py", line 406, in activation_visualization
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tf_cnnvis-1.0.0-py3.6.egg/tf_cnnvis/tf_cnnvis.py", line 169, in _get_visualization
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tf_cnnvis-1.0.0-py3.6.egg/tf_cnnvis/tf_cnnvis.py", line 227, in _visualization_by_layer_type
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tf_cnnvis-1.0.0-py3.6.egg/tf_cnnvis/tf_cnnvis.py", line 270, in _visualization_by_layer_name
  File "/usr/local/anaconda3/lib/python3.6/site-packages/tf_cnnvis-1.0.0-py3.6.egg/tf_cnnvis/utils.py", line 79, in parse_tensors_dict
AttributeError: 'int' object has no attribute 'name'

Can I set learning_phase to 0 in value_feed_dict as I would with feed_dict, or should I be doing something differently ?