{"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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"},"accelerator":"GPU","gpuClass":"standard","kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install py7zr","metadata":{"id":"A47Bhq576UHz","outputId":"a7a70f85-eaec-459e-efd5-8f6bff69de95","execution":{"iopub.status.busy":"2024-07-23T09:36:08.926657Z","iopub.execute_input":"2024-07-23T09:36:08.927412Z","iopub.status.idle":"2024-07-23T09:36:26.162229Z","shell.execute_reply.started":"2024-07-23T09:36:08.927381Z","shell.execute_reply":"2024-07-23T09:36:26.161087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📊 CIFAR Image Dataset Overview\n\nWelcome to the **CIFAR Image Dataset** competition! 🚀 In this notebook, we'll explore the CIFAR dataset, a popular benchmark in machine learning for image classification.\n\n## 🖼️ Dataset Description\n\nThe CIFAR dataset consists of 60,000 32x32 color images categorized into 10 distinct classes. Each class contains 6,000 images, and the dataset is widely used to evaluate and improve image classification models. \n\n## 🏷️ Labels and Their Corresponding Emojis\n\nHere’s a breakdown of the classes in the dataset along with their respective labels:\n\n- **✈️ Airplane:** 0\n- **🚗 Automobile:** 1\n- **🐦 Bird:** 2\n- **🐱 Cat:** 3\n- **🦌 Deer:** 4\n- **🐶 Dog:** 5\n- **🐸 Frog:** 6\n- **🐴 Horse:** 7\n- **🚢 Ship:** 8\n- **🚚 Truck:** 9\n\n## 🗂️ Steps for Analysis\n\n1. **Data Loading**: We will start by loading the CIFAR dataset and examining its structure, including the training and testing sets.\n\n2. **Exploratory Data Analysis (EDA)**: Conduct a thorough analysis of the dataset to understand the distribution of classes and visualize sample images.\n\n3. **Preprocessing**: Prepare the data for training by normalizing images and splitting the dataset into training and validation sets.\n\n4. **Model Training**: Implement and train a machine learning model (e.g., Convolutional Neural Network) on the CIFAR dataset.\n\n5. **Evaluation**: Assess the performance of the trained model using metrics like accuracy and loss. Visualize predictions to understand how well the model performs.\n\n6. **Improvement and Tuning**: Experiment with different architectures, hyperparameters, and data augmentation techniques to improve model performance.\n\n## 📈 Visualization\n\nWe will include visualizations of sample images from each class to gain insight into what each category looks like and verify our data processing steps.\n\n## 📝 Conclusion\n\nBy the end of this notebook, we will have a solid understanding of the CIFAR dataset and have built a model to classify images into one of the 10 categories. Let’s get started! 💪\n","metadata":{}},{"cell_type":"code","source":"#/kaggle/input/cifar-10/trainLabels.csv\n#/kaggle/input/cifar-10/sampleSubmission.csv\n#/kaggle/input/cifar-10/test.7z\n#/kaggle/input/cifar-10/train.7z","metadata":{"execution":{"iopub.status.busy":"2024-07-23T09:36:26.164198Z","iopub.execute_input":"2024-07-23T09:36:26.164471Z","iopub.status.idle":"2024-07-23T09:36:26.168684Z","shell.execute_reply.started":"2024-07-23T09:36:26.164445Z","shell.execute_reply":"2024-07-23T09:36:26.167848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import py7zr\n\narchive = py7zr.SevenZipFile('/kaggle/input/cifar-10/train.7z', mode='r')\narchive.extractall()     #archive.extractall(path='/content/Training Data')\narchive.close()","metadata":{"id":"1hLTfXc_6WsK","execution":{"iopub.status.busy":"2024-07-23T09:36:26.169948Z","iopub.execute_input":"2024-07-23T09:36:26.170229Z","iopub.status.idle":"2024-07-23T09:36:52.142987Z","shell.execute_reply.started":"2024-07-23T09:36:26.170206Z","shell.execute_reply":"2024-07-23T09:36:52.141946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Importing the Dependencies","metadata":{"id":"7ycg4hpg4l_v"}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nimport cv2","metadata":{"id":"XDrc46Od4PNm","execution":{"iopub.status.busy":"2024-07-23T09:36:52.145437Z","iopub.execute_input":"2024-07-23T09:36:52.145854Z","iopub.status.idle":"2024-07-23T09:36:53.432927Z","shell.execute_reply.started":"2024-07-23T09:36:52.145826Z","shell.execute_reply":"2024-07-23T09:36:53.431943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir('/kaggle/working/train')","metadata":{"id":"fYPF2UQa45eP","execution":{"iopub.status.busy":"2024-07-23T09:36:53.434033Z","iopub.execute_input":"2024-07-23T09:36:53.434433Z","iopub.status.idle":"2024-07-23T09:36:53.466686Z","shell.execute_reply.started":"2024-07-23T09:36:53.434408Z","shell.execute_reply":"2024-07-23T09:36:53.465949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(filenames)","metadata":{"id":"LcB0c9Ou5DSy","outputId":"03c769e0-e0ff-48c4-cad7-5db05ae215cb","execution":{"iopub.status.busy":"2024-07-23T09:36:53.467688Z","iopub.execute_input":"2024-07-23T09:36:53.467953Z","iopub.status.idle":"2024-07-23T09:36:53.474521Z","shell.execute_reply.started":"2024-07-23T09:36:53.467930Z","shell.execute_reply":"2024-07-23T09:36:53.473605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(filenames)","metadata":{"id":"wiB5cUw-5Ec9","outputId":"6e1a33ce-37ad-4221-b184-01445bd2bb77","execution":{"iopub.status.busy":"2024-07-23T09:36:53.475902Z","iopub.execute_input":"2024-07-23T09:36:53.476158Z","iopub.status.idle":"2024-07-23T09:36:53.484455Z","shell.execute_reply.started":"2024-07-23T09:36:53.476135Z","shell.execute_reply":"2024-07-23T09:36:53.483518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(filenames[0:5])\nprint(filenames[-5:])","metadata":{"id":"8QK16l2D5GIg","outputId":"984052ae-e4b5-4680-e6a6-dab185f13e42","execution":{"iopub.status.busy":"2024-07-23T09:36:53.485482Z","iopub.execute_input":"2024-07-23T09:36:53.485803Z","iopub.status.idle":"2024-07-23T09:36:53.493526Z","shell.execute_reply.started":"2024-07-23T09:36:53.485780Z","shell.execute_reply":"2024-07-23T09:36:53.492688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Labels Processing**","metadata":{"id":"QQmWrdzU5cHu"}},{"cell_type":"code","source":"labels_df = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')","metadata":{"id":"AmiWTQhr5Vrj","execution":{"iopub.status.busy":"2024-07-23T09:36:53.494548Z","iopub.execute_input":"2024-07-23T09:36:53.494907Z","iopub.status.idle":"2024-07-23T09:36:53.534803Z","shell.execute_reply.started":"2024-07-23T09:36:53.494883Z","shell.execute_reply":"2024-07-23T09:36:53.533632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.shape","metadata":{"id":"YIBmcE215jyX","outputId":"afef2856-4e1b-4b1c-be1d-e033212b5dba","execution":{"iopub.status.busy":"2024-07-23T09:36:53.539877Z","iopub.execute_input":"2024-07-23T09:36:53.540167Z","iopub.status.idle":"2024-07-23T09:36:53.545753Z","shell.execute_reply.started":"2024-07-23T09:36:53.540142Z","shell.execute_reply":"2024-07-23T09:36:53.544871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head()","metadata":{"id":"ALaGYrGH5l4i","outputId":"f56192a3-be64-4c5a-c1b6-8b5f2c153bc5","execution":{"iopub.status.busy":"2024-07-23T09:36:53.546909Z","iopub.execute_input":"2024-07-23T09:36:53.547306Z","iopub.status.idle":"2024-07-23T09:36:53.568150Z","shell.execute_reply.started":"2024-07-23T09:36:53.547275Z","shell.execute_reply":"2024-07-23T09:36:53.567273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 7796]","metadata":{"id":"qHTffWNq5qFM","outputId":"7b8ff35b-058d-4640-9bb3-e6a957414848","execution":{"iopub.status.busy":"2024-07-23T09:36:53.569553Z","iopub.execute_input":"2024-07-23T09:36:53.569895Z","iopub.status.idle":"2024-07-23T09:36:53.583380Z","shell.execute_reply.started":"2024-07-23T09:36:53.569866Z","shell.execute_reply":"2024-07-23T09:36:53.582360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head(10)","metadata":{"id":"FHrIlSmD52ss","outputId":"259b4b01-3046-42ec-ae96-ece848084bfb","execution":{"iopub.status.busy":"2024-07-23T09:36:53.584361Z","iopub.execute_input":"2024-07-23T09:36:53.584631Z","iopub.status.idle":"2024-07-23T09:36:53.595660Z","shell.execute_reply.started":"2024-07-23T09:36:53.584606Z","shell.execute_reply":"2024-07-23T09:36:53.594736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.tail(10)","metadata":{"id":"uAFtg_Qw6CMJ","outputId":"81fe0999-30c9-42fe-f408-da540faa542e","execution":{"iopub.status.busy":"2024-07-23T09:36:53.596783Z","iopub.execute_input":"2024-07-23T09:36:53.597123Z","iopub.status.idle":"2024-07-23T09:36:53.609402Z","shell.execute_reply.started":"2024-07-23T09:36:53.597090Z","shell.execute_reply":"2024-07-23T09:36:53.608511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df['label'].value_counts()","metadata":{"id":"l3cckecv6Eq6","outputId":"e348cbb4-3e6f-41e5-cc2e-67b6ee59fe09","execution":{"iopub.status.busy":"2024-07-23T09:36:53.610764Z","iopub.execute_input":"2024-07-23T09:36:53.611398Z","iopub.status.idle":"2024-07-23T09:36:53.635594Z","shell.execute_reply.started":"2024-07-23T09:36:53.611373Z","shell.execute_reply":"2024-07-23T09:36:53.634686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df['label']","metadata":{"id":"HH11pas67cC_","outputId":"6c19ab40-2430-4268-ace0-c5e28b962948","execution":{"iopub.status.busy":"2024-07-23T09:36:53.636812Z","iopub.execute_input":"2024-07-23T09:36:53.637067Z","iopub.status.idle":"2024-07-23T09:36:53.644306Z","shell.execute_reply.started":"2024-07-23T09:36:53.637046Z","shell.execute_reply":"2024-07-23T09:36:53.643315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dictionary = {'airplane':0, 'automobile':1, 'bird':2, 'cat':3, 'deer':4, 'dog':5, 'frog':6, 'horse':7, 'ship':8, 'truck':9}\n\nlabels = [labels_dictionary[i] for i in labels_df['label']]","metadata":{"id":"lsfTS0y56re8","execution":{"iopub.status.busy":"2024-07-23T09:36:53.645544Z","iopub.execute_input":"2024-07-23T09:36:53.645912Z","iopub.status.idle":"2024-07-23T09:36:53.663445Z","shell.execute_reply.started":"2024-07-23T09:36:53.645884Z","shell.execute_reply":"2024-07-23T09:36:53.662776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels[0:5])\nprint(labels[-5:])","metadata":{"id":"7Zi081ze7wDo","outputId":"0a45e36d-bdda-498d-807d-9747601ddcdb","execution":{"iopub.status.busy":"2024-07-23T09:36:53.664469Z","iopub.execute_input":"2024-07-23T09:36:53.664794Z","iopub.status.idle":"2024-07-23T09:36:53.674837Z","shell.execute_reply.started":"2024-07-23T09:36:53.664764Z","shell.execute_reply":"2024-07-23T09:36:53.673745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimg = cv2.imread('train/7796.png')\nplt.imshow(img)","metadata":{"id":"Gu3-MI0X71sm","outputId":"ce6d1819-405a-41e2-e740-d137331331fc","execution":{"iopub.status.busy":"2024-07-23T09:36:53.675879Z","iopub.execute_input":"2024-07-23T09:36:53.676108Z","iopub.status.idle":"2024-07-23T09:36:53.988958Z","shell.execute_reply.started":"2024-07-23T09:36:53.676088Z","shell.execute_reply":"2024-07-23T09:36:53.988089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread('train/45888.png')\nplt.imshow(img)","metadata":{"id":"zdK90p1c8jmM","outputId":"4be0acc3-60f0-45b1-c67b-9a7a3e058f01","execution":{"iopub.status.busy":"2024-07-23T09:36:53.990183Z","iopub.execute_input":"2024-07-23T09:36:53.990537Z","iopub.status.idle":"2024-07-23T09:36:54.255210Z","shell.execute_reply.started":"2024-07-23T09:36:53.990503Z","shell.execute_reply":"2024-07-23T09:36:54.254287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 45888]","metadata":{"id":"8Kpcl2kn8s4j","outputId":"f42a8e01-0eea-41ef-9c48-54fff0f4d508","execution":{"iopub.status.busy":"2024-07-23T09:36:54.256330Z","iopub.execute_input":"2024-07-23T09:36:54.256608Z","iopub.status.idle":"2024-07-23T09:36:54.266856Z","shell.execute_reply.started":"2024-07-23T09:36:54.256584Z","shell.execute_reply":"2024-07-23T09:36:54.265803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head()","metadata":{"id":"sfSsyPGm8yCA","outputId":"d244260b-5cf8-45a7-d05d-564b8ba5f27f","execution":{"iopub.status.busy":"2024-07-23T09:36:54.268188Z","iopub.execute_input":"2024-07-23T09:36:54.268491Z","iopub.status.idle":"2024-07-23T09:36:54.281466Z","shell.execute_reply.started":"2024-07-23T09:36:54.268467Z","shell.execute_reply":"2024-07-23T09:36:54.280467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_list = list(labels_df['id'])","metadata":{"id":"b75ucjTu8721","execution":{"iopub.status.busy":"2024-07-23T09:36:54.282557Z","iopub.execute_input":"2024-07-23T09:36:54.283181Z","iopub.status.idle":"2024-07-23T09:36:54.298759Z","shell.execute_reply.started":"2024-07-23T09:36:54.283154Z","shell.execute_reply":"2024-07-23T09:36:54.297831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(id_list[0:5])\nprint(id_list[-5:])","metadata":{"id":"VIEfktRL9Aga","outputId":"0254dc22-ac99-48d9-de4a-654c630982c4","execution":{"iopub.status.busy":"2024-07-23T09:36:54.300047Z","iopub.execute_input":"2024-07-23T09:36:54.300388Z","iopub.status.idle":"2024-07-23T09:36:54.308708Z","shell.execute_reply.started":"2024-07-23T09:36:54.300360Z","shell.execute_reply":"2024-07-23T09:36:54.307791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Image Processing**","metadata":{"id":"L0LJZjQB9Kdu"}},{"cell_type":"code","source":"train_data_folder = '/kaggle/working/train'\ndata=[]\nfor id in id_list:\n    file_path = os.path.join(train_data_folder, f'{id}.png')\n\n    if os.path.isfile(file_path):\n        image = Image.open(file_path)\n        image = np.array(image)\n        data.append(image)\n    else:\n        print(f\"File not found: {file_path}\")\n\ndata = np.array(data)\n\nprint(f\"Loaded {len(data)} images with shape: {data.shape}\")","metadata":{"id":"z7Op0HnY9EeP","execution":{"iopub.status.busy":"2024-07-23T09:36:54.309824Z","iopub.execute_input":"2024-07-23T09:36:54.310088Z","iopub.status.idle":"2024-07-23T09:37:03.629104Z","shell.execute_reply.started":"2024-07-23T09:36:54.310067Z","shell.execute_reply":"2024-07-23T09:37:03.628168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(data)","metadata":{"id":"ohsDh2Cb93gj","outputId":"31854a19-8e95-4b3c-aa81-b5c8849e1287","execution":{"iopub.status.busy":"2024-07-23T09:37:03.631024Z","iopub.execute_input":"2024-07-23T09:37:03.631778Z","iopub.status.idle":"2024-07-23T09:37:03.638606Z","shell.execute_reply.started":"2024-07-23T09:37:03.631739Z","shell.execute_reply":"2024-07-23T09:37:03.637699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(data)","metadata":{"id":"AfrZID68-r-A","outputId":"923c1c59-b86b-43fe-9358-a54b62b1b8e6","execution":{"iopub.status.busy":"2024-07-23T09:37:03.639721Z","iopub.execute_input":"2024-07-23T09:37:03.640512Z","iopub.status.idle":"2024-07-23T09:37:03.652116Z","shell.execute_reply.started":"2024-07-23T09:37:03.640478Z","shell.execute_reply":"2024-07-23T09:37:03.651283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(data[0])","metadata":{"id":"10shzis2-tqk","outputId":"22263467-b72b-45eb-e226-8f29959bc40d","execution":{"iopub.status.busy":"2024-07-23T09:37:03.658464Z","iopub.execute_input":"2024-07-23T09:37:03.658744Z","iopub.status.idle":"2024-07-23T09:37:03.664415Z","shell.execute_reply.started":"2024-07-23T09:37:03.658722Z","shell.execute_reply":"2024-07-23T09:37:03.663601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[0].shape","metadata":{"id":"2wGYWLGP-yGa","outputId":"7fd17fef-83e1-46cb-d9d5-448b70c7d73a","execution":{"iopub.status.busy":"2024-07-23T09:37:03.665661Z","iopub.execute_input":"2024-07-23T09:37:03.665988Z","iopub.status.idle":"2024-07-23T09:37:03.675720Z","shell.execute_reply.started":"2024-07-23T09:37:03.665958Z","shell.execute_reply":"2024-07-23T09:37:03.674969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[0]","metadata":{"id":"yG26hpqs-10y","outputId":"1bcde0a9-cdcb-4931-b020-7df3310f134a","execution":{"iopub.status.busy":"2024-07-23T09:37:03.676859Z","iopub.execute_input":"2024-07-23T09:37:03.677139Z","iopub.status.idle":"2024-07-23T09:37:03.686486Z","shell.execute_reply.started":"2024-07-23T09:37:03.677117Z","shell.execute_reply":"2024-07-23T09:37:03.685629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nX = np.array(data)\nY = np.array(labels)","metadata":{"id":"YFTwI-ha_QjV","execution":{"iopub.status.busy":"2024-07-23T09:37:03.687553Z","iopub.execute_input":"2024-07-23T09:37:03.687859Z","iopub.status.idle":"2024-07-23T09:37:03.753586Z","shell.execute_reply.started":"2024-07-23T09:37:03.687837Z","shell.execute_reply":"2024-07-23T09:37:03.752805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(X)","metadata":{"id":"ww6XHsI__1jp","outputId":"3fc5a4dd-838c-4bec-b762-c85c8ecd132b","execution":{"iopub.status.busy":"2024-07-23T09:37:03.754733Z","iopub.execute_input":"2024-07-23T09:37:03.755012Z","iopub.status.idle":"2024-07-23T09:37:03.760486Z","shell.execute_reply.started":"2024-07-23T09:37:03.754988Z","shell.execute_reply":"2024-07-23T09:37:03.759616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape)\nprint(Y.shape)","metadata":{"id":"JuOKKmvx_3yL","outputId":"ce63794f-33fb-49f2-a921-8eb152d257e4","execution":{"iopub.status.busy":"2024-07-23T09:37:03.761648Z","iopub.execute_input":"2024-07-23T09:37:03.761920Z","iopub.status.idle":"2024-07-23T09:37:03.769386Z","shell.execute_reply.started":"2024-07-23T09:37:03.761899Z","shell.execute_reply":"2024-07-23T09:37:03.768637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Train Test Split**","metadata":{"id":"TnLV3dJXAGin"}},{"cell_type":"code","source":"X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=2)","metadata":{"id":"Trb9IyVO_7Si","execution":{"iopub.status.busy":"2024-07-23T09:37:03.770342Z","iopub.execute_input":"2024-07-23T09:37:03.770616Z","iopub.status.idle":"2024-07-23T09:37:03.832921Z","shell.execute_reply.started":"2024-07-23T09:37:03.770594Z","shell.execute_reply":"2024-07-23T09:37:03.832134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape, X_train.shape, X_test.shape)","metadata":{"id":"sznRIBQsAnXr","outputId":"ec1e569e-6a86-4fc5-97a8-769896ceb7db","execution":{"iopub.status.busy":"2024-07-23T09:37:03.834042Z","iopub.execute_input":"2024-07-23T09:37:03.834346Z","iopub.status.idle":"2024-07-23T09:37:03.839259Z","shell.execute_reply.started":"2024-07-23T09:37:03.834322Z","shell.execute_reply":"2024-07-23T09:37:03.838324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nX_train_scaled = X_train/255\n\nX_test_scaled = X_test/255","metadata":{"id":"XQJyHDgtAsR7","execution":{"iopub.status.busy":"2024-07-23T09:37:03.840280Z","iopub.execute_input":"2024-07-23T09:37:03.840531Z","iopub.status.idle":"2024-07-23T09:37:04.222414Z","shell.execute_reply.started":"2024-07-23T09:37:03.840509Z","shell.execute_reply":"2024-07-23T09:37:04.221623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_scaled","metadata":{"id":"VZIiA77gBXho","outputId":"45ab6cd3-0a8f-450e-e364-84c78ecc9633","execution":{"iopub.status.busy":"2024-07-23T09:37:04.223499Z","iopub.execute_input":"2024-07-23T09:37:04.223799Z","iopub.status.idle":"2024-07-23T09:37:04.240787Z","shell.execute_reply.started":"2024-07-23T09:37:04.223774Z","shell.execute_reply":"2024-07-23T09:37:04.239814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[0]","metadata":{"id":"Yaske_y1BZ4p","outputId":"072aa9d9-4927-4893-fe7f-fc9b420818f1","execution":{"iopub.status.busy":"2024-07-23T09:37:04.241890Z","iopub.execute_input":"2024-07-23T09:37:04.242178Z","iopub.status.idle":"2024-07-23T09:37:04.253983Z","shell.execute_reply.started":"2024-07-23T09:37:04.242144Z","shell.execute_reply":"2024-07-23T09:37:04.253017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Building the Neural Network**","metadata":{"id":"5cYLBpAlBo-5"}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras","metadata":{"id":"ngnXZmeIBd6E","execution":{"iopub.status.busy":"2024-07-23T09:37:04.255100Z","iopub.execute_input":"2024-07-23T09:37:04.255434Z","iopub.status.idle":"2024-07-23T09:37:17.932732Z","shell.execute_reply.started":"2024-07-23T09:37:04.255408Z","shell.execute_reply":"2024-07-23T09:37:17.931595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_of_classes = 10\n\n\n\nmodel = keras.Sequential([\n\n    keras.layers.Flatten(input_shape=(32,32,3)),\n    keras.layers.Dense(64, activation='relu'),\n    keras.layers.Dense(num_of_classes, activation='softmax')\n])","metadata":{"id":"muIs9RZMBx3q","execution":{"iopub.status.busy":"2024-07-23T09:37:17.937841Z","iopub.execute_input":"2024-07-23T09:37:17.939078Z","iopub.status.idle":"2024-07-23T09:37:18.888010Z","shell.execute_reply.started":"2024-07-23T09:37:17.939021Z","shell.execute_reply":"2024-07-23T09:37:18.887119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compile the neural network\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['acc'])","metadata":{"id":"P2klFi7WEAES","execution":{"iopub.status.busy":"2024-07-23T09:37:18.889070Z","iopub.execute_input":"2024-07-23T09:37:18.889360Z","iopub.status.idle":"2024-07-23T09:37:18.903978Z","shell.execute_reply.started":"2024-07-23T09:37:18.889335Z","shell.execute_reply":"2024-07-23T09:37:18.903171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel.fit(X_train_scaled, Y_train, validation_split=0.1, epochs=10)","metadata":{"id":"lppHDlsZEV_z","outputId":"b0d03696-8f59-44fb-ecde-b56d7c1acac6","execution":{"iopub.status.busy":"2024-07-23T09:37:18.905066Z","iopub.execute_input":"2024-07-23T09:37:18.905328Z","iopub.status.idle":"2024-07-23T09:37:43.497417Z","shell.execute_reply.started":"2024-07-23T09:37:18.905306Z","shell.execute_reply":"2024-07-23T09:37:43.496551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**ResNet50**","metadata":{"id":"kHMcPU12FasK"}},{"cell_type":"code","source":"from tensorflow.keras import Sequential, models, layers\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras import optimizers","metadata":{"id":"iVDwQYaNExBP","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nnum_of_classes = 10\n\nconvolutional_base = ResNet50(weights='imagenet', include_top=False, input_shape=(256, 256, 3))\n\n","metadata":{"id":"giNF_-ctGG7q","outputId":"15370752-6e2d-4abd-da57-699b1d9af484","execution":{"iopub.status.busy":"2024-07-23T09:41:17.300480Z","iopub.execute_input":"2024-07-23T09:41:17.301298Z","iopub.status.idle":"2024-07-23T09:41:18.402274Z","shell.execute_reply.started":"2024-07-23T09:41:17.301266Z","shell.execute_reply":"2024-07-23T09:41:18.401497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the model\nmodel = models.Sequential()\nmodel.add(layers.InputLayer(input_shape=(32, 32, 3)))  \nmodel.add(layers.UpSampling2D((2,2))) \nmodel.add(layers.UpSampling2D((2,2)))  \nmodel.add(layers.UpSampling2D((2,2)))  \nmodel.add(convolutional_base)           \nmodel.add(layers.Flatten())             \nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(128, activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(num_of_classes, activation='softmax'))\n","metadata":{"id":"z0JHpKGOHxWA","execution":{"iopub.status.busy":"2024-07-23T09:41:20.723220Z","iopub.execute_input":"2024-07-23T09:41:20.723587Z","iopub.status.idle":"2024-07-23T09:41:20.871029Z","shell.execute_reply.started":"2024-07-23T09:41:20.723547Z","shell.execute_reply":"2024-07-23T09:41:20.869869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = optimizers.RMSprop(learning_rate=2e-5)\nmodel.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n","metadata":{"id":"mHrpFL9EI40w","outputId":"0c4b56f2-a979-4b6f-e2a1-e7d2873a7b68","execution":{"iopub.status.busy":"2024-07-23T09:41:28.289968Z","iopub.execute_input":"2024-07-23T09:41:28.290312Z","iopub.status.idle":"2024-07-23T09:41:28.300475Z","shell.execute_reply.started":"2024-07-23T09:41:28.290285Z","shell.execute_reply":"2024-07-23T09:41:28.299624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-23T09:41:32.278459Z","iopub.execute_input":"2024-07-23T09:41:32.278902Z","iopub.status.idle":"2024-07-23T09:41:32.317886Z","shell.execute_reply.started":"2024-07-23T09:41:32.278868Z","shell.execute_reply":"2024-07-23T09:41:32.317011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train_scaled, Y_train, validation_split=0.1, epochs=10)","metadata":{"id":"Ym68j-ejLA5M","outputId":"bbfb2d82-be36-436b-d1a8-35504d8e34ec","execution":{"iopub.status.busy":"2024-07-23T09:41:38.961729Z","iopub.execute_input":"2024-07-23T09:41:38.962052Z","iopub.status.idle":"2024-07-23T10:44:32.387182Z","shell.execute_reply.started":"2024-07-23T09:41:38.962028Z","shell.execute_reply":"2024-07-23T10:44:32.386214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, accuracy = model.evaluate(X_test_scaled, Y_test)\nprint('Test Accuracy =', accuracy)","metadata":{"id":"jaGeZePUdj5W","outputId":"2af540d5-43e4-47f5-bd4d-15db2ad9a10a","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('CIFAR.h5')","metadata":{"id":"osOpuMXmd1vc","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as mp\nmp.figure(figsize=(12, 4))\n\nmp.subplot(1, 2, 1)\nmp.plot(history.history['accuracy'])\nmp.plot(history.history['val_accuracy'])\nmp.title('Model Accuracy')\nmp.ylabel('Accuracy')\nmp.xlabel('Epoch')\nmp.legend(['Train', 'Validation'], loc='upper left')\n\n# Plot training & validation loss values\nmp.subplot(1, 2, 2)\nmp.plot(history.history['loss'])\nmp.plot(history.history['val_loss'])\nmp.title('Model Loss')\nmp.ylabel('Loss')\nmp.xlabel('Epoch')\nmp.legend(['Train', 'Validation'], loc='upper left')\n\nmp.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-23T10:49:54.268011Z","iopub.execute_input":"2024-07-23T10:49:54.268947Z","iopub.status.idle":"2024-07-23T10:49:54.731054Z","shell.execute_reply.started":"2024-07-23T10:49:54.268902Z","shell.execute_reply":"2024-07-23T10:49:54.729962Z"},"trusted":true},"execution_count":null,"outputs":[]}]}