{"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":"2022-05-04T16:57:03.761352Z","iopub.execute_input":"2022-05-04T16:57:03.761653Z","iopub.status.idle":"2022-05-04T16:57:03.800342Z","shell.execute_reply.started":"2022-05-04T16:57:03.761548Z","shell.execute_reply":"2022-05-04T16:57:03.799136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nfrom keras.datasets import cifar10\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras import models\nfrom keras.layers import Dense, Dropout, Activation, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:57:03.802384Z","iopub.execute_input":"2022-05-04T16:57:03.803029Z","iopub.status.idle":"2022-05-04T16:57:09.593333Z","shell.execute_reply.started":"2022-05-04T16:57:03.802987Z","shell.execute_reply":"2022-05-04T16:57:09.592311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(x_train, y_train), (x_test, y_test) = cifar10.load_data()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:57:09.596089Z","iopub.execute_input":"2022-05-04T16:57:09.596605Z","iopub.status.idle":"2022-05-04T16:57:18.192961Z","shell.execute_reply.started":"2022-05-04T16:57:09.596557Z","shell.execute_reply":"2022-05-04T16:57:18.192025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('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":"2022-05-04T16:57:18.195929Z","iopub.execute_input":"2022-05-04T16:57:18.196229Z","iopub.status.idle":"2022-05-04T16:57:18.206831Z","shell.execute_reply.started":"2022-05-04T16:57:18.196177Z","shell.execute_reply":"2022-05-04T16:57:18.205696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize the data. Before we need to connvert data type to float for computation.\nx_train = x_train.astype('float32')\nx_test = x_test.astype('float32')\nx_train /= 255\nx_test /= 255","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:57:18.208575Z","iopub.execute_input":"2022-05-04T16:57:18.209073Z","iopub.status.idle":"2022-05-04T16:57:18.752685Z","shell.execute_reply.started":"2022-05-04T16:57:18.208998Z","shell.execute_reply":"2022-05-04T16:57:18.751732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import np_utils\n\n# Convert class vectors to binary class matrices. This is called one hot encoding.\ny_train = keras.utils.np_utils.to_categorical(y_train, 10)\ny_test = keras.utils.np_utils.to_categorical(y_test, 10)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:57:18.754044Z","iopub.execute_input":"2022-05-04T16:57:18.754307Z","iopub.status.idle":"2022-05-04T16:57:18.76211Z","shell.execute_reply.started":"2022-05-04T16:57:18.754268Z","shell.execute_reply":"2022-05-04T16:57:18.76115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#define the convnet\nModel = Sequential([\n    # CONV => RELU => CONV => RELU => POOL => DROPOUT\n    Conv2D(32, (3, 3), activation = 'relu', padding='same',input_shape=x_train.shape[1:]),\n    Conv2D(32, (3, 3), activation = 'relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    \n    # CONV => RELU => CONV => RELU => POOL => DROPOUT\n    Conv2D(64, (3, 3), activation = 'relu', padding='same'),\n    Conv2D(64, (3, 3), activation = 'relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    \n    # FLATTERN => DENSE => RELU => DROPOUT\n    Flatten(),\n    Dense(512, activation = 'relu'),\n    Dropout(0.5),\n    Dense(10, activation = 'softmax')\n    \n])\n\nModel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:57:18.763642Z","iopub.execute_input":"2022-05-04T16:57:18.764291Z","iopub.status.idle":"2022-05-04T16:57:21.845425Z","shell.execute_reply.started":"2022-05-04T16:57:18.764235Z","shell.execute_reply":"2022-05-04T16:57:21.844588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model.compile(loss='categorical_crossentropy', \n              optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:57:21.849945Z","iopub.execute_input":"2022-05-04T16:57:21.852812Z","iopub.status.idle":"2022-05-04T16:57:21.869795Z","shell.execute_reply.started":"2022-05-04T16:57:21.852769Z","shell.execute_reply":"2022-05-04T16:57:21.868691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = Model.fit(x_train, y_train, batch_size=60,epochs=30)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:57:21.872334Z","iopub.execute_input":"2022-05-04T16:57:21.874212Z","iopub.status.idle":"2022-05-04T17:00:13.716719Z","shell.execute_reply.started":"2022-05-04T16:57:21.874167Z","shell.execute_reply":"2022-05-04T17:00:13.714175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = Model.evaluate(x_train, y_train)\nprint(f\"Loss: {metrics[0]}\")\nprint(f\"Accuracy: {metrics[1]}\")","metadata":{"execution":{"iopub.status.busy":"2022-05-04T17:00:13.723821Z","iopub.execute_input":"2022-05-04T17:00:13.724439Z","iopub.status.idle":"2022-05-04T17:00:21.724697Z","shell.execute_reply.started":"2022-05-04T17:00:13.724392Z","shell.execute_reply":"2022-05-04T17:00:21.723415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model.save('cifar10.model')","metadata":{"execution":{"iopub.status.busy":"2022-05-04T17:00:21.813964Z","iopub.execute_input":"2022-05-04T17:00:21.815242Z","iopub.status.idle":"2022-05-04T17:00:24.622511Z","shell.execute_reply.started":"2022-05-04T17:00:21.815196Z","shell.execute_reply":"2022-05-04T17:00:24.621508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model = models.load_model('cifar10.model')\n\nnew_model","metadata":{"execution":{"iopub.status.busy":"2022-05-04T17:00:24.630185Z","iopub.execute_input":"2022-05-04T17:00:24.630585Z","iopub.status.idle":"2022-05-04T17:00:25.60574Z","shell.execute_reply.started":"2022-05-04T17:00:24.630523Z","shell.execute_reply":"2022-05-04T17:00:25.604591Z"},"trusted":true},"execution_count":null,"outputs":[]}]}