{"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":{"execution":{"iopub.status.busy":"2023-03-20T06:42:41.437786Z","iopub.execute_input":"2023-03-20T06:42:41.438237Z","iopub.status.idle":"2023-03-20T06:42:41.446569Z","shell.execute_reply.started":"2023-03-20T06:42:41.438187Z","shell.execute_reply":"2023-03-20T06:42:41.445860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd ","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2023-03-20T06:42:41.448455Z","iopub.execute_input":"2023-03-20T06:42:41.448958Z","iopub.status.idle":"2023-03-20T06:42:41.458155Z","shell.execute_reply.started":"2023-03-20T06:42:41.448915Z","shell.execute_reply":"2023-03-20T06:42:41.457268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:42:41.459528Z","iopub.execute_input":"2023-03-20T06:42:41.459977Z","iopub.status.idle":"2023-03-20T06:42:53.547977Z","shell.execute_reply.started":"2023-03-20T06:42:41.459943Z","shell.execute_reply":"2023-03-20T06:42:53.547076Z"},"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":"2023-03-20T06:42:53.550504Z","iopub.execute_input":"2023-03-20T06:42:53.550786Z","iopub.status.idle":"2023-03-20T06:42:53.801722Z","shell.execute_reply.started":"2023-03-20T06:42:53.550751Z","shell.execute_reply":"2023-03-20T06:42:53.800978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('/kaggle/input/cifar-10/train.7z', '/kaggle/temp/')","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:42:53.803086Z","iopub.execute_input":"2023-03-20T06:42:53.803397Z","iopub.status.idle":"2023-03-20T06:43:39.881581Z","shell.execute_reply.started":"2023-03-20T06:42:53.803351Z","shell.execute_reply":"2023-03-20T06:43:39.880698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels=pd.read_csv(\"/kaggle/input/cifar-10/trainLabels.csv\", header='infer')\n\nclasses=train_labels['label'].unique()\n\nimport os\n\nif not os.path.exists(\"/kaggle/temp/validation/\"):\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/\"\n\nfor class1 in classes:\n    path_train=os.path.join(parent_path_train,class1)\n    if not os.path.exists(path_train):\n        os.mkdir(path_train)\n    path_validation=os.path.join(parent_path_validation,class1)\n    if not os.path.exists(path_validation):\n        os.mkdir(path_validation)\n\nfor (int_ind,row) in train_labels.iterrows():\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":"2023-03-20T06:43:39.883077Z","iopub.execute_input":"2023-03-20T06:43:39.883569Z","iopub.status.idle":"2023-03-20T06:43:45.778602Z","shell.execute_reply.started":"2023-03-20T06:43:39.883529Z","shell.execute_reply":"2023-03-20T06:43:45.777843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp/train'),os.listdir('/kaggle/temp/validation'),len(os.listdir('/kaggle/temp/train/horse'))","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:43:45.779988Z","iopub.execute_input":"2023-03-20T06:43:45.780240Z","iopub.status.idle":"2023-03-20T06:43:45.807472Z","shell.execute_reply.started":"2023-03-20T06:43:45.780208Z","shell.execute_reply":"2023-03-20T06:43:45.806824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\n\nfrom tensorflow.keras.applications.xception import Xception, preprocess_input\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Activation, BatchNormalization, GlobalAveragePooling2D,Conv2D,Flatten, Dropout, LeakyReLU\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:43:45.809465Z","iopub.execute_input":"2023-03-20T06:43:45.810026Z","iopub.status.idle":"2023-03-20T06:43:51.817949Z","shell.execute_reply.started":"2023-03-20T06:43:45.809988Z","shell.execute_reply":"2023-03-20T06:43:51.817096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduceLROnPlateau = ReduceLROnPlateau(monitor='val_acc', \n                                patience=3,\n                                verbose=1, \n                                factor=0.5,\n                                min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:43:51.820742Z","iopub.execute_input":"2023-03-20T06:43:51.821064Z","iopub.status.idle":"2023-03-20T06:43:51.829642Z","shell.execute_reply.started":"2023-03-20T06:43:51.821026Z","shell.execute_reply":"2023-03-20T06:43:51.826942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=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\n                        )\ntrain_gen=train_datagen.flow_from_directory(directory='/kaggle/temp/train/', target_size=(72,72), batch_size=32) \n\nvalidation_datagen=ImageDataGenerator(rescale=1./255)\n\nvalid_gen=validation_datagen.flow_from_directory(directory='/kaggle/temp/validation/', target_size=(72,72), batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:43:51.831391Z","iopub.execute_input":"2023-03-20T06:43:51.831780Z","iopub.status.idle":"2023-03-20T06:43:52.960296Z","shell.execute_reply.started":"2023-03-20T06:43:51.831743Z","shell.execute_reply":"2023-03-20T06:43:52.959604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.xception import Xception, preprocess_input\n#train_gen = preprocess_input(train_gen)","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:43:52.961368Z","iopub.execute_input":"2023-03-20T06:43:52.961600Z","iopub.status.idle":"2023-03-20T06:43:52.966549Z","shell.execute_reply.started":"2023-03-20T06:43:52.961567Z","shell.execute_reply":"2023-03-20T06:43:52.965643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_pretrained_wo_top=Xception(include_top=False, weights='imagenet', input_tensor=None, input_shape=(72,72,3), pooling=None)\n\n# x_test=model_pretrained_wo_top.predict(train_gen)\n# x_test","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:43:52.967725Z","iopub.execute_input":"2023-03-20T06:43:52.968291Z","iopub.status.idle":"2023-03-20T06:43:58.731584Z","shell.execute_reply.started":"2023-03-20T06:43:52.968240Z","shell.execute_reply":"2023-03-20T06:43:58.730854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(x_test.shape)\n# y_train = train_gen.labels\n# y_train = to_categorical(y_train)\n# y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:43:58.733041Z","iopub.execute_input":"2023-03-20T06:43:58.733297Z","iopub.status.idle":"2023-03-20T06:43:58.736963Z","shell.execute_reply.started":"2023-03-20T06:43:58.733262Z","shell.execute_reply":"2023-03-20T06:43:58.736259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(model_pretrained_wo_top)\nmodel.add(Flatten())\nmodel.add(Dense(512))\nmodel.add(LeakyReLU())\nmodel.add(Dropout(0.3))\nmodel.add(Dense(10, activation='softmax'))\n#model.summary()\n\nmodel.compile(loss='categorical_crossentropy', optimizer='Nadam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:43:58.738254Z","iopub.execute_input":"2023-03-20T06:43:58.738716Z","iopub.status.idle":"2023-03-20T06:43:59.108047Z","shell.execute_reply.started":"2023-03-20T06:43:58.738675Z","shell.execute_reply":"2023-03-20T06:43:59.107192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:43:59.109324Z","iopub.execute_input":"2023-03-20T06:43:59.109567Z","iopub.status.idle":"2023-03-20T06:43:59.126842Z","shell.execute_reply.started":"2023-03-20T06:43:59.109532Z","shell.execute_reply":"2023-03-20T06:43:59.125635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_gen, validation_data=valid_gen,callbacks=[reduceLROnPlateau], batch_size=32, epochs=20,steps_per_epoch = train_gen.n//train_gen.batch_size,\n                    validation_steps = valid_gen.n//valid_gen.batch_size)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-20T06:43:59.128344Z","iopub.execute_input":"2023-03-20T06:43:59.128702Z","iopub.status.idle":"2023-03-20T07:48:52.786467Z","shell.execute_reply.started":"2023-03-20T06:43:59.128660Z","shell.execute_reply":"2023-03-20T07:48:52.785586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('/kaggle/input/cifar-10/test.7z', '/kaggle/temp/test')","metadata":{"execution":{"iopub.status.busy":"2023-03-20T07:48:52.788138Z","iopub.execute_input":"2023-03-20T07:48:52.788415Z","iopub.status.idle":"2023-03-20T08:02:43.210153Z","shell.execute_reply.started":"2023-03-20T07:48:52.788380Z","shell.execute_reply":"2023-03-20T08:02:43.209285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen=ImageDataGenerator(rescale=1./255)\n\ntest_gen=test_datagen.flow_from_directory(directory='/kaggle/temp/test', target_size=(72,72), batch_size=32, class_mode=None, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-20T08:02:43.211628Z","iopub.execute_input":"2023-03-20T08:02:43.211900Z","iopub.status.idle":"2023-03-20T08:02:48.786303Z","shell.execute_reply.started":"2023-03-20T08:02:43.211865Z","shell.execute_reply":"2023-03-20T08:02:48.785577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(test_gen)","metadata":{"execution":{"iopub.status.busy":"2023-03-20T08:03:41.844708Z","iopub.execute_input":"2023-03-20T08:03:41.845499Z","iopub.status.idle":"2023-03-20T08:04:08.190704Z","shell.execute_reply.started":"2023-03-20T08:03:41.845460Z","shell.execute_reply":"2023-03-20T08:04:08.189538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen.reset()\nprediction_vectors3=model.predict(test_gen)\npredictions_final=np.argmax(prediction_vectors3, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-20T08:04:12.730852Z","iopub.execute_input":"2023-03-20T08:04:12.731146Z","iopub.status.idle":"2023-03-20T08:04:32.306174Z","shell.execute_reply.started":"2023-03-20T08:04:12.731113Z","shell.execute_reply":"2023-03-20T08:04:32.304288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_final","metadata":{"execution":{"iopub.status.busy":"2023-03-20T08:03:37.296162Z","iopub.status.idle":"2023-03-20T08:03:37.296748Z","shell.execute_reply.started":"2023-03-20T08:03:37.296503Z","shell.execute_reply":"2023-03-20T08:03:37.296531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for f in test_gen.filenames:\n    print(f[5:])\n    break","metadata":{"execution":{"iopub.status.busy":"2023-03-20T08:03:37.297926Z","iopub.status.idle":"2023-03-20T08:03:37.298503Z","shell.execute_reply.started":"2023-03-20T08:03:37.298261Z","shell.execute_reply":"2023-03-20T08:03:37.298288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(train_gen.class_indices))\nprint(train_gen.class_indices)\n\nclasses = {value:key for (key,value) in train_gen.class_indices.items()}\nprint(classes)\n\npredicted_classes=np.empty(shape=300000,dtype=np.dtype('U20'))\n\nind=0\nfor i in predictions_final.tolist():\n    predicted_classes[ind]=classes[i]\n    ind=ind+1\n    \nfilenames_wo_ext = []\nfor fname in test_gen.filenames:\n    filenames_wo_ext.append(int(fname[5:].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":"2023-03-20T08:03:37.299646Z","iopub.status.idle":"2023-03-20T08:03:37.300236Z","shell.execute_reply.started":"2023-03-20T08:03:37.299990Z","shell.execute_reply":"2023-03-20T08:03:37.300027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/cifar-10/sampleSubmission.csv',header='infer')\nsub.info()","metadata":{"execution":{"iopub.status.busy":"2023-03-20T08:03:37.301352Z","iopub.status.idle":"2023-03-20T08:03:37.301928Z","shell.execute_reply.started":"2023-03-20T08:03:37.301670Z","shell.execute_reply":"2023-03-20T08:03:37.301699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['label'] = predicted_classes_final\nsub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-20T08:03:37.303056Z","iopub.status.idle":"2023-03-20T08:03:37.303641Z","shell.execute_reply.started":"2023-03-20T08:03:37.303397Z","shell.execute_reply":"2023-03-20T08:03:37.303423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes_final","metadata":{"execution":{"iopub.status.busy":"2023-03-20T08:03:37.304762Z","iopub.status.idle":"2023-03-20T08:03:37.305351Z","shell.execute_reply.started":"2023-03-20T08:03:37.305110Z","shell.execute_reply":"2023-03-20T08:03:37.305138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pwd","metadata":{"execution":{"iopub.status.busy":"2023-03-20T08:03:37.306451Z","iopub.status.idle":"2023-03-20T08:03:37.307040Z","shell.execute_reply.started":"2023-03-20T08:03:37.306783Z","shell.execute_reply":"2023-03-20T08:03:37.306826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cifar10_teacher.h5')","metadata":{"execution":{"iopub.status.busy":"2023-03-20T08:04:48.991597Z","iopub.execute_input":"2023-03-20T08:04:48.991873Z","iopub.status.idle":"2023-03-20T08:04:50.129515Z","shell.execute_reply.started":"2023-03-20T08:04:48.991842Z","shell.execute_reply":"2023-03-20T08:04:50.128748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}