Skip to content

Commit

Permalink
yes!
Browse files Browse the repository at this point in the history
  • Loading branch information
yreddy31 committed Mar 6, 2021
1 parent 3f44286 commit 1862375
Show file tree
Hide file tree
Showing 12 changed files with 1,237 additions and 28 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Pytorch utilities for simple pytorch needs
# Utilities for simple needs



```python
## Logging and plotting metrics
```python
from torch_snippets import Report
Expand All @@ -25,7 +28,7 @@ for epoch in range(n_epochs):
log.report_avgs(epoch+1) # persist the report

```
![](assets/demo.gif)
![](../assets/demo.gif)
* Auto calculates time remaining
* No need to preinitialize empty lists
* Automatically stores metrics as collection of key words
Expand All @@ -39,7 +42,7 @@ for epoch in range(n_epochs):

* Auto calculates average of all metrics in an epoch
* Plot entire training history with one command
![](assets/avgs0.png)
![](../assets/avgs0.png)


* selectively plot logged metrics using regex
Expand Down Expand Up @@ -74,3 +77,5 @@ and many more...
import pytorch_snippets
dir(pytorch_snippets)
```

```
3 changes: 3 additions & 0 deletions docs/_data/sidebars/home_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ entries:
- output: web,pdf
title: Report
url: report.html
- output: web,pdf
title: Show
url: show.html
output: web
title: torch_snippets
output: web
Expand Down
8 changes: 4 additions & 4 deletions docs/charts.html

Large diffs are not rendered by default.

76 changes: 74 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

title: Title
title: Utilities for simple needs


keywords: fastai
Expand Down Expand Up @@ -36,7 +36,79 @@

<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">torch_snippets</span> <span class="kn">import</span> <span class="o">*</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="err">```</span><span class="n">python</span>
<span class="kn">from</span> <span class="nn">torch_snippets</span> <span class="kn">import</span> <span class="n">Report</span>

<span class="n">n_epochs</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">Report</span><span class="p">(</span><span class="n">n_epochs</span><span class="p">)</span>

<span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_epochs</span><span class="p">):</span>
<span class="c1"># No need to --&gt; train_epoch_losses, train_epoch_accuracies = [], []</span>
<span class="n">N</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">trn_dl</span><span class="p">)</span>
<span class="k">for</span> <span class="n">ix</span><span class="p">,</span> <span class="n">batch</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">iter</span><span class="p">(</span><span class="n">trn_dl</span><span class="p">)):</span>
<span class="o">...</span>
<span class="n">pos</span> <span class="o">=</span> <span class="p">(</span><span class="n">epoch</span> <span class="o">+</span> <span class="p">(</span><span class="n">ix</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">N</span><span class="p">)</span> <span class="c1"># a float between 0 - n_epochs</span>
<span class="c1"># give any number of kwargs that need to be reported and stored.</span>
<span class="c1"># args should be float</span>
<span class="n">log</span><span class="o">.</span><span class="n">record</span><span class="p">(</span><span class="n">pos</span><span class="o">=</span><span class="n">pos</span><span class="p">,</span> <span class="n">train_acc</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">is_correct</span><span class="p">),</span> <span class="n">train_loss</span><span class="o">=</span><span class="n">batch_loss</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39;</span><span class="se">\r</span><span class="s1">&#39;</span><span class="p">)</span> <span class="c1"># impersistent log</span>

<span class="n">N</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">val_dl</span><span class="p">)</span>
<span class="k">for</span> <span class="n">ix</span><span class="p">,</span> <span class="n">batch</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">iter</span><span class="p">(</span><span class="n">val_dl</span><span class="p">)):</span>
<span class="o">...</span>
<span class="n">pos</span> <span class="o">=</span> <span class="p">(</span><span class="n">epoch</span> <span class="o">+</span> <span class="p">(</span><span class="n">ix</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">N</span><span class="p">)</span> <span class="c1"># a float between 0 - n_epochs</span>
<span class="n">log</span><span class="o">.</span><span class="n">record</span><span class="p">(</span><span class="n">pos</span><span class="o">=</span><span class="n">pos</span><span class="p">,</span> <span class="n">val_loss</span><span class="o">=</span><span class="n">batch_loss</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39;</span><span class="se">\r</span><span class="s1">&#39;</span><span class="p">)</span> <span class="c1"># impersistent log</span>
<span class="n">log</span><span class="o">.</span><span class="n">report_avgs</span><span class="p">(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># persist the report</span>

<span class="err">```</span>
<span class="o">![](</span>../assets/demo.gif<span class="o">)</span>
<span class="o">*</span> <span class="n">Auto</span> <span class="n">calculates</span> <span class="n">time</span> <span class="n">remaining</span>
<span class="o">*</span> <span class="n">No</span> <span class="n">need</span> <span class="n">to</span> <span class="n">preinitialize</span> <span class="n">empty</span> <span class="n">lists</span>
<span class="o">*</span> <span class="n">Automatically</span> <span class="n">stores</span> <span class="n">metrics</span> <span class="k">as</span> <span class="n">collection</span> <span class="n">of</span> <span class="n">key</span> <span class="n">words</span>
<span class="o">*</span> <span class="n">Persistent</span> <span class="n">vs</span> <span class="n">Transient</span> <span class="n">logging</span> <span class="p">(</span><span class="n">use</span> <span class="err">`</span><span class="n">end</span><span class="o">=</span><span class="s1">&#39;</span><span class="se">\r</span><span class="s1">&#39;</span><span class="err">`</span><span class="p">)</span>
<span class="err">```</span><span class="n">python</span>
<span class="o">&gt;&gt;&gt;</span> <span class="nb">print</span><span class="p">(</span><span class="n">log</span><span class="o">.</span><span class="n">logged</span><span class="p">)</span> <span class="c1"># get logged metric names</span>
<span class="c1"># [&#39;train_loss&#39;, &#39;train_acc&#39;, &#39;val_loss&#39;, &#39;val_acc&#39;]</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">log</span><span class="o">.</span><span class="n">plot</span><span class="p">()</span> <span class="c1"># plot all the logged metrics</span>
<span class="err">```</span>


<span class="o">*</span> <span class="n">Auto</span> <span class="n">calculates</span> <span class="n">average</span> <span class="n">of</span> <span class="nb">all</span> <span class="n">metrics</span> <span class="ow">in</span> <span class="n">an</span> <span class="n">epoch</span>
<span class="o">*</span> <span class="n">Plot</span> <span class="n">entire</span> <span class="n">training</span> <span class="n">history</span> <span class="k">with</span> <span class="n">one</span> <span class="n">command</span>
<span class="o">![](</span>../assets/avgs0.png<span class="o">)</span>


<span class="o">*</span> <span class="n">selectively</span> <span class="n">plot</span> <span class="n">logged</span> <span class="n">metrics</span> <span class="n">using</span> <span class="n">regex</span>
<span class="err">```</span><span class="n">python</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">log</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="s1">&#39;*_loss&#39;</span><span class="p">)</span>
<span class="c1"># or you can list out the metrics that need plotting</span>
<span class="c1"># &gt;&gt;&gt; log.plot([&#39;train_acc&#39;,&#39;val_acc&#39;])</span>
<span class="err">```</span>
<span class="o">![](</span>assets/avgs1.png<span class="o">)</span>

<span class="c1">## Auxiliary Functions</span>
<span class="n">There</span> <span class="n">are</span> <span class="n">simple</span> <span class="n">functions</span> <span class="n">that</span> <span class="n">are</span> <span class="n">overloaded</span> <span class="n">to</span> <span class="n">take</span> <span class="n">inputs</span> <span class="ow">and</span> <span class="n">perform</span> <span class="n">repetitive</span> <span class="n">tasks</span> <span class="n">that</span> <span class="n">usually</span> <span class="n">take</span> <span class="n">a</span> <span class="n">few</span> <span class="n">lines</span> <span class="n">to</span> <span class="n">write</span>
<span class="c1">#### Images</span>
<span class="err">`</span><span class="n">show</span><span class="err">`</span><span class="p">,</span> <span class="err">`</span><span class="n">inspect</span><span class="err">`</span><span class="p">,</span> <span class="err">`</span><span class="n">Glob</span><span class="err">`</span><span class="p">,</span> <span class="err">`</span><span class="n">read</span><span class="err">`</span>
<span class="c1">#### FilePaths</span>
<span class="err">`</span><span class="n">stem</span><span class="err">`</span><span class="p">,</span> <span class="err">`</span><span class="n">Glob</span><span class="err">`</span><span class="p">,</span> <span class="err">`</span><span class="n">parent</span><span class="err">`</span><span class="p">,</span> <span class="err">`</span><span class="n">name</span><span class="err">`</span>
<span class="c1">#### Dump and load python objects</span>
<span class="err">`</span><span class="n">loaddill</span><span class="err">`</span><span class="p">,</span><span class="err">`</span><span class="n">dumpdill</span><span class="err">`</span>
<span class="c1">#### Misc </span>
<span class="err">`</span><span class="n">Tqdm</span><span class="err">`</span><span class="p">,</span> <span class="err">`</span><span class="n">Timer</span><span class="err">`</span><span class="p">,</span> <span class="err">`</span><span class="n">randint</span><span class="err">`</span><span class="p">,</span> <span class="err">`</span><span class="n">unique</span><span class="err">`</span><span class="p">,</span> <span class="err">`</span><span class="n">diff</span><span class="err">`</span>
<span class="c1">#### Pytorch Modules</span>
<span class="err">`</span><span class="n">Reshape</span><span class="err">`</span> <span class="ow">and</span> <span class="err">`</span><span class="n">Permute</span><span class="err">`</span> <span class="p">(</span><span class="err">`</span><span class="n">nn</span><span class="o">.</span><span class="n">Modules</span><span class="err">`</span><span class="p">)</span>


<span class="ow">and</span> <span class="n">many</span> <span class="n">more</span><span class="o">...</span>

<span class="c1">## Install</span>
<span class="err">`</span><span class="n">pip</span> <span class="n">install</span> <span class="n">torch_snippets</span><span class="err">`</span>

<span class="c1">## Usage</span>
<span class="err">```</span><span class="n">python</span>
<span class="kn">import</span> <span class="nn">pytorch_snippets</span>
<span class="nb">dir</span><span class="p">(</span><span class="n">pytorch_snippets</span><span class="p">)</span>
<span class="err">```</span>
</pre></div>

</div>
Expand Down
557 changes: 557 additions & 0 deletions docs/show.html

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion docs/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"torch_snippets": {
"Overview": "/",
"Charts": "charts.html",
"Report": "report.html"
"Report": "report.html",
"Show": "show.html"
}
}
14 changes: 7 additions & 7 deletions nbs/charts.ipynb

Large diffs are not rendered by default.

83 changes: 78 additions & 5 deletions nbs/index.ipynb
Original file line number Diff line number Diff line change
@@ -1,20 +1,93 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"from torch_snippets import *"
"# Utilities for simple needs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"## Logging and plotting metrics\n",
"```python\n",
"from torch_snippets import Report\n",
"\n",
"n_epochs = 5\n",
"log = Report(n_epochs)\n",
"\n",
"for epoch in range(n_epochs):\n",
" # No need to --> train_epoch_losses, train_epoch_accuracies = [], []\n",
" N = len(trn_dl)\n",
" for ix, batch in enumerate(iter(trn_dl)):\n",
" ...\n",
" pos = (epoch + (ix+1)/N) # a float between 0 - n_epochs\n",
" # give any number of kwargs that need to be reported and stored.\n",
" # args should be float\n",
" log.record(pos=pos, train_acc=np.mean(is_correct), train_loss=batch_loss, end='\\r') # impersistent log\n",
"\n",
" N = len(val_dl)\n",
" for ix, batch in enumerate(iter(val_dl)):\n",
" ...\n",
" pos = (epoch + (ix+1)/N) # a float between 0 - n_epochs\n",
" log.record(pos=pos, val_loss=batch_loss, end='\\r') # impersistent log\n",
" log.report_avgs(epoch+1) # persist the report\n",
"\n",
"```\n",
"![](../assets/demo.gif)\n",
"* Auto calculates time remaining\n",
"* No need to preinitialize empty lists\n",
"* Automatically stores metrics as collection of key words\n",
"* Persistent vs Transient logging (use `end='\\r'`) \n",
"```python\n",
">>> print(log.logged) # get logged metric names\n",
"# ['train_loss', 'train_acc', 'val_loss', 'val_acc']\n",
">>> log.plot() # plot all the logged metrics\n",
"```\n",
"\n",
"\n",
"* Auto calculates average of all metrics in an epoch\n",
"* Plot entire training history with one command \n",
"![](../assets/avgs0.png)\n",
"\n",
"\n",
"* selectively plot logged metrics using regex\n",
"```python\n",
">>> log.plot('*_loss')\n",
"# or you can list out the metrics that need plotting\n",
"# >>> log.plot(['train_acc','val_acc'])\n",
"```\n",
"![](assets/avgs1.png)\n",
"\n",
"## Auxiliary Functions\n",
"There are simple functions that are overloaded to take inputs and perform repetitive tasks that usually take a few lines to write\n",
"#### Images\n",
"`show`, `inspect`, `Glob`, `read`\n",
"#### FilePaths\n",
"`stem`, `Glob`, `parent`, `name`\n",
"#### Dump and load python objects\n",
"`loaddill`,`dumpdill`\n",
"#### Misc \n",
"`Tqdm`, `Timer`, `randint`, `unique`, `diff`\n",
"#### Pytorch Modules\n",
"`Reshape` and `Permute` (`nn.Modules`)\n",
"\n",
"\n",
"and many more... \n",
" \n",
"## Install\n",
"`pip install torch_snippets`\n",
"\n",
"## Usage\n",
"```python\n",
"import pytorch_snippets\n",
"dir(pytorch_snippets)\n",
"```\n"
]
}
],
"metadata": {
Expand Down
487 changes: 487 additions & 0 deletions nbs/show.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ author = Yeshwanth Reddy
author_email = [email protected]
copyright = sizhky
branch = master
version = 0.321
version = 0.322
min_python = 3.6
audience = Developers
language = English
Expand Down
13 changes: 9 additions & 4 deletions torch_snippets.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
Metadata-Version: 2.1
Name: torch-snippets
Version: 0.320
Version: 0.322
Summary: One line functions for common tasks
Home-page: https://github.com/sizhky/torch_snippets/tree/master/
Author: Yeshwanth Reddy
Author-email: [email protected]
License: Apache Software License 2.0
Description: # Pytorch utilities for simple pytorch needs
Description: # Utilities for simple needs



```python
## Logging and plotting metrics
```python
from torch_snippets import Report
Expand All @@ -33,7 +36,7 @@ Description: # Pytorch utilities for simple pytorch needs
log.report_avgs(epoch+1) # persist the report

```
![](assets/demo.gif)
![](../assets/demo.gif)
* Auto calculates time remaining
* No need to preinitialize empty lists
* Automatically stores metrics as collection of key words
Expand All @@ -47,7 +50,7 @@ Description: # Pytorch utilities for simple pytorch needs

* Auto calculates average of all metrics in an epoch
* Plot entire training history with one command
![](assets/avgs0.png)
![](../assets/avgs0.png)


* selectively plot logged metrics using regex
Expand Down Expand Up @@ -83,6 +86,8 @@ Description: # Pytorch utilities for simple pytorch needs
dir(pytorch_snippets)
```

```

Keywords: snippets,torch
Platform: UNKNOWN
Classifier: Development Status :: 3 - Alpha
Expand Down
8 changes: 7 additions & 1 deletion torch_snippets/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
'readlines','writelines',
'zip_files','unzip_file',
'BB','bbfy','xywh2xyXY','df2bbs',
'Info','Warn','Debug','Excep'
'Info','Warn','Debug','Excep',
'display'
]

import glob, numpy as np, pandas as pd, tqdm, os, sys, re
from IPython.display import display
import PIL
from PIL import Image
try:
Expand Down Expand Up @@ -274,6 +276,10 @@ def show(img=None, ax=None, title=None, sz=None, bbs=None, confs=None,
if isinstance(img, torch.Tensor): img = img.cpu().detach().numpy().copy()
if isinstance(img, PIL.Image.Image): img = np.array(img)
except: ...
if not isinstance(img, np.ndarray):
display(img)
return

if len(img.shape) == 3 and len(img) == 3:
# this is likely a torch tensor
img = img.transpose(1,2,0)
Expand Down

0 comments on commit 1862375

Please sign in to comment.