I am trying to vectorize this method that I am using for image augmentation in ML:
def random_erase_from_image(images, random_erasing, image_size):
#could probably be vectorized to speed up
to_return = images
for t in range(images.shape[0]):
if np.random.randint(0, 2) == 0:#do random erasing
x_erase_size = np.random.randint(0, random_erasing)
y_erase_size = np.random.randint(0, random_erasing)
x_erase_start = np.random.randint(0, image_size-x_erase_size)
y_erase_start = np.random.randint(0, image_size-y_erase_size)
shape = to_return[t, y_erase_start:y_erase_start+y_erase_size, x_erase_start:x_erase_start+x_erase_size, :].shape
print(shape)
to_return[t, y_erase_start:y_erase_start+y_erase_size, x_erase_start:x_erase_start+x_erase_size, :] = (np.random.random(shape) * 255).astype('uint8')
return images
This is as far as I have gotten, but don't know how to slice properly.
def random_erase_vec(images, random_erasing, image_size):
#could probably be vectorized to speed up
to_return = images
mask = np.random.choice(a=[False, True], size=images.shape[0], p=[.5, .5])
x_erase_size = np.random.randint(0, random_erasing, size=images.shape[0])
y_erase_size = np.random.randint(0, random_erasing, size=images.shape[0])
x_erase_start = np.random.randint(0, image_size-x_erase_size, size=images.shape[0])
y_erase_start = np.random.randint(0, image_size-y_erase_size, size=images.shape[0])
random_values = (np.random.random((images.shape))* 255).astype('uint8')
to_return[:, [y_erase_start[:]]:[y_erase_start[:]+y_erase_size[:]], [x_erase_start[:]]:[x_erase_start[:]+x_erase_size[:]], :] = random_values[:, [y_erase_start[:]]:[y_erase_start[:]+y_erase_size[:]], [x_erase_start[:]]:[x_erase_start[:]+x_erase_size[:]], :]
return images
I am trying to avoid reshaping, but if that is what is needed, I guess it will do. Let me know any ways you can think of to speed up the original method.
I am getting this error on the slicing line: "slice indices must be integers or None or have an index method"
I also want to mask so not all images are randomly erased, but I want to do that after I get the slicing part completed.
Thank you for your help.
Edit: Example inputs:
images: numpy array with dimensions [# of images, height (32), width (32), channels (3)
random_erasing: poorly names, but the max size of the image in either dimension to be erased. Currently set to 20
image_size: Could have gotten from the images array now that I think about it, but cleaning up hasn't been a priority yet