使用python函数和tf.Dataset API进行数据扩充

使用python函数和tf.Dataset API进行数据扩充,python,tensorflow,deep-learning,dataset,tensorflow-datasets,Python,Tensorflow,Deep Learning,Dataset,Tensorflow Datasets,我正在寻找动态读取的图像,并为我的图像分割问题应用数据增强。从目前为止我所看到的情况来看,最好的方法是使用带有.map函数的tf.DatasetAPI 然而,从我所看到的示例中,我认为我必须将所有函数调整为tensorflow样式(使用tf.cond而不是if,等等)。问题是我需要应用一些非常复杂的函数。因此,我考虑像这样使用tf.py_func: import tensorflow as tf img_path_list = [...] # List of paths to read m

我正在寻找动态读取的图像,并为我的图像分割问题应用数据增强。从目前为止我所看到的情况来看,最好的方法是使用带有
.map
函数的
tf.Dataset
API

然而,从我所看到的示例中,我认为我必须将所有函数调整为tensorflow样式(使用
tf.cond
而不是
if
,等等)。问题是我需要应用一些非常复杂的函数。因此,我考虑像这样使用
tf.py_func

import tensorflow as tf

img_path_list = [...]   # List of paths to read
mask_path_list = [...]  # List of paths to read

dataset = tf.data.Dataset.from_tensor_slices((img_path_list, mask_path_list))

def parse_function(img_path_list, mask_path_list):
    '''load image and mask from paths'''
    return img, mask

def data_augmentation(img, mask):
    '''process data with complex logic'''
    return aug_img, aug_mask

# py_func wrappers
def parse_function_wrapper(img_path_list, mask_path_list):
    return tf.py_func(func=parse_function,
                      inp=(img_path_list, mask_path_list),
                      Tout=(tf.float32, tf.float32))

def data_augmentation_wrapper(img, mask):
    return tf.py_func(func=data_augmentation,
                      inp=(img, mask),
                      Tout=(tf.float32, tf.float32))        

# Maps py_funcs to dataset
dataset = dataset.map(parse_function_wrapper,
                      num_parallel_calls=4)
dataset = dataset.map(data_augmentation_wrapper,
                      num_parallel_calls=4)

dataset = dataset.batch(32)
iter = dataset.make_one_shot_iterator()
imgs, labels = iter.get_next()

然而,从这一点来看,使用
py_func
进行并行处理似乎不起作用。还有其他选择吗?

py_-func受python-GIL的限制,因此不会有太多的并行性。最好的办法是用tensorflow编写数据扩充(或者预计算并序列化到磁盘)


如果您确实想用tensorflow编写它,您可以尝试使用tf.contrib.autograph将简单的python ifs和for循环转换为tf.conds和tf.while\u循环,这可能会大大简化您的代码。

我明白了,所以我可能无法避免将所有内容转换为张量。问题是我的函数非常复杂,涉及到进入dir、加载多个图像、合并它们、处理等等。。。我想知道使用多进程处理Python中的所有内容,然后使用feed_dict传递它是否会比tf.Dataset+py_func方法更快。你对此有什么想法吗?谢谢,多进程可能会让你使用更多的内核,是的。但是要小心,因为tensorflow内部有很多线程,所以多处理不能很好地使用它(因此您只想在分叉进程后导入tensorflow)