<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
import scipy as math
import matplotlib.pyplot as plt
import seaborn as sns
import pickle
# from PIL import Image

from sklearn.model_selection import train_test_split
import keras

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

from keras.models import Model, Sequential
from keras import layers
from keras.layers import Dense, Input, Dropout, GlobalAveragePooling2D, Flatten, Conv2D, BatchNormalization, Activation, MaxPooling2D
from keras import optimizers
from keras.optimizers import Adam
from keras.models import load_model

import tensorflow as tf

import sys
import os

from google.colab import drive
drive.mount('/content/drive')
%cd drive/My\ Drive/
train_path = 'data/asl_alphabet_train'
validation_path = 'data/asl_alphabet_validation'
sam_path = 'data/asl_extra_val'
%ls
key_dict = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 
            'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9, 
            'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 
            'P': 15, 'Q': 16, 'R': 17, 'S': 18, 'T': 19, 
            'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24, 
            'Z': 25, 'nothing': 26, 'space': 27}
input_size=150
letter = load_img(os.path.join(sam_path, 'A/A_sam.jpg'), color_mode='grayscale')#, target_size=(input_size, input_size))
letter_array = img_to_array(letter)
letter_array = np.expand_dims(letter_array, axis=0)
letter = tf.image.central_crop(letter_array, 1.0)
letter.shape
y_proba = model.predict(letter_array)
proba_index = y_proba.argmax(axis=-1) - 1
letter_predict = list(key_dict.keys())[list(key_dict.values()).index(proba_index)]
letter_predict
batch_size = 32
datagen = keras.preprocessing.image.ImageDataGenerator(
                rescale=1./255,
                validation_split=0.3) 

train_generator = datagen.flow_from_directory(
                train_path,  #directory for training images
                target_size=(input_size, input_size),
                batch_size=batch_size,
                class_mode='categorical',
                color_mode='grayscale',
                shuffle=True,
                subset='training')
print('done')
test_generator = datagen.flow_from_directory(
                train_path,  # same directory for testing images
                target_size=(input_size, input_size),
                batch_size=batch_size,
                class_mode='categorical',
                color_mode='grayscale',
                shuffle=True,
                subset='validation')
print('done')


image_size = 128
batch_size = 32
valgen = keras.preprocessing.image.ImageDataGenerator(rescale=1./255) 
val_generator = valgen.flow_from_directory(
                validation_path,  # directory for validation images
                target_size=(image_size, image_size),
                batch_size=batch_size,
                class_mode='categorical',
                color_mode='grayscale',
                shuffle=False)

samgen = keras.preprocessing.image.ImageDataGenerator(rescale=1./255, preprocessing_function=(lambda x: tf.image.central_crop(x, central_fraction=1)))
sam_generator = samgen.flow_from_directory(
                sam_path,  # directory for me-made validation images
                target_size=(image_size, image_size),
                batch_size=batch_size,
                class_mode='categorical',
                color_mode='grayscale',
                shuffle=False)
model_path = 'asl_alphabet_image_classification/models/'
# base_model = load_model(os.path.join(model_path,'base_model.h5'))
# base_inception = load_model(os.path.join(model_path,'base__inceptionv3.h5'))
# model1 = load_model(os.path.join(model_path,'model1.h5'))
# model2 = load_model(os.path.join(model_path,'model2.h5'))
model3 = load_model(os.path.join(model_path, 'model3.h5'))
model3.evaluate_generator(val_generator, verbose=1)
def prob(data):
    return np.array(list(zip(1-model.predict(data),model.predict(data))))
!pip install lime

# Improting Image class from PIL module 
from PIL import Image 
  
# Opens a image in RGB mode 
# im = Image.open('data/asl_extra_val/A/A_sam.jpg')
im = load_img(os.path.join(validation_path, 'A/A_test.jpg'), color_mode='grayscale') 

# Size of the image in pixels (size of orginal image) 
# (This is not mandatory) 
width, height = im.size 
  
# Setting the points for cropped image 
left = (width-height)/2
top = 0 
right = width - ((width-height)/2)
bottom = height
  
# Cropped image of above dimension 
# (It will not change orginal image) 
im1 = im.crop((left, top, right, bottom))
im1 = im1.resize((128,128))

im = load_img(os.path.join(validation_path, 'D/D_test.jpg'), color_mode='grayscale')
im = im1.resize((128,128))
im =img_to_array(im)
im = np.expand_dims(im, axis=0)
predict = model3.predict(im, steps=1)
np.argmax(predict)
import lime
import lime.lime_image
explainer = lime.lime_image.LimeImageExplainer()
im2 = tf.convert_to_tensor(img_to_array(im1))
im2 = np.expand_dims(im2, axis=0)
# print(im2.ndim)
predict = model3.predict(im, steps=1)
predict
def predict_on(image):
  return model3.predict(image, steps=1)
exp = explainer.explain_instance(val_generator[0][0][0], model3.predict_generator, num_samples=1)
np.argmax(predict)
# predict
preds = model3.predict_generator(val_generator).argmax(-1)
true = val_generator.labels
key_dict = val_generator.class_indices
key_dict
letters = []
trues = []
for letter in preds:
  letter_predict = list(key_dict.keys())[list(key_dict.values()).index(letter)]
  letters.append(letter_predict)
for letter in true:
  letter_actual = list(key_dict.keys())[list(key_dict.values()).index(letter)]
  trues.append(letter_actual)
print('model predictions:', letters)
print('true labels:      ', trues)
model3.summary()
base_model.evaluate_generator(val_generator, verbose=1)
del model<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12274736484189051, 'fraction_numerical': 0.022951377082624957, 'mean_word_length': 3.9106844741235394, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '616414', 'license': 'MIT', 'n_tokens_mistral': 2099, 'n_tokens_neox': 1966, 'n_words': 422, 'path': 'notebooks/exploratory/model_test.ipynb', 'repo_name': 'sam-thurman/asl_alphabet_image_classification', 'size': '5882'}