【问题标题】:Preprocessing for TensorFlow Dataset 'cats_vs_dogs'TensorFlow 数据集“cats_vs_dogs”的预处理
【发布时间】:2023-03-15 08:25:01
【问题描述】:

我正在尝试创建一个预处理函数,以便可以将 training_dataset 直接输入到 keras 顺序神经网络中。预处理函数应该返回特征和标签。

def preprocessing_function(data):
        features = ...
        labels = ...
        return features, labels

dataset, info = tfds.load(name='cats_vs_dogs', split=tfds.Split.TRAIN, with_info=True)
    
training_dataset = dataset.map(preprocessing_function)

我应该如何写preprocessing_function?我花了几个小时研究并试图实现它,但无济于事。希望有人能提供帮助。

【问题讨论】:

    标签: tensorflow tensorflow-datasets feature-selection


    【解决方案1】:

    这里有两个用于预处理的函数。第一个将应用于训练和验证数据,以标准化数据并调整网络的预期大小。第二个功能,增强,将仅应用于训练集。您想要执行的增强类型取决于您的数据集和应用程序,但我提供了这个作为示例。

    #Fetching, pre-processing & preparing data-pipeline
    def preprocess(ds):
        x = tf.image.resize_with_pad(ds['image'], IMG_SIZE_W, IMG_SIZE_H)
        x = tf.cast(x, tf.float32)
        x = (x-MEAN)/(VARIANCE)
        y = tf.one_hot(ds['label'], NUM_CLASSES)
        return x, y
    
    def augmentation(image,label):
        image = tf.image.random_flip_left_right(image)
        image = tf.image.resize_with_crop_or_pad(image, IMG_W+4, IMG_W+4) # zero pad each side with 4 pixels
        image = tf.image.random_crop(image, size=[BATCH_SIZE, IMG_W, IMG_H, 3]) # Random crop back to 32x32
        return image, label
    

    要加载训练和验证数据集,请执行以下操作:

    def get_dataset(dataset_name, shuffle_buff_size=1024, batch_size=BATCH_SIZE, augmented=True):
        train, info_train = tfds.load(dataset_name, split='train[:80%]', with_info=True)
        val, info_val = tfds.load(dataset_name, split='train[80%:]', with_info=True)
    
        TRAIN_SIZE = info_train.splits['train'].num_examples * 0.8
        VAL_SIZE = info_train.splits['train'].num_examples * 0.2
    
        train = train.map(preprocess).cache().repeat().shuffle(shuffle_buff_size).batch(batch_size)
        if augmented==True:
            train = train.map(augmentation)
        train = train.prefetch(tf.data.experimental.AUTOTUNE)
    
        val = val.map(preprocess).cache().repeat().batch(batch_size)
        val = val.prefetch(tf.data.experimental.AUTOTUNE)
    
        return train, info_train, val, info_val, TRAIN_SIZE, VAL_SIZE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-05
      • 2021-09-06
      • 2019-12-07
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      相关资源
      最近更新 更多