--- annotations_creators: - other language: - en language_creators: - found - expert-generated license: - mit multilinguality: - monolingual pretty_name: RSNA-ATD2023 size_categories: - 10K PIL Image # train[0]['mask'] -> PIL Image ``` ### Get Image & Segmentation Mask ```python ids = 3 image, mask = train[ids]['image'], \ # shape: (512, 512) train[ids]['mask'] # shape: (512, 512) ``` ### Convert mask into np.ndarray ```python mask = np.array(mask) ``` ### Visualize Image & Mask ```python fig = plt.figure(figsize=(16,16)) ax1 = fig.add_subplot(131) plt.axis('off') ax1.imshow(image, cmap='gray') ax2 = fig.add_subplot(132) plt.axis('off') ax2.imshow(mask, cmap='gray') ax3 = fig.add_subplot(133) ax3.imshow(image*np.where(mask>0,1,0), cmap='gray') plt.axis('off') plt.show() ``` ![raw cmap](https://huggingface.co/datasets/ziq/RSNA-ATD2023/resolve/main/assets/raw.png) ### Write Custom Plotting Function ```python from matplotlib.colors import ListedColormap, BoundaryNorm colors = ['#02020e', '#520e6d', '#c13a50', '#f57d15', '#fac62c', '#f4f88e'] # inferno bounds = range(0, len(colors) + 1) # Define the boundaries for each class in the colormap cmap, norm = ListedColormap(colors), BoundaryNorm(bounds, len(colors)) # Plot the segmentation mask with the custom colormap def plot_mask(mask, alpha=1.0): _, ax = plt.subplots() cax = ax.imshow(mask, cmap=cmap, norm=norm, alpha=alpha) cbar = plt.colorbar(cax, cmap=cmap, norm=norm, boundaries=bounds, ticks=bounds) cbar.set_ticks([]) _labels = [""] + labels for i in range(1, len(_labels)): cbar.ax.text(2, -0.5 + i, _labels[i], ha='left', color=colors[i - 1], fontsize=8) plt.axis('off') plt.show() ``` ### Custom Color ```python plot_mask(mask) ``` ![custom cmap](https://huggingface.co/datasets/ziq/RSNA-ATD2023/resolve/main/assets/mask.png) ### Plot only one class (e.g. liver) ```python liver, spleen, right_kidney, left_kidney, bowel = [(mask == i,1,0)[0] * i for i in range(1, len(labels))] plot_mask(liver) ``` ![liver](https://huggingface.co/datasets/ziq/RSNA-ATD2023/resolve/main/assets/liver.png)