{"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":"markdown","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"}},{"cell_type":"code","source":"!pip install pytest-shutil\n!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2022-03-24T20:31:58.311368Z","iopub.execute_input":"2022-03-24T20:31:58.311662Z","iopub.status.idle":"2022-03-24T20:32:24.847431Z","shell.execute_reply.started":"2022-03-24T20:31:58.311631Z","shell.execute_reply":"2022-03-24T20:32:24.846162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2 as cv\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization\nfrom keras.layers import Conv2D, MaxPool2D","metadata":{"execution":{"iopub.status.busy":"2022-03-24T20:50:55.743366Z","iopub.execute_input":"2022-03-24T20:50:55.743834Z","iopub.status.idle":"2022-03-24T20:51:00.475891Z","shell.execute_reply.started":"2022-03-24T20:50:55.743796Z","shell.execute_reply":"2022-03-24T20:51:00.474872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nfrom py7zr import unpack_7zarchive\n\ntrain=\"../input/cifar-10/train.7z\"\ntest=\"../input/cifar-10/test.7z\"\nshutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T20:32:24.850718Z","iopub.execute_input":"2022-03-24T20:32:24.851707Z","iopub.status.idle":"2022-03-24T20:32:25.147246Z","shell.execute_reply.started":"2022-03-24T20:32:24.851612Z","shell.execute_reply":"2022-03-24T20:32:25.14624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive(train, '../kaggle/temp')\nshutil.unpack_archive(test, '../kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2022-03-24T20:32:25.149126Z","iopub.execute_input":"2022-03-24T20:32:25.149477Z","iopub.status.idle":"2022-03-24T20:49:24.294205Z","shell.execute_reply.started":"2022-03-24T20:32:25.149426Z","shell.execute_reply":"2022-03-24T20:49:24.292841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n \n# creating a object\nim = Image.open(r\"../kaggle/temp/test/1.png\")\n \nprint(im)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T21:28:35.749006Z","iopub.execute_input":"2022-03-24T21:28:35.749308Z","iopub.status.idle":"2022-03-24T21:28:35.757193Z","shell.execute_reply.started":"2022-03-24T21:28:35.749277Z","shell.execute_reply":"2022-03-24T21:28:35.756132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../kaggle/temp/train","metadata":{"execution":{"iopub.status.busy":"2022-03-24T20:49:42.003814Z","iopub.execute_input":"2022-03-24T20:49:42.004396Z","iopub.status.idle":"2022-03-24T20:49:43.366093Z","shell.execute_reply.started":"2022-03-24T20:49:42.004324Z","shell.execute_reply":"2022-03-24T20:49:43.364648Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels=pd.read_csv('../input/cifar-10/trainLabels.csv')\nlabels=train_labels['label'].unique()\nlabels","metadata":{"execution":{"iopub.status.busy":"2022-03-24T20:51:06.072989Z","iopub.execute_input":"2022-03-24T20:51:06.073606Z","iopub.status.idle":"2022-03-24T20:51:06.145761Z","shell.execute_reply.started":"2022-03-24T20:51:06.073556Z","shell.execute_reply":"2022-03-24T20:51:06.144892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_p='../kaggle/temp/train'\ntest_p='../kaggle/temp/test'\nvalidation_p='../kaggle/temp/validation'\nos.mkdir(validation_p)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T20:54:00.596964Z","iopub.execute_input":"2022-03-24T20:54:00.597309Z","iopub.status.idle":"2022-03-24T20:54:00.603095Z","shell.execute_reply.started":"2022-03-24T20:54:00.597263Z","shell.execute_reply":"2022-03-24T20:54:00.602079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for label in labels:\n    temp_train=os.path.join(train_p,label)\n    if not os.path.exists(temp_train):\n        os.mkdir(temp_train)\n    temp_validation=os.path.join(validation_p,label)\n    if not os.path.exists(temp_validation):\n        os.mkdir(temp_validation)\nfor ind,row in train_labels.iterrows():\n    idno=str(row[\"id\"])+\".png\"\n    from_path=os.path.join(train_p,idno)\n    prob=np.random.random()\n    if(prob<=0.8):\n        to_path=os.path.join(train_p,row[\"label\"],idno)\n        os.replace(from_path,to_path)\n    else:\n        to_path=os.path.join(validation_p,row[\"label\"],idno)\n        os.replace(from_path,to_path)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T20:54:11.853643Z","iopub.execute_input":"2022-03-24T20:54:11.853952Z","iopub.status.idle":"2022-03-24T20:54:19.239251Z","shell.execute_reply.started":"2022-03-24T20:54:11.85391Z","shell.execute_reply":"2022-03-24T20:54:19.238113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model\ncnn=Sequential()\ncnn.add(Conv2D(filters=16, kernel_size=(3,3), activation=None, use_bias=False, input_shape=(32,32,3)))\ncnn.add(BatchNormalization())\ncnn.add(Activation('relu'))\ncnn.add(MaxPool2D(pool_size=(2,2)))\n\ncnn.add(Conv2D(filters=32, kernel_size=(3,3), activation=None, use_bias=False))\ncnn.add(BatchNormalization())\ncnn.add(Activation('relu'))\n\n\ncnn.add(Conv2D(filters=64, kernel_size=(3,3), activation=None, use_bias=False))\ncnn.add(BatchNormalization())\ncnn.add(Activation('relu'))\ncnn.add(MaxPool2D(pool_size=(2,2)))\n\ncnn.add(Conv2D(filters=128, kernel_size=(3,3), activation=None, use_bias=False))\ncnn.add(BatchNormalization())\ncnn.add(Activation('relu'))\n\ncnn.add(Flatten())\n\ncnn.add(Dense(units=10))\n\ncnn.add(BatchNormalization())\ncnn.add(Activation('softmax'))\n\ncnn.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-24T20:52:35.014393Z","iopub.execute_input":"2022-03-24T20:52:35.01471Z","iopub.status.idle":"2022-03-24T20:52:38.240896Z","shell.execute_reply.started":"2022-03-24T20:52:35.014676Z","shell.execute_reply":"2022-03-24T20:52:38.239863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train=ImageDataGenerator(featurewise_center=False,\n                             samplewise_center=False,\n                             featurewise_std_normalization=False,\n                             samplewise_std_normalization=False,\n                             zca_whitening=False,\n                             rotation_range=10,\n                             zoom_range=0.1,\n                             width_shift_range=0.1,\n                             height_shift_range=0.1,\n                             horizontal_flip=False,\n                             vertical_flip=False,\n                             rescale=1./255 )\ndata_train=data_train.flow_from_directory('../kaggle/temp/train',batch_size=120,target_size=(32,32))\ndata_validation=ImageDataGenerator(rescale=1./255)\ndata_validation=data_validation.flow_from_directory('../kaggle/temp/validation',batch_size=120,target_size=(32,32))","metadata":{"execution":{"iopub.status.busy":"2022-03-24T20:54:41.998143Z","iopub.execute_input":"2022-03-24T20:54:41.998435Z","iopub.status.idle":"2022-03-24T20:54:46.129825Z","shell.execute_reply.started":"2022-03-24T20:54:41.998404Z","shell.execute_reply":"2022-03-24T20:54:46.128806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.fit(data_train,validation_data=data_validation,epochs=50)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T21:11:07.983974Z","iopub.execute_input":"2022-03-24T21:11:07.984253Z","iopub.status.idle":"2022-03-24T21:11:54.256717Z","shell.execute_reply.started":"2022-03-24T21:11:07.984222Z","shell.execute_reply":"2022-03-24T21:11:54.255636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../kaggle/temp/test","metadata":{"execution":{"iopub.status.busy":"2022-03-24T21:32:24.881859Z","iopub.execute_input":"2022-03-24T21:32:24.882171Z","iopub.status.idle":"2022-03-24T21:32:25.694227Z","shell.execute_reply.started":"2022-03-24T21:32:24.882138Z","shell.execute_reply":"2022-03-24T21:32:25.693102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test=ImageDataGenerator(rescale=1./255)\ndata_test=data_test.flow_from_directory(directory='../kaggle/temp/test/',target_size=(32,32),batch_size=120,class_mode=None,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T21:26:02.939774Z","iopub.execute_input":"2022-03-24T21:26:02.940606Z","iopub.status.idle":"2022-03-24T21:26:05.436056Z","shell.execute_reply.started":"2022-03-24T21:26:02.940573Z","shell.execute_reply":"2022-03-24T21:26:05.434365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test.reset()\nx=cnn.predict(data_test)\nx=np.argmax(x,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T21:25:35.492432Z","iopub.execute_input":"2022-03-24T21:25:35.492723Z","iopub.status.idle":"2022-03-24T21:25:35.526317Z","shell.execute_reply.started":"2022-03-24T21:25:35.492692Z","shell.execute_reply":"2022-03-24T21:25:35.524947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-03-24T21:12:32.543891Z","iopub.execute_input":"2022-03-24T21:12:32.544281Z","iopub.status.idle":"2022-03-24T21:12:32.580774Z","shell.execute_reply.started":"2022-03-24T21:12:32.544237Z","shell.execute_reply":"2022-03-24T21:12:32.578304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fin=[]\nfor key in sorted(final_result):\n    fin.append([key,final_result[key]])","metadata":{"jupyter":{"source_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=pd.read_csv('../input/cifar-10/sampleSubmission.csv',header='infer')\nsub['label']=x\nsub.to_csv('submission.csv',index=False)","metadata":{},"execution_count":null,"outputs":[]}]}