Convert a Tensor to Numpy Array in Tensorflow

How to convert Tensor into NumPy array

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

Loading a numpy array into Tensorflow input pipeline

The comment of @André put me in the right direction. The code below works.


def process_image(file_path):
label = get_label(file_path)
label = np.uint8(label)

img = np.load(file_path)
img = tf.convert_to_tensor(img/255, dtype=tf.float32)

return img , label

train_ds = images_ds.map(lambda item: tf.numpy_function(
process_image, [item], (tf.float32, tf.uint8)))

Converting KerasTensor to numpy array

There is no value to convert to numpy.

You need an input to have an output.

In keras, the best to do is to build a submodel.

submodel = Model(original_model.inputs, original_model.get_layer("encoder_output").output)   
results = submodel.predict(numpy_input)


Related Topics



Leave a reply



Submit