【问题标题】:Simple cut out augmentation using tensorflow, issue with assigning values to tensors使用 tensorflow 进行简单的裁剪增强,将值分配给张量的问题
【发布时间】:2021-12-16 06:28:15
【问题描述】:

我想对我的图像做一个简单的剪切增强,但我对张量不太熟悉,所以我不知道该怎么做。

代码如下:

def augment_cutout(image, label, size=68, n_squares=1):
    h, w, channels = image.shape
    new_image = image
    for _ in range(n_squares):
        y = np.random.randint(h)
        x = np.random.randint(w)
        y1 = np.clip(y - size // 2, 0, h)
        y2 = np.clip(y + size // 2, 0, h)
        x1 = np.clip(x - size // 2, 0, w)
        x2 = np.clip(x + size // 2, 0, w)
        new_image[y1:y2,x1:x2,:] = 0
    return tf.cast(new_image, tf.float32), label
train_dataset = train_dataset.map(map_func = augment_cutout, num_parallel_calls=AUTOTUNE)

以上代码导致如下错误

<ipython-input-209-308483a55fd4>:59 augment_cutout  *
        new_image = new_image[y1:y2,x1:x2,:].assign(0)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:401 __getattr__
        self.__getattribute__(name)

    AttributeError: 'Tensor' object has no attribute 'assign'

有人知道怎么解决吗?

编辑: 我已经尝试过tfa.image.random_cutout 功能,但也不起作用。

import tensorflow_addons as tfa

def random_cut_out(images, labels):
    return tfa.image.random_cutout(images, (64, 64), constant_values = 1), labels

train_dataset = train_ds.map(map_func = preprocess_img, num_parallel_calls=AUTOTUNE) 
train_dataset = train_dataset.map(map_func = augment, num_parallel_calls=AUTOTUNE)
train_dataset = train_dataset.map(random_cut_out)

我收到以下错误:

ValueError: slice index 3 of dimension 0 out of bounds. for '{{node 

cutout/strided_slice_2}} = StridedSlice[Index=DT_INT32, T=DT_INT32, 

begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, 

shrink_axis_mask=1](cutout/Shape, cutout/strided_slice_2/stack, 

cutout/strided_slice_2/stack_1, cutout/strided_slice_2/stack_2)' with input

 shapes: [3], [1], [1], [1] and with computed input tensors: input[1] = <3>,

 input[2] = <4>, input[3] = <1>.

有关如何创建数据集的更多信息:

import tensorflow_datasets as tfds
builder = tfds.ImageFolder('/content/dataset2')
print(builder.info) 
train_ds, val_ds, test_ds = builder.as_dataset(split=['train', 'val', 'test'], shuffle_files=True, as_supervised=True)
def preprocess_img(image, label, img_shape=160):
  image = tf.image.resize(image, [img_shape, img_shape]) # reshape to img_shape
  return tf.cast(image, tf.float32), label # return (float32_image, label) tuple

BATCH_SIZE = 256
AUTOTUNE = tf.data.AUTOTUNE


train_dataset = train_ds.map(map_func = preprocess_img, num_parallel_calls=AUTOTUNE)

train_dataset = train_dataset.shuffle(buffer_size=1000).batch(batch_size=BATCH_SIZE).prefetch(buffer_size=AUTOTUNE)

【问题讨论】:

    标签: python tensorflow deep-learning tensor data-augmentation


    【解决方案1】:

    使用tfa.image.random_cutout 应该完全符合您的要求:

    import tensorflow as tf
    import matplotlib.pyplot as plt
    import tensorflow_addons as tfa
    
    def random_cut_out(images, labels):
        return tfa.image.random_cutout(images, (64, 64), constant_values = 1), labels
    
    flowers = tf.keras.utils.get_file(
        'flower_photos',
        'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
        untar=True)
    
    img_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
    
    ds = tf.data.Dataset.from_generator(
        lambda: img_gen.flow_from_directory(flowers, batch_size=32, shuffle=True),
        output_types=(tf.float32, tf.float32))
    
    ds = ds.map(random_cut_out)
    images, _ = next(iter(ds.take(1)))
    image = images[0]
    
    plt.imshow(image.numpy())
    
    Found 3670 images belonging to 5 classes.
    <matplotlib.image.AxesImage at 0x7f85f5692210>
    

    tfa.image.random_cutout 函数需要一个形状为 (batch_size, height, width, channels) 的张量。因此,当您将单个图像输入自定义函数时,您需要添加一个额外的维度:

    def random_cut_out(image, label):
        image = tf.expand_dims(image, axis=0)
        return tf.squeeze(tfa.image.random_cutout(image, (64, 64), constant_values = 1), axis=0), label
    

    【讨论】:

    • 谢谢,但我使用该方法时出错。这绝对应该是可行的,我只是做错了什么。我用更多信息编辑了我的问题。
    • 更新答案。
    • 现在我没有收到错误,但是当我开始训练时,我收到以下错误;输入 0 与层 model_2 不兼容:预期 shape=(None, None, None, 3), found shape=(None, 1, 185, 185, 3)
    • 再次更新答案。检查底部的功能。它是这样工作的吗?
    • 是的,现在可以了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 2019-05-07
    • 2018-08-27
    • 1970-01-01
    相关资源
    最近更新 更多