<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Data Science Guts</title>
<link>https://datascienceguts.com/#category=Python</link>
<atom:link href="https://datascienceguts.com/index-python.xml" rel="self" type="application/rss+xml"/>
<description>Machine learning, statistics and deep learning in Python and R.</description>
<generator>quarto-1.10.19</generator>
<lastBuildDate>Thu, 12 Nov 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Semantic segmentation in rplatypus and pyplatypus: what is in the box</title>
  <dc:creator>Michał Maj</dc:creator>
  <link>https://datascienceguts.com/posts/2026-11-12-segmentation-tour/</link>
  <description><![CDATA[ 






<p>The <a href="../2026-11-06-nuclei-mean-dice/">last post</a> took one published model and one dataset and asked where the model is weakest. This one steps back and opens the box: everything rplatypus and pyplatypus offer for semantic segmentation - a mask per class, scored by overlap - and how to choose among it all.</p>
<p>Training is what the package is for, and almost every field below is about training a model. To keep the post runnable on any laptop, its code only reads configurations and builds the networks, on the four nuclei images from the <a href="../2026-11-03-rplatypus/">first post</a>; the scores in it come from full training runs of the same configurations.</p>
<section id="one-file-holds-the-experiment" class="level2">
<h2 class="anchored" data-anchor-id="one-file-holds-the-experiment">One file holds the experiment</h2>
<p>Everything below can be passed to constructors in R or Python, but the clearest way to see it is the file both packages read. This one describes a whole training run:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>nuclei.yaml</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" data-filename="nuclei.yaml" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">task</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> semantic_segmentation</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb1-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">train_path</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> nuclei</span></span>
<span id="cb1-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">validation_path</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> nuclei</span></span>
<span id="cb1-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mode</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> nested_dirs</span></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colormap</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb1-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">]</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          # background</span></span>
<span id="cb1-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">]</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    # nucleus</span></span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">models</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb1-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> unet</span></span>
<span id="cb1-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">architecture</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> u_net</span></span>
<span id="cb1-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">input_shape</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">]</span></span>
<span id="cb1-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">blocks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb1-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filters</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span id="cb1-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loss</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb1-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> cce_dice</span></span>
<span id="cb1-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">metrics</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb1-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> dice</span></span>
<span id="cb1-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">include_background</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">false</span></span>
<span id="cb1-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">optimizer</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb1-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> adam</span></span>
<span id="cb1-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">learning_rate</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span></span>
<span id="cb1-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">callbacks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb1-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> early_stopping</span></span>
<span id="cb1-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">monitor</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> val_dice</span></span>
<span id="cb1-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">patience</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb1-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">augmentation</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb1-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> HorizontalFlip</span></span>
<span id="cb1-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">params</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">{</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">}</span></span>
<span id="cb1-32"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> RandomRotate90</span></span>
<span id="cb1-33"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">params</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">{</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">}</span></span>
<span id="cb1-34"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">epochs</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span></span>
<span id="cb1-35"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">batch_size</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span></span></code></pre></div></div>
</div>
<p>Read from the top:</p>
<ul>
<li><strong><code>task</code></strong> decides what the rest of the file may contain. Semantic segmentation is one of four; the others - classification, instance segmentation and object detection - get posts of their own.</li>
<li><strong><code>data</code></strong> says where the images are and how to read them:
<ul>
<li><code>train_path</code> and <code>validation_path</code> say where the training and validation data are;</li>
<li><code>mode</code> is how the files are arranged: <code>nested_dirs</code> is one directory per image with <code>images/</code> and <code>masks/</code> inside, <code>config_file</code> is a CSV listing the files, which is what <code>split_dataset()</code> writes;</li>
<li><strong><code>colormap</code></strong> says which colour in a mask means which class, one entry per class, background first.</li>
</ul></li>
<li><strong><code>models</code></strong> is a list, so one file can train several models on the same data and compare them. Each entry has:
<ul>
<li>a <code>name</code>, an <code>architecture</code> and an <code>input_shape</code> - these are required;</li>
<li><code>blocks</code>, the depth;</li>
<li><code>filters</code>, the width of the first block, doubling at each level down.</li>
</ul></li>
<li><strong><code>weights</code></strong> and <strong><code>fit</code></strong> are not in this file, because it trains from scratch:
<ul>
<li><code>weights</code> names a checkpoint - a published name from <code>available_weights()</code>, a Hugging Face reference or a local file - and training then starts from it rather than from random numbers;</li>
<li><code>fit: false</code> means no training at all: the weights are loaded and the model is ready for <code>predict()</code> and <code>evaluate()</code>, which is how the <a href="../2026-11-06-nuclei-mean-dice/">last post</a> scored the published nuclei model.</li>
</ul></li>
<li><strong><code>loss</code></strong>, <strong><code>metrics</code></strong>, <strong><code>optimizer</code></strong>, <strong><code>callbacks</code></strong> and <strong><code>augmentation</code></strong> each take a name and that component’s own arguments. Every name comes from a catalogue the package can print, which is most of the rest of this post.</li>
</ul>
<p>Reading it gives the same object as building it from arguments:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rplatypus)</span>
<span id="cb2-2"></span>
<span id="cb2-3">spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">platypus_spec</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei.yaml"</span>)</span>
<span id="cb2-4">spec</span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; platypus specification</span></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   from        : nuclei.yaml </span></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   data        : nuclei </span></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   classes     : 2 </span></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   models      : 1 </span></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     unet           u_net            256x256, cce_dice, 30 epochs</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pyplatypus <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pp</span>
<span id="cb3-2"></span>
<span id="cb3-3">spec <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pp.from_yaml(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei.yaml"</span>)</span>
<span id="cb3-4">[(m.name, m.architecture.value, m.input_shape) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> spec.models]</span>
<span id="cb3-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [('unet', 'u_net', (256, 256))]</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>The file is checked before a single image is read or the network is built, so a mistake costs a second rather than an epoch. Asking for something the architecture does not have is refused with the field that is wrong:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">platypus_spec</span>(</span>
<span id="cb4-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segmentation_data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colormap =</span> binary_colormap),</span>
<span id="cb4-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">models =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">u_net</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">input_shape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>),</span>
<span id="cb4-4">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deep_supervision =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb4-5">)</span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Error in `platypus_spec()`:</span></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ! The specification has a problem:</span></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   * models[[1]]: deep_supervision requires architecture 'u_net_plus_plus'; 'u_net' has no intermediate outputs to supervise</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">pp.from_dict({</span>
<span id="cb5-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"task"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"semantic_segmentation"</span>,</span>
<span id="cb5-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>: {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"train_path"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation_path"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>,</span>
<span id="cb5-4">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"colormap"</span>: [[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>]]},</span>
<span id="cb5-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"models"</span>: [{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"architecture"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u_net"</span>,</span>
<span id="cb5-6">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input_shape"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deep_supervision"</span>: <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>}],</span>
<span id="cb5-7">})</span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ConfigError: The configuration has 1 problem:</span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   - models[0]: deep_supervision requires architecture 'u_net_plus_plus';</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     'u_net' has no intermediate outputs to supervise</span></span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="four-architectures" class="level2">
<h2 class="anchored" data-anchor-id="four-architectures">Four architectures</h2>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-3-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-1" aria-controls="tabset-3-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-3-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-2" aria-controls="tabset-3-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-3-1" class="tab-pane active" aria-labelledby="tabset-3-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">available_models</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">task =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"semantic_segmentation"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name</span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "u_net"           "u_net_plus_plus" "res_u_net"       "linknet"</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-3-2" class="tab-pane" aria-labelledby="tabset-3-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">[m[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> pp.available_models(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"semantic_segmentation"</span>)]</span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ['u_net', 'u_net_plus_plus', 'res_u_net', 'linknet']</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>All four are the same U: an encoder that halves the picture and doubles the features <code>blocks</code> times, and a decoder that climbs back up, with skip connections handing each level’s detail across. They differ in what is inside a block and in how the skips arrive.</p>
<p>Blue blocks are the encoder, green the decoder; each arrow is a connection. The picture going in is a stained tissue image from the same Data Science Bowl collection, and the one coming out is its true mask.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-4-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-1" aria-controls="tabset-4-1" aria-selected="true" href="">U-Net</a></li><li class="nav-item"><a class="nav-link" id="tabset-4-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-2" aria-controls="tabset-4-2" aria-selected="false" href="">U-Net++</a></li><li class="nav-item"><a class="nav-link" id="tabset-4-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-3" aria-controls="tabset-4-3" aria-selected="false" href="">Res-U-Net</a></li><li class="nav-item"><a class="nav-link" id="tabset-4-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-4" aria-controls="tabset-4-4" aria-selected="false" href="">LinkNet</a></li></ul>
<div class="tab-content">
<div id="tabset-4-1" class="tab-pane active" aria-labelledby="tabset-4-1-tab">
<div class="cell" data-renderings="[&quot;light&quot;,&quot;dark&quot;]">
<div class="cell-output-display light-content">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datascienceguts.com/posts/2026-11-12-segmentation-tour/index_files/figure-html/u-net-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>U-Net: each skip connection goes straight into the decoder block at the same level.</figcaption>
</figure>
</div>
</div>
</div>
<p>The plain U. Each skip connection is <em>concatenated</em> onto the decoder’s features, so a decoder block sees both what came up from below and what the encoder saw at its own scale. It is the one to start with.</p>
</div>
<div id="tabset-4-2" class="tab-pane" aria-labelledby="tabset-4-2-tab">
<div class="cell" data-renderings="[&quot;light&quot;,&quot;dark&quot;]">
<div class="cell-output-display light-content">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datascienceguts.com/posts/2026-11-12-segmentation-tour/index_files/figure-html/u-net-plus-plus-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>U-Net++: the space between encoder and decoder is filled with nested blocks, each fed by every earlier block in its row and by the one diagonally below.</figcaption>
</figure>
</div>
</div>
</div>
<p>The space between encoder and decoder is filled with nested blocks: each takes every earlier block in its row and the one diagonally below, so the top row - which is the decoder - meets the encoder at every depth rather than one. It is the only one of the four that can take <code>deep_supervision</code>, a loss on each block of the top row, which is the refusal above.</p>
</div>
<div id="tabset-4-3" class="tab-pane" aria-labelledby="tabset-4-3-tab">
<div class="cell" data-renderings="[&quot;light&quot;,&quot;dark&quot;]">
<div class="cell-output-display light-content">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datascienceguts.com/posts/2026-11-12-segmentation-tour/index_files/figure-html/res-u-net-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Res-U-Net: the U-Net layout with residual blocks - two layers and a shortcut over them, added back at the end.</figcaption>
</figure>
</div>
</div>
</div>
<p>The same U as U-Net, with residual blocks: each block’s input skips over its two layers and is <em>added</em> back at the end, so the layers learn a correction rather than a whole new representation. The usual reason to try it is depth, when more <code>blocks</code> stop helping.</p>
</div>
<div id="tabset-4-4" class="tab-pane" aria-labelledby="tabset-4-4-tab">
<div class="cell" data-renderings="[&quot;light&quot;,&quot;dark&quot;]">
<div class="cell-output-display light-content">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datascienceguts.com/posts/2026-11-12-segmentation-tour/index_files/figure-html/linknet-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>LinkNet: residual encoder blocks, skip connections added rather than concatenated, and a lighter decoder.</figcaption>
</figure>
</div>
</div>
</div>
<p>Residual blocks in the encoder, and skip connections that are <em>added</em> to the decoder’s features instead of concatenated onto them. Fewer channels reach each decoder block, so the decoder is lighter. Its argument is memory and speed, not score.</p>
</div>
</div>
</div>
<p>How much each costs at the same depth and width is quick to see. The cheapest way to build all four is one epoch on four small images:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-5-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-5-1" aria-controls="tabset-5-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-5-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-5-2" aria-controls="tabset-5-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-5-1" class="tab-pane active" aria-labelledby="tabset-5-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">one_epoch <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(architecture, name) {</span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">architecture</span>(name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">input_shape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">blocks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filters =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>,</span>
<span id="cb8-3">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">epochs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">batch_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb8-4">}</span>
<span id="cb8-5">four <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">platypus_spec</span>(</span>
<span id="cb8-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segmentation_data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colormap =</span> binary_colormap),</span>
<span id="cb8-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">models =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">one_epoch</span>(u_net, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u_net"</span>),</span>
<span id="cb8-8">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">one_epoch</span>(u_net_plus_plus, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u_net_plus_plus"</span>),</span>
<span id="cb8-9">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">one_epoch</span>(res_u_net, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"res_u_net"</span>),</span>
<span id="cb8-10">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">one_epoch</span>(linknet, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"linknet"</span>))</span>
<span id="cb8-11">)</span>
<span id="cb8-12">built <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">platypus_fit</span>(four, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb8-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evaluate</span>(built)[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"model"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"parameters"</span>)]</span>
<span id="cb8-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;             model parameters</span></span>
<span id="cb8-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1           u_net    1942594</span></span>
<span id="cb8-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2 u_net_plus_plus    2263730</span></span>
<span id="cb8-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3       res_u_net    2029682</span></span>
<span id="cb8-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4         linknet    1746754</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-5-2" class="tab-pane" aria-labelledby="tabset-5-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> one_epoch(architecture):</span>
<span id="cb9-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: architecture, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"architecture"</span>: architecture,</span>
<span id="cb9-3">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input_shape"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blocks"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"filters"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>,</span>
<span id="cb9-4">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"epochs"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"batch_size"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>}</span>
<span id="cb9-5"></span>
<span id="cb9-6">engine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pp.build_engine(pp.from_dict({</span>
<span id="cb9-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"task"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"semantic_segmentation"</span>,</span>
<span id="cb9-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>: {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"train_path"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation_path"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>,</span>
<span id="cb9-9">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"colormap"</span>: [[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>]]},</span>
<span id="cb9-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"models"</span>: [one_epoch(a) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span></span>
<span id="cb9-11">               [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u_net"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u_net_plus_plus"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"res_u_net"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"linknet"</span>]],</span>
<span id="cb9-12">}))</span>
<span id="cb9-13">engine.fit()</span>
<span id="cb9-14">[(row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"model"</span>], row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"parameters"</span>]) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> engine.evaluate()]</span>
<span id="cb9-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [('u_net', 1942594), ('u_net_plus_plus', 2263730),</span></span>
<span id="cb9-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  ('res_u_net', 2029682), ('linknet', 1746754)]</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>LinkNet is 10% smaller than U-Net here, not half: at 16 filters and four blocks the shared skeleton dominates, and the decoder savings are a small part of it.</p>
<section id="which-one-wins" class="level3">
<h3 class="anchored" data-anchor-id="which-one-wins">Which one wins</h3>
<p>When the nuclei weights were published, all four were trained on the same split of the 2018 Data Science Bowl images, with the same specification, for 60 epochs:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>architecture</th>
<th style="text-align: right;">parameters</th>
<th style="text-align: right;">mean Dice</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>U-Net</td>
<td style="text-align: right;">1,942,594</td>
<td style="text-align: right;">0.9217</td>
</tr>
<tr class="even">
<td>U-Net++</td>
<td style="text-align: right;">2,263,730</td>
<td style="text-align: right;">0.9212</td>
</tr>
<tr class="odd">
<td>LinkNet</td>
<td style="text-align: right;">1,746,754</td>
<td style="text-align: right;">0.9203</td>
</tr>
<tr class="even">
<td>Res-U-Net</td>
<td style="text-align: right;">2,029,682</td>
<td style="text-align: right;">0.9187</td>
</tr>
</tbody>
</table>
<p>The four means are <strong>0.0030</strong> apart. Within any one of them, the images it was scored on are <strong>0.054</strong> apart. And training the same U-Net again with a different seed moved its mean by 0.0012 - 40% of the whole gap between architectures.</p>
<p>On this problem the architecture is not where the result comes from, and that is why only one set of weights was published rather than four. It will not be true of every dataset, but it is the reason to start with a plain U-Net and look for a gain somewhere else first - the data, the loss, the resolution - before swapping the network.</p>
</section>
</section>
<section id="losses-and-metrics" class="level2">
<h2 class="anchored" data-anchor-id="losses-and-metrics">Losses and metrics</h2>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-6-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-6-1" aria-controls="tabset-6-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-6-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-6-2" aria-controls="tabset-6-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-6-1" class="tab-pane active" aria-labelledby="tabset-6-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">available_losses</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">task =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"semantic_segmentation"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name</span>
<span id="cb10-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [1] "boundary"         "cce"              "cce_dice"        </span></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [4] "combo"            "dice"             "focal"           </span></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [7] "focal_tversky"    "generalized_dice" "iou"             </span></span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [10] "lovasz"           "tversky"</span></span>
<span id="cb10-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">available_metrics</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">task =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"semantic_segmentation"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name</span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "cldice"       "dice"         "iou"          "surface_dice"</span></span>
<span id="cb10-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [5] "tversky"</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-6-2" class="tab-pane" aria-labelledby="tabset-6-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">[l[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> pp.available_losses(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"semantic_segmentation"</span>)]</span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ['boundary', 'cce', 'cce_dice', 'combo', 'dice', 'focal', 'focal_tversky',</span></span>
<span id="cb11-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  'generalized_dice', 'iou', 'lovasz', 'tversky']</span></span>
<span id="cb11-4">[m[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> pp.available_metrics(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"semantic_segmentation"</span>)]</span>
<span id="cb11-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ['cldice', 'dice', 'iou', 'surface_dice', 'tversky']</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>The <code>task</code> matters here: classification, detection and instance segmentation each have their own architectures, losses and metrics, and the same functions list them. Each entry also comes with its arguments and a one-line description. The short version of the losses:</p>
<ul>
<li><strong><code>cce_dice</code></strong> - cross-entropy blended with Dice - is the one to start a run with.</li>
<li><strong><code>tversky</code></strong> and <strong><code>focal_tversky</code></strong> take an <code>alpha</code> that weighs a missed pixel against a false alarm. Above 0.5 they would rather over-segment than miss, which suits a small class you cannot afford to lose.</li>
<li><strong><code>generalized_dice</code></strong> weights each class by the inverse square of its area, so with several classes of very different size the small ones still count.</li>
<li><strong><code>boundary</code></strong> adds a term for how far a wrong pixel is from the true edge, and <strong><code>lovasz</code></strong> optimises the IoU you are going to report rather than a stand-in for it.</li>
</ul>
<p>The metrics are fewer and each answers a different question. <strong><code>dice</code></strong> is the one to report unless there is a reason not to - with <code>include_background = FALSE</code>, as the last post explained. <strong><code>cldice</code></strong> asks whether a thin structure stays connected, which a vessel post later in the series needs, and <strong><code>surface_dice</code></strong> asks how much of a boundary lies within a tolerance of the truth, which matters for volumes.</p>
</section>
<section id="training-around-the-model" class="level2">
<h2 class="anchored" data-anchor-id="training-around-the-model">Training, around the model</h2>
<p>The rest of the file is the training run itself, and each part has its catalogue too:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-7-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-7-1" aria-controls="tabset-7-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-7-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-7-2" aria-controls="tabset-7-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-7-1" class="tab-pane active" aria-labelledby="tabset-7-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">available_optimizers</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name</span>
<span id="cb12-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "adadelta" "adagrad"  "adam"     "adamax"   "adamw"    "nadam"   </span></span>
<span id="cb12-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [7] "rmsprop"  "sgd"</span></span>
<span id="cb12-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">available_callbacks</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name</span>
<span id="cb12-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [1] "cosine_annealing"      "csv_logger"            "early_stopping"       </span></span>
<span id="cb12-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [4] "ema"                   "linear_annealing"      "model_checkpoint"     </span></span>
<span id="cb12-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [7] "reduce_lr_on_plateau"  "step_decay"            "stop_after"           </span></span>
<span id="cb12-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [10] "swa"                   "terminate_on_collapse" "terminate_on_nan"</span></span>
<span id="cb12-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">available_transforms</span>())</span>
<span id="cb12-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 118</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-7-2" class="tab-pane" aria-labelledby="tabset-7-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">[o[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> o <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> pp.available_optimizers()]</span>
<span id="cb13-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ['adadelta', 'adagrad', 'adam', 'adamax', 'adamw', 'nadam', 'rmsprop',</span></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  'sgd']</span></span>
<span id="cb13-4">[c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> pp.available_callbacks()]</span>
<span id="cb13-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ['cosine_annealing', 'csv_logger', 'early_stopping', 'ema',</span></span>
<span id="cb13-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  'linear_annealing', 'model_checkpoint', 'reduce_lr_on_plateau',</span></span>
<span id="cb13-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  'step_decay', 'stop_after', 'swa', 'terminate_on_collapse',</span></span>
<span id="cb13-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  'terminate_on_nan']</span></span>
<span id="cb13-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(pp.available_transforms())</span>
<span id="cb13-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 118</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>Eight optimisers; twelve callbacks, from early stopping and learning-rate schedules to weight averaging (<code>ema</code>, <code>swa</code>) and two that stop a run that has gone wrong rather than letting it finish; and 118 augmentations from <a href="https://albumentations.ai">albumentations</a>, each named exactly as albumentations names it, with its own parameters under <code>params</code>.</p>
<p>Fifteen of them, on the image from the architecture diagrams, each applied every time (<code>p = 1</code>); the yellow line is the mask that comes out with it. The ones that move pixels around move the mask with the image, and the ones that change colour, contrast or sharpness leave it where it was:</p>
<div class="cell" data-renderings="[&quot;light&quot;,&quot;dark&quot;]">
<div class="cell-output-display light-content">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datascienceguts.com/posts/2026-11-12-segmentation-tour/index_files/figure-html/augmentations-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>The original image and fifteen albumentations transforms. Yellow: the outline of the mask after each transform.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="big-images-tiles-instead-of-shrinking" class="level2">
<h2 class="anchored" data-anchor-id="big-images-tiles-instead-of-shrinking">Big images: tiles instead of shrinking</h2>
<p>Every image is resized to <code>input_shape</code> before the network sees it. The nuclei lose little that way, which is why <code>nuclei.yaml</code> has no <code>splits</code>, but a 1024-pixel image shrunk to 128 keeps a nucleus as a few pixels. <strong><code>splits</code></strong> cuts the image into a grid of tiles instead, one number per spatial dimension:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">models</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb14-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> tiled</span></span>
<span id="cb14-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">architecture</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> u_net</span></span>
<span id="cb14-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">input_shape</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">]</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">     # what the network sees: one tile</span></span>
<span id="cb14-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">splits</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">]</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">              # 4 rows x 4 columns</span></span>
<span id="cb14-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">batch_size</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span></code></pre></div></div>
<p>The constructors take the same field:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-8-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-8-1" aria-controls="tabset-8-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-8-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-8-2" aria-controls="tabset-8-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-8-1" class="tab-pane active" aria-labelledby="tabset-8-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">tiled <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">u_net</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tiled"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">input_shape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">splits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb15-2">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">blocks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filters =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">batch_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">epochs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>)</span>
<span id="cb15-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">platypus_spec</span>(</span>
<span id="cb15-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segmentation_data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colormap =</span> binary_colormap),</span>
<span id="cb15-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">models =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(tiled)</span>
<span id="cb15-6">)</span>
<span id="cb15-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; platypus specification</span></span>
<span id="cb15-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   data        : nuclei </span></span>
<span id="cb15-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   classes     : 2 </span></span>
<span id="cb15-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   models      : 1 </span></span>
<span id="cb15-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     tiled          u_net            128x128, tiled 4x4, cce, 40 epochs</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-8-2" class="tab-pane" aria-labelledby="tabset-8-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1">tiled <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tiled"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"architecture"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u_net"</span>,</span>
<span id="cb16-2">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input_shape"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"splits"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>],</span>
<span id="cb16-3">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blocks"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"filters"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"batch_size"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"epochs"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>}</span>
<span id="cb16-4">spec <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pp.from_dict({</span>
<span id="cb16-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"task"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"semantic_segmentation"</span>,</span>
<span id="cb16-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>: {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"train_path"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation_path"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>,</span>
<span id="cb16-7">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"colormap"</span>: [[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>]]},</span>
<span id="cb16-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"models"</span>: [tiled],</span>
<span id="cb16-9">})</span>
<span id="cb16-10">m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spec.models[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb16-11">m.load_shape, m.tiles_per_image</span>
<span id="cb16-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ((512, 512), 16)</span></span></code></pre></div></div>
</div>
</div>
</div>
<p><code>input_shape</code> is still what the network sees, which is now one tile. The image is read at <code>splits</code> times that and cut, so one source image gives as many training samples as there are tiles:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>splits</th>
<th style="text-align: right;">image read at</th>
<th style="text-align: right;">tiles per image</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>not set</td>
<td style="text-align: right;">128 x 128</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td><code>[2, 3]</code></td>
<td style="text-align: right;">256 x 384</td>
<td style="text-align: right;">6</td>
</tr>
<tr class="odd">
<td><code>[4, 4]</code></td>
<td style="text-align: right;">512 x 512</td>
<td style="text-align: right;">16</td>
</tr>
</tbody>
</table>
<div class="cell" data-renderings="[&quot;light&quot;,&quot;dark&quot;]">
<div class="cell-output-display light-content">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datascienceguts.com/posts/2026-11-12-segmentation-tour/index_files/figure-html/tiling-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>One 1024 x 1024 image from the nuclei data as a network with input_shape 128 x 128 gets it: shrunk without splits, cut into sixteen tiles with splits [4, 4].</figcaption>
</figure>
</div>
</div>
</div>
<p>Three things follow:</p>
<ul>
<li>The source image does not have to be a multiple of the tile: it is resized to the size it is read at - 1024 x 1024 to 512 x 512 above, and a 696 x 520 image to the same square - and only then cut. The tile has the rule it had without tiling: every block halves it, so with <code>blocks = 4</code> each side of <code>input_shape</code> must divide by 16. A tile of <code>127 x 127</code> is refused when the file is read: <em>every spatial dimension must be divisible by 16; input_shape=(127, 127) is not</em>.</li>
<li><code>predict()</code> puts the tiles back together, so it returns one mask per image at its original size rather than sixteen pieces.</li>
<li><code>cldice</code> and <code>surface_dice</code> are properties of a whole mask, not of a tile, so a tiled run does not report them per epoch; <code>evaluate_cases()</code> scores them per image afterwards. A callback that watches one of them on a tiled run is refused when the file is read.</li>
</ul>
</section>
<section id="the-series-so-far" class="level2">
<h2 class="anchored" data-anchor-id="the-series-so-far">The series so far</h2>
<ol type="1">
<li><a href="../2026-11-03-rplatypus/">rplatypus rebuilt: deep learning for medical images in R</a> - why the 2020 package had to be rewritten, how it now installs its own Python, and a first mask from published weights.</li>
<li><a href="../2026-11-06-nuclei-mean-dice/">Nuclei, five years later</a> - a mean Dice of 0.92, and the three images it hides.</li>
<li><strong>Semantic segmentation in rplatypus and pyplatypus: what is in the box</strong> - this post.</li>
</ol>
<p>The next one leaves segmentation for classification, where the number that hides things is not a mean but an accuracy: a classifier at 0.97 that has learnt nothing at all.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>Python</category>
  <category>deep learning</category>
  <category>medical imaging</category>
  <category>rplatypus</category>
  <category>pyplatypus</category>
  <guid>https://datascienceguts.com/posts/2026-11-12-segmentation-tour/</guid>
  <pubDate>Thu, 12 Nov 2026 00:00:00 GMT</pubDate>
  <media:content url="https://datascienceguts.com/posts/2026-11-12-segmentation-tour/thumbnail.png" medium="image" type="image/png" height="72" width="144"/>
</item>
<item>
  <title>Nuclei, five years later: what a mean Dice hides</title>
  <dc:creator>Michał Maj</dc:creator>
  <link>https://datascienceguts.com/posts/2026-11-06-nuclei-mean-dice/</link>
  <description><![CDATA[ 






<p>In 2020 I trained a U-Net on the <a href="https://www.kaggle.com/c/data-science-bowl-2018">2018 Data Science Bowl</a> nuclei with the first version of this package, watched a Dice score climb during training, and finished the post with four predicted masks on the competition’s test images. Those images have no masks. The pictures looked good, and I had no way of knowing where the model was wrong - so I never asked.</p>
<p>This post asks. Nothing in it trains: the model is the published <code>dsbowl-unet</code> from the <a href="../2026-11-03-rplatypus/">first post of the series</a>, scored on the images it was held out from. Scoring all of them took about twelve seconds on a processor.</p>
<section id="the-images-it-was-held-out-from" class="level2">
<h2 class="anchored" data-anchor-id="the-images-it-was-held-out-from">The images it was held out from</h2>
<p>The data is <a href="https://bbbc.broadinstitute.org/BBBC038">BBBC038v1</a> from the Broad Institute - the Data Science Bowl’s images under a CC0 waiver. Its <code>stage1_train</code> holds 670 annotated images, one directory each, with one mask file per nucleus.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">data_dir <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stage1_train"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># BBBC038v1, unzipped</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">data_dir <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stage1_train"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># BBBC038v1, unzipped</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>The weights were measured on an 80/20 split of all 670 at seed 1, so that split can be made again. <code>split_dataset()</code> copies nothing; it writes CSVs listing the files:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rplatypus)</span>
<span id="cb3-2"></span>
<span id="cb3-3">split <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split_dataset</span>(data_dir, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tempfile</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"splits"</span>),</span>
<span id="cb3-4">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fractions =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-5">split</span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; platypus split</span></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   train        536 samples</span></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   validation   134 samples</span></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   every sample its own group</span></span>
<span id="cb3-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   seed 1 - the same inputs give this split again</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tempfile</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pyplatypus <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pp</span>
<span id="cb4-3"></span>
<span id="cb4-4">split <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pp.split_dataset(data_dir, tempfile.mkdtemp(),</span>
<span id="cb4-5">                         fractions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>), seed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-6">split[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"samples"</span>]</span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; {'train': 536, 'validation': 134, 'test': 0}</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>Making the split again matters more than it looks. My own copy of this dataset has a directory called <code>stage1_validation</code> with exactly 134 images in it - the same count - and 101 of them are in the training part of the split the weights were measured on. Scored there, the model would be graded partly on images it learnt from, and the count would never tell you.</p>
</section>
<section id="one-number" class="level2">
<h2 class="anchored" data-anchor-id="one-number">One number</h2>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-3-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-1" aria-controls="tabset-3-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-3-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-2" aria-controls="tabset-3-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-3-1" class="tab-pane active" aria-labelledby="tabset-3-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">platypus_spec</span>(</span>
<span id="cb5-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segmentation_data</span>(split, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colormap =</span> binary_colormap),</span>
<span id="cb5-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">models =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">u_net</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">input_shape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">blocks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filters =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>,</span>
<span id="cb5-5">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dsbowl-unet"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fit =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb5-6">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metrics =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">metric_dice</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include_background =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)))</span>
<span id="cb5-7">  )</span>
<span id="cb5-8">)</span>
<span id="cb5-9">fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">platypus_fit</span>(spec, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb5-10"></span>
<span id="cb5-11">cases <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evaluate_cases</span>(fit)</span>
<span id="cb5-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(cases)</span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   metric   n      mean         sd    median       min       max</span></span>
<span id="cb5-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1   dice 134 0.9205236 0.05322748 0.9313503 0.7239583 0.9860661</span></span>
<span id="cb5-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                                                         worst_case</span></span>
<span id="cb5-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1 7f38885521586fc6011bef1314a9fb2aa1e4935bd581b2991e1d963395eab770</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-3-2" class="tab-pane" aria-labelledby="tabset-3-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">engine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pp.build_engine(pp.from_dict({</span>
<span id="cb6-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"task"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"semantic_segmentation"</span>,</span>
<span id="cb6-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>: {</span>
<span id="cb6-4">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"train_path"</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(split[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"paths"</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"train"</span>]),</span>
<span id="cb6-5">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation_path"</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(split[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"paths"</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation"</span>]),</span>
<span id="cb6-6">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"colormap"</span>: [[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>]],</span>
<span id="cb6-7">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mode"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"config_file"</span>,</span>
<span id="cb6-8">    },</span>
<span id="cb6-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"models"</span>: [{</span>
<span id="cb6-10">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>,</span>
<span id="cb6-11">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"architecture"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u_net"</span>,</span>
<span id="cb6-12">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input_shape"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>],</span>
<span id="cb6-13">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blocks"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb6-14">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"filters"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>,</span>
<span id="cb6-15">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"weights"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dsbowl-unet"</span>,</span>
<span id="cb6-16">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fit"</span>: <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb6-17">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"metrics"</span>: [{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dice"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"include_background"</span>: <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>}],</span>
<span id="cb6-18">    }],</span>
<span id="cb6-19">}))</span>
<span id="cb6-20">engine.fit()</span>
<span id="cb6-21"></span>
<span id="cb6-22">cases <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> engine.evaluate_cases(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>)</span>
<span id="cb6-23">pp.summarise_cases(cases)</span>
<span id="cb6-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [{'metric': 'dice', 'n': 134, 'mean': 0.9205236150257623,</span></span>
<span id="cb6-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   'sd': 0.05322747958922138, 'median': 0.9313502907752991,</span></span>
<span id="cb6-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   'min': 0.7239583134651184, 'max': 0.9860661029815674,</span></span>
<span id="cb6-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   'worst_case':</span></span>
<span id="cb6-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     '7f38885521586fc6011bef1314a9fb2aa1e4935bd581b2991e1d963395eab770'}]</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>A mean Dice of 0.9205 and a worst image at 0.7240 - the model card’s numbers to four digits, which is the check that this is the card’s split. <code>include_background = FALSE</code> keeps the background out of the average; with it in, a model that found no nuclei at all would still score around half.</p>
<p>0.92 is a good number, and it is true. The question is what it is an average <em>of</em>.</p>
</section>
<section id="one-row-per-image" class="level2">
<h2 class="anchored" data-anchor-id="one-row-per-image">One row per image</h2>
<p><code>evaluate()</code> gives one row per model. <code>evaluate_cases()</code> gives one row per image, and that is the table worth reading:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-4-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-1" aria-controls="tabset-4-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-4-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-2" aria-controls="tabset-4-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-4-1" class="tab-pane active" aria-labelledby="tabset-4-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(cases[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dice), ], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; platypus scores by case (5 rows)</span></span>
<span id="cb7-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                                                              case      dice</span></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  7f38885521586fc6011bef1314a9fb2aa1e4935bd581b2991e1d963395eab770 0.7239583</span></span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  29ea4f6eb4545f43868a9b40a60000426bf8dfd9d062546656a37bd2a2aaf9ec 0.7531800</span></span>
<span id="cb7-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  8f94a80b95a881d0efdec36affc915dca9609f4cba8134c4a91b219d418778aa 0.7541562</span></span>
<span id="cb7-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  a6515d73077866808ad4cb837ecdac33612527b8a1041e82135e40fce2bb9380 0.7588775</span></span>
<span id="cb7-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  2a1a294e21d76efd0399e4eb321b45f44f7510911acd92c988480195c5b4c812 0.7702851</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-4-2" class="tab-pane" aria-labelledby="tabset-4-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1">worst <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(cases, key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> row: row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dice"</span>])[:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]</span>
<span id="cb8-2">[(row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"case"</span>][:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>], <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dice"</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> worst]</span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [('7f38885521586fc6', 0.724), ('29ea4f6eb4545f43', 0.7532),</span></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  ('8f94a80b95a881d0', 0.7542), ('a6515d7307786680', 0.7589),</span></span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  ('2a1a294e21d76efd', 0.7703)]</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>Those names say nothing, so give each image a label. The Data Science Bowl mixed three kinds of microscopy, and two numbers tell them apart well enough: whether the picture has colour, and whether its background is light or dark.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-5-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-5-1" aria-controls="tabset-5-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-5-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-5-2" aria-controls="tabset-5-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-5-1" class="tab-pane active" aria-labelledby="tabset-5-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">image_kind <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(case) {</span>
<span id="cb9-2">  path <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list.files</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(data_dir, case, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"images"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb9-3">  pixels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> png<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readPNG</span>(path[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])[, , <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb9-4">  colour <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(pixels[, , <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pixels[, , <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(pixels[, , <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pixels[, , <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]))</span>
<span id="cb9-6">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (colour <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stained"</span>)</span>
<span id="cb9-7">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pixels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brightfield"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fluorescence"</span></span>
<span id="cb9-8">}</span>
<span id="cb9-9">cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>kind <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>case, image_kind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">character</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb9-10"></span>
<span id="cb9-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aggregate</span>(dice <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> kind, cases, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb9-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(x)), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb9-13">})</span>
<span id="cb9-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;           kind  dice.n dice.mean dice.min</span></span>
<span id="cb9-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1  brightfield   3.000     0.749    0.724</span></span>
<span id="cb9-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2 fluorescence 105.000     0.935    0.753</span></span>
<span id="cb9-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3      stained  26.000     0.883    0.805</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-5-2" class="tab-pane" aria-labelledby="tabset-5-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pathlib <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Path</span>
<span id="cb10-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb10-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> PIL <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Image</span>
<span id="cb10-4"></span>
<span id="cb10-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> image_kind(case):</span>
<span id="cb10-6">    path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">next</span>((Path(data_dir) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> case <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"images"</span>).glob(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"*.png"</span>))</span>
<span id="cb10-7">    pixels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.asarray(Image.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(path).convert(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RGB"</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span></span>
<span id="cb10-8">    colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(pixels[..., <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pixels[..., <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]).mean()</span>
<span id="cb10-9">              <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(pixels[..., <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pixels[..., <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]).mean())</span>
<span id="cb10-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>:</span>
<span id="cb10-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stained"</span></span>
<span id="cb10-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brightfield"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> pixels.mean() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fluorescence"</span></span>
<span id="cb10-13"></span>
<span id="cb10-14"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cases:</span>
<span id="cb10-15">    row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kind"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> image_kind(row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"case"</span>])</span>
<span id="cb10-16"></span>
<span id="cb10-17">kinds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>({row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kind"</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cases})</span>
<span id="cb10-18">dice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {k: [r[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dice"</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cases <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> r[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kind"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> k] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> kinds}</span>
<span id="cb10-19">{k: (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(d), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(np.mean(d)), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(d), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb10-20"> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k, d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> dice.items()}</span>
<span id="cb10-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; {'brightfield': (3, 0.749, 0.724), 'fluorescence': (105, 0.935, 0.753),</span></span>
<span id="cb10-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  'stained': (26, 0.883, 0.805)}</span></span></code></pre></div></div>
</div>
</div>
</div>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-6-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-6-1" aria-controls="tabset-6-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-6-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-6-2" aria-controls="tabset-6-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-6-1" class="tab-pane active" aria-labelledby="tabset-6-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(cases, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(dice, kind)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dice), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>,</span>
<span id="cb11-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_jitter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dice, per image"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>)</span></code></pre></div></div>
</div>
</div>
<div id="tabset-6-2" class="tab-pane" aria-labelledby="tabset-6-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb12-2"></span>
<span id="cb12-3">rng <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.default_rng(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb12-4">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>))</span>
<span id="cb12-5">ax.axvline(np.mean([r[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dice"</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cases]), linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"--"</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>)</span>
<span id="cb12-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(kinds):</span>
<span id="cb12-7">    jitter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rng.uniform(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(dice[k]))</span>
<span id="cb12-8">    ax.scatter(dice[k], i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> jitter, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>)</span>
<span id="cb12-9">ax.set_yticks(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(kinds)), kinds)</span>
<span id="cb12-10">ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dice, per image"</span>)</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="cell-output-display">
<div id="fig-cases" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-cases-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datascienceguts.com/posts/2026-11-06-nuclei-mean-dice/index_files/figure-html/fig-cases-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-cases-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Dice for each of the 134 held-out images. The dashed line is the mean.
</figcaption>
</figure>
</div>
</div>
</div>
<p>There are three brightfield images among the 134, and they score 0.72, 0.75 and 0.77 - three of the five worst. Fluorescence averages 0.93 over 105 images, and it is most of what the mean measures. Stained tissue sits between the two, at 0.88.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-7-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-7-1" aria-controls="tabset-7-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-7-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-7-2" aria-controls="tabset-7-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-7-1" class="tab-pane active" aria-labelledby="tabset-7-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">predictions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">split =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation"</span>)</span>
<span id="cb13-2"></span>
<span id="cb13-3">files_of <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(case, what) {</span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list.files</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(data_dir, case, what), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb13-5">}</span>
<span id="cb13-6">truth_of <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(case) {</span>
<span id="cb13-7">  masks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">files_of</span>(case, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"masks"</span>), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(f) {</span>
<span id="cb13-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_masks</span>(f, binary_colormap, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>))[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, , ]</span>
<span id="cb13-9">  })</span>
<span id="cb13-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unite_masks</span>(masks)</span>
<span id="cb13-11">}</span>
<span id="cb13-12"></span>
<span id="cb13-13">middle <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which.min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dice <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dice)))</span>
<span id="cb13-14">shown <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>kind <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brightfield"</span>), middle)</span>
<span id="cb13-15">images <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_images</span>(</span>
<span id="cb13-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>case[shown], <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(k) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">files_of</span>(k, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"images"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>),</span>
<span id="cb13-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)</span>
<span id="cb13-18">)</span>
<span id="cb13-19">truth <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simplify2array</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>case[shown], truth_of))</span>
<span id="cb13-20"></span>
<span id="cb13-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_masks</span>(</span>
<span id="cb13-22">  images,</span>
<span id="cb13-23">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prediction =</span> predictions[shown, , ],</span>
<span id="cb13-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aperm</span>(truth, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)),</span>
<span id="cb13-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colormap   =</span> binary_colormap,</span>
<span id="cb13-26">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%s, Dice %.2f"</span>, cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>kind[shown], cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dice[shown])</span>
<span id="cb13-27">)</span></code></pre></div></div>
</div>
</div>
<div id="tabset-7-2" class="tab-pane" aria-labelledby="tabset-7-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1">predictions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> engine.predict(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation"</span>)</span>
<span id="cb14-2">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> engine.dataset(engine.runs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>].spec, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation"</span>)</span>
<span id="cb14-3"></span>
<span id="cb14-4">middle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.median([r[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dice"</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cases])</span>
<span id="cb14-5">shown <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(cases) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> r[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kind"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brightfield"</span>]</span>
<span id="cb14-6">shown.append(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(cases)),</span>
<span id="cb14-7">                 key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> i: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(cases[i][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dice"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> middle)))</span>
<span id="cb14-8"></span>
<span id="cb14-9">pp.plot_masks(</span>
<span id="cb14-10">    np.stack([data[i][<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> shown]),</span>
<span id="cb14-11">    prediction<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>predictions[shown],</span>
<span id="cb14-12">    truth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>np.stack([data[i][<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> shown]),</span>
<span id="cb14-13">    labels<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cases[i][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'kind'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Dice </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cases[i][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dice'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb14-14">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> shown],</span>
<span id="cb14-15">)</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="cell-output-display">
<div id="fig-worst" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-worst-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datascienceguts.com/posts/2026-11-06-nuclei-mean-dice/index_files/figure-html/fig-worst-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-worst-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: The three brightfield images, and a fluorescence image from the middle of the ranking. In the last column green is found, red is missed and yellow is invented.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="the-kind-of-microscope-or-the-size-of-a-nucleus" class="level2">
<h2 class="anchored" data-anchor-id="the-kind-of-microscope-or-the-size-of-a-nucleus">The kind of microscope, or the size of a nucleus?</h2>
<p>The pictures suggest a second reading. The brightfield images are 1024 by 1024 pixels, and the model sees every image at 256 by 256 - so a nucleus sixteen pixels across arrives as four. Measure the average nucleus on the model’s grid:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-8-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-8-1" aria-controls="tabset-8-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-8-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-8-2" aria-controls="tabset-8-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-8-1" class="tab-pane active" aria-labelledby="tabset-8-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">nucleus_area <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(case) {</span>
<span id="cb15-2">  files <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list.files</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(data_dir, case, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"masks"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb15-3">  areas <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(files, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(f) {</span>
<span id="cb15-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_masks</span>(f, binary_colormap) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb15-5">  }, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb15-6">  size <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_masks</span>(files[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], binary_colormap))[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb15-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(areas) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prod</span>(size)</span>
<span id="cb15-8">}</span>
<span id="cb15-9">cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>area <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(cases<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>case, nucleus_area, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb15-10"></span>
<span id="cb15-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aggregate</span>(area <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> kind, cases, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(x)))</span>
<span id="cb15-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;           kind area.1 area.2</span></span>
<span id="cb15-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1  brightfield     11     17</span></span>
<span id="cb15-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2 fluorescence     69    957</span></span>
<span id="cb15-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3      stained    137   1893</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-8-2" class="tab-pane" aria-labelledby="tabset-8-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> nucleus_area(case):</span>
<span id="cb16-2">    files <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>((Path(data_dir) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> case <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"masks"</span>).glob(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"*.png"</span>))</span>
<span id="cb16-3">    masks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [np.asarray(Image.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(f)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> files]</span>
<span id="cb16-4">    height, width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> masks[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].shape[:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb16-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.mean([m.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> masks]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> width)</span>
<span id="cb16-6"></span>
<span id="cb16-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cases:</span>
<span id="cb16-8">    row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nucleus_area(row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"case"</span>])</span>
<span id="cb16-9"></span>
<span id="cb16-10">areas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {k: [r[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area"</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cases <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> r[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kind"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> k] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> kinds}</span>
<span id="cb16-11">{k: (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(a)), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(a))) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k, a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> areas.items()}</span>
<span id="cb16-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; {'brightfield': (11, 17), 'fluorescence': (69, 957),</span></span>
<span id="cb16-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  'stained': (137, 1893)}</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>On every other image an average nucleus covers at least 69 pixels of the grid. On the three brightfield images it covers eleven to seventeen. So the three images are both the only brightfield in the split and the only ones where the nuclei are this small, and <strong>three images cannot tell those two explanations apart</strong>. Training does not help either: of the 536 training images, 13 are brightfield, all of them 1024 by 1024 as well.</p>
<p>Scoring at the files’ own resolution does not separate them either, because the detail was lost before the score was taken - the prediction was made at 256:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-9-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-9-1" aria-controls="tabset-9-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-9-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-9-2" aria-controls="tabset-9-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-9-1" class="tab-pane active" aria-labelledby="tabset-9-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evaluate_cases</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source"</span>))</span>
<span id="cb17-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   metric   n     mean         sd    median       min       max</span></span>
<span id="cb17-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1   dice 134 0.920808 0.05371718 0.9313503 0.7164674 0.9860661</span></span>
<span id="cb17-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                                                         worst_case</span></span>
<span id="cb17-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1 7f38885521586fc6011bef1314a9fb2aa1e4935bd581b2991e1d963395eab770</span></span></code></pre></div></div>
</div>
</div>
<div id="tabset-9-2" class="tab-pane" aria-labelledby="tabset-9-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1">pp.summarise_cases(engine.evaluate_cases(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, at<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source"</span>))</span>
<span id="cb18-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [{'metric': 'dice', 'n': 134, 'mean': 0.920808045276955,</span></span>
<span id="cb18-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   'sd': 0.053717183966230486, 'median': 0.9313502907752991,</span></span>
<span id="cb18-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   'min': 0.7164673805236816, 'max': 0.9860661029815674,</span></span>
<span id="cb18-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   'worst_case':</span></span>
<span id="cb18-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     '7f38885521586fc6011bef1314a9fb2aa1e4935bd581b2991e1d963395eab770'}]</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>What would separate them is a model that sees these images at a resolution where a nucleus is as large as it is everywhere else - tiling instead of resizing, which is a post of its own later in the series.</p>
</section>
<section id="what-to-take-from-this" class="level2">
<h2 class="anchored" data-anchor-id="what-to-take-from-this">What to take from this</h2>
<p>The mean was not wrong. 0.92 is what this model scores on these 134 images. But it is mostly an average over 105 fluorescence images, and the three images it does worst on share two properties at once, either of which could be the cause. None of that is visible in one number; all of it is visible in a table with one row per image and a column you add yourself.</p>
<p>If you report a segmentation score, report the worst case beside the mean, and look at what the worst cases have in common. Here it took one function call and a few lines of R or Python.</p>
</section>
<section id="the-series-so-far" class="level2">
<h2 class="anchored" data-anchor-id="the-series-so-far">The series so far</h2>
<ol type="1">
<li><a href="../2026-11-03-rplatypus/">rplatypus rebuilt: deep learning for medical images in R</a> - why the 2020 package had to be rewritten, how it now installs its own Python, and a first mask from the same published weights used here.</li>
<li><strong>Nuclei, five years later</strong> - this post.</li>
</ol>
<p>The <a href="../2026-11-12-segmentation-tour/">next one</a> stays with segmentation but steps back from a single dataset: what rplatypus offers for it - four architectures and how they differ, the losses and metrics to choose from, and the YAML file that describes a whole experiment, field by field.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>Python</category>
  <category>deep learning</category>
  <category>medical imaging</category>
  <category>rplatypus</category>
  <category>pyplatypus</category>
  <guid>https://datascienceguts.com/posts/2026-11-06-nuclei-mean-dice/</guid>
  <pubDate>Fri, 06 Nov 2026 00:00:00 GMT</pubDate>
  <media:content url="https://datascienceguts.com/posts/2026-11-06-nuclei-mean-dice/thumbnail.png" medium="image" type="image/png" height="72" width="144"/>
</item>
<item>
  <title>rplatypus rebuilt: deep learning for medical images in R</title>
  <dc:creator>Michał Maj</dc:creator>
  <link>https://datascienceguts.com/posts/2026-11-03-rplatypus/</link>
  <description><![CDATA[ 






<p>In October 2020 I wrote about <a href="https://github.com/maju116/rplatypus/tree/master">platypus</a>, an R package for object detection and image segmentation: YOLOv3 and U-Net in a few lines of R, on top of the <code>keras</code> and <code>tensorflow</code> packages. People used it. It collected 134 stars and a queue of issues - and then it quietly stopped working, and I stopped answering them.</p>
<p>This year I went back to it. Computer vision is the part of data science I enjoy most, and this package has always been my passion project - the place where I get to learn things my day job does not ask of me. What came out is not an update but a different package, with a new name, a different backend and a narrower purpose: <strong>rplatypus</strong>, deep learning for medical images in R. It has been submitted to CRAN and is waiting for review; until it is accepted, install it from GitHub:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages("remotes")</span></span>
<span id="cb1-2">remotes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"maju116/rplatypus"</span>)</span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Update, 14 November 2026:</strong> rplatypus is on CRAN. Version 0.3.1 is there now, with classification included:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rplatypus"</span>)</span></code></pre></div></div>
</div>
</div>
<p>This post is about why it had to be rewritten rather than fixed, what is in it now, and where the series of posts that follows is going.</p>
<section id="why-the-old-one-broke" class="level2">
<h2 class="anchored" data-anchor-id="why-the-old-one-broke">Why the old one broke</h2>
<p>R’s <code>keras</code> and <code>tensorflow</code> packages are not bindings to a C++ library. They are bridges, through <code>reticulate</code>, into a Python installation of TensorFlow - and in 2020 that was <em>the</em> Python installation, the one shared with everything else on the machine. Upgrade numpy for an unrelated project and an R session using platypus could stop loading. The failure never appeared in my package’s code, which made it miserable to support: the bug reports were about other people’s Python.</p>
<p>The second problem is the one that actually made me stop. platypus sat on two layers I did not control: the R <code>keras</code> and <code>tensorflow</code> packages on top of TensorFlow itself. When TensorFlow changed in Python, the R packages had to catch up before platypus could, and that often took a very long time. Until they did, my package was stuck, with issues I could not fix from my side. After enough of that, I lost the will to keep working on it.</p>
<p>Two things have changed since, and together they remove both problems at the root rather than patching around them.</p>
<p><strong>reticulate learned to isolate.</strong> Since version 1.41, a package declares its Python requirements with <code>py_require()</code>, and reticulate builds a private environment for them with <a href="https://docs.astral.sh/uv/">uv</a>, cached and shared with nobody. There is no common Python left for two packages to fight over. rplatypus never touches a Python anyone else uses - and the person installing it never chooses an interpreter, activates anything or matches a version. The first call that needs the engine builds the environment; every call after that reuses it. And there is no R wrapper in between any more: rplatypus calls its own engine, which I release myself, so nothing waits for anybody else to catch up.</p>
<p><strong>The medical imaging world settled on PyTorch.</strong> MONAI, nnU-Net, TorchIO, timm, the pretrained encoders and the foundation models - all of it is PyTorch. TensorFlow for medical images is effectively over, and the TensorFlow 2.9 the old engine was pinned to does not even install on Python 3.12. Staying would have meant rewriting anyway, onto a backend with no future in the field.</p>
<p>A third thing changed too. It does not fix the old failure, but it is the reason the rewrite happened at all instead of staying a good idea: <strong>writing a package got much cheaper.</strong> In 2020 I wrote every line of platypus myself, and the parts that make a package trustworthy - tests, documentation, error messages that say what went wrong - were the parts I never had time left for. This time I worked with an AI coding agent, <a href="https://claude.com/claude-code">Claude Code</a>.</p>
<p>I want to be precise about what that means, because “AI wrote my package” is not it. The decisions were mine: what the package is for, which datasets to measure it on, what it should refuse, and what a result has to show before it goes into the documentation - every number in this series was measured, not generated. What the agent changed was the cost of everything around those decisions: writing the tests, keeping two packages in two languages in step, chasing a failing check through reticulate, uv and CUDA. That is exactly the work that used to end side projects like this one. The rewrite took around a month.</p>
<p>So the computation moved to <a href="https://pypi.org/project/pyplatypus/">pyplatypus</a>, a Python engine on PyTorch, and rplatypus is the R package on top of it. The split is deliberate:</p>
<ul>
<li><strong>The engine is in Python</strong>, because that is where the models, the weights and the medical libraries already are. One codebase to maintain, and everything PyTorch has is within reach.</li>
<li><strong>The product is in R</strong>, because that is where the gap is. Reading and combining masks, plotting, comparing models, the analysis around the network - that part is plain R and needs no Python at all.</li>
</ul>
<p>I did consider native R <code>torch</code>, which binds libtorch directly and needs no Python. For one person maintaining this in their evenings it would have meant writing every architecture, loss and data pipeline twice, and giving up every pretrained weight in the Python ecosystem.</p>
</section>
<section id="pyplatypus-the-same-engine-from-python" class="level2">
<h2 class="anchored" data-anchor-id="pyplatypus-the-same-engine-from-python">pyplatypus: the same engine, from Python</h2>
<p>The engine is not hidden plumbing. <a href="https://github.com/maju116/pyplatypus">pyplatypus</a> is a package on PyPI in its own right - it too started life in 2022 on TensorFlow and was rewritten alongside - and if you work in Python you can use it directly:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pyplatypus[hub]"</span></span></code></pre></div></div>
</div>
<p>The example below runs from both languages, out of one file.</p>
<p>The two are developed together, and in a fixed order. A feature lands in the engine first, the engine is published to PyPI, and only then does rplatypus move to it - each release of the R package pins one exact version of pyplatypus, so it cannot break because the engine changed underneath it. They also share one <a href="https://maju116.github.io/pyplatypus/config.html">configuration format</a>: the same YAML file describes the same experiment whichever language reads it, so an R user and a Python user can hand each other a run.</p>
<p>That is also how this series is written. The examples are in R, because that is where the gap is, and each post shows what the same step looks like from Python.</p>
</section>
<section id="the-gap" class="level2">
<h2 class="anchored" data-anchor-id="the-gap">The gap</h2>
<p>Here is the part that convinced me this was worth doing. R has excellent tools for <em>reading</em> medical images - RNifti, oro.dicom, divest - and a healthy deep learning stack. Almost nothing joins them. When I checked in September, the CRAN Task View for <a href="https://cran.r-project.org/view=MedicalImaging">Medical Image Analysis</a> (last updated August 2025) listed no deep learning packages at all.</p>
<p>So rplatypus is meant to be that missing piece: not a wrapper around a Python library you then have to learn, but an R package that happens to compute in Python.</p>
</section>
<section id="a-mask-without-training-anything" class="level2">
<h2 class="anchored" data-anchor-id="a-mask-without-training-anything">A mask, without training anything</h2>
<p>The quickest way to see whether a package does anything useful is to borrow a model someone already trained. rplatypus publishes weights by name - here a U-Net trained on the 2018 Data Science Bowl, the same nuclei dataset I used on this blog in 2020.</p>
<p>The folder <code>nuclei/</code> holds four microscopy images in the dataset’s own layout - one directory per image, with <code>images/</code> and <code>masks/</code> inside.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rplatypus)</span>
<span id="cb4-2"></span>
<span id="cb4-3">spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">platypus_spec</span>(</span>
<span id="cb4-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segmentation_data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colormap =</span> binary_colormap),</span>
<span id="cb4-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">models =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">u_net</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">input_shape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">blocks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filters =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>,</span>
<span id="cb4-6">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dsbowl-unet"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fit =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb4-7">)</span>
<span id="cb4-8">fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">platypus_fit</span>(spec, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb4-9">masks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">split =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation"</span>)</span>
<span id="cb4-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(masks)</span>
<span id="cb4-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1]   4 256 256</span></span></code></pre></div></div>
</div>
<p>Four images, 256 by 256 pixels, one class label per pixel. This ran on the processor, with no GPU; <code>platypus_device()</code> tells you whether yours will use one, and why not if it can’t.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">images <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list.files</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[.]png$"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">recursive =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb5-2">images <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_images</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/images/"</span>, images, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>))</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_masks</span>(images, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prediction =</span> masks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colormap =</span> binary_colormap)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-nuclei" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-nuclei-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datascienceguts.com/posts/2026-11-03-rplatypus/index_files/figure-html/fig-nuclei-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-nuclei-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Nuclei found by the published <code>dsbowl-unet</code> weights, on four images of different kinds.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The first three are fluorescence images, and the nuclei come out as clean blobs. The fourth is a brightfield image of stained cells, and the model finds specks - the small dark nuclei inside the cells. That is the hard kind of image in this dataset, and the next post looks at it closely.</p>
<p>There is no score here, and that is on purpose. These four images come from the same collection the weights were trained on, so any number measured on them would flatter the model. The numbers that mean something were measured on a held-out split when the weights were published, and <code>available_weights()</code> says what they are and what the model cannot do - for instance, that it is a <em>semantic</em> model, so touching nuclei come back as one region and a count taken from them would be wrong.</p>
</section>
<section id="the-same-thing-from-a-file" class="level2">
<h2 class="anchored" data-anchor-id="the-same-thing-from-a-file">The same thing from a file</h2>
<p>The same specification can live in a file. This one started as <code>save_spec(spec, "nuclei.yaml")</code>, with the lists tidied by hand:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>nuclei.yaml</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" data-filename="nuclei.yaml" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">task</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> semantic_segmentation</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb6-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">train_path</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> nuclei</span></span>
<span id="cb6-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">validation_path</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> nuclei</span></span>
<span id="cb6-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colormap</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">],</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">]]</span></span>
<span id="cb6-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">models</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb6-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> nuclei</span></span>
<span id="cb6-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">architecture</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> u_net</span></span>
<span id="cb6-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">input_shape</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">]</span></span>
<span id="cb6-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">blocks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb6-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filters</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span id="cb6-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weights</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> dsbowl-unet</span></span>
<span id="cb6-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">false</span></span></code></pre></div></div>
</div>
<p>Reading it back gives exactly the object built from arguments above:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">identical</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">platypus_spec</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei.yaml"</span>)), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(spec))</span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] TRUE</span></span></code></pre></div></div>
</div>
<p>So <code>platypus_fit(platypus_spec("nuclei.yaml"))</code> does everything the R code did, and nothing downstream can tell which way the specification was written. The Python engine reads the very same file:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyplatypus <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> build_engine, from_yaml</span>
<span id="cb8-2"></span>
<span id="cb8-3">engine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> build_engine(from_yaml(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei.yaml"</span>))</span>
<span id="cb8-4">engine.fit()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># with `fit: false` this loads the published weights instead of training</span></span>
<span id="cb8-5">masks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> engine.predict(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nuclei"</span>, split<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation"</span>)</span></code></pre></div></div>
</div>
<p>A configuration file and an R script describe the same experiment, and either can be handed to a colleague - whichever language they work in.</p>
</section>
<section id="what-is-in-it" class="level2">
<h2 class="anchored" data-anchor-id="what-is-in-it">What is in it</h2>
<p>Briefly, because most of these get their own post:</p>
<ul>
<li><strong>Segmentation</strong>, 2D and 3D: U-Net, U-Net++, Res-U-Net and LinkNet, eleven losses, five metrics that can leave the background out - which matters when the background is most of the image.</li>
<li><strong>Classification</strong> - one label for a whole image, scored by AUC rather than accuracy, because a classifier that answers “no finding” where 3% have one scores 0.97 and has learnt nothing.</li>
<li><strong>Object detection</strong> with YOLOv3, with anchors fitted to your boxes rather than borrowed from COCO.</li>
<li><strong>Instance segmentation</strong> - a count of objects instead of an area.</li>
<li><strong>Volumes</strong>: NIfTI and DICOM in, with voxel spacing handled, a split by patient rather than by slice, and results in millilitres rather than voxels.</li>
<li><strong>A score per case, not one per dataset</strong>, so the worst image has a name.</li>
<li><strong>Refusals before a wasted day</strong>: a colormap that matches none of your masks, channels in an order you did not intend, a DICOM series with a missing slice - each stopped before training, by name.</li>
<li><strong>One specification</strong> - from R arguments or from a YAML file, read the same way by the Python engine.</li>
</ul>
<p>The full reference and seven vignettes, six of them precomputed from real runs, are at <a href="https://maju116.github.io/rplatypus/">maju116.github.io/rplatypus</a>.</p>
<p>This is where the package starts, not where it ends. The U-Net family, YOLOv3 and the classifier are the first models in it, not the only ones that will be: more architectures are coming, along with what is still missing - multi-label classification, ensembling, boxes in three dimensions. Which of those comes first depends partly on what people actually need, which is one more reason for the question at the end of this post.</p>
<p>Development is moving fast right now, and CRAN asks for new versions no more often than every month or two, so once the package is on CRAN, <code>install.packages("rplatypus")</code> will usually be a step behind. The GitHub version above will always be the newest.</p>
</section>
<section id="what-comes-next" class="level2">
<h2 class="anchored" data-anchor-id="what-comes-next">What comes next</h2>
<p>This is the first post of a series. Each of the next ones takes one dataset and one thing that measurement on it taught me:</p>
<ol type="1">
<li><strong>Nuclei, five years later</strong> - a model with a mean Dice of 0.855 that scores 0.006 on three images, and why the mean could not tell you.</li>
<li><strong>Classification</strong> - a classifier with 0.97 accuracy that has learnt nothing, and why balanced accuracy does not rescue it.</li>
<li><strong>Blood cells and YOLOv3</strong> - anchors, and why a point of mAP across five seeds is noise.</li>
<li><strong>CT volumes</strong> - a Dice of 0.88 and volumes 12-23% too large, which is not a contradiction.</li>
<li><strong>Retinal vessels</strong> - why you cannot resize a vessel nine pixels wide.</li>
<li><strong>Counting touching nuclei</strong> - 245 objects or 180, from the same forward pass.</li>
<li><strong>Pretrained encoders</strong> - measured on three modalities, where a 1.9M-parameter encoder beats ResNet-34 on two of them.</li>
</ol>
</section>
<section id="one-question-for-you" class="level2">
<h2 class="anchored" data-anchor-id="one-question-for-you">One question for you</h2>
<p>Everything above rests on an assumption I have not been able to test: that people do medical image analysis in R and would rather stay in R to do deep learning on it. If that is you - or if it is your lab, and you went to Python because there was nothing here - I would really like to hear about it. <a href="https://github.com/maju116/rplatypus/issues">Open an issue</a>, write to me at <a href="mailto:michalmaj116@gmail.com">michalmaj116@gmail.com</a>, or find me on <a href="https://www.linkedin.com/in/michal-maj116/">LinkedIn</a>.</p>
<p>And one more thing, since this is a good place to say it: I am looking for my next role. If your team works on computer vision, medical imaging, or any problem where a model has to be measured honestly before anyone trusts it, I would be glad to talk - the same email works.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>Python</category>
  <category>deep learning</category>
  <category>medical imaging</category>
  <category>rplatypus</category>
  <category>pyplatypus</category>
  <guid>https://datascienceguts.com/posts/2026-11-03-rplatypus/</guid>
  <pubDate>Tue, 03 Nov 2026 00:00:00 GMT</pubDate>
  <media:content url="https://datascienceguts.com/posts/2026-11-03-rplatypus/thumbnail.png" medium="image" type="image/png" height="69" width="144"/>
</item>
</channel>
</rss>
