from keras.preprocessing import image
import imgaug as ia
from imgaug import augmenters as iaa
sometimes = lambda aug: iaa.Sometimes(0.9, aug)
seq = iaa.Sequential([
iaa.Fliplr(0.5),
sometimes(
iaa.OneOf([
iaa.Affine(
rotate=(-10, 10), # rotate by -45 to +45 degrees
cval=0 # if mode is constant, use a cval between 0 and 255
),
iaa.AddToHueAndSaturation((-20, 20)),
iaa.Add((-20, 20), per_channel=0.5),
iaa.Multiply((0.8, 1.2), per_channel=0.5),
iaa.GaussianBlur((0, 0.5)), # blur images with a sigma between 0 and 3.0
iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5), # improve or worsen the contrast
iaa.Sharpen(alpha=(0, 0.3), lightness=(0.7, 1.3)), # sharpen images
iaa.Emboss(alpha=(0, 0.5), strength=(0, 0.5)) # emboss images
])
),
iaa.Crop(percent=(0, 0.1))
],random_order=True)
def train_generator(df, batch_size):
while True:
df = df.sample(frac=1).reset_index(drop=True)
for start in range(0, df.shape[0], batch_size):
end = min(start + batch_size, df.shape[0])
sub_df = df.iloc[start:end,:]
x_batch = []
y_batch = []
for index, row in sub_df.iterrows():
img_path = '../../data/images/train/%d.jpg'%row['imageId']
img = cv2.imread(img_path)
img = cv2.resize(img,(SIZE, SIZE), interpolation = cv2.INTER_CUBIC)
img = seq.augment_image(img)
img = image.img_to_array(img)
img = preprocess_input(img)
x_batch.append(img)
y_batch.append(row[0:NUMBER_OF_CLASSES])
yield np.array(x_batch), np.array(y_batch)