类型错误:“函数”需要 1 个位置参数,但给出了 2 个

问题描述 投票:0回答:2

我想应用下面的函数,它负责增强每个图像并对其进行转换:

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]))

tensorflow2.0 tensorflow2.x
2个回答
2
投票

您收到此错误是因为您的

training_set
有 2 个元素,但您只将一个元素传递给函数
distort_simclr

下面是一个简单的代码来重现您的错误 -

错误代码-

import itertools
import tensorflow as tf

def gen():
  for i in itertools.count(1):
    yield (i, [1] * i)

dataset = tf.data.Dataset.from_generator(
     gen,
     (tf.int64, tf.int64),
     (tf.TensorShape([]), tf.TensorShape([None])))

print(dataset)

def doNothing(i):
    return i

dataset = dataset.map(doNothing)

list(dataset.take(3).as_numpy_iterator())

输出-

<FlatMapDataset shapes: ((), (None,)), types: (tf.int64, tf.int64)>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-27a58aace75c> in <module>()
     15     return i
     16 
---> 17 dataset = dataset.map(doNothing)
     18 
     19 list(dataset.take(3).as_numpy_iterator())

10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
    256       except Exception as e:  # pylint:disable=broad-except
    257         if hasattr(e, 'ag_error_metadata'):
--> 258           raise e.ag_error_metadata.to_exception(e)
    259         else:
    260           raise

TypeError: in user code:


    TypeError: tf__doNothing() takes 1 positional argument but 2 were given

要修复错误,请将两个元素传递给函数。

固定代码 -

import itertools
import tensorflow as tf

def gen():
  for i in itertools.count(1):
    yield (i, [1] * i)

dataset = tf.data.Dataset.from_generator(
     gen,
     (tf.int64, tf.int64),
     (tf.TensorShape([]), tf.TensorShape([None])))

print(dataset)

def doNothing(i,j):
    return i,j

dataset = dataset.map(doNothing)

list(dataset.take(3).as_numpy_iterator())

输出-

<FlatMapDataset shapes: ((), (None,)), types: (tf.int64, tf.int64)>
[(1, array([1])), (2, array([1, 1])), (3, array([1, 1, 1]))]

0
投票

你也可以使用

doNothing = lambda *args: args
© www.soinside.com 2019 - 2024. All rights reserved.