{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nos.listdir(\"../input/cifar-10\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\ntrainLabels = pd.read_csv(\"../input/cifar-10/trainLabels.csv\")\ntrainLabels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install py7zr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!python -m py7zr x ../input/cifar-10/train.7z #takes 10 oof minutes of CPU to decompress this file. Big OOF","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#!python -m py7zr x ../input/cifar-10/test.7z # No sir, I shan't decompress thou","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport glob\nimport numpy as np\nfrom PIL import Image\ny = pd.read_csv('../input/cifar-10/trainLabels.csv')\nfilelist = glob.glob(\"../working/train/*\")\nfilelist = sorted(filelist,key= lambda x:int(x.split(\"/\")[-1].split(\".\")[0]))\nX = np.array([np.array(Image.open(fname)) for fname in filelist])\nprint(f\"X.shape : {X.shape} y.shape : {y.shape}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# got X and y at this point, now some preprocessing\nimport tensorflow\ny = y['label'].values\n\nX = X.astype('float32')\n\nX = X/255.0\n\ntemp = y\ncategories = list(pd.CategoricalIndex(temp).categories)\nmapping = dict()\nfor i in range(len(categories)):\n    mapping[categories[i]] = i\nfor i in range(len(temp)):\n    temp[i] = mapping[temp[i]]\ny = tensorflow.keras.utils.to_categorical(temp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f\"X.shape : {X.shape} , y.shape : {y.shape}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#preprocessing finally completed, now creating a basic model with keras\n\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import *\n\n\nmodel = Sequential()\nmodel.add(Conv2D(24,kernel_size=(3,3),padding='same',activation='relu',input_shape=(32,32,3)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(24,kernel_size=(3,3),padding='same',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.4))\nmodel.add(Conv2D(64,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.4))\nmodel.add(Conv2D(128,kernel_size=4,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Flatten())\nmodel.add(Dropout(0.4))\nmodel.add(Dense(10,activation='softmax'))\n\nmodel.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])\n\nmodel.summary()\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import *\nmodelMP = Sequential()\nmodelMP.add(Conv2D(24,kernel_size=(3,3),padding='same',activation='relu',input_shape=(32,32,3)))\nmodelMP.add(BatchNormalization())\nmodelMP.add(Conv2D(24,kernel_size=(3,3),padding='same',activation='relu'))\nmodelMP.add(BatchNormalization())\nmodelMP.add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))\nmodelMP.add(BatchNormalization())\nmodelMP.add(Dropout(0.4))\nmodelMP.add(Conv2D(64,(3,3),activation='relu'))\nmodelMP.add(BatchNormalization())\nmodelMP.add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))\nmodelMP.add(BatchNormalization())\nmodelMP.add(Dropout(0.4))\nmodelMP.add(Conv2D(128,kernel_size=4,activation='relu'))\nmodelMP.add(BatchNormalization())\nmodelMP.add(Conv2D(256,kernel_size=3,activation='relu'))\nmodelMP.add(BatchNormalization())\nmodelMP.add(Conv2D(512,kernel_size=2,activation='relu'))\nmodelMP.add(BatchNormalization())\nmodelMP.add(Flatten())\nmodelMP.add(Dropout(0.4))\nmodelMP.add(Dense(10,activation='softmax'))\nmodelMP.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"modelMP.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# data augmentation\n# starting Data Augmentation now\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\ndatagen = ImageDataGenerator(\n            rotation_range=10,\n            zoom_range=0.1,\n            width_shift_range=0.1,\n            height_shift_range=0.1\n            )\ndatagen.fit(X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# for validation testing, base model : with 10+40 epochs reached 84% accuracy on the private leaderboard\nfrom sklearn.model_selection import train_test_split\n\nX_train,X_val,y_train,y_val = train_test_split(X,y,test_size=0.2,random_state=1)\nmodel.fit(datagen.flow(X_train,y_train),batch_size=128,epochs=10,verbose=1,validation_data=(X_val,y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# for validation testing\nfrom sklearn.model_selection import train_test_split\n\nX_train,X_val,y_train,y_val = train_test_split(X,y,test_size=0.2,random_state=1)\nmodelMP.fit(datagen.flow(X_train,y_train),batch_size=128,epochs=10,verbose=1,validation_data=(X_val,y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# for actual\nmodelMP.fit(datagen.flow(X,y),batch_size=128,epochs=40,verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# decompressing the testing data\n!python -m py7zr x ../input/cifar-10/test.7z # No sir, I shan't decompress thou","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# taking input\ntestfilelist = glob.glob(\"../working/test/*\")\ntestfilelist = sorted(testfilelist,key= lambda x:int(x.split(\"/\")[-1].split(\".\")[0]))\nX_test = np.array([np.array(Image.open(fname)) for fname in testfilelist])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# preprocessing the testing dataset\nX_test = X_test.astype('float32')\nX_test = X_test/255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# reverse mapping of labels vs indices\nrevMapping = {}\nfor x in mapping.keys():\n    revMapping[mapping[x]] =  x\nrevMapping","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# delete everything that is of no use now\ndel X\ndel y\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# predicting on the testing dataset\nresults = modelMP.predict(X_test)\n# delete X_test now, we only need the results now\ndel X_test\nresults = results.argmax(axis=1)\nans = []\nfor i in range(len(results)):\n    ans.append(revMapping[results[i]])\nans","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,len(ans)+1),name='id').astype('int32'),pd.Series(ans,name='label')],axis=1)\nsubmission.to_csv('submission4.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# testing\n\nimport numpy as np\nrevMapping = {0: 'airplane',\n 1: 'automobile',\n 2: 'bird',\n 3: 'cat',\n 4: 'deer',\n 5: 'dog',\n 6: 'frog',\n 7: 'horse',\n 8: 'ship',\n 9: 'truck'}\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"arr = np.zeros(10)\nfor i in range(10):\n    arr[i] = i \narr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"charray = np.array([revMapping[x] for x in arr])\ncharray","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"int(\"21\")==float(21)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"2**++-3-3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"2+++++3\n2++++++3\n2+++++++3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nos.chdir(r'../working')\nfrom IPython.display import FileLink\nFileLink(r'submission4.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}