{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#!pip install segmentation_models_pytorch --quiet","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom torch.utils.data import DataLoader, Dataset, SubsetRandomSampler\nimport cv2,torch ,os\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nfrom albumentations import ImageOnlyTransform\n\nfrom sklearn.metrics import roc_auc_score, accuracy_score, f1_score, log_loss\n\nfrom torch.utils.data import DataLoader\nfrom torch.cuda.amp import autocast, GradScaler\nimport pandas as pd\n \nimport pickle,sys,warnings,gc,math,time,random ,shutil,importlib,argparse,datetime\nfrom pathlib import Path\nfrom contextlib import contextmanager\nfrom collections import defaultdict, Counter\n\n\nimport scipy as sp\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nfrom tqdm.auto import tqdm\nfrom functools import partial\n\nimport torch.nn as nn\nfrom torch.optim import Adam, SGD, AdamW\n\n\nfrom torch.optim.lr_scheduler import CosineAnnealingWarmRestarts, CosineAnnealingLR, ReduceLROnPlateau","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('/kaggle/input/pretrainedmodels/pretrainedmodels-0.7.4')\nsys.path.append('/kaggle/input/efficientnet-pytorch/EfficientNet-PyTorch-master')\nsys.path.append('/kaggle/input/timm-pytorch-image-models/pytorch-image-models-master')\nsys.path.append('/kaggle/input/segmentation-models-pytorch/segmentation_models.pytorch-master')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import segmentation_models_pytorch as smp\n#from segmentation_models_pytorch import utils","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Configuration","metadata":{}},{"cell_type":"code","source":"class CFG:\n    # ============== comp exp name =============\n    comp_name = 'vesuvius'\n    comp_dir_path = '/kaggle/input/'\n    comp_folder_name = 'vesuvius-challenge-ink-detection'\n    comp_dataset_path = f'{comp_dir_path}{comp_folder_name}/'\n    \n    # ============== pred target =============\n    target_size = 1\n\n    # ============== model cfg =============\n    model_name = 'Unet'\n    backbone= 'efficientnet-b8'\n    #resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152',\n    #'resnext50_32x4d', 'resnext101_32x4d', 'resnext101_32x8d', \n    #'resnext101_32x16d', 'resnext101_32x32d', 'resnext101_32x48d',\n    #'dpn68', 'dpn68b', 'dpn92', 'dpn98', 'dpn107', 'dpn131', 'vgg11',\n    #'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn', 'vgg19',#\n    #'vgg19_bn', 'senet154', 'se_resnet50', 'se_resnet101', 'se_resnet152',\n    #'se_resnext50_32x4d', 'se_resnext101_32x4d', 'densenet121', 'densenet169',\n    #'densenet201', 'densenet161', 'inceptionresnetv2', 'inceptionv4', 'efficientnet-b0',\n    #'efficientnet-b1', 'efficientnet-b2', 'efficientnet-b3', 'efficientnet-b4', 'efficientnet-b5',\n    #'efficientnet-b6', 'efficientnet-b7', 'mobilenet_v2', 'xception', 'timm-efficientnet-b0',\n    #'timm-efficientnet-b1', 'timm-efficientnet-b2', 'timm-efficientnet-b3', 'timm-efficientnet-b4',\n    #'timm-efficientnet-b5', 'timm-efficientnet-b6', 'timm-efficientnet-b7', 'timm-efficientnet-b8',\n    #'timm-efficientnet-l2', 'timm-tf_efficientnet_lite0', 'timm-tf_efficientnet_lite1',\n    #'timm-tf_efficientnet_lite2', 'timm-tf_efficientnet_lite3', 'timm-tf_efficientnet_lite4',\n    #'timm-resnest14d', 'timm-resnest26d', 'timm-resnest50d', 'timm-resnest101e', 'timm-resnest200e',\n    #'timm-resnest269e', 'timm-resnest50d_4s2x40d', 'timm-resnest50d_1s4x24d', 'timm-res2net50_26w_4s',\n    #'timm-res2net101_26w_4s', 'timm-res2net50_26w_6s', 'timm-res2net50_26w_8s', 'timm-res2net50_48w_2s',\n    #'timm-res2net50_14w_8s', 'timm-res2next50', 'timm-regnetx_002', 'timm-regnetx_004', 'timm-regnetx_006',\n    #'timm-regnetx_008', 'timm-regnetx_016', 'timm-regnetx_032', 'timm-regnetx_040', 'timm-regnetx_064',\n    #'timm-regnetx_080', 'timm-regnetx_120', 'timm-regnetx_160', 'timm-regnetx_320', 'timm-regnety_002',\n    #'timm-regnety_004', 'timm-regnety_006', 'timm-regnety_008', 'timm-regnety_016', 'timm-regnety_032',\n    #'timm-regnety_040', 'timm-regnety_064', 'timm-regnety_080', 'timm-regnety_120', 'timm-regnety_160',\n    #'timm-regnety_320', 'timm-skresnet18', 'timm-skresnet34', 'timm-skresnext50_32x4d', \n    #'timm-mobilenetv3_large_075', 'timm-mobilenetv3_large_100', 'timm-mobilenetv3_large_minimal_100',\n    #'timm-mobilenetv3_small_075', 'timm-mobilenetv3_small_100', 'timm-mobilenetv3_small_minimal_100',\n    #'timm-gernet_s', 'timm-gernet_m', 'timm-gernet_l', 'mit_b0', 'mit_b1', 'mit_b2', 'mit_b3', 'mit_b4'\n    #, 'mit_b5', 'mobileone_s0', 'mobileone_s1', 'mobileone_s2', 'mobileone_s3', 'mobileone_s4'\n    '''\n    EfficientNet-B4 ResNet50  DenseNet161 MobileNetV2 EfficientNet-B0\n    '''\n    #backbone = 'se_resnext50_32x4d'\n    #backbone = 'efficientnet-b5'\n\n    in_chans = 13 # 65\n    # ============== training cfg =============\n    size = 224\n    tile_size = 224\n    stride = tile_size // 2\n\n    train_batch_size = 8 \n    valid_batch_size = train_batch_size * 2\n\n    # adamW warmupあり\n    warmup_factor = 10\n    lr = 1e-4 / warmup_factor\n\n    # ============== fold =============\n    valid_id = 1\n\n    # objective_cv = 'binary'  # 'binary', 'multiclass', 'regression'\n    metric_direction = 'maximize'  # maximize, 'minimize'\n    # metrics = 'dice_coef'\n    # ============== fixed =============\n    pretrained = True\n    inf_weight = 'best'  # 'best'\n\n    min_lr = 1e-6\n    weight_decay = 1e-6\n    max_grad_norm = 1000\n\n    print_freq = 50\n    num_workers = 2\n\n    seed = 42\n    #'''\n    # ============== set dataset path =============\n    print('set dataset path')\n    exp_name = 'exp_name'\n    outputs_path = f'/kaggle/working/outputs/{comp_name}/{exp_name}/'\n\n    submission_dir = outputs_path + 'submissions/'\n    submission_path = submission_dir + f'submission_{exp_name}.csv'\n\n    model_dir = outputs_path + \\\n        f'{comp_name}-models/'\n\n    figures_dir = outputs_path + 'figures/'\n\n    log_dir = outputs_path + 'logs/'\n    log_path = log_dir + f'{exp_name}.txt'\n    #'''\n    # ============== augmentation =============\n    \n    train_aug_list = [\n        A.RandomBrightnessContrast(p=0.25),\n        A.RandomGridShuffle(grid=(3, 3), always_apply=False, p=0.4),\n        A.OneOf([\n                A.GaussNoise(var_limit=[10, 50]),\n                A.GaussianBlur(),\n                A.MotionBlur(),\n                ], p=0.4),\n        A.GridDistortion(num_steps=5, distort_limit=0.3, p=0.5),\n        A.CoarseDropout(max_holes=1, max_width=int(size * 0.3), max_height=int(size * 0.3), \n                        mask_fill_value=0, p=0.5),\n        A.Normalize(mean= [0] * in_chans, std= [1] * in_chans),\n        ToTensorV2(transpose_mask=True)\n    ]\n\n    valid_aug_list = [\n        A.OneOf([\n                A.GaussNoise(var_limit=[10, 50]),\n                A.GaussianBlur(),\n                A.MotionBlur(),\n                ], p=0.4),\n        A.RandomGridShuffle(grid=(3, 3), always_apply=False, p=0.4),\n        A.Normalize(mean= [0] * in_chans, std= [1] * in_chans),\n        ToTensorV2(transpose_mask=True)\n    ]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Helper Functions","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef print_image_details(image):\n    print('///////////////////////')\n    print(f\"Image shape: {image.shape if isinstance(image, np.ndarray) else image.size }\")\n    print(f\"Pixel values (min, max, mean, std): ({np.min(image)}, {np.max(image)}, {np.mean(image):.2f}, {np.std(image):.2f})\")\n    print(f\"Data type: {type(image)}\")\n    print('//////////////////////')\n\ndef create_subplots(images:list, titles:list = None,wantAxis = False,figsize=(12, 8)):\n    fig = plt.figure(figsize=figsize)\n    num_subplots = len(images)\n    for i in range(num_subplots):\n        plt.subplot(1, num_subplots, i+1)\n        plt.imshow(images[i], cmap=\"gray\")\n        plt.axis(\"off\" if wantAxis ==False else \"on\")\n        plt.title(titles[i] if titles != None else i)\n    plt.show()\n\ndef show(image,WantAxis=True,text=\"Image\"):\n    create_subplots([image],[text],WantAxis,(6,4))\n\n\ndef getCut(img,left,right,up,down,toshow = False):\n    x, y = img.shape\n    left_cor,up_cor = (left > 0 and left < x),(up > 0 and up < y)\n    right_cor,down_cor = (right > left_cor and right < x),(down > up_cor and down < y)\n    if (left_cor and right_cor and up_cor and down_cor):\n        region = (left, up, right, down)\n        cropped_image = img.crop(region)\n        # if its type ndarray , the size is opposite...\n        if toshow == True:\n            plt.imshow(cropped_image, cmap='gray')\n            plt.show()\n        return cropped_image\n    return \n\n    \n    \ndef create_param_table():\n    # Create an empty DataFrame with the desired columns\n    columns = ['Parameter', 'Value', 'Data Type', 'Size', 'Length', 'Shape', 'Memory Usage', 'Missing Values']\n    param_table = pd.DataFrame(columns=columns)\n    styled_table = None  #global variable\n    \n    def add_param(param_name=None, param_value=None, toshow=False):\n        nonlocal styled_table  # Access the styled_table from the outer scope\n        \n        if param_name is None and param_value is None and toshow is False:\n            return \"Nothing to display.\"\n        \n        param_size = param_value.size if hasattr(param_value, 'size') else '-'\n        param_length = len(param_value) if hasattr(param_value, '__len__') else '-'\n        param_shape = param_value.shape if hasattr(param_value, 'shape') else '-'\n        param_data_type = type(param_value).__name__\n        param_memory_usage = sys.getsizeof(param_value)\n        param_missing_values = param_value.isnull().sum() if isinstance(param_value, pd.DataFrame) else '-'\n        param_value = 'long array numbers' if isinstance(param_value, list) else param_value\n        # Add the parameter and its details to the table\n        param_table.loc[len(param_table)] = [param_name, param_value, param_data_type, param_size, param_length,\n                                             param_shape, param_memory_usage, param_missing_values]\n        \n        # Apply color to the \"Value\" column\n        styled_table = param_table.style.applymap(lambda x: 'background-color: lightblue', subset=['Value'])\n        \n        # Set styling options for the table\n        styled_table.set_table_styles([\n            {'selector': 'table', 'props': [('border', '2px solid grey'), ('border-collapse', 'collapse')]},\n            {'selector': 'th', 'props': [('background-color', 'lightgrey'), ('border', '1px solid black'),\n                                          ('padding', '5px'), ('text-align', 'left')]},\n            {'selector': 'td', 'props': [('border', '1px solid black'), ('padding', '5px')]}\n        ])\n        \n        if toshow:\n            # Display the styled table\n            display(styled_table)\n        \n        # Return the table for further access and usage\n        return param_table\n    \n    # Function to print the table without adding a new parameter\n    def print_table():\n        if styled_table is not None:\n            display(styled_table)\n        else:\n            print(\"No table to display.\")\n    #now can access and call the print_table() even outside the create_param_table() function\n    add_param.print_table = print_table\n    \n    return add_param\n\n\ndef plot_metrics(train_logs, valid_logs):\n    epochs = range(1, len(train_logs['loss']) + 1)\n\n    # Plot loss\n    plt.figure(figsize=(16, 4))\n    plt.subplot(1, 3, 1)\n    plt.plot(epochs, train_logs['loss'], 'b-', label='Train Loss')\n    plt.plot(epochs, valid_logs['loss'], 'r-', label='Validation Loss')\n    plt.title('Train and Validation Loss')\n    plt.xlabel('Epochs')\n    plt.ylabel('Loss')\n    plt.legend()\n\n    # Plot F-score\n    plt.subplot(1, 3, 2)\n    plt.plot(epochs, train_logs['fscore'], 'b-', label='Train F-score')\n    plt.plot(epochs, valid_logs['fscore'], 'r-', label='Validation F-score')\n    plt.title('Train and Validation F-score')\n    plt.xlabel('Epochs')\n    plt.ylabel('F-score')\n    plt.legend()\n\n    # Plot IoU\n    plt.subplot(1, 3, 3)\n    plt.plot(epochs, train_logs['iou'], 'b-', label='Train IoU')\n    plt.plot(epochs, valid_logs['iou'], 'r-', label='Validation IoU')\n    plt.title('Train and Validation IoU')\n    plt.xlabel('Epochs')\n    plt.ylabel('IoU')\n    plt.legend()\n\n    # Adjust layout and display the plot\n    plt.tight_layout()\n    plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    # pixels = (pixels >= thr).astype(int)\n    \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)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle12(output):\n    flat_img = np.where(output.flatten() > 0.4, 1, 0).astype(np.uint8)\n    starts = np.array((flat_img[:-1] == 0) & (flat_img[1:] == 1))\n    ends = np.array((flat_img[:-1] == 1) & (flat_img[1:] == 0))\n    starts_ix = np.where(starts)[0] + 2\n    ends_ix = np.where(ends)[0] + 2\n    lengths = ends_ix - starts_ix\n    return \" \".join(map(str, sum(zip(starts_ix, lengths), ())))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ndef init_logger(log_file):\n    from logging import getLogger, INFO, FileHandler, Formatter, StreamHandler\n    logger = getLogger(__name__)\n    logger.setLevel(INFO)\n    handler1 = StreamHandler()\n    handler1.setFormatter(Formatter(\"%(message)s\"))\n    handler2 = FileHandler(filename=log_file)\n    handler2.setFormatter(Formatter(\"%(message)s\"))\n    logger.addHandler(handler1)\n    logger.addHandler(handler2)\n    return logger\n\ndef set_seed(seed=None, cudnn_deterministic=True):\n    if seed is None:\n        seed = 42\n\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = cudnn_deterministic\n    torch.backends.cudnn.benchmark = False\n    '''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''def make_dirs(cfg):\n    for dir in [cfg.model_dir, cfg.figures_dir, cfg.submission_dir, cfg.log_dir]:\n        os.makedirs(dir, exist_ok=True)\n   '''     ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ndef cfg_init(cfg, mode='train'):\n    set_seed(cfg.seed)\n    # set_env_name()\n    # set_dataset_path(cfg)\n\n    if mode == 'train':\n        make_dirs(cfg)\n        '''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''cfg_init(CFG)\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\nLogger = init_logger(log_file=CFG.log_path)\n\nLogger.info('\\n\\n-------- exp_info -----------------')\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image & Mask","metadata":{}},{"cell_type":"code","source":"def read_image_mask(fragment_id, mode='train'):\n    \n    \"\"\"\n    This function takes a fragment ID (1/2/3) as input and returns the middle 6 layers (29-34) \n    out of a total of 65 layers from the surface_volume file. It also pads both the surface_volume \n    file and the corresponding mask, and normalizes the mask values to range between 0 and 1.\n    \n    Args:\n        fragment_id (int): ID of the fragment (1/2/3).\n    \n    Returns:\n        surface_volume_layers (numpy array): The middle 6 layers (29-34) of the surface_volume file stacked, after padding.\n        normalized_mask (numpy array): The corresponding mask, padded and normalized between 0 and 1.\n    \"\"\"\n    \n    images = []\n    \n    # idxs = range(65)\n    #mid = 65 // 2\n    start = 22#mid - CFG.in_chans // 2\n    end = 35# mid + CFG.in_chans // 2\n    idxs = range(start, end)\n    #pixelsValueThreshHold = 200 if fragment_id == 3 else 220\n    #pixelsValueThreshHold = 140 if fragment_id == 3 else 130\n    #idxs = range(21, 34) if fragment_id == 1 else (range(25, 38) if fragment_id == 2 else range(20, 33))\n    #idxs = range(22, 33) if fragment_id == 1 else (range(26, 37) if fragment_id == 2 else range(21, 32))\n    #idxs = range(24, 37) if fragment_id == 1 else (range(29, 42) if fragment_id == 2 else range(22, 35))\n\n    for i in tqdm(idxs):\n        ForStringNumber = str(\"{:02d}\".format(i))\n        tpath = CFG.comp_dataset_path + f\"{mode}/{fragment_id}/surface_volume/{ForStringNumber}.tif\"\n        image = cv2.imread(tpath,0)\n        \n        pad0 = (CFG.tile_size - image.shape[0] % CFG.tile_size)\n        pad1 = (CFG.tile_size - image.shape[1] % CFG.tile_size)\n\n        image = np.pad(image, [(0, pad0), (0, pad1)], constant_values=0)\n\n        images.append(image)\n        \n    images = np.stack(images, axis=2)\n    #images[images > pixelsValueThreshHold] = 0\n    #images = images# / np.max(images)\n    if mode == 'test':\n        return images\n\n    mask = cv2.imread(CFG.comp_dataset_path + f\"train/{fragment_id}/inklabels.png\", 0)\n    mask = np.pad(mask, [(0, pad0), (0, pad1)], constant_values=0)\n    mask = mask.astype('float32')\n    #mask[mask > pixelsValueThreshHold] = 0\n    mask = mask / np.float32(255)\n    return images, mask\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''i, m = read_image_mask(1)\nh = reduce_object(i,500)\ncreate_subplots([i[:,:,0],h[:,:,0]])\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nimport numpy as np\n\ndef reduce_object(matrix, depth=1):\n    rows, cols, channels = matrix.shape\n    \n    reduced_matrix = np.copy(matrix)\n\n    # Check left and right sides\n    for i in range(rows):\n        for channel in range(channels):\n            left_idx = np.argmax(matrix[i, :, channel] != 0)\n            right_idx = np.argmax(matrix[i, ::-1, channel] != 0)\n\n            if left_idx < right_idx:\n                reduced_matrix[i, left_idx:min(left_idx + depth, right_idx), channel] = 1\n            elif right_idx < left_idx:\n                reduced_matrix[i, max(0, cols - right_idx - 1 - depth):cols - right_idx - 1, channel] = 1\n\n    # Check top and bottom sides\n    for j in range(cols):\n        for channel in range(channels):\n            top_idx = np.argmax(matrix[:, j, channel] != 0)\n            bottom_idx = np.argmax(matrix[::-1, j, channel] != 0)\n\n            if top_idx < bottom_idx:\n                reduced_matrix[top_idx:min(top_idx + depth, bottom_idx), j, channel] = 1\n            elif bottom_idx < top_idx:\n                reduced_matrix[max(0, rows - bottom_idx - 1 - depth):rows - bottom_idx - 1, j, channel] = 1\n\n    return reduced_matrix\n\n\ndef preserve_zeros(image1, image2, a_min1, a_max1):\n    newi = image1.copy()\n    maski = image2.copy()\n    x = newi.shape[0]\n    y = newi.shape[1]\n    z = newi.shape[2]\n    j = np.zeros((x, y))\n    for n in range(z):\n        j = j + newi[:,:,n]\n    \n    # Find the locations with zeros in the first image\n    zero_indices = np.where(j == 0)\n    \n    # Set those locations to zeros in the second image\n    maski[zero_indices] = 0\n    newi = np.clip(newi, a_min=a_min1, a_max=a_max1)\n    return newi ,maski\n\n\ndef separate_pixels(image):\n    # Convert grayscale image to RGB\n    if len(image.shape) == 2:\n        image = np.expand_dims(image, axis=-1)\n        image = np.repeat(image, 3, axis=-1)\n    \n    # Separate even and odd pixels\n    image1 = image[::2, ::2, :]\n    image2 = image[1::2, 1::2, :]\n    \n    return image1, image2\n\n\n\ndef opposite_pooling(image):\n    # Check if the image is grayscale (2D shape)\n    if len(image.shape) == 2:\n        image = np.expand_dims(image, axis=-1)\n    \n    # Determine the size of the output image\n    rows, cols, _ = image.shape\n    new_rows, new_cols = rows * 2, cols * 2\n    \n    # Upsample the image using nearest-neighbor interpolation\n    upsampled_image = cv2.resize(image, (new_cols, new_rows))\n    \n    # Split the upsampled image into four quadrants\n    image1 = upsampled_image[::2, ::2]\n    image2 = upsampled_image[::2, 1::2]\n    image3 = upsampled_image[1::2, ::2]\n    image4 = upsampled_image[1::2, 1::2]\n    \n    return image1, image2, image3, image4\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''i,m = read_image_mask(1)\ni1,i2,i3,i4 = opposite_pooling(i)\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n#train with other things \ndef get_train_valid_dataset():\n\n    train_images = []\n    train_masks = []\n\n    valid_images = []\n    valid_masks = []\n    valid_xyxys = []\n\n    for fragment_id in range(1, 4):\n\n        image, mask = read_image_mask(fragment_id)\n        x1_list = list(range(0, image.shape[1]-CFG.tile_size+1, CFG.stride))\n        y1_list = list(range(0, image.shape[0]-CFG.tile_size+1, CFG.stride))\n        \n        for y1 in y1_list:\n            for x1 in x1_list:\n                y2 = y1 + CFG.tile_size\n                x2 = x1 + CFG.tile_size\n                imageres = reduce_object(image,500)\n                #newImage,newMask = preserve_zeros(imageres,mask,0,130)\n                #newImage,newMask = imageres,mask\n                i1,i2, = separate_pixels(imageres)\n                \n                \n                n1,n2 = i1[y1:y2, x1:x2],i2[y1:y2, x1:x2]\n                m1,m2 = separate_pixels(mask)\n                \n                o1,o2 = m1[y1:y2, x1:x2, None],m2[y1:y2, x1:x2, None]\n                if fragment_id == CFG.valid_id:\n                    valid_images.append(n1)\n                    valid_images.append(n2)\n                    \n                    \n                    valid_masks.append(o1)\n                    valid_masks.append(o2)\n                  \n\n                    valid_xyxys.append([x1, y1, x2, y2])\n                    valid_xyxys.append([x1, y1, x2, y2])\n                    \n                else:\n                    train_images.append(n1)\n                    train_images.append(n2)\n                \n                    train_masks.append(o1)\n                    train_masks.append(o2)\n                   \n        del mask\n        del imageres\n        del image\n        #del newImage\n        del i1,i2,i3,i4\n        #del newMask\n    return train_images, train_masks, valid_images, valid_masks, valid_xyxys\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ndef get_train_valid_dataset():\n    \n    \"\"\"\n    This function separates the fragments into 2 training data fragments and 1 validation data fragment.\n    Each fragment is divided into smaller images of shape (224, 224) with a stride of (112, 112).\n    The function applies the same division process to the fragments' masks and returns the train_images,\n    train_masks, valid_images and valid_masks. Additionally, it returns valid_xyxys, which represents the \n    (x, y) coordinates of the top-left and bottom-right vertices of each image in the validation set.\n    \n    Returns:\n        train_images (list): List of training images divided from the fragments.\n        train_masks (list): List of training masks divided from the fragments masks.\n        valid_images (list): List of validation images divided from the fragment.\n        valid_masks (list): List of validation masks divided from the fragment mask.\n        valid_xyxys (list): List of (x, y) coordinates representing the top-left and bottom-right vertices\n                            of each image in the validation set.\n    \"\"\"\n    train_images = []\n    train_masks = []\n\n    valid_images = []\n    valid_masks = []\n    valid_xyxys = []\n\n    for fragment_id in range(1, 4):\n\n        image, mask = read_image_mask(fragment_id)\n        x1_list = list(range(0, image.shape[1]-CFG.tile_size+1, CFG.stride))\n        y1_list = list(range(0, image.shape[0]-CFG.tile_size+1, CFG.stride))\n\n        for y1 in y1_list:\n            for x1 in x1_list:\n                y2 = y1 + CFG.tile_size\n                x2 = x1 + CFG.tile_size\n        \n                if fragment_id == CFG.valid_id:\n                    valid_images.append(image[y1:y2, x1:x2])\n                    valid_masks.append(mask[y1:y2, x1:x2, None])\n\n                    valid_xyxys.append([x1, y1, x2, y2])\n                else:\n                    train_images.append(image[y1:y2, x1:x2])\n                    train_masks.append(mask[y1:y2, x1:x2, None])\n        del image\n        del mask\n    return train_images, train_masks, valid_images, valid_masks, valid_xyxys\n    \n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''train_images, train_masks, valid_images, valid_masks, valid_xyxys1 = get_train_valid_dataset()\nvalid_xyxys = np.stack(valid_xyxys1)\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''print(f\"Train set: {len(train_images)} images | {len(train_masks)} masks\")\nprint(f\"Validation set: {len(valid_images)} images | {len(valid_masks)} masks\")\nprint(f\"\\nFirst 5 validation images coordinates:\\n{valid_xyxys[:5]}\")'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset","metadata":{}},{"cell_type":"code","source":"def get_transforms(data, cfg):\n    if data == 'train':\n        aug = A.Compose(cfg.train_aug_list)\n    elif data == 'valid':\n        aug = A.Compose(cfg.valid_aug_list)\n\n    return aug","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class InkDetectionDataset(Dataset):\n    def __init__(self, images, cfg, labels=None, transform=None, mode='train'):\n        self.images = images\n        self.cfg = cfg\n        self.labels = labels\n        self.transform = transform\n        self.mode = mode\n\n    def __len__(self):\n        return len(self.images)\n\n    def __getitem__(self, idx):\n        image = self.images[idx]\n        \n        if self.mode == 'test':\n            label = np.zeros_like(image)\n        else:\n             label = self.labels[idx]\n\n        if self.transform:\n            data = self.transform(image=image, mask=label)\n            image = data['image']\n            label = data['mask']\n        \n        if self.mode == 'test':\n            return image\n        else:\n            return image, label","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''train_dataset = InkDetectionDataset(\n    train_images, CFG, labels=train_masks, transform=get_transforms(data='train', cfg=CFG))\nvalid_dataset = InkDetectionDataset(\n    valid_images, CFG, labels=valid_masks, transform=get_transforms(data='valid', cfg=CFG))\n\ntrain_loader = DataLoader(train_dataset,\n                          batch_size=CFG.train_batch_size,\n                          shuffle=True,\n                          num_workers=CFG.num_workers, pin_memory=True, drop_last=True,\n                          )\nvalid_loader = DataLoader(valid_dataset,\n                          batch_size=CFG.valid_batch_size,\n                          shuffle=False,\n                          num_workers=CFG.num_workers, pin_memory=True, drop_last=False)'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''NUM_SAMPLES = 1000\n\n# Create a random subset of indices\nsampled_indices_train = np.random.choice(len(train_dataset), NUM_SAMPLES, replace=False)\nsampled_indices_valid = np.random.choice(len(valid_dataset), min(NUM_SAMPLES//10, len(valid_dataset)), replace=False)\n\n# Use the subset indices to create the SubsetRandomSampler\nsampler_train = SubsetRandomSampler(sampled_indices_train)\nsampler_valid = SubsetRandomSampler(sampled_indices_valid)\n\n# Create sub samples of the train and validation loaders\ntrain_loader_sample = DataLoader(dataset=train_dataset, batch_size=CFG.train_batch_size, sampler=sampler_train)\nvalid_loader_sample = DataLoader(dataset=valid_dataset, batch_size=CFG.train_batch_size, sampler=sampler_valid)\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"import ssl\nssl._create_default_https_context = ssl._create_unverified_context","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n\nmodel2 = smp.Unet(\n        encoder_name='vgg16', \n        encoder_weights='imagenet', \n        classes=1,\n        in_channels=CFG.in_chans,\n        activation=None)# sigmoid/softmax/logsoftmax/tanh/argmax/argmax2d/clamp/None\n   \npreprocessing_fn = smp.encoders.get_preprocessing_fn('vgg16', 'imagenet')\nloss = smp.utils.losses.BCEWithLogitsLoss()\nmetrics = [smp.utils.metrics.Fscore(beta=0.5),smp.utils.metrics.IoU()]\noptimizer = torch.optim.Adam([ \n    dict(params=model2.parameters(), lr=0.0001),\n])\n\n\nmodel2 = smp.Unet(\n        encoder_name='efficientnet-b6', \n        encoder_weights='imagenet', \n        classes=1,\n        in_channels=CFG.in_chans,\n        activation=None)# sigmoid/softmax/logsoftmax/tanh/argmax/argmax2d/clamp/None\n  \npreprocessing_fn = smp.encoders.get_preprocessing_fn('efficientnet-b6', 'imagenet')\nloss = smp.utils.losses.BCEWithLogitsLoss()\nmetrics = [smp.utils.metrics.Fscore(beta=0.5),smp.utils.metrics.IoU()]\noptimizer = torch.optim.Adam([ \n    dict(params=model2.parameters(), lr=0.0001),\n])\n\n\n\n\nmodel4 = smp.Unet(\n        encoder_name='efficientnet-b5', \n        encoder_weights='imagenet', \n        classes=1,\n        in_channels=CFG.in_chans,\n        activation=None)# sigmoid/softmax/logsoftmax/tanh/argmax/argmax2d/clamp/None\n   \npreprocessing_fn = smp.encoders.get_preprocessing_fn('efficientnet-b5', 'imagenet')\nloss = smp.utils.losses.BCEWithLogitsLoss()\nmetrics = [smp.utils.metrics.Fscore(beta=0.5),smp.utils.metrics.IoU()]\noptimizer = torch.optim.Adam([ \n    dict(params=model4.parameters(), lr=0.0001),\n])\n\n\n\nmodel2 = smp.Unet(\n        encoder_name='inceptionv4', \n        encoder_weights='imagenet', \n        classes=1,\n        in_channels=CFG.in_chans,\n        activation=None)# sigmoid/softmax/logsoftmax/tanh/argmax/argmax2d/clamp/None\n   \npreprocessing_fn = smp.encoders.get_preprocessing_fn('inceptionv4', 'imagenet')\nloss = smp.utils.losses.BCEWithLogitsLoss()\nmetrics = [smp.utils.metrics.Fscore(beta=0.5),smp.utils.metrics.IoU()]\noptimizer = torch.optim.Adam([ \n    dict(params=model2.parameters(), lr=0.0001),\n])\n'''\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train & Validate","metadata":{}},{"cell_type":"code","source":"'''TRAIN = True\n#models = [model2,model3,model4,model5]'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ntrain_epoch = smp.utils.train.TrainEpoch(\n    model2, \n    loss=loss, \n    metrics=metrics, \n    optimizer=optimizer,\n    device=device,\n    verbose=True,)\nval_epoch = smp.utils.train.ValidEpoch(\n    model2,\n    loss=loss,\n    metrics=metrics,\n    device=device,\n    verbose=True,)\n\n\nif TRAIN:\n    min_loss = 999\n    metrics_values = {\n        'loss': {'train': [], 'valid': []},\n        'fscore': {'train': [], 'valid': []},\n        'iou': {'train': [], 'valid': []}\n    }\n    #for j in range(len(train_epoch_arr)):\n    for i in range(0, 2):\n\n        print('\\nEpoch: {}'.format(i))\n        train_logs = train_epoch.run(train_loader)\n        valid_logs = val_epoch.run(valid_loader)\n        \n         # Store metric values for plotting\n        metrics_values['loss']['train'].append(train_logs['bce_with_logits_loss'])\n        metrics_values['loss']['valid'].append(valid_logs['bce_with_logits_loss'])\n        metrics_values['fscore']['train'].append(train_logs['fscore'])\n        metrics_values['fscore']['valid'].append(valid_logs['fscore'])\n        metrics_values['iou']['train'].append(train_logs['iou_score'])\n        metrics_values['iou']['valid'].append(valid_logs['iou_score'])\n        \n\n        if min_loss > valid_logs['bce_with_logits_loss']:\n            min_loss = valid_logs['bce_with_logits_loss']\n            #torch.save(model, f'./resnet50_best_model_updated{i}.pth')\n            torch.save(model2, f'./model2_inceptionv4_best_model_updated{i}.pth')\n            print('Model saved!')\n            \n    # Plot metrics after all epochs\n    plot_metrics(\n        {'loss': metrics_values['loss']['train'], 'fscore': metrics_values['fscore']['train'], 'iou': metrics_values['iou']['train']},\n        {'loss': metrics_values['loss']['valid'], 'fscore': metrics_values['fscore']['valid'], 'iou': metrics_values['iou']['valid']})\n  \n            '''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''if TRAIN:\n    min_loss = 999\n    metrics_values = {\n        'loss': {'train': [], 'valid': []},\n        'fscore': {'train': [], 'valid': []},\n        'iou': {'train': [], 'valid': []}\n    }\n    #for j in range(len(train_epoch_arr)):\n    for i in range(0, 2):\n\n        print('\\nEpoch: {}'.format(i))\n        train_logs = train_epoch.run(train_loader)\n        valid_logs = val_epoch.run(valid_loader)\n        \n         # Store metric values for plotting\n        metrics_values['loss']['train'].append(train_logs['bce_with_logits_loss'])\n        metrics_values['loss']['valid'].append(valid_logs['bce_with_logits_loss'])\n        metrics_values['fscore']['train'].append(train_logs['fscore'])\n        metrics_values['fscore']['valid'].append(valid_logs['fscore'])\n        metrics_values['iou']['train'].append(train_logs['iou_score'])\n        metrics_values['iou']['valid'].append(valid_logs['iou_score'])\n        \n\n        if min_loss > valid_logs['bce_with_logits_loss']:\n            min_loss = valid_logs['bce_with_logits_loss']\n            #torch.save(model, f'./resnet50_best_model_updated{i}.pth')\n            torch.save(model2, f'./model2_2epochPlusbest_model_updated{i}.pth')\n            print('Model saved!')\n            \n    # Plot metrics after all epochs\n    plot_metrics(\n        {'loss': metrics_values['loss']['train'], 'fscore': metrics_values['fscore']['train'], 'iou': metrics_values['iou']['train']},\n        {'loss': metrics_values['loss']['valid'], 'fscore': metrics_values['fscore']['valid'], 'iou': metrics_values['iou']['valid']})\n  \n            '''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''if os.path.exists('/kaggle/input/my-model-final/model2_2epochPlusbest_model_updated0.pth'):\n    model = torch.load('/kaggle/input/my-model-final/model2_2epochPlusbest_model_updated0.pth', map_location=device)\n    print(\"Model loaded successfully.\")\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''TRAIN = True'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ntrain_epoch = smp.utils.train.TrainEpoch(\n    model, \n    loss=loss, \n    metrics=metrics, \n    optimizer=optimizer,\n    device=device,\n    verbose=True,)\nval_epoch = smp.utils.train.ValidEpoch(\n    model,\n    loss=loss,\n    metrics=metrics,\n    device=device,\n    verbose=True,)\n    '''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n\nif TRAIN:\n    min_loss = 999\n    metrics_values = {\n        'loss': {'train': [], 'valid': []},\n        'fscore': {'train': [], 'valid': []},\n        'iou': {'train': [], 'valid': []}\n    }\n    #for j in range(len(train_epoch_arr)):\n    for i in range(0, 2):\n\n        print('\\nEpoch: {}'.format(i))\n        train_logs = train_epoch.run(train_loader)\n        valid_logs = val_epoch.run(valid_loader)\n        \n         # Store metric values for plotting\n        metrics_values['loss']['train'].append(train_logs['bce_with_logits_loss'])\n        metrics_values['loss']['valid'].append(valid_logs['bce_with_logits_loss'])\n        metrics_values['fscore']['train'].append(train_logs['fscore'])\n        metrics_values['fscore']['valid'].append(valid_logs['fscore'])\n        metrics_values['iou']['train'].append(train_logs['iou_score'])\n        metrics_values['iou']['valid'].append(valid_logs['iou_score'])\n        \n\n        if min_loss > valid_logs['bce_with_logits_loss']:\n            min_loss = valid_logs['bce_with_logits_loss']\n            #torch.save(model, f'./resnet50_best_model_updated{i}.pth')\n            torch.save(model, f'./{CFG.backbone}_best_model_updated{i}.pth')\n            print('Model saved!')\n            \n    # Plot metrics after all epochs\n    plot_metrics(\n        {'loss': metrics_values['loss']['train'], 'fscore': metrics_values['fscore']['train'], 'iou': metrics_values['iou']['train']},\n        {'loss': metrics_values['loss']['valid'], 'fscore': metrics_values['fscore']['valid'], 'iou': metrics_values['iou']['valid']})\n        \n        '''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# for long training functions","metadata":{}},{"cell_type":"code","source":"'''\n\nbackbonemodels_arr = []\nlen(backbonemodels_arr)\nbackbone_arr =['efficientnet-b4', 'resnet50',  'densenet161' , 'efficientnet-b0']\nfor i in backbone_arr:\n    model = smp.Unet(\n        encoder_name=i, \n        encoder_weights='imagenet', \n        classes=1,\n        in_channels=CFG.in_chans,\n        activation=None) # 'sigmoid,tanh'\n    preprocessing_fn = smp.encoders.get_preprocessing_fn(i, 'imagenet')\n    backbonemodels_arr.append(model)\n\nfor i in backbone_arr:\n    model = smp.Unet(\n        encoder_name=i, \n        encoder_weights='imagenet', \n        classes=1,\n        in_channels=CFG.in_chans,\n        activation='sigmoid') # 'sigmoid,tanh'\n    preprocessing_fn = smp.encoders.get_preprocessing_fn(i, 'imagenet')\n    backbonemodels_arr.append(model)\nfor i in backbone_arr:\n    model = smp.Unet(\n        encoder_name=i, \n        encoder_weights='imagenet', \n        classes=1,\n        in_channels=CFG.in_chans,\n        activation='tanh') # 'sigmoid,tanh'\n    preprocessing_fn = smp.encoders.get_preprocessing_fn(i, 'imagenet')\n    backbonemodels_arr.append(model)\nloss = smp.utils.losses.BCEWithLogitsLoss()\n\nmetrics = [\n    smp.utils.metrics.Fscore(beta=0.5),\n    smp.utils.metrics.IoU()\n]\n\noptimizerW = torch.optim.AdamW([\n    {'params': model.parameters(), 'lr': 0.001, 'betas': (0.9, 0.999), 'weight_decay': 0.0001}\n])\n\noptimizer = torch.optim.AdamW([ \n    dict(params=model.parameters(), lr=0.001,momentum=0.9, weight_decay=0.0001),\n])\n\n\n\ntrain_epoch_arr =[]\nval_epoch_arr =[]\nfor i in backbonemodels_arr:\n    train_epoch = smp.utils.train.TrainEpoch(\n    i, \n    loss=loss, \n    metrics=metrics, \n    optimizer=optimizer,\n    device=device,\n    verbose=True,)\n    train_epoch_arr.append(train_epoch)\n    val_epoch = smp.utils.train.ValidEpoch(\n    i,\n    loss=loss,\n    metrics=metrics,\n    device=device,\n    verbose=True,)\n    val_epoch_arr.append(val_epoch)\n    \n#names =['efficientnet-b4', 'resnet50',  'densenet161' , 'efficientnet-b0',\n              #'efficientnet-b4_sigmoid', 'resnet50_sigmoid',  'densenet161_sigmoid' , 'efficientnet-b0_sigmoid',\n              #'efficientnet-b4_tahn', 'resnet50_tahn',  'densenet161_tahn' , 'efficientnet-b0_tahn']\n#backbone_arr =['efficientnet-b4', 'resnet50',  'densenet161' , 'efficientnet-b0',\n      #  'efficientnet-b4', 'resnet50',  'densenet161' , 'efficientnet-b0',\n        #'efficientnet-b4', 'resnet50',  'densenet161' , 'efficientnet-b0']\nlen(train_epoch_arr)\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"if os.path.exists('/kaggle/input/othermodels/u-resnet34_tanhadamW_best_model_lr00001.pth'):\n    model = torch.load('/kaggle/input/othermodels/u-resnet34_tanhadamW_best_model_lr00001.pth', map_location=device)\n    print(\"Model loaded successfully.\")\n   \n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmode = 'test'\nTH = 0.4\n\nif mode == 'test':\n    fragment_ids = sorted(os.listdir(CFG.comp_dataset_path + mode))\nelse:\n    fragment_ids = CFG.valid_id\n   ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_test_dataset(fragment_id):\n    test_images = read_image_mask(fragment_id, mode=mode)\n    \n    x1_list = list(range(0, test_images.shape[1]-CFG.tile_size+1,CFG.stride//4))\n    y1_list = list(range(0, test_images.shape[0]-CFG.tile_size+1, CFG.stride//4))\n    \n    test_images_list = []\n    xyxys = []\n    for y1 in y1_list:\n        for x1 in x1_list:\n            y2 = y1 + CFG.tile_size\n            x2 = x1 + CFG.tile_size\n            \n            test_images_list.append(test_images[y1:y2, x1:x2])\n            xyxys.append((x1, y1, x2, y2))\n    xyxys = np.stack(xyxys)\n            \n    test_dataset = InkDetectionDataset(test_images_list, CFG, transform=get_transforms(data='valid', cfg=CFG), mode=mode)\n    \n    test_loader = DataLoader(test_dataset,\n                          batch_size=CFG.train_batch_size,\n                          shuffle=False,\n                          num_workers=CFG.num_workers, pin_memory=True, drop_last=False)\n    \n    return test_loader, xyxys","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = []\nfor fragment_id in fragment_ids:\n    \n    test_loader, xyxys = make_test_dataset(fragment_id)\n    \n    \n    binary_mask = cv2.imread(CFG.comp_dataset_path + f\"{mode}/{fragment_id}/mask.png\", 0)\n    \n    binary_mask = (binary_mask / 255).astype(int)\n    \n    ori_h = binary_mask.shape[0]\n    ori_w = binary_mask.shape[1]\n    # mask = mask / 255\n\n    pad0 = (CFG.tile_size - binary_mask.shape[0] % CFG.tile_size)\n    pad1 = (CFG.tile_size - binary_mask.shape[1] % CFG.tile_size)\n\n    binary_mask = np.pad(binary_mask, [(0, pad0), (0, pad1)], constant_values=0)\n    \n    mask_pred = np.zeros(binary_mask.shape)\n    mask_count = np.zeros(binary_mask.shape)\n\n    for step, (images) in tqdm(enumerate(test_loader), total=len(test_loader)):\n        images = images.to(device)\n        batch_size = images.size(0)\n\n        with torch.no_grad():\n            y_preds = model(images)\n\n        start_idx = step*CFG.train_batch_size\n        end_idx = start_idx + batch_size\n        for i, (x1, y1, x2, y2) in enumerate(xyxys[start_idx:end_idx]):\n            mask_pred[y1:y2, x1:x2] += y_preds[i].squeeze(0).cpu().detach().numpy()\n            mask_count[y1:y2, x1:x2] += np.ones((CFG.tile_size, CFG.tile_size))            \n    \n    plt.imshow(mask_count)\n    plt.show()\n    \n    print(f'mask_count_min: {mask_count.min()}')\n    mask_pred /= mask_count\n    \n    mask_pred = mask_pred[:ori_h, :ori_w]\n    binary_mask = binary_mask[:ori_h, :ori_w]\n    \n    mask_pred = (mask_pred >= TH).astype(int)\n    mask_pred *= binary_mask\n    \n    plt.imshow(mask_pred)\n    plt.show()\n    \n    inklabels_rle = rle(mask_pred)\n    \n    results.append((fragment_id, inklabels_rle))\n    \n\n    #del mask_pred, mask_count\n    del test_loader\n    \n    gc.collect()\n    torch.cuda.empty_cache()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame(results, columns=['Id', 'Predicted'])\nsub","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv(CFG.comp_dataset_path + 'sample_submission.csv')\nsample_sub = pd.merge(sample_sub[['Id']], sub, on='Id', how='left')\nsample_sub","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# סתם פונקציות לנסיונות\n","metadata":{}},{"cell_type":"code","source":"'''\nnew_Train_images = []\nfor i in range(len(train_images)):\n    is_zero_array = np.array_equal(train_images[i], np.zeros_like(train_images[i]))\n    if is_zero_array:\n        continue\n    new_Train_images.append(train_images[i])'''\n\n'''\ndef returnimageready(image,mask):\n    newi = image.copy()\n    maski = mask.copy()\n    newi[newi > 130] = 0\n    x = newi.shape[0]\n    y = newi.shape[1]\n    z = newi.shape[2]\n    j = np.zeros((x, y))\n    for n in range(z):\n        j = j + image[:,:,n]\n    maski[j == 0] = 0\n    maski[j > 130] = 0 \n    return newi / np.max(newi),maski\n\n\n#arr = [image[:,:,0],image[:,:,1],image[:,:,2],image[:,:,3],image[:,:,4],image[:,:,5],mask]\n#create_subplots(arr)\n#add_param = create_param_table()\n#add_param('image',image)\n#add_param('mask',mask)\n#add_param.print_table()\n\n\ndef greater_then(image,value): \n    has_ = np.any(image > value)\n    message = f\"The image contains values greater than {value}.\" if has_ else f\"The image does not contain values greater than {value}.\"\n    p(message)\n\n'''\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}