{"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","execution":{"iopub.status.busy":"2023-07-16T14:11:44.719356Z","iopub.execute_input":"2023-07-16T14:11:44.719727Z","iopub.status.idle":"2023-07-16T14:11:44.730683Z","shell.execute_reply.started":"2023-07-16T14:11:44.719697Z","shell.execute_reply":"2023-07-16T14:11:44.729660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense,Dropout,Conv2D,MaxPooling2D,Flatten\nfrom keras.constraints import maxnorm\nfrom keras.optimizers import SGD\nfrom keras.utils import np_utils\nfrom keras import backend as K","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:11:44.843719Z","iopub.execute_input":"2023-07-16T14:11:44.844450Z","iopub.status.idle":"2023-07-16T14:11:52.455943Z","shell.execute_reply.started":"2023-07-16T14:11:44.844413Z","shell.execute_reply":"2023-07-16T14:11:52.454950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr ","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:11:52.457933Z","iopub.execute_input":"2023-07-16T14:11:52.458688Z","iopub.status.idle":"2023-07-16T14:12:07.804010Z","shell.execute_reply.started":"2023-07-16T14:11:52.458651Z","shell.execute_reply":"2023-07-16T14:12:07.802861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\nimport glob\ntemp_path = '/kaggle/temp/'\nif not os.path.exists(temp_path):\n    os.mkdir(temp_path)\ntrain_file_path = '../input/cifar-10/train.7z'\narchive = py7zr.SevenZipFile(train_file_path, mode='r')\narchive.extractall(path=temp_path)\narchive.close()","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:12:07.805812Z","iopub.execute_input":"2023-07-16T14:12:07.806196Z","iopub.status.idle":"2023-07-16T14:13:05.360919Z","shell.execute_reply.started":"2023-07-16T14:12:07.806160Z","shell.execute_reply":"2023-07-16T14:13:05.359892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = glob.glob('/kaggle/temp/train/*.png')\nlen(images)","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:13:05.363573Z","iopub.execute_input":"2023-07-16T14:13:05.363934Z","iopub.status.idle":"2023-07-16T14:13:05.580417Z","shell.execute_reply.started":"2023-07-16T14:13:05.363900Z","shell.execute_reply":"2023-07-16T14:13:05.579401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport cv2 as cv\n\nfor i in range(9):\n    plt.subplot(330+1+i)\n    img=cv.imread(images[i])\n    plt.imshow(img)\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:13:05.581807Z","iopub.execute_input":"2023-07-16T14:13:05.582846Z","iopub.status.idle":"2023-07-16T14:13:06.671681Z","shell.execute_reply.started":"2023-07-16T14:13:05.582808Z","shell.execute_reply":"2023-07-16T14:13:06.670763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntrain_labels = pd.read_csv('../input/cifar-10/trainLabels.csv')\ntrain_labels\n","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:13:06.673181Z","iopub.execute_input":"2023-07-16T14:13:06.673781Z","iopub.status.idle":"2023-07-16T14:13:06.717930Z","shell.execute_reply.started":"2023-07-16T14:13:06.673746Z","shell.execute_reply":"2023-07-16T14:13:06.717061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LABELS=['frog', 'truck', 'deer', 'automobile', 'bird', 'horse', 'ship',\n        'cat', 'dog', 'airplane']","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:13:06.719215Z","iopub.execute_input":"2023-07-16T14:13:06.719735Z","iopub.status.idle":"2023-07-16T14:13:06.724812Z","shell.execute_reply.started":"2023-07-16T14:13:06.719703Z","shell.execute_reply":"2023-07-16T14:13:06.723915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = []\nY_train = []\n\n\nX_test = []\nY_test = []","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:51:36.140208Z","iopub.execute_input":"2023-07-16T14:51:36.140663Z","iopub.status.idle":"2023-07-16T14:51:36.530116Z","shell.execute_reply.started":"2023-07-16T14:51:36.140627Z","shell.execute_reply":"2023-07-16T14:51:36.528709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport random as rd\nimage_size=48\nfor img in images:\n    prob = rd.random()\n    label = train_labels.iloc[int(img[19:-4])-1]['label']\n    img_arr = cv2.imread(img)\n    img_arr = cv2.resize(img_arr,(32,32),interpolation=cv2.INTER_AREA)\n    if prob > 0.8:\n        X_test.append(list(img_arr))\n        Y_test.append(LABELS.index(label))\n    else:\n        X_train.append(list(img_arr))\n        Y_train.append(LABELS.index(label))","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:51:37.365440Z","iopub.execute_input":"2023-07-16T14:51:37.366007Z","iopub.status.idle":"2023-07-16T14:51:49.461917Z","shell.execute_reply.started":"2023-07-16T14:51:37.365958Z","shell.execute_reply":"2023-07-16T14:51:49.460896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.array(X_train, dtype=np.float32) \ny_train = np.array(Y_train)\n\nx_test = np.array(X_test, dtype=np.float32)\ny_test = np.array(Y_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:51:49.463826Z","iopub.execute_input":"2023-07-16T14:51:49.464480Z","iopub.status.idle":"2023-07-16T14:51:50.853041Z","shell.execute_reply.started":"2023-07-16T14:51:49.464443Z","shell.execute_reply":"2023-07-16T14:51:50.851931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:51:50.854921Z","iopub.execute_input":"2023-07-16T14:51:50.855293Z","iopub.status.idle":"2023-07-16T14:51:50.862889Z","shell.execute_reply.started":"2023-07-16T14:51:50.855258Z","shell.execute_reply":"2023-07-16T14:51:50.861716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train/255.0\nx_test=x_test/255.0","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:51:50.865311Z","iopub.execute_input":"2023-07-16T14:51:50.866340Z","iopub.status.idle":"2023-07-16T14:51:51.058632Z","shell.execute_reply.started":"2023-07-16T14:51:50.866292Z","shell.execute_reply":"2023-07-16T14:51:51.057564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train=np_utils.to_categorical(y_train)\ny_test=np_utils.to_categorical(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:51:51.059954Z","iopub.execute_input":"2023-07-16T14:51:51.060880Z","iopub.status.idle":"2023-07-16T14:51:51.067209Z","shell.execute_reply.started":"2023-07-16T14:51:51.060840Z","shell.execute_reply":"2023-07-16T14:51:51.066217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(32,(3,3),input_shape=(32,32,3),activation=\"relu\"))\nmodel.add(Dropout(0.2))\n# model.add(Conv2D(32,(3,3),activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(64,(3,3),activation=\"relu\"))\nmodel.add(Dropout(0.2))\n# model.add(Conv2D(64,(3,3),activation=\"relu\",padding=\"same\"))\nmodel.add(MaxPooling2D(pool_size=(2,2),padding=\"same\"))\nmodel.add(Conv2D(128,(3,3),activation=\"relu\",padding=\"same\"))\nmodel.add(Dropout(0.2))\n# model.add(Conv2D(128,(3,3),activation=\"relu\",padding=\"same\"))\nmodel.add(MaxPooling2D(pool_size=(2,2),padding=\"same\"))\nmodel.add(Flatten())\nmodel.add(Dropout(0.2))\nmodel.add(Dense(1024,activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(1024,activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10,activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:17:29.931444Z","iopub.execute_input":"2023-07-16T15:17:29.931841Z","iopub.status.idle":"2023-07-16T15:17:30.049963Z","shell.execute_reply.started":"2023-07-16T15:17:29.931810Z","shell.execute_reply":"2023-07-16T15:17:30.049036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=25\nlrate=0.01\ndecay=lrate/epochs\nsgd=SGD(lr=lrate,momentum=0.9,decay=decay,nesterov=False)\nmodel.compile(loss=\"categorical_crossentropy\",optimizer=sgd,metrics=[\"accuracy\"])\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:17:35.649888Z","iopub.execute_input":"2023-07-16T15:17:35.650276Z","iopub.status.idle":"2023-07-16T15:17:35.698287Z","shell.execute_reply.started":"2023-07-16T15:17:35.650245Z","shell.execute_reply":"2023-07-16T15:17:35.697423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=25,batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:17:45.789850Z","iopub.execute_input":"2023-07-16T15:17:45.790225Z","iopub.status.idle":"2023-07-16T15:19:29.481990Z","shell.execute_reply.started":"2023-07-16T15:17:45.790195Z","shell.execute_reply":"2023-07-16T15:19:29.480899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:21:54.306772Z","iopub.execute_input":"2023-07-16T15:21:54.307773Z","iopub.status.idle":"2023-07-16T15:21:54.312491Z","shell.execute_reply.started":"2023-07-16T15:21:54.307735Z","shell.execute_reply":"2023-07-16T15:21:54.311199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"accuracy\"])\nplt.plot(history.history[\"val_accuracy\"])\nplt.title(\"Model Accuracy\")\nplt.ylabel(\"accuracy\")\nplt.xlabel(\"epoch\")\nplt.legend([\"train\",\"test\"],loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:21:59.481298Z","iopub.execute_input":"2023-07-16T15:21:59.481700Z","iopub.status.idle":"2023-07-16T15:21:59.791477Z","shell.execute_reply.started":"2023-07-16T15:21:59.481665Z","shell.execute_reply":"2023-07-16T15:21:59.790472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"loss\"])\nplt.plot(history.history[\"val_loss\"])\nplt.title(\"Model Loss\")\nplt.ylabel(\"Loss\")\nplt.xlabel(\"epoch\")\nplt.legend([\"train\",\"test\"],loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:22:03.186443Z","iopub.execute_input":"2023-07-16T15:22:03.187442Z","iopub.status.idle":"2023-07-16T15:22:03.496426Z","shell.execute_reply.started":"2023-07-16T15:22:03.187404Z","shell.execute_reply":"2023-07-16T15:22:03.495452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}