<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tutorial 1: Model training (simple) — slideflow 3.0.0 documentation</title>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<!-- <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> -->
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="index" title="Index" href="../genindex/" />
<link rel="search" title="Search" href="../search/" />
<link rel="next" title="Tutorial 2: Model training (advanced)" href="../tutorial2/" />
<link rel="prev" title="slideflow.studio" href="../studio_module/" />
<script src="../_static/js/modernizr.min.js"></script>
<!-- Preload the theme fonts -->
<link rel="preload" href="../_static/fonts/FreightSans/freight-sans-book.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../_static/fonts/FreightSans/freight-sans-medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../_static/fonts/IBMPlexMono/IBMPlexMono-Medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../_static/fonts/FreightSans/freight-sans-bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../_static/fonts/FreightSans/freight-sans-medium-italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../_static/fonts/IBMPlexMono/IBMPlexMono-SemiBold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<!-- Preload the katex fonts -->
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Math-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size1-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size4-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size2-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size3-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Caligraphic-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.2/css/all.css" integrity="sha384-vSIIfh2YWi9wW0r9iZe7RJPrKwp6bG+s9QZMoITbCckVJqGCCRhc+ccxNcdpHuYu" crossorigin="anonymous">
<script defer data-domain="slideflow.dev" src="https://plausible.io/js/script.js"></script>
</head>
<div class="container-fluid header-holder tutorials-header" id="header-holder">
<div class="container">
<div class="header-container">
<a class="header-logo" href="https://slideflow.dev" aria-label="Slideflow"></a>
<div class="main-menu">
<ul>
<li class="active">
<a href="https://slideflow.dev">Docs</a>
</li>
<li>
<a href="https://slideflow.dev/tutorial1/">Tutorials</a>
</li>
<li>
<a href="https://github.com/slideflow/slideflow">GitHub</a>
</li>
</ul>
</div>
<a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a>
</div>
</div>
</div>
<body class="pytorch-body">
<div class="table-of-contents-link-wrapper">
<span>Table of Contents</span>
<a href="#" class="toggle-table-of-contents" data-behavior="toggle-table-of-contents"></a>
</div>
<nav data-toggle="wy-nav-shift" class="pytorch-left-menu" id="pytorch-left-menu">
<div class="pytorch-side-scroll">
<div class="pytorch-menu pytorch-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<div class="pytorch-left-menu-search">
<div class="version">
3.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search/" method="get">
<input type="text" name="q" placeholder="Search Docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<p class="caption" role="heading"><span class="caption-text">Introduction</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../overview/">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../quickstart/">Quickstart</a></li>
<li class="toctree-l1"><a class="reference internal" href="../project_setup/">Setting up a Project</a></li>
<li class="toctree-l1"><a class="reference internal" href="../datasets_and_val/">Datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../slide_processing/">Slide Processing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../training/">Training</a></li>
<li class="toctree-l1"><a class="reference internal" href="../evaluation/">Evaluation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../posthoc/">Layer Activations</a></li>
<li class="toctree-l1"><a class="reference internal" href="../uq/">Uncertainty Quantification</a></li>
<li class="toctree-l1"><a class="reference internal" href="../features/">Generating Features</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mil/">Multiple-Instance Learning (MIL)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../ssl/">Self-Supervised Learning (SSL)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stylegan/">Generative Networks (GANs)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../saliency/">Saliency Maps</a></li>
<li class="toctree-l1"><a class="reference internal" href="../segmentation/">Tissue Segmentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../cellseg/">Cell Segmentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../custom_loops/">Custom Training Loops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../studio/">Slideflow Studio: Live Visualization</a></li>
<li class="toctree-l1"><a class="reference internal" href="../troubleshooting/">Troubleshooting</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Developer Notes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tfrecords/">TFRecords: Reading and Writing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dataloaders/">Dataloaders: Sampling and Augmentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../custom_extractors/">Custom Feature Extractors</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tile_labels/">Strong Supervision with Tile Labels</a></li>
<li class="toctree-l1"><a class="reference internal" href="../plugins/">Creating a Slideflow Plugin</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../slideflow/">slideflow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../project/">slideflow.Project</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dataset/">slideflow.Dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dataset_features/">slideflow.DatasetFeatures</a></li>
<li class="toctree-l1"><a class="reference internal" href="../heatmap/">slideflow.Heatmap</a></li>
<li class="toctree-l1"><a class="reference internal" href="../model_params/">slideflow.ModelParams</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mosaic/">slideflow.Mosaic</a></li>
<li class="toctree-l1"><a class="reference internal" href="../slidemap/">slideflow.SlideMap</a></li>
<li class="toctree-l1"><a class="reference internal" href="../biscuit/">slideflow.biscuit</a></li>
<li class="toctree-l1"><a class="reference internal" href="../slideflow_cellseg/">slideflow.cellseg</a></li>
<li class="toctree-l1"><a class="reference internal" href="../io/">slideflow.io</a></li>
<li class="toctree-l1"><a class="reference internal" href="../io_tensorflow/">slideflow.io.tensorflow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../io_torch/">slideflow.io.torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="../gan/">slideflow.gan</a></li>
<li class="toctree-l1"><a class="reference internal" href="../grad/">slideflow.grad</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mil_module/">slideflow.mil</a></li>
<li class="toctree-l1"><a class="reference internal" href="../model/">slideflow.model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../model_tensorflow/">slideflow.model.tensorflow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../model_torch/">slideflow.model.torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="../norm/">slideflow.norm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../simclr/">slideflow.simclr</a></li>
<li class="toctree-l1"><a class="reference internal" href="../slide/">slideflow.slide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../slide_qc/">slideflow.slide.qc</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stats/">slideflow.stats</a></li>
<li class="toctree-l1"><a class="reference internal" href="../util/">slideflow.util</a></li>
<li class="toctree-l1"><a class="reference internal" href="../studio_module/">slideflow.studio</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Tutorials</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="#">Tutorial 1: Model training (simple)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial2/">Tutorial 2: Model training (advanced)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial3/">Tutorial 3: Using a custom architecture</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial4/">Tutorial 4: Model evaluation & heatmaps</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial5/">Tutorial 5: Creating a mosaic map</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial6/">Tutorial 6: Custom slide filtering</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial7/">Tutorial 7: Training with custom augmentations</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial8/">Tutorial 8: Multiple-Instance Learning</a></li>
</ul>
</div>
</div>
</nav>
<div class="pytorch-container">
<div class="pytorch-page-level-bar" id="pytorch-page-level-bar">
<div class="pytorch-breadcrumbs-wrapper">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="pytorch-breadcrumbs">
<li>
<a href="../">
Docs
</a> >
</li>
<li>Tutorial 1: Model training (simple)</li>
<li class="pytorch-breadcrumbs-aside">
<a href="../_sources/tutorial1.rst.txt" rel="nofollow"><img src="../_static/images/view-page-source-icon.svg"></a>
</li>
</ul>
</div>
</div>
<div class="pytorch-shortcuts-wrapper" id="pytorch-shortcuts-wrapper">
Shortcuts
</div>
</div>
<section data-toggle="wy-nav-shift" id="pytorch-content-wrap" class="pytorch-content-wrap">
<div class="pytorch-content-left">
<div class="rst-content">
<div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
<article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
<section id="tutorial-1-model-training-simple">
<span id="tutorial1"></span><h1>Tutorial 1: Model training (simple)<a class="headerlink" href="#tutorial-1-model-training-simple" title="Permalink to this heading">¶</a></h1>
<p>In this first tutorial, we will walk through the steps needed to take an example project from start to finish. As with all of these tutorials, we will use
publicly available data from <a class="reference external" href="https://portal.gdc.cancer.gov">The Cancer Genome Atlas (TCGA)</a>. In this first tutorial,
we will train a model to predict ER status from breast cancer slides.</p>
<p>Examples will be given assuming project files are in the directory <code class="docutils literal notranslate"><span class="pre">/home/er_project</span></code> and slides are in
<code class="docutils literal notranslate"><span class="pre">/home/brca_slides</span></code>, although you will need to customize these paths according to your needs.</p>
<section id="create-a-project">
<h2>Create a Project<a class="headerlink" href="#create-a-project" title="Permalink to this heading">¶</a></h2>
<p>First, download slides and annotations for the TCGA-BRCA project using the <a class="reference external" href="https://portal.gdc.cancer.gov/legacy-archive/search/f">legacy GDC portal</a>. We should have a total of 1133 diagnostic slides across 1062
patients. Our outcome of interest is “er_status_by_ihc”, of which 1011 have a documented result (either “Positive”
or “Negative”), giving us our final patient count of 1011.</p>
<p>Create a new project, and pass the path to the downloaded slides to the argument <code class="docutils literal notranslate"><span class="pre">slides</span></code>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">slideflow</span> <span class="k">as</span> <span class="nn">sf</span>
<span class="n">P</span> <span class="o">=</span> <span class="n">sf</span><span class="o">.</span><span class="n">create_project</span><span class="p">(</span>
<span class="n">root</span><span class="o">=</span><span class="s1">'/home/er_project'</span><span class="p">,</span>
<span class="n">slides</span><span class="o">=</span><span class="s1">'/path/to/slides'</span>
<span class="p">)</span>
</pre></div>
</div>
<p>After the project is created, we can load the project with:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">P</span> <span class="o">=</span> <span class="n">sf</span><span class="o">.</span><span class="n">load_project</span><span class="p">(</span><span class="s1">'/home/er_project'</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="setting-up-annotations">
<h2>Setting up annotations<a class="headerlink" href="#setting-up-annotations" title="Permalink to this heading">¶</a></h2>
<p>With our project initialized, we can set up our annotations file. Use the downloaded annotations file to create a new
CSV file, with a column “patient” indicating patient name (in the case of TCGA, these are in the format
TCGA-SS-XXXX, where SS indicates site of origin and XXXX is the patient identifier), and a column “er_status_by_ihc”
containing our outcome of interest. Add a third column “slide” containing the name of the slide associated with the
patient (without the file extension). If there are multiple slides per patient, list each slide on a separate row.
Finally, add a column “dataset” to indicate whether the slide should be used for training or evaluation. Set aside
somewhere around 10-30% of the dataset for evaluation.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>If patient names are identical to the slide filenames, the “slide” column does not need to be manually added, as
slideflow will auto-associate slides to patients.</p>
</div>
<p>Your annotations file should look something like:</p>
<table class="docutils align-default">
<tbody>
<tr class="row-odd"><td><p><em>patient</em></p></td>
<td><p><em>er_status_by_ihc</em></p></td>
<td><p><em>dataset</em></p></td>
<td><p><em>slide</em></p></td>
</tr>
<tr class="row-even"><td><p>TCGA-EL-A23A</p></td>
<td><p>Positive</p></td>
<td><p>train</p></td>
<td><p>TCGA-EL-A3CO-01Z-00-DX1-7BF5F…</p></td>
</tr>
<tr class="row-odd"><td><p>TCGA-EL-A24B</p></td>
<td><p>Negative</p></td>
<td><p>train</p></td>
<td><p>TCGA-EL-A24B-01Z-00-DX1-7BF5F…</p></td>
</tr>
<tr class="row-even"><td><p>TCGA-EL-A25C</p></td>
<td><p>Positive</p></td>
<td><p>train</p></td>
<td><p>TCGA-EL-A25C-01Z-00-DX1-7BF5F…</p></td>
</tr>
<tr class="row-odd"><td><p>TCGA-EH-B31C</p></td>
<td><p>Negative</p></td>
<td><p>eval</p></td>
<td><p>TCGA-EH-B31C-01Z-00-DX1-7BF5F…</p></td>
</tr>
<tr class="row-even"><td><p>…</p></td>
<td><p>…</p></td>
<td><p>…</p></td>
<td><p>…</p></td>
</tr>
</tbody>
</table>
<p>Save this CSV file in your project folder with the name <code class="docutils literal notranslate"><span class="pre">annotations.csv</span></code>.</p>
</section>
<section id="tile-extraction">
<h2>Tile extraction<a class="headerlink" href="#tile-extraction" title="Permalink to this heading">¶</a></h2>
<p>The next step is to extract tiles from our slides. For this example, we will use a 256px x 256px tile size,
at 0.5 µm/pixel (128 um).</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Extract tiles at 256 pixels, 0.5 um/px</span>
<span class="n">P</span><span class="o">.</span><span class="n">extract_tiles</span><span class="p">(</span><span class="n">tile_px</span><span class="o">=</span><span class="mi">256</span><span class="p">,</span> <span class="n">tile_um</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<p>Tile extraction speed is greatly improved when slides are on an SSD or ramdisk; slides can be automatically
buffered to an SSD or ramdisk directory by passing a directory to the argument <code class="docutils literal notranslate"><span class="pre">buffer</span></code>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">P</span><span class="o">.</span><span class="n">extract_tiles</span><span class="p">(</span><span class="mi">256</span><span class="p">,</span> <span class="mi">128</span><span class="p">,</span> <span class="n">buffer</span><span class="o">=</span><span class="s1">'/mnt/ramdisk'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</section>
<section id="training">
<h2>Training<a class="headerlink" href="#training" title="Permalink to this heading">¶</a></h2>
<p>After tiles are extracted, the dataset will be ready for training. We will train with a single set of manually defined
hyperparameters, which we can configure with <a class="reference internal" href="../model_params/#slideflow.ModelParams" title="slideflow.ModelParams"><code class="xref py py-class docutils literal notranslate"><span class="pre">slideflow.ModelParams</span></code></a>. We will use the
<a class="reference external" href="https://arxiv.org/abs/1610.02357">Xception</a> model with a batch size of 32, otherwise keeping defaults.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">hp</span> <span class="o">=</span> <span class="n">sf</span><span class="o">.</span><span class="n">ModelParams</span><span class="p">(</span>
<span class="n">tile_px</span><span class="o">=</span><span class="mi">256</span><span class="p">,</span>
<span class="n">tile_um</span><span class="o">=</span><span class="mi">128</span><span class="p">,</span>
<span class="n">model</span><span class="o">=</span><span class="s1">'xception'</span><span class="p">,</span>
<span class="n">batch_size</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span>
<span class="n">epochs</span><span class="o">=</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
<span class="p">)</span>
</pre></div>
</div>
<p>For training, we will use 5-fold cross-validation on the training dataset. To set up training, invoke the
<a class="reference internal" href="../project/#slideflow.Project.train" title="slideflow.Project.train"><code class="xref py py-meth docutils literal notranslate"><span class="pre">slideflow.Project.train()</span></code></a> function with the outcome of interest, our hyperparameters, and our validation plan.
We will use the <code class="docutils literal notranslate"><span class="pre">filters</span></code> argument to limit our training to the “train” dataset, as well as limit the training
to only include patients with documented ER status (otherwise a blank “” would be marked as a third outcome).</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Train with 5-fold cross-validation</span>
<span class="n">P</span><span class="o">.</span><span class="n">train</span><span class="p">(</span>
<span class="s1">'er_status_by_ihc'</span><span class="p">,</span>
<span class="n">params</span><span class="o">=</span><span class="n">hp</span><span class="p">,</span>
<span class="n">val_k_fold</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span>
<span class="n">filters</span><span class="o">=</span><span class="p">{</span><span class="s1">'dataset'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'train'</span><span class="p">],</span>
<span class="s1">'er_status_by_ihc'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'Positive'</span><span class="p">,</span> <span class="s1">'Negative'</span><span class="p">]}</span>
<span class="p">)</span>
</pre></div>
</div>
<p>After cross validation is complete, we will want to have a model trained across the entire dataset, so we can assess
performance on our held-out evaluation set. To train a model across the entire training dataset without validation,
we will set <code class="docutils literal notranslate"><span class="pre">val_strategy</span></code> to <code class="docutils literal notranslate"><span class="pre">None</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Train across the entire training dataset</span>
<span class="n">P</span><span class="o">.</span><span class="n">train</span><span class="p">(</span>
<span class="s1">'er_status_by_ihc'</span><span class="p">,</span>
<span class="n">params</span><span class="o">=</span><span class="n">hp</span><span class="p">,</span>
<span class="n">val_strategy</span><span class="o">=</span><span class="s1">'none'</span><span class="p">,</span>
<span class="n">filters</span><span class="o">=</span><span class="p">{</span><span class="s1">'dataset'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'train'</span><span class="p">],</span>
<span class="s1">'er_status_by_ihc'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'Positive'</span><span class="p">,</span> <span class="s1">'Negative'</span><span class="p">]}</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Now, it’s time to start our pipeline. To review, our complete script should look like:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">slideflow</span> <span class="k">as</span> <span class="nn">sf</span>
<span class="c1"># Create a new project</span>
<span class="n">P</span> <span class="o">=</span> <span class="n">sf</span><span class="o">.</span><span class="n">create_project</span><span class="p">(</span>
<span class="n">root</span><span class="o">=</span><span class="s1">'/home/er_project'</span><span class="p">,</span>
<span class="n">slides</span><span class="o">=</span><span class="s1">'/path/to/slides'</span>
<span class="p">)</span>
<span class="c1"># Extract tiles at 256 pixels, 0.5 um/px</span>
<span class="n">P</span><span class="o">.</span><span class="n">extract_tiles</span><span class="p">(</span><span class="n">tile_px</span><span class="o">=</span><span class="mi">256</span><span class="p">,</span> <span class="n">tile_um</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>
<span class="n">hp</span> <span class="o">=</span> <span class="n">ModelParams</span><span class="p">(</span>
<span class="n">tile_px</span><span class="o">=</span><span class="mi">256</span><span class="p">,</span>
<span class="n">tile_um</span><span class="o">=</span><span class="mi">128</span><span class="p">,</span>
<span class="n">model</span><span class="o">=</span><span class="s1">'xception'</span><span class="p">,</span>
<span class="n">batch_size</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span>
<span class="n">epochs</span><span class="o">=</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">]</span>
<span class="p">)</span>
<span class="c1"># Train with 5-fold cross-validation</span>
<span class="n">P</span><span class="o">.</span><span class="n">train</span><span class="p">(</span>
<span class="s1">'er_status_by_ihc'</span><span class="p">,</span>
<span class="n">params</span><span class="o">=</span><span class="n">hp</span><span class="p">,</span>
<span class="n">val_k_fold</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span>
<span class="n">filters</span><span class="o">=</span><span class="p">{</span><span class="s1">'dataset'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'train'</span><span class="p">],</span>
<span class="s1">'er_status_by_ihc'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'Positive'</span><span class="p">,</span> <span class="s1">'Negative'</span><span class="p">]}</span>
<span class="p">)</span>
<span class="c1"># Train across the entire training dataset</span>
<span class="n">P</span><span class="o">.</span><span class="n">train</span><span class="p">(</span>
<span class="s1">'er_status_by_ihc'</span><span class="p">,</span>
<span class="n">params</span><span class="o">=</span><span class="n">hp</span><span class="p">,</span>
<span class="n">val_strategy</span><span class="o">=</span><span class="s1">'none'</span><span class="p">,</span>
<span class="n">filters</span><span class="o">=</span><span class="p">{</span><span class="s1">'dataset'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'train'</span><span class="p">],</span>
<span class="s1">'er_status_by_ihc'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'Positive'</span><span class="p">,</span> <span class="s1">'Negative'</span><span class="p">]}</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The final training results should should show an average AUROC of around 0.87, with average AP around 0.83. Tile, slide,
and patient-level receiver operator curves are saved in the model folder, along with precision-recall curves (not shown):</p>
<table class="docutils align-default">
<tbody>
<tr class="row-odd"><td><figure class="align-default">
<img alt="../_images/er_roc_tile.png" src="../_images/er_roc_tile.png" />
</figure>
<p>Tile-level receiver operator curve</p>
</td>
<td><figure class="align-default">
<img alt="../_images/er_roc_patient.png" src="../_images/er_roc_patient.png" />
</figure>
<p>Patient-level receiver operator curve</p>
</td>
</tr>
</tbody>
</table>
</section>
<section id="monitoring-with-tensorboard">
<h2>Monitoring with Tensorboard<a class="headerlink" href="#monitoring-with-tensorboard" title="Permalink to this heading">¶</a></h2>
<p>Tensorboard-formatted training and validation logs are saved the model directory. To monitor training with Tensorboard:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>tensorboard<span class="w"> </span>--logdir<span class="o">=</span>/project_path/models/00001-outcome-HP0
</pre></div>
</div>
<p>Tensorboard can then be accessed by navigating to <code class="docutils literal notranslate"><span class="pre">https://localhost:6006</span></code> in a browser.</p>
</section>
</section>
</article>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="../tutorial2/" class="btn btn-neutral float-right" title="Tutorial 2: Model training (advanced)" accesskey="n" rel="next">Next <img src="../_static/images/chevron-right-orange.svg" class="next-page"></a>
<a href="../studio_module/" class="btn btn-neutral" title="slideflow.studio" accesskey="p" rel="prev"><img src="../_static/images/chevron-right-orange.svg" class="previous-page"> Previous</a>
</div>
<hr>
<div role="contentinfo">
<p>
© Copyright 2023, James M Dolezal.
</p>
</div>
<div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</div>
</footer>
</div>
</div>
<div class="pytorch-content-right" id="pytorch-content-right">
<div class="pytorch-right-menu" id="pytorch-right-menu">
<div class="pytorch-side-scroll" id="pytorch-side-scroll-right">
<ul>
<li><a class="reference internal" href="#">Tutorial 1: Model training (simple)</a><ul>
<li><a class="reference internal" href="#create-a-project">Create a Project</a></li>
<li><a class="reference internal" href="#setting-up-annotations">Setting up annotations</a></li>
<li><a class="reference internal" href="#tile-extraction">Tile extraction</a></li>
<li><a class="reference internal" href="#training">Training</a></li>
<li><a class="reference internal" href="#monitoring-with-tensorboard">Monitoring with Tensorboard</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</section>
</div>
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/sphinx_highlight.js"></script>
<script type="text/javascript" src="../_static/js/vendor/jquery-3.6.3.min.js"></script>
<script type="text/javascript" src="../_static/js/vendor/popper.min.js"></script>
<script type="text/javascript" src="../_static/js/vendor/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
<!-- Begin Footer -->
<!-- End Footer -->
<!-- Begin Mobile Menu -->
<div class="mobile-main-menu">
<div class="container-fluid">
<div class="container">
<div class="mobile-main-menu-header-container">
<a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>
<a class="main-menu-close-button" href="#" data-behavior="close-mobile-menu"></a>
</div>
</div>
</div>
<div class="mobile-main-menu-links-container">
<div class="main-menu">
<ul>
<li>
<a href="https://slideflow.dev">Docs</a>
</li>
<li>
<a href="https://slideflow.dev/tutorial1/">Tutorials</a>
</li>
<li>
<a href="https://github.com/slideflow/slideflow">Github</a>
</li>
</ul>
</div>
</div>
</div>
<!-- End Mobile Menu -->
<script script type="text/javascript">
var collapsedSections = [];
</script>
<script type="text/javascript" src="../_static/js/vendor/anchor.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
mobileMenu.bind();
mobileTOC.bind();
pytorchAnchors.bind();
sideMenus.bind();
scrollToAnchor.bind();
highlightNavigation.bind();
mainMenuDropdown.bind();
filterTags.bind();
// Add class to links that have code blocks, since we cannot create links in code blocks
$("article.pytorch-article a span.pre").each(function(e) {
$(this).closest("a").addClass("has-code");
});
})
</script>
</body>
</html>