seni
seni

Reputation: 711

TypeError: "function" takes 1 positional argument but 2 were given

I would like to applicate the function bellow, which is a responsible to augment each image and transform it :

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

on my dataset imported like bellow

training_set = tf.data.Dataset.from_generator(path, output_types=(tf.float32, tf.float32), output_shapes = ([2,224,224,3],[2,2]))

So I write this :

training_set = training_set.map(distort_simclr, num_parallel_calls=tf.data.experimental.AUTOTUNE)

I find this:

tf__distort_simclr() takes 1 positional argument but 2 were given

Here is an example of my dataset :

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

Upvotes: 1

Views: 9212

Answers (2)

Galuoises
Galuoises

Reputation: 3283

you might also use

doNothing = lambda *args: args

Upvotes: 0

user11530462
user11530462

Reputation:

You are getting this error because your training_set has 2 elements but you are passing just one element to function distort_simclr.

Below is a simple code to reproduce your error -

Error code -

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

Output -

<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

To fix the error pass both the elements to the function.

Fixed Code -

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

Output -

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

Upvotes: 2

Related Questions