{"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 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":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-25T10:53:06.833124Z","iopub.execute_input":"2022-07-25T10:53:06.833515Z","iopub.status.idle":"2022-07-25T10:53:06.871243Z","shell.execute_reply.started":"2022-07-25T10:53:06.83348Z","shell.execute_reply":"2022-07-25T10:53:06.869484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom PIL import Image\nimport tensorflow as tf\n\nfrom tqdm.notebook import tqdm\nimport shap\nimport seaborn as sns \nsns.set()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:22.070975Z","iopub.execute_input":"2022-07-25T10:54:22.071993Z","iopub.status.idle":"2022-07-25T10:54:22.082214Z","shell.execute_reply.started":"2022-07-25T10:54:22.07195Z","shell.execute_reply":"2022-07-25T10:54:22.081147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/mayo-clinic-strip-ai/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:26.681503Z","iopub.execute_input":"2022-07-25T10:54:26.681941Z","iopub.status.idle":"2022-07-25T10:54:26.697435Z","shell.execute_reply.started":"2022-07-25T10:54:26.681906Z","shell.execute_reply":"2022-07-25T10:54:26.696142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:30.479615Z","iopub.execute_input":"2022-07-25T10:54:30.480175Z","iopub.status.idle":"2022-07-25T10:54:30.504997Z","shell.execute_reply.started":"2022-07-25T10:54:30.480127Z","shell.execute_reply":"2022-07-25T10:54:30.503621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/mayo-clinic-strip-ai/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:33.463655Z","iopub.execute_input":"2022-07-25T10:54:33.464188Z","iopub.status.idle":"2022-07-25T10:54:33.476967Z","shell.execute_reply.started":"2022-07-25T10:54:33.464144Z","shell.execute_reply":"2022-07-25T10:54:33.475928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:37.426465Z","iopub.execute_input":"2022-07-25T10:54:37.427437Z","iopub.status.idle":"2022-07-25T10:54:37.440692Z","shell.execute_reply.started":"2022-07-25T10:54:37.427382Z","shell.execute_reply":"2022-07-25T10:54:37.43985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other = pd.read_csv(\"../input/mayo-clinic-strip-ai/other.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:41.670815Z","iopub.execute_input":"2022-07-25T10:54:41.671232Z","iopub.status.idle":"2022-07-25T10:54:41.680094Z","shell.execute_reply.started":"2022-07-25T10:54:41.671197Z","shell.execute_reply":"2022-07-25T10:54:41.678982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:44.932222Z","iopub.execute_input":"2022-07-25T10:54:44.932666Z","iopub.status.idle":"2022-07-25T10:54:44.946808Z","shell.execute_reply.started":"2022-07-25T10:54:44.93262Z","shell.execute_reply":"2022-07-25T10:54:44.945856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:47.851023Z","iopub.execute_input":"2022-07-25T10:54:47.851564Z","iopub.status.idle":"2022-07-25T10:54:47.858501Z","shell.execute_reply.started":"2022-07-25T10:54:47.851517Z","shell.execute_reply":"2022-07-25T10:54:47.857641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:50.446751Z","iopub.execute_input":"2022-07-25T10:54:50.447913Z","iopub.status.idle":"2022-07-25T10:54:50.456132Z","shell.execute_reply.started":"2022-07-25T10:54:50.447848Z","shell.execute_reply":"2022-07-25T10:54:50.454472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:53.141451Z","iopub.execute_input":"2022-07-25T10:54:53.141876Z","iopub.status.idle":"2022-07-25T10:54:53.149636Z","shell.execute_reply.started":"2022-07-25T10:54:53.141841Z","shell.execute_reply":"2022-07-25T10:54:53.148442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:56.095462Z","iopub.execute_input":"2022-07-25T10:54:56.096101Z","iopub.status.idle":"2022-07-25T10:54:56.125202Z","shell.execute_reply.started":"2022-07-25T10:54:56.096058Z","shell.execute_reply":"2022-07-25T10:54:56.124187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:54:59.86288Z","iopub.execute_input":"2022-07-25T10:54:59.863278Z","iopub.status.idle":"2022-07-25T10:54:59.877047Z","shell.execute_reply.started":"2022-07-25T10:54:59.863247Z","shell.execute_reply":"2022-07-25T10:54:59.876003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#total image 1154","metadata":{}},{"cell_type":"code","source":"train.patient_id.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:55:03.185904Z","iopub.execute_input":"2022-07-25T10:55:03.186274Z","iopub.status.idle":"2022-07-25T10:55:03.195512Z","shell.execute_reply.started":"2022-07-25T10:55:03.186245Z","shell.execute_reply":"2022-07-25T10:55:03.194097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nsns.countplot(train.groupby(\"patient_id\").image_num.size(), palette=\"Greens_r\")\nplt.xlabel(\"Number of images per patient\")\nplt.title(\"Max image number per patient in train\");","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:25:54.258177Z","iopub.execute_input":"2022-07-25T12:25:54.258955Z","iopub.status.idle":"2022-07-25T12:25:54.467269Z","shell.execute_reply.started":"2022-07-25T12:25:54.258914Z","shell.execute_reply":"2022-07-25T12:25:54.466452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"patient_id\").center_id.nunique().max()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:25:58.593031Z","iopub.execute_input":"2022-07-25T12:25:58.593435Z","iopub.status.idle":"2022-07-25T12:25:58.603188Z","shell.execute_reply.started":"2022-07-25T12:25:58.593401Z","shell.execute_reply":"2022-07-25T12:25:58.602272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"center_id\").patient_id.size().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:01.958627Z","iopub.execute_input":"2022-07-25T12:26:01.959926Z","iopub.status.idle":"2022-07-25T12:26:01.970455Z","shell.execute_reply.started":"2022-07-25T12:26:01.959861Z","shell.execute_reply":"2022-07-25T12:26:01.969169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"let's do with target distribution\n","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"sns.countplot(train.label,palette=\"pink_r\")","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:06.719138Z","iopub.execute_input":"2022-07-25T12:26:06.719987Z","iopub.status.idle":"2022-07-25T12:26:06.900881Z","shell.execute_reply.started":"2022-07-25T12:26:06.719935Z","shell.execute_reply":"2022-07-25T12:26:06.899724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.label.value_counts() / train.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:10.455487Z","iopub.execute_input":"2022-07-25T12:26:10.456295Z","iopub.status.idle":"2022-07-25T12:26:10.465983Z","shell.execute_reply.started":"2022-07-25T12:26:10.456251Z","shell.execute_reply":"2022-07-25T12:26:10.464504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"patient_id\").label.nunique().max()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:13.501551Z","iopub.execute_input":"2022-07-25T12:26:13.502016Z","iopub.status.idle":"2022-07-25T12:26:13.512934Z","shell.execute_reply.started":"2022-07-25T12:26:13.50198Z","shell.execute_reply":"2022-07-25T12:26:13.51172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/mayo-clinic-strip-ai/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:16.466605Z","iopub.execute_input":"2022-07-25T12:26:16.467046Z","iopub.status.idle":"2022-07-25T12:26:16.480994Z","shell.execute_reply.started":"2022-07-25T12:26:16.467011Z","shell.execute_reply":"2022-07-25T12:26:16.480053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:19.686739Z","iopub.execute_input":"2022-07-25T12:26:19.68802Z","iopub.status.idle":"2022-07-25T12:26:19.700902Z","shell.execute_reply.started":"2022-07-25T12:26:19.687973Z","shell.execute_reply":"2022-07-25T12:26:19.699644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"image_path\"]= test[\"image_id\"].apply(lambda x:os.path.join(\"../input/mayo-clinic-strip-ai/test/\", x+\".tif\"))\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:24.973508Z","iopub.execute_input":"2022-07-25T12:26:24.974569Z","iopub.status.idle":"2022-07-25T12:26:24.989739Z","shell.execute_reply.started":"2022-07-25T12:26:24.974525Z","shell.execute_reply":"2022-07-25T12:26:24.988672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nImage.MAX_IMAGE_PIXELS = None","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:27.964185Z","iopub.execute_input":"2022-07-25T12:26:27.964809Z","iopub.status.idle":"2022-07-25T12:26:27.969899Z","shell.execute_reply.started":"2022-07-25T12:26:27.964752Z","shell.execute_reply":"2022-07-25T12:26:27.968861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example1=(\"../input/mayo-clinic-strip-ai/test/006388_0.tif\")\nimg=Image.open(example1)\nimg.size","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:30.995467Z","iopub.execute_input":"2022-07-25T12:26:30.996147Z","iopub.status.idle":"2022-07-25T12:26:31.437347Z","shell.execute_reply.started":"2022-07-25T12:26:30.996106Z","shell.execute_reply":"2022-07-25T12:26:31.436258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example2=(\"../input/mayo-clinic-strip-ai/test/01adc5_0.tif\")\nimg=Image.open(example2)\nimg.size","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:33.966807Z","iopub.execute_input":"2022-07-25T12:26:33.967389Z","iopub.status.idle":"2022-07-25T12:26:33.982669Z","shell.execute_reply.started":"2022-07-25T12:26:33.967347Z","shell.execute_reply":"2022-07-25T12:26:33.981313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x1 = img.size[0]\nx2 = img.size[1]\nsc = x2/x1","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:36.68799Z","iopub.execute_input":"2022-07-25T12:26:36.688361Z","iopub.status.idle":"2022-07-25T12:26:36.693252Z","shell.execute_reply.started":"2022-07-25T12:26:36.688331Z","shell.execute_reply":"2022-07-25T12:26:36.692047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"factor = 1/10\nresized_img = img.resize((int(x1*factor), int(sc*x1*factor)))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:26:39.79792Z","iopub.execute_input":"2022-07-25T12:26:39.798942Z","iopub.status.idle":"2022-07-25T12:27:21.276355Z","shell.execute_reply.started":"2022-07-25T12:26:39.798898Z","shell.execute_reply":"2022-07-25T12:27:21.275101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_img.size","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:58:54.779161Z","iopub.execute_input":"2022-07-25T10:58:54.779649Z","iopub.status.idle":"2022-07-25T10:58:54.7867Z","shell.execute_reply.started":"2022-07-25T10:58:54.779611Z","shell.execute_reply":"2022-07-25T10:58:54.785815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_img.rotate(90, expand=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T10:59:26.014485Z","iopub.execute_input":"2022-07-25T10:59:26.01492Z","iopub.status.idle":"2022-07-25T10:59:30.852345Z","shell.execute_reply.started":"2022-07-25T10:59:26.014886Z","shell.execute_reply":"2022-07-25T10:59:30.850903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom PIL import Image\n\nimport tensorflow as tf\n\nfrom tqdm.notebook import tqdm\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch import nn\n\nimport seaborn as sns\nsns.set()\n\nimport shap","metadata":{"execution":{"iopub.status.busy":"2022-07-25T11:11:19.971571Z","iopub.execute_input":"2022-07-25T11:11:19.972581Z","iopub.status.idle":"2022-07-25T11:11:19.983942Z","shell.execute_reply.started":"2022-07-25T11:11:19.972536Z","shell.execute_reply":"2022-07-25T11:11:19.98249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)\nwarnings.filterwarnings(\"ignore\", category=UserWarning)\nwarnings.filterwarnings(\"ignore\", category=FutureWarning)\n\nfrom IPython.display import HTML","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:18:00.348445Z","iopub.execute_input":"2022-07-25T12:18:00.349389Z","iopub.status.idle":"2022-07-25T12:18:00.356222Z","shell.execute_reply.started":"2022-07-25T12:18:00.349342Z","shell.execute_reply":"2022-07-25T12:18:00.354475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImgDataset(Dataset):\n    \n    def __init__(self, df):\n        self.df = df \n        self.train = 'label' in df.columns\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        paths = [\"../input/jpg-images-strip-ai/test/\", \"../input/jpg-images-strip-ai/train/\"]\n        try:\n            image = cv2.imread(paths[self.train] + self.df.iloc[index].image_id + \".jpg\")\n        except:\n            image = np.zeros((512,512,3), np.uint8)\n        label = 0\n        try:\n            if len(image.shape) == 5:\n                image = image.squeeze().transpose(1, 2, 0)\n            image = cv2.resize(image, (512, 512)).transpose(2, 0, 1)\n        except:\n            image = np.zeros((3, 512, 512))\n        if(self.train):\n            label = {\"CE\" : 0, \"LAA\": 1}[self.df.iloc[index].label]\n        patient_id = self.df.iloc[index].patient_id\n        return image, label, patient_id\n    \n    \ndef predict(model, dataloader):\n    model.cuda()\n    model.eval()\n    dataloader = dataloader\n    outputs = []\n    s = nn.Softmax(dim=1)\n    ids = []\n    for item in tqdm(dataloader, leave=False):\n        patient_id = item[2][0]\n        try:\n            images = item[0].cuda().float()\n            ids.append(patient_id)\n            output = model(images)\n            outputs.append(s(output.cpu()[:,:2])[0].detach().numpy())\n        except:\n            ids.append(patient_id)\n            outputs.append(s(torch.tensor([[1, 1]]).float())[0].detach().numpy())\n    return np.array(outputs), ids","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:18:03.937812Z","iopub.execute_input":"2022-07-25T12:18:03.938391Z","iopub.status.idle":"2022-07-25T12:18:03.953477Z","shell.execute_reply.started":"2022-07-25T12:18:03.938356Z","shell.execute_reply":"2022-07-25T12:18:03.952272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torch.jit.load('../input/strip-ai-models-to-explain/kabir_ivan_cnn_strip_ai_v1.pth')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:18:06.532954Z","iopub.execute_input":"2022-07-25T12:18:06.534124Z","iopub.status.idle":"2022-07-25T12:18:06.56639Z","shell.execute_reply.started":"2022-07-25T12:18:06.534082Z","shell.execute_reply":"2022-07-25T12:18:06.564836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 1\ntest_loader = DataLoader(\n    ImgDataset(test.iloc[0:4]), \n    batch_size=batch_size, \n    shuffle=False, \n    num_workers=1\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:18:10.597147Z","iopub.execute_input":"2022-07-25T12:18:10.597553Z","iopub.status.idle":"2022-07-25T12:18:10.603975Z","shell.execute_reply.started":"2022-07-25T12:18:10.597519Z","shell.execute_reply":"2022-07-25T12:18:10.602652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"anss, ids = predict(model, test_loader)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:18:13.810681Z","iopub.execute_input":"2022-07-25T12:18:13.81113Z","iopub.status.idle":"2022-07-25T12:18:13.8413Z","shell.execute_reply.started":"2022-07-25T12:18:13.811093Z","shell.execute_reply":"2022-07-25T12:18:13.839678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"anss","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:18:16.844595Z","iopub.execute_input":"2022-07-25T12:18:16.845707Z","iopub.status.idle":"2022-07-25T12:18:16.877833Z","shell.execute_reply.started":"2022-07-25T12:18:16.845646Z","shell.execute_reply":"2022-07-25T12:18:16.876599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_transform():\n    pass\n\ndef pil_transform():\n    pass","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lime import lime_image","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"explainer = lime_image.LimeImageExplainer()\n\nexplanation = explainer.explain_instance( ,\n                                     predict, # classification function\n                                     top_labels=2, \n                                     hide_color=0, \n                                     num_samples=10)\ntemp, mask = explanation.get_image_and_mask(explanation.top_labels[0],\n                                            positive_only=False,\n                                            num_features=5,\n                                            hide_rest=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:17:38.4693Z","iopub.execute_input":"2022-07-25T12:17:38.470292Z","iopub.status.idle":"2022-07-25T12:17:38.507237Z","shell.execute_reply.started":"2022-07-25T12:17:38.470245Z","shell.execute_reply":"2022-07-25T12:17:38.505538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}