{"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":" [code]","metadata":{"_uuid":"36f723cb-117a-429c-89d8-b5a27b79b26d","_cell_guid":"03cce71b-26a2-496d-ae35-9934f45f7b1c","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"7c5163b2-a3ec-4c9b-8213-cb0832d2efc5","_cell_guid":"0ce524b8-0e85-4c93-b262-3ddd24101c6f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-05-12T02:01:52.955141Z","iopub.execute_input":"2023-05-12T02:01:52.955699Z","iopub.status.idle":"2023-05-12T02:01:53.037447Z","shell.execute_reply.started":"2023-05-12T02:01:52.955661Z","shell.execute_reply":"2023-05-12T02:01:53.036398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import Libraries and set paths","metadata":{"_uuid":"4eb05f93-cbad-41c6-858a-6c622262f9b4","_cell_guid":"d48b3ccc-66ab-49cf-8927-e54f1fd84f8e","trusted":true}},{"cell_type":"code","source":"import torch\nimport glob\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom skimage.measure import label, regionprops\nfrom skimage.io import imsave\nfrom tqdm import tqdm\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom torch import nn, optim\nfrom torch.optim.lr_scheduler import OneCycleLR\nimport torch.nn.functional as F\nimport matplotlib.patches as patches\nimport gc\n\n# Constants\nPREFIX = '/kaggle/input/vesuvius-challenge-ink-detection/train/1/'\nBUFFER = 30\nZ_START = 27\nZ_DIM = 10\nTRAINING_STEPS = 30000\nLEARNING_RATE = 0.001\nBATCH_SIZE = 16\nDEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n# Define a Dataset class for the 3d x-ray scan\nclass XRayDataset(Dataset):\n    def __init__(self, file_paths, transform=None):\n        self.file_paths = file_paths\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.file_paths)\n\n    def __getitem__(self, idx):\n        image = np.array(Image.open(self.file_paths[idx]), dtype=np.float32)/65535.0\n        if self.transform:\n            image = self.transform(image)\n        return image\n\n# Create a DataLoader for the 3d x-ray scan\nfile_paths = sorted(glob.glob(PREFIX+\"surface_volume/*.tif\"))[Z_START:Z_START+Z_DIM]\ndataset = XRayDataset(file_paths, transform=torch.from_numpy)\ndataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True)\n\n# Display a few images\nimages = [dataset[i] for i in range(5)]\nfig, axes = plt.subplots(1, len(images), figsize=(15, 3))\nfor image, ax in zip(images, axes):\n    ax.imshow(image, cmap='gray')\n    ax.set_xticks([]); ax.set_yticks([])\nfig.tight_layout()\nplt.show()\n\n# Load the label data\nlabel = torch.from_numpy(np.array(Image.open(PREFIX+\"inklabels.png\"))).gt(0).float().to(DEVICE)\n\n# Now we'll create a dataset of subvolumes. We use a small rectangle around the letter \"P\" for our evaluation, and we'll exclude those pixels from the training set. (It's actually a Greek letter \"rho\", which looks similar to our \"P\".)\nrect = (1100, 3500, 700, 950)\nfig, ax = plt.subplots()\nax.imshow(label.cpu())\npatch = patches.Rectangle((rect[0], rect[1]), rect[2], rect[3], linewidth=2, edgecolor='r', facecolor='none')\nax.add_patch(patch)\nplt.show()","metadata":{"_uuid":"83431872-843a-42f3-bf86-1b9614d5713c","_cell_guid":"dbddb561-7249-46dd-b86c-f7c1b2b340f2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-05-12T02:01:53.357358Z","iopub.execute_input":"2023-05-12T02:01:53.358433Z","iopub.status.idle":"2023-05-12T02:02:14.178915Z","shell.execute_reply.started":"2023-05-12T02:01:53.358397Z","shell.execute_reply":"2023-05-12T02:02:14.177727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define SubvolumeDataset class","metadata":{"_uuid":"4ec2f6dc-2b91-45f6-bffd-bd85544aa9cb","_cell_guid":"ceed30e2-e2d5-44bd-a421-181d935ce9f9","trusted":true}},{"cell_type":"code","source":"# Define SubvolumeDataset class\nclass SubvolumeDataset(Dataset):\n    def __init__(self, image_stack, label, ink_pixels):\n        self.image_stack = image_stack\n        self.label = label\n        self.ink_pixels = ink_pixels\n\n    def __len__(self):\n        return len(self.ink_pixels)\n\n    def __getitem__(self, idx):\n        y, x = self.ink_pixels[idx]\n        z_start = max(0, Z_START - BUFFER)\n        z_end = min(self.image_stack.shape[0], Z_START + BUFFER)\n        subvolume = self.image_stack[z_start:z_end, y-BUFFER:y+BUFFER, x-BUFFER:x+BUFFER]\n        inklabel = self.label[y, x]\n        return subvolume, inklabel\n    \n    # Create a DataLoader for the subvolumes\nink_pixels = torch.stack((label.nonzero(as_tuple=True)[0][BUFFER:-BUFFER], label.nonzero(as_tuple=True)[1][BUFFER:-BUFFER]), dim=1)\nink_pixels = ink_pixels[~((ink_pixels[:,0] > rect[1]) & (ink_pixels[:,0] < rect[1]+rect[3]) & (ink_pixels[:,1] > rect[0]) & (ink_pixels[:,1] < rect[0]+rect[2]))]\nsubvolume_dataset = SubvolumeDataset(torch.stack([dataset[i] for i in range(len(dataset))]).to(DEVICE), label, ink_pixels)\nsubvolume_dataloader = DataLoader(subvolume_dataset, batch_size=BATCH_SIZE, shuffle=True)\n\ndef process_file(file_path):\n    with open(file_path, 'r') as file:\n        for line in file:\n            yield line.strip().split(',')\n\ndef find_common_elements(file1, file2):\n    file1_data = set(process_file(file1))\n    file2_data = set(process_file(file2))\n\n    common_elements = file1_data & file2_data\n\n    return common_elements","metadata":{"_uuid":"b100da4f-ebe1-4dfe-bec0-37d40ba6db67","_cell_guid":"3174538c-6668-4e74-b2af-738a4361de77","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-05-12T02:02:14.181054Z","iopub.execute_input":"2023-05-12T02:02:14.181420Z","iopub.status.idle":"2023-05-12T02:02:28.097625Z","shell.execute_reply.started":"2023-05-12T02:02:14.181391Z","shell.execute_reply":"2023-05-12T02:02:28.096414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define InkDetectionModel class","metadata":{"_uuid":"6a272509-c6b9-452a-b37f-608531441af7","_cell_guid":"b6e88877-a210-4ff5-8bf6-94c44b451941","trusted":true}},{"cell_type":"code","source":"class InkDetectionModel(nn.Module):\n    def __init__(self):\n        super(InkDetectionModel, self).__init__()\n        self.conv1 = nn.Conv3d(1, 64, kernel_size=3, stride=1, padding=1)\n        self.pool1 = nn.MaxPool3d(kernel_size=2, stride=2)\n        self.conv2 = nn.Conv3d(64, 128, kernel_size=3, stride=1, padding=1)\n        self.pool2 = nn.MaxPool3d(kernel_size=2, stride=2)\n        self.conv3 = nn.Conv3d(128, 256, kernel_size=3, stride=1, padding=1)\n        self.pool3 = nn.MaxPool3d(kernel_size=2, stride=2)\n        self.fc = nn.Linear(256 * 4 * 4 * 4, 1)  # This needs to be adjusted based on the output size of the conv layers and input image size\n\n    def forward(self, x):\n        x = self.pool1(F.relu(self.conv1(x)))\n        x = self.pool2(F.relu(self.conv2(x)))\n        x = self.pool3(F.relu(self.conv3(x)))\n        x = x.view(x.size(0), -1)  # Flatten the tensor\n        x = torch.sigmoid(self.fc(x))\n        return x","metadata":{"_uuid":"28a8c394-ff96-4626-be71-2f13a3ece5b3","_cell_guid":"0d50eab6-bb57-4139-a638-cef38ab1290e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-05-12T02:02:28.099038Z","iopub.execute_input":"2023-05-12T02:02:28.100073Z","iopub.status.idle":"2023-05-12T02:02:28.110433Z","shell.execute_reply.started":"2023-05-12T02:02:28.100037Z","shell.execute_reply":"2023-05-12T02:02:28.109512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{"_uuid":"3dc72f88-8c18-412d-8898-877f5c6714a0","_cell_guid":"192b5f8e-c6e9-4d8e-b18a-a405257e6468","trusted":true}},{"cell_type":"code","source":"mask = np.array(Image.open(PREFIX+\"mask.png\").convert('1'))\nlabel = torch.from_numpy(np.array(Image.open(PREFIX+\"inklabels.png\"))).gt(0).float().to(DEVICE)\nimages = [np.array(Image.open(filename), dtype=np.float32)/65535\n    for filename in sorted(glob.glob(PREFIX+\"surface_volume/*.tif\"))]\nimage_stack = torch.from_numpy(np.stack(images)).float().to(DEVICE)","metadata":{"_uuid":"fad0e360-fe5c-4912-a4d1-d38f5a12ebfc","_cell_guid":"740b46f5-f8f1-43e1-b784-5fd402e5d3fb","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-05-12T02:02:28.112675Z","iopub.execute_input":"2023-05-12T02:02:28.113458Z","iopub.status.idle":"2023-05-12T02:04:52.061024Z","shell.execute_reply.started":"2023-05-12T02:02:28.113424Z","shell.execute_reply":"2023-05-12T02:04:52.054404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compute ink pixels","metadata":{"_uuid":"5012432c-4b4a-497c-8209-bdad534838f2","_cell_guid":"a529794c-b5ff-41c1-9cf5-eba395d79008","trusted":true}},{"cell_type":"code","source":"# Compute ink pixels\nink_pixels = np.argwhere(mask)\nnp.random.shuffle(ink_pixels)\n\n# Prepare Dataset and Dataloader\ndataset = SubvolumeDataset(image_stack, label, ink_pixels)\ndataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True)","metadata":{"_uuid":"19dfc144-ddda-477a-aafb-0ae26a19135c","_cell_guid":"76019287-c901-4e64-8cdc-82f31aef5908","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-05-12T02:04:52.067240Z","iopub.execute_input":"2023-05-12T02:04:52.067929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Dataset and Dataloader","metadata":{"_uuid":"dd4d6188-3630-4d16-927b-9d1e4272e2b7","_cell_guid":"e6ac0f10-f9c4-4f9b-ad3e-8921d5dab0ad","trusted":true}},{"cell_type":"code","source":"dataset = SubvolumeDataset(image_stack, label, ink_pixels)\ndataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True)","metadata":{"_uuid":"b14dbefe-123f-4af5-aa4e-1b425a696e70","_cell_guid":"e8f81428-b79e-4233-b005-276a7899e944","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Initialize Model, Optimizer, and Scheduler","metadata":{"_uuid":"6b13ddf4-ea24-41a5-b953-1d0ccf38e433","_cell_guid":"3202a66e-825a-4782-8564-93cd59c58c9c","trusted":true}},{"cell_type":"code","source":"model = InkDetectionModel().to(DEVICE)\noptimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)\nscheduler = OneCycleLR(optimizer, max_lr=LEARNING_RATE, steps_per_epoch=len(dataloader), epochs=TRAINING_STEPS//len(dataloader))","metadata":{"_uuid":"614e6b6d-4ce0-4732-98e1-038fd4dd96b6","_cell_guid":"e9caa812-4fa6-48e1-9eb7-679fcfb733b4","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Loop","metadata":{"_uuid":"a656f2b3-3115-499f-bb65-7bb5851e6519","_cell_guid":"463a486f-90b5-4732-839e-b3a3bca08d6b","trusted":true}},{"cell_type":"code","source":"# Training Loop\nmodel.train()\nloss_values = []  # to store loss values\nbest_loss = np.inf  # initialize best loss to infinity\nearly_stopping_counter = 0  # counter for early stopping\nEARLY_STOPPING_STEPS = 10  # number of steps with no improvement after which training will be stopped\nfor step in tqdm(range(TRAINING_STEPS)):\n    subvolumes, inklabels = next(iter(dataloader))\n    subvolumes, inklabels = subvolumes.to(DEVICE), inklabels.to(DEVICE)\n\n    optimizer.zero_grad()\n    outputs = model(subvolumes)\n    loss = nn.BCELoss()(outputs, inklabels)\n    loss.backward()\n    optimizer.step()\n    scheduler.step()\n    \n    torch.cuda.empty_cache()\n\n    loss_values.append(loss.item())\n\n    # print loss every 1000 steps\n    if step % 1000 == 0:\n        print(f\"Step {step}, Loss: {loss.item()}\")","metadata":{"_uuid":"6219ae29-c284-46c0-86aa-9afd09f1ec6e","_cell_guid":"723ccaef-efe9-4e2a-87a7-4d164f824983","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save Model","metadata":{"_uuid":"7e59ea5c-d689-4ec3-8bd5-d36bd34f99bd","_cell_guid":"75e08bd6-4d97-4a7c-a94a-88910e1ca599","trusted":true}},{"cell_type":"code","source":"if loss.item() < best_loss:\n        best_loss = loss.item()\n        torch.save(model.state_dict(), 'best_model.pth')\n        early_stopping_counter = 0  # reset counter\nelse:\n    early_stopping_counter += 1\n        \n   # early stopping\nif early_stopping_counter >= EARLY_STOPPING_STEPS:\n        print(\"Early stopping...\")\n        break\n\n    # free up memory\n    del subvolumes, inklabels, outputs\n    torch.cuda.empty_cache()\n    gc.collect()","metadata":{"_uuid":"f5a2d680-f564-457f-94eb-d122690a949b","_cell_guid":"8542380d-fcb7-47d4-b2c6-2738098dbc91","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Perform inference and generate a probability map","metadata":{"_uuid":"0a29d01b-6d97-4b96-ba41-603b3f216fd4","_cell_guid":"97e67be9-69b6-436b-8e17-89a6c2da3cef","trusted":true}},{"cell_type":"code","source":"# Evaluate Model\nmodel.eval()\nwith torch.no_grad():\n    correct = 0\n    total = 0\n    for subvolumes, inklabels in dataloader:\n        subvolumes, inklabels = subvolumes.to(DEVICE), inklabels.to(DEVICE)\n        outputs = model(subvolumes)\n        predicted = (outputs > 0.5).float()\n        total += inklabels.size(0)\n        correct += (predicted == inklabels).sum().item()\n\n    print(f'Accuracy of the network on the test subvolumes: {100 * correct / total}%')","metadata":{"_uuid":"092a58ff-dcb9-4076-85a5-8de614a53076","_cell_guid":"e43f5b6a-d6ca-467f-979d-f8a78a35eeda","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation of Model on the Rectangle Region","metadata":{"_uuid":"b97734cc-985b-4729-9993-667d48fb9294","_cell_guid":"f5450435-aa56-4684-9ef2-2c5b93cd6aae","trusted":true}},{"cell_type":"code","source":"eval_dataset = SubvolumeDataset(image_stack, label, pixels_inside_rect)\neval_loader = data.DataLoader(eval_dataset, batch_size=BATCH_SIZE, shuffle=False)","metadata":{"_uuid":"2023e471-6d52-49ce-9844-2597152dfd0d","_cell_guid":"66272702-bfd1-48b2-a078-c7cff3da6e9d","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Apply the model to the evaluation dataset","metadata":{"_uuid":"e7f967a0-8a05-4fbc-be7b-c09def844c78","_cell_guid":"b6dbf480-55ee-4dae-a21c-d938ad55d594","trusted":true}},{"cell_type":"code","source":"model.eval()\noutputs = []\nwith torch.no_grad():\n    for subvolumes, _ in tqdm(eval_loader):\n        subvolumes = subvolumes.to(DEVICE)\n        output = model(subvolumes)\n        outputs.append(output.cpu())","metadata":{"_uuid":"9642ac16-b6c3-470c-8590-81f1e26771f7","_cell_guid":"1c3ecda7-53d3-492f-8924-fba8e9997c62","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Concatenate the outputs into a single tensor","metadata":{"_uuid":"73c33565-f31b-4e0b-8388-94ef1242eb67","_cell_guid":"918f459a-e293-4b3b-a9ee-c9197b8f2106","trusted":true}},{"cell_type":"code","source":"outputs = torch.cat(outputs)","metadata":{"_uuid":"40655b5f-f077-4e77-aead-8eeeea599ac0","_cell_guid":"2181262f-4545-437c-ba62-209c43a5ff1c","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create an empty array to hold the predicted ink locations","metadata":{"_uuid":"3f106deb-87e1-4c45-9812-9d670086181d","_cell_guid":"3c3f74a6-c44f-403a-b8af-093c37309f7c","trusted":true}},{"cell_type":"code","source":"predicted_ink = np.zeros(inside_rect.shape, dtype=bool)","metadata":{"_uuid":"97b6726b-faac-4648-b49a-c298330f9ade","_cell_guid":"0e6b64e5-76bb-4196-bed8-b7efbc363f0b","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# For each pixel in the rectangle, if the model's output is greater than 0.5, mark it as ink","metadata":{"_uuid":"e2d1ef56-3cc5-4309-abc7-d071e1c94daa","_cell_guid":"627fd9ab-3556-45c2-8947-3121cd411e67","trusted":true}},{"cell_type":"code","source":"for pixel, output in zip(pixels_inside_rect, outputs):\n    y, x = pixel\n    predicted_ink[y, x] = output.item() > 0.5","metadata":{"_uuid":"6be9f206-d5a3-4c68-b05a-2a30888f7298","_cell_guid":"5c9fca3f-8f41-4619-bd01-da3589bed1b2","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Display the predicted ink locations","metadata":{"_uuid":"70b9e868-d3c5-4a14-bd94-5585b4cfead4","_cell_guid":"33be1bbf-8fd4-40f6-88ee-a52615127929","trusted":true}},{"cell_type":"code","source":"plt.imshow(predicted_ink, cmap='gray')\nplt.title('Predicted Ink Locations')\nplt.show()","metadata":{"_uuid":"0cf72aa0-8efe-4a45-ab6c-8400dc9bc16b","_cell_guid":"9e6b0add-45fa-4c22-8d6e-0f86bb1323c4","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Thresholding and Visualization","metadata":{"_uuid":"54c158d4-2982-43b8-bc16-ed3d8ae638ac","_cell_guid":"59aca8bd-2150-4b72-b939-894331cbe006","trusted":true}},{"cell_type":"code","source":"# Thresholding and Visualization\nTHRESHOLD = 0.4  # Threshold for classifying a subvolume as ink\n\n# Evaluation of Model on the Rectangle Region\neval_dataset = SubvolumeDataset(image_stack, label, ink_pixels)\neval_loader = DataLoader(eval_dataset, batch_size=BATCH_SIZE, shuffle=False)\n\n# Apply the model to the evaluation dataset\nmodel.eval()\noutputs = []\nwith torch.no_grad():\n    for subvolumes, _ in tqdm(eval_loader):\n        subvolumes = subvolumes.to(DEVICE)\n        output = model(subvolumes)\n        outputs.append(output.cpu())\n\n# Concatenate the outputs into a single tensor\noutputs = torch.cat(outputs)\n\n# Create an empty array to hold the predicted ink locations\npredicted_ink = np.zeros(mask.shape, dtype=bool)\n\n# For each pixel in the rectangle, if the model's output is greater than THRESHOLD, mark it as ink\nfor pixel, output in zip(ink_pixels, outputs):\n    z, y, x = pixel\n    predicted_ink[z, y, x] = output.item() > THRESHOLD\n\n# Display the predicted ink locations\nplt.imshow(predicted_ink[0], cmap='gray')  # Change the index based on which slice you want to visualize\nplt.title('Predicted Ink Locations')\nplt.show()","metadata":{"_uuid":"de3f6ae4-7df0-468b-a6d5-d229061cc33b","_cell_guid":"62e3ab03-c8f0-4365-80da-caea11a895b6","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Run-Length Encoding (RLE) for Submission","metadata":{"_uuid":"eede328a-981e-4fa4-8f61-8175e9513ee9","_cell_guid":"8ab982f4-6222-4ecb-9249-377a4ceeb1a3","trusted":true}},{"cell_type":"code","source":"# Run-Length Encoding (RLE) for Submission\ndef rle(output):\n    pixels = np.where(output.flatten() > THRESHOLD, 1, 0).astype(np.uint8)\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] = runs[1::2] - runs[:-1:2]\n    return ' '.join(str(x) for x in runs)\n\nrle_output = rle(predicted_ink)\nprint(\"Id,Predicted\\na,\" + rle_output + \"\\nb,\" + rle_output, file=open('submission.csv', 'w'))","metadata":{"_uuid":"ff7c0055-59ae-40ba-923f-86bc0430515a","_cell_guid":"6ee2bc16-d6c1-4ba9-a2d8-aad65d5520e7","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}