{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-06-14T04:54:34.889039Z","iopub.execute_input":"2024-06-14T04:54:34.889447Z","iopub.status.idle":"2024-06-14T04:54:34.900470Z","shell.execute_reply.started":"2024-06-14T04:54:34.889415Z","shell.execute_reply":"2024-06-14T04:54:34.899218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import datasets, layers, models\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n(X_train, y_train), (X_test, y_test)= datasets.cifar10.load_data()","metadata":{"execution":{"iopub.status.busy":"2024-06-14T05:13:09.269428Z","iopub.execute_input":"2024-06-14T05:13:09.269888Z","iopub.status.idle":"2024-06-14T05:13:10.227809Z","shell.execute_reply.started":"2024-06-14T05:13:09.269854Z","shell.execute_reply":"2024-06-14T05:13:10.226654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-14T04:54:35.831113Z","iopub.execute_input":"2024-06-14T04:54:35.831454Z","iopub.status.idle":"2024-06-14T04:54:35.838539Z","shell.execute_reply.started":"2024-06-14T04:54:35.831425Z","shell.execute_reply":"2024-06-14T04:54:35.837321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-14T04:54:35.839960Z","iopub.execute_input":"2024-06-14T04:54:35.840322Z","iopub.status.idle":"2024-06-14T04:54:35.851404Z","shell.execute_reply.started":"2024-06-14T04:54:35.840291Z","shell.execute_reply":"2024-06-14T04:54:35.850112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = y_train.reshape(-1,)\ny_train[:5]","metadata":{"execution":{"iopub.status.busy":"2024-06-14T04:54:35.855374Z","iopub.execute_input":"2024-06-14T04:54:35.855801Z","iopub.status.idle":"2024-06-14T04:54:35.864350Z","shell.execute_reply.started":"2024-06-14T04:54:35.855756Z","shell.execute_reply":"2024-06-14T04:54:35.863326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes= [\"airplane\", \"automobile\", \"bird\", \"cat\", \"deer\", \"dog \",\"frog\", \"horse\", \"ship\", \"truck\"]\nclasses[4]","metadata":{"execution":{"iopub.status.busy":"2024-06-14T04:54:35.865551Z","iopub.execute_input":"2024-06-14T04:54:35.865904Z","iopub.status.idle":"2024-06-14T04:54:35.876313Z","shell.execute_reply.started":"2024-06-14T04:54:35.865876Z","shell.execute_reply":"2024-06-14T04:54:35.875184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sample(X, y, index):\n    plt.figure(figsize=(15,2))\n    plt.imshow(X[index])\n    plt.xlabel(classes[y[index]])","metadata":{"execution":{"iopub.status.busy":"2024-06-14T04:54:35.877643Z","iopub.execute_input":"2024-06-14T04:54:35.878005Z","iopub.status.idle":"2024-06-14T04:54:35.887697Z","shell.execute_reply.started":"2024-06-14T04:54:35.877978Z","shell.execute_reply":"2024-06-14T04:54:35.886492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, 105)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T04:54:35.889383Z","iopub.execute_input":"2024-06-14T04:54:35.889866Z","iopub.status.idle":"2024-06-14T04:54:36.114312Z","shell.execute_reply.started":"2024-06-14T04:54:35.889828Z","shell.execute_reply":"2024-06-14T04:54:36.113142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train / 255\nX_test = X_test / 255","metadata":{"execution":{"iopub.status.busy":"2024-06-14T04:54:36.115889Z","iopub.execute_input":"2024-06-14T04:54:36.116325Z","iopub.status.idle":"2024-06-14T04:54:36.687906Z","shell.execute_reply.started":"2024-06-14T04:54:36.116282Z","shell.execute_reply":"2024-06-14T04:54:36.686616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn = models.Sequential([\n    #cnn\n    layers.Conv2D(filters= 32, kernel_size= (3,3), activation='relu', input_shape= (32,32,3)),\n    layers.MaxPooling2D((2,2)),\n\n    layers.Conv2D(filters= 64, kernel_size= (3,3), activation='relu'),\n    layers.MaxPooling2D((2,2)),\n\n    #dense\n    layers.Flatten(),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(10, activation='softmax')\n    \n])","metadata":{"execution":{"iopub.status.busy":"2024-06-14T04:54:36.689153Z","iopub.execute_input":"2024-06-14T04:54:36.689510Z","iopub.status.idle":"2024-06-14T04:54:36.743509Z","shell.execute_reply.started":"2024-06-14T04:54:36.689480Z","shell.execute_reply":"2024-06-14T04:54:36.742363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.compile(optimizer= 'adam',\n            loss='sparse_categorical_crossentropy',\n            metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-14T04:54:36.745367Z","iopub.execute_input":"2024-06-14T04:54:36.745869Z","iopub.status.idle":"2024-06-14T04:54:36.757076Z","shell.execute_reply.started":"2024-06-14T04:54:36.745834Z","shell.execute_reply":"2024-06-14T04:54:36.755818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.fit(X_train,y_train, validation_data = (X_test,  y_test), epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T04:54:36.759114Z","iopub.execute_input":"2024-06-14T04:54:36.759511Z","iopub.status.idle":"2024-06-14T05:00:26.286714Z","shell.execute_reply.started":"2024-06-14T04:54:36.759473Z","shell.execute_reply":"2024-06-14T05:00:26.285556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.save(\"cnn_trained_weights.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-06-14T05:02:57.200871Z","iopub.execute_input":"2024-06-14T05:02:57.201279Z","iopub.status.idle":"2024-06-14T05:02:57.235674Z","shell.execute_reply.started":"2024-06-14T05:02:57.201248Z","shell.execute_reply":"2024-06-14T05:02:57.234484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model = tf.keras.models.load_model(\"cnn_trained_weights.h5\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\nimport py7zr\n\nwith py7zr.SevenZipFile('/kaggle/input/cifar-10/test.7z' , mode='r') as z:\n    z.extractall(\"/kaggle/working/\")","metadata":{"execution":{"iopub.status.busy":"2024-06-14T05:07:26.830899Z","iopub.execute_input":"2024-06-14T05:07:26.831304Z","iopub.status.idle":"2024-06-14T05:11:24.579274Z","shell.execute_reply.started":"2024-06-14T05:07:26.831269Z","shell.execute_reply":"2024-06-14T05:11:24.577707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = tf.keras.preprocessing.image_dataset_from_directory(\n    directory=\"/kaggle/working/test/\",\n    labels= None,\n    label_mode= None,\n    class_names=None,\n    color_mode='rgb',\n    batch_size=1, image_size= (32,32), \n    shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T05:13:17.851001Z","iopub.execute_input":"2024-06-14T05:13:17.851482Z","iopub.status.idle":"2024-06-14T05:13:36.814841Z","shell.execute_reply.started":"2024-06-14T05:13:17.851448Z","shell.execute_reply":"2024-06-14T05:13:36.812973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_class_ids = tf.math.argmax(cnn.predict(test_dataset), axis = 1).numpy()","metadata":{"execution":{"iopub.status.busy":"2024-06-14T05:14:51.562282Z","iopub.execute_input":"2024-06-14T05:14:51.562765Z","iopub.status.idle":"2024-06-14T05:28:11.788459Z","shell.execute_reply.started":"2024-06-14T05:14:51.562716Z","shell.execute_reply":"2024-06-14T05:28:11.786973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open(\"/kaggle/working/submissions_cifar10.csv\", \"w\")\nf.write(\"id,label\\n\")\n\nfor index, class_id in enumerate(predicted_class_ids):\n    class_name_string = classes[class_id]\n    line = str(index+1) +\",\" + class_name_string+\"\\n\"\n    f.write(line)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T05:30:45.867351Z","iopub.execute_input":"2024-06-14T05:30:45.867816Z","iopub.status.idle":"2024-06-14T05:30:46.268979Z","shell.execute_reply.started":"2024-06-14T05:30:45.867780Z","shell.execute_reply":"2024-06-14T05:30:46.267408Z"},"trusted":true},"execution_count":null,"outputs":[]}]}