{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Introduction\n\nThis notebook downloads the [segmentation_models_python package](https://github.com/qubvel/segmentation_models.pytorch) **0.3.3** and the checkpoints of the pretrained ResNet family encoders.\n\nThis is necessary to make the `segmentation_models_python` package and the weights available in notebooks without Internet connection like the [SenNet + HOA - Hacking the Human Vasculature in 3D](https://www.kaggle.com/competitions/blood-vessel-segmentation) competition which requires \"Internet access disabled\" but \"freely & publicly available external data is allowed, including pre-trained models\".\n\nTested with python 3.10 environments.\n\n## How to install the segmentation_models_python package from your notebook without Internet connection\n\n* Add this notebook output to your notebook input:\n * Click \"Add Data\"\n * Search the`install-segmentation-models-pytorch-0-3-3-ckpt` notebook\n * Add it clicking on the \"+\"\n* Import the `segmentation_models_pytorch` package with these 3 lines:\n\n  `!mkdir -p /root/.cache/torch/hub/checkpoints`\n\n  `!cp /kaggle/input/install-segmentation-models-pytorch-0-3-3-ckpt/smp_ckpt/* /root/.cache/torch/hub/checkpoints/`\n\n  `!python -m pip install --no-index --find-links=/kaggle/input/install-segmentation-models-pytorch-0-3-3-ckpt/smp_whl segmentation-models-pytorch`\n\n  `import segmentation_models_pytorch as smp`","metadata":{}},{"cell_type":"markdown","source":"## Code to download the wheel files and install the package","metadata":{}},{"cell_type":"code","source":"!python --version\n!pip download segmentation_models_pytorch==0.3.3 -d /kaggle/working/smp_whl","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-23T23:36:12.009384Z","iopub.execute_input":"2024-01-23T23:36:12.010050Z","iopub.status.idle":"2024-01-23T23:37:36.047567Z","shell.execute_reply.started":"2024-01-23T23:36:12.010012Z","shell.execute_reply":"2024-01-23T23:37:36.044916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m pip install --no-index --find-links=/kaggle/working/smp_whl segmentation-models-pytorch","metadata":{"execution":{"iopub.status.busy":"2024-01-23T23:37:36.053090Z","iopub.execute_input":"2024-01-23T23:37:36.053564Z","iopub.status.idle":"2024-01-23T23:37:54.965276Z","shell.execute_reply.started":"2024-01-23T23:37:36.053532Z","shell.execute_reply":"2024-01-23T23:37:54.963295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Download the ResNet and SENet encoders checkpoints","metadata":{}},{"cell_type":"code","source":"import os\nfrom torch.hub import get_dir, download_url_to_file\nfrom segmentation_models_pytorch.encoders.resnet import new_settings as resnet_settings\nfrom pretrainedmodels.models.senet import pretrained_settings as senet_settings\ndest_dir = os.path.join(get_dir(), \"checkpoints\")\nos.makedirs(dest_dir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-23T23:52:19.757030Z","iopub.execute_input":"2024-01-23T23:52:19.757519Z","iopub.status.idle":"2024-01-23T23:52:19.764414Z","shell.execute_reply.started":"2024-01-23T23:52:19.757484Z","shell.execute_reply":"2024-01-23T23:52:19.762673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Download the ResNet and SeNet families encoders checkpoints (you can extend this logic to download other checkpoints)","metadata":{}},{"cell_type":"markdown","source":"### ResNet checkpoints on all dataset","metadata":{}},{"cell_type":"code","source":"for model_name, pretrained_dataset_names_and_urls in resnet_settings.items():\n    for pretrained_dataset_name in pretrained_dataset_names_and_urls:\n        ckpt_url = resnet_settings[model_name][pretrained_dataset_name]\n        print(f\"Model {model_name}[{pretrained_dataset_name}]: {ckpt_url}\")\n        filename = os.path.basename(ckpt_url)\n        dest_file = os.path.join(dest_dir, filename)\n        download_url_to_file(ckpt_url, dest_file, progress=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-24T00:03:50.997334Z","iopub.execute_input":"2024-01-24T00:03:50.997918Z","iopub.status.idle":"2024-01-24T00:05:01.491409Z","shell.execute_reply.started":"2024-01-24T00:03:50.997883Z","shell.execute_reply":"2024-01-24T00:05:01.489282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### SENet checkpoints on all the datasets (ImageNet is the only available)","metadata":{}},{"cell_type":"code","source":"import ssl\nssl._create_default_https_context = ssl._create_unverified_context\nfor model_name, pretrained_datasets_metadata in senet_settings.items():\n    for pretrained_dataset_name in pretrained_datasets_metadata:\n        ckpt_url = pretrained_datasets_metadata[pretrained_dataset_name][\"url\"]\n        print(f\"Model {model_name}[{pretrained_dataset_name}]: {ckpt_url}\")\n        filename = os.path.basename(ckpt_url)\n        dest_file = os.path.join(dest_dir, filename)\n        download_url_to_file(ckpt_url, dest_file, progress=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-24T00:05:01.493679Z","iopub.execute_input":"2024-01-24T00:05:01.494453Z","iopub.status.idle":"2024-01-24T01:08:03.918776Z","shell.execute_reply.started":"2024-01-24T00:05:01.494410Z","shell.execute_reply":"2024-01-24T01:08:03.917122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /root/.cache/torch/hub/checkpoints/","metadata":{"execution":{"iopub.status.busy":"2024-01-24T01:09:29.952043Z","iopub.execute_input":"2024-01-24T01:09:29.953561Z","iopub.status.idle":"2024-01-24T01:09:30.307509Z","shell.execute_reply.started":"2024-01-24T01:09:29.953475Z","shell.execute_reply":"2024-01-24T01:09:30.305119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /kaggle/working/smp_ckpt\n!mv /root/.cache/torch/hub/checkpoints/*.pth /kaggle/working/smp_ckpt/","metadata":{"execution":{"iopub.status.busy":"2024-01-24T01:09:30.310071Z","iopub.execute_input":"2024-01-24T01:09:30.310640Z","iopub.status.idle":"2024-01-24T01:09:30.966314Z","shell.execute_reply.started":"2024-01-24T01:09:30.310557Z","shell.execute_reply":"2024-01-24T01:09:30.963044Z"},"trusted":true},"execution_count":null,"outputs":[]}]}