<?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>Deven Mistry</title>
<link>https://deven367.github.io/til.html</link>
<atom:link href="https://deven367.github.io/til.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Fri, 01 Nov 2024 00:00:00 GMT</lastBuildDate>
<item>
  <title>Locked SQLite DB</title>
  <dc:creator>Deven Mistry</dc:creator>
  <link>https://deven367.github.io/TIL/sqlite-locked-db.html</link>
  <description><![CDATA[ 









 ]]></description>
  <category>python</category>
  <category>sqlite</category>
  <guid>https://deven367.github.io/TIL/sqlite-locked-db.html</guid>
  <pubDate>Fri, 01 Nov 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Viewing images in the terminal</title>
  <dc:creator>Deven Mistry</dc:creator>
  <link>https://deven367.github.io/TIL/viewing-images-in-terminal.html</link>
  <description><![CDATA[ 






<p>The ability to view images in a terminal is a very handy skill. I recently ran into this issue when I was working on a project which had over a million images on a network drive.</p>
<p>The problem became a complex one to solve as even running a mere <code>ls</code> command was not an option on the network drive.</p>
<p>Firing up a jupyter notebook server everytime to view an image was not a feasible option. And there had to be a better way.</p>
<p>Upon some searching I found some popular options</p>
<ol type="1">
<li><a href="https://github.com/derf/feh">feh</a></li>
<li><a href="https://github.com/stefanhaustein/TerminalImageViewer">TerminalImageViewer</a> (tiv)</li>
<li><a href="https://github.com/atanunq/viu">Viu</a></li>
</ol>
<p>In my use case, the best one which worked the best in my use case was <code>viu</code> because of its support for jpgs.</p>



 ]]></description>
  <category>c</category>
  <category>cpp</category>
  <category>rust</category>
  <category>terminal</category>
  <guid>https://deven367.github.io/TIL/viewing-images-in-terminal.html</guid>
  <pubDate>Thu, 24 Oct 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>nohup and &amp;</title>
  <dc:creator>Deven Mistry</dc:creator>
  <link>https://deven367.github.io/TIL/nohup.html</link>
  <description><![CDATA[ 






<p>Yesterday, I ran into a problem where I wanted to run a shell script in the background on a linux machine. I knew that</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bash</span> foo.sh <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span></span></code></pre></div></div>
<p>would solve the problem for me, however I was working on a remote machine, which meant that closing the <code>ssh</code> connection would terminate the execution of the command.</p>
<p>Enter <code>nohup</code>. This is a wonderful bash utility which allows you to run your script in the background on a remote machine even when you close the connection.</p>
<p><code>nohup</code> is just short for <code>no hang up</code>. You can use this command in multiple ways. There’s a wonderful article on DigitalOcean<sup>1</sup> explaining this command. I’ll just list a few of them here.</p>
<p>By default, <code>nohup</code> will save the output of the execution in <code>nohup.out</code>. You can change that with the redirect output command.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb2-1"></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nohup</span> ./foo.sh</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nohup</span> ./foo.sh <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> output.txt     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># write (redirect) output to output.txt</span></span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nohup</span> ./foo.sh <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> output.txt   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># write (redirect) output to output.txt and run the script in the background</span></span></code></pre></div></div>
<p>Here’s another good reference from StackOverFlow<sup>2</sup></p>




<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p><a href="https://www.digitalocean.com/community/tutorials/nohup-command-in-linux">DigitalOcean</a>↩︎</p></li>
<li id="fn2"><p><a href="https://stackoverflow.com/questions/44222883/run-a-shell-script-and-immediately-background-it-however-keep-the-ability-to-in">StackOverFlow</a>↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>linux</category>
  <category>bash</category>
  <guid>https://deven367.github.io/TIL/nohup.html</guid>
  <pubDate>Wed, 20 Mar 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>isinstance vs type and == vs is</title>
  <dc:creator>Deven Mistry</dc:creator>
  <link>https://deven367.github.io/TIL/isinstance-vs-type.html</link>
  <description><![CDATA[ 






<p>There are multiple ways in Python to achieve a single task and at times you can get away with implementing things the wrong way and getting the correct answer.</p>
<p>This TIL has come into existence as I struggled with a issue that I was trying to fix at work.</p>
<section id="isinstance-vs-type" class="level2">
<h2 class="anchored" data-anchor-id="isinstance-vs-type"><code>isinstance</code> vs <code>type</code></h2>
<p>We’ve all used the <code>type</code> and the <code>isinstance</code> method at some point in our functions.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode py code-with-copy"><code class="sourceCode python"><span id="cb1-1">s <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;">'foo'</span></span>
<span id="cb1-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>(s) <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;">str</span>:</span>
<span id="cb1-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'this variable is of string type'</span>)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(s, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb1-6">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'this variable is of string type'</span>)</span></code></pre></div></div>
<p>If you were to execute this code, you would get the same output.</p>
<p>But what’s the difference between them?</p>
<p><code>instance</code> can check types of even derived classes (meaning, it is intended to be used in cases involving inheritence) whereas <code>type</code> is not.</p>
<p>TLDR; <sup>1</sup></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode py code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Person: <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Student(Person): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb2-4"></span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(Person(), Person)        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># returns True</span></span>
<span id="cb2-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>(Student()) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Student          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># returns True</span></span>
<span id="cb2-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(Student(), Person)       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># returns True</span></span>
<span id="cb2-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>(Student()) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Person           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># returns False, and this probably won't be what you want.</span></span></code></pre></div></div>
<p>There’s a similar confusion between <code>is</code> and <code>==</code>.</p>
</section>
<section id="is-vs" class="level2">
<h2 class="anchored" data-anchor-id="is-vs"><code>is</code> vs <code>==</code></h2>
<p><code>is</code> looks for the same object (in memory), whereas <code>==</code> looks for the values referred by the variables.</p>
<p>TLDR; <sup>2</sup></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode py code-with-copy"><code class="sourceCode python"><span id="cb3-1">n <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;">5</span></span>
<span id="cb3-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> n <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;">5</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Yep!'</span>)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># prints Yep!</span></span>
<span id="cb3-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Yay!'</span>)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># prints Yep!</span></span>
<span id="cb3-4"></span>
<span id="cb3-5">L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb3-6">L.append(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> L <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 class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Yay!'</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># prints Yep!</span></span>
<span id="cb3-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> L <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Yay!'</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># prints nothing</span></span></code></pre></div></div>
<p>H/T to <a href="https://github.com/muellerzr">Zach Mueller</a> for helping me understand this</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p><a href="https://stackoverflow.com/questions/1549801/what-are-the-differences-between-type-and-isinstance">differences between type() and isinstance()?</a>↩︎</p></li>
<li id="fn2"><p><a href="https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is">difference between “==” and “is”?</a>↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>python</category>
  <guid>https://deven367.github.io/TIL/isinstance-vs-type.html</guid>
  <pubDate>Fri, 18 Nov 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Learning from pkl files</title>
  <dc:creator>Deven Mistry</dc:creator>
  <link>https://deven367.github.io/TIL/learning-from-pkl-files.html</link>
  <description><![CDATA[ 






<p>When we train a model using <code>fastai</code> and export it using <code>learn.export</code>, you need to re-declare the functions that you had used to train the model.</p>
<p>Let me show you what I mean by that,</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> fastai.vision.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb1-2">set_seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb1-3">path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> untar_data(URLs.PETS)<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></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> label_func(x): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].isupper()</span>
<span id="cb1-6"></span>
<span id="cb1-7">dls <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ImageDataLoaders.from_name_func(</span>
<span id="cb1-8">    path, get_image_files(path), valid_pct<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.2</span>,</span>
<span id="cb1-9">    label_func<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>label_func, item_tfms<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Resize(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">224</span>))</span>
<span id="cb1-10"></span>
<span id="cb1-11">learn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vision_learner(dls, resnet34, metrics<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>error_rate)</span>
<span id="cb1-12">learn.fine_tune(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-13">learn.export(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'saved-model.pkl'</span>)</span></code></pre></div></div>
<p>Here, we are training a simple model for image classification and then exporting the saved model.</p>
<p>Now, if you were to use this model in a different jupyter notebook or a py file using the <code>load_learner</code> function.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode py code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> fastai.vision.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb2-2">learn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> load_learner(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'saved-model.pkl'</span>)</span></code></pre></div></div>
<p>You would get an error something like this…</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode py code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---------------------------------------------------------------------------</span></span>
<span id="cb3-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">AttributeError</span>                            Traceback (most recent call last)</span>
<span id="cb3-3"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>ipython<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;">input</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;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>c010bc50794d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>module<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-4"><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;">1</span> learn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> load_learner(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'saved-model.pkl'</span>)</span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> frames</span>
<span id="cb3-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>usr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>local<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>lib<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>python3<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>packages<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>torch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>serialization.py <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> find_class(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, mod_name, name)</span>
<span id="cb3-8">   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1040</span>                     <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb3-9">   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1041</span>             mod_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> load_module_mapping.get(mod_name, mod_name)</span>
<span id="cb3-10"><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;">1042</span>             <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().find_class(mod_name, name)</span>
<span id="cb3-11">   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1043</span></span>
<span id="cb3-12">   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1044</span>     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the data (which may in turn use `persistent_load` to load tensors)</span></span>
<span id="cb3-13"></span>
<span id="cb3-14"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">AttributeError</span>: Custom classes <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> functions exported <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> your `Learner` <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> available <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> namespace.\Re<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>declare<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> before loading:</span>
<span id="cb3-15">    Can<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'t get attribute '</span>label_func<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' on &lt;module '</span>__main__<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'&gt;</span></span></code></pre></div></div>
<p>This error is essentially saying that, before you load your model, the script (py file) or the notebook is looking for the function <code>label_func</code>.</p>
<p>Previously, I would just copy that whole function again from the previous file and paste it in my new file, something like this.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode py code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> fastai.vision.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb4-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> label_func(x): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].isupper()</span>
<span id="cb4-3"></span>
<span id="cb4-4">learn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> load_learner(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'saved-model.pkl'</span>)</span></code></pre></div></div>
<p>But, as it turns out, the <code>load_learner</code> is just looking for a reference to the <code>label_func</code> and not it’s entire definition. So, in theory, you could have your code look something like…</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode py code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> fastai.vision.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## notice the empty function declaration</span></span>
<span id="cb5-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> label_func(x): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb5-5"></span>
<span id="cb5-6">learn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> load_learner(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'saved-model.pkl'</span>)</span></code></pre></div></div>
<p>With this declaration, your inference would work just fine and you won’t get any problems.</p>
<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>Use this method only, when you wish to do <code>inference</code> with your model. <br> If you wish to re-train the model, you would need a new <code>dataloader</code>, <code>label_func</code> and the whole nine-yards.</p>
</div>
</div>
<p>I hope this was helpful. Happy Learning. Cheers!</p>



 ]]></description>
  <category>pkl</category>
  <category>pytorch</category>
  <category>fastai</category>
  <guid>https://deven367.github.io/TIL/learning-from-pkl-files.html</guid>
  <pubDate>Tue, 15 Nov 2022 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
