{"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":"# 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 algebrame\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":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-18T04:31:37.387699Z","iopub.execute_input":"2023-03-18T04:31:37.388390Z","iopub.status.idle":"2023-03-18T04:31:37.411775Z","shell.execute_reply.started":"2023-03-18T04:31:37.388352Z","shell.execute_reply":"2023-03-18T04:31:37.410727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport numpy as np\nimport glob\nimport PIL.Image as Image\nimport torch.utils.data as data\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom tqdm import tqdm\nfrom ipywidgets import interact, fixed\n\nPREFIX = '/kaggle/input/vesuvius-challenge-ink-detection/train/1/'\nBUFFER = 30  # Buffer size in x and y direction\nZ_START = 27 # First slice in the z direction to use\nZ_DIM = 10   # Number of slices in the z direction\nTRAINING_STEPS = 30000\nLEARNING_RATE = 0.03\nBATCH_SIZE = 32\nDEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nplt.imshow(Image.open(PREFIX+\"ir.png\"), cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-03-18T04:31:37.414419Z","iopub.execute_input":"2023-03-18T04:31:37.415044Z","iopub.status.idle":"2023-03-18T04:31:40.388416Z","shell.execute_reply.started":"2023-03-18T04:31:37.415007Z","shell.execute_reply":"2023-03-18T04:31:40.387266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\n","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-03-18T04:31:40.390170Z","iopub.execute_input":"2023-03-18T04:31:40.390555Z","iopub.status.idle":"2023-03-18T04:31:40.395225Z","shell.execute_reply.started":"2023-03-18T04:31:40.390516Z","shell.execute_reply":"2023-03-18T04:31:40.393892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport gc\nfrom skimage import io\nfrom skimage.transform import resize\n\n# Set the directory paths\ntrain_dir = \"/kaggle/input/vesuvius-challenge-ink-detection/train\"\ninklabels_dir = os.path.join(train_dir, \"1\")\nsurface_volume_dir = os.path.join(train_dir, \"1\", \"surface_volume\")\n\n# Load inklabels RLE\ninklabels_rle_path = os.path.join(inklabels_dir, \"inklabels_rle.csv\")\ninklabels_rle = pd.read_csv(inklabels_rle_path)\n\n# Load the image files\nimage_files = os.listdir(surface_volume_dir)\nimage_files = [f for f in image_files if f.endswith(\".tif\")]\n\n# Resize the images and convert to grayscale\nimage_size = (256, 256)\nimages = np.zeros((len(image_files), *image_size))\nfor i, file in enumerate(image_files):\n    image = io.imread(os.path.join(surface_volume_dir, file))\n    image = resize(image, image_size, preserve_range=True).astype(np.uint8)\n    images[i] = image\n\n# Merge the inklabels RLE with the image file names\ninklabels_rle[\"Id\"] = inklabels_rle[\"Id\"].replace(\".png\", \".tif\", regex=True)\ninklabels_rle = inklabels_rle[inklabels_rle[\"Id\"].isin(image_files)]\ninklabels_rle = inklabels_rle.reset_index(drop=True)\n\n# Remove duplicate and NaN rows\ninklabels_rle = inklabels_rle.drop_duplicates(subset=[\"Id\"])\ninklabels_rle = inklabels_rle.dropna()\n\n# Merge the inklabels RLE with the images\ninklabels = np.zeros(len(images), dtype=np.uint8)\nfor i, row in inklabels_rle.iterrows():\n    file = row[\"Id\"]\n    label = row[\"Predicted\"]\n    index = image_files.index(file)\n    inklabels[index] = label\n\n# Print the shape of the images and inklabels arrays\nprint(\"Shape of images array:\", images.shape)\nprint(\"Shape of inklabels array:\", inklabels.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T04:45:37.668750Z","iopub.execute_input":"2023-03-18T04:45:37.669489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\nfrom torch.utils import data\n# Check if GPU is available and use it, otherwise use CPU\nDEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nclass _DenseLayer(nn.Sequential):\n    def __init__(self, num_input_features, growth_rate, bn_size, drop_rate):\n        super(_DenseLayer, self).__init__()\n        self.add_module(\"norm1\", nn.BatchNorm3d(num_input_features)),\n        self.add_module(\"relu1\", nn.ReLU(inplace=True)),\n        self.add_module(\"conv1\", nn.Conv3d(num_input_features, bn_size * growth_rate, kernel_size=1, stride=1, bias=False)),\n        self.add_module(\"norm2\", nn.BatchNorm3d(bn_size * growth_rate)),\n        self.add_module(\"relu2\", nn.ReLU(inplace=True)),\n        self.add_module(\"conv2\", nn.Conv3d(bn_size * growth_rate, growth_rate, kernel_size=3, stride=1, padding=1, bias=False)),\n        self.drop_rate = drop_rate\n\n    def forward(self, x):\n        new_features = super(_DenseLayer, self).forward(x)\n        if self.drop_rate > 0:\n            new_features = F.dropout(new_features, p=self.drop_rate, training=self.training)\n        return torch.cat([x, new_features], 1)\n\nclass _DenseBlock(nn.Sequential):\n    def __init__(self, num_layers, num_input_features, bn_size, growth_rate, drop_rate):\n        super(_DenseBlock, self).__init__()\n        for i in range(num_layers):\n            layer = _DenseLayer(num_input_features + i * growth_rate, growth_rate, bn_size, drop_rate)\n            self.add_module(\"denselayer%d\" % (i + 1), layer)\n\nclass DenseNet(nn.Module):\n    def __init__(self, growth_rate=32, block_config=(6, 12, 24, 16),\n                 num_init_features=64, bn_size=4, drop_rate=0, num_classes=1):\n\n        super(DenseNet, self).__init__()\n\n        # First convolution\n        self.features = nn.Sequential(\n            nn.Conv3d(1, num_init_features, kernel_size=3, stride=1, padding=1, bias=False),\n            nn.BatchNorm3d(num_init_features),\n            nn.ReLU(inplace=True),\n        )\n\n        # Each denseblock\n        num_features = num_init_features\n        for i, num_layers in enumerate(block_config):\n            block = _DenseBlock(num_layers=num_layers, num_input_features=num_features,\n                                bn_size=bn_size, growth_rate=growth_rate, drop_rate=drop_rate)\n            self.features.add_module(\"denseblock%d\" % (i + 1), block)\n            num_features = num_features + num_layers * growth_rate\n\n        # Final batch norm\n        self.features.add_module(\"norm_final\", nn.BatchNorm3d(num_features))\n        self.classifier = nn.Linear(num_features, num_classes)\n        self.sigmoid = nn.Sigmoid()\n\n    def forward(self, x):\n        features = self.features(x)\n        out = F.relu(features, inplace=True)\n        out = F.adaptive_avg_pool3d(out, (1, 1, 1)).view(features.size(0), -1)\n        out = self.classifier(out)\n        out = self.sigmoid(out)\n        return out\n\n# Instantiate the DenseNet model\nmodel = DenseNet().to(DEVICE)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T04:33:03.919513Z","iopub.status.idle":"2023-03-18T04:33:03.919880Z","shell.execute_reply.started":"2023-03-18T04:33:03.919697Z","shell.execute_reply":"2023-03-18T04:33:03.919714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}