每个时期带有数据增强功能的tf.Data输入管道如何工作?

时间:2019-05-13 14:09:42

标签: python tensorflow keras image-segmentation

我将此代码(https://colab.research.google.com/github/tensorflow/models/blob/master/samples/outreach/blogs/segmentation_blogpost/image_segmentation.ipynb#scrollTo=tkNqQaR2HQbd)用于我的数据张量流管道。但是我不明白它是如何工作的。他们告诉我们“在训练期间,我们的模型将永远不会看到两倍于完全相同的图片”。但这如何工作?我只将tf.data Map-Function与_augment-Function一起使用。这会发生在我的model.fit函数的每一步吗?

我尝试通过打印出一些内容来验证_augment函数。但这只会在第一次而不是每个时期发生。

def get_baseline_dataset(filenames, 
                         labels,
                         preproc_fn=functools.partial(_augment),
                         threads=5, 
                         batch_size=batch_size,
                         shuffle=True):           
  num_x = len(filenames)
  # Create a dataset from the filenames and labels
  dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
  # Map our preprocessing function to every element in our dataset, taking
  # advantage of multithreading
  dataset = dataset.map(_process_pathnames, num_parallel_calls=threads)
  if preproc_fn.keywords is not None and 'resize' not in preproc_fn.keywords:
    assert batch_size == 1, "Batching images must be of the same size"

  dataset = dataset.map(preproc_fn, num_parallel_calls=threads)

  if shuffle:
    dataset = dataset.shuffle(num_x)


  # It's necessary to repeat our data for all epochs 
  dataset = dataset.repeat().batch(batch_size)
  return dataset
tr_cfg = {
    'resize': [img_shape[0], img_shape[1]],
    'scale': 1 / 255.,
    'hue_delta': 0.1,
    'horizontal_flip': True,
    'width_shift_range': 0.1,
    'height_shift_range': 0.1
}
tr_preprocessing_fn = functools.partial(_augment, **tr_cfg)
train_ds = get_baseline_dataset(x_train_filenames,
                                y_train_filenames,
                                preproc_fn=tr_preprocessing_fn,
                                batch_size=batch_size)

1 个答案:

答案 0 :(得分:0)

我引用了https://cs230-stanford.github.io/tensorflow-input-data中的基本步骤 我建议您浏览一下这篇文章以了解详细信息。

” 总而言之,不同转换的一个好顺序是:

  1. 创建数据集
  2. 随机播放(缓冲区大小足够大)
  3. 重复
  4. 使用多个并行调用映射实际工作(预处理,扩充…)。
  5. 批次
  6. 预取 “

这应该可以提供您想要的内容,因为“重复”之后是“增强”。 希望对您有所帮助。