<jupyter_start><jupyter_text>Code for cuda CNN


Mounts the Google Drive<jupyter_code>from google.colab import drive
drive.mount('/content/drive')<jupyter_output>Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).<jupyter_text>Import statements<jupyter_code>import numpy as np 
%tensorflow_version 1.x
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
import os
import cv2
import keras
import torch as th
import torchvision
import torchvision.transforms as transforms
th.manual_seed(42)
import time 
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Conv2D, Dense, Dropout, Flatten
from keras.layers import Flatten, Dense
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from keras import regularizers
tf.set_random_seed(2)
print(tf.__version__)
np.random.seed(5)
tf.set_random_seed(2)
start_time = time.time()<jupyter_output>1.15.2<jupyter_text>Turning on Cuda and making CudNN as our backend library<jupyter_code>if th.cuda.is_available():
  # Make CuDNN Determinist
  th.backends.cudnn.deterministic = True
  th.cuda.manual_seed(42)
  print("GPU is available and turned on")
device = th.device("cuda" if th.cuda.is_available() else "cpu")<jupyter_output>GPU is available and turned on<jupyter_text>Setting the Two Directories<jupyter_code>train_set = "/content/drive/MyDrive/asl_alphabet_train"
eval_set = "/content/drive/MyDrive/asl-alphabet-test"<jupyter_output><empty_output><jupyter_text>Loading the data<jupyter_code>def load_data():
  images= []
  labels = []
  size = 64,64
  print ("Loading ", end = "")
  for folder_index, folder in enumerate(os.listdir(train_set)):
    print(folder,end='|')
    for image in os.listdir(train_set + "/"+folder):
      temp_img = cv2.imread(train_set + '/' + folder + '/' + image)
      temp_img = cv2.resize(temp_img, size)
      images.append(temp_img)
      labels.append(folder_index)
  images= np.array(images)
  images = images.astype('float32')/255.0  #normalize the RGB values
  labels = keras.utils.to_categorical(labels) # one hot encoding
  X_train, X_test, Y_train, Y_test = train_test_split(images, labels, test_size = 0.1)
  # print()
  # print(len(labels))
  # print(len(images))
  # print(len(X_train))
  # print(len(X_test))
  # print(len(Y_train))
  # print(len(Y_test))
  return X_train, X_test, Y_train, Y_test, labels<jupyter_output><empty_output><jupyter_text>Setting our data for training and testing<jupyter_code>x_train, x_test, y_train, y_test, Labels = load_data()<jupyter_output><empty_output><jupyter_text>Print function for our images<jupyter_code>def print_images(image_list):
    n = len(image_list)
    cols = 8
    rows = 4
    fig = plt.figure(figsize = (24, 12))
    
    for i in range(29):
      ax = plt.subplot(rows,cols, i+1)
      plt.imshow(image_list[i])
      # plt.title(Labels[i])
      ax.title.set_fontsize(20)
      ax.axis('off')
    plt.show()<jupyter_output><empty_output><jupyter_text>Training model<jupyter_code>model = Sequential()
model.add(Conv2D(filters=5, kernel_size=5, padding='same', activation='relu', input_shape=(64, 64, 3)))
model.add(MaxPooling2D((4,4)))
model.add(Conv2D(filters=15, kernel_size=(5,5), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(4,4)))
model.add(Flatten())
model.add(Dense(29, activation='softmax'))
model.summary()
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
hist = model.fit(x_train, y_train, validation_split=0.2, epochs=10, batch_size=32)
hist = model.history
epochs = range(1, len(hist.history['loss']) + 1)
plt.plot(epochs, hist.history['loss'], 'bo-')
plt.plot(epochs, hist.history['val_loss'], 'ro--')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.show()
score = model.evaluate(x=x_test, y=y_test,verbose=0)
print('Test accuracy:', score[1])
Total_time = time.time()-start_time
print("Total _runtime", Total_time)
print_images(x_train)<jupyter_output><empty_output><jupyter_text>If you want to test the code with you image and see the result you have to pick the address and put it instead if the address we have in eval_set.
But please put it in a folder which is showing its right class.This will not effect the predicting but after predicting you can see whether it is working right or not.<jupyter_code>def load_testdata():
  images= []
  labels = []
  size = 64,64
  print ("Loading ", end = "")
  for folder_index, folder in enumerate(os.listdir(eval_set)):
    print(folder,end='|')
    for image in os.listdir(eval_set + "/"+folder):
      temp_img = cv2.imread(eval_set + '/' + folder + '/' + image)
      temp_img = cv2.resize(temp_img, size)
      images.append(temp_img)
      labels.append(folder_index)
  images= np.array(images)
  images = images.astype('float32')/255.0  #normalize the RGB values
  labels = keras.utils.to_categorical(labels) # one hot encoding
  return images,labels<jupyter_output><empty_output><jupyter_text>Here we load the pictures for your test.<jupyter_code>images,labels=load_testdata()
predicted_classes = model.predict_classes(images)
print("The number of the class in which the pictuter belong is")
print(predicted_classes)<jupyter_output>The number of the class in which the pictuter bilong is
[ 4  1 16 16 16  5 16  4 14 16 15 16 15 16 16 16  1 14 25 16 16 16 16 16
  1 17 24  4  9 16  1 15 15  5 15  5  5  4  1 16 15  4  5 15 15  5  5  5
  5  1  9  3 10  2 16  4 15 15  4  3 16 12 10 16 10 16 16 16 10 10  3 10
 16 16  1 16 10  3 24 10 16 16 16 16 10 16 16 17  3 16  5 16 24  1 10 26
 16 14  1 24 16 16 10 16  1  6  1 16 16 16 10  3 23 16  5  1 16  5  1 10
 15 15 12 15  5  5 12  4  2  2 15  9 15 16 15  2 12 16 15  2  4 14  4  4
 16 15 15  1 15 15 16 16 16 10 16 16 10 10 16 10 24  1 15 10  5  5 10 10
 16 10  5 16  9 10 16 10 16 10 10 16 15 15 15 15 16 16 14 14 16  5 14  6
 16 15 25 19 14 16 16 24 14  7 14 14 19 26  7 16  4 14  2 15  9 15 10 15
 16 15  1  1 15 16  1  2  1 15 14  4  2 14 15 25 14 16  2 14 15 14 15  1
  3 16 24 16 16 24 16 16 14 16 16 16 16  5 16 24  6 24 16 16  5 16  3 15
 16 16 25 16 16 16  5 14 20 16 10 15 24 14 15 24 24 16 16 14 24 16 16  2
 14 14 14 16 15  5 15 16  3 16  5  3 17 17 17  5 17 14 17  5 15 14 1[...]
{'fraction_non_alphanumeric': 0.10445899594636732, 'fraction_numerical': 0.09619582164016215, 'mean_word_length': 3.8746200607902734, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '97148', 'license': 'MIT', 'n_tokens_mistral': 2840, 'n_tokens_neox': 2328, 'n_words': 740, 'path': 'main.ipynb', 'repo_name': 'cybertron97/ASL-CUDNN', 'size': '6414'}