有没有办法实现张量图像剪切/旋转/平移?

2024-09-23 14:35:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试做不同种类的(图像)数据增强来训练我的神经网络。在

我知道tf.图像提供了一些增强功能,但它们太简单了-例如,我只能将图像旋转90度,而不是任何角度。在

我也知道tf.keras.预处理图像提供随机旋转、随机剪切、随机移动和随机缩放。但是这些方法只能应用于numpy数组,而不能应用于张量。在

我知道我可以先读图像,使用tf.keras.预处理.image进行增强,然后将这些增强的numpy数组转换为张量。在

但是,我只是想知道是否有一种方法可以实现张量方面的增强,这样我就不必费心于“图像文件->张量->numpy数组->张量”过程。在


为那些想知道如何应用转换的人更新:

对于详细的源代码,您可能需要检查tf.contrib.image.transform和{a2}。在

这是我的代码:

def transformImg(imgIn,forward_transform):
    t = tf.contrib.image.matrices_to_flat_transforms(tf.linalg.inv(forward_transform))
    # please notice that forward_transform must be a float matrix,
    # e.g. [[2.0,0,0],[0,1.0,0],[0,0,1]] will work
    # but [[2,0,0],[0,1,0],[0,0,1]] will not
    imgOut = tf.contrib.image.transform(imgIn, t, interpolation="BILINEAR",name=None)
    return imgOut

基本上,上面的代码

enter image description here表示imgIn中的每个点(x,y)。在

例如,与x轴平行的shear transform

enter image description here

因此,我们可以这样实现剪切变换(使用上面定义的transformImg()):

^{pr2}$

原始图像: enter image description here

转换后:enter image description here

(请注意,img是张量,不包括将张量转换为图像文件的代码。)

p.S.

以上代码适用于tensorflow 1.10.1,可能不适用于将来的版本。

老实说,我真的不知道他们为什么要设计tf.contrib.image文件。以我们必须使用其他函数的方式进行转换(利纳格投资基金)为了得到我们想要的。我真的希望他们能改变tf.contrib.image文件。转换为在a more intuitive way中工作。


Tags: 方法代码图像imagenumpytf图像文件transform
2条回答

看看^{}。它允许对图像应用一般的投影变换。在

您还需要查看^{},将仿射矩阵转换为tf.contrib.image.transform接受的投影格式。在

我通常使用tf.data.Dataset和{}和{}。Dataset.prefetch意味着通常没有时间开销(只要CPU上的预处理比在GPU上运行网络花费的时间少)。如果你是跨多个GPU操作,你可能需要重新考虑,但下面的工作对我在单GPU系统上很好。在

为了简单起见,我假设您的所有图像都在磁盘上的不同文件中,尽管它可以很容易地适应zip存档或其他格式,如hdf5(不适用于.tar文件-不知道为什么,但我怀疑这是否是个好主意)

import tensorflow as tf
from PIL import Image


def map_tf(path_tensor, label_tensor):
    # path_tensor and label_tensor correspond to a single example

    def map_np(path_str):
        # path_str is just a normal string here
        image = np.array(Image.load(path_str), dtype=np.uint8)
        image = any_cv2_or_numpy_augmentations(image)
        return image,

    image, = tf.py_func(
        map_np, (path_tensor,), Tout=(tf.uint8,), stateful=False)
    # any tensorflow operations here.
    image = tf.cast(image, tf.float32) / 255

    image.set_shape((224, 224, 3))
    return image, label


paths, labels = load_image_paths_and_labels()
dataset = tf.data.Dataset.from_tensor_slices((paths, labels))
if is_training:
    shuffle_buffer = len(paths)  # full shuffling - can be shorter
    dataset = dataset.shuffle(shuffle_buffer).repeat()
dataset = dataset.map(map_tf_fn, num_parallel_calls=8)
dataset = dataset.batch(batch_size)

dataset = dataset.prefetch(1)
# play with the following if you want - not finalized API, and only in
# more recent version of tensorflow
# dataset = dataset.apply(tf.contrib.data.prefetch_to_device('/gpu:0'))

image_batch, label_batch = dataset.make_one_shot_iterator().get_next()

您也可以在tensorflow中进行解码,并直接在py_func中使用any_cv2_or_numpy_augmentations(尽管您没有避免您在问题中提到的张量->;numpy->;张量舞蹈)。我怀疑你会注意到性能的不同。在

有关更多选项,请查看this answer。在

相关问题 更多 >