{"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\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\nimport numpy as np\nimport torch\nimport matplotlib.pyplot as plt\nimport cv2\nimport sys\nimport os\nimport shutil\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-09-29T03:56:06.599473Z","iopub.execute_input":"2023-09-29T03:56:06.599917Z","iopub.status.idle":"2023-09-29T03:56:09.889957Z","shell.execute_reply.started":"2023-09-29T03:56:06.599881Z","shell.execute_reply":"2023-09-29T03:56:09.888986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/OpenGVLab/SAM-Med2D.git SAM_Med2D","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:56:09.891714Z","iopub.execute_input":"2023-09-29T03:56:09.8923Z","iopub.status.idle":"2023-09-29T03:56:12.735307Z","shell.execute_reply.started":"2023-09-29T03:56:09.892271Z","shell.execute_reply":"2023-09-29T03:56:12.733922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install gdown\nimport gdown\nfile_id = '1ARiB5RkSsWmAB_8mqWnwDF8ZKTtFwsjl'\n\n# Replace 'output_file' with the desired output file name\noutput_file = 'sam-med2d_b.pth'\n\n# Use gdown to download the file\ngdown.download(f'https://drive.google.com/uc?id={file_id}', MODEL_DIR+\"/\"+output_file, quiet=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:59:27.964328Z","iopub.execute_input":"2023-09-29T03:59:27.96477Z","iopub.status.idle":"2023-09-29T04:00:05.752603Z","shell.execute_reply.started":"2023-09-29T03:59:27.96474Z","shell.execute_reply":"2023-09-29T04:00:05.751149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CODE_DIR=\"/kaggle/working/SAM_Med2D\"\nROOT_DIR = \"/kaggle/working/SAM_Med2D\"\nMODEL_DIR = f\"{CODE_DIR}/pretrain_model\"","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:57:31.925216Z","iopub.execute_input":"2023-09-29T03:57:31.925629Z","iopub.status.idle":"2023-09-29T03:57:31.932447Z","shell.execute_reply.started":"2023-09-29T03:57:31.925597Z","shell.execute_reply":"2023-09-29T03:57:31.931103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append(ROOT_DIR)","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:36:04.245613Z","iopub.execute_input":"2023-09-29T03:36:04.246009Z","iopub.status.idle":"2023-09-29T03:36:04.252805Z","shell.execute_reply.started":"2023-09-29T03:36:04.245981Z","shell.execute_reply":"2023-09-29T03:36:04.251151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(MODEL_DIR, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:58:59.284775Z","iopub.execute_input":"2023-09-29T03:58:59.285168Z","iopub.status.idle":"2023-09-29T03:58:59.29124Z","shell.execute_reply.started":"2023-09-29T03:58:59.285136Z","shell.execute_reply":"2023-09-29T03:58:59.289734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndef show_mask(mask, ax, random_color=False):\n    if random_color:\n        color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)\n    else:\n        color = np.array([30/255, 144/255, 255/255, 0.6])\n    h, w = mask.shape[-2:]\n    mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)\n    ax.imshow(mask_image)\n\ndef show_points(coords, labels, ax, marker_size=375):\n    pos_points = coords[labels==1]\n    neg_points = coords[labels==0]\n    ax.scatter(pos_points[:, 0], pos_points[:, 1], color='green', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)\n    ax.scatter(neg_points[:, 0], neg_points[:, 1], color='red', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)\n\ndef show_box(box, ax):\n    x0, y0 = box[0], box[1]\n    w, h = box[2] - box[0], box[3] - box[1]\n    ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0,0,0,0), lw=2))\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:36:05.700301Z","iopub.execute_input":"2023-09-29T03:36:05.700702Z","iopub.status.idle":"2023-09-29T03:36:05.711818Z","shell.execute_reply.started":"2023-09-29T03:36:05.700674Z","shell.execute_reply":"2023-09-29T03:36:05.710157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir(f'{CODE_DIR}')\nimage = cv2.imread('/kaggle/input/sam-med2d-model/SAM_Med2D/data_demo/images/amos_0004_75.png')\nimage.shape\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:36:37.615822Z","iopub.execute_input":"2023-09-29T03:36:37.616157Z","iopub.status.idle":"2023-09-29T03:36:37.648192Z","shell.execute_reply.started":"2023-09-29T03:36:37.616131Z","shell.execute_reply":"2023-09-29T03:36:37.647041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = cv2.imread(\"/kaggle/input/sam-med2d-model/SAM_Med2D/data_demo/masks/amos_0004_75_inferior_vena_cava_000.png\")\nplt.imshow(mask)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:36:56.340832Z","iopub.execute_input":"2023-09-29T03:36:56.341218Z","iopub.status.idle":"2023-09-29T03:36:56.591148Z","shell.execute_reply.started":"2023-09-29T03:36:56.341179Z","shell.execute_reply":"2023-09-29T03:36:56.590034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:36:58.200571Z","iopub.execute_input":"2023-09-29T03:36:58.200925Z","iopub.status.idle":"2023-09-29T03:36:58.484971Z","shell.execute_reply.started":"2023-09-29T03:36:58.200899Z","shell.execute_reply":"2023-09-29T03:36:58.483687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from segment_anything import sam_model_registry\nfrom segment_anything.predictor_sammed import SammedPredictor\nfrom argparse import Namespace\nargs = Namespace()\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nargs.image_size = 256\nargs.encoder_adapter = True\nargs.sam_checkpoint = \"/kaggle/input/sam-med2d/sam-med2d_b.pth\"","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:37:27.641027Z","iopub.execute_input":"2023-09-29T03:37:27.641496Z","iopub.status.idle":"2023-09-29T03:37:29.385736Z","shell.execute_reply.started":"2023-09-29T03:37:27.641461Z","shell.execute_reply":"2023-09-29T03:37:29.38459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = sam_model_registry[\"vit_b\"](args).to(device)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:38:58.735828Z","iopub.execute_input":"2023-09-29T03:38:58.736291Z","iopub.status.idle":"2023-09-29T03:38:58.744488Z","shell.execute_reply.started":"2023-09-29T03:38:58.73625Z","shell.execute_reply":"2023-09-29T03:38:58.74321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cd ./SAM-Med2d\n!python train.py -sam_checkpoint","metadata":{"execution":{"iopub.status.busy":"2023-09-29T03:44:13.616815Z","iopub.execute_input":"2023-09-29T03:44:13.617298Z","iopub.status.idle":"2023-09-29T03:44:19.913318Z","shell.execute_reply.started":"2023-09-29T03:44:13.61726Z","shell.execute_reply":"2023-09-29T03:44:19.911559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img0 = torch.Tensor(image)\nimg=img0.permute(2,0,1)[None,]\nimg.shape,img0.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-27T06:07:12.084964Z","iopub.execute_input":"2023-09-27T06:07:12.085383Z","iopub.status.idle":"2023-09-27T06:07:12.096477Z","shell.execute_reply.started":"2023-09-27T06:07:12.085354Z","shell.execute_reply":"2023-09-27T06:07:12.094872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"F.interpolate(img0.permute(2,0,1)[None,],(256,256))[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-09-27T06:09:22.095159Z","iopub.execute_input":"2023-09-27T06:09:22.095651Z","iopub.status.idle":"2023-09-27T06:09:22.106494Z","shell.execute_reply.started":"2023-09-27T06:09:22.095622Z","shell.execute_reply":"2023-09-27T06:09:22.104822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#img=F.interpolate(img,(256,256))\nplt.imshow(F.interpolate(img0.permute(2,0,1)[None,],(256,256),mode='bilinear',align_corners=False)[0].permute(1,2,0))\nplt.show()\n\nplt.imshow(image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T06:12:16.099837Z","iopub.execute_input":"2023-09-27T06:12:16.100333Z","iopub.status.idle":"2023-09-27T06:12:16.660024Z","shell.execute_reply.started":"2023-09-27T06:12:16.100299Z","shell.execute_reply":"2023-09-27T06:12:16.658658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder=model.image_encoder(img)","metadata":{"execution":{"iopub.status.busy":"2023-09-27T06:00:46.935149Z","iopub.execute_input":"2023-09-27T06:00:46.935605Z","iopub.status.idle":"2023-09-27T06:00:48.546375Z","shell.execute_reply.started":"2023-09-27T06:00:46.935575Z","shell.execute_reply":"2023-09-27T06:00:48.545161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"decoder = model.mask_decoder()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T06:00:57.860254Z","iopub.execute_input":"2023-09-27T06:00:57.860736Z","iopub.status.idle":"2023-09-27T06:00:57.869466Z","shell.execute_reply.started":"2023-09-27T06:00:57.860683Z","shell.execute_reply":"2023-09-27T06:00:57.867786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ori_h, ori_w, _ = image.shape\ninput_point = np.array([[182, 207]])\ninput_label = np.array([1])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-27T05:35:11.645392Z","iopub.execute_input":"2023-09-27T05:35:11.645913Z","iopub.status.idle":"2023-09-27T05:35:11.653487Z","shell.execute_reply.started":"2023-09-27T05:35:11.64588Z","shell.execute_reply":"2023-09-27T05:35:11.651793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread('/kaggle/input/sam-med2d/SAM_Med2D/data_demo/images/s0114_111.png')\npredictor.set_image(image)","metadata":{"execution":{"iopub.status.busy":"2023-09-27T11:27:45.238796Z","iopub.execute_input":"2023-09-27T11:27:45.239168Z","iopub.status.idle":"2023-09-27T11:27:49.896834Z","shell.execute_reply.started":"2023-09-27T11:27:45.239115Z","shell.execute_reply":"2023-09-27T11:27:49.895871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_size = 10\narray_size = 256\n# Generate the grid points\nx = np.linspace(0, array_size-1, grid_size)\ny = np.linspace(0, array_size-1, grid_size)\n\n# Generate a grid of coordinates\nxv, yv = np.meshgrid(x, y)\n\n# Convert the numpy arrays to lists\nxv_list = xv.tolist()\nyv_list = yv.tolist()\n\n# Combine the x and y coordinates into a list of list of lists\ninput_points = [[[int(x), int(y)] for x, y in zip(x_row, y_row)] for x_row, y_row in zip(xv_list, yv_list)]\ninput_boxes = torch.tensor(input_points).view(1, 1, grid_size*grid_size, 2)","metadata":{"execution":{"iopub.status.busy":"2023-09-27T11:16:31.807739Z","iopub.execute_input":"2023-09-27T11:16:31.808384Z","iopub.status.idle":"2023-09-27T11:16:31.81961Z","shell.execute_reply.started":"2023-09-27T11:16:31.808339Z","shell.execute_reply":"2023-09-27T11:16:31.818028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#input_boxes = torch.tensor([[72,110,136,143],[124,92,160,132]], device=predictor.device)\ninput_point = torch.tensor([[500, 375], [213, 32], [543, 43], [453, 45]])\ninput_label = torch.tensor([1])\n\n#transformed_boxes = predictor.apply_boxes_torch(input_boxes, image.shape[:2], (args.image_size, args.image_size))\n\nmasks, _, _ = predictor.predict_torch(\n    point_coords=input_point,\n    point_labels=None,\n    #boxes=transformed_boxes,\n    multimask_output=True,\n)\n#print(transformed_boxes.shape)\nprint(masks.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-27T11:32:07.399065Z","iopub.execute_input":"2023-09-27T11:32:07.399465Z","iopub.status.idle":"2023-09-27T11:32:07.525692Z","shell.execute_reply.started":"2023-09-27T11:32:07.399436Z","shell.execute_reply":"2023-09-27T11:32:07.524171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image)\nfor mask in masks:\n    show_mask(mask.cpu().numpy(), plt.gca(), random_color=True)\n#for box in input_boxes:\n    #show_box(box.cpu().numpy(), plt.gca())\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T11:29:58.058964Z","iopub.execute_input":"2023-09-27T11:29:58.059639Z","iopub.status.idle":"2023-09-27T11:29:58.47422Z","shell.execute_reply.started":"2023-09-27T11:29:58.059606Z","shell.execute_reply":"2023-09-27T11:29:58.473065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}