{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\nimport py7zr","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\n\nwith py7zr.SevenZipFile('../input/cifar-10/train.7z', mode='r') as z:\n    z.extractall()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with py7zr.SevenZipFile('../input/cifar-10/test.7z', mode='r') as z:\n    z.extractall()","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:23:08.926061Z","iopub.execute_input":"2022-03-08T18:23:08.926323Z","iopub.status.idle":"2022-03-08T18:37:00.359332Z","shell.execute_reply.started":"2022-03-08T18:23:08.926293Z","shell.execute_reply":"2022-03-08T18:37:00.358579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimagesList = os.listdir('./train')\nimageIndex = range(1, 50001)\n\n\n# fetching image number\n# for i in range(len(imagesIndex)):\n#     imagesIndex[i] = int(imagesIndex[i][0:-4])","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:13.648503Z","iopub.execute_input":"2022-03-08T18:14:13.648788Z","iopub.status.idle":"2022-03-08T18:14:13.68072Z","shell.execute_reply.started":"2022-03-08T18:14:13.648749Z","shell.execute_reply":"2022-03-08T18:14:13.680011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# getting the labels\nlabels = pd.read_csv('../input/cifar-10/trainLabels.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:13.682123Z","iopub.execute_input":"2022-03-08T18:14:13.682397Z","iopub.status.idle":"2022-03-08T18:14:13.714894Z","shell.execute_reply.started":"2022-03-08T18:14:13.682361Z","shell.execute_reply":"2022-03-08T18:14:13.714171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# plotting image\nfrom PIL import Image\n# Import an image from directory:\ninput_image = Image.open(\"train/\" + str(imageIndex[2])+ \".png\")\ninput_image","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:13.716355Z","iopub.execute_input":"2022-03-08T18:14:13.716618Z","iopub.status.idle":"2022-03-08T18:14:13.735389Z","shell.execute_reply.started":"2022-03-08T18:14:13.716582Z","shell.execute_reply":"2022-03-08T18:14:13.734751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import image as IMAGE\ndef imageToPixel(imageName):\n    ig = IMAGE.imread(\"train/\" + imageName + \".png\")\n#   RGB Weights\n    return (ig)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:13.737217Z","iopub.execute_input":"2022-03-08T18:14:13.737645Z","iopub.status.idle":"2022-03-08T18:14:13.742354Z","shell.execute_reply.started":"2022-03-08T18:14:13.737606Z","shell.execute_reply":"2022-03-08T18:14:13.741682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt\ndef plotImage(img):\n    plt.imshow(img, cmap=plt.get_cmap('gray'))","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:13.744012Z","iopub.execute_input":"2022-03-08T18:14:13.744368Z","iopub.status.idle":"2022-03-08T18:14:13.751362Z","shell.execute_reply.started":"2022-03-08T18:14:13.744333Z","shell.execute_reply":"2022-03-08T18:14:13.750617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = imageToPixel(\"3\")\nprint(image.shape)\nplotImage(image)\nprint(labels['label'][2])","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:13.752452Z","iopub.execute_input":"2022-03-08T18:14:13.753131Z","iopub.status.idle":"2022-03-08T18:14:13.978833Z","shell.execute_reply.started":"2022-03-08T18:14:13.753093Z","shell.execute_reply":"2022-03-08T18:14:13.978098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Pixel_Images = np.zeros((50000,32, 32, 3))\nfor idx,i in enumerate(imageIndex):\n#     print(i, idx)\n    Pixel_Images[idx] = np.array(imageToPixel( str(i) ))\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:13.98201Z","iopub.execute_input":"2022-03-08T18:14:13.982215Z","iopub.status.idle":"2022-03-08T18:14:28.715333Z","shell.execute_reply.started":"2022-03-08T18:14:13.982189Z","shell.execute_reply":"2022-03-08T18:14:28.714617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Pixel_Images.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.716419Z","iopub.execute_input":"2022-03-08T18:14:28.71665Z","iopub.status.idle":"2022-03-08T18:14:28.722577Z","shell.execute_reply.started":"2022-03-08T18:14:28.716617Z","shell.execute_reply":"2022-03-08T18:14:28.720753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Inp = pd.DataFrame(columns = ['image','label'])","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.724116Z","iopub.execute_input":"2022-03-08T18:14:28.724706Z","iopub.status.idle":"2022-03-08T18:14:28.733512Z","shell.execute_reply.started":"2022-03-08T18:14:28.724668Z","shell.execute_reply":"2022-03-08T18:14:28.732709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Inp['image'] = Pixel_Images","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.735087Z","iopub.execute_input":"2022-03-08T18:14:28.735353Z","iopub.status.idle":"2022-03-08T18:14:28.739857Z","shell.execute_reply.started":"2022-03-08T18:14:28.735319Z","shell.execute_reply":"2022-03-08T18:14:28.739088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Inp['label'] = labels['label']","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.741319Z","iopub.execute_input":"2022-03-08T18:14:28.741755Z","iopub.status.idle":"2022-03-08T18:14:28.758899Z","shell.execute_reply.started":"2022-03-08T18:14:28.741717Z","shell.execute_reply":"2022-03-08T18:14:28.758259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Inp)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.760175Z","iopub.execute_input":"2022-03-08T18:14:28.76058Z","iopub.status.idle":"2022-03-08T18:14:28.769857Z","shell.execute_reply.started":"2022-03-08T18:14:28.760544Z","shell.execute_reply":"2022-03-08T18:14:28.769063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"UniqueLabels = labels['label'].unique()\nmp = {}\nmp1 = {}","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.771597Z","iopub.execute_input":"2022-03-08T18:14:28.771774Z","iopub.status.idle":"2022-03-08T18:14:28.781183Z","shell.execute_reply.started":"2022-03-08T18:14:28.771751Z","shell.execute_reply":"2022-03-08T18:14:28.780407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mapping the indexing\nfor index, val in enumerate(UniqueLabels):\n    mp[val] = index\n    mp1[index] = val","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.782675Z","iopub.execute_input":"2022-03-08T18:14:28.783129Z","iopub.status.idle":"2022-03-08T18:14:28.787599Z","shell.execute_reply.started":"2022-03-08T18:14:28.783094Z","shell.execute_reply":"2022-03-08T18:14:28.78663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = np.array(labels['label'])\nfor i in range(y.size):\n    y[i] = mp[y[i]]\n\ny = np.array(y, dtype=int)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.789339Z","iopub.execute_input":"2022-03-08T18:14:28.789522Z","iopub.status.idle":"2022-03-08T18:14:28.816973Z","shell.execute_reply.started":"2022-03-08T18:14:28.789499Z","shell.execute_reply":"2022-03-08T18:14:28.816281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Inp['label'] = y","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.818082Z","iopub.execute_input":"2022-03-08T18:14:28.818721Z","iopub.status.idle":"2022-03-08T18:14:28.8237Z","shell.execute_reply.started":"2022-03-08T18:14:28.818684Z","shell.execute_reply":"2022-03-08T18:14:28.822988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Inp)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.824959Z","iopub.execute_input":"2022-03-08T18:14:28.825693Z","iopub.status.idle":"2022-03-08T18:14:28.835736Z","shell.execute_reply.started":"2022-03-08T18:14:28.825654Z","shell.execute_reply":"2022-03-08T18:14:28.834438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Inp.to_csv('Train_data.csv')\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.838345Z","iopub.execute_input":"2022-03-08T18:14:28.838827Z","iopub.status.idle":"2022-03-08T18:14:28.843306Z","shell.execute_reply.started":"2022-03-08T18:14:28.838798Z","shell.execute_reply":"2022-03-08T18:14:28.841214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data_train=pd.read_csv('./Train_data.csv')\n# y_train=Data_train[\"label\"].values\n# X_train=Data_train['image'].values\n# print(X_train.shape)\nPixel_Images = np.array(Pixel_Images, dtype = 'float')\n\nX_train = Pixel_Images\nX_train=X_train.reshape(50000,32,32,3)\nY_train = np.array(Inp['label'].values)\n\n\n\nfrom  sklearn.preprocessing  import OneHotEncoder\nOE = OneHotEncoder()\n#HERE OE return sparse array therefore converting it to array\nY_train = OE.fit_transform(pd.DataFrame(Y_train)).toarray()\nprint(Y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:28.844787Z","iopub.execute_input":"2022-03-08T18:14:28.84519Z","iopub.status.idle":"2022-03-08T18:14:30.186452Z","shell.execute_reply.started":"2022-03-08T18:14:28.845153Z","shell.execute_reply":"2022-03-08T18:14:30.185653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:30.187914Z","iopub.execute_input":"2022-03-08T18:14:30.188363Z","iopub.status.idle":"2022-03-08T18:14:30.192398Z","shell.execute_reply.started":"2022-03-08T18:14:30.188321Z","shell.execute_reply":"2022-03-08T18:14:30.191309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\") \n\nfrom tensorflow import keras\nfrom keras import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten, BatchNormalization, Activation, MaxPooling2D, Dropout","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:30.193869Z","iopub.execute_input":"2022-03-08T18:14:30.194463Z","iopub.status.idle":"2022-03-08T18:14:34.497959Z","shell.execute_reply.started":"2022-03-08T18:14:30.19442Z","shell.execute_reply":"2022-03-08T18:14:34.49722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL= Sequential()\nMODEL.add(Conv2D(filters=32,kernel_size=(3,3),strides=(1,1),padding='valid',activation=None,use_bias=False,input_shape=(32,32,3)))\nMODEL.add(BatchNormalization())\nMODEL.add(Activation('relu'))\n\nMODEL.add(Conv2D(filters=48, kernel_size=(3,3), strides=(1,1), padding='valid', activation=None, use_bias=False))\nMODEL.add(BatchNormalization())\nMODEL.add(Activation('relu'))\n\nMODEL.add(Conv2D(filters=64, kernel_size=(3,3), strides=(1,1), padding='valid', activation=None, use_bias=False))\nMODEL.add(BatchNormalization())\nMODEL.add(Activation('relu'))\n\nMODEL.add(Conv2D(filters=80, kernel_size=(3,3), strides=(1,1), padding='valid', activation=None, use_bias=False))\nMODEL.add(BatchNormalization())\nMODEL.add(Activation('relu'))\n\nMODEL.add(Conv2D(filters=96, kernel_size=(3,3), strides=(1,1), padding='valid', activation=None, use_bias=False))\nMODEL.add(BatchNormalization())\nMODEL.add(Activation('relu'))\n\nMODEL.add(Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), padding='valid', activation=None, use_bias=False))\nMODEL.add(BatchNormalization())\nMODEL.add(Activation('relu'))\n\nMODEL.add(Flatten())\nMODEL.add(Dense(units=10))\nMODEL.add(BatchNormalization())\nMODEL.add(Activation('softmax'))\nMODEL.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:34.499348Z","iopub.execute_input":"2022-03-08T18:14:34.499583Z","iopub.status.idle":"2022-03-08T18:14:36.697032Z","shell.execute_reply.started":"2022-03-08T18:14:34.49955Z","shell.execute_reply":"2022-03-08T18:14:36.696318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:36.698331Z","iopub.execute_input":"2022-03-08T18:14:36.698731Z","iopub.status.idle":"2022-03-08T18:14:36.708179Z","shell.execute_reply.started":"2022-03-08T18:14:36.698694Z","shell.execute_reply":"2022-03-08T18:14:36.707456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(Pixel_Images.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:14:36.709525Z","iopub.execute_input":"2022-03-08T18:14:36.709777Z","iopub.status.idle":"2022-03-08T18:14:36.716257Z","shell.execute_reply.started":"2022-03-08T18:14:36.709738Z","shell.execute_reply":"2022-03-08T18:14:36.715533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL.fit(X_train, Y_train, epochs=20) \n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:17:45.244446Z","iopub.execute_input":"2022-03-08T18:17:45.244984Z","iopub.status.idle":"2022-03-08T18:22:06.053828Z","shell.execute_reply.started":"2022-03-08T18:17:45.244948Z","shell.execute_reply":"2022-03-08T18:22:06.053097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SUB = pd.read_csv(\"../input/cifar-10/sampleSubmission.csv\")\nprint(SUB.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:38:31.385019Z","iopub.execute_input":"2022-03-08T18:38:31.385299Z","iopub.status.idle":"2022-03-08T18:38:31.450532Z","shell.execute_reply.started":"2022-03-08T18:38:31.385265Z","shell.execute_reply":"2022-03-08T18:38:31.44978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_test = pd.read_csv('')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import image as IMAGE\ndef imageToPixel_test(imageName):\n    ig = IMAGE.imread(\"test/\" + imageName + \".png\")\n    return ig","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:38:33.598957Z","iopub.execute_input":"2022-03-08T18:38:33.599508Z","iopub.status.idle":"2022-03-08T18:38:33.6041Z","shell.execute_reply.started":"2022-03-08T18:38:33.59947Z","shell.execute_reply":"2022-03-08T18:38:33.603282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Pixel_Images = np.zeros((SUB.shape[0],32, 32, 3))\nfor idx,i in enumerate(imageIndex):\n#     print(i, idx)\n    Pixel_Images[idx] = np.array(imageToPixel_test( str(i) ))\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:38:34.71463Z","iopub.execute_input":"2022-03-08T18:38:34.71488Z","iopub.status.idle":"2022-03-08T18:38:48.173325Z","shell.execute_reply.started":"2022-03-08T18:38:34.714853Z","shell.execute_reply":"2022-03-08T18:38:48.172548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = Pixel_Images\nX_test=X_test.reshape(SUB.shape[0],32,32,3)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:38:58.077954Z","iopub.execute_input":"2022-03-08T18:38:58.078268Z","iopub.status.idle":"2022-03-08T18:38:58.082949Z","shell.execute_reply.started":"2022-03-08T18:38:58.078211Z","shell.execute_reply":"2022-03-08T18:38:58.082279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=MODEL.predict(X_test) #make predictions on test images\nprint(predictions.shape) #This will be 28K x 10. For each test image, probability of 10 classes\n\n#Decide class label for each test image based on max probability.\n#This will be vector of size 28K. \npredictions=np.argmax(predictions,axis=1)\n# pred = np.array((SUB.shape[0]), dtype='object')","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:39:00.996616Z","iopub.execute_input":"2022-03-08T18:39:00.997405Z","iopub.status.idle":"2022-03-08T18:39:33.484038Z","shell.execute_reply.started":"2022-03-08T18:39:00.997356Z","shell.execute_reply":"2022-03-08T18:39:33.48307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(predictions.shape)\n# print( mp1[predictions[10]])\npred = []\nfor i in range(predictions.shape[0]):\n#     print(predictions[i].shape)\n    pred.append(mp1[predictions[i]])\nprint(len(pred))","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:39:45.385744Z","iopub.execute_input":"2022-03-08T18:39:45.386349Z","iopub.status.idle":"2022-03-08T18:39:45.574678Z","shell.execute_reply.started":"2022-03-08T18:39:45.386313Z","shell.execute_reply":"2022-03-08T18:39:45.57389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SUB[\"label\"]=pred\n\nSUB.to_csv('kaggle/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:40:38.563711Z","iopub.execute_input":"2022-03-08T18:40:38.563977Z","iopub.status.idle":"2022-03-08T18:40:38.609968Z","shell.execute_reply.started":"2022-03-08T18:40:38.563945Z","shell.execute_reply":"2022-03-08T18:40:38.608985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predictions.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T18:39:51.496585Z","iopub.execute_input":"2022-03-08T18:39:51.497221Z","iopub.status.idle":"2022-03-08T18:39:51.503182Z","shell.execute_reply.started":"2022-03-08T18:39:51.497165Z","shell.execute_reply":"2022-03-08T18:39:51.502064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imagesList = os.listdir('./test')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}