{"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":{"id":"xVT7zON96Bg-","execution":{"iopub.status.busy":"2022-05-05T17:58:48.393203Z","iopub.execute_input":"2022-05-05T17:58:48.393513Z","iopub.status.idle":"2022-05-05T17:58:48.406398Z","shell.execute_reply.started":"2022-05-05T17:58:48.393468Z","shell.execute_reply":"2022-05-05T17:58:48.404919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:48.420234Z","iopub.execute_input":"2022-05-05T17:58:48.420453Z","iopub.status.idle":"2022-05-05T17:58:57.182528Z","shell.execute_reply.started":"2022-05-05T17:58:48.420425Z","shell.execute_reply":"2022-05-05T17:58:57.181311Z"},"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-05T17:58:57.185553Z","iopub.execute_input":"2022-05-05T17:58:57.185941Z","iopub.status.idle":"2022-05-05T17:58:57.227388Z","shell.execute_reply.started":"2022-05-05T17:58:57.185849Z","shell.execute_reply":"2022-05-05T17:58:57.224046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#unpack train images to temp directory\nshutil.unpack_archive('/kaggle/input/cifar-10/train.7z','/kaggle/temp/')","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.228293Z","iopub.status.idle":"2022-05-05T17:58:57.228647Z","shell.execute_reply.started":"2022-05-05T17:58:57.228452Z","shell.execute_reply":"2022-05-05T17:58:57.228479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv', header= 'infer')\n\n#unique labels\nclasses = train_labels['label'].unique()\n\nprint(classes)\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\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        \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.80:\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)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.230082Z","iopub.status.idle":"2022-05-05T17:58:57.230903Z","shell.execute_reply.started":"2022-05-05T17:58:57.230596Z","shell.execute_reply":"2022-05-05T17:58:57.230629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport tensorflow\nfrom tensorflow import keras\nfrom keras.datasets import cifar10\nfrom keras.utils import np_utils\nfrom keras.applications.xception import Xception,preprocess_input\nfrom keras.layers import Dense,MaxPool2D,Conv2D,Dropout,Flatten,GlobalAveragePooling2D\nfrom keras.models import Sequential\nimport numpy as np\nimport cv2 \nfrom keras.utils.np_utils import to_categorical\nfrom keras.callbacks import ReduceLROnPlateau\n\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.233858Z","iopub.status.idle":"2022-05-05T17:58:57.234592Z","shell.execute_reply.started":"2022-05-05T17:58:57.234288Z","shell.execute_reply":"2022-05-05T17:58:57.234321Z"},"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                                  )\n\ntrain_gen = train_datagen.flow_from_directory(directory='/kaggle/temp/train/',target_size=(32,32),batch_size=128)\nvalidation_datagen = ImageDataGenerator(rescale=1./255)\nvalid_gen = validation_datagen.flow_from_directory(directory='/kaggle/temp/validation/',target_size=(32,32),batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.236054Z","iopub.status.idle":"2022-05-05T17:58:57.236975Z","shell.execute_reply.started":"2022-05-05T17:58:57.236592Z","shell.execute_reply":"2022-05-05T17:58:57.236623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# (X_train, y_train), (X_test, y_test) = cifar10.load_data()\n\n# print(X_train.shape)\n# print(X_test.shape)\n# print(y_train.shape)\n# print(y_test.shape)","metadata":{"id":"KgwVZ3Bz6Iin","outputId":"dc9a9f8b-2eb3-4bfd-fd5a-fffc1dfc1be5","execution":{"iopub.status.busy":"2022-05-05T17:58:57.238583Z","iopub.status.idle":"2022-05-05T17:58:57.239345Z","shell.execute_reply.started":"2022-05-05T17:58:57.239022Z","shell.execute_reply":"2022-05-05T17:58:57.239057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_train = np.array([cv2.resize(X_train[i], dsize=(72, 72), interpolation=cv2.INTER_CUBIC) for i in range(0, len(X_train))]).astype('float16')\n# X_test = np.array([cv2.resize(X_test[i], dsize=(72, 72), interpolation=cv2.INTER_CUBIC) for i in range(0, len(X_test))]).astype('float16')","metadata":{"id":"ZwAy3cTjA3X_","execution":{"iopub.status.busy":"2022-05-05T17:58:57.240912Z","iopub.status.idle":"2022-05-05T17:58:57.241653Z","shell.execute_reply.started":"2022-05-05T17:58:57.24135Z","shell.execute_reply":"2022-05-05T17:58:57.241384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def preprocess_data(X, Y):\n#     X = preprocess_input(X)\n#     Y = np_utils.to_categorical(Y)\n#     return X, Y","metadata":{"id":"qWXyIQmw6XWK","execution":{"iopub.status.busy":"2022-05-05T17:58:57.24333Z","iopub.status.idle":"2022-05-05T17:58:57.244103Z","shell.execute_reply.started":"2022-05-05T17:58:57.243748Z","shell.execute_reply":"2022-05-05T17:58:57.243799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_target = y_test\n\n# x_train, y_train = preprocess_data(X_train, y_train)\n# x_test, y_test = preprocess_data(X_test, y_test)","metadata":{"id":"nMdJijop6iBB","execution":{"iopub.status.busy":"2022-05-05T17:58:57.245593Z","iopub.status.idle":"2022-05-05T17:58:57.246328Z","shell.execute_reply.started":"2022-05-05T17:58:57.246012Z","shell.execute_reply":"2022-05-05T17:58:57.246043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow\n\ninitializer = keras.initializers.he_normal()\ninput_tensor = keras.Input(shape=(32, 32, 3))\n\nresized_images = keras.layers.Lambda(lambda image: tensorflow.image.resize(image, (72, 72)))(input_tensor)\nmodel = Xception(include_top=False,weights='imagenet',input_tensor=resized_images,input_shape=(72, 72, 3),pooling='max')\n\nfor layer in model.layers:\n    layer.trainable = False\noutput = model.layers[-1].output\n\nflatten = Flatten()\noutput = flatten(output)\nlayer_256 = Dense(units=256,activation='relu',kernel_initializer=initializer,kernel_regularizer=keras.regularizers.l2())\noutput = layer_256(output)\n\ndropout = Dropout(0.3)\noutput = dropout(output)\n\nsoftmax = Dense(units=10,activation='softmax',kernel_initializer=initializer,kernel_regularizer=keras.regularizers.l2())\noutput = softmax(output)\n\nmodel = keras.models.Model(inputs=input_tensor, outputs=output)","metadata":{"id":"2UEKgIiW6xT8","execution":{"iopub.status.busy":"2022-05-05T17:58:57.24787Z","iopub.status.idle":"2022-05-05T17:58:57.248637Z","shell.execute_reply.started":"2022-05-05T17:58:57.248285Z","shell.execute_reply":"2022-05-05T17:58:57.248331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.250282Z","iopub.status.idle":"2022-05-05T17:58:57.250999Z","shell.execute_reply.started":"2022-05-05T17:58:57.250694Z","shell.execute_reply":"2022-05-05T17:58:57.250727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])\n","metadata":{"id":"TX23gskR7-_I","outputId":"b6fa5157-ff26-4642-e88b-689d9611bd18","execution":{"iopub.status.busy":"2022-05-05T17:58:57.252513Z","iopub.status.idle":"2022-05-05T17:58:57.25326Z","shell.execute_reply.started":"2022-05-05T17:58:57.252942Z","shell.execute_reply":"2022-05-05T17:58:57.252975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_gen,epochs=50,validation_data=valid_gen,steps_per_epoch=train_gen.n//train_gen.batch_size,\n         validation_steps= valid_gen.n//valid_gen.batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.254612Z","iopub.status.idle":"2022-05-05T17:58:57.255334Z","shell.execute_reply.started":"2022-05-05T17:58:57.255031Z","shell.execute_reply":"2022-05-05T17:58:57.255063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_datagen = keras.preprocessing.image.ImageDataGenerator(horizontal_flip=True)\n# train_generator = train_datagen.flow(x_train,y_train,batch_size=32)\n\n# val_datagen = keras.preprocessing.image.ImageDataGenerator(horizontal_flip=True)\n# val_generator = val_datagen.flow(x_test,y_test,batch_size=32)\n\n# train_steps_per_epoch = x_train.shape[0] // 32\n# val_steps_per_epoch = x_test.shape[0] // 32\n\n# model.fit(train_generator,steps_per_epoch=train_steps_per_epoch,validation_data=val_generator,validation_steps=val_steps_per_epoch,epochs=20,shuffle=True,verbose=1)","metadata":{"id":"--B53HFe9KdR","outputId":"0efe4422-3bcd-4c1d-80b1-9467441271a1","execution":{"iopub.status.busy":"2022-05-05T17:58:57.25673Z","iopub.status.idle":"2022-05-05T17:58:57.257507Z","shell.execute_reply.started":"2022-05-05T17:58:57.257195Z","shell.execute_reply":"2022-05-05T17:58:57.257225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try :\n    shutil.register_unpack_format('7zip',['.7z'],unpack_7zarchive)\nexcept Exception as e :\n    print(e)\nshutil.unpack_archive('/kaggle/input/cifar-10/test.7z','/kaggle/temp/test')","metadata":{"id":"YIXt2UnwUzZm","outputId":"96a34135-07c8-4a7f-d14a-a29dce64bcad","execution":{"iopub.status.busy":"2022-05-05T17:58:57.259015Z","iopub.status.idle":"2022-05-05T17:58:57.259808Z","shell.execute_reply.started":"2022-05-05T17:58:57.259461Z","shell.execute_reply":"2022-05-05T17:58:57.259493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unregister_unpack_format('7zip')","metadata":{"id":"LcdSZUurVgx2","outputId":"f567faf9-b5b2-4646-fce5-cbc17699d84f","execution":{"iopub.status.busy":"2022-05-05T17:58:57.261419Z","iopub.status.idle":"2022-05-05T17:58:57.262254Z","shell.execute_reply.started":"2022-05-05T17:58:57.261922Z","shell.execute_reply":"2022-05-05T17:58:57.261957Z"},"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=(32,32),batch_size=64,class_mode=None,shuffle=False)","metadata":{"id":"rFEdviFm-V4e","outputId":"b6d27095-c347-4370-e4b2-a5813cbeb82d","execution":{"iopub.status.busy":"2022-05-05T17:58:57.263694Z","iopub.status.idle":"2022-05-05T17:58:57.264409Z","shell.execute_reply.started":"2022-05-05T17:58:57.264127Z","shell.execute_reply":"2022-05-05T17:58:57.264159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nos.chdir('/kaggle/temp/test/test')\nprint(os.getcwd())","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.26582Z","iopub.status.idle":"2022-05-05T17:58:57.266529Z","shell.execute_reply.started":"2022-05-05T17:58:57.266235Z","shell.execute_reply":"2022-05-05T17:58:57.266282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen.filenames","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.268251Z","iopub.status.idle":"2022-05-05T17:58:57.269072Z","shell.execute_reply.started":"2022-05-05T17:58:57.268695Z","shell.execute_reply":"2022-05-05T17:58:57.268744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_final = model.predict(test_gen)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.270722Z","iopub.status.idle":"2022-05-05T17:58:57.271577Z","shell.execute_reply.started":"2022-05-05T17:58:57.271273Z","shell.execute_reply":"2022-05-05T17:58:57.271334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_final[1]","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.273063Z","iopub.status.idle":"2022-05-05T17:58:57.273807Z","shell.execute_reply.started":"2022-05-05T17:58:57.2735Z","shell.execute_reply":"2022-05-05T17:58:57.273532Z"},"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[np.argmax(i)]\n    ind=ind+1","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.275312Z","iopub.status.idle":"2022-05-05T17:58:57.276062Z","shell.execute_reply.started":"2022-05-05T17:58:57.275771Z","shell.execute_reply":"2022-05-05T17:58:57.275801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes[9]","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.277404Z","iopub.status.idle":"2022-05-05T17:58:57.278205Z","shell.execute_reply.started":"2022-05-05T17:58:57.277823Z","shell.execute_reply":"2022-05-05T17:58:57.277855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames_wo_ext = []\nfor fname in test_gen.filenames:\n    filenames_wo_ext.append(int(fname.split(sep=\"/\")[-1].split(sep=\".\")[0])-1)\n\n# print(filenames_wo_ext)\n# print(len(predicted_classes))\n\npredicted_classes_final = np.empty(shape=300000,dtype=np.dtype('U20'))\n# for i in range(0,300000) :\n#     predicted_classes_final[i]= predicted_classes[i]\npredicted_classes_final[filenames_wo_ext]=predicted_classes","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.279649Z","iopub.status.idle":"2022-05-05T17:58:57.280409Z","shell.execute_reply.started":"2022-05-05T17:58:57.280062Z","shell.execute_reply":"2022-05-05T17:58:57.280121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes_final","metadata":{"execution":{"iopub.status.busy":"2022-05-05T17:58:57.281965Z","iopub.status.idle":"2022-05-05T17:58:57.282694Z","shell.execute_reply.started":"2022-05-05T17:58:57.282399Z","shell.execute_reply":"2022-05-05T17:58:57.282434Z"},"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":"2022-05-05T17:58:57.28437Z","iopub.status.idle":"2022-05-05T17:58:57.285089Z","shell.execute_reply.started":"2022-05-05T17:58:57.284793Z","shell.execute_reply":"2022-05-05T17:58:57.28483Z"},"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":"2022-05-05T17:58:57.286509Z","iopub.status.idle":"2022-05-05T17:58:57.287246Z","shell.execute_reply.started":"2022-05-05T17:58:57.286925Z","shell.execute_reply":"2022-05-05T17:58:57.286961Z"},"trusted":true},"execution_count":null,"outputs":[]}]}