.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/05_engineering/plot_05_segmentation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_05_engineering_plot_05_segmentation.py: Tuning an image-segmentation pipeline with a ground truth ========================================================= Segmentation pipelines (smoothing, background removal, thresholding, morphology, size filtering) have many interacting parameters, and their quality is usually judged by looking at the outlines. The synthetic microscopy scene of :class:`~easyiec.problems.ImageSegmentation` comes with the true mask, so the Dice coefficient tells how good each judgment-driven tuning really is — the setting used in the Claude case study of the book. .. GENERATED FROM PYTHON SOURCE LINES 14-16 The scene and three random pipelines ------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 16-36 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import easyiec as ei from easyiec.problems import ImageSegmentation from easyiec.strategies import InteractiveCMAES, InteractiveGA, RandomSearch from easyiec.validation import curve_frame problem = ImageSegmentation() rng = np.random.default_rng(0) fig, axes = plt.subplots(1, 4, figsize=(11, 3)) axes[0].imshow(problem.img, cmap="gray") axes[0].set_title("synthetic cells (true outlines in the oracle)", fontsize=8) for ax in axes[1:]: x = problem.random(rng) problem.render(x, ax=ax) ax.set_title(f"Dice {problem.objectives(x)['dice']:.2f}", fontsize=8) for ax in axes: ax.axis("off") .. image-sg:: /auto_examples/05_engineering/images/sphx_glr_plot_05_segmentation_001.png :alt: synthetic cells (true outlines in the oracle), Dice 0.18, Dice 0.28, Dice 0.50 :srcset: /auto_examples/05_engineering/images/sphx_glr_plot_05_segmentation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 37-41 Three strategies, a careful simulated observer ---------------------------------------------- The observer prefers outlines that match the cells (its utility is a smooth function of the Dice coefficient and of the error in the number of cells). .. GENERATED FROM PYTHON SOURCE LINES 41-50 .. code-block:: Python results, labels = [], [] for name, factory in {"GA": lambda: InteractiveGA(pop_size=9), "CMA-ES": lambda: InteractiveCMAES(pop_size=9), "random": lambda: RandomSearch(pop_size=9)}.items(): for seed in range(5): user = ei.SimulatedUser(problem, target=None, noise=0.03, seed=seed) results.append(ei.run(problem, factory(), user, generations=12, seed=seed)) labels.append(name) ei.viz.plot_convergence(curve_frame(results, labels=labels), title="segmentation tuning, five seeds") .. image-sg:: /auto_examples/05_engineering/images/sphx_glr_plot_05_segmentation_002.png :alt: segmentation tuning, five seeds :srcset: /auto_examples/05_engineering/images/sphx_glr_plot_05_segmentation_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 51-53 The best segmentation of the GA ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 53-58 .. code-block:: Python best = max((r for r, lab in zip(results, labels) if lab == "GA"), key=lambda r: r.best_true) fig, ax = plt.subplots(figsize=(4, 4)) problem.render(best.best.genome, ax=ax) ax.set_title(f"Dice {problem.objectives(best.best.genome)['dice']:.2f}", fontsize=9) plt.show() .. image-sg:: /auto_examples/05_engineering/images/sphx_glr_plot_05_segmentation_003.png :alt: Dice 0.86 :srcset: /auto_examples/05_engineering/images/sphx_glr_plot_05_segmentation_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.024 seconds) .. _sphx_glr_download_auto_examples_05_engineering_plot_05_segmentation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/easyiec/main?urlpath=lab/tree/notebooks/auto_examples/05_engineering/plot_05_segmentation.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_segmentation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_segmentation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_segmentation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_