【问题标题】:How to seperate a Tensorflow dataset object image and label如何分离 Tensorflow 数据集对象图像和标签
【发布时间】:2021-09-29 10:47:54
【问题描述】:

我有一个形状为 (N_img,60,80,1) 的数据集和 1 个使用 tf.data.Dataset.from_tensor_slices 创建的标签数组。现在我想用 .map 函数将其拆分为 x_train 和 y_train 但它给了我这个错误。

TypeError: in user code:    TypeError: <lambda>() takes 1 positional argument but 2 were given

这是我的代码

img_width, img_height = 80, 60
n_positives_img, n_negatives_img = 17874, 26308 
n_total_img = 44182

#Imports of datasets inside Drive
ds_negatives = np.loadtxt('/content/drive/MyDrive/Colab Notebooks/negative_depth.txt')
ds_positives = np.loadtxt('/content/drive/MyDrive/Colab Notebooks/positive_depth.txt')

#Labeled arrays for datasets
arrayceros = np.zeros(n_negatives_img)
arrayunos = np.ones(n_positives_img)

#Reshaping of datasets to convert separate them
arraynegativos= ds_negatives.reshape(( n_negatives_img, img_height, img_width,1))
arraypositivos= ds_positives.reshape((n_positives_img, img_height, img_width,1))

#Labeling datasets with the arrays
ds_negatives_target = tf.data.Dataset.from_tensor_slices((arraynegativos, arrayceros))
ds_positives_target = tf.data.Dataset.from_tensor_slices((arraypositivos, arrayunos))


#Concatenate 2 datasets and shuffle them
ds_concatenate = ds_negatives_target.concatenate(ds_positives_target)
datasetfinal = ds_concatenate.shuffle(n_total_img)

#24000 images for the training ds and 20000 for the validation ds
valid_ds_f = datasetfinal.take(20000)
train_ds_f = datasetfinal.skip(20000)


valid_ds = valid_ds_f.batch(12)
train_ds = train_ds_f.batch(12)

这就是给我错误的原因:

train_x_batches = train_ds.map(lambda x: x[0]) 
train_y_batches = train_ds.map(lambda x: x[1]) 

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    是的,您有一个元组数据集,因此 lambda 函数需要两个参数,您可以按以下方式拆分数据集

    train_ds.map(lambda x, y: x) 
    train_ds.map(lambda x, y: y)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-14
      • 2021-06-08
      • 2020-09-23
      • 1970-01-01
      • 2020-02-15
      • 2021-08-18
      • 2020-12-08
      • 1970-01-01
      相关资源
      最近更新 更多