Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
GFPGAN restore faces error
Using GFPGAN restore faces gives following error

Traceback (most recent call last):
  File "/home/x/stable-diff/stable-diffusion-webui/modules/ui.py", line 128, in f
    res = list(func(*args, **kwargs))
  File "/home/x/stable-diff/stable-diffusion-webui/webui.py", line 55, in f
    res = func(*args, **kwargs)
  File "/home/x/stable-diff/stable-diffusion-webui/modules/txt2img.py", line 39, in txt2img
    processed = process_images(p)
  File "/home/x/stable-diff/stable-diffusion-webui/modules/processing.py", line 314, in process_images
    x_sample = modules.face_restoration.restore_faces(x_sample)
  File "/home/x/stable-diff/stable-diffusion-webui/modules/face_restoration.py", line 19, in restore_faces
    return face_restorer.restore(np_image)
  File "/home/x/stable-diff/stable-diffusion-webui/modules/codeformer_model.py", line 79, in restore
    self.face_helper.get_face_landmarks_5(only_center_face=False, resize=640, eye_dist_threshold=5)
  File "/home/x/stable-diff/stable-diffusion-webui/repositories/CodeFormer/facelib/utils/face_restoration_helper.py", line 151, in get_face_landmarks_5
    bboxes = self.face_det.detect_faces(input_img)
  File "/home/x/stable-diff/stable-diffusion-webui/repositories/CodeFormer/facelib/detection/retinaface/retinaface.py", line 231, in detect_faces
    keep = py_cpu_nms(bounding_boxes, nms_threshold)
  File "/home/x/stable-diff/stable-diffusion-webui/repositories/CodeFormer/facelib/detection/retinaface/retinaface_utils.py", line 41, in py_cpu_nms
    keep = torchvision.ops.nms(
  File "/home/x/.local/lib/python3.10/site-packages/torchvision/ops/boxes.py", line 40, in nms
    _assert_has_ops()
  File "/home/x/.local/lib/python3.10/site-packages/torchvision/extension.py", line 33, in _assert_has_ops
    raise RuntimeError(
RuntimeError: Couldn't load custom C++ ops. This can happen if your PyTorch and torchvision versions are incompatible, or if you had errors while compiling torchvision from source. For further information on the compatible versions, check https://github.com/pytorch/vision#installation for the compatibility matrix. Please check your PyTorch version with torch.__version__ and your torchvision version with torchvision.__version__ and verify if they are compatible, and if not please reinstall torchvision so that it matches your PyTorch install.


Running: python -c "import torch; import torchvision; print(torch.__version__); print(torchvision.__version__)"

Gives the following results:
1.12.1+cu113
0.13.1+cu102


on Latest Arch Linux.

GFPGAN works without issues in this similar tool: https://github.com/cmdr2/stable-diffusion-ui




Discussion:
I'm seeing this as well. also on arch linux. 
I was getting this with GFPGAN and codeformer :scream: 
adding the correct version of torchvision to `requirements_version.txt` and running webui.sh seemed to fix it, but i needed to install cuda and cudnn from pacman
I fixed this (Arch Linux) by setting the torch version to `+cu102` (instead of `+cu113`), which makes it match the subversion of torchvision.

You may be able to fix an existing installation by changing into the base directory and issuing the following commands.
```
source venv/bin/activate
 pip3 install -Iv torch  --extra-index-url https://download.pytorch.org/whl/cu102
```

This will activate the python virtual environment sd-webgui runs in and pip should download cu102 and uninstall the later version (e.g. cu113).
> I fixed this (Arch Linux) by setting the torch version to `+cu102` (instead of `+cu113`), which makes it match the subversion of torchvision.
> 
> You may be able to fix an existing installation by changing into the base directory and issuing the following commands.
> 
> ```
> source venv/bin/activate
>  pip3 install -Iv torch  --extra-index-url https://download.pytorch.org/whl/cu102
> ```
> 
> This will activate the python virtual environment sd-webgui runs in and pip should download cu102 and uninstall the later version (e.g. cu113).

Thanks, i did this with cu113 and it is now restoring faces. 
`source venv/bin/activate`
`pip3 install torchvision==0.13.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113
`

> Thanks, i did this with cu113 and it is now restoring faces.

Glad to hear that it worked for you! Also, useful to know that upgrading `torchvision`, rather than downgrading `torch` worked as well, so thanks for confirming that.

Resolving patch:
diff --git a/launch.py b/launch.py
--- a/launch.py
+++ b/launch.py
@@ -11,7 +11,7 @@
 
 python = sys.executable
 git = os.environ.get('GIT', "git")
-torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==1.12.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113")
+torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113")
 requirements_file = os.environ.get('REQS_FILE', "requirements_versions.txt")
 commandline_args = os.environ.get('COMMANDLINE_ARGS', "")
 
@@ -92,8 +92,9 @@ def git_clone(url, dir, name, commithash=None):
 print(f"Python {sys.version}")
 print(f"Commit hash: {commit}")
 
-if not is_installed("torch"):
-    run(f'"{python}" -m {torch_command}', "Installing torch", "Couldn't install torch")
+
+if not is_installed("torch") or not is_installed("torchvision"):
+    run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch")
 
 run_python("import torch; assert torch.cuda.is_available(), 'Torch is not able to use GPU'")
 


Test patch: