Python 数据增广映射函数中的Tensorflow随机数
我想使用Python 数据增广映射函数中的Tensorflow随机数,python,tensorflow,tensorflow-datasets,data-augmentation,Python,Tensorflow,Tensorflow Datasets,Data Augmentation,我想使用crop_central函数,随机浮动范围为0.50-1.00,用于数据扩充。但是,当使用numpy.random.uniform(0.50,1.00)并绘制图像时,裁剪是恒定的。我通过使用4个图像和绘制8行进行调试,这些图像是相同的 一般来说,这个问题可以表述如下:如何在数据集映射函数中使用随机数 def data_augment(image, label=None, seed=2020): # I want a random number here for every ind
crop_central
函数,随机浮动范围为0.50-1.00,用于数据扩充。但是,当使用numpy.random.uniform(0.50,1.00)
并绘制图像时,裁剪是恒定的。我通过使用4个图像和绘制8行进行调试,这些图像是相同的
一般来说,这个问题可以表述如下:如何在数据集映射函数中使用随机数
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
早些时候,我误解了这个问题。这是你一直在寻找的答案 我可以使用下面的代码重新创建您的问题- 再现问题的代码-作物图像的输出完全相同
%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
输出-第一行是原始图像。其余行是裁剪图像
这非常具有挑战性,我们提供了以下两种解决方案-
解决方案1:使用np.random.uniform
和tf.py_函数
np.随机.均匀(0.50,1.00)
tf.py_函数
来修饰函数调用-tf.py_函数(data_augment[x],[tf.float32])
%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
%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
输出-第一行是原始图像。其余行是裁剪图像
解决方案2:使用tf.random.uniform
和tf.py_函数
tf.random.uniform(shape=(),minval=0.50,maxval=1).numpy()
AttributeError:“Tensor”对象没有属性“numpy”
。要解决此问题,您需要使用tf.py\u函数(data\u augment[x],[tf.float32])
来修饰函数%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
%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
输出-第一行是原始图像。其余行是裁剪图像
希望这能回答你的问题。快乐学习。@Mark wijkhuizen-如果答案回答了您的问题,请您接受并投票表决。非常感谢。