{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Import librarys and data\n"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\nimport os\nimport cv2\nfrom sklearn.preprocessing import MinMaxScaler\nimport tensorflow as tf\nfrom keras.optimizers import Adam\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import Dropout\nfrom keras.layers import Flatten\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.convolutional import MaxPooling2D\nfrom keras import backend as K\nfrom keras.utils import np_utils\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report\nfrom keras.callbacks import TensorBoard\n%matplotlib inline\n#ignore warning messages \nimport warnings\nwarnings.filterwarnings('ignore') \n\nsns.set()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data Input"},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = pd.read_csv(\"../input/az-handwritten-alphabets-in-csv-format/A_Z Handwritten Data.csv\").astype('float32')\ndataset.rename(columns={'0':'label'}, inplace=True)\ntrain_d = pd.read_csv('../input/digit-recognizer/train.csv')\ntest_d = pd.read_csv('../input/digit-recognizer/test.csv')\n# Splite data the X - Our data , and y - the prdict label\nX_l = dataset.drop('label',axis = 1)\ny_l = dataset['label']","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"print(\"shape:\",X_l.shape)\nprint(\"culoms count:\",len(X_l.iloc[1]))\nprint(\"784 = 28X28\")\n\nX_l.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualize"},{"metadata":{"trusted":true},"cell_type":"code","source":"training_d = np.array(train_d, dtype = 'float32')\ntesting_d = np.array(test_d, dtype = 'float32')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"training_d = np.array(train_d, dtype = 'float32')\ntesting_d = np.array(test_d, dtype = 'float32')\ni = random.randint(0, len(training_d))\nplt.imshow(d_image, cmap = 'Greys')\n#plt.axis('off')\nlabel = training_d[i, 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = []\nw_grid = 5\nl_grid = 2\nfig, axes = plt.subplots(l_grid, w_grid, figsize = (20, 8))\naxes = axes.ravel()\ni = 0\nwhile len(x) < 10:\n    index = np.random.randint(0, len(training_d))\n    if training_d[index, 0] not in x:\n        axes[i].imshow(training_d[index, 1:].reshape((28, 28)), cmap = 'Greys') \n        x.append(training_d[index, 0])\n        i += 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.utils import shuffle\n\nX_shuffle = shuffle(X_l)\n\nplt.figure(figsize = (12,10))\nrow, colums = 4, 4\nfor i in range(16):  \n    plt.subplot(colums, row, i+1)\n    plt.imshow(X_shuffle.iloc[i].values.reshape(28,28),interpolation='nearest', cmap='Greys')\n    #plt.imshow(test_input,interpolation='nearest', cmap='Greys')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Amount of each labels\")\n\n# Change label to alphabets\nalphabets_mapper = {0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J',10:'K',11:'L',12:'M',13:'N',14:'O',15:'P',16:'Q',17:'R',18:'S',19:'T',20:'U',21:'V',22:'W',23:'X',24:'Y',25:'Z'} \ndataset_alphabets = dataset.copy()\ndataset['label'] = dataset['label'].map(alphabets_mapper)\n\nlabel_size = dataset.groupby('label').size()\nlabel_size.plot.barh(figsize=(10,10))\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data preparation"},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train_d = training_d[:, 1:]\ny_train_d = training_d[:, 0]\n\nX_train_d, X_validate_d, y_train_d, y_validate_d = train_test_split(X_train_d, y_train_d, test_size = 0.2, random_state = 0)\n\nX_train_d = X_train_d.reshape(X_train_d.shape[0], *(28, 28, 1))\nX_validate_d = X_validate_d.reshape(X_validate_d.shape[0], *(28, 28, 1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# splite the data\nX_train_l, X_test_l, y_train_l, y_test_l = train_test_split(X_l,y_l)\n\n# scale data\nstandard_scaler = MinMaxScaler()\nstandard_scaler.fit(X_train_l)\n\nX_train_l = standard_scaler.transform(X_train_l)\nX_test_l = standard_scaler.transform(X_test_l)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Data after scaler\")\nX_shuffle = shuffle(X_train_l)\n\nplt.figure(figsize = (12,10))\nrow, colums = 4, 4\nfor i in range(16):  \n    plt.subplot(colums, row, i+1)\n    plt.imshow(X_shuffle[i].reshape(28,28),interpolation='nearest', cmap='Greys')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train_l = X_train_l.reshape(X_train_l.shape[0], 28, 28, 1).astype('float32')\nX_test_l = X_test_l.reshape(X_test_l.shape[0], 28, 28, 1).astype('float32')\n\ny_train_l = np_utils.to_categorical(y_train_l)\ny_test_l = np_utils.to_categorical(y_test_l)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Build the model\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"#For Digits \ncnn = Sequential()\ncnn.add(Conv2D(128, 3, 3, input_shape = (28, 28, 1), activation = 'relu'))\ncnn.add(MaxPooling2D(pool_size = (2, 2)))\ncnn.add(Flatten())\ncnn.add(Dense(output_dim = 32, activation = 'relu'))\ncnn.add(Dense(output_dim = 10, activation = 'sigmoid'))\ncnn.compile(optimizer= Adam(lr = 0.001), loss = 'sparse_categorical_crossentropy', metrics= ['accuracy'])\nepochs = 20\ncnn.fit(X_train_d, y_train_d, batch_size = 512, epochs = epochs, validation_data = (X_validate_d, y_validate_d), verbose = 1,)\n\nscores = cnn.evaluate(X_validate_d, y_validate_d, verbose=0)\n\nprint(\"CNN Score for digits:\",scores[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#For Letters\ncls = Sequential()\ncls.add(Conv2D(32, (5, 5), input_shape=(28, 28, 1), activation='relu'))\ncls.add(MaxPooling2D(pool_size=(2, 2)))\ncls.add(Dropout(0.3))\ncls.add(Flatten())\ncls.add(Dense(128, activation='relu'))\ncls.add(Dense(len(y.unique()), activation='softmax'))\n\ncls.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nhistory = cls.fit(X_train_l, y_train_l, validation_data=(X_test_l, y_test_l), epochs=18, batch_size=200, verbose=2)\n\nscores = cls.evaluate(X_test_l,y_test_l, verbose=0)\nprint(\"CNN Score for letters:\",scores[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"cm=confusion_matrix(y_test_l.argmax(axis=1),cls.predict(X_test_l).argmax(axis=1))\ndf_cm = pd.DataFrame(cm, range(26),range(26))\nplt.figure(figsize = (20,15))\nsns.set(font_scale=1.4)#for label size\nsns.heatmap(df_cm, annot=True,annot_kws={\"size\": 16})# font size","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"check = int(input('Enter the index number to check : '))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **Check for value from index**# "},{"metadata":{"trusted":true},"cell_type":"code","source":"digit_check = X_train_d[check].reshape(1, 28, 28, 1).astype('float32')\ndigit_out = cnn.predict(digit_check).argmax(axis=1)\nprint(digit_out)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alpha_mapper = {0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J',10:'K',11:'L',12:'M',13:'N',14:'O',15:'P',16:'Q',17:'R',18:'S',19:'T',20:'U',21:'V',22:'W',23:'X',24:'Y',25:'Z'} \ntoPre = X_train_l[check].reshape(1, 28, 28, 1).astype('float32')\ntoOut = cls.predict(toPre).argmax(axis=1)\nprint(alpha_mapper[toOut[0]])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#  **Check With Image**"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (12, 10))\nrow, colums = 4, 4\nplt.imshow(X_train_d[check].reshape(28, 28),cmap='Greys')\n#plt.axis('off')\nplt.show\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (12,10))\nrow, colums = 4, 4\nplt.imshow(X_train_l[check].reshape(28,28), cmap='Greys')\n#plt.axis('off')\nplt.show","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **External Testing**"},{"metadata":{"trusted":true},"cell_type":"code","source":"d_image = cv2.imread('../input/handwriten-digit-test/4test.png')\ngray_d = cv2.cvtColor(d_image, cv2.COLOR_BGR2GRAY)\ngray_d = cv2.resize(gray_d, (28, 28))\ntest_input_d = cv2.bitwise_not(gray_d)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_input_d.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"digit_out = cnn.predict(test_input_d.reshape(1, 28, 28, 1)).argmax(axis=1)\nprint(digit_out)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"  \nimage = cv2.imread('../input/handwritten-testing/MTest.png')\ngrayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\ngrayImage = cv2.resize(grayImage, (28, 28))\ntest_input = cv2.bitwise_not(grayImage)\nstandard_scaler.fit(test_input)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"alpha_mapper = {0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J',10:'K',11:'L',12:'M',13:'N',14:'O',15:'P',16:'Q',17:'R',18:'S',19:'T',20:'U',21:'V',22:'W',23:'X',24:'Y',25:'Z'} \ntoOut = cls.predict(test_input.reshape(1,28,28,1)).argmax(axis=1)\nprint(alpha_mapper[toOut[0]])","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":4}