text_plain_output_1.png
import cv2

import os

import random

import tensorflow

from imgaug import augmenters as iaa
import numpy as np
import time
import random
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
import os
from sklearn.utils.multiclass import unique_labels
import cv2
import matplotlib.pyplot as plt
import seaborn as sn
import tensorflow
layers = tensorflow.keras.layers
BatchNormalization = tensorflow.keras.layers.BatchNormalization
Conv2D = tensorflow.keras.layers.Conv2D
Flatten = tensorflow.keras.layers.Flatten
MaxPooling2D = tensorflow.keras.layers.MaxPooling2D
Dropout = tensorflow.keras.layers.Dropout
Dense = tensorflow.keras.layers.Dense
ImageDataGenerator = tensorflow.keras.preprocessing.image.ImageDataGenerator
Sequential = tensorflow.keras.Sequential
TensorBoard = tensorflow.keras.callbacks.TensorBoard
ModelCheckpoint = tensorflow.keras.callbacks.ModelCheckpoint
Adam = tensorflow.keras.optimizers.Adam
regularizers = tensorflow.keras.regularizers
categorical_crossentropy = tensorflow.keras.losses
K = tensorflow.keras.backend
plot_model = tensorflow.keras.utils.plot_model
from nltk.corpus import words
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
DATADIR = '../input/lse-alphabet/train_data'
CATEGORIES = ['A_PD', 'A_PF', 'B', 'C', 'CH', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'LL', 'M', 'N', 'Ñ', 'O', 'P', 'Q', 'R', 'RR', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'nothing']
TEST_DIR = '../input/lse-alphabet/test_data'
NUM_CATEGORIES = 32

def create_training_data(DIR):
    """This function is run for each model in order to get the training data from the filepath 
    and convert it into array format"""
    training_data = []
    for category in CATEGORIES:
        path = os.path.join(DIR, category)
        class_num = CATEGORIES.index(category)
        for img in os.listdir(path):
            try:
                img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_COLOR)
                new_array = cv2.resize(img_array, (64, 64))
                final_img = cv2.cvtColor(new_array, cv2.COLOR_BGR2RGB)
                training_data.append([final_img, class_num])
                seq = iaa.Sequential([iaa.Multiply((0.8, 1.2), per_channel=0.2), iaa.Crop(px=(0, 16)), iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5)))])
                final_img_aug = seq.augment_images([final_img])
                training_data.append([final_img, class_num])
                training_data.append([final_img_aug[0], class_num])
            except Exception as e:
                pass
    return training_data

def additional_augmenation(image):
    randomValue = random.randint(1, 10)
    if randomValue == 9:
        aug = iaa.GaussianBlur(sigma=(0, 0.5))
        return aug.augment_image(image)
    elif randomValue == 8:
        aug = iaa.Multiply((0.8, 1.2), per_channel=0.2)
        return aug.augment_image(image)
    elif randomValue == 7:
        aug = iaa.Crop(px=(0, 16))
        return aug.augment_image(image)
    else:
        return image

input_shape = (64, 64, 3)
training_data = create_training_data(DATADIR)
print(len(training_data))
random.shuffle(training_data)