{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":30201,"databundleVersionId":2750748,"sourceType":"competition"},{"sourceId":3849,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":2750,"modelId":324}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# ! pip install squidpy","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:34.734200Z","iopub.execute_input":"2024-10-31T07:52:34.734482Z","iopub.status.idle":"2024-10-31T07:52:34.738925Z","shell.execute_reply.started":"2024-10-31T07:52:34.734450Z","shell.execute_reply":"2024-10-31T07:52:34.738100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom tqdm.notebook import tqdm\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport matplotlib.patches as patches\n# import squidpy as sq\n# import anndata as ad\n# from squidpy.im import ImageContainer","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-31T08:03:19.104001Z","iopub.execute_input":"2024-10-31T08:03:19.104939Z","iopub.status.idle":"2024-10-31T08:03:19.112341Z","shell.execute_reply.started":"2024-10-31T08:03:19.104895Z","shell.execute_reply":"2024-10-31T08:03:19.111385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/sartorius-cell-instance-segmentation/train.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:37.299142Z","iopub.execute_input":"2024-10-31T07:52:37.299658Z","iopub.status.idle":"2024-10-31T07:52:38.058331Z","shell.execute_reply.started":"2024-10-31T07:52:37.299615Z","shell.execute_reply":"2024-10-31T07:52:38.056936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Appending files in train_semi_supervised directory can be appended to metadata file for further analysis.","metadata":{}},{"cell_type":"code","source":"def parse_filename(filename):\n    image_id = filename.split('.')[0]\n    cell_type = filename.split('[')[0]\n    filename_split = filename.split('_')\n    plate_time = filename_split[-3]\n    sample_date = filename_split[-4]\n    sample_id = '_'.join(filename_split[:3]) + '_' + '_'.join(filename_split[-2:]).split('.')[0]\n    \n    return image_id, cell_type, plate_time, sample_date, sample_id\n\n\ntrain_semi_supervised_images = os.listdir('../input/sartorius-cell-instance-segmentation/train_semi_supervised/')\n\nfor filename in tqdm(train_semi_supervised_images):\n    image_id, cell_type, plate_time, sample_date, sample_id = parse_filename(filename)\n    sample = {\n        'id': image_id,\n        'annotation': np.nan,\n        'width': 704,\n        'height': 520,\n        'cell_type': cell_type,\n        'plate_time': plate_time,\n        'sample_date': sample_date,\n        'sample_id': sample_id\n    }\n    df = pd.concat([df, pd.DataFrame([sample])], ignore_index=True)\n    \ndf['cell_type'] = df['cell_type'].str.rstrip('s')\nprint(f'Training Set Shape: {df.shape} - {df[\"id\"].nunique()} Images - Memory Usage: {df.memory_usage().sum() / 1024 ** 2:.2f} MB')","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:38.060644Z","iopub.execute_input":"2024-10-31T07:52:38.061406Z","iopub.status.idle":"2024-10-31T07:52:48.177148Z","shell.execute_reply.started":"2024-10-31T07:52:38.061357Z","shell.execute_reply":"2024-10-31T07:52:48.176241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For advanced data analysis and feature extraction, masks have to be decoded into 2 dimensional arrays. Since the training annotations are provided as run length encoded strings, they can be decoded with the function defined below.","metadata":{}},{"cell_type":"code","source":"def decode_rle_mask(rle_mask, shape):\n\n    \"\"\"\n    Decode run-length encoded segmentation mask string into 2d array\n\n    Parameters\n    ----------\n    rle_mask (str): Run-length encoded segmentation mask string\n    shape (tuple): Height and width of the mask\n\n    Returns\n    -------\n    mask [numpy.ndarray of shape (height, width)]: Decoded 2d segmentation mask\n    \"\"\"\n\n    rle_mask = rle_mask.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (rle_mask[0:][::2], rle_mask[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n\n    mask = np.zeros((shape[0] * shape[1]), dtype=np.uint8)\n    for start, end in zip(starts, ends):\n        mask[start:end] = 1\n\n    mask = mask.reshape(shape[0], shape[1])\n    return mask","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:48.179186Z","iopub.execute_input":"2024-10-31T07:52:48.179490Z","iopub.status.idle":"2024-10-31T07:52:48.186521Z","shell.execute_reply.started":"2024-10-31T07:52:48.179458Z","shell.execute_reply":"2024-10-31T07:52:48.185648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Additional metadata features like image mean, image standard deviation are extracted from both annotated and unannotated images. Mask area and annotation count are also extracted for only annotated files. Final dataframe with unannotated images and extracted metadata is saved as a csv file.","metadata":{}},{"cell_type":"code","source":"# for image_id in tqdm(df.loc[~df['annotation'].isnull(), 'id'].unique()):\n    \n#     image = cv2.imread(f'../input/sartorius-cell-instance-segmentation/train/{image_id}.png')\n#     image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n#     df.loc[df['id'] == image_id, 'image_mean'] = np.mean(image)\n#     df.loc[df['id'] == image_id, 'image_std'] = np.std(image)\n    \n#     for rle_mask in df.loc[df['id'] == image_id, 'annotation']:\n        \n#         mask = decode_rle_mask(rle_mask, (520, 704))\n#         df.loc[(df['id'] == image_id) & (df['annotation'] == rle_mask), 'mask_area'] = np.sum(mask)\n\n\n# for image_id in tqdm(df.loc[df['annotation'].isnull(), 'id'].unique()):\n    \n#     image = cv2.imread(f'../input/sartorius-cell-instance-segmentation/train_semi_supervised/{image_id}.png')\n#     image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    \n#     df.loc[df['id'] == image_id, 'image_mean'] = np.mean(image)\n#     df.loc[df['id'] == image_id, 'image_std'] = np.std(image)\n\n\n# annotation_counts = df.loc[~df['annotation'].isnull()].groupby('id')['annotation'].count()\n# df['annotation_count'] = df['id'].map(annotation_counts)\n# df.to_csv('train_processed.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:48.187610Z","iopub.execute_input":"2024-10-31T07:52:48.187898Z","iopub.status.idle":"2024-10-31T07:52:48.200648Z","shell.execute_reply.started":"2024-10-31T07:52:48.187862Z","shell.execute_reply":"2024-10-31T07:52:48.199857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 3 types of cell lines in images and each image contains only a single cell type. Those types are cort (neurons), shsy5y (neuroblastoma) and astro (astrocytes). Each cell lines is different from each other in terms of characteristics and statistics, so each type might require its own unique processing techniques.\n\nDistributions of cell types are different in annotated and unannotated training set. Annotated training set has higher number of cort but unannotated training set has higher number astro cell lines.","metadata":{}},{"cell_type":"code","source":"def visualize_cell_type_distributions(df, title):\n    fig, ax = plt.subplots(figsize=(24, 5), dpi=100)\n\n    sns.barplot(\n        x=df['cell_type'].value_counts().index,\n        y=df['cell_type'].value_counts().values,\n        ax=ax\n    )\n\n    ax.set_xlabel('')\n    ax.set_ylabel('')\n    ax.set_xticklabels([f'{target} ({value_count:,})' for value_count, target in zip(df['cell_type'].value_counts().values, df['cell_type'].value_counts().index)])\n    ax.tick_params(axis='x', labelsize=15, pad=10)\n    ax.tick_params(axis='y', labelsize=15, pad=10)\n    ax.set_title(title, size=20, pad=15)\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:48.201694Z","iopub.execute_input":"2024-10-31T07:52:48.202055Z","iopub.status.idle":"2024-10-31T07:52:48.212332Z","shell.execute_reply.started":"2024-10-31T07:52:48.202013Z","shell.execute_reply":"2024-10-31T07:52:48.211480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_supervised_cell_types = df[~df['annotation'].isnull()].groupby('id')['cell_type'].first().reset_index()\n\nvisualize_cell_type_distributions(df=df_train_supervised_cell_types, title='Cell Type Distribution in Annotated Training Set')\n\ndf_train_unsupervised_cell_types = df[df['annotation'].isnull()].groupby('id')['cell_type'].first().reset_index()\n\nvisualize_cell_type_distributions(df=df_train_unsupervised_cell_types, title='Cell Type Distribution in Unannotated Training Set')\n\ndf_train_all_cell_types = df.groupby('id')['cell_type'].first().reset_index()\n\nvisualize_cell_type_distributions(df=df_train_all_cell_types, title='Cell Type Distribution in All Training Set')","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:48.213343Z","iopub.execute_input":"2024-10-31T07:52:48.213630Z","iopub.status.idle":"2024-10-31T07:52:49.124671Z","shell.execute_reply.started":"2024-10-31T07:52:48.213587Z","shell.execute_reply":"2024-10-31T07:52:49.123768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Image Visualization","metadata":{}},{"cell_type":"code","source":"# def visualize_image(df, image_id):\n\n#     \"\"\"\n#     Visualize image along with segmentation masks\n\n#     Parameters\n#     ----------\n#     df [pandas.DataFrame of shape (73585, 9)]: Training dataframe\n#     image_id (str): Image ID (filename)\n#     \"\"\"\n    \n#     image_path = df.loc[df['id'] == image_id, 'id'].values[0]\n#     cell_type = df.loc[df['id'] == image_id, 'cell_type'].values[0]\n#     annotation_count = df.loc[df['id'] == image_id, 'annotation_count'].values[0]\n#     plate_time = df.loc[df['id'] == image_id, 'plate_time'].values[0]\n#     sample_date = df.loc[df['id'] == image_id, 'sample_date'].values[0]\n#     sample_id = df.loc[df['id'] == image_id, 'sample_id'].values[0]\n\n#     image = cv2.imread(f'../input/sartorius-cell-instance-segmentation/train/{image_path}.png')\n#     image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    \n#     print(f'{image_id}\\n{\"-\" * len(image_id)}')\n#     print(f'Image Mean: {np.mean(image):.4f}  -  Median: {np.median(image):.4f}  -  Std: {np.std(image):.4f} - Min: {np.min(image):.4f} -  Max: {np.max(image):.4f}')\n\n#     fig, axes = plt.subplots(figsize=(20, 20), ncols=2)\n#     fig.tight_layout(pad=5.0)\n    \n#     axes[0].imshow(image, cmap='gray')\n#     masks = []\n#     for mask in df.loc[df['id'] == image_id, 'annotation'].values:\n#         decoded_mask = decode_rle_mask(rle_mask=mask, shape=image.shape)\n#         masks.append(decoded_mask)\n#     mask = np.stack(masks)\n#     mask = np.any(mask == 1, axis=0)\n#     axes[1].imshow(image, cmap='gray')\n#     axes[1].imshow(mask, alpha=0.4)\n\n#     for i in range(2):\n#         axes[i].set_xlabel('')\n#         axes[i].set_ylabel('')\n#         axes[i].tick_params(axis='x', labelsize=15, pad=10)\n#         axes[i].tick_params(axis='y', labelsize=15, pad=10)\n        \n#     axes[0].set_title(f'{image_path} - {cell_type} - {int(annotation_count)} Annotations\\n{plate_time} - {sample_date} - {sample_id}', fontsize=20, pad=15)\n#     axes[1].set_title('Segmentation Mask', fontsize=20, pad=15)\n#     plt.show()\n#     plt.close(fig)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.125958Z","iopub.execute_input":"2024-10-31T07:52:49.126272Z","iopub.status.idle":"2024-10-31T07:52:49.132123Z","shell.execute_reply.started":"2024-10-31T07:52:49.126239Z","shell.execute_reply":"2024-10-31T07:52:49.131097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for image_id in df.loc[(df['cell_type'] == 'cort') & (~df['annotation'].isnull()), 'id'].unique()[:10]:\n#     visualize_image(df=df, image_id=image_id)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.133185Z","iopub.execute_input":"2024-10-31T07:52:49.133468Z","iopub.status.idle":"2024-10-31T07:52:49.146502Z","shell.execute_reply.started":"2024-10-31T07:52:49.133437Z","shell.execute_reply":"2024-10-31T07:52:49.145447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# img = sq.datasets.visium_fluo_image_crop()","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.149604Z","iopub.execute_input":"2024-10-31T07:52:49.149860Z","iopub.status.idle":"2024-10-31T07:52:49.155923Z","shell.execute_reply.started":"2024-10-31T07:52:49.149831Z","shell.execute_reply":"2024-10-31T07:52:49.155141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display(img)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.156837Z","iopub.execute_input":"2024-10-31T07:52:49.157128Z","iopub.status.idle":"2024-10-31T07:52:49.166376Z","shell.execute_reply.started":"2024-10-31T07:52:49.157068Z","shell.execute_reply":"2024-10-31T07:52:49.165527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display(crop)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.167468Z","iopub.execute_input":"2024-10-31T07:52:49.168193Z","iopub.status.idle":"2024-10-31T07:52:49.176281Z","shell.execute_reply.started":"2024-10-31T07:52:49.168150Z","shell.execute_reply":"2024-10-31T07:52:49.175504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# crop = img.crop_corner(1000, 1000, size=1000)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.177391Z","iopub.execute_input":"2024-10-31T07:52:49.177759Z","iopub.status.idle":"2024-10-31T07:52:49.187119Z","shell.execute_reply.started":"2024-10-31T07:52:49.177719Z","shell.execute_reply":"2024-10-31T07:52:49.186269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# crop.show(\"image\", channelwise=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.188121Z","iopub.execute_input":"2024-10-31T07:52:49.188412Z","iopub.status.idle":"2024-10-31T07:52:49.196809Z","shell.execute_reply.started":"2024-10-31T07:52:49.188383Z","shell.execute_reply":"2024-10-31T07:52:49.196048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sq.im.segment(\n#     img=crop, layer=\"image\", channel=0, method=\"watershed\", thresh=None, geq=True\n# )","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.197972Z","iopub.execute_input":"2024-10-31T07:52:49.198752Z","iopub.status.idle":"2024-10-31T07:52:49.207394Z","shell.execute_reply.started":"2024-10-31T07:52:49.198713Z","shell.execute_reply":"2024-10-31T07:52:49.206689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(crop)\n# print(f\"Number of segments in crop: {len(np.unique(crop['segmented_watershed']))}\")\n\n# fig, axes = plt.subplots(1, 2)\n# crop.show(\"image\", channel=0, ax=axes[0])\n# _ = axes[0].set_title(\"DAPI\")\n# crop.show(\"segmented_watershed\", cmap=\"jet\", interpolation=\"none\", ax=axes[1])\n# _ = axes[1].set_title(\"segmentation\")","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.208339Z","iopub.execute_input":"2024-10-31T07:52:49.208604Z","iopub.status.idle":"2024-10-31T07:52:49.217099Z","shell.execute_reply.started":"2024-10-31T07:52:49.208574Z","shell.execute_reply":"2024-10-31T07:52:49.216274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_path = \"/kaggle/input/sartorius-cell-instance-segmentation/train/0030fd0e6378.png\"\ntrain_img = cv2.imread(train_img_path)\ntrain_img = cv2.cvtColor(train_img, cv2.COLOR_BGR2GRAY)\n\n# Create a single plot\nplt.figure(figsize=(10, 10))\nplt.imshow(train_img, cmap='gray')\nplt.axis('off')  # Turn off axis labels\nplt.title('Grayscale Image')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.218265Z","iopub.execute_input":"2024-10-31T07:52:49.218860Z","iopub.status.idle":"2024-10-31T07:52:49.659846Z","shell.execute_reply.started":"2024-10-31T07:52:49.218818Z","shell.execute_reply":"2024-10-31T07:52:49.658992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Ensure the image is a numpy array and normalize it to [0, 1] range\n# train_img = np.array(train_img).astype(np.float32) / 255.0\n\n# # Create an ImageContainer\n# img_container = ImageContainer(train_img, layer=\"image\", dims=(\"y\", \"x\"))\n\n# # Now you can use the image with sq.im.segment\n# segmented = sq.im.segment(\n#     img=img_container,\n#     layer=\"image\",\n#     channel=0,\n#     method=\"watershed\",\n#     thresh=None,\n#     geq=True\n# )\n\n# print(img_container)\n# print(f\"Number of segments in img_container: {len(np.unique(img_container['segmented_watershed']))}\")\n\n# fig, axes = plt.subplots(1, 2)\n# img_container.show(\"image\", channel=0, ax=axes[0])\n# _ = axes[0].set_title(\"DAPI\")\n# img_container.show(\"segmented_watershed\", cmap=\"jet\", interpolation=\"none\", ax=axes[1])\n# _ = axes[1].set_title(\"segmentation\")","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.660960Z","iopub.execute_input":"2024-10-31T07:52:49.661331Z","iopub.status.idle":"2024-10-31T07:52:49.666564Z","shell.execute_reply.started":"2024-10-31T07:52:49.661288Z","shell.execute_reply":"2024-10-31T07:52:49.665774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install git+https://github.com/facebookresearch/segment-anything.git","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:52:49.667735Z","iopub.execute_input":"2024-10-31T07:52:49.668024Z","iopub.status.idle":"2024-10-31T07:53:05.034056Z","shell.execute_reply.started":"2024-10-31T07:52:49.667993Z","shell.execute_reply":"2024-10-31T07:53:05.033131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_path = \"/kaggle/input/sartorius-cell-instance-segmentation/train/0030fd0e6378.png\"\nimage = cv2.imread(train_img_path)\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:53:05.035443Z","iopub.execute_input":"2024-10-31T07:53:05.035750Z","iopub.status.idle":"2024-10-31T07:53:05.052231Z","shell.execute_reply.started":"2024-10-31T07:53:05.035716Z","shell.execute_reply":"2024-10-31T07:53:05.051539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from segment_anything import SamAutomaticMaskGenerator, sam_model_registry, SamPredictor\n\nsam = sam_model_registry[\"vit_h\"](checkpoint=\"/kaggle/input/segment-anything/pytorch/vit-h/1/model.pth\")\nsam.to(device=\"cuda\")\nmask_generator = SamAutomaticMaskGenerator(sam)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:56:15.741893Z","iopub.execute_input":"2024-10-31T07:56:15.742512Z","iopub.status.idle":"2024-10-31T07:56:23.328670Z","shell.execute_reply.started":"2024-10-31T07:56:15.742462Z","shell.execute_reply":"2024-10-31T07:56:23.327867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks = mask_generator.generate(image)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:53:28.058343Z","iopub.execute_input":"2024-10-31T07:53:28.058789Z","iopub.status.idle":"2024-10-31T07:53:35.445522Z","shell.execute_reply.started":"2024-10-31T07:53:28.058755Z","shell.execute_reply":"2024-10-31T07:53:35.444505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_anns(anns, axes=None):\n    if len(anns) == 0:\n        return\n    if axes:\n        ax = axes\n    else:\n        ax = plt.gca()\n        ax.set_autoscale_on(False)\n    sorted_anns = sorted(anns, key=(lambda x: x['area']), reverse=True)\n    polygons = []\n    color = []\n    for ann in sorted_anns:\n        m = ann['segmentation']\n        img = np.ones((m.shape[0], m.shape[1], 3))\n        color_mask = np.random.random((1, 3)).tolist()[0]\n        for i in range(3):\n            img[:,:,i] = color_mask[i]\n        ax.imshow(np.dstack((img, m*0.5)))\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\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\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))  ","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:53:35.446763Z","iopub.execute_input":"2024-10-31T07:53:35.447170Z","iopub.status.idle":"2024-10-31T07:53:35.461764Z","shell.execute_reply.started":"2024-10-31T07:53:35.447125Z","shell.execute_reply":"2024-10-31T07:53:35.460879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, axes = plt.subplots(1,3, figsize=(16,16))\naxes[0].imshow(image)\nshow_anns(masks, axes[1])\naxes[2].imshow(image)\nshow_anns(masks, axes[2])","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:53:35.463014Z","iopub.execute_input":"2024-10-31T07:53:35.463395Z","iopub.status.idle":"2024-10-31T07:54:24.282332Z","shell.execute_reply.started":"2024-10-31T07:53:35.463354Z","shell.execute_reply":"2024-10-31T07:54:24.281387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictor = SamPredictor(sam)\n\nimage_path = '/kaggle/input/sartorius-cell-instance-segmentation/train/0030fd0e6378.png'\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\npredictor.set_image(image_array)\n\ninput_point = np.array([[120, 135]])\ninput_label = np.array([1])\n\nplt.imshow(image_array)\nshow_points(input_point, input_label, plt.gca())\nplt.axis('on')\nplt.show()  \n\n\n\nmasks, scores, logits = predictor.predict(\n    point_coords=input_point,\n    point_labels=input_label,\n    multimask_output=True,\n)\n\nfor i, (mask, score) in enumerate(zip(masks, scores)):\n#     plt.figure(figsize=(10,10))\n    plt.imshow(image_array)\n    show_mask(mask, plt.gca())\n    show_points(input_point, input_label, plt.gca())\n    plt.title(f\"Mask {i+1}, Score: {score:.3f}\", fontsize=18)\n    plt.show()  ","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:56:52.961072Z","iopub.execute_input":"2024-10-31T07:56:52.961968Z","iopub.status.idle":"2024-10-31T07:56:56.452698Z","shell.execute_reply.started":"2024-10-31T07:56:52.961930Z","shell.execute_reply":"2024-10-31T07:56:56.451770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path ='/kaggle/input/sartorius-cell-instance-segmentation/train/0030fd0e6378.png'\nimage = cv2.imread(image_path)\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n#image = cv2.resize(image0,dsize=None,fx=0.1,fy=0.1)\n\nmasks = mask_generator.generate(image)\n\nfig, axs = plt.subplots(1,3,figsize=(12,4))\naxs[0].imshow(image)\naxs[2].imshow(image) \nshow_anns(masks,axs[1])\nshow_anns(masks,axs[2])\naxs[0].axis('off')\naxs[1].axis('off')    \naxs[2].axis('off')    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-31T07:59:43.621809Z","iopub.execute_input":"2024-10-31T07:59:43.622217Z","iopub.status.idle":"2024-10-31T08:00:23.252942Z","shell.execute_reply.started":"2024-10-31T07:59:43.622177Z","shell.execute_reply":"2024-10-31T08:00:23.252013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bgw = np.ones(image.shape)*255\nprint(len(masks))\nprint(masks[0].keys())","metadata":{"execution":{"iopub.status.busy":"2024-10-31T08:00:23.254938Z","iopub.execute_input":"2024-10-31T08:00:23.255593Z","iopub.status.idle":"2024-10-31T08:00:23.262771Z","shell.execute_reply.started":"2024-10-31T08:00:23.255548Z","shell.execute_reply":"2024-10-31T08:00:23.261829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(image.shape)\nplt.figure(figsize=(6,6))\nplt.imshow(image)\nplt.axis('off') \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-31T08:00:23.263972Z","iopub.execute_input":"2024-10-31T08:00:23.264357Z","iopub.status.idle":"2024-10-31T08:00:23.423598Z","shell.execute_reply.started":"2024-10-31T08:00:23.264313Z","shell.execute_reply":"2024-10-31T08:00:23.422394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(masks)):\n    plt.figure(figsize=(3,3))\n    plt.imshow(bgw)\n    show_anns([masks[i]])\n    plt.title(f'mask{i}')\n    plt.axis('off')  \n    plt.show()     ","metadata":{"execution":{"iopub.status.busy":"2024-10-31T08:00:49.762959Z","iopub.execute_input":"2024-10-31T08:00:49.763363Z","iopub.status.idle":"2024-10-31T08:01:51.134550Z","shell.execute_reply.started":"2024-10-31T08:00:49.763325Z","shell.execute_reply":"2024-10-31T08:01:51.133465Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(masks)):\n    fig, ax = plt.subplots(1,2,figsize=(6,3))\n    ax[0].imshow(bgw)\n    mask_i = masks[i]\n    box_0 = mask_i['bbox']\n    xc, yc, w, h = box_0\n    x0 = xc\n    y0 = yc\n    x1 = xc+w\n    y1 = yc+h\n    print(f'mask{i}')\n    print(box_0)\n    print([x0, x1, y0, y1])\n    rect = patches.Rectangle( (xc,yc),w,h, linewidth=2, edgecolor='yellow', fill=False)\n    \n    boximage=image[y0:y1,x0:x1,:]\n    cv2.imwrite(str(i).zfill(3)+'.png', boximage)\n    \n    show_anns([mask_i],ax[0])\n    ax[0].add_patch(rect)\n    ax[1].imshow(boximage)\n    ax[0].set_title(f'mask{i}') \n    plt.show()     ","metadata":{"execution":{"iopub.status.busy":"2024-10-31T08:03:22.941921Z","iopub.execute_input":"2024-10-31T08:03:22.942748Z","iopub.status.idle":"2024-10-31T08:05:24.340569Z","shell.execute_reply.started":"2024-10-31T08:03:22.942706Z","shell.execute_reply":"2024-10-31T08:05:24.339643Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import skimage.io as io\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-10-31T08:38:46.878990Z","iopub.execute_input":"2024-10-31T08:38:46.879966Z","iopub.status.idle":"2024-10-31T08:38:46.884875Z","shell.execute_reply.started":"2024-10-31T08:38:46.879915Z","shell.execute_reply":"2024-10-31T08:38:46.883617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path ='/kaggle/input/sartorius-cell-instance-segmentation/train/0030fd0e6378.png'\nimage = io.imread(image_path)\nfig, ax = plt.subplots(figsize=(10, 8))\nax.imshow(image, cmap='gray')\nax.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-31T08:38:54.241481Z","iopub.execute_input":"2024-10-31T08:38:54.241984Z","iopub.status.idle":"2024-10-31T08:38:54.617929Z","shell.execute_reply.started":"2024-10-31T08:38:54.241935Z","shell.execute_reply":"2024-10-31T08:38:54.616865Z"},"trusted":true},"execution_count":null,"outputs":[]}]}