【问题标题】:TypeError: "function" takes 1 positional argument but 2 were given类型错误:“函数”接受 1 个位置参数,但给出了 2 个
【发布时间】: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


【解决方案1】:

您收到此错误是因为您的 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]))]

【讨论】:

    猜你喜欢
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 2016-10-13
    • 2018-03-18
    • 2021-07-26
    • 1970-01-01
    • 2016-11-25
    相关资源
    最近更新 更多