I am trying to generate 90 and 270 degrees rotated versions of my sample images on the fly during training. I found an example and modifying it. But I am confused about what should be the order? For instance in one batch I have 32 images and my image generator should return total 64 images. Let's say upper case letters are 90 degree and lower case letters are 270 degree rotated images. Should the order be AaBbCc or ABCabc? I apply the same to the validation set. Here is the related code fragment:
Edit: Code fragment added.
def _get_batches_of_transformed_samples(self, index_array):
# create list to hold the images
batch_x = []
# create list to hold the labels
batch_y = []
# rotation angles
target_angles = [0, 90, 180, 270]
angle_categories = list(range(0, len(target_angles)))
self.classes = target_angles
self.class_indices = angle_categories
# generate rotated images and corresponding labels
for i, j in enumerate(index_array):
is_color = int(self.color_mode == 'rgb')
image = cv2.imread(self.filenames[j], is_color)
if is_color:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
for rotation_angle, cat_angle in zip(target_angles, angle_categories):
rotated_im = rotate(image, rotation_angle, self.target_size[:2])
if self.preprocess_func: rotated_im = self.preprocess_func(rotated_im)
# add dimension to account for the channels if the image is greyscale
if rotated_im.ndim == 2: rotated_im = np.expand_dims(rotated_im, axis=2)
batch_x.append(rotated_im)
batch_y.append(cat_angle)
# convert lists to numpy arrays
batch_x=np.asarray(batch_x)
batch_y=np.asarray(batch_y)
batch_y = to_categorical(batch_y, len(target_angles))
return batch_x, batch_y
I actually rotate them as 0, 90, 180 and 270 degrees. As seen in the code, for each batch I return all the rotated versions of all the images in the batch. But is this correct or should I return first 0 degree rotated versions, second 90 degree rotated versions so on?
Edit2: I checked my previous work which I use the built in Keras ImageDataGenerator
. generator.classes
returns [zeros(100,1); ones(100,1)]. In that study I only have two classes. I understand that Keras indexes the images as [class1, class2, ...]. I think I have to do the same.