{"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":"gpu","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":1807973,"sourceType":"datasetVersion","datasetId":1074109},{"sourceId":150248402,"sourceType":"kernelVersion"}],"dockerImageVersionId":30587,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!python -m pip install --no-index --find-links=/kaggle/input/pip-download-for-segmentation-models-pytorch segmentation-models-pytorch\n!mkdir -p /root/.cache/torch/hub/checkpoints/\n!cp /kaggle/input/se-net-pretrained-imagenet-weights/* /root/.cache/torch/hub/checkpoints/","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:54:39.752799Z","iopub.execute_input":"2023-12-18T03:54:39.753767Z","iopub.status.idle":"2023-12-18T03:55:13.477374Z","shell.execute_reply.started":"2023-12-18T03:54:39.753736Z","shell.execute_reply":"2023-12-18T03:55:13.476283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport pandas as pd\nimport os\nimport torch\nfrom torch import nn\nfrom torchvision import transforms\nimport matplotlib.pyplot as plt\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport os\nimport gc\nimport torch\nfrom torch import nn\nfrom torch import optim\nimport torchvision\nfrom torchvision import transforms\nimport matplotlib.pyplot as plt\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nfrom torch.utils.data import Dataset, DataLoader\nimport segmentation_models_pytorch as smp\nfrom tqdm import tqdm ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-18T03:55:13.479665Z","iopub.execute_input":"2023-12-18T03:55:13.480051Z","iopub.status.idle":"2023-12-18T03:55:20.816614Z","shell.execute_reply.started":"2023-12-18T03:55:13.480015Z","shell.execute_reply":"2023-12-18T03:55:20.815633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## process the csv file ","metadata":{}},{"cell_type":"code","source":"os.listdir('/kaggle/input/blood-vessel-segmentation/train')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:20.817955Z","iopub.execute_input":"2023-12-18T03:55:20.818329Z","iopub.status.idle":"2023-12-18T03:55:20.827498Z","shell.execute_reply.started":"2023-12-18T03:55:20.818296Z","shell.execute_reply":"2023-12-18T03:55:20.826629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/blood-vessel-segmentation/train_rles.csv')\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:20.829475Z","iopub.execute_input":"2023-12-18T03:55:20.830206Z","iopub.status.idle":"2023-12-18T03:55:21.954753Z","shell.execute_reply.started":"2023-12-18T03:55:20.830181Z","shell.execute_reply":"2023-12-18T03:55:21.953771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[['dir_name', 'img_name']] = train['id'].str.rsplit('_', n = 1, expand=True)\ntrain['img_name'] = train['img_name'] + '.tif'\ntrain['img_path'] = '/kaggle/input/blood-vessel-segmentation/train/' + train['dir_name'] + '/images/' + train['img_name']\ntrain['label_path'] = '/kaggle/input/blood-vessel-segmentation/train/' + train['dir_name'] + '/labels/' + train['img_name']\ntrain.loc[train['dir_name'] == 'kidney_3_dense', 'img_path'] = '/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse/images/' + train.loc[train['dir_name'] == 'kidney_3_dense', 'img_name']\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:21.956097Z","iopub.execute_input":"2023-12-18T03:55:21.956507Z","iopub.status.idle":"2023-12-18T03:55:22.006272Z","shell.execute_reply.started":"2023-12-18T03:55:21.956439Z","shell.execute_reply":"2023-12-18T03:55:22.005378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## init the hyperparameter and some setting","metadata":{}},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\nepoches = 10","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:22.007476Z","iopub.execute_input":"2023-12-18T03:55:22.008220Z","iopub.status.idle":"2023-12-18T03:55:22.034520Z","shell.execute_reply.started":"2023-12-18T03:55:22.008187Z","shell.execute_reply":"2023-12-18T03:55:22.033653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transform = A.Compose(\n#     [\n#         A.Rotate(limit=180, p=0.5), \n#         A.RandomCrop(512, 512, p=1), \n#         ToTensorV2(),\n#     ]\n# )\n\ntransform = A.Compose(\n    [\n        A.Resize(height=512, width=512),\n        A.HorizontalFlip(p=0.5),\n        ToTensorV2()\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:22.035503Z","iopub.execute_input":"2023-12-18T03:55:22.035791Z","iopub.status.idle":"2023-12-18T03:55:22.052010Z","shell.execute_reply.started":"2023-12-18T03:55:22.035768Z","shell.execute_reply":"2023-12-18T03:55:22.051234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## init the dataset ","metadata":{}},{"cell_type":"code","source":"class kidney_dataset(Dataset):\n    def __init__(self, df, transform=ToTensorV2()):\n        self.df = df\n        self.transform = transform\n    \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        image = cv2.imread(self.df.loc[idx, 'img_path'])\n        mask = cv2.imread(self.df.loc[idx, 'label_path'])\n        mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)\n        \n        image, mask = image.astype('float32'), mask.astype('float32')\n        image = (image - np.min(image)) / (np.max(image) - np.min(image) + 0.001)\n        mask = mask / 255\n        if self.transform:\n            data = self.transform(image=image, mask=mask)\n        else:\n            data = ToTensorV2(image=image, mask=mask)\n        image_arg, mask_arg = data['image'], data['mask']\n        return image_arg, mask_arg","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:22.053045Z","iopub.execute_input":"2023-12-18T03:55:22.053276Z","iopub.status.idle":"2023-12-18T03:55:22.068161Z","shell.execute_reply.started":"2023-12-18T03:55:22.053256Z","shell.execute_reply":"2023-12-18T03:55:22.067364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = kidney_dataset(train, transform=transform)\ntrain_dataset[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:22.069227Z","iopub.execute_input":"2023-12-18T03:55:22.069843Z","iopub.status.idle":"2023-12-18T03:55:22.262297Z","shell.execute_reply.started":"2023-12-18T03:55:22.069810Z","shell.execute_reply":"2023-12-18T03:55:22.261366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## use the model in smp ","metadata":{}},{"cell_type":"code","source":"model = smp.Unet(\n#     encoder_name = 'efficientnet-b1',\n    encoder_name = 'se_resnet50',\n    encoder_weights = 'imagenet',\n    classes =  1,\n    activation = None\n)\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:22.267054Z","iopub.execute_input":"2023-12-18T03:55:22.267352Z","iopub.status.idle":"2023-12-18T03:55:25.422473Z","shell.execute_reply.started":"2023-12-18T03:55:22.267327Z","shell.execute_reply":"2023-12-18T03:55:25.421633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model(torch.ones(1, 3, 256, 256).to(device)).shape","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:25.423522Z","iopub.execute_input":"2023-12-18T03:55:25.423788Z","iopub.status.idle":"2023-12-18T03:55:30.010276Z","shell.execute_reply.started":"2023-12-18T03:55:25.423766Z","shell.execute_reply":"2023-12-18T03:55:30.009290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## init the loss function and optimizer ","metadata":{}},{"cell_type":"code","source":"diceloss = smp.losses.DiceLoss(mode='binary')\noptimizer = optim.Adam(model.parameters(), lr=1e-4) ","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:30.011330Z","iopub.execute_input":"2023-12-18T03:55:30.011631Z","iopub.status.idle":"2023-12-18T03:55:30.017704Z","shell.execute_reply.started":"2023-12-18T03:55:30.011605Z","shell.execute_reply":"2023-12-18T03:55:30.016814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## init the train_dataset and data_loader \n## split the dataset for training and valid\nkidney_2 for valid","metadata":{}},{"cell_type":"code","source":"train['dir_name'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:30.019185Z","iopub.execute_input":"2023-12-18T03:55:30.019450Z","iopub.status.idle":"2023-12-18T03:55:30.056149Z","shell.execute_reply.started":"2023-12-18T03:55:30.019427Z","shell.execute_reply":"2023-12-18T03:55:30.055420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train.loc[train['dir_name'] != 'kidney_2']\ntrain_df = train_df.reset_index()\nvalid_df = train.loc[train['dir_name'] == 'kidney_2']\nvalid_df = valid_df.reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:30.057190Z","iopub.execute_input":"2023-12-18T03:55:30.057941Z","iopub.status.idle":"2023-12-18T03:55:30.072286Z","shell.execute_reply.started":"2023-12-18T03:55:30.057910Z","shell.execute_reply":"2023-12-18T03:55:30.071436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset, valid_dataset = kidney_dataset(train_df, transform=transform), kidney_dataset(valid_df, transform=transform)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:30.073489Z","iopub.execute_input":"2023-12-18T03:55:30.073764Z","iopub.status.idle":"2023-12-18T03:55:30.078644Z","shell.execute_reply.started":"2023-12-18T03:55:30.073741Z","shell.execute_reply":"2023-12-18T03:55:30.077443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_dataset, batch_size=8, num_workers=2, pin_memory=True)\nvalid_loader = DataLoader(valid_dataset, batch_size=8, num_workers=2, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:30.079703Z","iopub.execute_input":"2023-12-18T03:55:30.080000Z","iopub.status.idle":"2023-12-18T03:55:30.089843Z","shell.execute_reply.started":"2023-12-18T03:55:30.079953Z","shell.execute_reply":"2023-12-18T03:55:30.089060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:30.091150Z","iopub.execute_input":"2023-12-18T03:55:30.091500Z","iopub.status.idle":"2023-12-18T03:55:30.112723Z","shell.execute_reply.started":"2023-12-18T03:55:30.091445Z","shell.execute_reply":"2023-12-18T03:55:30.111797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# i = 0\n# for data in train_loader:\n#     img, label = data\n#     img, label = img.to(device), label.to(device)\n# #     print(img.shape, label.shape)\n#     output = model(img)\n#     print(i, diceloss(output, label))\n#     if i == 200:\n#         break\n#     else:\n#         i += 1","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:30.113849Z","iopub.execute_input":"2023-12-18T03:55:30.114175Z","iopub.status.idle":"2023-12-18T03:55:30.122661Z","shell.execute_reply.started":"2023-12-18T03:55:30.114145Z","shell.execute_reply":"2023-12-18T03:55:30.121746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## start to train","metadata":{}},{"cell_type":"code","source":"for epoch in range(epoches):\n    model.train()\n    total_training_loss = 0\n    print(f'{epoch + 1} training start')\n    for training_data in tqdm(train_loader):\n        training_image, training_mask = training_data\n        training_image, training_mask = training_image.to(device), training_mask.to(device) \n        training_output = model(training_image)\n        training_loss = diceloss(training_output, training_mask)\n        total_training_loss += training_loss\n        optimizer.zero_grad()\n        training_loss.backward()\n        optimizer.step()\n    print(total_training_loss)\n    \n    torch.cuda.empty_cache()\n    gc.collect()\n    \n    \n    with torch.no_grad():\n        total_valid_loss = 0\n        print(f'{epoch + 1} valid_start')\n        for valid_data in tqdm(valid_loader):\n            valid_image, valid_mask = valid_data\n            valid_image, valid_mask = valid_image.to(device), valid_mask.to(device)\n            valid_output = model(valid_image)\n            valid_loss = diceloss(valid_output, valid_mask)\n            total_valid_loss += valid_loss\n        print(total_valid_loss)\n        gc.collect()\n        torch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:30.123741Z","iopub.execute_input":"2023-12-18T03:55:30.123987Z","iopub.status.idle":"2023-12-18T03:55:54.724277Z","shell.execute_reply.started":"2023-12-18T03:55:30.123967Z","shell.execute_reply":"2023-12-18T03:55:54.721679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/blood-vessel-segmentation/sample_submission.csv')\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.725576Z","iopub.status.idle":"2023-12-18T03:55:54.726042Z","shell.execute_reply.started":"2023-12-18T03:55:54.725812Z","shell.execute_reply":"2023-12-18T03:55:54.725833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    rle = ' '.join(str(x) for x in runs)\n    if rle=='':\n        rle = '1 0'\n    return rle","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.727790Z","iopub.status.idle":"2023-12-18T03:55:54.728239Z","shell.execute_reply.started":"2023-12-18T03:55:54.728018Z","shell.execute_reply":"2023-12-18T03:55:54.728038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def remove_small_objects(img, min_size):\n    # Find all connected components (labels)\n    num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(img, connectivity=8)\n\n    # Create a mask where small objects are removed\n    new_img = np.zeros_like(img)\n    for label in range(1, num_labels):\n        if stats[label, cv2.CC_STAT_AREA] >= min_size:\n            new_img[labels == label] = 255\n\n    return new_img\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.729572Z","iopub.status.idle":"2023-12-18T03:55:54.729994Z","shell.execute_reply.started":"2023-12-18T03:55:54.729785Z","shell.execute_reply":"2023-12-18T03:55:54.729804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = np.ones((1303, 912, 1), dtype='float32')\nprint(a.dtype, type(a), a.shape)\nrle_encode(a)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.733420Z","iopub.status.idle":"2023-12-18T03:55:54.733744Z","shell.execute_reply.started":"2023-12-18T03:55:54.733593Z","shell.execute_reply":"2023-12-18T03:55:54.733608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path_list = []\nfor root_dir, dir_name, img_names in os.walk('/kaggle/input/blood-vessel-segmentation/test'):\n    if 'kidney' in root_dir:\n        for img in img_names:\n            img_path = os.path.join(root_dir, img)\n            img_path_list.append(img_path)\nimg_path_list","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.735947Z","iopub.status.idle":"2023-12-18T03:55:54.736834Z","shell.execute_reply.started":"2023-12-18T03:55:54.736589Z","shell.execute_reply":"2023-12-18T03:55:54.736612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_transform = A.Compose([\n    A.Resize(height=512, width=512),\n    ToTensorV2()\n])\nre_transform = A.Resize(height=1303, width=912)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.738070Z","iopub.status.idle":"2023-12-18T03:55:54.739173Z","shell.execute_reply.started":"2023-12-18T03:55:54.738911Z","shell.execute_reply":"2023-12-18T03:55:54.738941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path_list","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.740342Z","iopub.status.idle":"2023-12-18T03:55:54.740800Z","shell.execute_reply.started":"2023-12-18T03:55:54.740575Z","shell.execute_reply":"2023-12-18T03:55:54.740598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outputs = []\nids = []\nrles = []\nfor img_path in img_path_list:\n    img_id = img_path.split('/')[-3] + '_' + img_path.split('/')[-1][:4]\n    ids.append(img_id)\n    img = cv2.imread(img_path)\n    img = img.astype('float32')\n    img = (img - np.min(img)) / (np.max(img) - np.min(img) + 0.001)\n    img = test_transform(image=img)['image']\n    img = img.unsqueeze(0)\n    img = img.to(device)\n    output = model(img)\n#     output = torch.nn.Sigmoid()(output)\n    output = (output > 0.5) * 1\n    output = output.squeeze(0).permute((1, 2, 0))\n    output = output.detach().cpu().numpy()\n    output = output.astype('float32')\n    output = re_transform(image=output)['image']\n    output = (output > 0) * 1\n    output = output.astype('uint8')\n    output = remove_small_objects(output, min_size=10)\n    outputs.append(output)\n    rle = rle_encode(output)\n    rles.append(rle)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.742298Z","iopub.status.idle":"2023-12-18T03:55:54.743034Z","shell.execute_reply.started":"2023-12-18T03:55:54.742738Z","shell.execute_reply":"2023-12-18T03:55:54.742766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(img_path)\nimg = (img - np.min(img)) / (np.max(img) - np.min(img) + 0.001)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.745097Z","iopub.status.idle":"2023-12-18T03:55:54.745602Z","shell.execute_reply.started":"2023-12-18T03:55:54.745345Z","shell.execute_reply":"2023-12-18T03:55:54.745367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread('/kaggle/input/blood-vessel-segmentation/train/kidney_2/images/1400.tif')\nimage = image.astype('float32')\ntransform = A.Compose(\n    [\n        A.Resize(512, 512),\n        ToTensorV2()\n    ]\n)\naf_transform = transform(image = image)['image']\naf_transform = af_transform.unsqueeze(0)\naf_transform = af_transform.to(device)\nprint(af_transform.shape)\noutput = model(af_transform)\noutput = output.permute((0, 2, 3, 1)).squeeze(0).cpu().detach().numpy()\nplt.imshow(output, cmap = 'gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.746954Z","iopub.status.idle":"2023-12-18T03:55:54.747738Z","shell.execute_reply.started":"2023-12-18T03:55:54.747495Z","shell.execute_reply":"2023-12-18T03:55:54.747518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = cv2.imread('/kaggle/input/blood-vessel-segmentation/train/kidney_2/labels/1400.tif')\nlabel_trans = A.Compose(\n    [\n        A.Resize(512, 512)\n    ]\n)\nlabel = label_trans(image=label)['image']\nplt.imshow(label)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.749031Z","iopub.status.idle":"2023-12-18T03:55:54.749648Z","shell.execute_reply.started":"2023-12-18T03:55:54.749397Z","shell.execute_reply":"2023-12-18T03:55:54.749419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img1 = cv2.imread('/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0000.tif')\nimg2 = cv2.imread('/kaggle/input/blood-vessel-segmentation/test/kidney_6/images/0000.tif')\n(img1 == img2).sum(), img1.shape, img2.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.750800Z","iopub.status.idle":"2023-12-18T03:55:54.751721Z","shell.execute_reply.started":"2023-12-18T03:55:54.751472Z","shell.execute_reply":"2023-12-18T03:55:54.751497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame({'id': ids,\n                    'rle': rles})\nsub","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.753072Z","iopub.status.idle":"2023-12-18T03:55:54.753525Z","shell.execute_reply.started":"2023-12-18T03:55:54.753281Z","shell.execute_reply":"2023-12-18T03:55:54.753301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.754770Z","iopub.status.idle":"2023-12-18T03:55:54.756540Z","shell.execute_reply.started":"2023-12-18T03:55:54.756279Z","shell.execute_reply":"2023-12-18T03:55:54.756303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# def the visualize function \n# to see what's the argmentation with the data ","metadata":{}},{"cell_type":"code","source":"def visualize(image, mask, original_image=None, original_mask=None):\n    fontsize = 18\n    \n    if original_image is None and original_mask is None:\n        f, ax = plt.subplots(2, 1, figsize=(8, 8))\n\n        ax[0].imshow(image)\n        ax[1].imshow(mask)\n    else:\n        f, ax = plt.subplots(2, 2, figsize=(8, 8))\n\n        ax[0, 0].imshow(original_image, cmap='gray')\n        ax[0, 0].set_title('Original image', fontsize=fontsize)\n        \n        ax[1, 0].imshow(original_mask, cmap='gray')\n        ax[1, 0].set_title('Original mask', fontsize=fontsize)\n        \n        ax[0, 1].imshow(image, cmap='gray')\n        ax[0, 1].set_title('Transformed image', fontsize=fontsize)\n        \n        ax[1, 1].imshow(mask, cmap='gray')\n        ax[1, 1].set_title('Transformed mask', fontsize=fontsize)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.757902Z","iopub.status.idle":"2023-12-18T03:55:54.758367Z","shell.execute_reply.started":"2023-12-18T03:55:54.758141Z","shell.execute_reply":"2023-12-18T03:55:54.758162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image, mask = train_dataset[1400]\nimage = image.permute((1, 2, 0)).numpy()\nmask = mask.numpy()\nvisualize(image, mask)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.759809Z","iopub.status.idle":"2023-12-18T03:55:54.760270Z","shell.execute_reply.started":"2023-12-18T03:55:54.760043Z","shell.execute_reply":"2023-12-18T03:55:54.760064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0\nfor data in tqdm(train_loader):\n    img, label = data\n    ","metadata":{"execution":{"iopub.status.busy":"2023-12-18T03:55:54.761394Z","iopub.status.idle":"2023-12-18T03:55:54.761913Z","shell.execute_reply.started":"2023-12-18T03:55:54.761665Z","shell.execute_reply":"2023-12-18T03:55:54.761691Z"},"trusted":true},"execution_count":null,"outputs":[]}]}