{"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":"!git clone https://github.com/nyukat/GMIC.git\n\n","metadata":{"execution":{"iopub.status.busy":"2022-12-17T09:56:11.739926Z","iopub.execute_input":"2022-12-17T09:56:11.740387Z","iopub.status.idle":"2022-12-17T09:56:51.912786Z","shell.execute_reply.started":"2022-12-17T09:56:11.740285Z","shell.execute_reply":"2022-12-17T09:56:51.910916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.chdir(\"GMIC\")\n!rm sample_data/images/0*\nimport matplotlib.pyplot as plt\nfrom matplotlib.colors import LinearSegmentedColormap\n%matplotlib inline\n\nimport numpy as np, pandas as pd\nimport sys, pickle, os, torch, cv2\n# replace this path with path to GMIC\n#sys.path.append(\"/path/to/GMIC\")\n\nfrom src.data_loading import loading\nfrom src.modeling import gmic as gmic","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-17T09:59:01.186096Z","iopub.execute_input":"2022-12-17T09:59:01.186554Z","iopub.status.idle":"2022-12-17T09:59:04.609241Z","shell.execute_reply.started":"2022-12-17T09:59:01.186515Z","shell.execute_reply":"2022-12-17T09:59:04.60808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_pickle(\"sample_data/exam_list_before_cropping.pkl\")","metadata":{"execution":{"iopub.status.busy":"2022-12-17T09:59:07.383661Z","iopub.execute_input":"2022-12-17T09:59:07.384818Z","iopub.status.idle":"2022-12-17T09:59:07.397507Z","shell.execute_reply.started":"2022-12-17T09:59:07.384772Z","shell.execute_reply":"2022-12-17T09:59:07.396382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/train.csv\") \ntr.query(\"cancer == 1 and site_id == 2\")[0:1]","metadata":{"execution":{"iopub.status.busy":"2022-12-17T09:59:20.745762Z","iopub.execute_input":"2022-12-17T09:59:20.746132Z","iopub.status.idle":"2022-12-17T09:59:20.903697Z","shell.execute_reply.started":"2022-12-17T09:59:20.746102Z","shell.execute_reply":"2022-12-17T09:59:20.902582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom, cv2, pandas as pd, numpy as np\n\ndef rtf(r):\n    return f\"/kaggle/input/rsna-breast-cancer-detection/train_images/{r['patient_id']}/{r['image_id']}.dcm\"\n\n\npcancer =  tr.query(\"patient_id == 10432\")\nfor i,r in pcancer.iterrows():\n    f = rtf(r)\n    dicom = pydicom.dcmread(f)\n    img = dicom.pixel_array\n    img = (img - img.min()) / (img.max() - img.min())\n\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        img = 1 - img\n\n    img = cv2.resize(img, (1920, 2944))\n    f = f\"/kaggle/working/GMIC/sample_data/images/0_{r['laterality']}-{r['view']}.png\"\n    print(f)\n    plt.imshow(img, cmap=\"Greys_r\")\n    plt.axis(\"off\")\n    plt.show()\n    cv2.imwrite(f, (img * 255).astype(np.uint8))\n","metadata":{"execution":{"iopub.status.busy":"2022-12-17T09:59:25.113496Z","iopub.execute_input":"2022-12-17T09:59:25.113929Z","iopub.status.idle":"2022-12-17T09:59:37.172399Z","shell.execute_reply.started":"2022-12-17T09:59:25.113893Z","shell.execute_reply":"2022-12-17T09:59:37.171158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!chmod +x ./run.sh\n!./run.sh","metadata":{"execution":{"iopub.status.busy":"2022-12-17T09:59:41.766461Z","iopub.execute_input":"2022-12-17T09:59:41.766923Z","iopub.status.idle":"2022-12-17T10:00:44.933464Z","shell.execute_reply.started":"2022-12-17T09:59:41.766879Z","shell.execute_reply":"2022-12-17T10:00:44.93181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(\"sample_output/data.pkl\", \"rb\") as f:\n    exam_list = pickle.load(f)\ndatum = exam_list[0]\nview = \"L-CC\"\nshort_file_path = datum[view][0]","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:00:47.639773Z","iopub.execute_input":"2022-12-17T10:00:47.640248Z","iopub.status.idle":"2022-12-17T10:00:47.64782Z","shell.execute_reply.started":"2022-12-17T10:00:47.640207Z","shell.execute_reply":"2022-12-17T10:00:47.646839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_image = loading.load_image(\n                    image_path=os.path.join(\"sample_output/cropped_images/\", short_file_path + \".png\"),\n                    view=view, horizontal_flip=datum[\"horizontal_flip\"])","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:00:51.248592Z","iopub.execute_input":"2022-12-17T10:00:51.249263Z","iopub.status.idle":"2022-12-17T10:00:51.317846Z","shell.execute_reply.started":"2022-12-17T10:00:51.249225Z","shell.execute_reply":"2022-12-17T10:00:51.316687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_image = loading.process_image(loaded_image, view, datum[\"best_center\"][view][0])\n","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:00:53.355684Z","iopub.execute_input":"2022-12-17T10:00:53.356075Z","iopub.status.idle":"2022-12-17T10:00:53.413453Z","shell.execute_reply.started":"2022-12-17T10:00:53.356041Z","shell.execute_reply":"2022-12-17T10:00:53.412511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(loaded_image.shape)\nplt.imshow(loaded_image, cmap=\"Greys_r\")\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:00:55.672128Z","iopub.execute_input":"2022-12-17T10:00:55.672495Z","iopub.status.idle":"2022-12-17T10:00:56.339619Z","shell.execute_reply.started":"2022-12-17T10:00:55.672465Z","shell.execute_reply":"2022-12-17T10:00:56.338467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parameters = {\n        \"device_type\":\"cpu\",\n        \"cam_size\": (46, 30),\n        \"K\": 6,\n        \"crop_shape\": (256, 256),\n        \"post_processing_dim\": 256,\n        \"num_classes\": 2,\n        \"percent_t\":0.02\n    }","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:00:59.084223Z","iopub.execute_input":"2022-12-17T10:00:59.084692Z","iopub.status.idle":"2022-12-17T10:00:59.089711Z","shell.execute_reply.started":"2022-12-17T10:00:59.084644Z","shell.execute_reply":"2022-12-17T10:00:59.088929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = gmic.GMIC(parameters)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:01:02.04407Z","iopub.execute_input":"2022-12-17T10:01:02.044489Z","iopub.status.idle":"2022-12-17T10:01:02.261618Z","shell.execute_reply.started":"2022-12-17T10:01:02.044451Z","shell.execute_reply":"2022-12-17T10:01:02.260692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_state_dict(torch.load(\"models/sample_model_1.p\", map_location=\"cpu\"), strict=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:01:04.887432Z","iopub.execute_input":"2022-12-17T10:01:04.887831Z","iopub.status.idle":"2022-12-17T10:01:04.973951Z","shell.execute_reply.started":"2022-12-17T10:01:04.887798Z","shell.execute_reply":"2022-12-17T10:01:04.973006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor_batch = torch.Tensor(np.expand_dims(np.expand_dims(loaded_image, 0), 0).copy())\noutput = model(tensor_batch)\npred_numpy = output.data.cpu().numpy()\nmalignant_pred = pred_numpy[0, 1]","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:01:07.477081Z","iopub.execute_input":"2022-12-17T10:01:07.477523Z","iopub.status.idle":"2022-12-17T10:01:09.907984Z","shell.execute_reply.started":"2022-12-17T10:01:07.477486Z","shell.execute_reply":"2022-12-17T10:01:09.907081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"malignant prediction = {}\".format(malignant_pred))","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:01:13.244577Z","iopub.execute_input":"2022-12-17T10:01:13.245034Z","iopub.status.idle":"2022-12-17T10:01:13.249785Z","shell.execute_reply.started":"2022-12-17T10:01:13.244995Z","shell.execute_reply":"2022-12-17T10:01:13.248883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"saliency_maps = model.saliency_map.data.cpu().numpy()\nmalignant_saliency_map = saliency_maps[0,1,:,:]\nalphas = np.abs(np.linspace(0, 0.95, 259))\nalpha_red = plt.cm.get_cmap('Reds')\nplt.figure()\nplt.imshow(loaded_image, cmap=\"Greys_r\")\nplt.imshow(cv2.resize(malignant_saliency_map, (1920, 2944)), alpha=0.3, cmap=alpha_red, clim=[0.0, 1.0])\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:01:18.554989Z","iopub.execute_input":"2022-12-17T10:01:18.555392Z","iopub.status.idle":"2022-12-17T10:01:19.987903Z","shell.execute_reply.started":"2022-12-17T10:01:18.555358Z","shell.execute_reply":"2022-12-17T10:01:19.986715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(\"sample_output/data.pkl\", \"rb\") as f:\n    exam_list = pickle.load(f)\ndatum = exam_list[0]\nview = \"R-CC\"\nshort_file_path = datum[view][0]\n\nloaded_image = loading.load_image(\n                    image_path=os.path.join(\"sample_output/cropped_images/\", short_file_path + \".png\"),\n                    view=view, horizontal_flip=datum[\"horizontal_flip\"])\n\nloaded_image = loading.process_image(loaded_image, view, datum[\"best_center\"][view][0])\nprint(loaded_image.shape)\nplt.imshow(loaded_image, cmap=\"Greys_r\")\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:02:23.01926Z","iopub.execute_input":"2022-12-17T10:02:23.019725Z","iopub.status.idle":"2022-12-17T10:02:23.705116Z","shell.execute_reply.started":"2022-12-17T10:02:23.019682Z","shell.execute_reply":"2022-12-17T10:02:23.70417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = gmic.GMIC(parameters)\nmodel.load_state_dict(torch.load(\"models/sample_model_1.p\", map_location=\"cpu\"), strict=False)\ntensor_batch = torch.Tensor(np.expand_dims(np.expand_dims(loaded_image, 0), 0).copy())\noutput = model(tensor_batch)\npred_numpy = output.data.cpu().numpy()\nmalignant_pred = pred_numpy[0, 1]\nprint(\"malignant prediction = {}\".format(malignant_pred))\nsaliency_maps = model.saliency_map.data.cpu().numpy()\nmalignant_saliency_map = saliency_maps[0,1,:,:]\nalphas = np.abs(np.linspace(0, 0.95, 259))\nalpha_red = plt.cm.get_cmap('Reds')\nplt.figure()\nplt.imshow(loaded_image, cmap=\"Greys_r\")\nplt.imshow(cv2.resize(malignant_saliency_map, (1920, 2944)), alpha=0.3, cmap=alpha_red, clim=[0.0, 1.0])\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T10:03:16.642822Z","iopub.execute_input":"2022-12-17T10:03:16.643247Z","iopub.status.idle":"2022-12-17T10:03:20.501656Z","shell.execute_reply.started":"2022-12-17T10:03:16.643215Z","shell.execute_reply":"2022-12-17T10:03:20.500644Z"},"trusted":true},"execution_count":null,"outputs":[]}]}