Installing pytorch (and torchvision) via requirements.txt file is simple enough. We just specify it in the file as below.

torchvision==0.13.1
torch==1.12.1

And then run pip install -r requirements.txt. But one problem Ive had always with this on Windows systems is that this approach installs the CPU version and not the GPU version. Apparently its because pytorch isnt detection a CUDA capable environment even though I have an NVIDIA GPU. I think it might be related to setting an environment variable. I dont really know.

In any case, I got around this issue by updating my requirements.txt file as below:

--extra-index-url https://download.pytorch.org/whl/cu116
torchvision==0.13.1+cu116
torch==1.12.1+cu116

The first line is necessary because PyPI does not host the CUDA version of pytorch since these are hosted by Pytorch directly on their own custom index url as specified above.

You can figure out the custom index url for the specific Pytorch version by checking the Pytorch download page for the previous versions here