<jupyter_start><jupyter_text>Data Preparation<jupyter_code>'''
    - imports for data processing
    - Organize data into train, valid, test dirs (70/15/15)
    - Color Data Augmentation
'''
# imports for data processing
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input
import os
import shutil
import random
import numpy as np
import matplotlib.pyplot as plt
# Organize data into train, valid, test dirs
os.chdir('C:/Users/Anastasia/Projects/ASL/ASL')
if os.path.isdir('train/A') is False: 
    os.mkdir('train')
    os.mkdir('valid')
    os.mkdir('test')

    for filename in os.listdir('C:/Users/Anastasia/Projects/ASL/ASL'):
        if filename != 'train' and filename != 'valid' and filename != 'test':
            shutil.move(f'{filename}', 'train')
            os.mkdir(f'valid/{filename}')
            os.mkdir(f'test/{filename}')

            valid_samples = random.sample(os.listdir(f'train/{filename}'), 300)
            for j in valid_samples:
                shutil.move(f'train/{filename}/{j}', f'valid/{filename}')

            test_samples = random.sample(os.listdir(f'train/{filename}'), 300)
            for k in test_samples:
                shutil.move(f'train/{filename}/{k}', f'test/{filename}')
os.chdir('../..')
# Color Data Augmentation
gen = ImageDataGenerator(
    rotation_range=10, 
    width_shift_range=0.1,
    height_shift_range=0.1, 
    brightness_range=(0.2, 0.8), 
    shear_range=0.15, 
    zoom_range=0.15,
    channel_shift_range=0.15,
    horizontal_flip=True, 
    vertical_flip=True, 
)

for filename in os.listdir('C:/Users/Anastasia/Projects/ASL/ASL/test'):
    for image in os.listdir('C:/Users/Anastasia/Projects/ASL/ASL/test/' + filename):
        image_path = 'C:/Users/Anastasia/Projects/ASL/ASL/test/' + filename + '/' + image
        image = np.expand_dims(plt.imread(image_path),0)
        aug_iter = gen.flow(image, save_to_dir='C:/Users/Anastasia/Projects/ASL/ASL/test/' + filename, save_prefix='aug-image-', save_format='jpeg')
        aug_images = [next(aug_iter)[0].astype(np.uint8) for i in range(4)]

for filename in os.listdir('C:/Users/Anastasia/Projects/ASL/ASL/train'):
    for image in os.listdir('C:/Users/Anastasia/Projects/ASL/ASL/train/' + filename):
        image_path = 'C:/Users/Anastasia/Projects/ASL/ASL/train/' + filename + '/' + image
        image = np.expand_dims(plt.imread(image_path),0)
        aug_iter = gen.flow(image, save_to_dir='C:/Users/Anastasia/Projects/ASL/ASL/train/' + filename, save_prefix='aug-image-', save_format='jpeg')
        aug_images = [next(aug_iter)[0].astype(np.uint8) for i in range(4)]

for filename in os.listdir('C:/Users/Anastasia/Projects/ASL/ASL/valid'):
    for image in os.listdir('C:/Users/Anastasia/Projects/ASL/ASL/valid/' + filename):
        image_path = 'C:/Users/Anastasia/Projects/ASL/ASL/valid/' + filename + '/' + image
        image = np.expand_dims(plt.imread(image_path),0)
        aug_iter = gen.flow(image, save_to_dir='C:/Users/Anastasia/Projects/ASL/ASL/valid/' + filename, save_prefix='aug-image-', save_format='jpeg')
        aug_images = [next(aug_iter)[0].astype(np.uint8) for i in range(4)]
# Devision to train/ valid, and test

train_path = 'C:/Users/Anastasia/Projects/ASL/ASL/train'
valid_path = 'C:/Users/Anastasia/Projects/ASL/ASL/valid'
test_path = 'C:/Users/Anastasia/Projects/ASL/ASL/test'

train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.inception_v3.preprocess_input).flow_from_directory(
    directory=train_path, target_size=(224,224), batch_size=10)
valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.inception_v3.preprocess_input).flow_from_directory(
    directory=valid_path, target_size=(224,224), batch_size=10)
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.inception_v3.preprocess_input).flow_from_directory(
    directory=test_path, target_size=(224,224), batch_size=10, shuffle=False)<jupyter_output>Found 248526 images belonging to 29 classes.
Found 41442 images belonging to 29 classes.
Found 41501 images belonging to 29 classes.<jupyter_text>Training<jupyter_code>import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.applications import imagenet_utils
from tensorflow.keras.callbacks import TensorBoard, EarlyStopping, ModelCheckpoint
from sklearn.metrics import confusion_matrix
import itertools
import os
import shutil
import random
import matplotlib.pyplot as plt
import datetime
%matplotlib inline
%load_ext tensorboard
base_model = tf.keras.applications.inception_v3.InceptionV3()
x = base_model.output
predictions = Dense(29, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
for layer in model.layers[:-30]:
    layer.trainable = False
model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x=train_batches,
            steps_per_epoch=len(train_batches),
            validation_data=valid_batches,
            validation_steps=len(valid_batches),
            epochs=30,
            verbose=2
)
# saving model
model.save('C:/Users/Anastasia/Projects/ASL/ASL_model')<jupyter_output>WARNING:tensorflow:From C:\Users\Anastasia\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\training\tracking\tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
WARNING:tensorflow:From C:\Users\Anastasia\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\training\tracking\tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
INFO:tensorflow:Assets written to: C:/Users/Anastasia/Projects/ASL/ASL_model\assets<jupyter_text>Test batch testing<jupyter_code>from pylab import rcParams
test_labels = test_batches.classes
predictions = model.predict(x=test_batches, steps=len(test_batches), verbose=0)
count = 0
for i in range(len(test_labels)):
    if (test_labels[i] != predictions.argmax(axis=1)[i]):
        count = count + 1
print('Test accuracy = ', (len(test_labels)-count)/len(test_labels))
cm = confusion_matrix(y_true=test_labels, y_pred=predictions.argmax(axis=1))
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
            horizontalalignment="center",
            color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    rcParams['figure.figsize'] = 5, 10
test_batches.class_indices
cm_plot_labels = ['0','1','2','3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28']
plot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix - ASL')<jupyter_output>Confusion matrix, without normalization
[[1402    0    0    0   13    0    0    0    0    0    0    0    3    0
     0    0    0    0    6    4    0    0    2    0    0    6    0    0
     0]
 [   0 1400    0    1   14    1    0    0    4    0    1    0    2    0
     0    0    0    0    1    0    3    0    1    0    0    0    0    1
     0]
 [   0    4 1422    1    3    0    0    0    2    0    0    0    0    0
     1    0    1    0    0    0    0    0    0    0    0    0    1    0
     2]
 [   0    1    1 1387    3    0    0    0    2    0    1    0    0    0
    16    4    0    0    0    0    1    1    0    0    0    0    0    0
     0]
 [   8    7    0    0 1391    0    0    0    7    0    0    0    0    1
     0    0    0    1    7    2    5    0    0    0    0    4    0    1
     0]
 [   0    0    3    0    3 1412    0    0    0    0    0    1    0    0
     1    0    0    0    0    1    0    0    1    0    0    0    0    0
     0]
 [   0    0    1    0    0    0 1384   24    3  [...]<jupyter_text>Random Sample testing<jupyter_code>import tensorflow as tf
from tensorflow.keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator
from tensorflow.keras.applications.inception_v3 import preprocess_input
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
%matplotlib inline

def prediction(image_path, model):
    
    #Plot the image
    img = mpimg.imread(image_path)
    imgplot = plt.imshow(img)
    
    #load the image
    my_image = load_img(image_path, target_size=(224, 224))

    #preprocess the image
    my_image = img_to_array(my_image)
    my_image = my_image.reshape((1, my_image.shape[0], my_image.shape[1], my_image.shape[2]))
    my_image = preprocess_input(my_image)

    #make the prediction
    prediction = model.predict(my_image)
    return prediction.argmax(axis=1)
# Test 1
print(prediction('C:/Users/Anastasia/Projects/ASL/ASL/test/nothing/aug-image-_0_106.jpeg', model))
# Test 2
print(prediction('C:/Users/Anastasia/Projects/ASL/ASL/test/M/aug-image-_0_54.jpeg', model))<jupyter_output>[12]
{'fraction_non_alphanumeric': 0.11878291398478642, 'fraction_numerical': 0.0404720109225668, 'mean_word_length': 3.1771894093686353, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '47739', 'license': 'MIT', 'n_tokens_mistral': 3705, 'n_tokens_neox': 3510, 'n_words': 783, 'path': 'ASL_Code.ipynb', 'repo_name': 'avganshina/ASL', 'size': '10254'}