{"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-10-03T10:17:38.056475Z","iopub.execute_input":"2023-10-03T10:17:38.056823Z","iopub.status.idle":"2023-10-03T10:17:38.368311Z","shell.execute_reply.started":"2023-10-03T10:17:38.056796Z","shell.execute_reply":"2023-10-03T10:17:38.367324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:39:24.258590Z","iopub.execute_input":"2023-10-03T08:39:24.258985Z","iopub.status.idle":"2023-10-03T08:39:38.597875Z","shell.execute_reply.started":"2023-10-03T08:39:24.258956Z","shell.execute_reply":"2023-10-03T08:39:38.596775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract zip file into folder containing image files\nimport py7zr\n\nwith py7zr.SevenZipFile('/kaggle/input/cifar-10/test.7z', mode='r') as archive:\n    archive.extractall(path='/kaggle/working/test_data_folder')\n\nwith py7zr.SevenZipFile('/kaggle/input/cifar-10/train.7z', mode='r') as archive:\n    archive.extractall(path='/kaggle/working/train_data_folder')","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:39:38.599709Z","iopub.execute_input":"2023-10-03T08:39:38.600044Z","iopub.status.idle":"2023-10-03T08:53:20.159202Z","shell.execute_reply.started":"2023-10-03T08:39:38.600009Z","shell.execute_reply":"2023-10-03T08:53:20.158269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load train labels\ntrain_labels_df = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\ntrain_labels_df","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:18:27.251739Z","iopub.execute_input":"2023-10-03T10:18:27.252088Z","iopub.status.idle":"2023-10-03T10:18:27.308967Z","shell.execute_reply.started":"2023-10-03T10:18:27.252060Z","shell.execute_reply":"2023-10-03T10:18:27.307944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocess the Data\nfrom sklearn.preprocessing import OneHotEncoder\nfrom PIL import Image\ndef load_data(folder_path, labels_df=None):\n    images = []\n    labels = []\n    img_ids = []\n    files = os.listdir(folder_path)\n    sorted_files = sorted(files, key=lambda x: int(os.path.splitext(x)[0]))    \n    for filename in sorted_files:\n        img_path = os.path.join(folder_path, filename)\n        img = Image.open(img_path)\n        images.append(np.array(img))   \n        img_id = int(filename.split('.')[0])  # Parse image ID from filename\n        img_ids.append(img_id)  # Store the parsed ID\n        if labels_df is not None:\n            img_id = int(filename.split('.')[0])\n            label = labels_df[labels_df['id'] == img_id]['label'].values[0]\n            labels.append(label)            \n    return np.array(images), labels\n\nX_train, y_train = load_data(\"/kaggle/working/train_data_folder/train\", train_labels_df)\nX_test, _ = load_data(\"/kaggle/working/test_data_folder/test\") \n\n# Normalize images\nX_train = X_train / 255.0\nX_test = X_test / 255.0\n\n# One-hot encode the labels\nencoder = OneHotEncoder(sparse=False)\ny_train_encoded = encoder.fit_transform(np.array(y_train).reshape(-1, 1))\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:18:29.220290Z","iopub.execute_input":"2023-10-03T10:18:29.220643Z","iopub.status.idle":"2023-10-03T10:21:11.495572Z","shell.execute_reply.started":"2023-10-03T10:18:29.220606Z","shell.execute_reply":"2023-10-03T10:21:11.494564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build the MLP Model in PyTorch\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nclass MLP(nn.Module):\n    def __init__(self):\n        super(MLP, self).__init__()\n        self.fc1 = nn.Linear(32*32*3, 512)\n        self.fc2 = nn.Linear(512, 256)\n        self.fc3 = nn.Linear(256, 128)\n        self.fc4 = nn.Linear(128, 10)\n\n    def forward(self, x):\n        x = x.view(-1, 32*32*3)  # Flatten the data\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = F.relu(self.fc3(x))\n        x = self.fc4(x)\n        return F.softmax(x, dim=1)\n\nmodel = MLP()\nprint(model)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:23:58.906495Z","iopub.execute_input":"2023-10-03T10:23:58.906902Z","iopub.status.idle":"2023-10-03T10:23:58.930259Z","shell.execute_reply.started":"2023-10-03T10:23:58.906872Z","shell.execute_reply":"2023-10-03T10:23:58.929077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom IPython import display\n\n# Initial setup for the plot\nplt.figure(figsize=(10, 5))\nplt.title('Training Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.grid(True)\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=0.001)\n\n# Convert data to PyTorch tensors\nX_train_tensor = torch.tensor(X_train, dtype=torch.float32)\ny_train_tensor = torch.tensor(np.argmax(y_train_encoded, axis=1), dtype=torch.long)  # Convert one-hot to indices\n\n# Training loop\nloss_values = []  # To store loss values\nnum_epochs = 20\nfor epoch in range(num_epochs):\n    optimizer.zero_grad()\n    outputs = model(X_train_tensor)\n    loss = criterion(outputs, y_train_tensor)\n    loss.backward()\n    optimizer.step()\n\n    loss_values.append(loss.item())\n    \n    # Dynamic plotting\n    display.clear_output(wait=True)\n    plt.plot(loss_values, label='Training Loss')\n    plt.legend()\n    plt.grid(True)\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.title('Training Loss over Time')\n    plt.pause(0.05)\n    \n    print(f\"Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}\")\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:24:01.739562Z","iopub.execute_input":"2023-10-03T10:24:01.739913Z","iopub.status.idle":"2023-10-03T10:25:10.182476Z","shell.execute_reply.started":"2023-10-03T10:24:01.739886Z","shell.execute_reply":"2023-10-03T10:25:10.181298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_tensor = torch.tensor(X_test, dtype=torch.float32)\npredictions = model(X_test_tensor)\n_, predicted_labels = torch.max(predictions, 1)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:33:57.757064Z","iopub.execute_input":"2023-10-03T10:33:57.757788Z","iopub.status.idle":"2023-10-03T10:33:57.992545Z","shell.execute_reply.started":"2023-10-03T10:33:57.757753Z","shell.execute_reply":"2023-10-03T10:33:57.991184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_label_strings = encoder.inverse_transform(predicted_labels.numpy().reshape(-1, 1))\n\nsubmission = pd.DataFrame(data={\n    \"id\": test_img_ids,\n    \"label\": predicted_label_strings.ravel()\n})\n\nsubmission.to_csv(\"submission.csv\", index=None)\n","metadata":{},"execution_count":null,"outputs":[]}]}