{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":false,"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-03-13T11:56:39.058915Z","iopub.execute_input":"2024-03-13T11:56:39.059225Z","iopub.status.idle":"2024-03-13T11:56:40.140254Z","shell.execute_reply.started":"2024-03-13T11:56:39.059196Z","shell.execute_reply":"2024-03-13T11:56:40.139118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnist_train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\nmnist_test = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:56:40.142032Z","iopub.execute_input":"2024-03-13T11:56:40.142577Z","iopub.status.idle":"2024-03-13T11:56:46.308785Z","shell.execute_reply.started":"2024-03-13T11:56:40.142535Z","shell.execute_reply":"2024-03-13T11:56:46.307776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train1 = pd.get_dummies(mnist_train.label)\nprint(y_train1.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:56:46.309975Z","iopub.execute_input":"2024-03-13T11:56:46.310284Z","iopub.status.idle":"2024-03-13T11:56:46.326591Z","shell.execute_reply.started":"2024-03-13T11:56:46.310256Z","shell.execute_reply":"2024-03-13T11:56:46.325578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"npx = mnist_train.drop(['label'], axis=1)\nx_train1 = np.array(npx).reshape(42000,28,28,1)/255\nprint(x_train1.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:56:46.329167Z","iopub.execute_input":"2024-03-13T11:56:46.329492Z","iopub.status.idle":"2024-03-13T11:56:46.613844Z","shell.execute_reply.started":"2024-03-13T11:56:46.329460Z","shell.execute_reply":"2024-03-13T11:56:46.612685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split #to split our dataset\nx_train, x_val, y_train, y_val = train_test_split(x_train1, y_train1, test_size=0.1, random_state=101)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:56:46.615347Z","iopub.execute_input":"2024-03-13T11:56:46.615677Z","iopub.status.idle":"2024-03-13T11:56:47.605258Z","shell.execute_reply.started":"2024-03-13T11:56:46.615650Z","shell.execute_reply":"2024-03-13T11:56:47.604167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"npx = mnist_test\nx_test = np.array(npx).reshape(28000,28,28,1)/255\nprint(x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:56:47.606583Z","iopub.execute_input":"2024-03-13T11:56:47.607196Z","iopub.status.idle":"2024-03-13T11:56:47.745704Z","shell.execute_reply.started":"2024-03-13T11:56:47.607168Z","shell.execute_reply":"2024-03-13T11:56:47.744679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, BatchNormalization, Conv2D, MaxPooling2D\nfrom keras.callbacks import ReduceLROnPlateau\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2 \nfrom PIL import Image \n\nnp.random.seed(99)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:56:47.746857Z","iopub.execute_input":"2024-03-13T11:56:47.747134Z","iopub.status.idle":"2024-03-13T11:57:00.717248Z","shell.execute_reply.started":"2024-03-13T11:56:47.747110Z","shell.execute_reply":"2024-03-13T11:57:00.716208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display(img):\n  plt.figure(figsize=(5,5))\n  plt.axis(\"off\")\n  plt.imshow(img,cmap=\"gray\")\n\ndisplay(x_train[1191])","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:57:00.718701Z","iopub.execute_input":"2024-03-13T11:57:00.719395Z","iopub.status.idle":"2024-03-13T11:57:00.844655Z","shell.execute_reply.started":"2024-03-13T11:57:00.719366Z","shell.execute_reply":"2024-03-13T11:57:00.843255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(rotation_range = 10, width_shift_range = 0.1, shear_range = 0.1,\n                             height_shift_range = 0.1, zoom_range = [0.9,1.1], fill_mode = 'nearest', \n                             horizontal_flip = False, vertical_flip = False)\ndatagen.fit(x_train)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T11:57:00.846522Z","iopub.execute_input":"2024-03-13T11:57:00.847278Z","iopub.status.idle":"2024-03-13T11:57:00.969690Z","shell.execute_reply.started":"2024-03-13T11:57:00.847224Z","shell.execute_reply":"2024-03-13T11:57:00.968907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Conv_Net_model():\n  model = Sequential()\n  model.add(Conv2D(32,(3,3), input_shape=(28,28,1), padding=\"same\", activation=\"relu\"))\n  model.add(MaxPooling2D(pool_size=(2,2)))\n  model.add(BatchNormalization(axis = -1))\n  model.add(Conv2D(64,(3,3), activation=\"relu\", padding=\"same\"))\n  model.add(MaxPooling2D(pool_size=(2,2)))\n  model.add(BatchNormalization(axis = -1))\n  model.add(Flatten())\n  model.add(Dense(200, activation=\"relu\"))\n  model.add(BatchNormalization(axis = -1))\n  model.add(Dropout(rate=0.4))\n  model.add(Dense(100, activation=\"relu\"))\n  model.add(BatchNormalization(axis = -1))\n  model.add(Dropout(rate=0.4))\n  model.add(Dense(10, activation=\"softmax\"))\n  return model\n\nmodel = Conv_Net_model()\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2024-03-13T12:39:09.523526Z","iopub.execute_input":"2024-03-13T12:39:09.523906Z","iopub.status.idle":"2024-03-13T12:39:09.707838Z","shell.execute_reply.started":"2024-03-13T12:39:09.523875Z","shell.execute_reply":"2024-03-13T12:39:09.706890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduce_lr = ReduceLROnPlateau(monitor = 'val_accuracy', \n                              min_lr = 0.00001, verbose = 1,  patience = 2, factor = 0.7, mode = 'auto', cooldown = 0)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T12:39:12.312860Z","iopub.execute_input":"2024-03-13T12:39:12.313574Z","iopub.status.idle":"2024-03-13T12:39:12.318098Z","shell.execute_reply.started":"2024-03-13T12:39:12.313541Z","shell.execute_reply":"2024-03-13T12:39:12.317102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\n\nopt = Adam(learning_rate=0.001, beta_1=0.99, beta_2=0.999, amsgrad=True)\nmodel.compile(optimizer=opt, loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])\nh = model.fit(datagen.flow(x_train, y_train, batch_size=256), epochs=20, \n              validation_data=(x_val, y_val), shuffle=1, verbose=1, callbacks=[reduce_lr])","metadata":{"execution":{"iopub.status.busy":"2024-03-13T12:51:11.414569Z","iopub.execute_input":"2024-03-13T12:51:11.414939Z","iopub.status.idle":"2024-03-13T12:55:50.888444Z","shell.execute_reply.started":"2024-03-13T12:51:11.414909Z","shell.execute_reply":"2024-03-13T12:55:50.887448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(x_test)\ny_test = np.argmax(y_pred,axis=1)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T12:55:59.853800Z","iopub.execute_input":"2024-03-13T12:55:59.854162Z","iopub.status.idle":"2024-03-13T12:56:02.182964Z","shell.execute_reply.started":"2024-03-13T12:55:59.854132Z","shell.execute_reply":"2024-03-13T12:56:02.181962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")\ni = sample.ImageId\n\noutput = pd.DataFrame({'ImageId' : i,'Label': y_test})\noutput.sample(20)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T12:56:05.229078Z","iopub.execute_input":"2024-03-13T12:56:05.229908Z","iopub.status.idle":"2024-03-13T12:56:05.257628Z","shell.execute_reply.started":"2024-03-13T12:56:05.229873Z","shell.execute_reply":"2024-03-13T12:56:05.256729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(x_test[15089])\n","metadata":{"execution":{"iopub.status.busy":"2024-03-13T12:56:14.696170Z","iopub.execute_input":"2024-03-13T12:56:14.696652Z","iopub.status.idle":"2024-03-13T12:56:14.782260Z","shell.execute_reply.started":"2024-03-13T12:56:14.696619Z","shell.execute_reply":"2024-03-13T12:56:14.780849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T12:56:20.862104Z","iopub.execute_input":"2024-03-13T12:56:20.862495Z","iopub.status.idle":"2024-03-13T12:56:20.903317Z","shell.execute_reply.started":"2024-03-13T12:56:20.862449Z","shell.execute_reply":"2024-03-13T12:56:20.902583Z"},"trusted":true},"execution_count":null,"outputs":[]}]}