{"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":"import numpy as np \nimport pandas as pd\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nimport torch.nn as nn\nimport torch.utils.data as Dataset","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-16T17:31:13.025951Z","iopub.execute_input":"2021-12-16T17:31:13.026628Z","iopub.status.idle":"2021-12-16T17:31:13.031883Z","shell.execute_reply.started":"2021-12-16T17:31:13.026594Z","shell.execute_reply":"2021-12-16T17:31:13.030928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SEED = 2021\ndef seedTorch(seed=SEED):\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    \nseedTorch()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T17:31:13.071352Z","iopub.execute_input":"2021-12-16T17:31:13.071765Z","iopub.status.idle":"2021-12-16T17:31:13.081355Z","shell.execute_reply.started":"2021-12-16T17:31:13.071668Z","shell.execute_reply":"2021-12-16T17:31:13.080358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_PATH = \"../input/recursion-cellular-image-classification\"\nTRAIN_PATH  = \"../input/recursion-cellular-image-classification/train.csv\"\nTEST_PATH = \"../input/recursion-cellular-image-classification/test.csv\"\nTRAIN_IMAGE_PATH = \"../input/recursion-cellular-image-classification/train\"\nTEST_IMAGE_PATH = \"../input/recursion-cellular-image-classification/test\"\n\nTEST_SIZE = 0.25\n\ndevice = 'cuda'\nBATCH_SIZE = 32\nCHANNELS = [1,2,3,4,5,6]","metadata":{"execution":{"iopub.status.busy":"2021-12-16T17:31:13.083691Z","iopub.execute_input":"2021-12-16T17:31:13.085577Z","iopub.status.idle":"2021-12-16T17:31:13.092707Z","shell.execute_reply.started":"2021-12-16T17:31:13.085544Z","shell.execute_reply":"2021-12-16T17:31:13.091465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_val = pd.read_csv(TRAIN_PATH)\ntrain, val = train_test_split(train_val, test_size = TEST_SIZE, random_state=SEED)\ntest = pd.read_csv(TEST_PATH)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T17:31:13.094444Z","iopub.execute_input":"2021-12-16T17:31:13.095056Z","iopub.status.idle":"2021-12-16T17:31:13.176750Z","shell.execute_reply.started":"2021-12-16T17:31:13.094989Z","shell.execute_reply":"2021-12-16T17:31:13.175739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"sample image path : ../input/recursion-cellular-image-classification/test/HEPG2-08/Plate1/B02_s1_w1.png","metadata":{}},{"cell_type":"code","source":"class ImagesDS(Dataset.Dataset):\n    def __init__(self, df, img_dir, mode='train'):\n        self.records = df.to_records(index=False)\n        self.channels = CHANNELS\n        self.site = 1\n        self.mode = mode\n        self.img_dir = img_dir\n        self.len = df.shape[0]\n        \n    @staticmethod\n    def _load_img_as_tensor(file_name):\n        with Image.open(file_name) as img:\n            return T.ToTensor()(img)\n\n    def _get_img_path(self, index, channel):\n        experiment, well, plate = self.records[index].experiment, self.records[index].well, self.records[index].plate\n        return '/'.join([self.img_dir,self.mode,experiment,f'Plate{plate}',f'{well}_s{self.site}_w{channel}.png'])\n        \n    def __getitem__(self, index):\n        paths = [self._get_img_path(index, ch) for ch in self.channels]\n        img = torch.cat([self._load_img_as_tensor(img_path) for img_path in paths])\n        if self.mode == 'train':\n            return img, int(self.records[index].sirna)\n        else:\n            return img, self.records[index].id_code\n\n    def __len__(self):\n        return self.len","metadata":{"execution":{"iopub.status.busy":"2021-12-16T17:31:13.181210Z","iopub.execute_input":"2021-12-16T17:31:13.182069Z","iopub.status.idle":"2021-12-16T17:31:13.194409Z","shell.execute_reply.started":"2021-12-16T17:31:13.181873Z","shell.execute_reply":"2021-12-16T17:31:13.193194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# make image dataset","metadata":{}},{"cell_type":"code","source":"train_ds = ImagesDS(train, ROOT_PATH, mode='train')\nval_ds = ImagesDS(val, ROOT_PATH, mode='train')\ntest_ds = ImagesDS(test, ROOT_PATH, mode='test')","metadata":{"execution":{"iopub.status.busy":"2021-12-16T17:31:13.195809Z","iopub.execute_input":"2021-12-16T17:31:13.196307Z","iopub.status.idle":"2021-12-16T17:31:13.232864Z","shell.execute_reply.started":"2021-12-16T17:31:13.196248Z","shell.execute_reply":"2021-12-16T17:31:13.232051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"DataLoader(dataset, batch_size=1, shuffle=False, sampler=None,\\\n           batch_sampler=None, num_workers=0, collate_fn=None,\\\n           pin_memory=False, drop_last=False, timeout=0,\\\n           worker_init_fn=None, *, prefetch_factor=2,\\\n           persistent_workers=False)","metadata":{}},{"cell_type":"markdown","source":"# make loader","metadata":{}},{"cell_type":"code","source":"train_loader = Dataset.DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True)\nval_loader = Dataset.DataLoader(val_ds, batch_size=BATCH_SIZE, shuffle=True)\ntest_loader = Dataset.DataLoader(test_ds, batch_size=BATCH_SIZE, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T17:31:13.234396Z","iopub.execute_input":"2021-12-16T17:31:13.234664Z","iopub.status.idle":"2021-12-16T17:31:13.267096Z","shell.execute_reply.started":"2021-12-16T17:31:13.234624Z","shell.execute_reply":"2021-12-16T17:31:13.265857Z"},"trusted":true},"execution_count":null,"outputs":[]}]}