{"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":"2023-08-09T06:34:05.064642Z","iopub.execute_input":"2023-08-09T06:34:05.065205Z","iopub.status.idle":"2023-08-09T06:34:05.074787Z","shell.execute_reply.started":"2023-08-09T06:34:05.065156Z","shell.execute_reply":"2023-08-09T06:34:05.073480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.datasets import cifar10\nfrom tensorflow import keras\nimport tensorflow as tf\nassert tf.__version__ >= \"2.0\"","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2023-08-09T06:34:05.076681Z","iopub.execute_input":"2023-08-09T06:34:05.077295Z","iopub.status.idle":"2023-08-09T06:34:10.198423Z","shell.execute_reply.started":"2023-08-09T06:34:05.077244Z","shell.execute_reply":"2023-08-09T06:34:10.197446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The data, split between train and test sets:\n(x_train, y_train), (x_test, y_test) = cifar10.load_data()\nprint('x_train shape:', x_train.shape)\nprint('y_train shape:', y_train.shape)\nprint(x_train.shape[0], 'train samples')\nprint(x_test.shape[0], 'test samples')","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:13:27.758434Z","iopub.execute_input":"2023-08-09T07:13:27.758819Z","iopub.status.idle":"2023-08-09T07:13:28.536446Z","shell.execute_reply.started":"2023-08-09T07:13:27.758783Z","shell.execute_reply":"2023-08-09T07:13:28.535473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### DNN model\n\n* Self normalizing network with SELU and lecun normal initializer","metadata":{}},{"cell_type":"code","source":"# model\n\nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.experimental.preprocessing.Rescaling(1./255, input_shape=(32,32,3)),\n    tf.keras.layers.Conv2D(512, (3,3), activation = 'relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(128, (3,3), activation = 'relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2,2)),\n    tf.keras.layers.Conv2D(128, (3,3), activation = 'relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(128, (3,3), activation = 'relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.GlobalMaxPooling2D(),\n    \n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Dense(128, activation = 'relu'),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Dense(64, activation = 'relu'),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Dense(64, activation = 'relu'),\n    tf.keras.layers.Dense(10, activation = 'softmax')\n])\n\n#optimizer = keras.optimizers.Nadam(lr=5e-2)\noptimizer = keras.optimizers.Adam()\nmodel.compile(loss='sparse_categorical_crossentropy', metrics = 'accuracy', optimizer = optimizer)\n\nearly_stoping = keras.callbacks.EarlyStopping(patience=10)\n#model_best = keras.callbacks.ModelCheckpoint(save_best_only=True)\ncallbacks = [early_stoping]","metadata":{"execution":{"iopub.status.busy":"2023-08-09T06:49:40.603765Z","iopub.execute_input":"2023-08-09T06:49:40.604169Z","iopub.status.idle":"2023-08-09T06:49:40.821517Z","shell.execute_reply.started":"2023-08-09T06:49:40.604134Z","shell.execute_reply":"2023-08-09T06:49:40.820625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model.summary())","metadata":{"execution":{"iopub.status.busy":"2023-08-09T06:49:40.990565Z","iopub.execute_input":"2023-08-09T06:49:40.990924Z","iopub.status.idle":"2023-08-09T06:49:41.007563Z","shell.execute_reply.started":"2023-08-09T06:49:40.990891Z","shell.execute_reply":"2023-08-09T06:49:41.006181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nplot_model(model)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T06:49:41.544417Z","iopub.execute_input":"2023-08-09T06:49:41.544790Z","iopub.status.idle":"2023-08-09T06:49:41.756164Z","shell.execute_reply.started":"2023-08-09T06:49:41.544751Z","shell.execute_reply":"2023-08-09T06:49:41.755172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, validation_split=0.3, epochs=500, callbacks = callbacks)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T06:49:42.352667Z","iopub.execute_input":"2023-08-09T06:49:42.353079Z","iopub.status.idle":"2023-08-09T06:59:52.374501Z","shell.execute_reply.started":"2023-08-09T06:49:42.353039Z","shell.execute_reply":"2023-08-09T06:59:52.373578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict_classes(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:12:14.273114Z","iopub.execute_input":"2023-08-09T07:12:14.273485Z","iopub.status.idle":"2023-08-09T07:12:15.255817Z","shell.execute_reply.started":"2023-08-09T07:12:14.273452Z","shell.execute_reply":"2023-08-09T07:12:15.254607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from itertools import chain\n\ny_test = list(chain(*y_test))\ny_pred = list(y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:13:43.181944Z","iopub.execute_input":"2023-08-09T07:13:43.182313Z","iopub.status.idle":"2023-08-09T07:13:43.201097Z","shell.execute_reply.started":"2023-08-09T07:13:43.182276Z","shell.execute_reply":"2023-08-09T07:13:43.200178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test Accuracy\nsum([x == y for x,y in zip(y_test, y_pred)])/ len(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:18:45.110077Z","iopub.execute_input":"2023-08-09T07:18:45.110500Z","iopub.status.idle":"2023-08-09T07:18:45.189106Z","shell.execute_reply.started":"2023-08-09T07:18:45.110462Z","shell.execute_reply":"2023-08-09T07:18:45.188081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import ConfusionMatrixDisplay\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:39:53.435312Z","iopub.execute_input":"2023-08-09T07:39:53.435667Z","iopub.status.idle":"2023-08-09T07:39:53.441706Z","shell.execute_reply.started":"2023-08-09T07:39:53.435633Z","shell.execute_reply":"2023-08-09T07:39:53.440768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['airplane', 'automobile','bird','cat','deer','dog','frog','horse','ship','truck',]","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:44:28.111004Z","iopub.execute_input":"2023-08-09T07:44:28.111404Z","iopub.status.idle":"2023-08-09T07:44:28.116292Z","shell.execute_reply.started":"2023-08-09T07:44:28.111368Z","shell.execute_reply":"2023-08-09T07:44:28.115288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize = (15,15))\ncm = tf.math.confusion_matrix(y_test, y_pred)\ncm_plot = ConfusionMatrixDisplay(cm.numpy(), display_labels = labels)\ncm_plot.plot(ax = ax)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T07:47:02.593999Z","iopub.execute_input":"2023-08-09T07:47:02.594388Z","iopub.status.idle":"2023-08-09T07:47:03.155114Z","shell.execute_reply.started":"2023-08-09T07:47:02.594350Z","shell.execute_reply":"2023-08-09T07:47:03.154074Z"},"trusted":true},"execution_count":null,"outputs":[]}]}