├── docs
├── .gitignore
├── _data
│ ├── topnav.yml
│ └── sidebars
│ │ └── home_sidebar.yml
├── sidebar.json
├── sitemap.xml
├── feed.xml
├── _config.yml
├── error.fastai.html
├── fastai.learner.html
├── index.html
└── fastai.data.html
├── fastdebug
├── __init__.py
├── fastai
│ ├── __init__.py
│ ├── transform.py
│ ├── dataloader.py
│ ├── datasets.py
│ └── learner.py
├── _nbdev.py
└── torch.py
├── MANIFEST.in
├── CHANGELOG.md
├── .devcontainer.json
├── Makefile
├── docker-compose.yml
├── .github
└── workflows
│ └── main.yml
├── .gitignore
├── CONTRIBUTING.md
├── settings.ini
├── setup.py
├── README.md
├── index.ipynb
├── LICENSE
├── 02_fastai.learner.ipynb
├── 00_torch.ipynb
└── 01_fastai.dataloader.ipynb
/docs/.gitignore:
--------------------------------------------------------------------------------
1 | _site/
2 |
--------------------------------------------------------------------------------
/fastdebug/__init__.py:
--------------------------------------------------------------------------------
1 | __version__ = "0.1.3"
2 |
3 | from fastdebug.torch import *
4 | from fastdebug.fastai import *
--------------------------------------------------------------------------------
/fastdebug/fastai/__init__.py:
--------------------------------------------------------------------------------
1 | from .dataloader import *
2 | from .learner import *
3 | from .transform import *
4 | from .datasets import *
--------------------------------------------------------------------------------
/MANIFEST.in:
--------------------------------------------------------------------------------
1 | include settings.ini
2 | include LICENSE
3 | include CONTRIBUTING.md
4 | include README.md
5 | recursive-exclude * __pycache__
6 |
--------------------------------------------------------------------------------
/docs/_data/topnav.yml:
--------------------------------------------------------------------------------
1 | topnav:
2 | - title: Topnav
3 | items:
4 | - title: github
5 | external_url: https://github.com/muellerzr/fastdebug/tree/master/
6 |
7 | #Topnav dropdowns
8 | topnav_dropdowns:
9 | - title: Topnav dropdowns
10 | folders:
--------------------------------------------------------------------------------
/docs/sidebar.json:
--------------------------------------------------------------------------------
1 | {
2 | "fastdebug": {
3 | "Overview": "/",
4 | "Pytorch Errors": "torch.html",
5 | "DataLoader Errors": "fastai.dataloader.html",
6 | "Learner Errors": "fastai.learner.html",
7 | "Transform Errors": "fastai.transform.html",
8 | "Dataset Errors": "fastai.datasets.html"
9 | }
10 | }
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Release Notes
2 |
3 | ## 0.0.5
4 | ### New Features:
5 | * DataLoader and Dataset errors, essentially FC
6 |
7 | ## 0.0.4
8 | ### New Features:
9 | * `layer_error` now supports multi-input models
10 |
11 | ## 0.0.3
12 | ### New Features:
13 | * Use Hooks to grab the problem layer when using `layer_error` rather than trying to guess. This way we can grab the exact one
--------------------------------------------------------------------------------
/.devcontainer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "nbdev_template-codespaces",
3 | "dockerComposeFile": "docker-compose.yml",
4 | "service": "watcher",
5 | "settings": {"terminal.integrated.shell.linux": "/bin/bash"},
6 | "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
7 | "forwardPorts": [4000, 8080],
8 | "appPort": [4000, 8080],
9 | "extensions": ["ms-python.python",
10 | "ms-azuretools.vscode-docker"],
11 | "runServices": ["notebook", "jekyll", "watcher"],
12 | "postStartCommand": "pip install -e ."
13 | }
14 |
--------------------------------------------------------------------------------
/docs/sitemap.xml:
--------------------------------------------------------------------------------
1 | ---
2 | layout: none
3 | search: exclude
4 | ---
5 |
6 |
7 | decodes to undo transform"
38 | try:
39 | return self._call('decodes', x, **kwargs)
40 | except Exception as e:
41 | transform_error(e, _get_name(self), 'decodes')
42 |
43 | # Cell
44 | @patch
45 | @delegates(DataLoader.new)
46 | def new(self:TfmdDL, dataset=None, cls=None, **kwargs):
47 | "Create a new version of self with a few changed attributes"
48 | res = super(TfmdDL, self).new(dataset, cls, do_setup=False, **kwargs)
49 | if not hasattr(self, '_n_inp') or not hasattr(self, '_types'):
50 | try:
51 | self._one_pass()
52 | res._n_inp,res._types = self._n_inp,self._types
53 | except Exception as e:
54 | print("Could not do one pass in your DataLoader, there is something wrong in it. Please see the stack trace below:")
55 | raise e
56 | else: res._n_inp,res._types = self._n_inp,self._types
57 | return res
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.bak
2 | .gitattributes
3 | .last_checked
4 | .gitconfig
5 | *.bak
6 | *.log
7 | *~
8 | ~*
9 | _tmp*
10 | tmp*
11 | tags
12 |
13 | # Byte-compiled / optimized / DLL files
14 | __pycache__/
15 | *.py[cod]
16 | *$py.class
17 |
18 | # C extensions
19 | *.so
20 |
21 | # Distribution / packaging
22 | .Python
23 | env/
24 | build/
25 | develop-eggs/
26 | dist/
27 | downloads/
28 | eggs/
29 | .eggs/
30 | lib/
31 | lib64/
32 | parts/
33 | sdist/
34 | var/
35 | wheels/
36 | *.egg-info/
37 | .installed.cfg
38 | *.egg
39 |
40 | # PyInstaller
41 | # Usually these files are written by a python script from a template
42 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
43 | *.manifest
44 | *.spec
45 |
46 | # Installer logs
47 | pip-log.txt
48 | pip-delete-this-directory.txt
49 |
50 | # Unit test / coverage reports
51 | htmlcov/
52 | .tox/
53 | .coverage
54 | .coverage.*
55 | .cache
56 | nosetests.xml
57 | coverage.xml
58 | *.cover
59 | .hypothesis/
60 |
61 | # Translations
62 | *.mo
63 | *.pot
64 |
65 | # Django stuff:
66 | *.log
67 | local_settings.py
68 |
69 | # Flask stuff:
70 | instance/
71 | .webassets-cache
72 |
73 | # Scrapy stuff:
74 | .scrapy
75 |
76 | # Sphinx documentation
77 | docs/_build/
78 |
79 | # PyBuilder
80 | target/
81 |
82 | # Jupyter Notebook
83 | .ipynb_checkpoints
84 |
85 | # pyenv
86 | .python-version
87 |
88 | # celery beat schedule file
89 | celerybeat-schedule
90 |
91 | # SageMath parsed files
92 | *.sage.py
93 |
94 | # dotenv
95 | .env
96 |
97 | # virtualenv
98 | .venv
99 | venv/
100 | ENV/
101 |
102 | # Spyder project settings
103 | .spyderproject
104 | .spyproject
105 |
106 | # Rope project settings
107 | .ropeproject
108 |
109 | # mkdocs documentation
110 | /site
111 |
112 | # mypy
113 | .mypy_cache/
114 |
115 | .vscode
116 | *.swp
117 |
118 | # osx generated files
119 | .DS_Store
120 | .DS_Store?
121 | .Trashes
122 | ehthumbs.db
123 | Thumbs.db
124 | .idea
125 |
126 | # pytest
127 | .pytest_cache
128 |
129 | # tools/trust-doc-nbs
130 | docs_src/.last_checked
131 |
132 | # symlinks to fastai
133 | docs_src/fastai
134 | tools/fastai
135 |
136 | # link checker
137 | checklink/cookies.txt
138 |
139 | # .gitconfig is now autogenerated
140 | .gitconfig
141 |
142 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # How to contribute
2 |
3 | ## How to get started
4 |
5 | Before anything else, please install the git hooks that run automatic scripts during each commit and merge to strip the notebooks of superfluous metadata (and avoid merge conflicts). After cloning the repository, run the following command inside it:
6 | ```
7 | nbdev_install_git_hooks
8 | ```
9 |
10 | ## Did you find a bug?
11 |
12 | * Ensure the bug was not already reported by searching on GitHub under Issues.
13 | * If you're unable to find an open issue addressing the problem, open a new one. Be sure to include a title and clear description, as much relevant information as possible, and a code sample or an executable test case demonstrating the expected behavior that is not occurring.
14 | * Be sure to add the complete error messages.
15 |
16 | #### Did you write a patch that fixes a bug?
17 |
18 | * Open a new GitHub pull request with the patch.
19 | * Ensure that your PR includes a test that fails without your patch, and pass with it.
20 | * Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.
21 |
22 | ## PR submission guidelines
23 |
24 | * Keep each PR focused. While it's more convenient, do not combine several unrelated fixes together. Create as many branches as needing to keep each PR focused.
25 | * Do not mix style changes/fixes with "functional" changes. It's very difficult to review such PRs and it most likely get rejected.
26 | * Do not add/remove vertical whitespace. Preserve the original style of the file you edit as much as you can.
27 | * Do not turn an already submitted PR into your development playground. If after you submitted PR, you discovered that more work is needed - close the PR, do the required work and then submit a new PR. Otherwise each of your commits requires attention from maintainers of the project.
28 | * If, however, you submitted a PR and received a request for changes, you should proceed with commits inside that PR, so that the maintainer can see the incremental fixes and won't need to review the whole PR again. In the exception case where you realize it'll take many many commits to complete the requests, then it's probably best to close the PR, do the work and then submit it again. Use common sense where you'd choose one way over another.
29 |
30 | ## Do you want to contribute to the documentation?
31 |
32 | * Docs are automatically created from the notebooks in the nbs folder.
33 |
34 |
--------------------------------------------------------------------------------
/fastdebug/fastai/dataloader.py:
--------------------------------------------------------------------------------
1 | # AUTOGENERATED! DO NOT EDIT! File to edit: 01_fastai.dataloader.ipynb (unless otherwise specified).
2 |
3 | __all__ = ['collate_error']
4 |
5 | # Cell
6 | import inflect
7 | from fastcore.basics import patch
8 | from fastai.data.core import TfmdDL
9 | from fastai.data.load import DataLoader, fa_collate, fa_convert
10 |
11 | # Cell
12 | def collate_error(e:Exception, batch):
13 | """
14 | Raises an explicit error when the batch could not collate, stating
15 | what items in the batch are different sizes and their types
16 | """
17 | p = inflect.engine()
18 | err = f'Error when trying to collate the data into batches with fa_collate, '
19 | err += 'at least two tensors in the batch are not the same size.\n\n'
20 | # we need to iterate through the entire batch and find a mismatch
21 | length = len(batch[0])
22 | for idx in range(length): # for each type in the batch
23 | for i, item in enumerate(batch):
24 | if i == 0:
25 | shape_a = item[idx].shape
26 | type_a = item[idx].__class__.__name__
27 | elif item[idx].shape != shape_a:
28 | shape_b = item[idx].shape
29 | if shape_a != shape_b:
30 | err += f'Mismatch found within the {p.ordinal(idx)} axis of the batch and is of type {type_a}:\n'
31 | err += f'The first item has shape: {shape_a}\n'
32 | err += f'The {p.number_to_words(p.ordinal(i+1))} item has shape: {shape_b}\n\n'
33 | err += f'Please include a transform in `after_item` that ensures all data of type {type_a} is the same size'
34 | e.args = [err]
35 | raise e
36 |
37 | # Cell
38 | @patch
39 | def create_batch(self:DataLoader, b):
40 | "Collate a list of items into a batch."
41 | func = (fa_collate,fa_convert)[self.prebatched]
42 | try:
43 | return func(b)
44 | except Exception as e:
45 | if not self.prebatched:
46 | collate_error(e, b)
47 | else: raise e
48 |
49 | # Cell
50 | @patch
51 | def new(self:TfmdDL, dataset=None, cls=None, **kwargs):
52 | res = super(TfmdDL, self).new(dataset, cls, do_setup=False, **kwargs)
53 | if not hasattr(self, '_n_inp') or not hasattr(self, '_types'):
54 | try:
55 | self._one_pass()
56 | res._n_inp,res._types = self._n_inp,self._types
57 | except Exception as e:
58 | print("Could not do one pass in your dataloader, there is something wrong in it")
59 | raise e
60 | else: res._n_inp,res._types = self._n_inp,self._types
61 | return res
--------------------------------------------------------------------------------
/fastdebug/fastai/datasets.py:
--------------------------------------------------------------------------------
1 | # AUTOGENERATED! DO NOT EDIT! File to edit: 04_fastai.datasets.ipynb (unless otherwise specified).
2 |
3 | __all__ = ['subset_error']
4 |
5 | # Cell
6 | from fastcore.basics import patch, store_attr
7 | from fastcore.foundation import L, mask2idxs
8 | from fastcore.transform import Pipeline
9 | from fastcore.xtras import is_listy
10 |
11 | from fastai.imports import pv
12 | from fastai.data.core import TfmdLists
13 |
14 | # Cell
15 | @patch
16 | def __init__(self:TfmdLists, items, tfms, use_list=None, do_setup=True, split_idx=None, train_setup=True,
17 | splits=None, types=None, verbose=False, dl_type=None):
18 | if items is None or len(items) == 0: raise IndexError('Items passed in either has a length of zero or is None')
19 | super(TfmdLists, self).__init__(items, use_list=use_list)
20 | if dl_type is not None: self._dl_type = dl_type
21 | self.splits = L([slice(None),[]] if splits is None else splits).map(mask2idxs)
22 | if isinstance(tfms,TfmdLists): tfms = tfms.tfms
23 | if isinstance(tfms,Pipeline): do_setup=False
24 | self.tfms = Pipeline(tfms, split_idx=split_idx)
25 | store_attr('types,split_idx')
26 | if do_setup:
27 | pv(f"Setting up {self.tfms}", verbose)
28 | self.setup(train_setup=train_setup)
29 |
30 | # Cell
31 | def subset_error(e:IndexError, i:int) -> IndexError:
32 | """
33 | IndexError when attempting to grab a non-existant subset in the dataset at index `i`
34 | """
35 | args = e.args[0]
36 | err = f'Tried to grab subset {i} in the Dataset, but it contains no items.\n\n'
37 | err += args
38 | e.args = [err]
39 | raise e
40 |
41 | # Cell
42 | @patch
43 | def subset(self:TfmdLists, i:int):
44 | "New `TfmdLists` with same tfms that only includes items in `i`th split"
45 | try: return self._new(self._get(self.splits[i]), split_idx=i)
46 | except IndexError as e: subset_error(e, i)
47 |
48 | # Cell
49 | @patch
50 | def setup(self:TfmdLists, train_setup=True):
51 | "Transform setup with self"
52 | self.tfms.setup(self, train_setup)
53 | if len(self) != 0:
54 | x = super(TfmdLists, self).__getitem__(0) if self.splits is None else super(TfmdLists, self).__getitem__(self.splits[0])[0]
55 | self.types = []
56 | for f in self.tfms.fs:
57 | self.types.append(getattr(f, 'input_types', type(x)))
58 | x = f(x)
59 | self.types.append(type(x))
60 | t = getattr(self, 'types', [])
61 | if t is None or len(t) == 0: raise Exception("The stored dataset contains no items and `self.types` has not been setup yet")
62 | types = L(t if is_listy(t) else [t] for t in self.types).concat().unique()
63 | self.pretty_types = '\n'.join([f' - {t}' for t in types])
--------------------------------------------------------------------------------
/settings.ini:
--------------------------------------------------------------------------------
1 | [DEFAULT]
2 | # All sections below are required unless otherwise specified
3 | host = github
4 | lib_name = fastdebug
5 | # For Enterprise Git add variable repo_name and company name
6 | # repo_name = analytics
7 | # company_name = nike
8 |
9 | user = muellerzr
10 | description = A library that improves the debugging messages for Pytorch and fastai
11 | keywords = fastai pytorch debugging
12 | author = Zachary Mueller
13 | author_email = muellerzr@gmail.com
14 | copyright = Zachary Mueller
15 | branch = master
16 | version = 0.1.4
17 | min_python = 3.6
18 | audience = Developers
19 | language = English
20 | # Set to True if you want to create a more fancy sidebar.json than the default
21 | custom_sidebar = False
22 | # Add licenses and see current list in `setup.py`
23 | license = apache2
24 | # From 1-7: Planning Pre-Alpha Alpha Beta Production Mature Inactive
25 | status = 2
26 |
27 | requirements = torch>=1.7.0 fastai>=2.0.0 inflect>=5.3.0
28 |
29 | # Optional. Same format as setuptools console_scripts
30 | # console_scripts =
31 | # Optional. Same format as setuptools dependency-links
32 | # dep_links =
33 |
34 | ###
35 | # You probably won't need to change anything under here,
36 | # unless you have some special requirements
37 | ###
38 |
39 | # Change to, e.g. "nbs", to put your notebooks in nbs dir instead of repo root
40 | nbs_path = .
41 | doc_path = docs
42 |
43 | # Whether to look for library notebooks recursively in the `nbs_path` dir
44 | recursive = False
45 |
46 | # Anything shown as '%(...)s' is substituted with that setting automatically
47 | doc_host = https://%(user)s.github.io
48 | #For Enterprise Git pages use:
49 | #doc_host = https://pages.github.%(company_name)s.com.
50 |
51 |
52 | doc_baseurl = /%(lib_name)s/
53 | # For Enterprise Github pages docs use:
54 | # doc_baseurl = /%(repo_name)s/%(lib_name)s/
55 |
56 | git_url = https://github.com/%(user)s/%(lib_name)s/tree/%(branch)s/
57 | # For Enterprise Github use:
58 | #git_url = https://github.%(company_name)s.com/%(repo_name)s/%(lib_name)s/tree/%(branch)s/
59 |
60 |
61 |
62 | lib_path = %(lib_name)s
63 | title = %(lib_name)s
64 |
65 | #Optional advanced parameters
66 | #Monospace docstings: adds
tags around the doc strings, preserving newlines/indentation.
67 | #monospace_docstrings = False
68 | #Test flags: introduce here the test flags you want to use separated by |
69 | tst_flags = slow|failing
70 | #Custom sidebar: customize sidebar.json yourself for advanced sidebars (False/True)
71 | #custom_sidebar =
72 | #Cell spacing: if you want cell blocks in code separated by more than one new line
73 | #cell_spacing =
74 | #Custom jekyll styles: if you want more jekyll styles than tip/important/warning, set them here
75 | #jekyll_styles = note,warning,tip,important
76 |
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | from pkg_resources import parse_version
2 | from configparser import ConfigParser
3 | import setuptools,re,sys
4 | assert parse_version(setuptools.__version__)>=parse_version('36.2')
5 |
6 | # note: all settings are in settings.ini; edit there, not here
7 | config = ConfigParser(delimiters=['='])
8 | config.read('settings.ini')
9 | cfg = config['DEFAULT']
10 |
11 | cfg_keys = 'version description keywords author author_email'.split()
12 | expected = cfg_keys + "lib_name user branch license status min_python audience language".split()
13 | for o in expected: assert o in cfg, "missing expected setting: {}".format(o)
14 | setup_cfg = {o:cfg[o] for o in cfg_keys}
15 |
16 | if len(sys.argv)>1 and sys.argv[1]=='version':
17 | print(setup_cfg['version'])
18 | exit()
19 |
20 | licenses = {
21 | 'apache2': ('Apache Software License 2.0','OSI Approved :: Apache Software License'),
22 | 'mit': ('MIT License', 'OSI Approved :: MIT License'),
23 | 'gpl2': ('GNU General Public License v2', 'OSI Approved :: GNU General Public License v2 (GPLv2)'),
24 | 'gpl3': ('GNU General Public License v3', 'OSI Approved :: GNU General Public License v3 (GPLv3)'),
25 | 'bsd3': ('BSD License', 'OSI Approved :: BSD License'),
26 | }
27 | statuses = [ '1 - Planning', '2 - Pre-Alpha', '3 - Alpha',
28 | '4 - Beta', '5 - Production/Stable', '6 - Mature', '7 - Inactive' ]
29 | py_versions = '2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8'.split()
30 |
31 | lic = licenses.get(cfg['license'].lower(), (cfg['license'], None))
32 | min_python = cfg['min_python']
33 |
34 | requirements = ['pip', 'packaging']
35 | if cfg.get('requirements'): requirements += cfg.get('requirements','').split()
36 | if cfg.get('pip_requirements'): requirements += cfg.get('pip_requirements','').split()
37 | dev_requirements = (cfg.get('dev_requirements') or '').split()
38 |
39 | long_description = open('README.md').read()
40 | # 
41 | for ext in ['png', 'svg']:
42 | long_description = re.sub(r'!\['+ext+'\]\((.*)\)', '+'/'+cfg['branch']+'/\\1)', long_description)
43 | long_description = re.sub(r'src=\"(.*)\.'+ext+'\"', 'src=\"https://raw.githubusercontent.com/{}/{}'.format(cfg['user'],cfg['lib_name'])+'/'+cfg['branch']+'/\\1.'+ext+'\"', long_description)
44 |
45 | setuptools.setup(
46 | name = cfg['lib_name'],
47 | license = lic[0],
48 | classifiers = [
49 | 'Development Status :: ' + statuses[int(cfg['status'])],
50 | 'Intended Audience :: ' + cfg['audience'].title(),
51 | 'Natural Language :: ' + cfg['language'].title(),
52 | ] + ['Programming Language :: Python :: '+o for o in py_versions[py_versions.index(min_python):]] + (['License :: ' + lic[1] ] if lic[1] else []),
53 | url = cfg['git_url'],
54 | packages = setuptools.find_packages(),
55 | include_package_data = True,
56 | install_requires = requirements,
57 | extras_require={ 'dev': dev_requirements },
58 | python_requires = '>=' + cfg['min_python'],
59 | long_description = long_description,
60 | long_description_content_type = 'text/markdown',
61 | zip_safe = False,
62 | entry_points = { 'console_scripts': cfg.get('console_scripts','').split() },
63 | **setup_cfg)
64 |
65 |
--------------------------------------------------------------------------------
/fastdebug/torch.py:
--------------------------------------------------------------------------------
1 | # AUTOGENERATED! DO NOT EDIT! File to edit: 00_torch.ipynb (unless otherwise specified).
2 |
3 | __all__ = ['device_error', 'hook_fn', 'PreHook', 'ForwardHooks', 'hook_outputs', 'layer_error']
4 |
5 | # Cell
6 | import torch
7 | import re
8 | from fastai.callback.hook import Hook
9 | from fastai.torch_core import to_detach
10 | from fastai.layers import flatten_model
11 |
12 | from fastcore.basics import store_attr
13 |
14 | # Cell
15 | def device_error(e:Exception, a:str, b:str) -> Exception:
16 | """
17 | Verbose error for if `a` and `b` are on different devices
18 | Should be used when checking if a model is on the same device, or two tensors
19 | """
20 | inp, weight, _ = e.args[0].replace('( ', '').split(')')
21 | inp = inp.replace('Input type', f'{a} has type: \t\t')
22 | weight = weight.replace(' and weight type', f'{b} have type: \t')
23 | err = f'Mismatch between weight types\n\n{inp})\n{weight})\n\nBoth should be the same.'
24 | e.args = [err]
25 | raise e
26 |
27 | # Cell
28 | def hook_fn(m, i):
29 | "Simple hook fn to return the layer"
30 | return m
31 |
32 | # Cell
33 | class PreHook(Hook):
34 | "Creates and registers a hook on `m` with `hook_func` as a forward pre_hook"
35 | def __init__(self, m, hook_func, is_forward=True, detach=True, cpu=False, gather=False):
36 | store_attr('hook_func,detach,cpu,gather')
37 | f = m.register_forward_pre_hook if is_forward else m.register_backward_pre_hook
38 | self.hook = f(self.hook_fn)
39 | self.stored,self.removed = None, False
40 |
41 | def hook_fn(self, module, inp):
42 | "Applies `hook_fn` to `module` and `inp`"
43 | if self.detach:
44 | inp = to_detach(inp, cpu=self.cpu, gather=self.gather)
45 | self.stored = self.hook_func(module, inp)
46 |
47 | # Cell
48 | class ForwardHooks():
49 | "Create several forward-hooks on the modules in `ms` with `hook_func`"
50 | def __init__(self, ms, hook_func, is_forward=True, detach=True, cpu=False):
51 | self.hooks = []
52 | for i, m in enumerate(flatten_model(ms)):
53 | self.hooks.append(PreHook(m, hook_func, is_forward, detach, cpu))
54 |
55 | # Cell
56 | def hook_outputs(modules, detach=True, cpu=False, grad=False):
57 | "Return `Hooks` that store activations of all `modules` in `self.stored`"
58 | return ForwardHooks(modules, hook_fn, detach=detach, cpu=cpu, is_forward=not grad)
59 |
60 | # Cell
61 | def layer_error(e:Exception, model, *inp) -> Exception:
62 | """
63 | Verbose error for when there is a size mismatch between some input and the model.
64 | `model` should be any torch model
65 | `inp` is the input that went to the model
66 | """
67 | args = e.args[0].replace("Expected", "Model expected")
68 | hooks = hook_outputs(model)
69 | try:
70 | _ = model(*inp)
71 | except:
72 | pass
73 | finally:
74 | layers,num = [], 0
75 | for i, layer in enumerate(hooks.hooks):
76 | if layer.stored is not None:
77 | layers.append(layer.stored)
78 | num += 1
79 | layer = layers[-1]
80 | [h.remove() for h in hooks.hooks]
81 | e.args = [f'Size mismatch between input tensors and what the model expects\n{"-"*76}\nLayer: {i}, {layer}\nError: {args}']
82 | raise e
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # fastdebug
3 | > A helpful library for improving torch and fastai errors
4 |
5 |
6 | ## Install
7 |
8 | `pip install fastdebug`
9 |
10 | ## How to use
11 |
12 | `fastdebug` is designed around improving the quality of life when dealing with Pytorch and fastai errors, while also including some new sanity checks (fastai only)
13 |
14 | ### Pytorch
15 |
16 | Pytorch now has:
17 | * `device_error`
18 | * `layer_error`
19 |
20 | Both can be imported with:
21 | ```python
22 | from fastdebug.error.torch import device_error, layer_error
23 | ```
24 |
25 | `device_error` prints out a much more readable error for when two tensors aren't on the same device:
26 |
27 | ```python
28 | inp = torch.rand().cuda()
29 | model = model.cpu()
30 | try:
31 | _ = model(inp)
32 | except Exception as e:
33 | device_error(e, 'Input type', 'Model weights')
34 | ```
35 | And our new log:
36 | ```bash
37 | ---------------------------------------------------------------------------
38 | RuntimeError Traceback (most recent call last)
39 | in ()
40 | 2 model(x)
41 | 3 except Exception as e:
42 | ----> 4 device_error(e, 'Input type', 'Model weights')
43 |
44 | 10 frames
45 | /usr/local/lib/python3.7/dist-packages/torch/tensor.py in __torch_function__(cls, func, types, args, kwargs)
46 | 993
47 | 994 with _C.DisableTorchFunction():
48 | --> 995 ret = func(*args, **kwargs)
49 | 996 return _convert(ret, cls)
50 | 997
51 |
52 | RuntimeError: Mismatch between weight types
53 |
54 | Input type has type: (torch.cuda.FloatTensor)
55 | Model weights have type: (torch.FloatTensor)
56 |
57 | Both should be the same.
58 | ```
59 |
60 | And with `layer_error`, if there is a shape mismatch it will attempt to find the right layer it was at:
61 | ```python
62 | inp = torch.rand(5,2, 3)
63 | try:
64 | m(inp)
65 | except Exception as e:
66 | layer_error(e, m)
67 | ```
68 |
69 | ```python
70 | ---------------------------------------------------------------------------
71 | RuntimeError Traceback (most recent call last)
72 | in ()
73 | 3 m(inp)
74 | 4 except Exception as e:
75 | ----> 5 layer_error(e, m)
76 |
77 | in layer_error(e, model)
78 | 8 i, layer = get_layer_by_shape(model, shape)
79 | 9 e.args = [f'Size mismatch between input tensors and what the model expects\n\n{args}\n\tat layer {i}: {layer}']
80 | ---> 10 raise e
81 |
82 | in ()
83 | 1 inp = torch.rand(5,2, 3)
84 | 2 try:
85 | ----> 3 m(inp)
86 | 4 except Exception as e:
87 | 5 layer_error(e, m)
88 |
89 | /mnt/d/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
90 | 725 result = self._slow_forward(*input, **kwargs)
91 | 726 else:
92 | --> 727 result = self.forward(*input, **kwargs)
93 | 728 for hook in itertools.chain(
94 | 729 _global_forward_hooks.values(),
95 |
96 | /mnt/d/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
97 | 115 def forward(self, input):
98 | 116 for module in self:
99 | --> 117 input = module(input)
100 | 118 return input
101 | 119
102 |
103 | /mnt/d/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
104 | 725 result = self._slow_forward(*input, **kwargs)
105 | 726 else:
106 | --> 727 result = self.forward(*input, **kwargs)
107 | 728 for hook in itertools.chain(
108 | 729 _global_forward_hooks.values(),
109 |
110 | /mnt/d/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
111 | 421
112 | 422 def forward(self, input: Tensor) -> Tensor:
113 | --> 423 return self._conv_forward(input, self.weight)
114 | 424
115 | 425 class Conv3d(_ConvNd):
116 |
117 | /mnt/d/lib/python3.7/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
118 | 418 _pair(0), self.dilation, self.groups)
119 | 419 return F.conv2d(input, weight, self.bias, self.stride,
120 | --> 420 self.padding, self.dilation, self.groups)
121 | 421
122 | 422 def forward(self, input: Tensor) -> Tensor:
123 |
124 | RuntimeError: Size mismatch between input tensors and what the model expects
125 |
126 | Model expected 4-dimensional input for 4-dimensional weight [3, 3, 1, 1], but got 3-dimensional input of size [5, 2, 3] instead
127 | at layer 1: Conv2d(3, 3, kernel_size=(1, 1), stride=(1, 1))
128 | ```
129 |
130 | ### fastai
131 |
132 | Along with the additions above (and are used during `fit`), fastai now has a `Learner.sanity_check` function, which allows you to quickly perform a basic check to ensure that your call to `fit` won't raise any exceptions. They are performed on the CPU for a partial epoch to make sure that `CUDA` device-assist errors can be preemptively found.
133 |
134 | To use it simply do:
135 | ```python
136 | from fastdebug.fastai import *
137 | from fastai.vision.all import *
138 |
139 | learn = Learner(...)
140 | learn.sanity_check()
141 | ```
142 |
143 | This is also now an argument in `Learner`, set to `False` by default, so that after making your `Learner` a quick check is ensured.
144 |
145 | ```python
146 | learn = Learner(..., sanity_check=True)
147 | ```
148 |
--------------------------------------------------------------------------------
/fastdebug/fastai/learner.py:
--------------------------------------------------------------------------------
1 | # AUTOGENERATED! DO NOT EDIT! File to edit: 02_fastai.learner.ipynb (unless otherwise specified).
2 |
3 | __all__ = ['loss_func_error', 'callback_error', 'catch_pred_errors', 'catch_loss_errors', 'module_error',
4 | 'load_learner']
5 |
6 | # Cell
7 | from ..torch import layer_error, device_error
8 |
9 | from fastai.data.all import *
10 | from fastai.optimizer import *
11 | from fastai.learner import *
12 | from fastai.callback.core import event
13 | from fastai.callback.training import ShortEpochCallback
14 | from fastai.torch_core import default_device
15 |
16 |
17 | from fastcore.basics import patch
18 | from fastcore.meta import delegates
19 |
20 | # Cell
21 | def loss_func_error(e:Exception, learn) -> Exception:
22 | """
23 | Error that should be run when there is an issue when working with the loss function
24 |
25 | Raises with a message stating the shapes of the inputs and targs, and the error
26 | """
27 | err = f'There was an issue with calculating the loss with `{getattr(learn.loss_func, "__name__", learn.loss_func)}`'
28 | err += f'\n\nPrediction shape(s): {[p.shape for p in listify(learn.pred)]}'
29 | err += f'\nLabel Shape(s): {[y.shape for y in learn.yb]}'
30 | err += f'\nError: {e.args[0]}'
31 | e.args = [err]
32 | raise e
33 |
34 | # Cell
35 | def callback_error(e:Exception, cb:str, event_name:str) -> Exception:
36 | """
37 | Raises an error from when a Callback event failed, showing what event, the name of the Callback and the trace
38 | """
39 | e.args = [f"Exception raised in the {cb} Callback during {event_name}:\n\n{e.args[0]}"]
40 | raise e
41 |
42 | # Cell
43 | def catch_pred_errors(e:Exception, model) -> Exception:
44 | "Catches any errors relating to prediction that are either related to the device or model layers. Else raise `e`"
45 | if "Input type" in e.args[0]: device_error(e, 'Input', 'Model weights')
46 | elif "Expected" in e.args[0]: layer_error(e, model)
47 | else: raise e # anything else
48 |
49 | # Cell
50 | def catch_loss_errors(e:Exception, learn):
51 | "Catches any errors that occur with the loss function and its calculation"
52 | if "Input type" in e.args[0]: device_error(e, 'Model prediction', 'Truths')
53 | else: loss_func_error(e, learn)
54 |
55 | # Cell
56 | @patch
57 | def sanity_check(self:Learner, show_table=False):
58 | "Performs a short epoch and uses all the callbacks in `self.cbs` on the CPU to ensure nothing is broken"
59 | device = getattr(self.dls, 'device', default_device())
60 | if hasattr(self.dls, 'device'):
61 | self.dls.device = 'cpu'
62 | else:
63 | # Using raw torch
64 | self.model.to('cpu')
65 | self.save('tmp')
66 | cbs = [ShortEpochCallback(short_valid=False)]
67 | if show_table:
68 | with self.no_bar(), self.no_logging():
69 | self.fit(1, cbs=cbs)
70 | else:
71 | self.fit(1, cbs=cbs)
72 | if hasattr(self.dls, 'device'):
73 | self.dls.device = device
74 | else:
75 | self.model.to(device)
76 | self.load('tmp')
77 |
78 | # Cell
79 | @patch
80 | @delegates(Learner.sanity_check)
81 | def __init__(self:Learner, dls, model, loss_func=None, opt_func=Adam, lr=defaults.lr, splitter=trainable_params, cbs=None,
82 | metrics=None, path=None, model_dir='models', wd=None, wd_bn_bias=False, train_bn=True,
83 | moms=(0.95,0.85,0.95), sanity_check=False, **kwargs):
84 | "Group together a `model`, some `dls` and a `loss_func` to handle training, potentially run a sanity check"
85 | path = Path(path) if path is not None else getattr(dls, 'path', Path('.'))
86 | if loss_func is None:
87 | loss_func = getattr(dls.train_ds, 'loss_func', None)
88 | assert loss_func is not None, "Could not infer loss function from the data, please pass a loss function."
89 | self.dls,self.model = dls,model
90 | store_attr(but='dls,model,cbs')
91 | self.training,self.create_mbar,self.logger,self.opt,self.cbs = False,True,print,None,L()
92 | self.add_cbs(L(defaults.callbacks)+L(cbs))
93 | self("after_create")
94 | if sanity_check: self.sanity_check(**kwargs)
95 |
96 | # Cell
97 | @patch
98 | def _do_one_batch(self:Learner):
99 | try:
100 | self.pred = self.model(*self.xb)
101 | except RuntimeError as e:
102 | catch_pred_errors(e, self.model)
103 | self('after_pred')
104 | if len(self.yb):
105 | try:
106 | self.loss_grad = self.loss_func(self.pred, *self.yb)
107 | except Exception as e:
108 | catch_loss_errors(e, self)
109 | self.loss = self.loss_grad.clone()
110 | self('after_loss')
111 | if not self.training or not len(self.yb): return
112 | self('before_backward')
113 | self.loss_grad.backward()
114 | self._with_events(self.opt.step, 'step', CancelStepException)
115 | self.opt.zero_grad()
116 |
117 | # Cell
118 | @patch
119 | def _call_one(self:Learner, event_name):
120 | if not hasattr(event, event_name): raise Exception(f'missing {event_name}')
121 | for cb in self.cbs.sorted('order'):
122 | try:
123 | cb(event_name)
124 | except Exception as e:
125 | callback_error(e, cb.__repr__(), event_name)
126 |
127 | # Cell
128 | def module_error(e:AttributeError) -> AttributeError:
129 | """
130 | Raises an error when trying to load in a previous `Learner` and custom functions were not available in the namespace
131 | """
132 | args = e.args[0]
133 | err = 'Custom classes or functions exported with your `Learner` are not available in the namespace currently.\n'
134 | err += 'Please re-declare them before calling `load_learner`:\n\n'
135 | err += args
136 | e.args = [err]
137 | raise e
138 |
139 | # Cell
140 | def load_learner(fname, cpu=True, pickle_module=pickle):
141 | "Load a `Learner` object in `fname`, optionally putting it on the `cpu`"
142 | distrib_barrier()
143 | try: res = torch.load(fname, map_location='cpu' if cpu else None, pickle_module=pickle_module)
144 | except AttributeError as e: module_error(e)
145 | if hasattr(res, 'to_fp32'): res = res.to_fp32()
146 | if cpu: res.dls.cpu()
147 | return res
--------------------------------------------------------------------------------
/index.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": null,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "#hide\n",
10 | "from fastdebug.torch import *\n",
11 | "from fastdebug.fastai import *"
12 | ]
13 | },
14 | {
15 | "cell_type": "markdown",
16 | "metadata": {},
17 | "source": [
18 | "# fastdebug\n",
19 | "\n",
20 | "> A helpful library for improving torch and fastai errors"
21 | ]
22 | },
23 | {
24 | "cell_type": "markdown",
25 | "metadata": {},
26 | "source": [
27 | "## Install"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {},
33 | "source": [
34 | "`pip install fastdebug`"
35 | ]
36 | },
37 | {
38 | "cell_type": "markdown",
39 | "metadata": {},
40 | "source": [
41 | "## How to use"
42 | ]
43 | },
44 | {
45 | "cell_type": "markdown",
46 | "metadata": {},
47 | "source": [
48 | "`fastdebug` is designed around improving the quality of life when dealing with Pytorch and fastai errors, while also including some new sanity checks (fastai only)"
49 | ]
50 | },
51 | {
52 | "cell_type": "markdown",
53 | "metadata": {},
54 | "source": [
55 | "### Pytorch\n",
56 | "\n",
57 | "Pytorch now has:\n",
58 | "* `device_error`\n",
59 | "* `layer_error`\n",
60 | "\n",
61 | "Both can be imported with:\n",
62 | "```python\n",
63 | "from fastdebug.error.torch import device_error, layer_error\n",
64 | "```\n",
65 | "\n",
66 | "`device_error` prints out a much more readable error for when two tensors aren't on the same device:"
67 | ]
68 | },
69 | {
70 | "cell_type": "markdown",
71 | "metadata": {},
72 | "source": [
73 | "```python\n",
74 | "inp = torch.rand().cuda()\n",
75 | "model = model.cpu()\n",
76 | "try:\n",
77 | " _ = model(inp)\n",
78 | "except Exception as e:\n",
79 | " device_error(e, 'Input type', 'Model weights')\n",
80 | "```\n",
81 | "And our new log:\n",
82 | "```bash\n",
83 | "---------------------------------------------------------------------------\n",
84 | "RuntimeError Traceback (most recent call last)\n",
85 | " in ()\n",
86 | " 2 model(x)\n",
87 | " 3 except Exception as e:\n",
88 | "----> 4 device_error(e, 'Input type', 'Model weights')\n",
89 | "\n",
90 | "10 frames\n",
91 | "/usr/local/lib/python3.7/dist-packages/torch/tensor.py in __torch_function__(cls, func, types, args, kwargs)\n",
92 | " 993 \n",
93 | " 994 with _C.DisableTorchFunction():\n",
94 | "--> 995 ret = func(*args, **kwargs)\n",
95 | " 996 return _convert(ret, cls)\n",
96 | " 997 \n",
97 | "\n",
98 | "RuntimeError: Mismatch between weight types\n",
99 | "\n",
100 | "Input type has type: \t\t (torch.cuda.FloatTensor)\n",
101 | "Model weights have type: \t (torch.FloatTensor)\n",
102 | "\n",
103 | "Both should be the same.\n",
104 | "```"
105 | ]
106 | },
107 | {
108 | "cell_type": "markdown",
109 | "metadata": {},
110 | "source": [
111 | "And with `layer_error`, if there is a shape mismatch it will attempt to find the right layer it was at:\n",
112 | "```python\n",
113 | "inp = torch.rand(5,2, 3)\n",
114 | "try:\n",
115 | " m(inp)\n",
116 | "except Exception as e:\n",
117 | " layer_error(e, m)\n",
118 | "```\n",
119 | "\n",
120 | "```python\n",
121 | "---------------------------------------------------------------------------\n",
122 | "RuntimeError Traceback (most recent call last)\n",
123 | " in ()\n",
124 | " 3 m(inp)\n",
125 | " 4 except Exception as e:\n",
126 | "----> 5 layer_error(e, m)\n",
127 | "\n",
128 | " in layer_error(e, model)\n",
129 | " 8 i, layer = get_layer_by_shape(model, shape)\n",
130 | " 9 e.args = [f'Size mismatch between input tensors and what the model expects\\n\\n{args}\\n\\tat layer {i}: {layer}']\n",
131 | "---> 10 raise e\n",
132 | "\n",
133 | " in ()\n",
134 | " 1 inp = torch.rand(5,2, 3)\n",
135 | " 2 try:\n",
136 | "----> 3 m(inp)\n",
137 | " 4 except Exception as e:\n",
138 | " 5 layer_error(e, m)\n",
139 | "\n",
140 | "/mnt/d/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)\n",
141 | " 725 result = self._slow_forward(*input, **kwargs)\n",
142 | " 726 else:\n",
143 | "--> 727 result = self.forward(*input, **kwargs)\n",
144 | " 728 for hook in itertools.chain(\n",
145 | " 729 _global_forward_hooks.values(),\n",
146 | "\n",
147 | "/mnt/d/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)\n",
148 | " 115 def forward(self, input):\n",
149 | " 116 for module in self:\n",
150 | "--> 117 input = module(input)\n",
151 | " 118 return input\n",
152 | " 119 \n",
153 | "\n",
154 | "/mnt/d/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)\n",
155 | " 725 result = self._slow_forward(*input, **kwargs)\n",
156 | " 726 else:\n",
157 | "--> 727 result = self.forward(*input, **kwargs)\n",
158 | " 728 for hook in itertools.chain(\n",
159 | " 729 _global_forward_hooks.values(),\n",
160 | "\n",
161 | "/mnt/d/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)\n",
162 | " 421 \n",
163 | " 422 def forward(self, input: Tensor) -> Tensor:\n",
164 | "--> 423 return self._conv_forward(input, self.weight)\n",
165 | " 424 \n",
166 | " 425 class Conv3d(_ConvNd):\n",
167 | "\n",
168 | "/mnt/d/lib/python3.7/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)\n",
169 | " 418 _pair(0), self.dilation, self.groups)\n",
170 | " 419 return F.conv2d(input, weight, self.bias, self.stride,\n",
171 | "--> 420 self.padding, self.dilation, self.groups)\n",
172 | " 421 \n",
173 | " 422 def forward(self, input: Tensor) -> Tensor:\n",
174 | "\n",
175 | "RuntimeError: Size mismatch between input tensors and what the model expects\n",
176 | "\n",
177 | "Model expected 4-dimensional input for 4-dimensional weight [3, 3, 1, 1], but got 3-dimensional input of size [5, 2, 3] instead\n",
178 | "\tat layer 1: Conv2d(3, 3, kernel_size=(1, 1), stride=(1, 1))\n",
179 | "```"
180 | ]
181 | },
182 | {
183 | "cell_type": "markdown",
184 | "metadata": {},
185 | "source": [
186 | "### fastai\n",
187 | "\n",
188 | "Along with the additions above (and are used during `fit`), fastai now has a `Learner.sanity_check` function, which allows you to quickly perform a basic check to ensure that your call to `fit` won't raise any exceptions. They are performed on the CPU for a partial epoch to make sure that `CUDA` device-assist errors can be preemptively found.\n",
189 | "\n",
190 | "To use it simply do:\n",
191 | "```python\n",
192 | "from fastdebug.fastai import *\n",
193 | "from fastai.vision.all import *\n",
194 | "\n",
195 | "learn = Learner(...)\n",
196 | "learn.sanity_check()\n",
197 | "```\n",
198 | "\n",
199 | "This is also now an argument in `Learner`, set to `False` by default, so that after making your `Learner` a quick check is ensured."
200 | ]
201 | },
202 | {
203 | "cell_type": "markdown",
204 | "metadata": {},
205 | "source": [
206 | "```python\n",
207 | "learn = Learner(..., sanity_check=True)\n",
208 | "```"
209 | ]
210 | }
211 | ],
212 | "metadata": {
213 | "kernelspec": {
214 | "display_name": "Python 3",
215 | "language": "python",
216 | "name": "python3"
217 | }
218 | },
219 | "nbformat": 4,
220 | "nbformat_minor": 2
221 | }
222 |
--------------------------------------------------------------------------------
/docs/error.fastai.html:
--------------------------------------------------------------------------------
1 | ---
2 |
3 | title: fastai Errors
4 |
5 |
6 | keywords: fastai
7 | sidebar: home_sidebar
8 |
9 | summary: "In-place fastai specific errors to ease debugging"
10 | description: "In-place fastai specific errors to ease debugging"
11 | nb_path: "01_error.fastai.ipynb"
12 | ---
13 |
22 |
23 |
24 |
25 | {% raw %}
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | {% endraw %}
46 |
47 | {% raw %}
48 |
49 |
50 |
51 |
52 | {% endraw %}
53 |
54 |
55 |
56 | This notebook contains a series of various errors that can be used when running with fastai. It should be noted here that there is no other imports or magic you need to do to use this section of the library other then: from fastdebug import *. It will automatically load in what's needed.
57 | As a style choice, we are choosing to do the .* notation as this loads in not only all of our errors, but also replaces sections of fastai's code to inject some error handling (as we'll see later)
58 |
59 |
60 |
61 |
62 |
68 | {% raw %}
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 | {% endraw %}
93 |
94 | {% raw %}
95 |
96 |
97 |
98 |
99 | {% endraw %}
100 |
101 | {% raw %}
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 | {% endraw %}
125 |
126 | {% raw %}
127 |
128 |
129 |
130 |
131 | {% endraw %}
132 |
133 | {% raw %}
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 | {% endraw %}
157 |
158 | {% raw %}
159 |
160 |
161 |
162 |
163 | {% endraw %}
164 |
165 | {% raw %}
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 | {% endraw %}
189 |
190 | {% raw %}
191 |
192 |
193 |
194 |
195 | {% endraw %}
196 |
197 |
203 | {% raw %}
204 |
205 |
206 |
207 |
208 | {% endraw %}
209 |
210 | {% raw %}
211 |
212 |
213 |
214 |
215 | {% endraw %}
216 |
217 | {% raw %}
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 | {% endraw %}
241 |
242 | {% raw %}
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 | {% endraw %}
266 |
267 |
268 |
269 | With sanity_check, you can make sure that you've set everything up properly and you won't get any issues before pushing to the GPU. This allows you to quickly ensure that you won't get any CUDA device-assist errors, and that the whole training regiment will go well.
270 |
271 |
272 |
273 |
274 | {% raw %}
275 |
276 |
277 |
278 |
279 | {% endraw %}
280 |
281 | {% raw %}
282 |
283 |
284 |
285 |
286 | {% endraw %}
287 |
288 |
289 |
290 |
291 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/docs/fastai.learner.html:
--------------------------------------------------------------------------------
1 | ---
2 |
3 | title: Learner Errors
4 |
5 |
6 | keywords: fastai
7 | sidebar: home_sidebar
8 |
9 | summary: "In-place fastai specific errors to ease debugging"
10 | description: "In-place fastai specific errors to ease debugging"
11 | nb_path: "02_fastai.learner.ipynb"
12 | ---
13 |
22 |
23 |
24 |
25 | {% raw %}
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | {% endraw %}
46 |
47 | {% raw %}
48 |
49 |
50 |
51 |
52 | {% endraw %}
53 |
54 |
55 |
56 | This notebook contains a series of various errors that can be used when running with fastai. It should be noted here that there is no other imports or magic you need to do to use this section of the library other then: from fastdebug import *. It will automatically load in what's needed.
57 | As a style choice, we are choosing to do the .* notation as this loads in not only all of our errors, but also replaces sections of fastai's code to inject some error handling (as we'll see later)
58 |
59 |
60 |
61 |
62 |
68 | {% raw %}
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 | {% endraw %}
93 |
94 | {% raw %}
95 |
96 |
97 |
98 |
99 | {% endraw %}
100 |
101 | {% raw %}
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 | {% endraw %}
125 |
126 | {% raw %}
127 |
128 |
129 |
130 |
131 | {% endraw %}
132 |
133 | {% raw %}
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 | {% endraw %}
157 |
158 | {% raw %}
159 |
160 |
161 |
162 |
163 | {% endraw %}
164 |
165 | {% raw %}
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 | {% endraw %}
189 |
190 | {% raw %}
191 |
192 |
193 |
194 |
195 | {% endraw %}
196 |
197 |
203 | {% raw %}
204 |
205 |
206 |
207 |
208 | {% endraw %}
209 |
210 | {% raw %}
211 |
212 |
213 |
214 |
215 | {% endraw %}
216 |
217 | {% raw %}
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 | {% endraw %}
241 |
242 | {% raw %}
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 | {% endraw %}
266 |
267 |
268 |
269 | With sanity_check, you can make sure that you've set everything up properly and you won't get any issues before pushing to the GPU. This allows you to quickly ensure that you won't get any CUDA device-assist errors, and that the whole training regiment will go well.
270 |
271 |
272 |
273 |
274 | {% raw %}
275 |
276 |
277 |
278 |
279 | {% endraw %}
280 |
281 | {% raw %}
282 |
283 |
284 |
285 |
286 | {% endraw %}
287 |
288 | {% raw %}
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 | {% endraw %}
312 |
313 | {% raw %}
314 |
315 |
316 |
317 |
318 | {% endraw %}
319 |
320 | {% raw %}
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 | {% endraw %}
344 |
345 | {% raw %}
346 |
347 |
348 |
349 |
350 | {% endraw %}
351 |
352 |
353 |
354 | We have a custom load_learner function here that can check if everything exported is available when bringing the model in, if not then it'll raise an explicit error
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
--------------------------------------------------------------------------------
/02_fastai.learner.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": null,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "#default_exp fastai.learner"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": null,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "#hide\n",
19 | "from nbdev.showdoc import *"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "# Learner Errors\n",
27 | "> In-place fastai specific errors to ease debugging"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "#export\n",
37 | "from fastdebug.torch import layer_error, device_error\n",
38 | "\n",
39 | "from fastai.data.all import *\n",
40 | "from fastai.optimizer import *\n",
41 | "from fastai.learner import *\n",
42 | "from fastai.callback.core import event\n",
43 | "from fastai.callback.training import ShortEpochCallback\n",
44 | "from fastai.torch_core import default_device\n",
45 | "\n",
46 | "\n",
47 | "from fastcore.basics import patch\n",
48 | "from fastcore.meta import delegates"
49 | ]
50 | },
51 | {
52 | "cell_type": "markdown",
53 | "metadata": {},
54 | "source": [
55 | "This notebook contains a series of various errors that can be used when running with `fastai`. It should be noted here that there is no other imports or magic you need to do to use this section of the library other then: `from fastdebug import *`. It will automatically load in what's needed.\n",
56 | "\n",
57 | "As a style choice, we are choosing to do the `.*` notation as this loads in not only all of our errors, but also replaces sections of `fastai`'s code to inject some error handling (as we'll see later)"
58 | ]
59 | },
60 | {
61 | "cell_type": "markdown",
62 | "metadata": {},
63 | "source": [
64 | "## Error Types"
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "execution_count": null,
70 | "metadata": {},
71 | "outputs": [],
72 | "source": [
73 | "#export\n",
74 | "def loss_func_error(e:Exception, learn) -> Exception:\n",
75 | " \"\"\"\n",
76 | " Error that should be run when there is an issue when working with the loss function\n",
77 | " \n",
78 | " Raises with a message stating the shapes of the inputs and targs, and the error\n",
79 | " \"\"\"\n",
80 | " err = f'There was an issue with calculating the loss with `{getattr(learn.loss_func, \"__name__\", learn.loss_func)}`'\n",
81 | " err += f'\\n\\nPrediction shape(s): {[p.shape for p in listify(learn.pred)]}'\n",
82 | " err += f'\\nLabel Shape(s): {[y.shape for y in learn.yb]}'\n",
83 | " err += f'\\nError: {e.args[0]}'\n",
84 | " e.args = [err]\n",
85 | " raise e"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": null,
91 | "metadata": {},
92 | "outputs": [],
93 | "source": [
94 | "#export\n",
95 | "def callback_error(e:Exception, cb:str, event_name:str) -> Exception:\n",
96 | " \"\"\"\n",
97 | " Raises an error from when a Callback event failed, showing what event, the name of the Callback and the trace\n",
98 | " \"\"\"\n",
99 | " e.args = [f\"Exception raised in the {cb} Callback during {event_name}:\\n\\n{e.args[0]}\"]\n",
100 | " raise e"
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": null,
106 | "metadata": {},
107 | "outputs": [],
108 | "source": [
109 | "#export\n",
110 | "def catch_pred_errors(e:Exception, model) -> Exception:\n",
111 | " \"Catches any errors relating to prediction that are either related to the device or model layers. Else raise `e`\"\n",
112 | " if \"Input type\" in e.args[0]: device_error(e, 'Input', 'Model weights')\n",
113 | " elif \"Expected\" in e.args[0]: layer_error(e, model)\n",
114 | " else: raise e # anything else "
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "execution_count": null,
120 | "metadata": {},
121 | "outputs": [],
122 | "source": [
123 | "#export\n",
124 | "def catch_loss_errors(e:Exception, learn):\n",
125 | " \"Catches any errors that occur with the loss function and its calculation\"\n",
126 | " if \"Input type\" in e.args[0]: device_error(e, 'Model prediction', 'Truths')\n",
127 | " else: loss_func_error(e, learn)"
128 | ]
129 | },
130 | {
131 | "cell_type": "markdown",
132 | "metadata": {},
133 | "source": [
134 | "## Modifications and Enhancements to the fastai Source Code and `Learner`:"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": null,
140 | "metadata": {},
141 | "outputs": [],
142 | "source": [
143 | "#export\n",
144 | "@patch\n",
145 | "def sanity_check(self:Learner, show_table=False):\n",
146 | " \"Performs a short epoch and uses all the callbacks in `self.cbs` on the CPU to ensure nothing is broken\"\n",
147 | " device = getattr(self.dls, 'device', default_device())\n",
148 | " if hasattr(self.dls, 'device'):\n",
149 | " self.dls.device = 'cpu'\n",
150 | " else:\n",
151 | " # Using raw torch\n",
152 | " self.model.to('cpu')\n",
153 | " self.save('tmp')\n",
154 | " cbs = [ShortEpochCallback(short_valid=False)]\n",
155 | " if show_table:\n",
156 | " with self.no_bar(), self.no_logging():\n",
157 | " self.fit(1, cbs=cbs)\n",
158 | " else:\n",
159 | " self.fit(1, cbs=cbs)\n",
160 | " if hasattr(self.dls, 'device'):\n",
161 | " self.dls.device = device\n",
162 | " else:\n",
163 | " self.model.to(device)\n",
164 | " self.load('tmp')"
165 | ]
166 | },
167 | {
168 | "cell_type": "code",
169 | "execution_count": null,
170 | "metadata": {},
171 | "outputs": [],
172 | "source": [
173 | "#export\n",
174 | "@patch\n",
175 | "@delegates(Learner.sanity_check)\n",
176 | "def __init__(self:Learner, dls, model, loss_func=None, opt_func=Adam, lr=defaults.lr, splitter=trainable_params, cbs=None,\n",
177 | " metrics=None, path=None, model_dir='models', wd=None, wd_bn_bias=False, train_bn=True,\n",
178 | " moms=(0.95,0.85,0.95), sanity_check=False, **kwargs):\n",
179 | " \"Group together a `model`, some `dls` and a `loss_func` to handle training, potentially run a sanity check\"\n",
180 | " path = Path(path) if path is not None else getattr(dls, 'path', Path('.'))\n",
181 | " if loss_func is None:\n",
182 | " loss_func = getattr(dls.train_ds, 'loss_func', None)\n",
183 | " assert loss_func is not None, \"Could not infer loss function from the data, please pass a loss function.\"\n",
184 | " self.dls,self.model = dls,model\n",
185 | " store_attr(but='dls,model,cbs')\n",
186 | " self.training,self.create_mbar,self.logger,self.opt,self.cbs = False,True,print,None,L()\n",
187 | " self.add_cbs(L(defaults.callbacks)+L(cbs))\n",
188 | " self(\"after_create\")\n",
189 | " if sanity_check: self.sanity_check(**kwargs)"
190 | ]
191 | },
192 | {
193 | "cell_type": "code",
194 | "execution_count": null,
195 | "metadata": {},
196 | "outputs": [
197 | {
198 | "data": {
199 | "text/markdown": [
200 | "Learner.__init__[source]
\n",
201 | "\n",
202 | "> Learner.__init__(**`dls`**, **`model`**, **`loss_func`**=*`None`*, **`opt_func`**=*`Adam`*, **`lr`**=*`0.001`*, **`splitter`**=*`trainable_params`*, **`cbs`**=*`None`*, **`metrics`**=*`None`*, **`path`**=*`None`*, **`model_dir`**=*`'models'`*, **`wd`**=*`None`*, **`wd_bn_bias`**=*`False`*, **`train_bn`**=*`True`*, **`moms`**=*`(0.95, 0.85, 0.95)`*, **`sanity_check`**=*`False`*, **`show_table`**=*`False`*)\n",
203 | "\n",
204 | "Group together a `model`, some `dls` and a `loss_func` to handle training, potentially run a sanity check"
205 | ],
206 | "text/plain": [
207 | ""
208 | ]
209 | },
210 | "metadata": {},
211 | "output_type": "display_data"
212 | }
213 | ],
214 | "source": [
215 | "show_doc(Learner.__init__)"
216 | ]
217 | },
218 | {
219 | "cell_type": "code",
220 | "execution_count": null,
221 | "metadata": {},
222 | "outputs": [
223 | {
224 | "data": {
225 | "text/markdown": [
226 | "Learner.sanity_check[source]
\n",
227 | "\n",
228 | "> Learner.sanity_check(**`show_table`**=*`False`*)\n",
229 | "\n",
230 | "Performs a short epoch and uses all the callbacks in `self.cbs` on the CPU to ensure nothing is broken"
231 | ],
232 | "text/plain": [
233 | ""
234 | ]
235 | },
236 | "metadata": {},
237 | "output_type": "display_data"
238 | }
239 | ],
240 | "source": [
241 | "show_doc(Learner.sanity_check)"
242 | ]
243 | },
244 | {
245 | "cell_type": "markdown",
246 | "metadata": {},
247 | "source": [
248 | "With `sanity_check`, you can make sure that you've set everything up properly and you won't get any issues before pushing to the GPU. This allows you to quickly ensure that you won't get any `CUDA` device-assist errors, and that the whole training regiment will go well. "
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": null,
254 | "metadata": {},
255 | "outputs": [],
256 | "source": [
257 | "#export\n",
258 | "@patch\n",
259 | "def _do_one_batch(self:Learner):\n",
260 | " try:\n",
261 | " self.pred = self.model(*self.xb)\n",
262 | " except RuntimeError as e:\n",
263 | " catch_pred_errors(e, self.model)\n",
264 | " self('after_pred')\n",
265 | " if len(self.yb):\n",
266 | " try:\n",
267 | " self.loss_grad = self.loss_func(self.pred, *self.yb)\n",
268 | " except Exception as e:\n",
269 | " catch_loss_errors(e, self)\n",
270 | " self.loss = self.loss_grad.clone()\n",
271 | " self('after_loss')\n",
272 | " if not self.training or not len(self.yb): return\n",
273 | " self('before_backward')\n",
274 | " self.loss_grad.backward()\n",
275 | " self._with_events(self.opt.step, 'step', CancelStepException)\n",
276 | " self.opt.zero_grad()"
277 | ]
278 | },
279 | {
280 | "cell_type": "code",
281 | "execution_count": null,
282 | "metadata": {},
283 | "outputs": [],
284 | "source": [
285 | "#export\n",
286 | "@patch\n",
287 | "def _call_one(self:Learner, event_name):\n",
288 | " if not hasattr(event, event_name): raise Exception(f'missing {event_name}')\n",
289 | " for cb in self.cbs.sorted('order'):\n",
290 | " try:\n",
291 | " cb(event_name)\n",
292 | " except Exception as e:\n",
293 | " callback_error(e, cb.__repr__(), event_name)"
294 | ]
295 | },
296 | {
297 | "cell_type": "code",
298 | "execution_count": null,
299 | "metadata": {},
300 | "outputs": [],
301 | "source": [
302 | "#export\n",
303 | "def module_error(e:AttributeError) -> AttributeError:\n",
304 | " \"\"\"\n",
305 | " Raises an error when trying to load in a previous `Learner` and custom functions were not available in the namespace\n",
306 | " \"\"\"\n",
307 | " args = e.args[0]\n",
308 | " err = 'Custom classes or functions exported with your `Learner` are not available in the namespace currently.\\n'\n",
309 | " err += 'Please re-declare them before calling `load_learner`:\\n\\n'\n",
310 | " err += args\n",
311 | " e.args = [err]\n",
312 | " raise e"
313 | ]
314 | },
315 | {
316 | "cell_type": "code",
317 | "execution_count": null,
318 | "metadata": {},
319 | "outputs": [],
320 | "source": [
321 | "#export\n",
322 | "def load_learner(fname, cpu=True, pickle_module=pickle):\n",
323 | " \"Load a `Learner` object in `fname`, optionally putting it on the `cpu`\"\n",
324 | " distrib_barrier()\n",
325 | " try: res = torch.load(fname, map_location='cpu' if cpu else None, pickle_module=pickle_module)\n",
326 | " except AttributeError as e: module_error(e)\n",
327 | " if hasattr(res, 'to_fp32'): res = res.to_fp32()\n",
328 | " if cpu: res.dls.cpu()\n",
329 | " return res"
330 | ]
331 | },
332 | {
333 | "cell_type": "markdown",
334 | "metadata": {},
335 | "source": [
336 | "We have a custom `load_learner` function here that can check if everything exported is available when bringing the model in, if not then it'll raise an explicit error"
337 | ]
338 | },
339 | {
340 | "cell_type": "code",
341 | "execution_count": null,
342 | "metadata": {},
343 | "outputs": [],
344 | "source": []
345 | }
346 | ],
347 | "metadata": {
348 | "kernelspec": {
349 | "display_name": "Python 3",
350 | "language": "python",
351 | "name": "python3"
352 | }
353 | },
354 | "nbformat": 4,
355 | "nbformat_minor": 2
356 | }
357 |
--------------------------------------------------------------------------------
/docs/index.html:
--------------------------------------------------------------------------------
1 | ---
2 |
3 | title: fastdebug
4 |
5 |
6 | keywords: fastai
7 | sidebar: home_sidebar
8 |
9 | summary: "A helpful library for improving torch and fastai errors"
10 | description: "A helpful library for improving torch and fastai errors"
11 | nb_path: "index.ipynb"
12 | ---
13 |
22 |
23 |
24 |
25 | {% raw %}
26 |
27 |
28 |
29 |
30 | {% endraw %}
31 |
32 |
38 |
39 |
40 | pip install fastdebug
41 |
42 |
43 |
44 |
45 |
51 |
52 |
53 | fastdebug is designed around improving the quality of life when dealing with Pytorch and fastai errors, while also including some new sanity checks (fastai only)
54 |
55 |
56 |
57 |
58 |
59 |
60 | Pytorch
Pytorch now has:
61 |
62 | device_error
63 | layer_error
64 |
65 | Both can be imported with:
66 | from fastdebug.error.torch import device_error, layer_error
67 |
68 | device_error prints out a much more readable error for when two tensors aren't on the same device:
69 |
70 |
71 |
72 |
73 |
74 |
75 | inp = torch.rand().cuda()
76 | model = model.cpu()
77 | try:
78 | _ = model(inp)
79 | except Exception as e:
80 | device_error(e, 'Input type', 'Model weights')
81 |
82 | And our new log:
83 | ---------------------------------------------------------------------------
84 | RuntimeError Traceback (most recent call last)
85 | <ipython-input-28-981e0ace9c38> in <module>()
86 | 2 model(x)
87 | 3 except Exception as e:
88 | ----> 4 device_error(e, 'Input type', 'Model weights')
89 |
90 | 10 frames
91 | /usr/local/lib/python3.7/dist-packages/torch/tensor.py in __torch_function__(cls, func, types, args, kwargs)
92 | 993
93 | 994 with _C.DisableTorchFunction():
94 | --> 995 ret = func(*args, **kwargs)
95 | 996 return _convert(ret, cls)
96 | 997
97 |
98 | RuntimeError: Mismatch between weight types
99 |
100 | Input type has type: (torch.cuda.FloatTensor)
101 | Model weights have type: (torch.FloatTensor)
102 |
103 | Both should be the same.
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 | And with layer_error, if there is a shape mismatch it will attempt to find the right layer it was at:
112 | inp = torch.rand(5,2, 3)
113 | try:
114 | m(inp)
115 | except Exception as e:
116 | layer_error(e, m)
117 |
118 | ---------------------------------------------------------------------------
119 | RuntimeError Traceback (most recent call last)
120 | <ipython-input-84-d4ab91131841> in <module>()
121 | 3 m(inp)
122 | 4 except Exception as e:
123 | ----> 5 layer_error(e, m)
124 |
125 | <ipython-input-83-ca2dc02cfff4> in layer_error(e, model)
126 | 8 i, layer = get_layer_by_shape(model, shape)
127 | 9 e.args = [f'Size mismatch between input tensors and what the model expects\n\n{args}\n\tat layer {i}: {layer}']
128 | ---> 10 raise e
129 |
130 | <ipython-input-84-d4ab91131841> in <module>()
131 | 1 inp = torch.rand(5,2, 3)
132 | 2 try:
133 | ----> 3 m(inp)
134 | 4 except Exception as e:
135 | 5 layer_error(e, m)
136 |
137 | /mnt/d/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
138 | 725 result = self._slow_forward(*input, **kwargs)
139 | 726 else:
140 | --> 727 result = self.forward(*input, **kwargs)
141 | 728 for hook in itertools.chain(
142 | 729 _global_forward_hooks.values(),
143 |
144 | /mnt/d/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
145 | 115 def forward(self, input):
146 | 116 for module in self:
147 | --> 117 input = module(input)
148 | 118 return input
149 | 119
150 |
151 | /mnt/d/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
152 | 725 result = self._slow_forward(*input, **kwargs)
153 | 726 else:
154 | --> 727 result = self.forward(*input, **kwargs)
155 | 728 for hook in itertools.chain(
156 | 729 _global_forward_hooks.values(),
157 |
158 | /mnt/d/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
159 | 421
160 | 422 def forward(self, input: Tensor) -> Tensor:
161 | --> 423 return self._conv_forward(input, self.weight)
162 | 424
163 | 425 class Conv3d(_ConvNd):
164 |
165 | /mnt/d/lib/python3.7/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
166 | 418 _pair(0), self.dilation, self.groups)
167 | 419 return F.conv2d(input, weight, self.bias, self.stride,
168 | --> 420 self.padding, self.dilation, self.groups)
169 | 421
170 | 422 def forward(self, input: Tensor) -> Tensor:
171 |
172 | RuntimeError: Size mismatch between input tensors and what the model expects
173 |
174 | Model expected 4-dimensional input for 4-dimensional weight [3, 3, 1, 1], but got 3-dimensional input of size [5, 2, 3] instead
175 | at layer 1: Conv2d(3, 3, kernel_size=(1, 1), stride=(1, 1))
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 | fastai
Along with the additions above (and are used during fit), fastai now has a Learner.sanity_check function, which allows you to quickly perform a basic check to ensure that your call to fit won't raise any exceptions. They are performed on the CPU for a partial epoch to make sure that CUDA device-assist errors can be preemptively found.
184 | To use it simply do:
185 | from fastdebug.fastai import *
186 | from fastai.vision.all import *
187 |
188 | learn = Learner(...)
189 | learn.sanity_check()
190 |
191 | This is also now an argument in Learner, set to False by default, so that after making your Learner a quick check is ensured.
192 |
193 |
194 |
195 |
196 |
197 |
198 | learn = Learner(..., sanity_check=True)
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
--------------------------------------------------------------------------------
/00_torch.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": null,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "# default_exp torch"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "# Pytorch Errors\n",
17 | "\n",
18 | "> All the possible errors that fastdebug can support and verbosify involving Pytorch"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": null,
24 | "metadata": {},
25 | "outputs": [],
26 | "source": [
27 | "#hide\n",
28 | "from nbdev.showdoc import *\n",
29 | "from fastcore.test import test_eq"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {},
36 | "outputs": [
37 | {
38 | "name": "stderr",
39 | "output_type": "stream",
40 | "text": [
41 | "/mnt/d/lib/python3.7/site-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at /pytorch/c10/cuda/CUDAFunctions.cpp:100.)\n",
42 | " return torch._C._cuda_getDeviceCount() > 0\n"
43 | ]
44 | }
45 | ],
46 | "source": [
47 | "#export\n",
48 | "import torch\n",
49 | "import re\n",
50 | "from fastai.callback.hook import Hook\n",
51 | "from fastai.torch_core import to_detach\n",
52 | "from fastai.layers import flatten_model\n",
53 | "\n",
54 | "from fastcore.basics import store_attr"
55 | ]
56 | },
57 | {
58 | "cell_type": "markdown",
59 | "metadata": {},
60 | "source": [
61 | "## Errors\n",
62 | "\n",
63 | "While some errrors are specifically designed for the [fastai](https://docs.fast.ai) library, the general idea still holds true in raw `Pytorch` as well. "
64 | ]
65 | },
66 | {
67 | "cell_type": "code",
68 | "execution_count": null,
69 | "metadata": {},
70 | "outputs": [],
71 | "source": [
72 | "#export\n",
73 | "def device_error(e:Exception, a:str, b:str) -> Exception:\n",
74 | " \"\"\"\n",
75 | " Verbose error for if `a` and `b` are on different devices\n",
76 | " Should be used when checking if a model is on the same device, or two tensors\n",
77 | " \"\"\"\n",
78 | " inp, weight, _ = e.args[0].replace('( ', '').split(')')\n",
79 | " inp = inp.replace('Input type', f'{a} has type: \\t\\t')\n",
80 | " weight = weight.replace(' and weight type', f'{b} have type: \\t')\n",
81 | " err = f'Mismatch between weight types\\n\\n{inp})\\n{weight})\\n\\nBoth should be the same.'\n",
82 | " e.args = [err]\n",
83 | " raise e"
84 | ]
85 | },
86 | {
87 | "cell_type": "markdown",
88 | "metadata": {},
89 | "source": [
90 | "The device error provides a much more readable error when `a` and `b` were on two different devices. An situation is below:\n",
91 | "```python\n",
92 | "inp = torch.rand().cuda()\n",
93 | "model = model.cpu()\n",
94 | "try:\n",
95 | " _ = model(inp)\n",
96 | "except Exception as e:\n",
97 | " device_error(e, 'Input type', 'Model weights')\n",
98 | "```\n",
99 | "And our new log:\n",
100 | "```bash\n",
101 | "---------------------------------------------------------------------------\n",
102 | "RuntimeError Traceback (most recent call last)\n",
103 | " in ()\n",
104 | " 2 model(x)\n",
105 | " 3 except Exception as e:\n",
106 | "----> 4 device_error(e, 'Input type', 'Model weights')\n",
107 | "\n",
108 | "10 frames\n",
109 | "/usr/local/lib/python3.7/dist-packages/torch/tensor.py in __torch_function__(cls, func, types, args, kwargs)\n",
110 | " 993 \n",
111 | " 994 with _C.DisableTorchFunction():\n",
112 | "--> 995 ret = func(*args, **kwargs)\n",
113 | " 996 return _convert(ret, cls)\n",
114 | " 997 \n",
115 | "\n",
116 | "RuntimeError: Mismatch between weight types\n",
117 | "\n",
118 | "Input type has type: \t\t (torch.cuda.FloatTensor)\n",
119 | "Model weights have type: \t (torch.FloatTensor)\n",
120 | "\n",
121 | "Both should be the same.\n",
122 | "```"
123 | ]
124 | },
125 | {
126 | "cell_type": "code",
127 | "execution_count": null,
128 | "metadata": {},
129 | "outputs": [],
130 | "source": [
131 | "#export\n",
132 | "def hook_fn(m, i):\n",
133 | " \"Simple hook fn to return the layer\"\n",
134 | " return m"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": null,
140 | "metadata": {},
141 | "outputs": [],
142 | "source": [
143 | "#export\n",
144 | "class PreHook(Hook):\n",
145 | " \"Creates and registers a hook on `m` with `hook_func` as a forward pre_hook\"\n",
146 | " def __init__(self, m, hook_func, is_forward=True, detach=True, cpu=False, gather=False):\n",
147 | " store_attr('hook_func,detach,cpu,gather')\n",
148 | " f = m.register_forward_pre_hook if is_forward else m.register_backward_pre_hook\n",
149 | " self.hook = f(self.hook_fn)\n",
150 | " self.stored,self.removed = None, False\n",
151 | "\n",
152 | " def hook_fn(self, module, inp):\n",
153 | " \"Applies `hook_fn` to `module` and `inp`\"\n",
154 | " if self.detach:\n",
155 | " inp = to_detach(inp, cpu=self.cpu, gather=self.gather)\n",
156 | " self.stored = self.hook_func(module, inp)"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": null,
162 | "metadata": {},
163 | "outputs": [],
164 | "source": [
165 | "#export\n",
166 | "class ForwardHooks():\n",
167 | " \"Create several forward-hooks on the modules in `ms` with `hook_func`\"\n",
168 | " def __init__(self, ms, hook_func, is_forward=True, detach=True, cpu=False):\n",
169 | " self.hooks = []\n",
170 | " for i, m in enumerate(flatten_model(ms)):\n",
171 | " self.hooks.append(PreHook(m, hook_func, is_forward, detach, cpu))"
172 | ]
173 | },
174 | {
175 | "cell_type": "code",
176 | "execution_count": null,
177 | "metadata": {},
178 | "outputs": [],
179 | "source": [
180 | "#export\n",
181 | "def hook_outputs(modules, detach=True, cpu=False, grad=False):\n",
182 | " \"Return `Hooks` that store activations of all `modules` in `self.stored`\"\n",
183 | " return ForwardHooks(modules, hook_fn, detach=detach, cpu=cpu, is_forward=not grad)"
184 | ]
185 | },
186 | {
187 | "cell_type": "markdown",
188 | "metadata": {},
189 | "source": [
190 | "By using forward hooks, we can locate our problem layers when they arrive rather than trying to figure out which one it is through a list of confusing errors.\n",
191 | "\n",
192 | "For this tutorial and testing we'll purposefully write a broken model:"
193 | ]
194 | },
195 | {
196 | "cell_type": "code",
197 | "execution_count": null,
198 | "metadata": {},
199 | "outputs": [],
200 | "source": [
201 | "from torch import nn\n",
202 | "m = nn.Sequential(\n",
203 | " nn.Conv2d(3,3,1),\n",
204 | " nn.ReLU(),\n",
205 | " nn.Linear(3,2)\n",
206 | ")"
207 | ]
208 | },
209 | {
210 | "cell_type": "code",
211 | "execution_count": null,
212 | "metadata": {},
213 | "outputs": [],
214 | "source": [
215 | "#export\n",
216 | "def layer_error(e:Exception, model, *inp) -> Exception:\n",
217 | " \"\"\"\n",
218 | " Verbose error for when there is a size mismatch between some input and the model. \n",
219 | " `model` should be any torch model\n",
220 | " `inp` is the input that went to the model\n",
221 | " \"\"\"\n",
222 | " args = e.args[0].replace(\"Expected\", \"Model expected\")\n",
223 | " hooks = hook_outputs(model)\n",
224 | " try:\n",
225 | " _ = model(*inp)\n",
226 | " except:\n",
227 | " pass\n",
228 | " finally:\n",
229 | " layers,num = [], 0\n",
230 | " for i, layer in enumerate(hooks.hooks):\n",
231 | " if layer.stored is not None: \n",
232 | " layers.append(layer.stored)\n",
233 | " num += 1\n",
234 | " layer = layers[-1]\n",
235 | " [h.remove() for h in hooks.hooks]\n",
236 | " e.args = [f'Size mismatch between input tensors and what the model expects\\n{\"-\"*76}\\nLayer: {i}, {layer}\\nError: {args}']\n",
237 | " raise e"
238 | ]
239 | },
240 | {
241 | "cell_type": "markdown",
242 | "metadata": {},
243 | "source": [
244 | "`layer_error` can be used anywhere that you want to check that the inputs are right for some model.\n",
245 | "\n",
246 | "Let's use our `m` model from earlier to show an example:"
247 | ]
248 | },
249 | {
250 | "cell_type": "code",
251 | "execution_count": null,
252 | "metadata": {},
253 | "outputs": [
254 | {
255 | "ename": "RuntimeError",
256 | "evalue": "Size mismatch between input tensors and what the model expects\n----------------------------------------------------------------------------\nLayer: 2, Conv2d(3, 3, kernel_size=(1, 1), stride=(1, 1))\nError: Model expected 4-dimensional input for 4-dimensional weight [3, 3, 1, 1], but got 3-dimensional input of size [5, 2, 3] instead",
257 | "output_type": "error",
258 | "traceback": [
259 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
260 | "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
261 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mlayer_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
262 | "\u001b[0;32m\u001b[0m in \u001b[0;36mlayer_error\u001b[0;34m(e, model, *inp)\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mh\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mh\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mhooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhooks\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34mf'Size mismatch between input tensors and what the model expects\\n{\"-\"*76}\\nLayer: {i}, {layer}\\nError: {args}'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
263 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0minp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrand\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mm\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mlayer_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
264 | "\u001b[0;32m/mnt/d/lib/python3.7/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 726\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 727\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 728\u001b[0m for hook in itertools.chain(\n\u001b[1;32m 729\u001b[0m \u001b[0m_global_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
265 | "\u001b[0;32m/mnt/d/lib/python3.7/site-packages/torch/nn/modules/container.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 115\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 116\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mmodule\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 117\u001b[0;31m \u001b[0minput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 118\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 119\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
266 | "\u001b[0;32m/mnt/d/lib/python3.7/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 726\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 727\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 728\u001b[0m for hook in itertools.chain(\n\u001b[1;32m 729\u001b[0m \u001b[0m_global_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
267 | "\u001b[0;32m/mnt/d/lib/python3.7/site-packages/torch/nn/modules/conv.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 421\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 422\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 423\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_conv_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweight\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 424\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 425\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mConv3d\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_ConvNd\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
268 | "\u001b[0;32m/mnt/d/lib/python3.7/site-packages/torch/nn/modules/conv.py\u001b[0m in \u001b[0;36m_conv_forward\u001b[0;34m(self, input, weight)\u001b[0m\n\u001b[1;32m 418\u001b[0m _pair(0), self.dilation, self.groups)\n\u001b[1;32m 419\u001b[0m return F.conv2d(input, weight, self.bias, self.stride,\n\u001b[0;32m--> 420\u001b[0;31m self.padding, self.dilation, self.groups)\n\u001b[0m\u001b[1;32m 421\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 422\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
269 | "\u001b[0;31mRuntimeError\u001b[0m: Size mismatch between input tensors and what the model expects\n----------------------------------------------------------------------------\nLayer: 2, Conv2d(3, 3, kernel_size=(1, 1), stride=(1, 1))\nError: Model expected 4-dimensional input for 4-dimensional weight [3, 3, 1, 1], but got 3-dimensional input of size [5, 2, 3] instead"
270 | ]
271 | }
272 | ],
273 | "source": [
274 | "#failing\n",
275 | "inp = torch.rand(5,2, 3)\n",
276 | "try:\n",
277 | " m(inp)\n",
278 | "except Exception as e:\n",
279 | " layer_error(e, m, inp)"
280 | ]
281 | },
282 | {
283 | "cell_type": "markdown",
284 | "metadata": {},
285 | "source": [
286 | "This will also work with multi-input and multi-output models:"
287 | ]
288 | },
289 | {
290 | "cell_type": "code",
291 | "execution_count": null,
292 | "metadata": {},
293 | "outputs": [],
294 | "source": [
295 | "class DoubleInputModel(nn.Sequential):\n",
296 | " def __init__(self):\n",
297 | " super().__init__()\n",
298 | " self.layers = nn.Sequential(nn.Conv2d(3,3,1),\n",
299 | " nn.ReLU(),\n",
300 | " nn.Linear(3,2))\n",
301 | " def forward(self, a, b):\n",
302 | " return self.layers(a), self.layers(b)"
303 | ]
304 | },
305 | {
306 | "cell_type": "code",
307 | "execution_count": null,
308 | "metadata": {},
309 | "outputs": [],
310 | "source": [
311 | "model = DoubleInputModel()"
312 | ]
313 | },
314 | {
315 | "cell_type": "code",
316 | "execution_count": null,
317 | "metadata": {},
318 | "outputs": [
319 | {
320 | "ename": "RuntimeError",
321 | "evalue": "Size mismatch between input tensors and what the model expects\n----------------------------------------------------------------------------\nLayer: 2, Conv2d(3, 3, kernel_size=(1, 1), stride=(1, 1))\nError: Model expected 4-dimensional input for 4-dimensional weight [3, 3, 1, 1], but got 3-dimensional input of size [5, 2, 3] instead",
322 | "output_type": "error",
323 | "traceback": [
324 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
325 | "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
326 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mlayer_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
327 | "\u001b[0;32m\u001b[0m in \u001b[0;36mlayer_error\u001b[0;34m(e, model, *inp)\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mh\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mh\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mhooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhooks\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34mf'Size mismatch between input tensors and what the model expects\\n{\"-\"*76}\\nLayer: {i}, {layer}\\nError: {args}'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
328 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0minp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrand\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mlayer_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
329 | "\u001b[0;32m/mnt/d/lib/python3.7/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 726\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 727\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 728\u001b[0m for hook in itertools.chain(\n\u001b[1;32m 729\u001b[0m \u001b[0m_global_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
330 | "\u001b[0;32m\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, a, b)\u001b[0m\n\u001b[1;32m 6\u001b[0m nn.Linear(3,2))\n\u001b[1;32m 7\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlayers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlayers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
331 | "\u001b[0;32m/mnt/d/lib/python3.7/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 726\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 727\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 728\u001b[0m for hook in itertools.chain(\n\u001b[1;32m 729\u001b[0m \u001b[0m_global_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
332 | "\u001b[0;32m/mnt/d/lib/python3.7/site-packages/torch/nn/modules/container.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 115\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 116\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mmodule\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 117\u001b[0;31m \u001b[0minput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 118\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 119\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
333 | "\u001b[0;32m/mnt/d/lib/python3.7/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 726\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 727\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 728\u001b[0m for hook in itertools.chain(\n\u001b[1;32m 729\u001b[0m \u001b[0m_global_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
334 | "\u001b[0;32m/mnt/d/lib/python3.7/site-packages/torch/nn/modules/conv.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 421\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 422\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 423\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_conv_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweight\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 424\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 425\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mConv3d\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_ConvNd\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
335 | "\u001b[0;32m/mnt/d/lib/python3.7/site-packages/torch/nn/modules/conv.py\u001b[0m in \u001b[0;36m_conv_forward\u001b[0;34m(self, input, weight)\u001b[0m\n\u001b[1;32m 418\u001b[0m _pair(0), self.dilation, self.groups)\n\u001b[1;32m 419\u001b[0m return F.conv2d(input, weight, self.bias, self.stride,\n\u001b[0;32m--> 420\u001b[0;31m self.padding, self.dilation, self.groups)\n\u001b[0m\u001b[1;32m 421\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 422\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
336 | "\u001b[0;31mRuntimeError\u001b[0m: Size mismatch between input tensors and what the model expects\n----------------------------------------------------------------------------\nLayer: 2, Conv2d(3, 3, kernel_size=(1, 1), stride=(1, 1))\nError: Model expected 4-dimensional input for 4-dimensional weight [3, 3, 1, 1], but got 3-dimensional input of size [5, 2, 3] instead"
337 | ]
338 | }
339 | ],
340 | "source": [
341 | "#failing\n",
342 | "inp = torch.rand(5,2, 3)\n",
343 | "try:\n",
344 | " model(inp, inp)\n",
345 | "except Exception as e:\n",
346 | " layer_error(e, model, inp, inp)"
347 | ]
348 | },
349 | {
350 | "cell_type": "markdown",
351 | "metadata": {},
352 | "source": [
353 | "Much more readable!"
354 | ]
355 | }
356 | ],
357 | "metadata": {
358 | "kernelspec": {
359 | "display_name": "Python 3",
360 | "language": "python",
361 | "name": "python3"
362 | }
363 | },
364 | "nbformat": 4,
365 | "nbformat_minor": 2
366 | }
367 |
--------------------------------------------------------------------------------
/01_fastai.dataloader.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": null,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "#default_exp fastai.dataloader"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "# DataLoader Errors\n",
17 | "> Errors and exceptions for any step of the `DataLoader` process"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "This includes `after_item`, `after_batch`, and collating. Anything in relation to the `Datasets` or anything before the `DataLoader` process can be found in `fastdebug.fastai.dataset`"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": null,
30 | "metadata": {},
31 | "outputs": [],
32 | "source": [
33 | "#export\n",
34 | "import inflect\n",
35 | "from fastcore.basics import patch\n",
36 | "from fastai.data.core import TfmdDL\n",
37 | "from fastai.data.load import DataLoader, fa_collate, fa_convert"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": null,
43 | "metadata": {},
44 | "outputs": [],
45 | "source": [
46 | "#export\n",
47 | "def collate_error(e:Exception, batch):\n",
48 | " \"\"\"\n",
49 | " Raises an explicit error when the batch could not collate, stating\n",
50 | " what items in the batch are different sizes and their types\n",
51 | " \"\"\"\n",
52 | " p = inflect.engine()\n",
53 | " err = f'Error when trying to collate the data into batches with fa_collate, '\n",
54 | " err += 'at least two tensors in the batch are not the same size.\\n\\n'\n",
55 | " # we need to iterate through the entire batch and find a mismatch\n",
56 | " length = len(batch[0])\n",
57 | " for idx in range(length): # for each type in the batch\n",
58 | " for i, item in enumerate(batch):\n",
59 | " if i == 0:\n",
60 | " shape_a = item[idx].shape\n",
61 | " type_a = item[idx].__class__.__name__\n",
62 | " elif item[idx].shape != shape_a:\n",
63 | " shape_b = item[idx].shape\n",
64 | " if shape_a != shape_b:\n",
65 | " err += f'Mismatch found within the {p.ordinal(idx)} axis of the batch and is of type {type_a}:\\n'\n",
66 | " err += f'The first item has shape: {shape_a}\\n'\n",
67 | " err += f'The {p.number_to_words(p.ordinal(i+1))} item has shape: {shape_b}\\n\\n'\n",
68 | " err += f'Please include a transform in `after_item` that ensures all data of type {type_a} is the same size'\n",
69 | " e.args = [err]\n",
70 | " raise e"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": null,
76 | "metadata": {},
77 | "outputs": [],
78 | "source": [
79 | "#export\n",
80 | "@patch\n",
81 | "def create_batch(self:DataLoader, b):\n",
82 | " \"Collate a list of items into a batch.\"\n",
83 | " func = (fa_collate,fa_convert)[self.prebatched]\n",
84 | " try:\n",
85 | " return func(b)\n",
86 | " except Exception as e:\n",
87 | " if not self.prebatched:\n",
88 | " collate_error(e, b) \n",
89 | " else: raise e"
90 | ]
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "metadata": {},
95 | "source": [
96 | "`collate_error` is `@patch`'d into `DataLoader`'s `create_batch` function through importing this module, so if there is any possible reason why the data cannot be collated into the batch, it is presented to the user.\n",
97 | "\n",
98 | "An example is below, where we forgot to include an item transform that resizes all our images to the same size:"
99 | ]
100 | },
101 | {
102 | "cell_type": "code",
103 | "execution_count": null,
104 | "metadata": {},
105 | "outputs": [
106 | {
107 | "ename": "RuntimeError",
108 | "evalue": "ignored",
109 | "output_type": "error",
110 | "traceback": [
111 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
112 | "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
113 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m label_func=lambda x: x[0].isupper())\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mone_batch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
114 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/fastai/data/load.py\u001b[0m in \u001b[0;36mone_batch\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 148\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mone_batch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 149\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mn\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'This DataLoader does not contain any batches'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 150\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfake_l\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mno_multiproc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfirst\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 151\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'it'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mdelattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'it'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 152\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mres\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
115 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/fastcore/basics.py\u001b[0m in \u001b[0;36mfirst\u001b[0;34m(x, f, negate, **kwargs)\u001b[0m\n\u001b[1;32m 545\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0miter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 546\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfilter_ex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnegate\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnegate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgen\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 547\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 548\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 549\u001b[0m \u001b[0;31m# Cell\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
116 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/fastai/data/load.py\u001b[0m in \u001b[0;36m__iter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbefore_iter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 108\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__idxs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_idxs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# called in context of main process (not workers/subprocesses)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 109\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mb\u001b[0m \u001b[0;32min\u001b[0m \u001b[0m_loaders\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfake_l\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnum_workers\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfake_l\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 110\u001b[0m \u001b[0;31m# fix issue 2899. If the process start method isn't fork, the data will be copied to cuda in learner one_batch.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 111\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdevice\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mmultiprocessing\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_start_method\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"fork\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
117 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py\u001b[0m in \u001b[0;36m__next__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 433\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_sampler_iter\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 434\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_reset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 435\u001b[0;31m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_next_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 436\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_num_yielded\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 437\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_dataset_kind\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0m_DatasetKind\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mIterable\u001b[0m \u001b[0;32mand\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
118 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py\u001b[0m in \u001b[0;36m_next_data\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 473\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_next_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 474\u001b[0m \u001b[0mindex\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_next_index\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# may raise StopIteration\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 475\u001b[0;31m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_dataset_fetcher\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfetch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# may raise StopIteration\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 476\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_pin_memory\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 477\u001b[0m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_utils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpin_memory\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpin_memory\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
119 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py\u001b[0m in \u001b[0;36mfetch\u001b[0;34m(self, possibly_batched_index)\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mStopIteration\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 34\u001b[0;31m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdataset_iter\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 35\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcollate_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 36\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
120 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/fastai/data/load.py\u001b[0m in \u001b[0;36mcreate_batches\u001b[0;34m(self, samps)\u001b[0m\n\u001b[1;32m 118\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdataset\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mit\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0miter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdataset\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 119\u001b[0m \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfilter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mo\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mo\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdo_item\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msamps\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 120\u001b[0;31m \u001b[0;32myield\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdo_batch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchunkify\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mres\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 121\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 122\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mnew\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdataset\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
121 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/fastai/data/load.py\u001b[0m in \u001b[0;36mdo_batch\u001b[0;34m(self, b)\u001b[0m\n\u001b[1;32m 144\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mIndexError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Cannot index an iterable dataset numerically - must use `None`.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 145\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcreate_batch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mfa_collate\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mfa_convert\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprebatched\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 146\u001b[0;31m \u001b[0;32mdef\u001b[0m \u001b[0mdo_batch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mretain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcreate_batch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbefore_batch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 147\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdevice\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdevice\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdevice\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 148\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mone_batch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
122 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcreate_batch\u001b[0;34m(self, b)\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprebatched\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mcollate_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 11\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
123 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcollate_error\u001b[0;34m(e, batch)\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0merr\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;34mf'Please include a transform in `after_item` that ensures all data of type {type_a} is the same size'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
124 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcreate_batch\u001b[0;34m(self, b)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mfunc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mfa_collate\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mfa_convert\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprebatched\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprebatched\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
125 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/fastai/data/load.py\u001b[0m in \u001b[0;36mfa_collate\u001b[0;34m(t)\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m return (default_collate(t) if isinstance(b, _collate_types)\n\u001b[0;32m---> 50\u001b[0;31m \u001b[0;32melse\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mfa_collate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ms\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mSequence\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 51\u001b[0m else default_collate(t))\n\u001b[1;32m 52\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
126 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/fastai/data/load.py\u001b[0m in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m return (default_collate(t) if isinstance(b, _collate_types)\n\u001b[0;32m---> 50\u001b[0;31m \u001b[0;32melse\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mfa_collate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ms\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mSequence\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 51\u001b[0m else default_collate(t))\n\u001b[1;32m 52\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
127 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/fastai/data/load.py\u001b[0m in \u001b[0;36mfa_collate\u001b[0;34m(t)\u001b[0m\n\u001b[1;32m 47\u001b[0m \u001b[0;34m\"A replacement for PyTorch `default_collate` which maintains types and handles `Sequence`s\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 49\u001b[0;31m return (default_collate(t) if isinstance(b, _collate_types)\n\u001b[0m\u001b[1;32m 50\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mfa_collate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ms\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mSequence\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m else default_collate(t))\n",
128 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/collate.py\u001b[0m in \u001b[0;36mdefault_collate\u001b[0;34m(batch)\u001b[0m\n\u001b[1;32m 53\u001b[0m \u001b[0mstorage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0melem\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstorage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_new_shared\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0mout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0melem\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnew\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstorage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 55\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 56\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0melem_type\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__module__\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'numpy'\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0melem_type\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__name__\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;34m'str_'\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0melem_type\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__name__\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;34m'string_'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
129 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/fastai/torch_core.py\u001b[0m in \u001b[0;36m__torch_function__\u001b[0;34m(self, func, types, args, kwargs)\u001b[0m\n\u001b[1;32m 327\u001b[0m \u001b[0mconvert\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 328\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m_torch_handled\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_opt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mconvert\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtypes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 329\u001b[0;31m \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__torch_function__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtypes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 330\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mconvert\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mconvert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mres\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 331\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mres\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTensorBase\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mres\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_meta\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mas_copy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
130 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/tensor.py\u001b[0m in \u001b[0;36m__torch_function__\u001b[0;34m(cls, func, types, args, kwargs)\u001b[0m\n\u001b[1;32m 993\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 994\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0m_C\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDisableTorchFunction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 995\u001b[0;31m \u001b[0mret\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 996\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mret\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 997\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
131 | "\u001b[0;31mRuntimeError\u001b[0m: Error when trying to collate the data into batches with fa_collate, at least two tensors in the batch are not the same size.\n\nMismatch found within the 0th axis of the batch and is of type TensorImage:\nThe first item has shape: torch.Size([3, 500, 333])\nThe second item has shape: torch.Size([3, 333, 500])\n\nPlease include a transform in `after_item` that ensures all data of type TensorImage is the same size"
132 | ]
133 | }
134 | ],
135 | "source": [
136 | "#failing\n",
137 | "from fastai.vision.all import *\n",
138 | "path = untar_data(URLs.PETS)/'images'\n",
139 | "dls = ImageDataLoaders.from_name_func(\n",
140 | " path, get_image_files(path), valid_pct=0.2,\n",
141 | " label_func=lambda x: x[0].isupper())\n",
142 | "\n",
143 | "x,y = dls.train.one_batch()"
144 | ]
145 | },
146 | {
147 | "cell_type": "code",
148 | "execution_count": null,
149 | "metadata": {},
150 | "outputs": [],
151 | "source": [
152 | "#export\n",
153 | "@patch\n",
154 | "def new(self:TfmdDL, dataset=None, cls=None, **kwargs):\n",
155 | " res = super(TfmdDL, self).new(dataset, cls, do_setup=False, **kwargs)\n",
156 | " if not hasattr(self, '_n_inp') or not hasattr(self, '_types'):\n",
157 | " try:\n",
158 | " self._one_pass()\n",
159 | " res._n_inp,res._types = self._n_inp,self._types\n",
160 | " except Exception as e: \n",
161 | " print(\"Could not do one pass in your dataloader, there is something wrong in it\")\n",
162 | " raise e\n",
163 | " else: res._n_inp,res._types = self._n_inp,self._types\n",
164 | " return res"
165 | ]
166 | }
167 | ],
168 | "metadata": {
169 | "kernelspec": {
170 | "display_name": "Python 3",
171 | "language": "python",
172 | "name": "python3"
173 | }
174 | },
175 | "nbformat": 4,
176 | "nbformat_minor": 1
177 | }
178 |
--------------------------------------------------------------------------------
/docs/fastai.data.html:
--------------------------------------------------------------------------------
1 | ---
2 |
3 | title: DataLoader Errors
4 |
5 |
6 | keywords: fastai
7 | sidebar: home_sidebar
8 |
9 | summary: "Errors and exceptions for any step of the `DataLoader` process"
10 | description: "Errors and exceptions for any step of the `DataLoader` process"
11 | nb_path: "01_fastai.data.ipynb"
12 | ---
13 |
22 |
23 |
24 |
25 | {% raw %}
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | {% endraw %}
46 |
47 |
48 |
49 | This includes after_item, after_batch, and collating. Anything in relation to the Datasets or anything before the DataLoader process can be found in fastdebug.fastai.dataset
50 |
51 |
52 |
53 |
54 | {% raw %}
55 |
56 |
57 |
58 |
59 | {% endraw %}
60 |
61 | {% raw %}
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 | {% endraw %}
86 |
87 | {% raw %}
88 |
89 |
90 |
91 |
92 | {% endraw %}
93 |
94 | {% raw %}
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 | {% endraw %}
118 |
119 | {% raw %}
120 |
121 |
122 |
123 |
124 | {% endraw %}
125 |
126 |
127 |
128 | collate_error is @patch'd into DataLoader's create_batch function through importing this module, so if there is any possible reason why the data cannot be collated into the batch, it is presented to the user.
129 | An example is below, where we forgot to include an item transform that resizes all our images to the same size:
130 |
131 |
132 |
133 |
134 | {% raw %}
135 |
136 |
137 |
138 |
139 |
140 |
141 | from fastai.vision.all import *
142 | path = untar_data(URLs.PETS)/'images'
143 | dls = ImageDataLoaders.from_name_func(
144 | path, get_image_files(path), valid_pct=0.2,
145 | label_func=lambda x: x[0].isupper())
146 |
147 | x,y = dls.train.one_batch()
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
292 |
293 |
294 |
295 |
296 |
297 |
298 | {% endraw %}
299 |
300 | {% raw %}
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 | {% endraw %}
324 |
325 | {% raw %}
326 |
327 |
328 |
329 |
330 | {% endraw %}
331 |
332 |
333 |
334 |
335 |
--------------------------------------------------------------------------------