---

title: Trainer


keywords: fastai
sidebar: home_sidebar

summary: "The Trainer class inherits `tf.keras.Model` and contains everything a model needs for training. It exposes `learner.cyclic_fit` method which trains the model using **Cyclic Learning rate** discovered by Leslie Smith."
description: "The Trainer class inherits `tf.keras.Model` and contains everything a model needs for training. It exposes `learner.cyclic_fit` method which trains the model using **Cyclic Learning rate** discovered by Leslie Smith."
nb_path: "nbs/03_trainer.ipynb"
---
<!--

#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: nbs/03_trainer.ipynb
# command to build the docs after a change: nbdev_build_docs

-->

<div class="container" id="notebook-container">
        
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="create_classifier" class="doc_header"><code>create_classifier</code><a href="https://github.com/aniketmaurya/chitra/tree/master/chitra/trainer.py#L64" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>create_classifier</code>(<strong><code>base_model_fn</code></strong>:<code>callable</code>, <strong><code>num_classes</code></strong>:<code>int</code>, <strong><code>weights</code></strong>=<em><code>'imagenet'</code></em>, <strong><code>dropout</code></strong>=<em><code>0</code></em>, <strong><code>include_top</code></strong>=<em><code>False</code></em>, <strong><code>name</code></strong>=<em><code>None</code></em>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="create_cnn" class="doc_header"><code>create_cnn</code><a href="https://github.com/aniketmaurya/chitra/tree/master/chitra/trainer.py#L89" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>create_cnn</code>(<strong><code>base_model</code></strong>:<code>Union</code>[<code>str</code>, <code>Model</code>], <strong><code>num_classes</code></strong>:<code>int</code>, <strong><code>drop_out</code></strong>=<em><code>0.5</code></em>, <strong><code>keras_applications</code></strong>:<code>bool</code>=<em><code>True</code></em>, <strong><code>pooling</code></strong>:<code>str</code>=<em><code>'avg'</code></em>, <strong><code>weights</code></strong>:<code>Optional</code>[<code>str</code>]=<em><code>'imagenet'</code></em>, <strong><code>name</code></strong>=<em><code>None</code></em>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="Trainer" class="doc_header"><code>class</code> <code>Trainer</code><a href="https://github.com/aniketmaurya/chitra/tree/master/chitra/trainer.py#L125" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>Trainer</code>(<strong>*<code>args</code></strong>, <strong>**<code>kwargs</code></strong>) :: <code>Model</code></p>
</blockquote>
<p>The Trainer class inherits tf.keras.Model and contains everything a model needs for training.
It exposes trainer.cyclic_fit method which trains the model using Cyclic Learning rate discovered by Leslie Smith.</p>
<p>Arguments:
ds: Dataset object
model: object of type tf.keras.Model
num_classes (int, None): number of classes in the dataset. If None then will auto infer from Dataset</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="InterpretModel" class="doc_header"><code>class</code> <code>InterpretModel</code><a href="https://github.com/aniketmaurya/chitra/tree/master/chitra/trainer.py#L302" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>InterpretModel</code>(<strong><code>gradcam_pp</code></strong>:<code>bool</code>, <strong><code>learner</code></strong>:<a href="/trainer.html#Trainer"><code>Trainer</code></a>, <strong><code>clone</code></strong>:<code>bool</code>=<em><code>False</code></em>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">path</span> <span class="o">=</span> <span class="s1">&#39;/Users/aniket/Pictures/data/train&#39;</span>
</pre></div>

    </div>
</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">glob</span> <span class="kn">import</span> <span class="n">glob</span>
<span class="kn">from</span> <span class="nn">chitra.core</span> <span class="kn">import</span> <span class="n">IMAGENET_LABELS</span>

<span class="k">def</span> <span class="nf">load_files</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">glob</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">path</span><span class="si">}</span><span class="s1">/*/images/*&#39;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">get_label</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">3</span><span class="p">]</span>


<span class="n">ds</span> <span class="o">=</span> <span class="n">Dataset</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>No item present in the image size list
</pre>
</div>
</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">model</span> <span class="o">=</span> <span class="n">create_cnn</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">keras</span><span class="o">.</span><span class="n">applications</span><span class="o">.</span><span class="n">MobileNetV2</span><span class="p">(</span><span class="n">include_top</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">keras_applications</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>num_classes is ignored. returning the passed model as it is.
</pre>
</div>
</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">trainer</span> <span class="o">=</span> <span class="n">Trainer</span><span class="p">(</span><span class="n">ds</span><span class="o">=</span><span class="n">ds</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="n">model</span><span class="p">,</span> <span class="n">num_classes</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>
</pre></div>

    </div>
</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">trainer</span><span class="o">.</span><span class="n">compile2</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;sgd&#39;</span><span class="p">)</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>Model compiled!
</pre>
</div>
</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">interpret</span> <span class="o">=</span> <span class="n">InterpretModel</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="n">trainer</span><span class="p">)</span>

<span class="c1"># path = &#39;/data/aniket/tiny-imagenet/data/tiny-imagenet-200/train/n01641577/images/n01641577_100.JPEG&#39;</span>
<span class="n">path</span>  <span class="o">=</span> <span class="s1">&#39;/Users/aniket/Pictures/data/train/cat/2.jpeg&#39;</span>
<span class="n">image</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">path</span><span class="p">);</span><span class="n">image</span>
</pre></div>

    </div>
</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">interpret</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">auto_resize</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</pre></div>

    </div>
</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">IMAGENET_LABELS</span><span class="p">[</span><span class="mi">285</span><span class="p">]</span>
</pre></div>

    </div>
</div>
</div>

</div>
    {% endraw %}

</div>
 

