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

'Tensor' object has no attribute 'ndim' #93

Closed
YIsonP opened this issue Nov 23, 2022 · 5 comments
Closed

'Tensor' object has no attribute 'ndim' #93

YIsonP opened this issue Nov 23, 2022 · 5 comments

Comments

@YIsonP
Copy link

YIsonP commented Nov 23, 2022

First, great Thanks to this masterpiece! It really saved my life when I found this toolkit. But I got some errors while I was trying to visualize my model via saliency map.
I think the problem is from the format of seed_input, I followed exactly the same way as the tutorial did, using one image, using img_to_array to process it, and passing the array to the seed_input, but some how seed_inputs were transformed into a tensor such that it has no attribute 'ndim', I tried to transform X into ndarray by K.eval(X), but so far it didn't work well. Many thanks for all the help!!

---> 94 seed_inputs = list(seed_inputs)
     95 total = (np.zeros_like(X[0]) for X in seed_inputs)
     96 for i in range(smooth_samples):

File ~\Anaconda3\lib\site-packages\tf_keras_vis\saliency.py:93, in <genexpr>(.0)
     89 seed_inputs = ((X, smooth_noise * (tf.math.reduce_max(X, axis=axis, keepdims=True) -
     90                                    tf.math.reduce_min(X, axis=axis, keepdims=True)))
     91                for X, axis in seed_inputs)
     92 print(seed_inputs)
---> 93 seed_inputs = (X + np.random.normal(0., sigma,X.shape) for X, sigma in seed_inputs)
     94 seed_inputs = list(seed_inputs)
     95 total = (np.zeros_like(X[0]) for X in seed_inputs)

File ~\Anaconda3\lib\site-packages\tf_keras_vis\saliency.py:89, in <genexpr>(.0)
     86 seed_inputs = (tf.reshape(X, (smooth_samples, -1) + tuple(X.shape[1:]))
     87                for X in seed_inputs)
     88 seed_inputs = ((X, tuple(range(K.eval(X).ndim)[2:])) for X in seed_inputs)
---> 89 seed_inputs = ((X, smooth_noise * (tf.math.reduce_max(X, axis=axis, keepdims=True) -
     90                                    tf.math.reduce_min(X, axis=axis, keepdims=True)))
     91                for X, axis in seed_inputs)
     92 print(seed_inputs)
     93 seed_inputs = (X + np.random.normal(0., sigma, X.shape) for X, sigma in seed_inputs)

File ~\Anaconda3\lib\site-packages\tf_keras_vis\saliency.py:88, in <genexpr>(.0)
     84 seed_inputs = (tf.tile(X, (smooth_samples, ) + tuple(np.ones(X.ndim - 1, np.int)))
     85                for X in seed_inputs)
     86 seed_inputs = (tf.reshape(X, (smooth_samples, -1) + tuple(X.shape[1:]))
     87                for X in seed_inputs)
---> 88 seed_inputs = ((X, tuple(range(X.ndim)[2:])) for X in seed_inputs)
     89 seed_inputs = ((X, smooth_noise * (tf.math.reduce_max(X, axis=axis, keepdims=True) -
     90                                    tf.math.reduce_min(X, axis=axis, keepdims=True)))
     91                for X, axis in seed_inputs)
     92 print(seed_inputs)

File ~\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py:446, in Tensor.__getattr__(self, name)
    437 if name in {"T", "astype", "ravel", "transpose", "reshape", "clip", "size",
    438             "tolist", "data"}:
    439   # TODO(wangpeng): Export the enable_numpy_behavior knob
    440   raise AttributeError(
    441       f"{type(self).__name__} object has no attribute '{name}'. " + """
    442     If you are looking for numpy-related methods, please run the following:
    443     from tensorflow.python.ops.numpy_ops import np_config
    444     np_config.enable_numpy_behavior()
    445   """)
--> 446 self.__getattribute__(name)

AttributeError: 'Tensor' object has no attribute 'ndim'
@keisen
Copy link
Owner

keisen commented Nov 24, 2022

Hi, @YIsonP . Thank you for your reporting.

I tried to reproduce your problem, I didn't get the error.
Could you please the code snippet to reproduce it?
And please let me know the versions of tf-keras-vis, Tensorflow and Python.

Thanks!

@YIsonP
Copy link
Author

YIsonP commented Nov 24, 2022

Hi @keisen. Thanks for reply. here is my env:
tf-keras-vis:0.8.2
tensorflow:2.10.0
python: 3.9.12
and this is the link for the snippet :https://github.com/YIsonP/saliency-map
a simple bg: I use pre-trained model in Keras and try to visualize its decision by saliency map.

@keisen
Copy link
Owner

keisen commented Nov 24, 2022

Hi, @YIsonP , Thank you for sharing the notebook.

The cause of the error is tf.compat.v1.disable_eager_execution() at 2nd cell in your notebook.
tf-keras-vis only support TF2.0+, but not TF1.x.
So if you want to visualize with the model built on TF1.x, I recommend you to use keras-vis.
If not, please use tf-keras-vis without tf.compat.v1.disable_eager_execution() .

Thanks!

@YIsonP
Copy link
Author

YIsonP commented Nov 25, 2022

Hi, @YIsonP , Thank you for sharing the notebook.

The cause of the error is tf.compat.v1.disable_eager_execution() at 2nd cell in your notebook. tf-keras-vis only support TF2.0+, but not TF1.x. So if you want to visualize with the model built on TF1.x, I recommend you to use keras-vis. If not, please use tf-keras-vis without tf.compat.v1.disable_eager_execution() .

Thanks!

Hi @keisen
It's been super helpful. Thank you so much for the help!!

@keisen
Copy link
Owner

keisen commented Nov 30, 2022

Anytime!

@keisen keisen closed this as completed Nov 30, 2022
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

2 participants