tensorflow,如何从张量中获取价值
tensorflow, How get value from Tensor
我使用 image_dataset_from_directory 方法 BatchDataset 上传数据
seed = 64
images = tf.keras.utils.image_dataset_from_directory(
'/content/drive/MyDrive/DATA_PYTHON/Recognize_Alphabet/Recognize_Alphabet',
validation_split=0.2,
image_size=(34, 34),
color_mode='rgb',
interpolation='nearest',
subset='training',
seed=seed)
我想反转我上传的所有图片的颜色。为此,我尝试编写一个方法:
def invertColor(im, b):
sess2 = tf2.Session()
im = sess2.run(im)
imI = PIL.ImageOps.invert(im)
imIN = np.asarray(imI)
imINC = cv2.cvtColor(imIN, cv2.COLOR_BGR2RGB)
bI = Image.fromarray(imINC, 'RGB')
return bI
当我用这个 invertColor 方法调用 map 时
images2 = images.map(invertColor)
我收到以下错误:
InvalidArgumentError: in user code:
File "<ipython-input-19-1f1e09851e25>", line 5, in invertColor *
sess2.run(im)
InvalidArgumentError: Graph execution error:
Detected at node 'args_0' defined at (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
如何在invertColor方法中获取im元素的值? (或者如何反转 BatchDataset 中的颜色?)
您可以尝试使用tf.py_function
以图形模式集成PIL
操作。这是一个批量大小为 1 的示例,以保持简单(您可以在之后更改批量大小):
之前
import tensorflow as tf
import matplotlib.pyplot as plt
import pathlib
import PIL
import cv2
from PIL import Image
import PIL.ImageOps
import numpy as np
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
batch_size = 1
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
shuffle= False,
image_size=(180, 180),
batch_size=batch_size)
image, _ = next(iter(train_ds.take(1)))
plt.imshow(image[0].numpy() / 255)
之后
def invert_color(image):
im = Image.fromarray(image[0].numpy().astype('uint8'), 'RGB')
imI = PIL.ImageOps.invert(im)
imIN = np.asarray(imI)
imINC = cv2.cvtColor(imIN, cv2.COLOR_BGR2RGB)
return imINC / 255
def change_data(image, label):
return tf.py_function(invert_color, [image], Tout=[tf.float32]), label
train_ds = train_ds.map(change_data)
image, _ = next(iter(train_ds.take(1)))
plt.imshow(image[0].numpy())
我使用 image_dataset_from_directory 方法 BatchDataset 上传数据
seed = 64
images = tf.keras.utils.image_dataset_from_directory(
'/content/drive/MyDrive/DATA_PYTHON/Recognize_Alphabet/Recognize_Alphabet',
validation_split=0.2,
image_size=(34, 34),
color_mode='rgb',
interpolation='nearest',
subset='training',
seed=seed)
我想反转我上传的所有图片的颜色。为此,我尝试编写一个方法:
def invertColor(im, b):
sess2 = tf2.Session()
im = sess2.run(im)
imI = PIL.ImageOps.invert(im)
imIN = np.asarray(imI)
imINC = cv2.cvtColor(imIN, cv2.COLOR_BGR2RGB)
bI = Image.fromarray(imINC, 'RGB')
return bI
当我用这个 invertColor 方法调用 map 时
images2 = images.map(invertColor)
我收到以下错误:
InvalidArgumentError: in user code: File "<ipython-input-19-1f1e09851e25>", line 5, in invertColor * sess2.run(im) InvalidArgumentError: Graph execution error: Detected at node 'args_0' defined at (most recent call last): File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec)
如何在invertColor方法中获取im元素的值? (或者如何反转 BatchDataset 中的颜色?)
您可以尝试使用tf.py_function
以图形模式集成PIL
操作。这是一个批量大小为 1 的示例,以保持简单(您可以在之后更改批量大小):
之前
import tensorflow as tf
import matplotlib.pyplot as plt
import pathlib
import PIL
import cv2
from PIL import Image
import PIL.ImageOps
import numpy as np
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
batch_size = 1
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
shuffle= False,
image_size=(180, 180),
batch_size=batch_size)
image, _ = next(iter(train_ds.take(1)))
plt.imshow(image[0].numpy() / 255)
之后
def invert_color(image):
im = Image.fromarray(image[0].numpy().astype('uint8'), 'RGB')
imI = PIL.ImageOps.invert(im)
imIN = np.asarray(imI)
imINC = cv2.cvtColor(imIN, cv2.COLOR_BGR2RGB)
return imINC / 255
def change_data(image, label):
return tf.py_function(invert_color, [image], Tout=[tf.float32]), label
train_ds = train_ds.map(change_data)
image, _ = next(iter(train_ds.take(1)))
plt.imshow(image[0].numpy())