Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Tensorflow中随机转换或移位一批张量_Python_Tensorflow - Fatal编程技术网

Python 如何在Tensorflow中随机转换或移位一批张量

Python 如何在Tensorflow中随机转换或移位一批张量,python,tensorflow,Python,Tensorflow,我想让我的输入图像(张量)在每个批次中随机上/下或右/左移动 例如,我有一批大小为[10,48,64,1]的灰度图像 如果有一个图像,我知道我可以使用tf.pad和tf.slice(或其他内置函数) 但我想用一次操作将随机移位应用于10个不同的图像 可能吗?或者我应该使用诸如tf.scan之类的循环吗?您正在寻找tf.random\u crop和tf.pad 嗯,当使用tf.random_crop时,随机移位将应用于批处理中的所有图像。批次内的班次相同,但不同批次的班次可能不同 如果您想在批处理

我想让我的输入图像(张量)在每个批次中随机上/下或右/左移动

例如,我有一批大小为
[10,48,64,1]
的灰度图像

如果有一个图像,我知道我可以使用tf.pad和tf.slice(或其他内置函数)

但我想用一次操作将随机移位应用于10个不同的图像


可能吗?或者我应该使用诸如tf.scan之类的循环吗?

您正在寻找
tf.random\u crop
tf.pad

嗯,当使用tf.random_crop时,随机移位将应用于批处理中的所有图像。批次内的班次相同,但不同批次的班次可能不同

如果您想在批处理中使用不同的移位,我认为最好使用队列/输入管道。更多信息,请参阅。 下面是我自己项目的一部分的示例代码
self.image\u names
是一个Python列表,其中包含指向所有培训图像的路径。在输入管道中,数据流就像一个流:您只需要处理一个图像,队列自动处理调度事务(一些线程读取数据,一些线程处理数据,一些线程将单个图像分组成批,另一些线程将数据馈送到GPU等,以保持整个管道的繁忙)。在下面的代码中,
图像
标签
是队列。也就是说,当您处理这个变量时(就像我在
self.data\u augmentation
中所做的那样),您可以认为它只包含一个图像,但实际上队列处理其中的每个项目(就像一个隐式循环),然后
tf.train.shuffle\u batch
将对队列中的训练数据进行洗牌,并将它们分组

def data_augmentation(images):
    if FLAGS.random_flip_up_down:
        images = tf.image.random_flip_up_down(images)
    if FLAGS.random_brightness:
        images = tf.image.random_brightness(images, max_delta=0.3)
    if FLAGS.random_contrast:
        images = tf.image.random_contrast(images, 0.8, 1.2)
    return images

def input_pipeline(self, batch_size, num_epochs=None, aug=False):
    images_tensor = tf.convert_to_tensor(self.image_names, dtype=tf.string)
    labels_tensor = tf.convert_to_tensor(self.labels, dtype=tf.int64)
    input_queue = tf.train.slice_input_producer([images_tensor, labels_tensor], num_epochs=num_epochs)

    labels = input_queue[1]
    images_content = tf.read_file(input_queue[0])
    images = tf.image.convert_image_dtype(tf.image.decode_png(images_content, channels=1), tf.float32)
    if aug:
        images = self.data_augmentation(images)
    new_size = tf.constant([FLAGS.image_size, FLAGS.image_size], dtype=tf.int32)
    images = tf.image.resize_images(images, new_size)
    image_batch, label_batch = tf.train.shuffle_batch([images, labels], batch_size=batch_size, capacity=50000,
                                                      min_after_dequeue=10000)
    # print 'image_batch', image_batch.get_shape()
    return image_batch, label_batch

你在找
tf.random\u crop
tf.pad

嗯,当使用tf.random_crop时,随机移位将应用于批处理中的所有图像。批次内的班次相同,但不同批次的班次可能不同

如果您想在批处理中使用不同的移位,我认为最好使用队列/输入管道。更多信息,请参阅。 下面是我自己项目的一部分的示例代码
self.image\u names
是一个Python列表,其中包含指向所有培训图像的路径。在输入管道中,数据流就像一个流:您只需要处理一个图像,队列自动处理调度事务(一些线程读取数据,一些线程处理数据,一些线程将单个图像分组成批,另一些线程将数据馈送到GPU等,以保持整个管道的繁忙)。在下面的代码中,
图像
标签
是队列。也就是说,当您处理这个变量时(就像我在
self.data\u augmentation
中所做的那样),您可以认为它只包含一个图像,但实际上队列处理其中的每个项目(就像一个隐式循环),然后
tf.train.shuffle\u batch
将对队列中的训练数据进行洗牌,并将它们分组

def data_augmentation(images):
    if FLAGS.random_flip_up_down:
        images = tf.image.random_flip_up_down(images)
    if FLAGS.random_brightness:
        images = tf.image.random_brightness(images, max_delta=0.3)
    if FLAGS.random_contrast:
        images = tf.image.random_contrast(images, 0.8, 1.2)
    return images

def input_pipeline(self, batch_size, num_epochs=None, aug=False):
    images_tensor = tf.convert_to_tensor(self.image_names, dtype=tf.string)
    labels_tensor = tf.convert_to_tensor(self.labels, dtype=tf.int64)
    input_queue = tf.train.slice_input_producer([images_tensor, labels_tensor], num_epochs=num_epochs)

    labels = input_queue[1]
    images_content = tf.read_file(input_queue[0])
    images = tf.image.convert_image_dtype(tf.image.decode_png(images_content, channels=1), tf.float32)
    if aug:
        images = self.data_augmentation(images)
    new_size = tf.constant([FLAGS.image_size, FLAGS.image_size], dtype=tf.int32)
    images = tf.image.resize_images(images, new_size)
    image_batch, label_batch = tf.train.shuffle_batch([images, labels], batch_size=batch_size, capacity=50000,
                                                      min_after_dequeue=10000)
    # print 'image_batch', image_batch.get_shape()
    return image_batch, label_batch

或者,您也可以使用参数a2和b2来转换图像:

import numpy as np
import tensorflow as tf

image1 = np.array([[[.1], [.1], [.1], [.1]],
                  [[.2], [.2], [.2], [.2]],
                  [[.3], [.3], [.3], [.3]],
                  [[.4], [.4], [.4], [.4]]])
image2 = np.array([[[.1], [.2], [.3], [.4]],
                  [[.1], [.2], [.3], [.4]],
                  [[.1], [.2], [.3], [.4]],
                  [[.1], [.2], [.3], [.4]]])
images = np.stack([image1, image2])
images_ = tf.convert_to_tensor(images, dtype=tf.float32)

shift1_x = 1
shift1_y = 2
shift2_x = -1
shift2_y = 0
transforms_ = tf.convert_to_tensor([[1, 0, -shift1_x, 0, 1, -shift1_y, 0, 0],
                                   [1, 0, -shift2_x, 0, 1, -shift2_y, 0, 0]],
                                   tf.float32)
shifted_ = tf.contrib.image.transform(images=images_,
                                      transforms=transforms_)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    shifted = sess.run([shifted_])
    print(shifted)
变换投影矩阵也可以是大小为nx8的张量,因此可以对批次的每个图像进行不同的移位。这可以很容易地扩展为包括每个图像的x/y偏移的一些随机性

编辑: 要对批次的每个图像使用随机移位,请执行以下操作:

...
images_ = tf.convert_to_tensor(images, dtype=tf.float32)

num_imgs = images.shape[0]
base_ = tf.convert_to_tensor(np.tile([1, 0, 0, 0, 1, 0, 0, 0], [num_imgs, 1]), dtype=tf.float32)
mask_ = tf.convert_to_tensor(np.tile([0, 0, 1, 0, 0, 1, 0, 0], [num_imgs, 1]), dtype=tf.float32)
random_shift_ = tf.random_uniform([num_imgs, 8], minval=-2.49, maxval=2.49, dtype=tf.float32)
transforms_ = base_ + random_shift_ * mask_

shifted_ = tf.contrib.image.transform(images=images_,
                                      transforms=transforms_)
...
编辑2: 为了完成此操作,此处仅使用另一个辅助函数对批次的每个图像应用随机旋转和移位:

def augment_data(input_data, angle, shift):
    num_images_ = tf.shape(input_data)[0]
    # random rotate
    processed_data = tf.contrib.image.rotate(input_data,
                                             tf.random_uniform([num_images_],
                                                               maxval=math.pi / 180 * angle,
                                                               minval=math.pi / 180 * -angle))
    # random shift
    base_row = tf.constant([1, 0, 0, 0, 1, 0, 0, 0], shape=[1, 8], dtype=tf.float32)
    base_ = tf.tile(base_row, [num_images_, 1])
    mask_row = tf.constant([0, 0, 1, 0, 0, 1, 0, 0], shape=[1, 8], dtype=tf.float32)
    mask_ = tf.tile(mask_row, [num_images_, 1])
    random_shift_ = tf.random_uniform([num_images_, 8], minval=-shift, maxval=shift, dtype=tf.float32)
    transforms_ = base_ + random_shift_ * mask_

    processed_data = tf.contrib.image.transform(images=processed_data,
                                                transforms=transforms_)
    return processed_data

或者,您也可以使用参数a2和b2来转换图像:

import numpy as np
import tensorflow as tf

image1 = np.array([[[.1], [.1], [.1], [.1]],
                  [[.2], [.2], [.2], [.2]],
                  [[.3], [.3], [.3], [.3]],
                  [[.4], [.4], [.4], [.4]]])
image2 = np.array([[[.1], [.2], [.3], [.4]],
                  [[.1], [.2], [.3], [.4]],
                  [[.1], [.2], [.3], [.4]],
                  [[.1], [.2], [.3], [.4]]])
images = np.stack([image1, image2])
images_ = tf.convert_to_tensor(images, dtype=tf.float32)

shift1_x = 1
shift1_y = 2
shift2_x = -1
shift2_y = 0
transforms_ = tf.convert_to_tensor([[1, 0, -shift1_x, 0, 1, -shift1_y, 0, 0],
                                   [1, 0, -shift2_x, 0, 1, -shift2_y, 0, 0]],
                                   tf.float32)
shifted_ = tf.contrib.image.transform(images=images_,
                                      transforms=transforms_)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    shifted = sess.run([shifted_])
    print(shifted)
变换投影矩阵也可以是大小为nx8的张量,因此可以对批次的每个图像进行不同的移位。这可以很容易地扩展为包括每个图像的x/y偏移的一些随机性

编辑: 要对批次的每个图像使用随机移位,请执行以下操作:

...
images_ = tf.convert_to_tensor(images, dtype=tf.float32)

num_imgs = images.shape[0]
base_ = tf.convert_to_tensor(np.tile([1, 0, 0, 0, 1, 0, 0, 0], [num_imgs, 1]), dtype=tf.float32)
mask_ = tf.convert_to_tensor(np.tile([0, 0, 1, 0, 0, 1, 0, 0], [num_imgs, 1]), dtype=tf.float32)
random_shift_ = tf.random_uniform([num_imgs, 8], minval=-2.49, maxval=2.49, dtype=tf.float32)
transforms_ = base_ + random_shift_ * mask_

shifted_ = tf.contrib.image.transform(images=images_,
                                      transforms=transforms_)
...
编辑2: 为了完成此操作,此处仅使用另一个辅助函数对批次的每个图像应用随机旋转和移位:

def augment_data(input_data, angle, shift):
    num_images_ = tf.shape(input_data)[0]
    # random rotate
    processed_data = tf.contrib.image.rotate(input_data,
                                             tf.random_uniform([num_images_],
                                                               maxval=math.pi / 180 * angle,
                                                               minval=math.pi / 180 * -angle))
    # random shift
    base_row = tf.constant([1, 0, 0, 0, 1, 0, 0, 0], shape=[1, 8], dtype=tf.float32)
    base_ = tf.tile(base_row, [num_images_, 1])
    mask_row = tf.constant([0, 0, 1, 0, 0, 1, 0, 0], shape=[1, 8], dtype=tf.float32)
    mask_ = tf.tile(mask_row, [num_images_, 1])
    random_shift_ = tf.random_uniform([num_images_, 8], minval=-shift, maxval=shift, dtype=tf.float32)
    transforms_ = base_ + random_shift_ * mask_

    processed_data = tf.contrib.image.transform(images=processed_data,
                                                transforms=transforms_)
    return processed_data

是否可以在批次内应用所有不同的随机移位?是否可以在批次内应用所有不同的随机移位?