Skip to content

Commit 3b92f86

Browse files
cpuhrschfacebook-github-bot
authored andcommitted
[fbsync] Add illustrations of transforms with sphinx-gallery (#3652)
Reviewed By: NicolasHug Differential Revision: D28169127 fbshipit-source-id: 8233282c3b03a051f253f6a0447029ed9d85ac4b
1 parent b38ed08 commit 3b92f86

File tree

8 files changed

+140
-1
lines changed

8 files changed

+140
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ torchvision/version.py
1010
*/**/*~
1111
*~
1212
docs/build
13+
docs/source/auto_examples/
14+
docs/source/gen_modules/
1315
.coverage
1416
htmlcov
1517
.*.swp

docs/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ docset: html
1919
cp $(SPHINXPROJ).docset/icon.png $(SPHINXPROJ).docset/[email protected]
2020
convert $(SPHINXPROJ).docset/[email protected] -resize 16x16 $(SPHINXPROJ).docset/icon.png
2121

22+
html-noplot: # Avoids running the gallery examples, which may take time
23+
$(SPHINXBUILD) -D plot_gallery=0 -b html $(ASPHINXOPTS) "$(BUILDDIR)"/html
24+
@echo
25+
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
26+
27+
clean:
28+
rm -rf $(BUILDDIR)/*
29+
rm -rf auto_examples/
30+
2231
.PHONY: help Makefile docset
2332

2433
# Catch-all target: route all unknown targets to Sphinx using the new

docs/requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
sphinx
1+
sphinx==2.4.4
2+
sphinx-gallery
3+
matplotlib
4+
numpy
25
-e git+git://github.com/pytorch/pytorch_sphinx_theme.git#egg=pytorch_sphinx_theme

docs/source/conf.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
# import os
2121
# import sys
2222
# sys.path.insert(0, os.path.abspath('.'))
23+
24+
from pathlib import Path
25+
import os
26+
2327
import torch
2428
import torchvision
2529
import pytorch_sphinx_theme
@@ -44,8 +48,16 @@
4448
'sphinx.ext.mathjax',
4549
'sphinx.ext.napoleon',
4650
'sphinx.ext.viewcode',
51+
'sphinx_gallery.gen_gallery',
4752
]
4853

54+
sphinx_gallery_conf = {
55+
'examples_dirs': '../../gallery/', # path to your example scripts
56+
'gallery_dirs': 'auto_examples', # path to where to save gallery generated output
57+
'backreferences_dir': 'gen_modules/backreferences',
58+
'doc_module': ('torchvision',),
59+
}
60+
4961
napoleon_use_ivar = True
5062
napoleon_numpy_docstring = False
5163
napoleon_google_docstring = True
@@ -248,3 +260,42 @@ def handle_item(fieldarg, content):
248260

249261

250262
TypedField.make_field = patched_make_field
263+
264+
265+
def inject_minigalleries(app, what, name, obj, options, lines):
266+
"""Inject a minigallery into a docstring.
267+
268+
This avoids having to manually write the .. minigallery directive for every item we want a minigallery for,
269+
as it would be easy to miss some.
270+
271+
This callback is called after the .. auto directives (like ..autoclass) have been processed,
272+
and modifies the lines parameter inplace to add the .. minigallery that will show which examples
273+
are using which object.
274+
275+
It's a bit hacky, but not *that* hacky when you consider that the recommended way is to do pretty much the same,
276+
but instead with templates using autosummary (which we don't want to use):
277+
(https://sphinx-gallery.github.io/stable/configuration.html#auto-documenting-your-api-with-links-to-examples)
278+
279+
For docs on autodoc-process-docstring, see the autodoc docs:
280+
https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html
281+
"""
282+
283+
conf_file_path = Path(__file__).parent.absolute()
284+
backrefs_path = conf_file_path / sphinx_gallery_conf['backreferences_dir'] / (name + '.examples')
285+
if not (os.path.isfile(backrefs_path) and os.path.getsize(backrefs_path) > 0):
286+
# We avoid showing the (empty) minigallery if there's nothing to show, i.e. if the object
287+
# isn't used in any example.
288+
# FIXME: this check can be removed once https://github.com/sphinx-gallery/sphinx-gallery/pull/813
289+
# is merged and the new sphinx-gallery version (> 0.8.x) is released.
290+
return
291+
292+
if what in ("class", "function"):
293+
lines.append(f".. minigallery:: {name}")
294+
lines.append(f" :add-heading: Examples using ``{name.split('.')[-1]}``:")
295+
# avoid heading entirely to avoid warning. As a bonud it actually renders better
296+
lines.append(" :heading-level: 9")
297+
lines.append("\n")
298+
299+
300+
def setup(app):
301+
app.connect('autodoc-process-docstring', inject_minigalleries)

docs/source/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ architectures, and common image transformations for computer vision.
3838
transforms
3939
utils
4040

41+
.. toctree::
42+
:maxdepth: 1
43+
:caption: Examples
44+
45+
auto_examples/index
46+
4147
.. automodule:: torchvision
4248
:members:
4349

gallery/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Example gallery
2+
===============
3+
4+
Below is a gallery of examples

gallery/assets/astronaut.jpg

39.4 KB
Loading

gallery/plot_transforms.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
==========================
3+
Illustration of transforms
4+
==========================
5+
6+
This example illustrates the various transforms available in :mod:`torchvision.transforms`.
7+
"""
8+
9+
from PIL import Image
10+
from pathlib import Path
11+
import matplotlib.pyplot as plt
12+
import numpy as np
13+
14+
import torchvision.transforms as T
15+
16+
17+
orig_img = Image.open(Path('assets') / 'astronaut.jpg')
18+
19+
20+
def plot(img, title="", with_orig=True, **kwargs):
21+
def _plot(img, title, **kwargs):
22+
plt.figure().suptitle(title, fontsize=25)
23+
plt.imshow(np.asarray(img), **kwargs)
24+
plt.axis('off')
25+
26+
if with_orig:
27+
_plot(orig_img, "Original image")
28+
_plot(img, title, **kwargs)
29+
30+
31+
####################################
32+
# Pad
33+
# ---
34+
# The :class:`~torchvision.transforms.Pad` transform
35+
# (see also :func:`~torchvision.transforms.functional.pad`)
36+
# fills image borders with some pixel values.
37+
padded_img = T.Pad(padding=30)(orig_img)
38+
plot(padded_img, "Padded image")
39+
40+
####################################
41+
# Resize
42+
# ------
43+
# The :class:`~torchvision.transforms.Resize` transform
44+
# (see also :func:`~torchvision.transforms.functional.resize`)
45+
# resizes an image.
46+
resized_img = T.Resize(size=30)(orig_img)
47+
plot(resized_img, "Resized image")
48+
49+
####################################
50+
# ColorJitter
51+
# -----------
52+
# The :class:`~torchvision.transforms.ColorJitter` transform
53+
# randomly changes the brightness, saturation, and other properties of an image.
54+
jitted_img = T.ColorJitter(brightness=.5, hue=.3)(orig_img)
55+
plot(jitted_img, "Jitted image")
56+
57+
####################################
58+
# Grayscale
59+
# ---------
60+
# The :class:`~torchvision.transforms.Grayscale` transform
61+
# (see also :func:`~torchvision.transforms.functional.to_grayscale`)
62+
# converts an image to grayscale
63+
gray_img = T.Grayscale()(orig_img)
64+
plot(gray_img, "Grayscale image", cmap='gray')

0 commit comments

Comments
 (0)