{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2021-09-04T07:58:27.575920Z","iopub.execute_input":"2021-09-04T07:58:27.576244Z","iopub.status.idle":"2021-09-04T07:58:27.583181Z","shell.execute_reply.started":"2021-09-04T07:58:27.576213Z","shell.execute_reply":"2021-09-04T07:58:27.581978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D\nfrom tensorflow import keras\nfrom numpy.linalg import norm\nimport os\nfrom tqdm import tqdm\nimport cv2\n%matplotlib inline\npd.set_option('display.max_columns', 1000)\npd.set_option('display.max_rows', None)\nimport warnings   # To avoid warning messages in the code run\nwarnings.filterwarnings(\"ignore\")\n\nimport os # os - operating system (Windows, Mac, Linux) os.mkdir('abc') --> will create a folder for me with the name 'abc'","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:58:27.589831Z","iopub.execute_input":"2021-09-04T07:58:27.590461Z","iopub.status.idle":"2021-09-04T07:58:32.597282Z","shell.execute_reply.started":"2021-09-04T07:58:27.590426Z","shell.execute_reply":"2021-09-04T07:58:32.596445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:58:32.598625Z","iopub.execute_input":"2021-09-04T07:58:32.598937Z","iopub.status.idle":"2021-09-04T07:58:37.857888Z","shell.execute_reply.started":"2021-09-04T07:58:32.598904Z","shell.execute_reply":"2021-09-04T07:58:37.857090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:58:37.859474Z","iopub.execute_input":"2021-09-04T07:58:37.859796Z","iopub.status.idle":"2021-09-04T07:58:37.864974Z","shell.execute_reply.started":"2021-09-04T07:58:37.859762Z","shell.execute_reply":"2021-09-04T07:58:37.864170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"order = list(np.sort(train['label'].unique()))\nprint(order)","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:58:37.866678Z","iopub.execute_input":"2021-09-04T07:58:37.867311Z","iopub.status.idle":"2021-09-04T07:58:37.879992Z","shell.execute_reply.started":"2021-09-04T07:58:37.867275Z","shell.execute_reply":"2021-09-04T07:58:37.879045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(train[\"label\"]);","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:58:37.881256Z","iopub.execute_input":"2021-09-04T07:58:37.881611Z","iopub.status.idle":"2021-09-04T07:58:38.060421Z","shell.execute_reply.started":"2021-09-04T07:58:37.881578Z","shell.execute_reply":"2021-09-04T07:58:38.059502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Basic Model ","metadata":{}},{"cell_type":"code","source":"X_train = (train.iloc[:,1:].values).astype('float32') \ny_train = train.iloc[:,0].values.astype('int32') \nX_test = test.values.astype('float32')\n\nX_train = X_train.reshape(X_train.shape[0],28,28,1).astype('float32')\nX_test = X_test.reshape(X_test.shape[0],28,28,1).astype('float32')\n\nX_train = X_train / 255\nX_test = X_test / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:58:38.061791Z","iopub.execute_input":"2021-09-04T07:58:38.062127Z","iopub.status.idle":"2021-09-04T07:58:38.307892Z","shell.execute_reply.started":"2021-09-04T07:58:38.062092Z","shell.execute_reply":"2021-09-04T07:58:38.306999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:58:38.309249Z","iopub.execute_input":"2021-09-04T07:58:38.309632Z","iopub.status.idle":"2021-09-04T07:58:38.315223Z","shell.execute_reply.started":"2021-09-04T07:58:38.309598Z","shell.execute_reply":"2021-09-04T07:58:38.314413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(28, kernel_size=(3,3)))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation=tf.nn.relu))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10,activation=tf.nn.softmax))","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:58:38.317731Z","iopub.execute_input":"2021-09-04T07:58:38.318281Z","iopub.status.idle":"2021-09-04T07:58:40.228816Z","shell.execute_reply.started":"2021-09-04T07:58:38.318244Z","shell.execute_reply":"2021-09-04T07:58:40.227947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(x=X_train,y=y_train, epochs=20)","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:58:40.230542Z","iopub.execute_input":"2021-09-04T07:58:40.230970Z","iopub.status.idle":"2021-09-04T07:59:45.290009Z","shell.execute_reply.started":"2021-09-04T07:58:40.230928Z","shell.execute_reply":"2021-09-04T07:59:45.289141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict_classes(X_test, verbose=0)\npredictions","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:59:45.291450Z","iopub.execute_input":"2021-09-04T07:59:45.291714Z","iopub.status.idle":"2021-09-04T07:59:46.236283Z","shell.execute_reply.started":"2021-09-04T07:59:45.291687Z","shell.execute_reply":"2021-09-04T07:59:46.235544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model using ResNet50","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:59:46.237567Z","iopub.execute_input":"2021-09-04T07:59:46.237914Z","iopub.status.idle":"2021-09-04T07:59:50.229621Z","shell.execute_reply.started":"2021-09-04T07:59:46.237877Z","shell.execute_reply":"2021-09-04T07:59:50.228746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_SHAPE_RN = (32, 32, 3)\n\ndef create_cnn_architecture_model2(input_shape):\n    inc_net = keras.applications.resnet50.ResNet50(include_top=False, weights='imagenet', \n                                                          input_shape=input_shape)\n    inc_net.trainable = True\n    # Fine-tune the layers\n    for layer in inc_net.layers:\n        layer.trainable = True\n    \n    base_inc = inc_net\n    base_out = base_inc.output\n    pool_out = keras.layers.Flatten()(base_out)\n    \n    hidden1 = keras.layers.Dense(512, activation='relu')(pool_out)\n    drop1 = keras.layers.Dropout(rate=0.3)(hidden1)\n    hidden2 = keras.layers.Dense(512, activation='relu')(drop1)\n    drop2 = keras.layers.Dropout(rate=0.3)(hidden2)\n\n    out = keras.layers.Dense(10, activation='softmax')(drop2)\n\n    model = keras.Model(inputs=base_inc.input, outputs=out)\n    model.compile(optimizer=keras.optimizers.RMSprop(lr=1e-4),\n                    loss='sparse_categorical_crossentropy',\n                    metrics=['accuracy'])\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:59:50.230875Z","iopub.execute_input":"2021-09-04T07:59:50.231195Z","iopub.status.idle":"2021-09-04T07:59:50.240568Z","shell.execute_reply.started":"2021-09-04T07:59:50.231162Z","shell.execute_reply":"2021-09-04T07:59:50.238759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = create_cnn_architecture_model2(input_shape=INPUT_SHAPE_RN)\nmodel2.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:59:50.241732Z","iopub.execute_input":"2021-09-04T07:59:50.242064Z","iopub.status.idle":"2021-09-04T07:59:52.736212Z","shell.execute_reply.started":"2021-09-04T07:59:50.242030Z","shell.execute_reply":"2021-09-04T07:59:52.735379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train['label'].values\nx_train = train.drop('label', axis = 1).values.reshape((-1, 28, 28, 1)) / 255\n# since ResNet is for 3 channel color image but digit is grayscale 1 channle, change it to 3 channel below\nx_train = np.concatenate((x_train,x_train,x_train), axis = 3)\nprint(x_train.shape)\n\nx_test = train.drop('label', axis = 1).values.reshape((-1, 28, 28, 1)) / 255\n# since ResNet is for 3 channel color image but digit is grayscale 1 channle, change it to 3 channel below\nx_test = np.concatenate((x_test,x_test,x_test), axis = 3)\nprint(x_test.shape)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:59:52.737591Z","iopub.execute_input":"2021-09-04T07:59:52.737935Z","iopub.status.idle":"2021-09-04T07:59:53.860294Z","shell.execute_reply.started":"2021-09-04T07:59:52.737899Z","shell.execute_reply":"2021-09-04T07:59:53.859092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_image_array(img, img_size_dims):\n    img = cv2.resize(img, dsize=img_size_dims, \n                     interpolation=cv2.INTER_CUBIC)\n    img = np.array(img, dtype=np.float32)\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:59:53.861625Z","iopub.execute_input":"2021-09-04T07:59:53.861961Z","iopub.status.idle":"2021-09-04T07:59:53.866760Z","shell.execute_reply.started":"2021-09-04T07:59:53.861926Z","shell.execute_reply":"2021-09-04T07:59:53.865949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_DIMS = (32, 32)\n\nx_train = np.array([resize_image_array(img, img_size_dims=IMG_DIMS) for img in x_train])\nx_test = np.array([resize_image_array(img, img_size_dims=IMG_DIMS) for img in x_test])\n\nprint('\\nTrain_images.shape: {}, of {}'.format(x_train.shape, x_train.dtype))\nprint('Test_images.shape: {}, of {}'.format(x_test.shape, x_test.dtype))","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:59:53.867889Z","iopub.execute_input":"2021-09-04T07:59:53.868614Z","iopub.status.idle":"2021-09-04T07:59:57.195370Z","shell.execute_reply.started":"2021-09-04T07:59:53.868492Z","shell.execute_reply":"2021-09-04T07:59:57.194382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 10\nx_train_scaled = x_train / 255.\nmodel2.fit(x_train_scaled, y_train, validation_split=0.1, epochs=EPOCHS)","metadata":{"execution":{"iopub.status.busy":"2021-09-04T07:59:57.197030Z","iopub.execute_input":"2021-09-04T07:59:57.197590Z","iopub.status.idle":"2021-09-04T08:08:40.305829Z","shell.execute_reply.started":"2021-09-04T07:59:57.197550Z","shell.execute_reply":"2021-09-04T08:08:40.305030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test_scaled = x_test / 255.\npredictions = model2.predict(x_test_scaled)\npredictions","metadata":{"execution":{"iopub.status.busy":"2021-09-04T08:08:40.308566Z","iopub.execute_input":"2021-09-04T08:08:40.308822Z","iopub.status.idle":"2021-09-04T08:08:53.327679Z","shell.execute_reply.started":"2021-09-04T08:08:40.308796Z","shell.execute_reply":"2021-09-04T08:08:53.326910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest_id = np.arange(1, x_test.shape[0] + 1, 1)\nprediction = np.argmax(predictions, axis = 1)\n\npred = pd.DataFrame(data = {'ImageId': test_id,\n                           'Label': prediction})","metadata":{"execution":{"iopub.status.busy":"2021-09-04T08:14:10.204834Z","iopub.execute_input":"2021-09-04T08:14:10.205138Z","iopub.status.idle":"2021-09-04T08:14:10.213453Z","shell.execute_reply.started":"2021-09-04T08:14:10.205109Z","shell.execute_reply":"2021-09-04T08:14:10.212562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}