{"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\n#import os\n#for 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":"dd834de8-e5d9-46fb-b495-dbcf65fa4fc6","_cell_guid":"583aaa04-ff56-4df4-92ca-97796512d85b","collapsed":false,"jupyter":{"outputs_hidden":false},"editable":false,"execution":{"iopub.status.busy":"2022-03-27T06:14:06.500186Z","iopub.execute_input":"2022-03-27T06:14:06.500908Z","iopub.status.idle":"2022-03-27T06:14:06.506427Z","shell.execute_reply.started":"2022-03-27T06:14:06.500871Z","shell.execute_reply":"2022-03-27T06:14:06.505481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom keras.layers import Conv2D,Dense,Input,Flatten\nfrom keras import Model","metadata":{"_uuid":"ee1942f1-2bbf-4581-8c82-56244bfa4734","_cell_guid":"3f6f5cc5-276c-4aad-a079-1810062ff7d0","collapsed":false,"jupyter":{"outputs_hidden":false},"editable":false,"execution":{"iopub.status.busy":"2022-03-27T06:14:08.688208Z","iopub.execute_input":"2022-03-27T06:14:08.688470Z","iopub.status.idle":"2022-03-27T06:14:08.693325Z","shell.execute_reply.started":"2022-03-27T06:14:08.688440Z","shell.execute_reply":"2022-03-27T06:14:08.692441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nd1={0: 'airplane', 1: 'automobile', 2: 'bird', 3: 'cat', 4: 'deer', 5: 'dog',6: 'frog', 7: 'horse', 8: 'ship', 9: 'truck'}\nd2={'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4, 'dog': 5,'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9}","metadata":{"execution":{"iopub.status.busy":"2022-03-27T06:14:11.844235Z","iopub.execute_input":"2022-03-27T06:14:11.844495Z","iopub.status.idle":"2022-03-27T06:14:11.850388Z","shell.execute_reply.started":"2022-03-27T06:14:11.844466Z","shell.execute_reply":"2022-03-27T06:14:11.849598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainX = np.load('../input/cifar10trainx/trainX.npy')\ntrainY = pd.read_csv('../input/cifar-10/trainLabels.csv')\ntrainY = np.array(trainY,copy = False)\ntrainY = trainY[:,-1]\ntrainY = np.array([d2[x] for x in trainY],copy=False)\nprint(trainX.shape,trainY.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T07:04:55.531352Z","iopub.execute_input":"2022-03-27T07:04:55.531599Z","iopub.status.idle":"2022-03-27T07:04:55.623184Z","shell.execute_reply.started":"2022-03-27T07:04:55.531572Z","shell.execute_reply":"2022-03-27T07:04:55.622378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = Input(shape = (32,32,3))\nx = keras.layers.Rescaling(1.0/255)(inputs)\nx = keras.layers.RandomFlip(\"horizontal_and_vertical\")(x)\nx = keras.layers.RandomRotation(0.2)(x)\nx = Conv2D(32,5,activation='selu',padding='same')(x)\nx = keras.layers.BatchNormalization()(x)\nx = Conv2D(32,5,activation='selu')(x)\nx = keras.layers.BatchNormalization()(x)\nx = Conv2D(32,5,activation='selu',padding='same')(x)\nx = keras.layers.BatchNormalization()(x)\nx = Conv2D(64,5,activation='selu')(x)\nx = keras.layers.BatchNormalization()(x)\nx = Conv2D(64,2,activation='selu',strides=(2,2))(x)\nx = keras.layers.BatchNormalization()(x)\nx = Conv2D(64,5,activation='selu')(x)\nx = keras.layers.BatchNormalization()(x)\nx = Conv2D(128,5,activation='selu')(x)\nx = keras.layers.BatchNormalization()(x)\nx = Conv2D(128,4,activation='selu')(x)\nx = keras.layers.BatchNormalization()(x)\nx = Flatten()(x)\nx = Dense(120,activation='selu')(x)\nx = keras.layers.Dropout(0.5)(x)\nx = Dense(80,activation='selu')(x)\nx = keras.layers.Dropout(0.5)(x)\nx = Dense(40,activation='selu')(x)\nx = keras.layers.Dropout(0.5)(x)\noutputs = Dense(10,activation='softmax')(x)\nmodel = Model(inputs=inputs,outputs=outputs,name='Cifar10_CNN')\nmodel.summary()\nmodel.compile(optimizer='adam',metrics=['accuracy'],loss = keras.losses.SparseCategoricalCrossentropy())\nreduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2,\n                              patience=5, min_lr=0.00001)\nmodel.fit(trainX,trainY, batch_size=32, epochs=200,callbacks=[reduce_lr])","metadata":{"execution":{"iopub.status.busy":"2022-03-27T07:04:57.732329Z","iopub.execute_input":"2022-03-27T07:04:57.732579Z","iopub.status.idle":"2022-03-27T07:05:31.138423Z","shell.execute_reply.started":"2022-03-27T07:04:57.732552Z","shell.execute_reply":"2022-03-27T07:05:31.137094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del trainX\ndel trainY\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T07:01:59.010627Z","iopub.execute_input":"2022-03-27T07:01:59.011179Z","iopub.status.idle":"2022-03-27T07:02:44.527250Z","shell.execute_reply.started":"2022-03-27T07:01:59.011142Z","shell.execute_reply":"2022-03-27T07:02:44.526573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = np.load('../input/testcifar10/test.npy')\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T07:02:44.528789Z","iopub.execute_input":"2022-03-27T07:02:44.529104Z","iopub.status.idle":"2022-03-27T07:02:44.857700Z","shell.execute_reply.started":"2022-03-27T07:02:44.529067Z","shell.execute_reply":"2022-03-27T07:02:44.856966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test)\nprint(predictions)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T07:02:48.063504Z","iopub.execute_input":"2022-03-27T07:02:48.063772Z","iopub.status.idle":"2022-03-27T07:03:10.287199Z","shell.execute_reply.started":"2022-03-27T07:02:48.063725Z","shell.execute_reply":"2022-03-27T07:03:10.286080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T07:03:12.230187Z","iopub.execute_input":"2022-03-27T07:03:12.230438Z","iopub.status.idle":"2022-03-27T07:03:12.473324Z","shell.execute_reply.started":"2022-03-27T07:03:12.230409Z","shell.execute_reply":"2022-03-27T07:03:12.472241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(predictions,axis=1)\nprint(predictions)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T07:03:14.900244Z","iopub.execute_input":"2022-03-27T07:03:14.900489Z","iopub.status.idle":"2022-03-27T07:03:14.913561Z","shell.execute_reply.started":"2022-03-27T07:03:14.900461Z","shell.execute_reply":"2022-03-27T07:03:14.912806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame([[x+1,d1[predictions[x]]] for x in range(len(predictions))])\ndf.to_csv('submission.csv',index=False,header=['id','label'])","metadata":{"execution":{"iopub.status.busy":"2022-03-27T07:03:17.431949Z","iopub.execute_input":"2022-03-27T07:03:17.432196Z","iopub.status.idle":"2022-03-27T07:03:18.662354Z","shell.execute_reply.started":"2022-03-27T07:03:17.432170Z","shell.execute_reply":"2022-03-27T07:03:18.661513Z"},"trusted":true},"execution_count":null,"outputs":[]}]}