{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":91249,"databundleVersionId":11294684,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os \nimport pandas \nimport numpy as np\nimport pandas as pd\nfrom sklearn.linear_model import LinearRegression\nfrom PIL import Image \nimport torch\nimport torchvision.transforms as transforms\n\nimport torch.optim as optim\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, TensorDataset, DataLoader\nfrom torchvision.datasets import ImageFolder\nfrom torch.utils.data.dataset import random_split\nfrom torch.utils.tensorboard import SummaryWriter\nimport matplotlib.pyplot as plt\n%matplotlib inline\nplt.style.use('fivethirtyeight')\nprint('Done')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-03-22T15:52:48.135210Z","iopub.execute_input":"2025-03-22T15:52:48.135660Z","iopub.status.idle":"2025-03-22T15:52:56.061882Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"### baseline IDEA we need to create data and map each image to its \n### z slice and y x axis this will be our target ","metadata":{"trusted":true,"execution":{"execution_failed":"2025-03-22T15:52:44.094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_data=pd.read_csv('/kaggle/input/byu-locating-bacterial-flagellar-motors-2025/train_labels.csv')\nprint('Done')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T15:52:58.399974Z","iopub.execute_input":"2025-03-22T15:52:58.400347Z","iopub.status.idle":"2025-03-22T15:52:58.412204Z","shell.execute_reply.started":"2025-03-22T15:52:58.400320Z","shell.execute_reply":"2025-03-22T15:52:58.411008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_data.columns=[iterable.replace(' ','_') for iterable in labels_data.columns]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T15:53:00.919177Z","iopub.execute_input":"2025-03-22T15:53:00.919567Z","iopub.status.idle":"2025-03-22T15:53:00.925023Z","shell.execute_reply.started":"2025-03-22T15:53:00.919531Z","shell.execute_reply":"2025-03-22T15:53:00.923720Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T15:53:02.180268Z","iopub.execute_input":"2025-03-22T15:53:02.180658Z","iopub.status.idle":"2025-03-22T15:53:02.206242Z","shell.execute_reply.started":"2025-03-22T15:53:02.180624Z","shell.execute_reply":"2025-03-22T15:53:02.205021Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class ImageDataset(Dataset):\n    def __init__(self,dir_path,transform=None):\n        self.root_dir=dir_path\n        self.image_folders=[os.path.join(dir_path,image_folder_path) for image_folder_path in os.listdir(dir_path)]\n\n        self.transform=transform\n\n    def __len__(self):\n        return len(self.image_folders)\n\n\n    def __getitem__(self,idx):\n        image_paths = [os.path.join(self.image_folders[idx], actual_image) for actual_image in os.listdir(self.image_folders[idx])]\n        slices=[]\n        target_image=0\n        filtered_values = labels_data[labels_data['tomo_id'] == image_paths[0].split('/')[-2]]['Motor_axis_0']\n        if not filtered_values.empty:\n            image_target = filtered_values.values[0] \n        if (image_target!=-1.0):\n            image_target=labels_data[labels_data['tomo_id']==image_paths[0].split('/')[-2]]['Motor_axis_0']\n        target_image_path='slice_0'+str(image_target)+'.jpg'                  \n        for image_path in image_paths:\n            if image_path.split('/')[-1]==target_image_path:\n                target_image=Image.open(target_image_path)\n                if self.transform:\n                    target_image=self.transform(target_image)\n                \n\n            else:\n                image=Image.open(image_path)\n                if self.transform: \n                    image=self.transform(image)\n                slices.append(image)\n        three_d_image=torch.stack(slices,dim=-1)\n        \n\n        return three_d_image,target_image\n        \n        ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T16:04:29.189125Z","iopub.execute_input":"2025-03-22T16:04:29.189489Z","iopub.status.idle":"2025-03-22T16:04:29.199416Z","shell.execute_reply.started":"2025-03-22T16:04:29.189463Z","shell.execute_reply":"2025-03-22T16:04:29.197778Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.ToTensor(),\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T16:04:29.498978Z","iopub.execute_input":"2025-03-22T16:04:29.499340Z","iopub.status.idle":"2025-03-22T16:04:29.504629Z","shell.execute_reply.started":"2025-03-22T16:04:29.499308Z","shell.execute_reply":"2025-03-22T16:04:29.503198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Data_set_of_slices=ImageDataset('/kaggle/input/byu-locating-bacterial-flagellar-motors-2025/train',transform)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T16:04:29.892359Z","iopub.execute_input":"2025-03-22T16:04:29.892795Z","iopub.status.idle":"2025-03-22T16:04:29.907030Z","shell.execute_reply.started":"2025-03-22T16:04:29.892745Z","shell.execute_reply":"2025-03-22T16:04:29.905657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(Data_set_of_slices) ## now we can call the folders ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T16:04:31.922924Z","iopub.execute_input":"2025-03-22T16:04:31.923279Z","iopub.status.idle":"2025-03-22T16:04:31.929517Z","shell.execute_reply.started":"2025-03-22T16:04:31.923253Z","shell.execute_reply":"2025-03-22T16:04:31.928327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"first_image=Data_set_of_slices[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T16:04:33.503402Z","iopub.execute_input":"2025-03-22T16:04:33.503791Z","iopub.status.idle":"2025-03-22T16:04:42.249332Z","shell.execute_reply.started":"2025-03-22T16:04:33.503758Z","shell.execute_reply":"2025-03-22T16:04:42.248160Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T16:05:54.297525Z","iopub.execute_input":"2025-03-22T16:05:54.297966Z","iopub.status.idle":"2025-03-22T16:05:54.304573Z","shell.execute_reply.started":"2025-03-22T16:05:54.297929Z","shell.execute_reply":"2025-03-22T16:05:54.303496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}