Yes I did the same thing!
Here’s the code
def augment_pitch_and_tempo(spec1,
max_tempo=1.2,
max_pitch=1.1,
min_pitch=0.95):
original_shape = tf.shape(spec1)
print(“original_height=”,original_shape[1])
print(“original_width=”,original_shape[2])
choosen_pitch = tf.random.uniform(shape=(), minval=min_pitch, maxval=max_pitch)
print(“choosen_pitch=”,choosen_pitch)
choosen_tempo = tf.random.uniform(shape=(), minval=1, maxval=max_tempo)
print(“choosen_tempo”,choosen_tempo)
new_width = tf.cast(tf.cast(original_shape[2], tf.float32)*choosen_pitch, tf.int32)
print(“new-width=”,new_width)
new_height = tf.cast(tf.cast(original_shape[1], tf.float32)/(choosen_tempo), tf.int32)
print(“new_height=”,new_height)
spectrogram_aug = tf.image.resize_bilinear(tf.expand_dims(spec1, -1), [new_height, new_width])
print(“bilinear_augmented=”,spectrogram_aug.shape)
spectrogram_aug = tf.image.crop_to_bounding_box(spectrogram_aug, offset_height=0, offset_width=0, target_height=tf.shape(spectrogram_aug)[1], target_width=tf.minimum(original_shape[2],new_width))
print(“crop-to-bounding=”,spectrogram_aug.shape)
spectrogram_aug = tf.cond(choosen_pitch < 1,
lambda: tf.image.pad_to_bounding_box(spectrogram_aug, offset_height=0, offset_width=0,target_height=tf.shape(spectrogram_aug)[1], target_width=original_shape[2]),
lambda: spectrogram_aug)
print(“augmented=”,spectrogram_aug.shape)
return spectrogram_aug[:, :, :, 0]
def augment_speed_up(spec1,
speed_std=0.1):
original_shape = tf.shape(spec1)
choosen_speed = tf.math.abs(tf.random.normal(shape=(), stddev=speed_std)) # abs makes sure the augmention will only speed up
print(“choosen_speed=”,choosen_speed)
choosen_speed = 1 + choosen_speed
print(“final_speed=”,choosen_speed)
new_height = tf.cast(tf.cast(original_shape[1], tf.float32)/(choosen_speed), tf.int32)
print(“new_height=”,new_height)
new_width = tf.cast(tf.cast(original_shape[2], tf.float32), tf.int32)
print(“new_width=”,new_width)
spectrogram_aug = tf.image.resize_bilinear(tf.expand_dims(spec1, -1), [new_height, new_width])
return spectrogram_aug[:, :, :, 0]
And it runs fine now
Thanks a ton again!! :)