【发布时间】:2020-12-29 01:00:20
【问题描述】:
我想应用下面的函数,它负责增强每个图像并对其进行转换:
def color_distortion(image, s=1.0):
# image is a tensor with value range in [0, 1].
# s is the strength of color distortion.
def color_jitter(x):
# one can also shuffle the order of following augmentations
# each time they are applied.
x = tf.image.random_brightness(x, max_delta=0.8 * s)
x = tf.image.random_contrast(x, lower=1 - 0.8 * s, upper=1 + 0.8 * s)
x = tf.image.random_saturation(x, lower=1 - 0.8 * s, upper=1 + 0.8 * s)
x = tf.image.random_hue(x, max_delta=0.2 * s)
x = tf.clip_by_value(x, 0, 1)
return x
def color_drop(x):
x = tf.image.rgb_to_grayscale(x)
x = tf.tile(x, [1, 1, 3])
return x
rand_ = tf.random.uniform(shape=(), minval=0, maxval=1)
# randomly apply transformation with probability p.
if rand_ < 0.8:
image = color_jitter(image)
rand_ = tf.random.uniform(shape=(), minval=0, maxval=1)
if rand_ < 0.2:
image = color_drop(image)
return image
def distort_simclr(image):
image = tf.cast(image, tf.float32)
v1 = color_distortion(image / 255.)
v2 = color_distortion(image / 255.)
return v1, v2
在我的数据集上像下面这样导入
training_set = tf.data.Dataset.from_generator(path, output_types=(tf.float32, tf.float32), output_shapes = ([2,224,224,3],[2,2]))
所以我写了这个:
training_set = training_set.map(distort_simclr, num_parallel_calls=tf.data.experimental.AUTOTUNE)
我发现了这个:
tf__distort_simclr() takes 1 positional argument but 2 were given
这是我的数据集的一个示例:
img_gen = tf.keras.preprocessing.image.ImageDataGenerator()
gen = img_gen.flow_from_directory('/train/',(224, 224),'rgb', batch_size = 2)
training_set = tf.data.Dataset.from_generator(lambda : gen, output_types=(tf.float32, tf.float32), output_shapes = ([2,224,224,3],[2,2]))
【问题讨论】:
-
欢迎来到 Stack Overflow,您能否提供一个最小的、可重现的代码示例(代码 + 数据),以便重现您的错误? (stackoverflow.com/help/minimal-reproducible-example)
-
好的,我编辑我的代码,我想现在更清楚了..
-
谢谢,但仍然无法重现您的错误。试着把你的代码和一些数据一起调整,这样任何人都可以运行它来遇到和你一样的错误。
-
您能否在定义 img_gen 的位置添加代码(我假设为 ImageDataGenerator())?您的“flow_from_directory”也不能为其他人执行,因为只有您拥有包含数据的目录。没有你的本地文件的人仍然无法运行你的代码。
-
我的文件夹包含狗和猫的图像(2 类)
标签: tensorflow2.0 tensorflow2.x