简体   繁体   English

在Numpy中预处理Tensorflow张量

[英]Preprocess a Tensorflow tensor in Numpy

I have set up a CNN in Tensorflow where I read my data with a TFRecordReader. 我在Tensorflow中设置了CNN,用TFRecordReader读取数据。 It works well but I would like to do some more preprocessing and data augmentation than offered by the tf.image functions. 它工作得很好,但我想做一些比tf.image函数提供的更多的预处理和数据扩充。 I would specifically like to do some randomized scaling. 我特别喜欢做一些随机缩放。

Is it possible to process a Tensorflow tensor in Numpy? 是否可以在Numpy中处理Tensorflow张量? Or do I need to drop the TFRecordReader and rather do all my preprocessing in Numpy and feed data using the feed_dict? 或者我是否需要删除TFRecordReader而是使用feed_dict在Numpy中进行所有预处理并提供数据? I suspect that the feed_dict method is slow when training on images, but I might be wrong? 我怀疑在训练图像时feed_dict方法很慢,但我可能错了?

If you could create a custom I/O pipeline that fetches intermediate results back from TensorFlow using one or more threads, applies arbitrary Python logic, and then feeds them into a queue for subsequent processing. 如果您可以创建一个自定义I / O管道,使用一个或多个线程从TensorFlow获取中间结果,则应用任意Python逻辑,然后将它们提供给队列以进行后续处理。 The resulting program would be somewhat more complicated, but I suggest you look at the threading and queues HOWTO for information on how to get started. 由此产生的程序会有些复杂,但我建议您查看线程和队列HOWTO以获取有关如何入门的信息。


There is an experimental feature that might make this easier, if you install from source . 如果从源代码安装,有一个实验性功能可能会使这更容易。

If you have already built a preprocessing pipeline using TensorFlow ops, the easiest way to add some custom Python code is to use the tf.py_func() operator, which takes a list of Tensor objects, and a Python function that maps one or more NumPy arrays to one or more NumPy arrays. 如果您已经使用TensorFlow操作构建了预处理管道,添加一些自定义Python代码的最简单方法是使用tf.py_func()运算符,该运算符采用Tensor对象列表,以及映射一个或多个NumPy的Python函数数组到一个或多个NumPy数组。

For example, let's say you have a pipeline like this: 例如,假设您有这样的管道:

reader = tf.TFRecordReader(...)
image_t = tf.image.decode_png(tf.parse_single_example(reader.read(), ...))

...you could use tf.py_func() to apply some custom NumPy processing as follows: ...您可以使用tf.py_func()来应用一些自定义NumPy处理,如下所示:

from scipy import ndimage
def preprocess(array):
  # `array` is a NumPy array containing.
  return ndimage.rotate(array, 45)

image_t = tf.py_func(preprocess, [image_t], [tf.float32])

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

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