2

I have trained ResNet50 model on my data. I want to get the output of a custom layer while making the prediction. I tried using the below code to get the output of a custom layer, it gives data in a tensor format, but I need the data in a NumPy array format. I tried to convert the tensor to NumPy array but getting errors, I have followed this post, but it wasn't helpful

Can anyone share some thoughts, any advice will be very helpful

from keras.models import load_model
import tensorflow as tf
import numpy as np

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.Session(config=config)

model = load_model(model_path) # load trained model

data = load_data(data_path) # load data for predictions
result = model.predict(data)
print(type(result_dev))
#<class 'numpy.ndarray'> 

result = model.get_layer('avg_pool').output
print(type(result))
#<class 'tensorflow.python.framework.ops.Tensor'>

Things I tried

Option 1

result = result.numpy()

AttributeError: 'Tensor' object has no attribute 'numpy'

Option 2

result = result.eval(session=tf.compat.v1.Session())

2020-09-22 11:21:59.522138: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-09-22 11:21:59.522343: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:

Dependency Installed:

tensorflow-gpu==1.15.0

3 Answers 3

0

You can only convert tensors to numpy arrays during Eager execution. Since you're using a version older than 2.0 this is not enabled by default.

In any case, you can call this after importing tensorflow:

tf.compat.v1.enable_eager_execution()

Depending on your framework and usecase, you might also have to run tf.config.run_functions_eagerly (if you have tf.function defined anywhere). For better support of the Eager mode, you should upgrade tensorflow to the newest version and use tf.keras as your code might not work properly with older standalone versions of Keras. In newer versions, you can specify your keras model to run eagerly like this:

model.run_eagerly = True
Sign up to request clarification or add additional context in comments.

2 Comments

I tried enabling eager excecution with tf.enable_eager_execution() but it throws error RuntimeError: get_session is not available when TensorFlow is executing eagerly.. I have also tried tensorflow-gpu==2.0 but as model was trained on tensorflow-gpu==1.15 its throwing error while loading it with tensorflow-gpu==2.0
I was expecting this, hence my warning. Compatibility in these older versions is tricky (see e.g. here) and I won't be able to directly help you with that as it is very project-specific and requires some trial and error until you find the correct package combinations. In any case, my answer stands: If you want to use Eager, this is how you go about it generally.
0

A tensor can be converted into a numpy array using following function of tensorflow:

import tensorflow as tf
tf.make_ndarray(
    tensor
)

For example:

# Tensor a has shape (2,3)
a = tf.constant([[1,2,3],[4,5,6]])
proto_tensor = tf.make_tensor_proto(a)  # convert `tensor a` to a proto tensor
tf.make_ndarray(proto_tensor) # output: array([[1, 2, 3],
#                                              [4, 5, 6]], dtype=int32)
# output has shape (2,3)

3 Comments

tf.make_ndarray(tensor) throws this error AttributeError: 'Tensor' object has no attribute 'tensor_shape'
How does the above link help solve the problem which OP has?
0

Finally approach mentioned here worked for me with tensorflow-gpu==2.0.0 and keras==2.2.4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.