Home > Net >  How do I apply custom data augmentation as preprocessing layer in tensorflow?
How do I apply custom data augmentation as preprocessing layer in tensorflow?

Time:06-28

I am performing data augmentation on spectrogram images and mask out time and frequencies as part of preprocessing layers in tensorflow. I am encountering the following:

'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment

here is the code I use:

def random_mask_time(img):
  MAX_OCCURENCE = 5
  MAX_LENGTH = 10
  nums = random.randint(0,MAX_OCCURENCE) # number of masks
  
  for n in range(nums):
    length = random.randint(0, MAX_LENGTH) # number of columns to mask (up to 20px in time)
    pos = random.randint(0, img.shape[0]-MAX_LENGTH) # position to start masking
    img[:,pos:(pos length),:] = 0

  return img


def layer_random_mask_time():
  return layers.Lambda(lambda x: random_mask_time(x))

rnd_time = layer_random_mask_time()

data_augmentation = tf.keras.Sequential([
  rnd_time,
  rnd_freq,
  layers.RandomCrop(input_shape[1], input_shape[0]),
])

I then use it as part of my keras sequential model.

I get that tensors are immutable, but how can I mask out images?

I used this for reference: enter image description here

enter image description here

  • Related