{"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":"## Creds","metadata":{}},{"cell_type":"code","source":"## id ","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:19.408215Z","iopub.execute_input":"2023-06-06T09:38:19.408585Z","iopub.status.idle":"2023-06-06T09:38:19.431607Z","shell.execute_reply.started":"2023-06-06T09:38:19.408553Z","shell.execute_reply":"2023-06-06T09:38:19.430182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# This Notebook","metadata":{}},{"cell_type":"markdown","source":" - uses random sampling training \n - pretrained Unet\n - referance for notebook that inspired me to create this: https://www.kaggle.com/code/fchollet/keras-starter-kit-unet-train-on-full-dataset\n - in connecteing training data and masks\n - in segregating validation area and training data","metadata":{}},{"cell_type":"markdown","source":"# worked\n- memory handling\n- creating the test by running on all the test data  \n# not worked\n- many code fails in shapes , batches , shapes , torch.tensor and numpy , and etc \n- many failed memories\n- try to test and validate before running training just to save gpu on kaggle. intead of training wasting gpu and then testing \n- sanity check helped me understand my problems\n- researching the model that fit","metadata":{}},{"cell_type":"markdown","source":"## install dependencies","metadata":{}},{"cell_type":"code","source":"!pip install segmentation-models-pytorch\n!pip install albumentations","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:21:26.618385Z","iopub.execute_input":"2023-06-06T18:21:26.619879Z","iopub.status.idle":"2023-06-06T18:21:53.764778Z","shell.execute_reply.started":"2023-06-06T18:21:26.619824Z","shell.execute_reply":"2023-06-06T18:21:53.763270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## imports","metadata":{}},{"cell_type":"code","source":"import torchvision.transforms as transforms\nimport torch.nn.functional as F\nimport albumentations as albu\nimport segmentation_models_pytorch as smp\nfrom segmentation_models_pytorch import utils\nfrom segmentation_models_pytorch.encoders import get_preprocessing_fn\nfrom torch import nn,optim\nimport tensorflow as tf\nimport numpy as np\nimport torch \nimport pandas as pd\nimport glob\nimport time\nimport PIL.Image as Image\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom tqdm import tqdm\nimport enum\nimport gc\nimport math\nfrom torch.utils.data import DataLoader,Dataset\nimport random\n%matplotlib inline\ntorch.cuda.is_available()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:33.941889Z","iopub.execute_input":"2023-06-06T09:38:33.942166Z","iopub.status.idle":"2023-06-06T09:38:48.578819Z","shell.execute_reply.started":"2023-06-06T09:38:33.942142Z","shell.execute_reply":"2023-06-06T09:38:48.577971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if torch.cuda.is_available():\n    torch.cuda.empty_cache()\n    torch.cuda.memory_summary(device=None, abbreviated=False)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:48.580945Z","iopub.execute_input":"2023-06-06T09:38:48.581523Z","iopub.status.idle":"2023-06-06T09:38:48.902238Z","shell.execute_reply.started":"2023-06-06T09:38:48.581501Z","shell.execute_reply":"2023-06-06T09:38:48.900622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Constants","metadata":{}},{"cell_type":"code","source":"BASE_DIR = '/kaggle/input/vesuvius-challenge-ink-detection/'\nSURFACE_VOLUME_DIR_NAME = \"surface_volume/\"\nSAMPLE = BASE_DIR + 'sample_submission.csv'\n\nIMAGE_NAME = 'ir.png'\nMASK_NAME = 'mask.png'\nINK_LABELS_NAME = 'inklabels.png'\nINK_LABELS_CSV_NAME = 'inklabels_rle.csv'\n\nTRAIN_DIR = BASE_DIR +  'train/'\nTEST_DIR = BASE_DIR +  'test/'\nTEST_A_DIR = TEST_DIR + 'a/'\nTEST_B_DIR = TEST_DIR + 'b/'\nTRAIN_1_DIR = TRAIN_DIR + '1/'\nTRAIN_2_DIR = TRAIN_DIR + '2/'\nTRAIN_3_DIR = TRAIN_DIR + '3/'\n\nTIF_FILES = \"*.tif\"\n\nTRAIN_1_SURFACE_VOLUME_DIR = TRAIN_1_DIR + SURFACE_VOLUME_DIR_NAME\nTRAIN_2_SURFACE_VOLUME_DIR = TRAIN_2_DIR + SURFACE_VOLUME_DIR_NAME\nTRAIN_3_SURFACE_VOLUME_DIR = TRAIN_3_DIR + SURFACE_VOLUME_DIR_NAME\n\nTEST_A_SURFACE_VOLUME_DIR = TEST_A_DIR + SURFACE_VOLUME_DIR_NAME\nTEST_B_SURFACE_VOLUME_DIR = TEST_B_DIR + SURFACE_VOLUME_DIR_NAME\n\nTRAIN_1_IR = TRAIN_1_DIR + IMAGE_NAME\nTRAIN_1_MASK = TRAIN_1_DIR + MASK_NAME\nTRAIN_1_INKLABELS = TRAIN_1_DIR + INK_LABELS_NAME\nTRAIN_1_RLE_CSV = TRAIN_1_DIR + INK_LABELS_CSV_NAME\n\nTRAIN_2_IR = TRAIN_2_DIR + IMAGE_NAME\nTRAIN_2_MASK = TRAIN_2_DIR + MASK_NAME\nTRAIN_2_INKLABELS = TRAIN_2_DIR + INK_LABELS_NAME\nTRAIN_2_RLE_CSV = TRAIN_2_DIR + INK_LABELS_CSV_NAME\n\nTRAIN_3_IR = TRAIN_3_DIR + IMAGE_NAME\nTRAIN_3_MASK = TRAIN_3_DIR + MASK_NAME\nTRAIN_3_INKLABELS = TRAIN_3_DIR + INK_LABELS_NAME\nTRAIN_3_RLE_CSV = TRAIN_3_DIR + INK_LABELS_CSV_NAME\n\n\nTEST_A_MASK = TEST_A_DIR + MASK_NAME\nTEST_B_MASK = TEST_B_DIR + MASK_NAME\n","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:48.903727Z","iopub.execute_input":"2023-06-06T09:38:48.904597Z","iopub.status.idle":"2023-06-06T09:38:48.917381Z","shell.execute_reply.started":"2023-06-06T09:38:48.904563Z","shell.execute_reply":"2023-06-06T09:38:48.916334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_1_image_paths = sorted(glob.glob(TRAIN_1_SURFACE_VOLUME_DIR + TIF_FILES))\ntrain_2_image_paths = sorted(glob.glob(TRAIN_2_SURFACE_VOLUME_DIR + TIF_FILES))\ntrain_3_image_paths = sorted(glob.glob(TRAIN_3_SURFACE_VOLUME_DIR + TIF_FILES))\ntest_a_image_paths = sorted(glob.glob(TEST_A_SURFACE_VOLUME_DIR + TIF_FILES))\ntest_b_image_paths = sorted(glob.glob(TEST_B_SURFACE_VOLUME_DIR + TIF_FILES))","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:48.919478Z","iopub.execute_input":"2023-06-06T09:38:48.920027Z","iopub.status.idle":"2023-06-06T09:38:49.021306Z","shell.execute_reply.started":"2023-06-06T09:38:48.919995Z","shell.execute_reply":"2023-06-06T09:38:49.020637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# utils functions","metadata":{}},{"cell_type":"code","source":"to_tensor = transforms.ToTensor()\nto_pil = transforms.ToPILImage()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:49.022509Z","iopub.execute_input":"2023-06-06T09:38:49.022926Z","iopub.status.idle":"2023-06-06T09:38:49.027157Z","shell.execute_reply.started":"2023-06-06T09:38:49.022904Z","shell.execute_reply":"2023-06-06T09:38:49.026057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_image(path):\n    img = Image.open(path)\n    return img\n\ndef plot_row(images):\n    fig, axis= plt.subplots(1, len(images),figsize=(10,8))\n    for i in range(0,len(images)):\n        axis[i].imshow(images[i], cmap='gray')\n    plt.show()\n    \ndef connect_images(images):\n    return tf.concat(images, axis=1)\n\ndef get_random_z_slice():\n    max_zstart = MAX_Z_SLICES - SLICE_BATCH_SIZE\n    zstart = random.randint(0, max_zstart)\n    zend = zstart + SLICE_BATCH_SIZE\n    return zstart,zend\n\ndef load_scan(path,zstart,zend):\n    path = path[zstart:zend]\n    scans = []\n    for scan_slice in tqdm(path):\n        img = Image.open(scan_slice)\n        resized_scan_slice = aspect_ratio_resize_by_height(img,HEIGHT)\n        resized_scan_slice_array = np.array(resized_scan_slice,dtype=\"float32\")\n        scans.append(resized_scan_slice_array)\n        del img\n        del resized_scan_slice\n    stack = tf.stack(scans,axis=-1)\n    del scans\n    return stack\n\n\ndef create_validation_area_and_patch(start_point,width,height):\n    validation_area = (start_point[1],start_point[1]+height,start_point[0], start_point[0] + width)\n    validation_patch = patches.Rectangle(\n        (start_point[0], start_point[1]),\n        width,\n        height,\n        linewidth=2,\n        edgecolor='b',\n        facecolor='none'\n    )\n    return validation_area,validation_patch\n\ndef calculate_iou(pred, target):\n    intersection = np.logical_and(target, pred)\n    union = np.logical_or(target, pred)\n    iou_score = np.sum(intersection) / np.sum(union)\n    return iou_score\n\ndef batch_calculate_iou(pred, target):\n    intersection = np.logical_and(target, pred)\n    union = np.logical_or(target, pred)\n    iou_score = np.sum(intersection, axis=(1, 2)) / np.sum(union, axis=(1, 2))\n    return iou_score\n\nsmooth = 1e-6  # Small constant to avoid division by zero\n\n\ndef dice_loss(output, label):\n    intersection = torch.sum(output * label)\n    union = torch.sum(output) + torch.sum(label)\n    dice = (2.0 * intersection + smooth) / (union + smooth)\n    loss = 1.0 - dice\n    return loss\n\ndef get_slice_batch():\n    zstart,zend = (Z_START,Z_END)\n    train_scan_1 = load_scan(train_1_image_paths,zstart,zend)\n    train_scan_2 = load_scan(train_2_image_paths,zstart,zend)\n    train_scan_3= load_scan(train_3_image_paths,zstart,zend)\n    train_scan = connect_images([train_scan_1,train_scan_2,train_scan_3])\n    return train_scan,zstart,zend\n\ndef aspect_ratio_resize_by_height(img, new_height:int,print_shape=False):\n    current_width, current_height = img.size\n    image_ratio = current_width / current_height\n    new_width = int(new_height * image_ratio)\n    new_size = (new_width, new_height)\n    if print_shape:\n        print(new_size)\n    img = img.resize(new_size)\n    return img\n\ndef 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    return ' '.join(str(x) for x in runs)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:49.028086Z","iopub.execute_input":"2023-06-06T09:38:49.028331Z","iopub.status.idle":"2023-06-06T09:38:49.048900Z","shell.execute_reply.started":"2023-06-06T09:38:49.028312Z","shell.execute_reply":"2023-06-06T09:38:49.047496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## learning the train data and visualizing","metadata":{}},{"cell_type":"code","source":"train_1_ir = read_image(TRAIN_1_IR)\ntrain_1_mask = read_image(TRAIN_1_MASK)\ntrain_1_label = read_image(TRAIN_1_INKLABELS)\ntrain_1_csv = pd.read_csv(TRAIN_1_RLE_CSV)\nprint(\"ir 1 shape: \",train_1_ir.size)\nprint(\"mask 1 shape: \",train_1_mask.size)\nprint(\"label 1 shape : \",train_1_label.size)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:49.050372Z","iopub.execute_input":"2023-06-06T09:38:49.050773Z","iopub.status.idle":"2023-06-06T09:38:49.165281Z","shell.execute_reply.started":"2023-06-06T09:38:49.050723Z","shell.execute_reply":"2023-06-06T09:38:49.164462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_2_ir = read_image(TRAIN_2_IR)\ntrain_2_mask = read_image(TRAIN_2_MASK)\ntrain_2_label = read_image(TRAIN_2_INKLABELS)\ntrain_2_csv = pd.read_csv(TRAIN_2_RLE_CSV)\nprint(\"ir 2 shape: \",train_2_ir.size)\nprint(\"mask 2 shape: \",train_2_mask.size)\nprint(\"label 2 shape : \",train_2_label.size)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:49.168404Z","iopub.execute_input":"2023-06-06T09:38:49.168910Z","iopub.status.idle":"2023-06-06T09:38:49.255670Z","shell.execute_reply.started":"2023-06-06T09:38:49.168886Z","shell.execute_reply":"2023-06-06T09:38:49.254819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_3_ir = read_image(TRAIN_3_IR)\ntrain_3_mask = read_image(TRAIN_3_MASK)\ntrain_3_label = read_image(TRAIN_3_INKLABELS)\ntrain_3_csv = pd.read_csv(TRAIN_3_RLE_CSV)\nprint(\"ir 3 shape: \",train_3_ir.size)\nprint(\"mask 3 shape: \",train_3_mask.size)\nprint(\"label 3 shape : \",train_3_label.size)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:49.256732Z","iopub.execute_input":"2023-06-06T09:38:49.257492Z","iopub.status.idle":"2023-06-06T09:38:49.336714Z","shell.execute_reply.started":"2023-06-06T09:38:49.257464Z","shell.execute_reply":"2023-06-06T09:38:49.335162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_row([train_1_ir,train_1_mask,train_1_label])\n","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:49.338361Z","iopub.execute_input":"2023-06-06T09:38:49.338690Z","iopub.status.idle":"2023-06-06T09:38:57.780067Z","shell.execute_reply.started":"2023-06-06T09:38:49.338664Z","shell.execute_reply":"2023-06-06T09:38:57.779258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_row([train_2_ir,train_2_mask,train_2_label])","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:38:57.781129Z","iopub.execute_input":"2023-06-06T09:38:57.781794Z","iopub.status.idle":"2023-06-06T09:39:15.361800Z","shell.execute_reply.started":"2023-06-06T09:38:57.781767Z","shell.execute_reply":"2023-06-06T09:39:15.360836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_row([train_3_ir,train_3_mask,train_3_label])","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:15.363232Z","iopub.execute_input":"2023-06-06T09:39:15.364253Z","iopub.status.idle":"2023-06-06T09:39:21.708078Z","shell.execute_reply.started":"2023-06-06T09:39:15.364216Z","shell.execute_reply":"2023-06-06T09:39:21.707056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_1_ir\ndel train_2_ir\ndel train_3_ir","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:21.709281Z","iopub.execute_input":"2023-06-06T09:39:21.710171Z","iopub.status.idle":"2023-06-06T09:39:21.727618Z","shell.execute_reply.started":"2023-06-06T09:39:21.710143Z","shell.execute_reply":"2023-06-06T09:39:21.726184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# hyperparameters","metadata":{}},{"cell_type":"code","source":"USE_MIXED_PRECISION = False\nUSE_JIT_COMPILE = False\nVALIDATION_START_POINT = (1000,1000)\nVALIDATION_WIDTH = 2000\nVALIDATION_HEIGHT = 1000\nMAX_Z_SLICES = 65\nSLICE_BATCH_SIZE = 32\nZ_START = 16\nZ_END = Z_START + SLICE_BATCH_SIZE\nHEIGHT = 4000\n\nTHRESHOLD = 0.5\nZ_SLICES = 65\n\n\nPATCH_SIZE_64 = (24,24)\nBUFFER_SIZE_64 = 20\n\nPATCH_SIZE_256 = (56,56)\nBUFFER_SIZE_256 = 100 \n\nPATCH_SIZE_128 = (28,28)\nBUFFER_SIZE_128 = 50 \n\n\nPATCH_SIZE_512 = (12,12)\nBUFFER_SIZE_512 = 250 \n\nPATCH_SIZE = PATCH_SIZE_64\nBUFFER_SIZE = BUFFER_SIZE_64\n","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:21.728703Z","iopub.execute_input":"2023-06-06T09:39:21.729022Z","iopub.status.idle":"2023-06-06T09:39:21.745702Z","shell.execute_reply.started":"2023-06-06T09:39:21.728998Z","shell.execute_reply":"2023-06-06T09:39:21.744532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### creating a data area that the training wont use for training so we can validate on","metadata":{}},{"cell_type":"code","source":"validation_area,validation_patch = create_validation_area_and_patch(\n    VALIDATION_START_POINT,\n    VALIDATION_WIDTH,\n    VALIDATION_HEIGHT\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:21.747286Z","iopub.execute_input":"2023-06-06T09:39:21.747900Z","iopub.status.idle":"2023-06-06T09:39:21.760430Z","shell.execute_reply.started":"2023-06-06T09:39:21.747869Z","shell.execute_reply":"2023-06-06T09:39:21.759419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ENCODER = \"resnet18\"\nENCODER_WEIGHTS = \"imagenet\"\nNUM_CHANNELS = 32\nENCODER_DEPTH = 5\nCLASSES = ['ink']\nACTIVATION = \"sigmoid\"\nDEVICE = 'cuda'\nLR = 0.0001\nTRAINING_EPOCH_SIZE = 80\nVALIDATION_EPOCH_SIZE= 20\nNUM_EPOCHS = 10\nTRAIN_BATCH_SIZE = 1\nVAL_BATCH_SIZE = 1","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:21.761953Z","iopub.execute_input":"2023-06-06T09:39:21.762261Z","iopub.status.idle":"2023-06-06T09:39:21.775584Z","shell.execute_reply.started":"2023-06-06T09:39:21.762235Z","shell.execute_reply":"2023-06-06T09:39:21.774454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nmodel = smp.Unet(\n    encoder_name=ENCODER,        \n    encoder_weights=ENCODER_WEIGHTS,   \n    encoder_depth=ENCODER_DEPTH,\n    in_channels=NUM_CHANNELS,                  \n    classes=len(CLASSES),     \n    activation=ACTIVATION,\n)\nprint(model.encoder)\npreprocess_input = get_preprocessing_fn(ENCODER, pretrained=ENCODER_WEIGHTS)\n\n\nloss = utils.losses.DiceLoss()\nmetrics = [\n    utils.metrics.IoU(threshold=THRESHOLD),\n    utils.metrics.Fscore(),\n    utils.metrics.Accuracy(),\n]\n\noptimizer = torch.optim.Adam([ \n    dict(params=model.parameters(), lr=LR),\n])\n\n\ntrain_epoch = utils.train.TrainEpoch(\n    model, \n    loss=loss, \n    metrics=metrics, \n    optimizer=optimizer,\n\n#     device=DEVICE,\n    verbose=True,\n)\n\nvalid_epoch = utils.train.ValidEpoch(\n    model, \n    loss=loss, \n    metrics=metrics, \n#     device=DEVICE,\n    verbose=True,\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:21.776795Z","iopub.execute_input":"2023-06-06T09:39:21.777068Z","iopub.status.idle":"2023-06-06T09:39:22.725331Z","shell.execute_reply.started":"2023-06-06T09:39:21.777045Z","shell.execute_reply":"2023-06-06T09:39:22.724006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Changing the train data\n- i decided to change the the train data to one big mask as label and combine all training data by scan z height, this idea came from this kaggle notebook\nhttps://www.kaggle.com/code/fchollet/keras-starter-kit-unet-train-on-full-dataset","metadata":{}},{"cell_type":"markdown","source":"## changing train data","metadata":{}},{"cell_type":"code","source":"train_1_label = aspect_ratio_resize_by_height(train_1_label,HEIGHT,True)\ntrain_2_label = aspect_ratio_resize_by_height(train_2_label,HEIGHT,True)\ntrain_3_label = aspect_ratio_resize_by_height(train_3_label,HEIGHT,True)\nplot_row([train_1_label,train_2_label,train_3_label])\nlabels = [train_1_label,train_2_label,train_3_label]\ndel train_1_label\ndel train_2_label\ndel train_3_label","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:22.726878Z","iopub.execute_input":"2023-06-06T09:39:22.727165Z","iopub.status.idle":"2023-06-06T09:39:25.760958Z","shell.execute_reply.started":"2023-06-06T09:39:22.727142Z","shell.execute_reply":"2023-06-06T09:39:25.759702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_val_label = connect_images(labels)\ndel labels\nplt.imshow(train_val_label,cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:25.762433Z","iopub.execute_input":"2023-06-06T09:39:25.762738Z","iopub.status.idle":"2023-06-06T09:39:27.476324Z","shell.execute_reply.started":"2023-06-06T09:39:25.762711Z","shell.execute_reply":"2023-06-06T09:39:27.475047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_1_mask = aspect_ratio_resize_by_height(train_1_mask,HEIGHT,True)\ntrain_2_mask = aspect_ratio_resize_by_height(train_2_mask,HEIGHT,True)\ntrain_3_mask = aspect_ratio_resize_by_height(train_3_mask,HEIGHT,True)\nplot_row([train_1_mask,train_2_mask,train_3_mask])\nmasks = [train_1_mask,train_2_mask,train_3_mask]\ndel train_1_mask\ndel train_2_mask\ndel train_3_mask","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:27.478059Z","iopub.execute_input":"2023-06-06T09:39:27.478439Z","iopub.status.idle":"2023-06-06T09:39:30.636465Z","shell.execute_reply.started":"2023-06-06T09:39:27.478405Z","shell.execute_reply":"2023-06-06T09:39:30.635021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_val_mask = connect_images(masks)\ndel masks\nplt.imshow(train_val_mask,cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:30.638027Z","iopub.execute_input":"2023-06-06T09:39:30.638321Z","iopub.status.idle":"2023-06-06T09:39:32.243204Z","shell.execute_reply.started":"2023-06-06T09:39:30.638296Z","shell.execute_reply":"2023-06-06T09:39:32.241795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_a_mask = read_image(TEST_A_MASK)\ntest_b_mask = read_image(TEST_B_MASK)\nplot_row([test_a_mask,test_b_mask])\ntest_a_shape = test_a_mask.size\ntest_b_shape = test_b_mask.size\n","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:32.244744Z","iopub.execute_input":"2023-06-06T09:39:32.245121Z","iopub.status.idle":"2023-06-06T09:39:36.079832Z","shell.execute_reply.started":"2023-06-06T09:39:32.245093Z","shell.execute_reply":"2023-06-06T09:39:36.078399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating Custom Dataset for train and val","metadata":{}},{"cell_type":"code","source":"class ScansSemanticSegmentationDataset(Dataset):\n    def __init__(self,\n                 mask,\n                 scan,\n                 epoch_size,\n                 label=None,\n                 transform=None,\n                 validation_area=None,\n                 validation_patch=None,\n                 train=True,\n                 augmentation=None, \n                 preprocessing=None,                 \n                 ) -> None:\n        self.validation_patch = validation_patch\n        self.validation_area = validation_area\n        self.slice_batch_size = SLICE_BATCH_SIZE\n        self.buffer_size = BUFFER_SIZE\n        self.patch_size = PATCH_SIZE\n        self.height = HEIGHT\n        self.mask = mask\n        self.label = label\n        self.transform = transform\n        self.augmentation = augmentation\n        self.preprocessing = preprocessing\n        self.train = train\n        self.scan = scan\n        self.epoch_size = epoch_size\n        \n    def __len__(self):\n        return self.epoch_size\n        \n    def __getitem__(self,idx):\n        patch , area = self._get_valid_patch()\n        scan = self.get_slice_area(area)\n        label = self._get_label(area)\n        image = scan.numpy()\n        mask = label.numpy()\n        del scan\n        del label\n        if self.augmentation:\n            sample = self.augmentation(image=image, mask=mask)\n            image, mask = sample['image'], sample['mask']        \n        if self.preprocessing:\n            sample = self.preprocessing(image=image, mask=mask)\n            image, mask = sample['image'], sample['mask']\n        image = image.transpose(2, 0, 1)\n        return image,mask\n    \n            \n    def _get_valid_patch(self):\n        while True:\n            patch, area = self._get_random_patch()\n            if self.train == True:\n                if not self._is_patch_in_mask(area) or self._is_patch_in_validation(area):\n                    continue\n            elif self.train == False:\n                if not self._is_patch_in_validation(area):\n                    continue\n            break\n        return patch,area \n        \n    def _get_random_patch(self):\n        max_x = self.label.shape[0] - self.patch_size[0] - 2 * self.buffer_size\n        max_y = self.label.shape[1] - self.patch_size[1] - 2 * self.buffer_size\n        random_x = random.randint(self.buffer_size, max_x)\n        random_y = random.randint(self.buffer_size, max_y)\n        patch = patches.Rectangle(\n            [random_y - self.buffer_size, random_x - self.buffer_size],\n            self.patch_size[1] + 2 * self.buffer_size,\n            self.patch_size[0] + 2 * self.buffer_size,\n            linewidth=2,\n            edgecolor='r',\n            facecolor='none'\n        )\n        x,y = patch.get_xy()\n        width = patch.get_width()\n        height = patch.get_height()\n        area = (y,y+height, x,x+width)\n        return patch,area\n    \n\n    def _is_patch_in_validation(self,area):    \n        y1, y1_end, x1, x1_end = self.validation_area\n        y2, y2_end, x2, x2_end = area\n        in_val = y2 < y1_end and y2_end > y1 and x2 < x1_end and x2_end > x1\n        return in_val\n    \n    def _is_patch_in_mask(self, area):\n        y, y_end, x, x_end = area\n        patch_mask = self.mask[y:y_end, x:x_end]\n        max_value = np.max(patch_mask)\n        return max_value\n\n    \n    def _get_label(self,area):\n        y,y_end,x,x_end = area\n        label = self.label[y:y_end,x:x_end]\n        label = tf.cast(label, dtype=\"float32\")\n        return label \n    \n\n    def get_slice_area(self,area):\n        y,y_end,x,x_end = area\n        return self.scan[y:y_end,x:x_end,:]\n","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:36.081398Z","iopub.execute_input":"2023-06-06T09:39:36.081774Z","iopub.status.idle":"2023-06-06T09:39:36.098291Z","shell.execute_reply.started":"2023-06-06T09:39:36.081727Z","shell.execute_reply":"2023-06-06T09:39:36.097361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# load training data to memory\n# from offset 16  - 48 since not all of it is relevent and not everything can # get into memory in kaggle","metadata":{}},{"cell_type":"code","source":"train_scan , zstart,zend = get_slice_batch()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:39:36.099492Z","iopub.execute_input":"2023-06-06T09:39:36.100665Z","iopub.status.idle":"2023-06-06T09:46:27.743711Z","shell.execute_reply.started":"2023-06-06T09:39:36.100632Z","shell.execute_reply":"2023-06-06T09:46:27.741630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_training_augmentation():\n    train_transform = [\n        albu.HorizontalFlip(p=0.5),\n        albu.ShiftScaleRotate(scale_limit=0.5, rotate_limit=0, shift_limit=0.1, p=1, border_mode=0),\n        albu.GaussNoise (p=0.2),\n        albu.Perspective(p=0.5),\n        albu.OneOf(\n            [\n                albu.RandomBrightness(p=1),\n                albu.RandomGamma(p=1),\n            ],\n            p=0.9,\n        ),\n        albu.OneOf(\n            [\n                albu.Sharpen(p=1),\n                albu.Blur(blur_limit=3, p=1),\n                albu.MotionBlur(blur_limit=3, p=1),\n            ],\n            p=0.9,\n        ),\n    ]\n    return albu.Compose(train_transform)\n\n\ndef get_validation_augmentation():\n    test_transform = [\n    ]\n    return albu.Compose(test_transform)\n\n\ndef to_tensor(x, **kwargs):\n    return x.transpose(2, 0, 1).astype('float32')\n\n\ndef get_preprocessing(preprocessing_fn):\n    _transform = [\n        albu.Lambda(image=preprocessing_fn),\n        albu.Lambda(image=to_tensor, mask=to_tensor),\n    ]\n    return albu.Compose(_transform)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:46:27.746181Z","iopub.execute_input":"2023-06-06T09:46:27.746483Z","iopub.status.idle":"2023-06-06T09:46:27.755746Z","shell.execute_reply.started":"2023-06-06T09:46:27.746460Z","shell.execute_reply":"2023-06-06T09:46:27.754687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = ScansSemanticSegmentationDataset(mask=train_val_mask,\n                                                 scan=train_scan,\n                                                 epoch_size = TRAINING_EPOCH_SIZE,\n                                                 label=train_val_label,validation_area=validation_area,\n                                                 validation_patch=validation_patch,\n                                                 transform=None,\n                                                 augmentation=get_training_augmentation(),\n                                                 #preprocessing=get_preprocessing(preprocess_input),\n                                                 train=True)\nval_dataset = ScansSemanticSegmentationDataset(mask=train_val_mask,\n                                               scan=train_scan,\n                                               epoch_size = VALIDATION_EPOCH_SIZE,\n                                               label=train_val_label,\n                                               validation_area=validation_area,\n                                               validation_patch=validation_patch,\n                                               transform=None,\n                                                 augmentation=get_validation_augmentation(),\n                                                 #preprocessing=get_preprocessing(preprocess_input),\n                                               train=False)\n\ntrain_loader = DataLoader(train_dataset,batch_size=TRAIN_BATCH_SIZE,shuffle=False)\nval_loader = DataLoader(val_dataset,batch_size=VAL_BATCH_SIZE,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:46:27.764373Z","iopub.execute_input":"2023-06-06T09:46:27.765121Z","iopub.status.idle":"2023-06-06T09:46:27.778696Z","shell.execute_reply.started":"2023-06-06T09:46:27.765089Z","shell.execute_reply":"2023-06-06T09:46:27.777221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patch , area = train_dataset._get_random_patch()\nw = patch.get_width()\nh = patch.get_height()\nprint(h,w)\nSTRIDE = w\nSTRIDE","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:46:27.780056Z","iopub.execute_input":"2023-06-06T09:46:27.780413Z","iopub.status.idle":"2023-06-06T09:46:27.806233Z","shell.execute_reply.started":"2023-06-06T09:46:27.780379Z","shell.execute_reply":"2023-06-06T09:46:27.804889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# visualizing training area","metadata":{}},{"cell_type":"code","source":"fig,axs = plt.subplots(1,1,figsize=(10,10))\naxs.imshow(train_dataset.label)\nfor i in range(0,300):\n    patch,area = train_dataset._get_valid_patch()\n    axs.add_patch(patch)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:46:27.807459Z","iopub.execute_input":"2023-06-06T09:46:27.808276Z","iopub.status.idle":"2023-06-06T09:46:29.671324Z","shell.execute_reply.started":"2023-06-06T09:46:27.808244Z","shell.execute_reply":"2023-06-06T09:46:29.670148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# visualizing validation data","metadata":{}},{"cell_type":"code","source":"fig,axs = plt.subplots(1,1,figsize=(10,10))\naxs.imshow(val_dataset.label)\nfor i in range(0,300):\n    patch,area = val_dataset._get_valid_patch()\n    axs.add_patch(patch)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:46:29.673048Z","iopub.execute_input":"2023-06-06T09:46:29.673447Z","iopub.status.idle":"2023-06-06T09:46:31.491329Z","shell.execute_reply.started":"2023-06-06T09:46:29.673409Z","shell.execute_reply":"2023-06-06T09:46:31.489593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_iou_scores = []\nval_iou_scores = []\n\ntrain_f_scores = []\nval_f_scores = []\n\ntrain_dice_losses = []\nval_dice_losses = []\n\ntrain_accuracies = []\nval_accuracies = []\n\nepochs_numbers = []","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:46:31.493385Z","iopub.execute_input":"2023-06-06T09:46:31.494268Z","iopub.status.idle":"2023-06-06T09:46:31.502093Z","shell.execute_reply.started":"2023-06-06T09:46:31.494216Z","shell.execute_reply":"2023-06-06T09:46:31.500607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor i in range(0, NUM_EPOCHS):\n    print('\\nEpoch: {}'.format(i))\n    train_logs = train_epoch.run(train_loader)\n    valid_logs = valid_epoch.run(val_loader)\n    epochs_numbers.append(i)\n\n\n    train_iou_scores.append(train_logs['iou_score'])\n    train_f_scores.append(train_logs['fscore'])\n    train_dice_losses.append(train_logs['dice_loss'])\n    train_accuracies.append(train_logs['accuracy'])\n\n    val_iou_scores.append(valid_logs['iou_score'])\n    val_f_scores.append(valid_logs['fscore'])\n    val_dice_losses.append(valid_logs['dice_loss'])\n    val_accuracies.append(valid_logs['accuracy'])\n\n\n    # if max_score < valid_logs['iou_score']:\n    #     max_score = valid_logs['iou_score']\n    #     torch.save(model, './best_model.pth')\n    #     print('model saved')\n    if i == 25:\n        optimizer.param_groups[0]['lr'] = 1e-5\n        print('Decrease decoder learning rate to 1e-5!')","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:46:31.504017Z","iopub.execute_input":"2023-06-06T09:46:31.504513Z","iopub.status.idle":"2023-06-06T09:55:38.443709Z","shell.execute_reply.started":"2023-06-06T09:46:31.504478Z","shell.execute_reply":"2023-06-06T09:55:38.442296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_scan","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:38.445713Z","iopub.execute_input":"2023-06-06T09:55:38.446130Z","iopub.status.idle":"2023-06-06T09:55:38.452420Z","shell.execute_reply.started":"2023-06-06T09:55:38.446098Z","shell.execute_reply":"2023-06-06T09:55:38.450682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(epochs_numbers,train_iou_scores,label='train')\nplt.plot(epochs_numbers,val_iou_scores,label='validation')\nplt.xlabel('epoch')\nplt.xlabel('iou')\nplt.title('graph of epoch, iou')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:38.454064Z","iopub.execute_input":"2023-06-06T09:55:38.454596Z","iopub.status.idle":"2023-06-06T09:55:38.722836Z","shell.execute_reply.started":"2023-06-06T09:55:38.454559Z","shell.execute_reply":"2023-06-06T09:55:38.721819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(epochs_numbers,train_f_scores,label='train')\nplt.plot(epochs_numbers,val_f_scores,label='validation')\nplt.xlabel('epoch')\nplt.xlabel('f score')\nplt.title('graph of epoch, f score')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:38.724191Z","iopub.execute_input":"2023-06-06T09:55:38.724505Z","iopub.status.idle":"2023-06-06T09:55:38.984408Z","shell.execute_reply.started":"2023-06-06T09:55:38.724479Z","shell.execute_reply":"2023-06-06T09:55:38.983434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(epochs_numbers,train_dice_losses,label='train')\nplt.plot(epochs_numbers,val_dice_losses,label='validation')\nplt.xlabel('epoch')\nplt.xlabel('dice loss')\nplt.title('graph of epoch, loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:38.985682Z","iopub.execute_input":"2023-06-06T09:55:38.986152Z","iopub.status.idle":"2023-06-06T09:55:39.244684Z","shell.execute_reply.started":"2023-06-06T09:55:38.986125Z","shell.execute_reply":"2023-06-06T09:55:39.243046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(epochs_numbers,train_dice_losses,label='train')\nplt.plot(epochs_numbers,val_dice_losses,label='validation')\nplt.xlabel('epoch')\nplt.xlabel('dice loss')\nplt.title('graph of epoch, loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:39.246464Z","iopub.execute_input":"2023-06-06T09:55:39.246918Z","iopub.status.idle":"2023-06-06T09:55:39.487490Z","shell.execute_reply.started":"2023-06-06T09:55:39.246881Z","shell.execute_reply":"2023-06-06T09:55:39.486428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(epochs_numbers,train_accuracies,label='train')\nplt.plot(epochs_numbers,val_accuracies,label='validation')\nplt.xlabel('epoch')\nplt.xlabel('accuracy')\nplt.title('graph of epoch, accuracy')\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:39.488956Z","iopub.execute_input":"2023-06-06T09:55:39.489326Z","iopub.status.idle":"2023-06-06T09:55:39.726274Z","shell.execute_reply.started":"2023-06-06T09:55:39.489292Z","shell.execute_reply":"2023-06-06T09:55:39.724492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del epochs_numbers\ndel train_iou_scores\ndel val_iou_scores\ndel train_f_scores\ndel val_f_scores\ndel train_dice_losses\ndel val_dice_losses\ndel train_accuracies\ndel val_accuracies","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:39.728863Z","iopub.execute_input":"2023-06-06T09:55:39.729340Z","iopub.status.idle":"2023-06-06T09:55:39.735937Z","shell.execute_reply.started":"2023-06-06T09:55:39.729302Z","shell.execute_reply":"2023-06-06T09:55:39.734023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_test_scans(paths,target_shape,dtype=\"float32\"):\n    test_scans = []\n    for scan_slice in tqdm(paths[Z_START:Z_END]):\n        img = Image.open(scan_slice)\n        img = img.resize(target_shape)\n        img = np.array(img,dtype=dtype)\n        test_scans.append(img)\n        del img\n    stack = tf.stack(test_scans,axis=-1)\n    print(stack.shape)\n    del test_scans\n    stack = np.transpose(stack,(1,0,2))\n    return stack\n\n    ","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:39.737979Z","iopub.execute_input":"2023-06-06T09:55:39.738368Z","iopub.status.idle":"2023-06-06T09:55:39.756324Z","shell.execute_reply.started":"2023-06-06T09:55:39.738334Z","shell.execute_reply":"2023-06-06T09:55:39.754659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_shape_size_by_stride(shape,stride):\n    x_target = math.ceil(shape[0] / stride) * stride\n    y_target = math.ceil(shape[1] / stride) * stride\n    target_shape = (x_target,y_target)\n    return target_shape","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:39.757687Z","iopub.execute_input":"2023-06-06T09:55:39.758040Z","iopub.status.idle":"2023-06-06T09:55:39.773434Z","shell.execute_reply.started":"2023-06-06T09:55:39.758013Z","shell.execute_reply":"2023-06-06T09:55:39.771664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = []","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:39.774701Z","iopub.execute_input":"2023-06-06T09:55:39.775022Z","iopub.status.idle":"2023-06-06T09:55:39.787578Z","shell.execute_reply.started":"2023-06-06T09:55:39.774997Z","shell.execute_reply":"2023-06-06T09:55:39.786467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_a_shape","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:39.788900Z","iopub.execute_input":"2023-06-06T09:55:39.789215Z","iopub.status.idle":"2023-06-06T09:55:39.804809Z","shell.execute_reply.started":"2023-06-06T09:55:39.789188Z","shell.execute_reply":"2023-06-06T09:55:39.803640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_shape  = get_shape_size_by_stride(test_a_shape,STRIDE)\ntarget_shape","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:39.805802Z","iopub.execute_input":"2023-06-06T09:55:39.806795Z","iopub.status.idle":"2023-06-06T09:55:39.821595Z","shell.execute_reply.started":"2023-06-06T09:55:39.806739Z","shell.execute_reply":"2023-06-06T09:55:39.819849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_a_scan = load_test_scans(test_a_image_paths,target_shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:55:39.822940Z","iopub.execute_input":"2023-06-06T09:55:39.823329Z","iopub.status.idle":"2023-06-06T09:56:03.816238Z","shell.execute_reply.started":"2023-06-06T09:55:39.823303Z","shell.execute_reply":"2023-06-06T09:56:03.814732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pil_to_tensor = transforms.PILToTensor()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_a = torch.transpose(pil_to_tensor(test_a_mask).squeeze(0), 0, 1)\nfull_mask = torch.zeros(test_a_scan.shape[0], test_a_scan.shape[1],dtype=torch.float32) \nfor i in range(0,test_a_scan.shape[0] - STRIDE + 1,STRIDE):\n    for j in range(0,test_a_scan.shape[1] - STRIDE + 1,STRIDE):\n        test_mask_patch = test_a_scan[i:i+STRIDE, j:j+STRIDE,:]\n        test_mask_patch = np.transpose(test_mask_patch,(2,0,1))\n        test_mask_patch = torch.from_numpy(test_mask_patch)\n        test_mask_patch = test_mask_patch.unsqueeze(0)\n        with torch.no_grad():\n            output = model(test_mask_patch)\n            output= output.squeeze(0)\n        full_mask[i:i+STRIDE,j:j+STRIDE] += output.squeeze(0)\n        del test_mask_patch,output\nfull_mask = full_mask.unsqueeze(0).unsqueeze(0)\nfull_mask = F.interpolate(full_mask, size=test_a_shape, mode='area').squeeze()\nfull_mask = torch.where(full_mask >= THRESHOLD, torch.tensor(1), torch.tensor(0))\nfull_mask = full_mask * mask_a","metadata":{"execution":{"iopub.status.busy":"2023-06-06T09:57:50.950068Z","iopub.execute_input":"2023-06-06T09:57:50.950476Z","iopub.status.idle":"2023-06-06T10:00:04.114499Z","shell.execute_reply.started":"2023-06-06T09:57:50.950447Z","shell.execute_reply":"2023-06-06T10:00:04.112646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoded_test_a_mask = rle_encode(full_mask) \npred_a  = {\"Id\":\"a\",\"Predicted\":encoded_test_a_mask}\npreds.append(pred_a)\nplt.imshow(full_mask,cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-06-06T10:00:04.117228Z","iopub.execute_input":"2023-06-06T10:00:04.117652Z","iopub.status.idle":"2023-06-06T10:00:05.226592Z","shell.execute_reply.started":"2023-06-06T10:00:04.117617Z","shell.execute_reply":"2023-06-06T10:00:05.224693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del full_mask\ndel mask_a\ndel test_a_scan\ndel encoded_test_a_mask\ndel pred_a","metadata":{"execution":{"iopub.status.busy":"2023-06-06T10:00:49.055433Z","iopub.execute_input":"2023-06-06T10:00:49.055875Z","iopub.status.idle":"2023-06-06T10:00:49.103160Z","shell.execute_reply.started":"2023-06-06T10:00:49.055840Z","shell.execute_reply":"2023-06-06T10:00:49.101838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_shape  = get_shape_size_by_stride(test_b_shape,STRIDE)\ntarget_shape","metadata":{"execution":{"iopub.status.busy":"2023-06-06T10:00:59.561550Z","iopub.execute_input":"2023-06-06T10:00:59.561956Z","iopub.status.idle":"2023-06-06T10:00:59.570300Z","shell.execute_reply.started":"2023-06-06T10:00:59.561925Z","shell.execute_reply":"2023-06-06T10:00:59.568676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_b_scan = load_test_scans(test_b_image_paths,target_shape)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-06T10:01:03.077311Z","iopub.execute_input":"2023-06-06T10:01:03.077765Z","iopub.status.idle":"2023-06-06T10:01:56.505023Z","shell.execute_reply.started":"2023-06-06T10:01:03.077716Z","shell.execute_reply":"2023-06-06T10:01:56.504095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_b = torch.transpose(pil_to_tensor(test_b_mask).squeeze(0), 0, 1)\nfull_mask = torch.zeros(test_b_scan.shape[0], test_b_scan.shape[1],dtype=torch.float32) \nfor i in range(0,test_b_scan.shape[0] - STRIDE + 1,STRIDE):\n    for j in range(0,test_b_scan.shape[1] - STRIDE + 1,STRIDE):\n        test_mask_patch = test_b_scan[i:i+STRIDE, j:j+STRIDE,:]\n        test_mask_patch = np.transpose(test_mask_patch,(2,0,1))\n        test_mask_patch = torch.from_numpy(test_mask_patch)\n        test_mask_patch = test_mask_patch.unsqueeze(0)\n        with torch.no_grad():\n            output = model(test_mask_patch)\n            output= output.squeeze(0)\n        full_mask[i:i+STRIDE,j:j+STRIDE] += output.squeeze(0)\n        del test_mask_patch,output\nfull_mask = full_mask.unsqueeze(0).unsqueeze(0)\nfull_mask = F.interpolate(full_mask, size=test_b_shape, mode='area').squeeze()\nfull_mask = torch.where(full_mask >= THRESHOLD, torch.tensor(1), torch.tensor(0))\nfull_mask = full_mask * mask_b","metadata":{"execution":{"iopub.status.busy":"2023-06-06T10:22:41.967412Z","iopub.execute_input":"2023-06-06T10:22:41.967934Z","iopub.status.idle":"2023-06-06T10:22:42.262654Z","shell.execute_reply.started":"2023-06-06T10:22:41.967897Z","shell.execute_reply":"2023-06-06T10:22:42.260791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoded_test_b_mask = rle_encode(full_mask) \npred_b  = {\"Id\":\"b\",\"Predicted\":encoded_test_b_mask}\npreds.append(pred_b)\nplt.imshow(full_mask,cmap=\"gray\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del full_mask\ndel mask_b\ndel test_b_scan\ndel encoded_test_b_mask\ndel pred_b","metadata":{"execution":{"iopub.status.busy":"2023-06-06T10:06:40.009074Z","iopub.execute_input":"2023-06-06T10:06:40.009427Z","iopub.status.idle":"2023-06-06T10:06:40.267902Z","shell.execute_reply.started":"2023-06-06T10:06:40.009401Z","shell.execute_reply":"2023-06-06T10:06:40.266274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(preds)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T10:06:40.270391Z","iopub.execute_input":"2023-06-06T10:06:40.270838Z","iopub.status.idle":"2023-06-06T10:06:40.290054Z","shell.execute_reply.started":"2023-06-06T10:06:40.270804Z","shell.execute_reply":"2023-06-06T10:06:40.288404Z"},"trusted":true},"execution_count":null,"outputs":[]}]}