{"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":"markdown","source":"### Libraries","metadata":{}},{"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\nimport os\nfor 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":{"execution":{"iopub.status.busy":"2022-08-24T22:36:16.485651Z","iopub.execute_input":"2022-08-24T22:36:16.486491Z","iopub.status.idle":"2022-08-24T22:36:16.568802Z","shell.execute_reply.started":"2022-08-24T22:36:16.486453Z","shell.execute_reply":"2022-08-24T22:36:16.567711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport glob\nimport torch \nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\nimport tifffile as tiff \nfrom tqdm import tqdm\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:35.379130Z","start_time":"2022-08-24T21:26:32.325446Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:16.570971Z","iopub.execute_input":"2022-08-24T22:36:16.571253Z","iopub.status.idle":"2022-08-24T22:36:16.577793Z","shell.execute_reply.started":"2022-08-24T22:36:16.571227Z","shell.execute_reply":"2022-08-24T22:36:16.576729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir(\"/kaggle/input/hubmap-organ-segmentation//\")","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:35.394153Z","start_time":"2022-08-24T21:26:35.380188Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:16.579302Z","iopub.execute_input":"2022-08-24T22:36:16.580478Z","iopub.status.idle":"2022-08-24T22:36:16.587336Z","shell.execute_reply.started":"2022-08-24T22:36:16.580437Z","shell.execute_reply":"2022-08-24T22:36:16.586357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(DEVICE)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:35.440578Z","start_time":"2022-08-24T21:26:35.397149Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:16.590113Z","iopub.execute_input":"2022-08-24T22:36:16.590812Z","iopub.status.idle":"2022-08-24T22:36:16.598445Z","shell.execute_reply.started":"2022-08-24T22:36:16.590766Z","shell.execute_reply":"2022-08-24T22:36:16.597497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EDA","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"train.csv\")\ndf.head()","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:35.596156Z","start_time":"2022-08-24T21:26:35.442574Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:16.600231Z","iopub.execute_input":"2022-08-24T22:36:16.600629Z","iopub.status.idle":"2022-08-24T22:36:16.912560Z","shell.execute_reply.started":"2022-08-24T22:36:16.600592Z","shell.execute_reply":"2022-08-24T22:36:16.911629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"id\"].values[0]","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:35.612123Z","start_time":"2022-08-24T21:26:35.598123Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:16.914141Z","iopub.execute_input":"2022-08-24T22:36:16.914490Z","iopub.status.idle":"2022-08-24T22:36:16.924185Z","shell.execute_reply.started":"2022-08-24T22:36:16.914456Z","shell.execute_reply":"2022-08-24T22:36:16.923066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = sns.diverging_palette(-10,10, as_cmap=True)\ndf.describe().style.background_gradient(cmap=cm)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:35.721218Z","start_time":"2022-08-24T21:26:35.613124Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:16.925986Z","iopub.execute_input":"2022-08-24T22:36:16.926816Z","iopub.status.idle":"2022-08-24T22:36:16.988511Z","shell.execute_reply.started":"2022-08-24T22:36:16.926771Z","shell.execute_reply":"2022-08-24T22:36:16.987637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking null values \ndf_check = pd.DataFrame(columns = ['Null_sum', 'Unique_count', 'Data_type'])\nfor col in df.columns:\n    df_check = df_check.append({'Null_sum': df[col].isna().sum(),\n                    'Unique_count': len(df[col].unique()),\n                    'Data_type':df[col].dtypes}, ignore_index = True)\n\ndf_check = pd.DataFrame(df_check.set_index(df.columns))\nprint(df_check)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:35.782226Z","start_time":"2022-08-24T21:26:35.722219Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:16.989884Z","iopub.execute_input":"2022-08-24T22:36:16.990243Z","iopub.status.idle":"2022-08-24T22:36:17.061002Z","shell.execute_reply.started":"2022-08-24T22:36:16.990210Z","shell.execute_reply":"2022-08-24T22:36:17.059861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_id_1 = 15329\nimg_1 = tiff.imread(\"train_images/\" + str(img_id_1) + \".tiff\")\nprint(img_1.shape)\nplt.imshow(img_1)\nplt.show()\n","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:36.586562Z","start_time":"2022-08-24T21:26:35.784225Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:17.062759Z","iopub.execute_input":"2022-08-24T22:36:17.063129Z","iopub.status.idle":"2022-08-24T22:36:18.274205Z","shell.execute_reply.started":"2022-08-24T22:36:17.063095Z","shell.execute_reply":"2022-08-24T22:36:18.273275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/paulorzp/rle-functions-run-length-encode-decode\ndef mask2rle(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels= img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] +1\n    print(runs.shape)\n    print(runs[1::2], runs[1::2].shape)\n    print(runs[::2], runs[::2].shape)\n    \n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n \ndef rle2mask(mask_rle, shape=(1600,256)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\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.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:36.602579Z","start_time":"2022-08-24T21:26:36.589556Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:18.279259Z","iopub.execute_input":"2022-08-24T22:36:18.281418Z","iopub.status.idle":"2022-08-24T22:36:18.291025Z","shell.execute_reply.started":"2022-08-24T22:36:18.281387Z","shell.execute_reply":"2022-08-24T22:36:18.290046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_1 = rle2mask(df[df[\"id\"]==img_id_1][\"rle\"].iloc[-1], (img_1.shape[1], img_1.shape[0]))\nmask_1.shape","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:36.617972Z","start_time":"2022-08-24T21:26:36.604552Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:18.292446Z","iopub.execute_input":"2022-08-24T22:36:18.293604Z","iopub.status.idle":"2022-08-24T22:36:18.307397Z","shell.execute_reply.started":"2022-08-24T22:36:18.293568Z","shell.execute_reply":"2022-08-24T22:36:18.306045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mask_1, cmap='coolwarm', alpha=0.5)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:37.421640Z","start_time":"2022-08-24T21:26:36.618989Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:18.308923Z","iopub.execute_input":"2022-08-24T22:36:18.309447Z","iopub.status.idle":"2022-08-24T22:36:19.348685Z","shell.execute_reply.started":"2022-08-24T22:36:18.309413Z","shell.execute_reply":"2022-08-24T22:36:19.347360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(7,7))\nplt.imshow(img_1)\nplt.imshow(mask_1, cmap='coolwarm', alpha=0.5)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:39.043187Z","start_time":"2022-08-24T21:26:37.422641Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:19.350756Z","iopub.execute_input":"2022-08-24T22:36:19.351109Z","iopub.status.idle":"2022-08-24T22:36:21.603539Z","shell.execute_reply.started":"2022-08-24T22:36:19.351074Z","shell.execute_reply":"2022-08-24T22:36:21.602589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (12,6))\nsns.countplot(df[\"organ\"])\nplt.title(\"Organs\")\nplt.show()","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:39.150850Z","start_time":"2022-08-24T21:26:39.045373Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:21.604950Z","iopub.execute_input":"2022-08-24T22:36:21.605771Z","iopub.status.idle":"2022-08-24T22:36:21.799937Z","shell.execute_reply.started":"2022-08-24T22:36:21.605725Z","shell.execute_reply":"2022-08-24T22:36:21.799040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ng = sns.FacetGrid(df,hue='sex')\ng = g.map(sns.kdeplot,'age',shade=True,alpha=0.4)                      \ng.add_legend()\nplt.show(g)\n\n\nsns.histplot(data = df, x = \"age\", hue = \"sex\", multiple = \"stack\")\nplt.show()","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:39.557561Z","start_time":"2022-08-24T21:26:39.151880Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:21.801354Z","iopub.execute_input":"2022-08-24T22:36:21.801806Z","iopub.status.idle":"2022-08-24T22:36:22.692428Z","shell.execute_reply.started":"2022-08-24T22:36:21.801762Z","shell.execute_reply":"2022-08-24T22:36:22.691518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,6))\nsns.histplot(data = df, x = \"organ\", hue = \"sex\")\nplt.show()","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:39.682768Z","start_time":"2022-08-24T21:26:39.559561Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:22.693693Z","iopub.execute_input":"2022-08-24T22:36:22.694085Z","iopub.status.idle":"2022-08-24T22:36:22.918161Z","shell.execute_reply.started":"2022-08-24T22:36:22.694046Z","shell.execute_reply":"2022-08-24T22:36:22.917112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Dataset","metadata":{}},{"cell_type":"code","source":"from torch.utils.data import Dataset as BaseDataset\nfrom torch.utils.data import DataLoader\nfrom torchvision import transforms\n\nclass Dataset(BaseDataset):\n    def __init__(self,df: pd.DataFrame, img_path, transforms, return_class: bool = False):\n        self.df = df\n        self.img_path = img_path\n        self.transform = transforms\n        self.return_class = return_class\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n\n        im = self.img_path + f\"{self.df['id'].iloc[idx]}.tiff\"\n        img = tiff.imread(im)\n        mask = rle2mask(self.df['rle'].iloc[idx], (img.shape[1],img.shape[0]))\n        mask = np.expand_dims(mask, axis=2)\n        \n        if self.transform:\n            img, mask = self.transform(img),self.transform(mask)\n        if self.return_class:\n            return img, mask, self.df['organ'].iloc[idx]\n        \n        return img, mask","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:40.015522Z","start_time":"2022-08-24T21:26:39.684562Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:22.919884Z","iopub.execute_input":"2022-08-24T22:36:22.920268Z","iopub.status.idle":"2022-08-24T22:36:22.928622Z","shell.execute_reply.started":"2022-08-24T22:36:22.920234Z","shell.execute_reply":"2022-08-24T22:36:22.927713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crop = 448\ntest_crop = 2016\ntransforms = transforms.Compose([transforms.ToPILImage(),\n                                transforms.Resize((224,\n                                224)),\n                                transforms.ToTensor()])\n# dataset for view images\nview_dataset = Dataset(\n    df = df,\n    img_path = \"train_images/\",\n    transforms = transforms,\n    return_class = True\n)\n# train dataset\ntrain_dataset = Dataset(\n    df = df,\n    img_path = \"train_images/\",\n    transforms = transforms)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:40.031454Z","start_time":"2022-08-24T21:26:40.017477Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:22.930100Z","iopub.execute_input":"2022-08-24T22:36:22.930700Z","iopub.status.idle":"2022-08-24T22:36:22.940068Z","shell.execute_reply.started":"2022-08-24T22:36:22.930644Z","shell.execute_reply":"2022-08-24T22:36:22.939194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"test.csv\")\ntest_df.head()","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:40.061702Z","start_time":"2022-08-24T21:26:40.033456Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:22.943408Z","iopub.execute_input":"2022-08-24T22:36:22.943786Z","iopub.status.idle":"2022-08-24T22:36:22.964588Z","shell.execute_reply.started":"2022-08-24T22:36:22.943755Z","shell.execute_reply":"2022-08-24T22:36:22.963727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_dataset)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:40.077771Z","start_time":"2022-08-24T21:26:40.063719Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:22.966152Z","iopub.execute_input":"2022-08-24T22:36:22.966713Z","iopub.status.idle":"2022-08-24T22:36:22.974730Z","shell.execute_reply.started":"2022-08-24T22:36:22.966639Z","shell.execute_reply":"2022-08-24T22:36:22.973718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (\"Image Shape: \",train_dataset[0][0].shape)\nprint (\"Mask Shape: \",train_dataset[0][1].shape)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:40.361102Z","start_time":"2022-08-24T21:26:40.079718Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:22.976091Z","iopub.execute_input":"2022-08-24T22:36:22.977048Z","iopub.status.idle":"2022-08-24T22:36:23.291785Z","shell.execute_reply.started":"2022-08-24T22:36:22.977002Z","shell.execute_reply":"2022-08-24T22:36:23.290729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install segmentation_models_pytorch","metadata":{"execution":{"iopub.status.busy":"2022-08-24T22:36:23.293217Z","iopub.execute_input":"2022-08-24T22:36:23.293859Z","iopub.status.idle":"2022-08-24T22:36:35.777174Z","shell.execute_reply.started":"2022-08-24T22:36:23.293821Z","shell.execute_reply":"2022-08-24T22:36:35.775848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport segmentation_models_pytorch as smp\nfrom segmentation_models_pytorch.losses import DiceLoss\nfrom torchmetrics import MeanMetric, Dice\nfrom torch.optim import Adam\nfrom torch.optim.lr_scheduler import CosineAnnealingLR","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:41.677273Z","start_time":"2022-08-24T21:26:40.363062Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:35.779024Z","iopub.execute_input":"2022-08-24T22:36:35.779699Z","iopub.status.idle":"2022-08-24T22:36:35.787273Z","shell.execute_reply.started":"2022-08-24T22:36:35.779628Z","shell.execute_reply":"2022-08-24T22:36:35.786038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import segmentation_models_pytorch as smp\n\nmodel = smp.Unet(\n    encoder_name=\"resnet34\",        # choose encoder, e.g. mobilenet_v2 or efficientnet-b7\n    encoder_weights=\"imagenet\",     # use `imagenet` pre-trained weights for encoder initialization\n    in_channels=3,                  # model input channels (1 for gray-scale images, 3 for RGB, etc.)\n)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:42.231571Z","start_time":"2022-08-24T21:26:41.677273Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:35.788606Z","iopub.execute_input":"2022-08-24T22:36:35.789287Z","iopub.status.idle":"2022-08-24T22:36:36.605236Z","shell.execute_reply.started":"2022-08-24T22:36:35.789252Z","shell.execute_reply":"2022-08-24T22:36:36.603713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model.to(DEVICE)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:42.420688Z","start_time":"2022-08-24T21:26:42.231571Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:36.607673Z","iopub.execute_input":"2022-08-24T22:36:36.609004Z","iopub.status.idle":"2022-08-24T22:36:36.647949Z","shell.execute_reply.started":"2022-08-24T22:36:36.608961Z","shell.execute_reply":"2022-08-24T22:36:36.647007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16\nvalidation_split = .2\nshuffle_dataset = True\nrandom_seed= 42\nfrom torch.utils.data.sampler import SubsetRandomSampler\n\n# Creating data indices for training and validation splits:\ndataset_size = len(train_dataset)\nindices = list(range(dataset_size))\nsplit = int(np.floor(validation_split * dataset_size))\nif shuffle_dataset :\n    np.random.seed(random_seed)\n    np.random.shuffle(indices)\ntrain_indices, val_indices = indices[split:], indices[:split]\n\n# Creating PT data samplers and loaders:\ntrain_sampler = SubsetRandomSampler(train_indices)\nvalid_sampler = SubsetRandomSampler(val_indices)\n\ntrain_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, \n                                           sampler=train_sampler)\nvalidation_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,\n                                                sampler=valid_sampler)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:42.436689Z","start_time":"2022-08-24T21:26:42.422689Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:36.649764Z","iopub.execute_input":"2022-08-24T22:36:36.650412Z","iopub.status.idle":"2022-08-24T22:36:36.660645Z","shell.execute_reply.started":"2022-08-24T22:36:36.650374Z","shell.execute_reply":"2022-08-24T22:36:36.659609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader.batch_size","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:42.452689Z","start_time":"2022-08-24T21:26:42.438689Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:36.663865Z","iopub.execute_input":"2022-08-24T22:36:36.664274Z","iopub.status.idle":"2022-08-24T22:36:36.673492Z","shell.execute_reply.started":"2022-08-24T22:36:36.664245Z","shell.execute_reply":"2022-08-24T22:36:36.672657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.tensorboard import SummaryWriter\n\ndef train (model, train_data, valid_data, num_epochs, experiment_name:str):\n    loss_function = DiceLoss(mode='multilabel')\n    metric = Dice(\n    average='samples',\n    ignore_index=0).to(DEVICE)\n    LR = 1e-3\n    optimizer = torch.optim.Adam(model.parameters(), lr=LR)\n    \n#     writer = SummaryWriter('tensorboard_logs/{0}_train'.format(experiment_name))\n#     val_writer = SummaryWriter('tensorboard_logs/{0}_val'.format(experiment_name))\n    \n    loss_list = []\n    batch_loss_list = []\n    global_writer_counter = 0\n    total,_ = next(iter(train_data))\n    recording_step = total.shape[0]//train_loader.batch_size \n    for epoch in range(num_epochs):\n        model.train()\n        for step_train, (image, label) in enumerate(tqdm(train_data, total = len(train_data))):\n            image = image.to(DEVICE).float()\n            label = label.to(DEVICE).float()\n            \n            optimizer.zero_grad()\n            pred = model(image)\n            loss = loss_function(pred, label)\n            loss = torch.mean(loss)\n            loss_list.append(loss.item())\n            batch_loss_list.append(loss.item())\n            loss.backward()\n            optimizer.step()\n            \n#             if(step_train+1 % recording_step == 0):\n#                 writer.add_scalar(\"training_loss\", torch.tensor(batch_loss_list).mean(), global_writer_counter)\n#                 batch_loss_list = []\n#                 global_writer_counter +=1\n                \n        print(np.mean(loss_list))\n            \n        val_loss_list = []     \n        with torch.no_grad():\n            model.eval()\n        for step_val, (val_image,val_label) in enumerate(tqdm(valid_data,total= len(valid_data))):\n            val_image = val_image.to(DEVICE).float()\n            val_label = val_label.to(DEVICE).float()\n            \n            val_pred = model(val_image)\n            val_loss = loss_function(val_pred, val_label)\n#             val_loss = torch.mean(val_loss)\n            val_loss_list.append(val_loss.item())\n    \n#         val_writer.add_scalar(\"val_loss\", torch.tensor(val_loss_list).mean(), epoch)   \n        print(np.mean(val_loss_list))   \n        ","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:42.689688Z","start_time":"2022-08-24T21:26:42.454692Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:36.682346Z","iopub.execute_input":"2022-08-24T22:36:36.683045Z","iopub.status.idle":"2022-08-24T22:36:36.699694Z","shell.execute_reply.started":"2022-08-24T22:36:36.683008Z","shell.execute_reply":"2022-08-24T22:36:36.698808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train(model, train_loader, validation_loader, 20, experiment_name=\"exp_2\")","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:42.705688Z","start_time":"2022-08-24T21:26:42.695692Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:36.701351Z","iopub.execute_input":"2022-08-24T22:36:36.702173Z","iopub.status.idle":"2022-08-24T22:36:36.712447Z","shell.execute_reply.started":"2022-08-24T22:36:36.702127Z","shell.execute_reply":"2022-08-24T22:36:36.711093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir(\"/kaggle/working/\")\nos.getcwd()","metadata":{"execution":{"iopub.status.busy":"2022-08-24T22:36:36.714317Z","iopub.execute_input":"2022-08-24T22:36:36.714796Z","iopub.status.idle":"2022-08-24T22:36:36.723867Z","shell.execute_reply.started":"2022-08-24T22:36:36.714752Z","shell.execute_reply":"2022-08-24T22:36:36.722659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model.state_dict(),\"output_model.pickle\")","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:42.879696Z","start_time":"2022-08-24T21:26:42.707689Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:36.725453Z","iopub.execute_input":"2022-08-24T22:36:36.726712Z","iopub.status.idle":"2022-08-24T22:36:37.000292Z","shell.execute_reply.started":"2022-08-24T22:36:36.726658Z","shell.execute_reply":"2022-08-24T22:36:36.998925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_state_dict(torch.load(\"output_model.pickle\"))","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:44.383534Z","start_time":"2022-08-24T21:26:42.880694Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:37.002472Z","iopub.execute_input":"2022-08-24T22:36:37.003201Z","iopub.status.idle":"2022-08-24T22:36:37.122544Z","shell.execute_reply.started":"2022-08-24T22:36:37.003161Z","shell.execute_reply":"2022-08-24T22:36:37.121610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:44.399527Z","start_time":"2022-08-24T21:26:44.385530Z"},"execution":{"iopub.status.busy":"2022-08-24T22:36:37.124024Z","iopub.execute_input":"2022-08-24T22:36:37.124509Z","iopub.status.idle":"2022-08-24T22:36:37.134617Z","shell.execute_reply.started":"2022-08-24T22:36:37.124471Z","shell.execute_reply":"2022-08-24T22:36:37.133594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = tiff.imread(\"/kaggle/input/hubmap-organ-segmentation/test_images/10078.tiff\")\ntest_img = transforms(test_img).to(DEVICE)\ntest_img =test_img.unsqueeze(0)\ntest_img.shape","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:44.447527Z","start_time":"2022-08-24T21:26:44.400528Z"},"execution":{"iopub.status.busy":"2022-08-24T22:37:20.780396Z","iopub.execute_input":"2022-08-24T22:37:20.780781Z","iopub.status.idle":"2022-08-24T22:37:20.841081Z","shell.execute_reply.started":"2022-08-24T22:37:20.780749Z","shell.execute_reply":"2022-08-24T22:37:20.839734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:44.495572Z","start_time":"2022-08-24T21:26:44.449529Z"},"execution":{"iopub.status.busy":"2022-08-24T22:37:22.413338Z","iopub.execute_input":"2022-08-24T22:37:22.413719Z","iopub.status.idle":"2022-08-24T22:37:22.428435Z","shell.execute_reply.started":"2022-08-24T22:37:22.413683Z","shell.execute_reply":"2022-08-24T22:37:22.427442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    pred = model(test_img)\n    pred = pred.squeeze().cpu().numpy()","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:26:47.066281Z","start_time":"2022-08-24T21:26:44.497529Z"},"execution":{"iopub.status.busy":"2022-08-24T22:37:24.634790Z","iopub.execute_input":"2022-08-24T22:37:24.635256Z","iopub.status.idle":"2022-08-24T22:37:24.657449Z","shell.execute_reply.started":"2022-08-24T22:37:24.635212Z","shell.execute_reply":"2022-08-24T22:37:24.656182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(mask_image):\n    pixels = mask_image.flatten()\n    # We avoid issues with '1' at the start or end (at the corners of \n    # the original image) by setting those pixels to '0' explicitly.\n    # We do not expect these to be non-zero for an accurate mask, \n    # so this should not harm the score.\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[:-1:2]\n    return ' '.join(str(x) for x in runs)\n","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:35:47.913181Z","start_time":"2022-08-24T21:35:47.903177Z"},"execution":{"iopub.status.busy":"2022-08-24T22:37:26.059812Z","iopub.execute_input":"2022-08-24T22:37:26.060204Z","iopub.status.idle":"2022-08-24T22:37:26.067087Z","shell.execute_reply.started":"2022-08-24T22:37:26.060172Z","shell.execute_reply":"2022-08-24T22:37:26.066010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output =rle_encode(pred)\nlen(output)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:36:15.281615Z","start_time":"2022-08-24T21:36:15.241259Z"},"execution":{"iopub.status.busy":"2022-08-24T22:37:27.241180Z","iopub.execute_input":"2022-08-24T22:37:27.241798Z","iopub.status.idle":"2022-08-24T22:37:27.278813Z","shell.execute_reply.started":"2022-08-24T22:37:27.241760Z","shell.execute_reply":"2022-08-24T22:37:27.277546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names = 10078","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:38:51.709551Z","start_time":"2022-08-24T21:38:51.696981Z"},"execution":{"iopub.status.busy":"2022-08-24T22:37:29.954230Z","iopub.execute_input":"2022-08-24T22:37:29.954730Z","iopub.status.idle":"2022-08-24T22:37:29.963539Z","shell.execute_reply.started":"2022-08-24T22:37:29.954644Z","shell.execute_reply":"2022-08-24T22:37:29.962218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'id':[names],'rle':[output]})\ndf.to_csv('submission.csv',index=False)","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:40:12.082679Z","start_time":"2022-08-24T21:40:12.069682Z"},"execution":{"iopub.status.busy":"2022-08-24T22:37:30.861945Z","iopub.execute_input":"2022-08-24T22:37:30.862515Z","iopub.status.idle":"2022-08-24T22:37:30.884275Z","shell.execute_reply.started":"2022-08-24T22:37:30.862466Z","shell.execute_reply":"2022-08-24T22:37:30.883270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"ExecuteTime":{"end_time":"2022-08-24T21:40:16.719808Z","start_time":"2022-08-24T21:40:16.703809Z"},"execution":{"iopub.status.busy":"2022-08-24T22:37:32.068528Z","iopub.execute_input":"2022-08-24T22:37:32.069242Z","iopub.status.idle":"2022-08-24T22:37:32.080499Z","shell.execute_reply.started":"2022-08-24T22:37:32.069202Z","shell.execute_reply":"2022-08-24T22:37:32.079519Z"},"trusted":true},"execution_count":null,"outputs":[]}]}