{"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-05T10:40:41.680326Z","iopub.execute_input":"2022-05-05T10:40:41.680788Z","iopub.status.idle":"2022-05-05T10:40:41.726057Z","shell.execute_reply.started":"2022-05-05T10:40:41.680665Z","shell.execute_reply":"2022-05-05T10:40:41.724927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2022-05-05T10:40:41.728416Z","iopub.execute_input":"2022-05-05T10:40:41.729089Z","iopub.status.idle":"2022-05-05T10:41:08.909187Z","shell.execute_reply.started":"2022-05-05T10:40:41.729051Z","shell.execute_reply":"2022-05-05T10:41:08.908373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from py7zr import unpack_7zarchive\nimport shutil\n\nshutil.register_unpack_format('7zip',['.7z'], unpack_7zarchive)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T10:41:08.910838Z","iopub.execute_input":"2022-05-05T10:41:08.911110Z","iopub.status.idle":"2022-05-05T10:41:09.160428Z","shell.execute_reply.started":"2022-05-05T10:41:08.911074Z","shell.execute_reply":"2022-05-05T10:41:09.159747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('../input/cifar-10/train.7z','/kaggle/temp/')\nshutil.unpack_archive('../input/cifar-10/test.7z','/kaggle/temp/')","metadata":{"execution":{"iopub.status.busy":"2022-05-05T10:41:09.162226Z","iopub.execute_input":"2022-05-05T10:41:09.162463Z","iopub.status.idle":"2022-05-05T10:57:02.951328Z","shell.execute_reply.started":"2022-05-05T10:41:09.162432Z","shell.execute_reply":"2022-05-05T10:57:02.950601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv('../input/cifar-10/trainLabels.csv',header='infer')\nclasses = train_labels['label'].unique()\nprint(classes)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T10:57:02.952762Z","iopub.execute_input":"2022-05-05T10:57:02.953024Z","iopub.status.idle":"2022-05-05T10:57:03.038812Z","shell.execute_reply.started":"2022-05-05T10:57:02.952991Z","shell.execute_reply":"2022-05-05T10:57:03.038045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if os.path.exists('/kaggle/temp/validation/') == False:\n    os.mkdir('/kaggle/temp/validation/')\n\nparent_path_train='/kaggle/temp/train/'\nparent_path_validation='/kaggle/temp/validation/'\nparent_path_test='/kaggle/temp/test/'\nfor class1 in classes:\n    path_train=os.path.join(parent_path_train,class1)\n    if os.path.exists(path_train) == False:\n        os.mkdir(path_train)\n    path_validation=os.path.join(parent_path_validation,class1)\n    if os.path.exists(path_validation) == False:\n        os.mkdir(path_validation)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T10:57:03.043417Z","iopub.execute_input":"2022-05-05T10:57:03.053220Z","iopub.status.idle":"2022-05-05T10:57:03.498585Z","shell.execute_reply.started":"2022-05-05T10:57:03.053178Z","shell.execute_reply":"2022-05-05T10:57:03.497755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (int_ind,row) in train_labels.iterrows():\n    \n    id=str(row['id'])+'.png'\n    source_path=os.path.join(parent_path_train,id)\n    \n    p=np.random.random()\n    if p<= 0.8:\n        target_path=os.path.join(parent_path_train,row['label'],id)\n        os.replace(source_path,target_path)\n    else:\n        target_path=os.path.join(parent_path_validation,row['label'],id)\n        os.replace(source_path,target_path)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T10:57:03.500176Z","iopub.execute_input":"2022-05-05T10:57:03.500467Z","iopub.status.idle":"2022-05-05T10:57:08.033971Z","shell.execute_reply.started":"2022-05-05T10:57:03.500431Z","shell.execute_reply":"2022-05-05T10:57:08.033093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.xception import preprocess_input\nimport cv2\n\ntrain = ImageDataGenerator(rescale=1./255, preprocessing_function = preprocess_input)\ntrain_batch = train.flow_from_directory(parent_path_train, batch_size = 132, target_size=(72,72))\n\nvalidation = ImageDataGenerator(rescale=1./255, preprocessing_function = preprocess_input)\nval_batch = validation.flow_from_directory(parent_path_validation, batch_size = 132, target_size=(72,72))#, target_size = (299,299))\n\ntestgen = ImageDataGenerator(rescale = 1 / 255.0)\ntestflow = testgen.flow_from_directory('/kaggle/temp/', classes=['test'], batch_size = 132, target_size = (72, 72), shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T11:26:50.543574Z","iopub.execute_input":"2022-05-05T11:26:50.543842Z","iopub.status.idle":"2022-05-05T11:26:58.113763Z","shell.execute_reply.started":"2022-05-05T11:26:50.543814Z","shell.execute_reply":"2022-05-05T11:26:58.112986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import Sequential\nfrom  keras.applications.xception import Xception\nfrom keras.layers import Flatten, Dense\nxception = Xception(include_top=False,\n                weights = \"imagenet\",\n                input_tensor = None,\n                input_shape = (72,72,3),\n                pooling = None)\n\nmodel = Sequential()\nmodel.add(xception)\nmodel.add(Flatten())\n\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.compile(\n    loss = \"categorical_crossentropy\",\n    optimizer = \"adam\",\n    metrics = ['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T11:26:58.115232Z","iopub.execute_input":"2022-05-05T11:26:58.115574Z","iopub.status.idle":"2022-05-05T11:26:59.889759Z","shell.execute_reply.started":"2022-05-05T11:26:58.115534Z","shell.execute_reply":"2022-05-05T11:26:59.888975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_batch, epochs=20, validation_data=val_batch, steps_per_epoch = train_batch.n//train_batch.batch_size,\n                    validation_steps = val_batch.n//val_batch.batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T11:27:26.133047Z","iopub.execute_input":"2022-05-05T11:27:26.133783Z","iopub.status.idle":"2022-05-05T11:51:51.274929Z","shell.execute_reply.started":"2022-05-05T11:27:26.133743Z","shell.execute_reply":"2022-05-05T11:51:51.274234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = np.argmax(model.predict(testflow), axis = 1)\npred = list(pred)\nclasses = {value:key for (key, value) in train_batch.class_indices.items()}\npredc = np.empty(300000, 'U16')\nidd = 0\nfor i in pred:\n    predc[idd] = classes[i]\n    idd += 1","metadata":{"execution":{"iopub.status.busy":"2022-05-05T11:53:56.935383Z","iopub.execute_input":"2022-05-05T11:53:56.935667Z","iopub.status.idle":"2022-05-05T11:55:49.502055Z","shell.execute_reply.started":"2022-05-05T11:53:56.935635Z","shell.execute_reply":"2022-05-05T11:55:49.501311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = []\nfor file in testflow.filenames:\n    files.append(int(file.split('/')[1].split('.')[0]) - 1)\nfiles = np.array(files)\n    \nfinal = np.empty(300000, dtype = 'U16')\nfinal[files] = predc","metadata":{"execution":{"iopub.status.busy":"2022-05-05T11:57:03.015269Z","iopub.execute_input":"2022-05-05T11:57:03.015530Z","iopub.status.idle":"2022-05-05T11:57:03.305280Z","shell.execute_reply.started":"2022-05-05T11:57:03.015502Z","shell.execute_reply":"2022-05-05T11:57:03.304561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/cifar-10/sampleSubmission.csv')\nsub['label'] = final\nsub.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T11:57:05.651736Z","iopub.execute_input":"2022-05-05T11:57:05.652456Z","iopub.status.idle":"2022-05-05T11:57:06.268175Z","shell.execute_reply.started":"2022-05-05T11:57:05.652423Z","shell.execute_reply":"2022-05-05T11:57:06.267445Z"},"trusted":true},"execution_count":null,"outputs":[]}]}