{"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":"from itertools import groupby\nimport numpy as np\nfrom tqdm.notebook import tqdm\nimport pandas as pd\nimport os\nimport pickle\nimport cv2\nfrom multiprocessing import Pool\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import DataLoader, Dataset, sampler\nfrom albumentations import (HorizontalFlip, VerticalFlip, ShiftScaleRotate, Normalize, Resize, Compose, GaussNoise)\nfrom albumentations.pytorch import ToTensorV2\nimport random","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-20T07:09:46.445071Z","iopub.execute_input":"2021-10-20T07:09:46.445511Z","iopub.status.idle":"2021-10-20T07:09:50.039751Z","shell.execute_reply.started":"2021-10-20T07:09:46.445410Z","shell.execute_reply":"2021-10-20T07:09:50.038993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SAMPLE_SUBMISSION  = '../input/sartorius-cell-instance-segmentation/sample_submission.csv'\nTRAIN_CSV = \"../input/sartorius-cell-instance-segmentation/train.csv\"\nTRAIN_PATH = \"../input/sartorius-cell-instance-segmentation/train\"\nTEST_PATH = \"../input/sartorius-cell-instance-segmentation/test\"","metadata":{"execution":{"iopub.status.busy":"2021-10-20T07:09:50.041775Z","iopub.execute_input":"2021-10-20T07:09:50.042053Z","iopub.status.idle":"2021-10-20T07:09:50.046204Z","shell.execute_reply.started":"2021-10-20T07:09:50.042018Z","shell.execute_reply":"2021-10-20T07:09:50.045678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv(\"../input/sartorius-cell-instance-segmentation/train.csv\")\ntrain_path = \"../input/sartorius-cell-instance-segmentation/train\"\ntrain_csv.head()\nIMAGE_RESIZE = (704, 520)\nRESNET_MEAN = (0.485, 0.456, 0.406)\nRESNET_STD = (0.229, 0.224, 0.225)","metadata":{"execution":{"iopub.status.busy":"2021-10-20T07:09:50.048557Z","iopub.execute_input":"2021-10-20T07:09:50.048866Z","iopub.status.idle":"2021-10-20T07:09:50.615541Z","shell.execute_reply.started":"2021-10-20T07:09:50.048840Z","shell.execute_reply":"2021-10-20T07:09:50.614914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(mask_rle, color=1):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n    \n    '''\n    shape = [520, 704]\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0] * shape[1], dtype=np.float32)\n    for lo, hi in zip(starts, ends):\n        img[lo : hi] = color\n    return img.reshape(shape)\n\ndef build_masks(df_train, image_id):\n    input_shape = (520, 704)\n    height, width = input_shape\n    labels = df_train[df_train[\"id\"] == image_id][\"annotation\"].tolist()\n    mask = np.zeros((height, width))\n    for label in labels:\n        mask += rle_decode(label)\n    mask = mask.clip(0, 1)\n    return mask","metadata":{"execution":{"iopub.status.busy":"2021-10-20T07:09:50.617107Z","iopub.execute_input":"2021-10-20T07:09:50.617444Z","iopub.status.idle":"2021-10-20T07:09:50.626196Z","shell.execute_reply.started":"2021-10-20T07:09:50.617416Z","shell.execute_reply":"2021-10-20T07:09:50.625548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CellDataset(Dataset):\n    def __init__(self, df):\n        self.df = df\n        self.base_path = TRAIN_PATH\n        self.transforms = Compose([Resize(IMAGE_RESIZE[0], IMAGE_RESIZE[1]), \n                                   Normalize(mean=RESNET_MEAN, std=RESNET_STD, p=1), \n                                   HorizontalFlip(p=0.5),\n                                   VerticalFlip(p=0.5),\n                                   ToTensorV2()])\n        self.gb = self.df.groupby('id')\n        self.image_ids = df.id.unique().tolist()\n\n    def __getitem__(self, idx):\n        image_id = self.image_ids[idx]\n        df = self.gb.get_group(image_id)\n        annotations = df['annotation'].tolist()\n        image_path = os.path.join(self.base_path, image_id + \".png\")\n        image = cv2.imread(image_path)\n        mask = build_masks(train_csv ,image_id)\n        mask = (mask >= 1).astype('float32')\n        augmented = self.transforms(image=image, mask=mask)\n        image = augmented['image']\n        mask = augmented['mask']\n        return image, mask.reshape((1, IMAGE_RESIZE[0], IMAGE_RESIZE[1]))\n\n    def __len__(self):\n        return len(self.image_ids)","metadata":{"execution":{"iopub.status.busy":"2021-10-20T07:09:50.627359Z","iopub.execute_input":"2021-10-20T07:09:50.627701Z","iopub.status.idle":"2021-10-20T07:09:50.639673Z","shell.execute_reply.started":"2021-10-20T07:09:50.627658Z","shell.execute_reply":"2021-10-20T07:09:50.638795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_train = CellDataset(train_csv)\nimage, mask = ds_train[1]\nimage.shape, mask.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-20T07:09:50.640965Z","iopub.execute_input":"2021-10-20T07:09:50.641265Z","iopub.status.idle":"2021-10-20T07:09:50.829309Z","shell.execute_reply.started":"2021-10-20T07:09:50.641237Z","shell.execute_reply":"2021-10-20T07:09:50.828480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img_plot(i):\n    image, mask = ds_train[i]\n    plt.imshow(image[0], cmap='bone', aspect = 'auto')\n    plt.show()\n    plt.imshow(mask[0], alpha=0.3, aspect = 'auto')\n    plt.show()\n    plt.imshow(image[0], cmap='bone', aspect = 'auto')\n    plt.imshow(mask[0], alpha=0.3, aspect = 'auto')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-20T07:09:50.830728Z","iopub.execute_input":"2021-10-20T07:09:50.831115Z","iopub.status.idle":"2021-10-20T07:09:50.836508Z","shell.execute_reply.started":"2021-10-20T07:09:50.831088Z","shell.execute_reply":"2021-10-20T07:09:50.835822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n = random.randint(0,606)\nimg_plot(n)","metadata":{"execution":{"iopub.status.busy":"2021-10-20T07:09:50.837445Z","iopub.execute_input":"2021-10-20T07:09:50.838081Z","iopub.status.idle":"2021-10-20T07:09:52.123110Z","shell.execute_reply.started":"2021-10-20T07:09:50.838051Z","shell.execute_reply":"2021-10-20T07:09:52.122285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}