I am trying to convert a tensor to numpy in the tesnorflow2.0 version. Since tf2.0 have eager execution enabled then it should work by default and working too in normal runtime. While I execute code in tf.data.Dataset API then it gives an error
"AttributeError: 'Tensor' object has no attribute 'numpy'"
I have tried ".numpy()" after tensorflow variable and for ".eval()" I am unable to get default session.
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
# tf.executing_eagerly()
import os
import time
import matplotlib.pyplot as plt
from IPython.display import clear_output
from model.utils import get_noise
import cv2
def random_noise(input_image):
img_out = get_noise(input_image)
return img_out
def load_denoising(image_file):
image = tf.io.read_file(image_file)
image = tf.image.decode_png(image)
real_image = image
input_image = random_noise(image.numpy())
input_image = tf.cast(input_image, tf.float32)
real_image = tf.cast(real_image, tf.float32)
return input_image, real_image
def load_image_train(image_file):
input_image, real_image = load_denoising(image_file)
return input_image, real_image
This works fine
inp, re = load_denoising('/data/images/train/18.png')
# Check for correct run
plt.figure()
plt.imshow(inp)
print(re.shape," ", inp.shape)
And this produces mentioned error
train_dataset = tf.data.Dataset.list_files('/data/images/train/*.png')
train_dataset = train_dataset.map(load_image_train,num_parallel_calls=tf.data.experimental.AUTOTUNE)
Note: random_noise have cv2 and sklearn functions