CONTRIBUTING.md 8.63 KB
Newer Older
vfdev's avatar
vfdev committed
1
2
3
4
5
6
# Contributing to Torchvision

We want to make contributing to this project as easy and transparent as possible.

## TL;DR

limm's avatar
limm committed
7
We appreciate all contributions. If you are interested in contributing to Torchvision, there are many ways to help out.
vfdev's avatar
vfdev committed
8
9
Your contributions may fall into the following categories:

limm's avatar
limm committed
10
- It helps the project if you could
vfdev's avatar
vfdev committed
11
    - Report issues you're facing
limm's avatar
limm committed
12
    - Give a :+1: on issues that others reported and that are relevant to you
vfdev's avatar
vfdev committed
13
14
15

- Answering queries on the issue tracker, investigating bugs are very valuable contributions to the project.

limm's avatar
limm committed
16
- You would like to improve the documentation. This is no less important than improving the library itself!
vfdev's avatar
vfdev committed
17
18
19
20
21
If you find a typo in the documentation, do not hesitate to submit a GitHub pull request.

- If you would like to fix a bug
    - please pick one from the [list of open issues labelled as "help wanted"](https://github.com/pytorch/vision/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22)
    - comment on the issue that you want to work on this issue
limm's avatar
limm committed
22
    - send a PR with your fix, see below.
vfdev's avatar
vfdev committed
23
24
25
26
27
28
29
30
31
32

- If you plan to contribute new features, utility functions or extensions, please first open an issue and discuss the feature with us.

## Issues

We use GitHub issues to track public bugs. Please ensure your description is
clear and has sufficient instructions to be able to reproduce the issue.

## Development installation

limm's avatar
limm committed
33
34
35
36
37
38
39
40
41
42
43
44

### Dependencies

Start by installing the **nightly** build of PyTorch following the [official
instructions](https://pytorch.org/get-started/locally/). Note that the official
instructions may ask you to install torchvision itself. If you are doing development
on torchvision, you should not install prebuilt torchvision packages.

**Optionally**, install `libpng` and `libjpeg-turbo` if you want to enable
support for
native encoding / decoding of PNG and JPEG formats in
[torchvision.io](https://pytorch.org/vision/stable/io.html#image):
vfdev's avatar
vfdev committed
45
46

```bash
limm's avatar
limm committed
47
conda install libpng libjpeg-turbo -c pytorch
vfdev's avatar
vfdev committed
48
49
```

limm's avatar
limm committed
50
51
52
53
54
55
56
Note: you can use the `TORCHVISION_INCLUDE` and `TORCHVISION_LIBRARY`
environment variables to tell the build system where to find those libraries if
they are in specific locations. Take a look at
[setup.py](https://github.com/pytorch/vision/blob/main/setup.py) for more
details.

### Clone and install torchvision
vfdev's avatar
vfdev committed
57
58
59
60

```bash
git clone https://github.com/pytorch/vision.git
cd vision
limm's avatar
limm committed
61
python setup.py develop  # use install instead of develop if you don't care about development.
vfdev's avatar
vfdev committed
62
# or, for OSX
63
# MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py develop
limm's avatar
limm committed
64
# for C++ debugging, use DEBUG=1
65
# DEBUG=1 python setup.py develop
vfdev's avatar
vfdev committed
66
```
limm's avatar
limm committed
67
68
69
70
71
72
73
74
75
76
77

By default, GPU support is built if CUDA is found and `torch.cuda.is_available()` is true. It's possible to force
building GPU support by setting `FORCE_CUDA=1` environment variable, which is useful when building a docker image.

We don't officially support building from source using `pip`, but _if_ you do, you'll need to use the
`--no-build-isolation` flag.

#### Other development dependencies (some of these are needed to run tests):

```
pip install expecttest flake8 typing mypy pytest pytest-mock scipy requests
vfdev's avatar
vfdev committed
78
79
80
81
82
83
```

## Development Process

If you plan to modify the code or documentation, please follow the steps below:

limm's avatar
limm committed
84
1. Fork the repository and create your branch from `main`.
vfdev's avatar
vfdev committed
85
86
87
2. If you have modified the code (new feature or bug-fix), please add unit tests.
3. If you have changed APIs, update the documentation. Make sure the documentation builds.
4. Ensure the test suite passes.
limm's avatar
limm committed
88
5. Make sure your code passes the formatting checks (see below).
vfdev's avatar
vfdev committed
89

limm's avatar
limm committed
90
91
For more details about pull requests,
please read [GitHub's guides](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request).
vfdev's avatar
vfdev committed
92

limm's avatar
limm committed
93
If you would like to contribute a new model, please see [here](#New-architecture-or-improved-model-weights).
vfdev's avatar
vfdev committed
94

limm's avatar
limm committed
95
If you would like to contribute a new dataset, please see [here](#New-dataset).
vfdev's avatar
vfdev committed
96
97
98

### Code formatting and typing

limm's avatar
limm committed
99
100
101
102
103
104
105
106
107
108
#### Formatting

The torchvision code is formatted by [black](https://black.readthedocs.io/en/stable/),
and checked against pep8 compliance with [flake8](https://flake8.pycqa.org/en/latest/).
Instead of relying directly on `black` however, we rely on
[ufmt](https://github.com/omnilib/ufmt), for compatibility reasons with Facebook
internal infrastructure.

To format your code, install `ufmt` with `pip install ufmt==1.3.3 black==22.3.0 usort==1.0.2` and use e.g.:

vfdev's avatar
vfdev committed
109
```bash
limm's avatar
limm committed
110
ufmt format torchvision
vfdev's avatar
vfdev committed
111
112
```

limm's avatar
limm committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
For the vast majority of cases, this is all you should need to run. For the
formatting to be a bit faster, you can also choose to only apply `ufmt` to the
files that were edited in your PR with e.g.:

```bash
ufmt format `git diff main --name-only`
```

Similarly, you can check for `flake8` errors with `flake8 torchvision`, although
they should be fairly rare considering that most of the errors are automatically
taken care of by `ufmt` already.

##### Pre-commit hooks

For convenience and **purely optionally**, you can rely on [pre-commit
hooks](https://pre-commit.com/) which will run both `ufmt` and `flake8` prior to
every commit.

First install the `pre-commit` package with `pip install pre-commit`, and then
run `pre-commit install` at the root of the repo for the hooks to be set up -
that's it.

Feel free to read the [pre-commit docs](https://pre-commit.com/#usage) to learn
more and improve your workflow. You'll see for example that `pre-commit run
--all-files` will run both `ufmt` and `flake8` without the need for you to
commit anything, and that the `--no-verify` flag can be added to `git commit` to
temporarily deactivate the hooks.

#### Type annotations

vfdev's avatar
vfdev committed
143
144
145
146
147
148
149
The codebase has type annotations, please make sure to add type hints if required. We use `mypy` tool for type checking:
```bash
mypy --config-file mypy.ini
```

### Unit tests

limm's avatar
limm committed
150
151
152
153
Before running tests make sure to install [test dependencies](#other-development-dependencies-some-of-these-are-needed-to-run-tests).

If you have modified the code by adding a new feature or a bug-fix, please add unit tests for that. To run a specific
test:
vfdev's avatar
vfdev committed
154
155
```bash
pytest test/<test-module.py> -vvv -k <test_myfunc>
156
# e.g. pytest test/test_transforms.py -vvv -k test_center_crop
vfdev's avatar
vfdev committed
157
158
159
160
161
```

If you would like to run all tests:
```bash
pytest test -vvv
limm's avatar
limm committed
162
```
vfdev's avatar
vfdev committed
163

164
165
166
Tests that require internet access should be in
`test/test_internet.py`.

vfdev's avatar
vfdev committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
### Documentation

Torchvision uses [Google style](http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html)
for formatting docstrings. Length of line inside docstrings block must be limited to 120 characters.

Please, follow the instructions to build and deploy the documentation locally.

#### Install requirements

```bash
cd docs
pip install -r requirements.txt
```

#### Build

```bash
cd docs
limm's avatar
limm committed
185
make html-noplot
vfdev's avatar
vfdev committed
186
187
```

Nicolas Hug's avatar
Nicolas Hug committed
188
Then open `docs/build/html/index.html` in your favorite browser.
vfdev's avatar
vfdev committed
189

190
The docs are also automatically built when you submit a PR. The job that
191
192
builds the docs is named `build_docs`. You can access the rendered docs by
clicking on that job and then going to the "Artifacts" tab.
193

194
195
196
197
198
You can clean the built docs and re-start the build from scratch by doing ``make
clean``.

#### Building the example gallery - or not

limm's avatar
limm committed
199
200
201
202
In most cases, running `make html-noplot` is enough to build the docs for your
specific use-case. The `noplot` part tells sphinx **not** to build the examples
in the [gallery](https://pytorch.org/vision/stable/auto_examples/index.html),
which saves a lot of building time.
203

limm's avatar
limm committed
204
205
If you need to build all the examples in the gallery, then you can use `make
html`.
206
207
208
209
210
211

You can also choose to only build a subset of the examples by using the
``EXAMPLES_PATTERN`` env variable, which accepts a regular expression. For
example ``EXAMPLES_PATTERN="transforms" make html`` will only build the examples
with "transforms" in their name.

limm's avatar
limm committed
212
213
214
### New architecture or improved model weights

Please refer to the guidelines in [Contributing to Torchvision - Models](https://github.com/pytorch/vision/blob/main/CONTRIBUTING_MODELS.md).
vfdev's avatar
vfdev committed
215
216
217

### New dataset

limm's avatar
limm committed
218
Please, do not send any PR with a new dataset without discussing
vfdev's avatar
vfdev committed
219
220
221
222
it in an issue as, most likely, it will not be accepted.

### Pull Request

limm's avatar
limm committed
223
224
If all previous checks (flake8, mypy, unit tests) are passing, please send a PR. Submitted PR will pass other tests on
different operating systems, python versions and hardware.
vfdev's avatar
vfdev committed
225

limm's avatar
limm committed
226
For more details about pull requests workflow,
vfdev's avatar
vfdev committed
227
228
229
230
231
232
please read [GitHub's guides](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request).

## License

By contributing to Torchvision, you agree that your contributions will be licensed
under the LICENSE file in the root directory of this source tree.
limm's avatar
limm committed
233
234

Contributors are also required to [sign our Contributor License Agreement](https://code.facebook.com/cla).