{"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-12-28T12:12:19.670433Z","iopub.execute_input":"2021-12-28T12:12:19.670784Z","iopub.status.idle":"2021-12-28T12:12:19.699978Z","shell.execute_reply.started":"2021-12-28T12:12:19.670686Z","shell.execute_reply":"2021-12-28T12:12:19.699061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_datasets as tfds\n(x_train, y_train), (x_test, y_test) = tfds.as_numpy(tfds.load('mnist', split=['train','test'],\n                                                              batch_size=-1,\n                                                              as_supervised=True,\n                                                              shuffle_files=True))","metadata":{"execution":{"iopub.status.busy":"2021-12-28T12:14:10.701288Z","iopub.execute_input":"2021-12-28T12:14:10.701604Z","iopub.status.idle":"2021-12-28T12:14:25.912541Z","shell.execute_reply.started":"2021-12-28T12:14:10.701554Z","shell.execute_reply":"2021-12-28T12:14:25.911550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimg_index = 777\nprint(\"The digit in the image: \", y_train[img_index])\nplt.imshow(x_train[img_index].reshape(28,28), cmap='Greys')","metadata":{"execution":{"iopub.status.busy":"2021-12-28T12:15:51.336953Z","iopub.execute_input":"2021-12-28T12:15:51.337320Z","iopub.status.idle":"2021-12-28T12:15:51.569873Z","shell.execute_reply.started":"2021-12-28T12:15:51.337285Z","shell.execute_reply":"2021-12-28T12:15:51.568857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-28T12:16:17.363267Z","iopub.execute_input":"2021-12-28T12:16:17.363594Z","iopub.status.idle":"2021-12-28T12:16:17.370121Z","shell.execute_reply.started":"2021-12-28T12:16:17.363542Z","shell.execute_reply":"2021-12-28T12:16:17.369260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making sure that the values are float so that we can get decimal points after division\nx_train = x_train.astype('float32')\nx_test = x_test.astype('float32')\n#Normalizing the grayscale RGB codes by dividing it to the 'max minus min grayscale RGB value\"\nx_train /=255\nx_test /=255\nprint('x_train shape:', x_train.shape)\nprint('Number of images in x_train', x_train.shape[0])\nprint('Number of images in x_test', x_test.shape[0])","metadata":{"execution":{"iopub.status.busy":"2021-12-28T12:19:28.409346Z","iopub.execute_input":"2021-12-28T12:19:28.410338Z","iopub.status.idle":"2021-12-28T12:19:28.531620Z","shell.execute_reply.started":"2021-12-28T12:19:28.410290Z","shell.execute_reply":"2021-12-28T12:19:28.530763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing the required Keras modules containing model and layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D\n\n#Creating a Sequential Model and adding the layers\nmodel = Sequential()\nmodel.add(Conv2D(28, kernel_size=(3,3), input_shape=(28,28,1)))\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-12-28T12:23:37.187065Z","iopub.execute_input":"2021-12-28T12:23:37.187400Z","iopub.status.idle":"2021-12-28T12:23:37.239204Z","shell.execute_reply.started":"2021-12-28T12:23:37.187369Z","shell.execute_reply":"2021-12-28T12:23:37.238337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compiling and Fitting the Model","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer='adam',\n             loss='sparse_categorical_crossentropy',\n             metrics=['accuracy'])\nmodel.fit(x=x_train, y=y_train, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2021-12-28T12:25:08.587331Z","iopub.execute_input":"2021-12-28T12:25:08.587636Z","iopub.status.idle":"2021-12-28T12:27:09.783273Z","shell.execute_reply.started":"2021-12-28T12:25:08.587596Z","shell.execute_reply":"2021-12-28T12:27:09.782504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2021-12-28T12:27:09.784714Z","iopub.execute_input":"2021-12-28T12:27:09.785318Z","iopub.status.idle":"2021-12-28T12:27:10.923359Z","shell.execute_reply.started":"2021-12-28T12:27:09.785282Z","shell.execute_reply":"2021-12-28T12:27:10.922416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_pred_index = 1000\nplt.imshow(x_test[img_pred_index].reshape(28,28), cmap='Greys')\npred= model.predict(x_test[img_pred_index].reshape(1,28,28,1))\nprint('The CNN predicts that the digit in the image is: ', pred.argmax())","metadata":{"execution":{"iopub.status.busy":"2021-12-28T12:28:15.997411Z","iopub.execute_input":"2021-12-28T12:28:15.997706Z","iopub.status.idle":"2021-12-28T12:28:16.531158Z","shell.execute_reply.started":"2021-12-28T12:28:15.997671Z","shell.execute_reply":"2021-12-28T12:28:16.530487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a directory\n!mkdir -p saved_model","metadata":{"execution":{"iopub.status.busy":"2021-12-28T12:29:33.080670Z","iopub.execute_input":"2021-12-28T12:29:33.080957Z","iopub.status.idle":"2021-12-28T12:29:33.889066Z","shell.execute_reply.started":"2021-12-28T12:29:33.080928Z","shell.execute_reply":"2021-12-28T12:29:33.888169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model\nmodel.save('saved_model/digit_classifier')","metadata":{"execution":{"iopub.status.busy":"2021-12-28T12:30:18.422811Z","iopub.execute_input":"2021-12-28T12:30:18.423231Z","iopub.status.idle":"2021-12-28T12:30:19.259332Z","shell.execute_reply.started":"2021-12-28T12:30:18.423201Z","shell.execute_reply":"2021-12-28T12:30:19.258646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}