{"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":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-27T14:40:01.461254Z","iopub.execute_input":"2024-07-27T14:40:01.461939Z","iopub.status.idle":"2024-07-27T14:40:01.840589Z","shell.execute_reply.started":"2024-07-27T14:40:01.461905Z","shell.execute_reply":"2024-07-27T14:40:01.839654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = \"/kaggle/input/digit-recognizer/train.csv\"\n","metadata":{"execution":{"iopub.status.busy":"2024-07-27T14:40:04.559725Z","iopub.execute_input":"2024-07-27T14:40:04.560675Z","iopub.status.idle":"2024-07-27T14:40:04.564702Z","shell.execute_reply.started":"2024-07-27T14:40:04.560641Z","shell.execute_reply":"2024-07-27T14:40:04.563715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_train_data(file_path):\n    df = pd.read_csv(file_path)\n    pp = df.values\n    images = []\n    labels = []\n    for p in pp:\n        labels.append(p[0])\n        images.append(p[1:].reshape(28,28,1))\n        \n    return labels, images","metadata":{"execution":{"iopub.status.busy":"2024-07-27T14:40:10.435717Z","iopub.execute_input":"2024-07-27T14:40:10.436009Z","iopub.status.idle":"2024-07-27T14:40:10.453372Z","shell.execute_reply.started":"2024-07-27T14:40:10.435983Z","shell.execute_reply":"2024-07-27T14:40:10.452369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels , train_images = load_train_data(x)","metadata":{"execution":{"iopub.status.busy":"2024-07-27T14:41:55.598160Z","iopub.execute_input":"2024-07-27T14:41:55.598884Z","iopub.status.idle":"2024-07-27T14:41:58.284052Z","shell.execute_reply.started":"2024-07-27T14:41:55.598848Z","shell.execute_reply":"2024-07-27T14:41:58.282954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-07-27T14:42:23.719303Z","iopub.execute_input":"2024-07-27T14:42:23.719781Z","iopub.status.idle":"2024-07-27T14:42:23.725613Z","shell.execute_reply.started":"2024-07-27T14:42:23.719749Z","shell.execute_reply":"2024-07-27T14:42:23.724593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-07-27T14:40:22.698314Z","iopub.execute_input":"2024-07-27T14:40:22.698724Z","iopub.status.idle":"2024-07-27T14:40:35.234568Z","shell.execute_reply.started":"2024-07-27T14:40:22.698690Z","shell.execute_reply":"2024-07-27T14:40:35.233745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\ntrain_labels = np.array(train_labels)\ntrain_labels = to_categorical(train_labels,10)\ntrain_labels\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-27T14:55:49.712779Z","iopub.execute_input":"2024-07-27T14:55:49.713108Z","iopub.status.idle":"2024-07-27T14:55:49.724996Z","shell.execute_reply.started":"2024-07-27T14:55:49.713082Z","shell.execute_reply":"2024-07-27T14:55:49.723898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-27T14:55:52.125139Z","iopub.execute_input":"2024-07-27T14:55:52.125570Z","iopub.status.idle":"2024-07-27T14:55:52.133270Z","shell.execute_reply.started":"2024-07-27T14:55:52.125538Z","shell.execute_reply":"2024-07-27T14:55:52.132232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = np.array(train_images)\ntrain_images = train_images / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-07-27T15:01:20.328321Z","iopub.execute_input":"2024-07-27T15:01:20.328743Z","iopub.status.idle":"2024-07-27T15:01:20.573871Z","shell.execute_reply.started":"2024-07-27T15:01:20.328711Z","shell.execute_reply":"2024-07-27T15:01:20.573048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels\n","metadata":{"execution":{"iopub.status.busy":"2024-07-27T15:01:22.075870Z","iopub.execute_input":"2024-07-27T15:01:22.076623Z","iopub.status.idle":"2024-07-27T15:01:22.083027Z","shell.execute_reply.started":"2024-07-27T15:01:22.076589Z","shell.execute_reply":"2024-07-27T15:01:22.082164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train,  y_test = train_test_split(train_images, train_labels, test_size=0.3, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-07-27T15:01:27.627881Z","iopub.execute_input":"2024-07-27T15:01:27.628231Z","iopub.status.idle":"2024-07-27T15:01:27.723733Z","shell.execute_reply.started":"2024-07-27T15:01:27.628203Z","shell.execute_reply":"2024-07-27T15:01:27.722726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.astype('float32')","metadata":{"execution":{"iopub.status.busy":"2024-07-27T15:01:29.305484Z","iopub.execute_input":"2024-07-27T15:01:29.306468Z","iopub.status.idle":"2024-07-27T15:01:29.347626Z","shell.execute_reply.started":"2024-07-27T15:01:29.306433Z","shell.execute_reply":"2024-07-27T15:01:29.346792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-27T15:01:31.074444Z","iopub.execute_input":"2024-07-27T15:01:31.074855Z","iopub.status.idle":"2024-07-27T15:01:31.080664Z","shell.execute_reply.started":"2024-07-27T15:01:31.074827Z","shell.execute_reply":"2024-07-27T15:01:31.079706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-27T15:01:32.792521Z","iopub.execute_input":"2024-07-27T15:01:32.793146Z","iopub.status.idle":"2024-07-27T15:01:32.798955Z","shell.execute_reply.started":"2024-07-27T15:01:32.793114Z","shell.execute_reply":"2024-07-27T15:01:32.798023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom tensorflow.keras.layers import Flatten, Conv2D, MaxPooling2D\nfrom tensorflow.keras.layers import BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2024-07-27T15:01:54.795673Z","iopub.execute_input":"2024-07-27T15:01:54.796512Z","iopub.status.idle":"2024-07-27T15:01:54.801015Z","shell.execute_reply.started":"2024-07-27T15:01:54.796479Z","shell.execute_reply":"2024-07-27T15:01:54.800131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**VGG16 **","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, 3 , activation='relu', padding='same', input_shape=(28, 28, 1)))\nmodel.add(Conv2D(64, 3, activation='relu', padding='same')) \nmodel.add(MaxPooling2D(2, 2))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(128, 3, activation='relu', padding='same'))\nmodel.add(Conv2D(128, 3, activation='relu', padding='same'))\nmodel.add(MaxPooling2D(2, 2))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(256, 3, activation='relu', padding='same'))\nmodel.add(Conv2D(256, 3, activation='relu', padding='same'))\nmodel.add(Conv2D(256, 3, activation='relu', padding='same'))\nmodel.add(MaxPooling2D(2, 2))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(512, 3, activation='relu', padding='same'))\nmodel.add(Conv2D(512, 3, activation='relu', padding='same'))\nmodel.add(Conv2D(512, 3, activation='relu', padding='same'))\nmodel.add(MaxPooling2D(2, 1)) # default stride is 2\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(512, 3, activation='relu', padding='same'))\nmodel.add(Conv2D(512, 3, activation='relu', padding='same'))\nmodel.add(Conv2D(512, 3, activation='relu', padding='same'))\nmodel.add(MaxPooling2D(2, 1)) # default stride is 2\nmodel.add(BatchNormalization())\n\nmodel.add(Flatten())\nmodel.add(Dense(4096, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(4096, activation='relu'))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-07-27T15:08:01.198885Z","iopub.execute_input":"2024-07-27T15:08:01.199271Z","iopub.status.idle":"2024-07-27T15:08:02.392506Z","shell.execute_reply.started":"2024-07-27T15:08:01.199241Z","shell.execute_reply":"2024-07-27T15:08:02.391456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-27T15:08:09.523120Z","iopub.execute_input":"2024-07-27T15:08:09.523492Z","iopub.status.idle":"2024-07-27T15:08:09.571395Z","shell.execute_reply.started":"2024-07-27T15:08:09.523461Z","shell.execute_reply":"2024-07-27T15:08:09.570552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n             optimizer='nadam',\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-07-27T15:09:45.870416Z","iopub.execute_input":"2024-07-27T15:09:45.870821Z","iopub.status.idle":"2024-07-27T15:09:45.880360Z","shell.execute_reply.started":"2024-07-27T15:09:45.870791Z","shell.execute_reply":"2024-07-27T15:09:45.879378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, \n                    y_train, \n                    batch_size=128, \n                    epochs=50,\n                    verbose=1,\n                    validation_data=(X_test, y_test)\n                   )","metadata":{"execution":{"iopub.status.busy":"2024-07-27T15:10:04.574776Z","iopub.execute_input":"2024-07-27T15:10:04.575108Z"},"trusted":true},"execution_count":null,"outputs":[]}]}