{"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":"# Mayo Clinic - STRIP AI (Offline Dependencies)\nImage Classification of Stroke Blood Clot Origin\n\n![banner](https://storage.googleapis.com/kaggle-competitions/kaggle/37333/logos/header.png?t=2022-06-29-00-47-20)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"As you may have realised that competition wants us to **disable** the **internet** access in our kaggle kernels which is mentioned in the [code requirements](https://www.kaggle.com/competitions/mayo-clinic-strip-ai/overview/code-requirements) section  \n\n<img src=\"https://i.ibb.co/JsxMNgy/image.png\" alt=\"image\" border=\"0\">\n\n(I didn't knew this and ended up ranting [here](https://twitter.com/PathikGhugare/status/1551113497769308160?s=20&t=Knh4FpF8QvFYAuQ_G-MpQg) XD )\n\nSince we'll be having **no** access to the **internet**, we won't be able to install any new packages even thought kaggle kernels by default has lot of packages installed but specifically for this competition I needed some packages like `pyvips`, `timm` and `torchstain` which were not there.","metadata":{}},{"cell_type":"markdown","source":"# Pyvips \n\nWe already saw that we got GBs of digital images in our dataset and its hard to preprocess them using standard libraries like `cv2` thats why we will beed `pyvips`","metadata":{}},{"cell_type":"code","source":"!mkdir pkgs\n!ls","metadata":{"execution":{"iopub.status.busy":"2022-07-26T16:07:25.993282Z","iopub.execute_input":"2022-07-26T16:07:25.993867Z","iopub.status.idle":"2022-07-26T16:07:27.562896Z","shell.execute_reply.started":"2022-07-26T16:07:25.993761Z","shell.execute_reply":"2022-07-26T16:07:27.561478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!conda config --add pkgs_dirs ./pkgs # Set the location where conda package will be downloaded","metadata":{"execution":{"iopub.status.busy":"2022-07-26T16:07:27.565576Z","iopub.execute_input":"2022-07-26T16:07:27.566098Z","iopub.status.idle":"2022-07-26T16:07:28.835515Z","shell.execute_reply.started":"2022-07-26T16:07:27.566051Z","shell.execute_reply":"2022-07-26T16:07:28.834228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!conda install --download-only -y pyvips # Download pyvips and dependencies","metadata":{"execution":{"iopub.status.busy":"2022-07-26T16:07:28.836814Z","iopub.execute_input":"2022-07-26T16:07:28.837171Z","iopub.status.idle":"2022-07-26T16:10:02.279684Z","shell.execute_reply.started":"2022-07-26T16:07:28.837139Z","shell.execute_reply":"2022-07-26T16:10:02.27826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# timm\n\nFor pretrained PyTorch models\n\nsame as before\n\nbut we will need pretrained weights of the model, I'll need `resnet26d` so I am downloading the same here but you can do the same by ","metadata":{}},{"cell_type":"code","source":"!conda install --download-only -y timm","metadata":{"execution":{"iopub.status.busy":"2022-07-26T16:10:02.282523Z","iopub.execute_input":"2022-07-26T16:10:02.28291Z","iopub.status.idle":"2022-07-26T16:12:01.339265Z","shell.execute_reply.started":"2022-07-26T16:10:02.282877Z","shell.execute_reply":"2022-07-26T16:12:01.337801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"You can find weights in source code of respective model here at https://github.com/rwightman/pytorch-image-models/tree/master/timm/models","metadata":{}},{"cell_type":"code","source":"!wget https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/resnet26d-69e92c46.pth","metadata":{"execution":{"iopub.status.busy":"2022-07-26T16:12:32.833492Z","iopub.execute_input":"2022-07-26T16:12:32.833968Z","iopub.status.idle":"2022-07-26T16:12:39.999593Z","shell.execute_reply.started":"2022-07-26T16:12:32.833936Z","shell.execute_reply":"2022-07-26T16:12:39.997889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/efficientnet_b2_ra-bcdf34b7.pth","metadata":{"execution":{"iopub.status.busy":"2022-07-26T16:12:40.002923Z","iopub.execute_input":"2022-07-26T16:12:40.004348Z","iopub.status.idle":"2022-07-26T16:12:50.467825Z","shell.execute_reply.started":"2022-07-26T16:12:40.00428Z","shell.execute_reply":"2022-07-26T16:12:50.46636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Torchstain\n\nfor stain normalization\n\nSince its a pip package and I was not able to cache downloads just like conda packages so downloading its latest release from https://github.com/EIDOSLAB/torchstain/releases/tag/v1.0.0","metadata":{}},{"cell_type":"code","source":"!wget https://github.com/EIDOSLAB/torchstain/archive/refs/tags/v1.0.0.tar.gz","metadata":{"execution":{"iopub.status.busy":"2022-07-26T16:13:04.626904Z","iopub.execute_input":"2022-07-26T16:13:04.627408Z","iopub.status.idle":"2022-07-26T16:13:07.190907Z","shell.execute_reply.started":"2022-07-26T16:13:04.62736Z","shell.execute_reply":"2022-07-26T16:13:07.189612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Installing them in another notebook","metadata":{}},{"cell_type":"markdown","source":"* Open your notebook\n* Click on `+ Add data` on top right\n* Go to `Notebook Output files`\n* Search your notebook name e.g. in my case `Mayo Clinic offline dependencies`\n* Add that notebook in your kernel\nthen \n<img src=\"https://i.ibb.co/w45pDwk/image.png\" alt=\"image\" border=\"0\">\n\nThen run the following commands to install the packages","metadata":{}},{"cell_type":"code","source":"# !conda install ../input/mayo-clinic-offline-dependencies/pkgs/*.tar.bz2 # for pyvips and timm","metadata":{"execution":{"iopub.status.busy":"2022-07-26T16:12:01.449906Z","iopub.status.idle":"2022-07-26T16:12:01.450899Z","shell.execute_reply.started":"2022-07-26T16:12:01.450665Z","shell.execute_reply":"2022-07-26T16:12:01.450689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install v1.0.0.tar.gz # for torchstain","metadata":{"execution":{"iopub.status.busy":"2022-07-26T16:12:01.452178Z","iopub.status.idle":"2022-07-26T16:12:01.453135Z","shell.execute_reply.started":"2022-07-26T16:12:01.452795Z","shell.execute_reply":"2022-07-26T16:12:01.452829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load pretrained weights\n\n```python\nimport timm\nmodel = timm.create_model('resnet26d', pretrained=False)\nmodel.load_state_dict(torch.load(\"../input/mayo-clinic-offline-dependencies/resnet26d-69e92c46.pth\"))\n```","metadata":{}},{"cell_type":"markdown","source":"And you're good to go !","metadata":{}}]}