{"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":"gpu","dataSources":[{"sourceId":77376,"databundleVersionId":8436622,"sourceType":"competition"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preparing your pytorch custom dataset.\nimport os\nimport torch\nfrom torchvision import transforms\nfrom torch.utils.data import Dataset, DataLoader\nfrom PIL import Image\nfrom torch import nn, optim\n\n\nclass ImageDataset(Dataset):\n    def __init__(self, img_dic, transform=None):\n        self.img_dic = img_dic\n        self.transform = transform\n\n        self.img_names = list(img_dic.keys())\n        self.labels = list(img_dic.values())\n\n    def __getitem__(self, index):\n        img = Image.open(self.img_names[index])\n        label = self.labels[index]\n        img = self.transform(img)\n        return img, label\n\n    def __len__(self):\n        return len(self.img_names)\n\ndef get_img_dic(csv_file, img_folder):\n    img_dic = {}\n    with open(csv_file) as label_file:\n        lines = label_file.readlines()\n    for line in lines[1:]:\n        items = line.strip().split(',')\n        if len(items) == 3:\n            _, img_name, label = items\n        else:\n            _, img_name = items\n            label = -1\n        img_dic[os.path.join(img_folder, img_name)] = int(label)\n    return img_dic\n","metadata":{"execution":{"iopub.status.busy":"2024-08-07T14:34:33.753033Z","iopub.execute_input":"2024-08-07T14:34:33.753874Z","iopub.status.idle":"2024-08-07T14:34:38.437089Z","shell.execute_reply.started":"2024-08-07T14:34:33.753837Z","shell.execute_reply":"2024-08-07T14:34:38.436158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5),     # Normalize the image with mean and std for each channel\n                         (0.5, 0.5, 0.5))])\n\ntrain_csv = '/kaggle/input/d-4-bootcamp-dnn/train.csv'\ntrain_img_dic = get_img_dic(train_csv, '/kaggle/input/d-4-bootcamp-dnn/train/train')\ntrain_dataset = ImageDataset(train_img_dic, transform=transform)\n\ntest_csv = '/kaggle/input/d-4-bootcamp-dnn/test.csv'\ntest_img_dic = get_img_dic(test_csv, '/kaggle/input/d-4-bootcamp-dnn/test/test')\ntest_dataset = ImageDataset(test_img_dic, transform=transform)\n\ntrain_dataloader = DataLoader(train_dataset, batch_size=32, shuffle=True)\ntest_dataloader = DataLoader(test_dataset, batch_size=32, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T14:34:43.722933Z","iopub.execute_input":"2024-08-07T14:34:43.723425Z","iopub.status.idle":"2024-08-07T14:34:43.962935Z","shell.execute_reply.started":"2024-08-07T14:34:43.723388Z","shell.execute_reply":"2024-08-07T14:34:43.962162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ThreeLayerNN(nn.Module):\n    def __init__(self, input_size, hidden1_size, hidden2_size, num_classes):\n        super(ThreeLayerNN, self).__init__()\n        self.fc1 = nn.Linear(input_size, hidden1_size)\n        self.relu1 = nn.ReLU()\n        self.fc2 = nn.Linear(hidden1_size, hidden2_size)\n        self.relu2 = nn.ReLU()\n        self.fc3 = nn.Linear(hidden2_size, num_classes)\n        self.softmax = nn.Softmax(dim=1)\n        self.bn1 = nn.BatchNorm1d(input_size)\n        self.bn2 = nn.BatchNorm1d(hidden1_size)\n        self.bn3 = nn.BatchNorm1d(hidden2_size)\n\n    def forward(self, x):\n        x = x.flatten(1)\n        x = self.bn1(x)\n        out = self.fc1(x)\n        out = self.relu1(out)\n        out = self.bn2(out)\n        out = self.fc2(out)\n        out = self.relu2(out)\n        out = self.bn3(out)\n        out = self.fc3(out)\n        return out\n    \ninput_size = 32 * 32 * 3  # Example for 128x128 RGB images\nhidden1_size = 1024\nhidden2_size = 1024\nnum_classes = 10  # Example number of classes\n\n# Instantiate the model, define the loss function and the optimizer\nmodel = ThreeLayerNN(input_size, hidden1_size, hidden2_size, num_classes)\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# Example forward pass with random input data\nexample_data = torch.randn(2, input_size)  # Batch size of 1\noutput = model(example_data)\nprint(output)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T14:35:52.723486Z","iopub.execute_input":"2024-08-07T14:35:52.724300Z","iopub.status.idle":"2024-08-07T14:35:52.869249Z","shell.execute_reply.started":"2024-08-07T14:35:52.724259Z","shell.execute_reply":"2024-08-07T14:35:52.868198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nmodel.to(device)\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T14:36:02.528588Z","iopub.execute_input":"2024-08-07T14:36:02.528918Z","iopub.status.idle":"2024-08-07T14:36:02.704785Z","shell.execute_reply.started":"2024-08-07T14:36:02.528893Z","shell.execute_reply":"2024-08-07T14:36:02.703872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\nfor epoch in range(15):\n    for images, labels in tqdm(train_dataloader):\n        # Flatten the images for the fully connected layer\n        images = images.view(images.size(0), -1)\n        images = images.to(device)\n        labels = labels.to(device)\n        # Forward pass\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n        \n        # Backward pass and optimization\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T14:27:26.948698Z","iopub.status.idle":"2024-08-07T14:27:26.949119Z","shell.execute_reply.started":"2024-08-07T14:27:26.948891Z","shell.execute_reply":"2024-08-07T14:27:26.948906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\nmodel.eval()\n    \npredictions = []\n\nfor images, labels in tqdm(test_dataloader):\n    images = images.view(images.size(0), -1)\n    images = images.to(device)\n    with torch.no_grad():        \n        outputs = model(images)\n\n    logits = outputs\n    logits = logits.detach().cpu().numpy()\n    predictions.append(logits)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T14:27:26.951176Z","iopub.status.idle":"2024-08-07T14:27:26.951555Z","shell.execute_reply.started":"2024-08-07T14:27:26.951388Z","shell.execute_reply":"2024-08-07T14:27:26.951404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\npredictions = np.concatenate(predictions, axis=0)\npreds_flat = np.argmax(predictions, axis=1).flatten()\nprint(preds_flat)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T14:27:26.952450Z","iopub.status.idle":"2024-08-07T14:27:26.952811Z","shell.execute_reply.started":"2024-08-07T14:27:26.952600Z","shell.execute_reply":"2024-08-07T14:27:26.952612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\nresult_df = pd.DataFrame({'prediction': preds_flat})\nresult_df = result_df.reset_index().rename(columns={'index': 'ID'})\n# result_df.set_index('ID', inplace=True)\nresult_df.to_csv('submission.csv', index=False)\nprint(result_df)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T14:27:26.953915Z","iopub.status.idle":"2024-08-07T14:27:26.954331Z","shell.execute_reply.started":"2024-08-07T14:27:26.954140Z","shell.execute_reply":"2024-08-07T14:27:26.954161Z"},"trusted":true},"execution_count":null,"outputs":[]}]}