{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":34877,"sourceType":"datasetVersion","datasetId":27352}],"dockerImageVersionId":30581,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## **MNIST_TF_CNN-Digit Recognizer**\n*     *Cristhian Ccala Huamani*","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport random as rd\n\nimport matplotlib.pyplot as plt \nimport seaborn as sns\n\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split \nfrom sklearn.metrics import confusion_matrix\n","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:30.979462Z","iopub.execute_input":"2023-11-26T01:30:30.979879Z","iopub.status.idle":"2023-11-26T01:30:30.985127Z","shell.execute_reply.started":"2023-11-26T01:30:30.979848Z","shell.execute_reply":"2023-11-26T01:30:30.984214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Cargamos el DataSet**","metadata":{}},{"cell_type":"code","source":"dir_train = '/kaggle/input/digit-recognizer/train.csv'\ndir_test = '/kaggle/input/digit-recognizer/test.csv'\ndir_mnist_train = '/kaggle/input/mnist-in-csv/mnist_train.csv'\ndir_mnist_test = '/kaggle/input/mnist-in-csv/mnist_test.csv'","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:30.986882Z","iopub.execute_input":"2023-11-26T01:30:30.987261Z","iopub.status.idle":"2023-11-26T01:30:30.995540Z","shell.execute_reply.started":"2023-11-26T01:30:30.987235Z","shell.execute_reply":"2023-11-26T01:30:30.994743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(dir_train)\ntest = pd.read_csv(dir_test)\nprint(train.shape)\nprint(test.shape)\n#test.head()\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:30.997289Z","iopub.execute_input":"2023-11-26T01:30:30.997587Z","iopub.status.idle":"2023-11-26T01:30:35.003016Z","shell.execute_reply.started":"2023-11-26T01:30:30.997564Z","shell.execute_reply":"2023-11-26T01:30:35.001913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnist_train = pd.read_csv(dir_mnist_train)\nmnist_test = pd.read_csv(dir_mnist_test)\nmnist = pd.concat([mnist_train, mnist_test])\nmnist.columns = train.columns\nmnist","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:35.005505Z","iopub.execute_input":"2023-11-26T01:30:35.006339Z","iopub.status.idle":"2023-11-26T01:30:39.326409Z","shell.execute_reply.started":"2023-11-26T01:30:35.006297Z","shell.execute_reply":"2023-11-26T01:30:39.325295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.concat([train, mnist])\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:39.327889Z","iopub.execute_input":"2023-11-26T01:30:39.328276Z","iopub.status.idle":"2023-11-26T01:30:39.595940Z","shell.execute_reply.started":"2023-11-26T01:30:39.328241Z","shell.execute_reply":"2023-11-26T01:30:39.594875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train.drop(['label'], axis=1)\nprint(x_train.shape)\nx_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:39.597976Z","iopub.execute_input":"2023-11-26T01:30:39.598291Z","iopub.status.idle":"2023-11-26T01:30:39.816353Z","shell.execute_reply.started":"2023-11-26T01:30:39.598265Z","shell.execute_reply":"2023-11-26T01:30:39.815220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train['label']\ny_train","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:39.817613Z","iopub.execute_input":"2023-11-26T01:30:39.817932Z","iopub.status.idle":"2023-11-26T01:30:39.824985Z","shell.execute_reply.started":"2023-11-26T01:30:39.817907Z","shell.execute_reply":"2023-11-26T01:30:39.823993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (12,10))\n# Ver  conjunto de datos\nfor i in range(16):\n    plt.subplot(6,8,i+1)\n    r = rd.randint(0, len(x_train))\n    img = x_train.iloc[r].to_numpy()\n    img = img.reshape((28,28))\n    plt.imshow(img,cmap='gray')\n    plt.title(train.iloc[r,0])\n    plt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:39.826303Z","iopub.execute_input":"2023-11-26T01:30:39.826652Z","iopub.status.idle":"2023-11-26T01:30:41.150299Z","shell.execute_reply.started":"2023-11-26T01:30:39.826623Z","shell.execute_reply":"2023-11-26T01:30:41.149421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,5))\nsns.countplot(x=y_train, data=x_train)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:41.151646Z","iopub.execute_input":"2023-11-26T01:30:41.152244Z","iopub.status.idle":"2023-11-26T01:30:41.539029Z","shell.execute_reply.started":"2023-11-26T01:30:41.152209Z","shell.execute_reply":"2023-11-26T01:30:41.538160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Preparomos los Datos**","metadata":{}},{"cell_type":"code","source":"X_train = x_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\nprint(X_train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:41.540218Z","iopub.execute_input":"2023-11-26T01:30:41.540520Z","iopub.status.idle":"2023-11-26T01:30:41.546659Z","shell.execute_reply.started":"2023-11-26T01:30:41.540495Z","shell.execute_reply":"2023-11-26T01:30:41.545727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = tf.keras.utils.to_categorical(y_train, 10)\nY_train","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:41.548300Z","iopub.execute_input":"2023-11-26T01:30:41.548869Z","iopub.status.idle":"2023-11-26T01:30:41.561833Z","shell.execute_reply.started":"2023-11-26T01:30:41.548841Z","shell.execute_reply":"2023-11-26T01:30:41.560750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Dividimos los Datos**","metadata":{}},{"cell_type":"code","source":"X_train, X_vald, Y_train, Y_vald = train_test_split(X_train, Y_train, \n                                                    test_size = 0.4, \n                                                    random_state=42)\nprint(X_train.shape)\nprint(X_vald.shape)\nprint(Y_train.shape)\nprint(Y_vald.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:41.565442Z","iopub.execute_input":"2023-11-26T01:30:41.565738Z","iopub.status.idle":"2023-11-26T01:30:42.557914Z","shell.execute_reply.started":"2023-11-26T01:30:41.565712Z","shell.execute_reply":"2023-11-26T01:30:42.556844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Aumento de Datos**","metadata":{}},{"cell_type":"code","source":"ds_train = tf.data.Dataset.from_tensor_slices((X_train, Y_train))\nds_valid = tf.data.Dataset.from_tensor_slices((X_vald, Y_vald))\nds_test = tf.data.Dataset.from_tensor_slices(test)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:42.559057Z","iopub.execute_input":"2023-11-26T01:30:42.559384Z","iopub.status.idle":"2023-11-26T01:30:44.494109Z","shell.execute_reply.started":"2023-11-26T01:30:42.559358Z","shell.execute_reply":"2023-11-26T01:30:44.493067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rescale_data = tf.keras.Sequential([\n    tf.keras.layers.experimental.preprocessing.Rescaling(1/255)\n])\ndata_augmenation = tf.keras.Sequential([\n    tf.keras.layers.experimental.preprocessing.RandomZoom(height_factor=0.05, width_factor=0.05),\n    tf.keras.layers.experimental.preprocessing.RandomRotation(factor=0.05),\n    tf.keras.layers.experimental.preprocessing.RandomTranslation(height_factor=0.05, width_factor=0.05)\n])","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:44.496061Z","iopub.execute_input":"2023-11-26T01:30:44.496523Z","iopub.status.idle":"2023-11-26T01:30:44.514026Z","shell.execute_reply.started":"2023-11-26T01:30:44.496482Z","shell.execute_reply":"2023-11-26T01:30:44.513148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_train = ds_train.map(lambda x, y: (rescale_data(x), y), num_parallel_calls=tf.data.AUTOTUNE)\nds_train = ds_train.batch(32)\nds_train = ds_train.map(lambda x, y: (data_augmenation(x, training=True), y), num_parallel_calls=tf.data.AUTOTUNE)\n\nds_valid = ds_valid.map(lambda x, y: (rescale_data(x), y), num_parallel_calls=tf.data.AUTOTUNE)\nds_valid = ds_valid.batch(32)\n\nds_test = ds_test.map(lambda x: (rescale_data(x)), num_parallel_calls=tf.data.AUTOTUNE)\nds_test = ds_test.batch(32)\n\ntest_ds = ds_test.prefetch(buffer_size=tf.data.AUTOTUNE)\nentrenar_ds = ds_train.prefetch(buffer_size=tf.data.AUTOTUNE) \nvalidar_ds = ds_valid.prefetch(buffer_size=tf.data.AUTOTUNE) ","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:44.515266Z","iopub.execute_input":"2023-11-26T01:30:44.515569Z","iopub.status.idle":"2023-11-26T01:30:45.225667Z","shell.execute_reply.started":"2023-11-26T01:30:44.515543Z","shell.execute_reply":"2023-11-26T01:30:45.224726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Creamos el Modelo CNN**","metadata":{}},{"cell_type":"code","source":"modelo = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='same', activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'),\n    tf.keras.layers.Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(pool_size=2, padding='same'),\n\n    tf.keras.layers.Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'),\n    tf.keras.layers.Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(pool_size=2, padding='same'),\n\n    tf.keras.layers.Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(pool_size=2, padding='same'),\n\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(256, activation='relu'),\n    tf.keras.layers.Dense(10, activation='softmax')\n])\n\nmodelo.compile(loss='categorical_crossentropy',\n               optimizer = 'adam',\n               metrics = ['accuracy'])\n\nmodelo.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:45.227872Z","iopub.execute_input":"2023-11-26T01:30:45.228318Z","iopub.status.idle":"2023-11-26T01:30:45.370523Z","shell.execute_reply.started":"2023-11-26T01:30:45.228279Z","shell.execute_reply":"2023-11-26T01:30:45.369627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(\n    monitor='loss', \n    factor=0.3, \n    patience=2, \n    min_lr=0.00000001\n)\nhistory = modelo.fit(\n    entrenar_ds,\n    epochs =50,\n    validation_data = validar_ds,\n    callbacks=[reduce_lr],\n    shuffle=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:30:45.371943Z","iopub.execute_input":"2023-11-26T01:30:45.372338Z","iopub.status.idle":"2023-11-26T01:55:01.159404Z","shell.execute_reply.started":"2023-11-26T01:30:45.372304Z","shell.execute_reply":"2023-11-26T01:55:01.158350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Evaluamos el Modelo**","metadata":{}},{"cell_type":"code","source":"modelo.evaluate(ds_valid)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:55:01.160947Z","iopub.execute_input":"2023-11-26T01:55:01.161712Z","iopub.status.idle":"2023-11-26T01:55:07.632770Z","shell.execute_reply.started":"2023-11-26T01:55:01.161672Z","shell.execute_reply":"2023-11-26T01:55:07.631885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history.history).plot()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:55:07.633842Z","iopub.execute_input":"2023-11-26T01:55:07.634141Z","iopub.status.idle":"2023-11-26T01:55:07.990384Z","shell.execute_reply.started":"2023-11-26T01:55:07.634087Z","shell.execute_reply":"2023-11-26T01:55:07.989455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = modelo.predict(ds_valid)\nY_pred_classes = np.argmax(Y_pred,axis = 1) \nY_true = np.argmax(Y_vald,axis = 1) \nplt.figure(figsize = (8,6))\nsns.heatmap(pd.DataFrame(confusion_matrix(Y_true, Y_pred_classes), index= range(10), columns = range(10)), annot = True, fmt='d')","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:55:07.991529Z","iopub.execute_input":"2023-11-26T01:55:07.991818Z","iopub.status.idle":"2023-11-26T01:55:13.519837Z","shell.execute_reply.started":"2023-11-26T01:55:07.991793Z","shell.execute_reply":"2023-11-26T01:55:13.518940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Prediccion de Prueba**","metadata":{}},{"cell_type":"code","source":"prediccion = modelo.predict(test_ds)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:55:13.521023Z","iopub.execute_input":"2023-11-26T01:55:13.521399Z","iopub.status.idle":"2023-11-26T01:55:16.720384Z","shell.execute_reply.started":"2023-11-26T01:55:13.521366Z","shell.execute_reply":"2023-11-26T01:55:16.719519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names= np.arange(10)\nprediccion = class_names[tf.argmax(prediccion, axis=1)]\nprediccion","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:55:16.721634Z","iopub.execute_input":"2023-11-26T01:55:16.721949Z","iopub.status.idle":"2023-11-26T01:55:16.731079Z","shell.execute_reply.started":"2023-11-26T01:55:16.721923Z","shell.execute_reply":"2023-11-26T01:55:16.730218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediccion.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:55:16.732350Z","iopub.execute_input":"2023-11-26T01:55:16.732689Z","iopub.status.idle":"2023-11-26T01:55:16.741970Z","shell.execute_reply.started":"2023-11-26T01:55:16.732660Z","shell.execute_reply":"2023-11-26T01:55:16.741051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Evio de Resultados**","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:55:16.743150Z","iopub.execute_input":"2023-11-26T01:55:16.743517Z","iopub.status.idle":"2023-11-26T01:55:16.751228Z","shell.execute_reply.started":"2023-11-26T01:55:16.743483Z","shell.execute_reply":"2023-11-26T01:55:16.750313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['ImageId'] = np.arange(1, 28001)\nsubmission['Label'] = prediccion\nsubmission#.head(20)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:55:16.752392Z","iopub.execute_input":"2023-11-26T01:55:16.752745Z","iopub.status.idle":"2023-11-26T01:55:16.770325Z","shell.execute_reply.started":"2023-11-26T01:55:16.752710Z","shell.execute_reply":"2023-11-26T01:55:16.769460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T01:55:16.771247Z","iopub.execute_input":"2023-11-26T01:55:16.771470Z","iopub.status.idle":"2023-11-26T01:55:16.839291Z","shell.execute_reply.started":"2023-11-26T01:55:16.771450Z","shell.execute_reply":"2023-11-26T01:55:16.838441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}