简体   繁体   English

如何在第一个epoch直接使用map函数生成的变换,而不是每个epoch都执行map函数?

[英]How to directly use the transformation generated by map function in first epoch instead of executing map function every epoch?

I want to apply some transformation to dataset by using map function.我想通过使用 map 函数对数据集应用一些转换。 However, I found the map function was executed in every epoch.但是,我发现 map 函数在每个 epoch 中都被执行。 Is it possible that the map function is only executed in first epoch and the following epochs directly use the transformation generated in first epoch?有没有可能 map 函数只在第一个 epoch 执行,后面的 epoch 直接使用第一个 epoch 生成的转换?

If you set the seed with Tensorflow and apply transformations with tf.image , the random transformations will be consistent between epochs.如果设置与Tensorflow种子和应用与转化tf.image ,随机转换将是时代之间是一致的。

import tensorflow as tf
from skimage import data
tf.random.set_seed(42)
import matplotlib.pyplot as plt

def transform(image):
    image = tf.image.random_hue(image, 0.5, 1.)
    image = tf.image.random_flip_left_right(image)
    image = tf.image.random_flip_up_down(image)
    return image

X = tf.stack([data.chelsea() for i in range(4)])
ds = tf.data.Dataset.from_tensor_slices(X).map(transform)

inputs = [[], []]

for epoch in range(2):
    for sample in ds:
        inputs[epoch].append(sample)

inputs_paired = [i for s in inputs for i in s]

fig = plt.figure(figsize=(16, 8))
plt.subplots_adjust(wspace=.1, hspace=.1)
for i in range(8):
    ax = plt.subplot(2, 4, i + 1)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.imshow(inputs_paired[i])
plt.show()

Top is the first epoch, bottom is the second epoch.顶部是第一个纪元,底部是第二纪元。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM