如何解决TypeError:“函数”接受1个位置参数,但给出了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,upper=1 + 0.8 * s)
x = tf.image.random_hue(x,max_delta=0.2 * s)
x = tf.clip_by_value(x,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=(),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,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,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,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: ((),tf.int64)>
[(1,array([1])),(2,array([1,1])),(3,1,1]))]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。