{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\n# Input data files are available in the \"../input/\" directory.\nimport os\nprint(os.listdir(\"../input\"))\n\n# This is a bit of magic to make matplotlib figures appear inline in the notebook\n# rather than in a new window.\nplt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots\nplt.rcParams['image.interpolation'] = 'nearest'\nplt.rcParams['image.cmap'] = 'gray'\n\n### Importing the data the data\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\n# Import Data\ntrain = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest= pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nprint(\"Train size:{}\\nTest size:{}\".format(train.shape, test.shape))\n\n# Transform Train and Test into images\\labels.\nx_train = train.drop(['label'], axis=1).values.astype('float32') # all pixel values\ny_train = train['label'].values.astype('int32') # only labels i.e targets digits\nx_test = test.values.astype('float32')\nx_train = x_train.reshape(x_train.shape[0], 28, 28) / 255.0\nx_test = x_test.reshape(x_test.shape[0], 28, 28) / 255.0\n\nx_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.3, random_state=42)\n\nprint(x_train.shape)\nprint(x_val.shape)\nprint(y_train.shape)\nprint(x_test.shape)\n\n## 2. Visualize some examples from the dataset.\n\n# classes for title\n# num classes for amount of examples\nclasses = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\nprint(x_train.shape)\nnum_classes = len(classes)\nsamples_per_class = 7\nplt.figure(0)\nfor y, cls in enumerate(classes):\n    idxs = np.flatnonzero(y_train == y)\n    idxs = np.random.choice(idxs, samples_per_class, replace=False)\n    for i, idx in enumerate(idxs):\n        plt_idx = i * num_classes + y + 1\n        plt.subplot(samples_per_class, num_classes, plt_idx)\n        plt.imshow(x_train[idx])\n        plt.axis('off')\n        if i == 0:\n            plt.title(cls)\nplt.show()\n\n### Adding dimensions for keras\n\nx_train = x_train.reshape(x_train.shape[0], 28, 28,1)  \nx_val = x_val.reshape(x_val.shape[0], 28, 28,1)  \nx_test = x_test.reshape(x_test.shape[0], 28, 28,1) \nprint(\"Train size:{}\\nvalidation size:{}\\nTest size:{}\".format(x_train.shape,x_val.shape, x_test.shape))\n\nmean_px = x_train.mean().astype(np.float32)\nstd_px = x_train.std().astype(np.float32)\n\n## 3. Defining the architecture\nfrom tensorflow.keras.layers import Input, Dense, Conv2D, Activation, Add, ReLU, MaxPool2D, Flatten, Dropout, BatchNormalization\nfrom tensorflow.keras.models import Model\n\ninput = Input(shape=[28, 28, 1])\nx = Conv2D(16, (5, 5), strides=1, padding='same', name='conv1')(input)\nx = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch1')(x)\nx = Activation('relu', name='relu1')(x)\nx = Conv2D(16, (5, 5), strides=1, padding='same', name='conv2')(x)\nx = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch2')(x)\nx = Activation('relu', name='relu2')(x)\nx = Conv2D(16, (5, 5), strides=1, padding='same', name='conv2add')(x)\nx = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch2add')(x)\nx = Activation('relu', name='relu2add')(x)\nx = Dropout (0.1)(x)\nx = MaxPool2D(pool_size=2, strides=2, padding='same')(x)\nx = Conv2D(32, (3, 3), strides=1, padding='same', name='conv3')(x)\nx = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch3')(x)\nx = Activation('relu', name='relu3')(x)\nx = Conv2D(62, (3, 3), strides=1, padding='same', name='conv4')(x)\nx = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch4')(x)\nx = Activation('relu', name='relu4')(x)\nx = Conv2D(32, (3, 3), strides=1, padding='same', name='conv5')(x)\nx = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch5')(x)\nx = Activation('relu', name='relu5')(x)\nx = Dropout (0.1)(x)\nx = MaxPool2D(pool_size=2, strides=2)(x)\nx = Flatten()(x)\nx = Dense(30, name='Dense30')(x)\nx = Activation('relu', name='relu6')(x)\nx = Dense(10, name='Dense10')(x)\nx = Activation('softmax')(x)\nmodel = Model(inputs = input, outputs = x)\n\nprint(model.summary())\n\n## 4. Train the model using data augmentation\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint\nfrom tensorflow.keras.optimizers import Adam, RMSprop\n\ncheckpoint = ModelCheckpoint(\"best_weights.keras\", monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')\n\n# CREATE MORE IMAGES VIA DATA AUGMENTATION\ndatagen = ImageDataGenerator(\n    rotation_range=8,  \n    zoom_range=0.08,  \n    width_shift_range=0.08, \n    height_shift_range=0.08\n)\n\nepochs = 40\nlr_initial = 0.001\noptimizer = Adam(learning_rate=lr_initial, decay=lr_initial / epochs)\n\nmodel.compile(optimizer=optimizer,\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\ndatagen.fit(x_train)\nbatch_size = 16                     # batch_size = 64\n\n\nhistory = model.fit(datagen.flow(x_train, y_train, batch_size=batch_size),\n                    epochs=epochs, validation_data=(x_val, y_val),\n                    verbose=1, steps_per_epoch=x_train.shape[0] // batch_size, callbacks=[checkpoint])\nmodel.load_weights(\"best_weights.hdf5\") \n\n### 5.1 Loss graph visualizations\nplt.figure(1)\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model Complexity Graph: Training vs. Validation Loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'validate'], loc='upper right')\n\nplt.figure(2)\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model Accuracy Graph: Training vs. Validation Accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'validate'], loc='upper right')\nplt.show()\n\n### 5.2 Prediction images visualization\n\n# predicted class\nnum_rows = 6\nnum_cols = 15\nsample_size = num_rows * num_cols\nindices = np.arange(sample_size)\nx_pred = x_test[indices, :, :]\npredictions = model.predict(x_pred)\nx_pred = np.squeeze(x_test[indices, :, :])\ny_pred = np.argmax(predictions, axis=1)\n\nnum_images = num_rows * num_cols\nplt.figure(figsize=(num_cols * 2, num_rows * 2))\nplt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=0.6)\nfor i in range(num_images):\n    plt.subplot(num_rows, num_cols, i + 1)\n    plt.imshow(x_pred[i])\n    plt.title(classes[y_pred[i]])\n    plt.axis('off')\nplt.show()\n\n### 5.3 Confusion matrix\nfrom sklearn.metrics import confusion_matrix\n\ny_vecs = model.predict(x_val)\ny_pred = np.argmax(y_vecs, axis=1)\ny_true = y_val\ncm = confusion_matrix(y_true, y_pred)\n\nplt.figure(11)\nfig, ax = plt.subplots()\nax.matshow(cm, cmap=plt.cm.Blues, alpha=0.3)\nfor i in range(cm.shape[0]):\n    for j in range(cm.shape[1]):\n        ax.text(x=j, y=i,s=cm[i, j], va='center', ha='center', size='xx-large')\n \nplt.xlabel('Predictions', fontsize=18)\nplt.ylabel('Actuals', fontsize=18)\nplt.title('Confusion Matrix', fontsize=18)\nplt.show()\n","metadata":{"_uuid":"a44b42fe-fd43-4931-9d07-126d2e6298a4","_cell_guid":"360f42a8-6378-45b1-a20b-db98fe5f1c9e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-16T08:54:10.754842Z","iopub.execute_input":"2024-07-16T08:54:10.755291Z","iopub.status.idle":"2024-07-16T09:30:35.575059Z","shell.execute_reply.started":"2024-07-16T08:54:10.75526Z","shell.execute_reply":"2024-07-16T09:30:35.573152Z"},"trusted":true},"execution_count":null,"outputs":[]}]}