使用tensorflow的簡單剪切增廣,為張量賦值的問題

我想在我的圖像上做一個簡單的剪切增強,但我對張量不太熟悉,所以我不知道怎么做。

代碼如下:

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'

有人知道怎么解決這個問題嗎?

編輯:我試過tensorflow插件的隨機剪切功能,但也不起作用。

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)
? 最佳回答:

使用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
主站蜘蛛池模板: 国产亚洲自拍一区| 三上悠亚国产精品一区| 国产人妖视频一区二区| 成人精品一区二区不卡视频| 久久婷婷色一区二区三区| 中文字幕一区二区三区视频在线| 亚洲熟妇成人精品一区| 亚洲一区二区三区高清视频| 国产一区二区电影| 国产午夜精品一区二区| 国产一区二区三区高清视频| 乱人伦一区二区三区| 一区三区三区不卡| 一区二区三区国模大胆| 一区二区三区视频免费| 视频一区视频二区在线观看| 国产AV天堂无码一区二区三区| 性色av无码免费一区二区三区| 亚洲午夜精品一区二区麻豆| 日产亚洲一区二区三区| 末成年女A∨片一区二区| 无码精品视频一区二区三区| 日本道免费精品一区二区| 一区二区乱子伦在线播放| 成人影片一区免费观看| 国产人妖视频一区二区| 精品一区精品二区制服| 亚洲AV网一区二区三区| 日本一区二区三区精品国产| 久久亚洲色一区二区三区| 精品视频一区二区三区在线观看| 国产AV午夜精品一区二区入口 | 伊人色综合网一区二区三区| 人妻夜夜爽天天爽爽一区| 91精品乱码一区二区三区| 亚洲av综合av一区二区三区| 精品视频一区二区| 一区二区乱子伦在线播放| 国产手机精品一区二区| 加勒比精品久久一区二区三区| 精品视频在线观看你懂的一区|