{"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# Import TensorFlow\nimport tensorflow as tf\n\n# Import Numpy and Matplotlib\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom keras.preprocessing.image import ImageDataGenerator\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-09-26T11:12:53.597828Z","iopub.execute_input":"2022-09-26T11:12:53.599047Z","iopub.status.idle":"2022-09-26T11:12:59.493412Z","shell.execute_reply.started":"2022-09-26T11:12:53.598944Z","shell.execute_reply":"2022-09-26T11:12:59.491206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:12:59.495451Z","iopub.execute_input":"2022-09-26T11:12:59.496153Z","iopub.status.idle":"2022-09-26T11:13:14.326932Z","shell.execute_reply.started":"2022-09-26T11:12:59.496115Z","shell.execute_reply":"2022-09-26T11:13:14.325728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m py7zr x ../input/cifar-10/train.7z","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:13:14.330202Z","iopub.execute_input":"2022-09-26T11:13:14.330571Z","iopub.status.idle":"2022-09-26T11:14:00.771238Z","shell.execute_reply.started":"2022-09-26T11:13:14.330538Z","shell.execute_reply":"2022-09-26T11:14:00.769886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\ny_test = pd.read_csv('/kaggle/input/cifar-10/sampleSubmission.csv')\n\ny_train = np.array(y_train['label'])\n#Y_test = np.array(y_test['label'])\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:00.774250Z","iopub.execute_input":"2022-09-26T11:14:00.774568Z","iopub.status.idle":"2022-09-26T11:14:00.920774Z","shell.execute_reply.started":"2022-09-26T11:14:00.774536Z","shell.execute_reply":"2022-09-26T11:14:00.919741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nfrom PIL import Image\nfilelist = glob.glob(\"train/*\")\nfilelist = sorted(filelist,key= lambda x:int(x.split(\"/\")[-1].split(\".\")[0]))\nx_train = np.array([np.array(Image.open(fname)) for fname in filelist])\nprint(f\"X.shape : {x_train.shape} y.shape : {y_train.shape}\")\n\nfilelist = glob.glob(\"test/*\")\nfilelist = sorted(filelist,key= lambda x:int(x.split(\"/\")[-1].split(\".\")[0]))\nx_test = np.array([np.array(Image.open(fname)) for fname in filelist])\nprint(f\"X.shape : {x_test.shape} y.shape : {y_test.shape}\")","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:00.922084Z","iopub.execute_input":"2022-09-26T11:14:00.922938Z","iopub.status.idle":"2022-09-26T11:14:10.090936Z","shell.execute_reply.started":"2022-09-26T11:14:00.922900Z","shell.execute_reply":"2022-09-26T11:14:10.089905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train[42]","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:10.092244Z","iopub.execute_input":"2022-09-26T11:14:10.093018Z","iopub.status.idle":"2022-09-26T11:14:10.105623Z","shell.execute_reply.started":"2022-09-26T11:14:10.092981Z","shell.execute_reply":"2022-09-26T11:14:10.104731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use the .imshow() function and show the plot\nplt.imshow(x_train[42])\nplt.show()\n# Print the shape of the sample image\nx_train[42].shape # RGB images have 3 channels ","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:10.107253Z","iopub.execute_input":"2022-09-26T11:14:10.107677Z","iopub.status.idle":"2022-09-26T11:14:10.315233Z","shell.execute_reply.started":"2022-09-26T11:14:10.107639Z","shell.execute_reply":"2022-09-26T11:14:10.314376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the validation datasets \n# and assign the last 10000 images of X_train and y_train\nx_val = x_train[40000:]\ny_val = y_train[40000:]\n# Create new train datasets\n# and assign the first 40000 images of X_train and y_train\nx_train = x_train[:40000]\ny_train = y_train[:40000]","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:10.316833Z","iopub.execute_input":"2022-09-26T11:14:10.317167Z","iopub.status.idle":"2022-09-26T11:14:10.323376Z","shell.execute_reply.started":"2022-09-26T11:14:10.317134Z","shell.execute_reply":"2022-09-26T11:14:10.322290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"X Train Length:\", len(x_train))\nprint(\"X Test Length:\", len(x_test))\nprint(\"X Val Length:\", len(x_val))\nprint(\"Y Train Length:\", len(y_train))\nprint(\"Y Test Length:\", len(y_test))\nprint(\"Y Val Length:\", len(y_val))","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:10.324719Z","iopub.execute_input":"2022-09-26T11:14:10.325491Z","iopub.status.idle":"2022-09-26T11:14:10.335932Z","shell.execute_reply.started":"2022-09-26T11:14:10.325455Z","shell.execute_reply":"2022-09-26T11:14:10.334747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Divide each dataset by 255\nx_train = x_train / 255\nx_val = x_val / 255","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:10.340375Z","iopub.execute_input":"2022-09-26T11:14:10.340662Z","iopub.status.idle":"2022-09-26T11:14:10.725124Z","shell.execute_reply.started":"2022-09-26T11:14:10.340636Z","shell.execute_reply":"2022-09-26T11:14:10.724057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a model object\nmodel = tf.keras.Sequential()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:10.726732Z","iopub.execute_input":"2022-09-26T11:14:10.727156Z","iopub.status.idle":"2022-09-26T11:14:13.721706Z","shell.execute_reply.started":"2022-09-26T11:14:10.727115Z","shell.execute_reply":"2022-09-26T11:14:13.720728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Conv2D(64,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\",\n                                 input_shape=(32,32,3)))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(64,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(64,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.MaxPooling2D((2,2)))\nmodel.add( tf.keras.layers.Dropout(0.25))\n\nmodel.add(tf.keras.layers.Conv2D(128,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(128,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(128,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add( tf.keras.layers.Dropout(0.30))\nmodel.add(tf.keras.layers.MaxPooling2D((2,2)))\n\nmodel.add(tf.keras.layers.Conv2D(256,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(256,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(256,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\n\nmodel.add(tf.keras.layers.MaxPooling2D((2,2)))\nmodel.add( tf.keras.layers.Dropout(0.35))","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:13.723334Z","iopub.execute_input":"2022-09-26T11:14:13.723729Z","iopub.status.idle":"2022-09-26T11:14:14.134361Z","shell.execute_reply.started":"2022-09-26T11:14:13.723676Z","shell.execute_reply":"2022-09-26T11:14:14.133433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add the dense layer and dropout layer\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(512, activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\nmodel.add( tf.keras.layers.Dropout(0.5))\n\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(1028, activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\nmodel.add( tf.keras.layers.Dropout(0.5))","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:14.136864Z","iopub.execute_input":"2022-09-26T11:14:14.137551Z","iopub.status.idle":"2022-09-26T11:14:14.201712Z","shell.execute_reply.started":"2022-09-26T11:14:14.137513Z","shell.execute_reply":"2022-09-26T11:14:14.200357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add the output layer\nmodel.add(tf.keras.layers.Dense(10, activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:14.203985Z","iopub.execute_input":"2022-09-26T11:14:14.204254Z","iopub.status.idle":"2022-09-26T11:14:14.220177Z","shell.execute_reply.started":"2022-09-26T11:14:14.204228Z","shell.execute_reply":"2022-09-26T11:14:14.219212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer=\"adam\",\n              loss=\"sparse_categorical_crossentropy\",\n              metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:14.222896Z","iopub.execute_input":"2022-09-26T11:14:14.223347Z","iopub.status.idle":"2022-09-26T11:14:14.238888Z","shell.execute_reply.started":"2022-09-26T11:14:14.223303Z","shell.execute_reply":"2022-09-26T11:14:14.238017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nencoder = LabelEncoder()\ny_train = encoder.fit_transform(y_train)\ny_val = encoder.fit_transform(y_val)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:14.242078Z","iopub.execute_input":"2022-09-26T11:14:14.242348Z","iopub.status.idle":"2022-09-26T11:14:14.493974Z","shell.execute_reply.started":"2022-09-26T11:14:14.242322Z","shell.execute_reply":"2022-09-26T11:14:14.492996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[0]","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:14.495643Z","iopub.execute_input":"2022-09-26T11:14:14.496055Z","iopub.status.idle":"2022-09-26T11:14:14.503216Z","shell.execute_reply.started":"2022-09-26T11:14:14.496018Z","shell.execute_reply":"2022-09-26T11:14:14.502199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.asarray(x_train).astype(np.float32)\nx_val = np.asarray(x_val).astype(np.float32)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:14.504960Z","iopub.execute_input":"2022-09-26T11:14:14.505624Z","iopub.status.idle":"2022-09-26T11:14:14.752424Z","shell.execute_reply.started":"2022-09-26T11:14:14.505589Z","shell.execute_reply":"2022-09-26T11:14:14.751446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.fit(x_train, y_train, epochs=50,\n                    validation_data=(x_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:14:14.754233Z","iopub.execute_input":"2022-09-26T11:14:14.754607Z","iopub.status.idle":"2022-09-26T11:26:30.808893Z","shell.execute_reply.started":"2022-09-26T11:14:14.754567Z","shell.execute_reply":"2022-09-26T11:26:30.807959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the the training loss\nplt.plot(results.history[\"loss\"], label=\"loss\")\n\n# Plot the the validation loss\nplt.plot(results.history[\"val_loss\"], label=\"val_loss\")\n\n# Name the x and y axises\nplt.xlabel(\"epochs\")\nplt.ylabel(\"Loss\")\n# Put legend table\nplt.legend()\nplt.grid()\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:26:30.810483Z","iopub.execute_input":"2022-09-26T11:26:30.810855Z","iopub.status.idle":"2022-09-26T11:26:31.030534Z","shell.execute_reply.started":"2022-09-26T11:26:30.810811Z","shell.execute_reply":"2022-09-26T11:26:31.029606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the the training accuracy\nplt.plot(results.history[\"accuracy\"], label=\"accuracy\")\n\n# Plot the the validation accuracy\nplt.plot(results.history[\"val_accuracy\"], label=\"val_accuracy\")\n\n# Name the x and y axises\nplt.xlabel(\"epochs\")\nplt.ylabel(\"Accuracy\")\n# Put legend table\nplt.legend()\nplt.grid()\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:26:31.031816Z","iopub.execute_input":"2022-09-26T11:26:31.032424Z","iopub.status.idle":"2022-09-26T11:26:31.263524Z","shell.execute_reply.started":"2022-09-26T11:26:31.032392Z","shell.execute_reply":"2022-09-26T11:26:31.262589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nfrom py7zr import unpack_7zarchive\nshutil.register_unpack_format('7zip',['.7z'],unpack_7zarchive)\nshutil.unpack_archive('/kaggle/input/cifar-10/test.7z','/kaggle/temp/test')\nshutil.unregister_unpack_format('7zip')\ntest_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\n\ntest_gen = test_datagen.flow_from_directory(directory='/kaggle/temp/test',target_size=(32,32),batch_size=64,class_mode=None,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:26:31.264942Z","iopub.execute_input":"2022-09-26T11:26:31.265972Z","iopub.status.idle":"2022-09-26T11:40:25.038934Z","shell.execute_reply.started":"2022-09-26T11:26:31.265933Z","shell.execute_reply":"2022-09-26T11:40:25.037934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen.reset()\npredictions_vecs = model.predict(test_gen)\n\npredictions_final = np.argmax(predictions_vecs, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:40:25.040572Z","iopub.execute_input":"2022-09-26T11:40:25.040981Z","iopub.status.idle":"2022-09-26T11:42:01.202424Z","shell.execute_reply.started":"2022-09-26T11:40:25.040942Z","shell.execute_reply":"2022-09-26T11:42:01.201395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cifar10_classes = [\"airplane\", \"automobile\", \"bird\", \"cat\", \"deer\", \"dog\", \"frog\", \"horse\", \"ship\", \"truck\"]\nprint(cifar10_classes)\npredicted_classes=np.empty(shape=300000,dtype=np.dtype('U20'))\n\nind=0\nfor i in predictions_final.tolist():\n    predicted_classes[ind]=cifar10_classes[i]\n    ind=ind+1\n    \nfilenames_wo_ext = []\nfor fname in test_gen.filenames:\n    filenames_wo_ext.append(int(fname.split(sep=\"/\")[1].split(sep=\".\")[0])-1)\n\npredicted_classes_final = np.empty(shape=300000,dtype=np.dtype('U20'))\npredicted_classes_final[filenames_wo_ext]=predicted_classes","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:42:01.203976Z","iopub.execute_input":"2022-09-26T11:42:01.204326Z","iopub.status.idle":"2022-09-26T11:42:01.566722Z","shell.execute_reply.started":"2022-09-26T11:42:01.204286Z","shell.execute_reply":"2022-09-26T11:42:01.565730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/cifar-10/sampleSubmission.csv',header='infer')\nsub.info()\n\nsub['label'] = predicted_classes_final\nsub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T11:42:01.568322Z","iopub.execute_input":"2022-09-26T11:42:01.568708Z","iopub.status.idle":"2022-09-26T11:42:01.964957Z","shell.execute_reply.started":"2022-09-26T11:42:01.568654Z","shell.execute_reply":"2022-09-26T11:42:01.963888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}