[英]Tensorflow random numbers in Data Augmentation map function
I want to use the crop_central
function with a random float between 0.50-1.00 for data augmentation.我想使用crop_central
function 和0.50-1.00 之间的随机浮点数进行数据增强。 However, when using numpy.random.uniform(0.50, 1.00)
and plotting the images the crop is constant.但是,当使用numpy.random.uniform(0.50, 1.00)
并绘制图像时,裁剪是恒定的。 I debugged this by using 4 images and plotting 8 rows, the images are identical.我通过使用 4 个图像并绘制 8 行来调试它,图像是相同的。
In general the question might be formulated as follows: How to use random numbers in the Dataset map functions?一般来说,问题可能如下表述:如何在数据集 map 函数中使用随机数?
def data_augment(image, label=None, seed=2020):
# I want a random number here for every individual image
image = tf.image.central_crop(image, np.random.uniform(0.50, 1.00)) # random crop central
image = tf.image.resize(image, INPUT_SHAPE) # the original image size
return image
train_dataset = (
tf.data.Dataset
.from_tensor_slices((train_paths, train_labels))
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.repeat()
.batch(4)
.prefetch(AUTO)
)
# Code to view the images
for idx, (imgs, _) in enumerate(train_dataset):
show_imgs(imgs, 'image', imgs_per_row=4)
if idx is 8:
del imgs
gc.collect()
break
Earlier, I misread the question.早些时候,我误读了这个问题。 Here is the answer you were looking for.这是您正在寻找的答案。
I was able to recreate your issue using the below code -我能够使用以下代码重新创建您的问题 -
Code to reproduce the issue - The output for the crop images were all identical.重现问题的代码 -裁剪图像的 output 完全相同。
%tensorflow_version 2.x
import tensorflow as tf
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array, array_to_img
from matplotlib import pyplot as plt
import numpy as np
AUTOTUNE = tf.data.experimental.AUTOTUNE
# Set the sub plot parameters
f, axarr = plt.subplots(5,4,figsize=(15, 15))
# Load just 4 images of Cifar10
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
images = x_train[:4]
for i in range(4):
axarr[0,i].title.set_text('Original Image')
axarr[0,i].imshow(x_train[i])
def data_augment(images):
image = tf.image.central_crop(images, np.random.uniform(0.50, 1.00)) # random crop central
image = tf.image.resize(image, (32,32)) # the original image size
return image
dataset = tf.data.Dataset.from_tensor_slices((images)).map(lambda x: data_augment(x)).repeat(4)
print(dataset)
ix = 0
i = 1
count = 0
for f in dataset:
crop_img = array_to_img(f)
axarr[i,ix].title.set_text('Crop Image')
axarr[i,ix].imshow(crop_img)
ix=ix+1
count = count + 1
if count == 4:
i = i + 1
count = 0
ix = 0
Output - 1st Row is the Original Image. Output -第一行是原始图像。 Remaining Rows are Crop Images.剩余的行是裁剪图像。
Well it was very challenging and have provided below the two solutions -好吧,这非常具有挑战性,并提供了以下两种解决方案-
Solution 1: Using np.random.uniform
and tf.py_function
.解决方案 1:使用np.random.uniform
和tf.py_function
。
np.random.uniform(0.50, 1.00)
.使用np.random.uniform(0.50, 1.00)
。tf.py_function
to decorate the function call - tf.py_function(data_augment, [x], [tf.float32])
.使用tf.py_function
来装饰 function 调用 - tf.py_function(data_augment, [x], [tf.float32])
。Code to Solve the issue - The crop output images are now different and not identical.解决问题的代码 - 裁剪 output 图像现在不同且不相同。
%tensorflow_version 2.x
import tensorflow as tf
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array, array_to_img
from matplotlib import pyplot as plt
import numpy as np
AUTOTUNE = tf.data.experimental.AUTOTUNE
# Set the sub plot parameters
f, axarr = plt.subplots(5,4,figsize=(15, 15))
# Load just 4 images of Cifar10
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
images = x_train[:4]
for i in range(4):
axarr[0,i].title.set_text('Original Image')
axarr[0,i].imshow(x_train[i])
def data_augment(images):
image = tf.image.central_crop(images, np.random.uniform(0.50, 1.00)) # random crop central
image = tf.image.resize(image, (32,32)) # the original image size
return image
dataset = tf.data.Dataset.from_tensor_slices((images)).map(lambda x: tf.py_function(data_augment, [x], [tf.float32])).repeat(4)
ix = 0
i = 1
count = 0
for f in dataset:
for l in f:
crop_img = array_to_img(l)
axarr[i,ix].title.set_text('Crop Image')
axarr[i,ix].imshow(crop_img)
ix=ix+1
count = count + 1
if count == 4:
i = i + 1
count = 0
ix = 0
Output - 1st Row is the original image. Output -第一行是原始图像。 Remaining rows are Crop Images.剩余的行是裁剪图像。
Solution 2: Using tf.random.uniform
and tf.py_function
.解决方案 2:使用tf.random.uniform
和tf.py_function
。
tf.random.uniform(shape=(), minval=0.50, maxval=1).numpy()
.使用tf.random.uniform(shape=(), minval=0.50, maxval=1).numpy()
。AttributeError: 'Tensor' object has no attribute 'numpy'
.仅通过使用上述选项,代码不起作用,因为它会引发错误AttributeError: 'Tensor' object has no attribute 'numpy'
。 To fix this issue, you need to decorate your function with tf.py_function(data_augment, [x], [tf.float32])
.要解决这个问题,你需要用tf.py_function(data_augment, [x], [tf.float32])
装饰你的 function 。Code to Solve the issue - The crop output images are now different and not identical.解决问题的代码 - 裁剪 output 图像现在不同且不相同。
%tensorflow_version 2.x
import tensorflow as tf
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array, array_to_img
from matplotlib import pyplot as plt
import numpy as np
AUTOTUNE = tf.data.experimental.AUTOTUNE
# Set the sub plot parameters
f, axarr = plt.subplots(5,4,figsize=(15, 15))
# Load just 4 images of Cifar10
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
images = x_train[:4]
for i in range(4):
axarr[0,i].title.set_text('Original Image')
axarr[0,i].imshow(x_train[i])
def data_augment(images):
image = tf.image.central_crop(images, tf.random.uniform(shape=(), minval=0.50, maxval=1).numpy()) # random crop central
image = tf.image.resize(image, (32,32)) # the original image size
return image
dataset = tf.data.Dataset.from_tensor_slices((images)).map(lambda x: tf.py_function(data_augment, [x], [tf.float32])).repeat(4)
ix = 0
i = 1
count = 0
for f in dataset:
for l in f:
crop_img = array_to_img(l)
axarr[i,ix].title.set_text('Crop Image')
axarr[i,ix].imshow(crop_img)
ix=ix+1
count = count + 1
if count == 4:
i = i + 1
count = 0
ix = 0
Output - 1st Row is the Original Image. Output -第一行是原始图像。 Remaining Rows are Crop Images.剩余的行是裁剪图像。
Hope this answers your question.希望这能回答你的问题。 Happy Learning.快乐学习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.