glucauze / sd-webui-faceswaplab

Extended faceswap extension for StableDiffusion web-ui with multiple faceswaps, inpainting, checkpoints, ....
https://glucauze.github.io/sd-webui-faceswaplab/
GNU Affero General Public License v3.0
731 stars 97 forks source link

Loading of swapping model failed on MAC M2 #14

Closed mesax1 closed 1 year ago

mesax1 commented 1 year ago

Hi, I'm getting this problem when trying to run on a Mac with M2

`preload_extensions_git_metadata for 11 extensions took 0.46s
100%|██████████████████████████████████████████████████████| 45/45 [00:31<00:00,  1.42it/s]
2023-08-01 16:22:15,075 - FaceSwapLab - INFO - Try to use model : /Users/juanmesa/Documents/stable-diffusion-webui/models/faceswaplab/inswapper_128.onnx
2023-08-01 16:22:15,851 - FaceSwapLab - INFO - blend all faces together
2023-08-01 16:22:15,852 - FaceSwapLab - INFO - Load analysis model, will take some time.
Applied providers: ['CPUExecutionProvider'], with options: {'CPUExecutionProvider': {}}
find model: /Users/juanmesa/Documents/stable-diffusion-webui/models/faceswaplab/analysers/models/buffalo_l/1k3d68.onnx landmark_3d_68 ['None', 3, 192, 192] 0.0 1.0
Applied providers: ['CPUExecutionProvider'], with options: {'CPUExecutionProvider': {}}
find model: /Users/juanmesa/Documents/stable-diffusion-webui/models/faceswaplab/analysers/models/buffalo_l/2d106det.onnx landmark_2d_106 ['None', 3, 192, 192] 0.0 1.0
Applied providers: ['CPUExecutionProvider'], with options: {'CPUExecutionProvider': {}}
find model: /Users/juanmesa/Documents/stable-diffusion-webui/models/faceswaplab/analysers/models/buffalo_l/det_10g.onnx detection [1, 3, '?', '?'] 127.5 128.0
Applied providers: ['CPUExecutionProvider'], with options: {'CPUExecutionProvider': {}}
find model: /Users/juanmesa/Documents/stable-diffusion-webui/models/faceswaplab/analysers/models/buffalo_l/genderage.onnx genderage ['None', 3, 96, 96] 0.0 1.0
Applied providers: ['CPUExecutionProvider'], with options: {'CPUExecutionProvider': {}}
find model: /Users/juanmesa/Documents/stable-diffusion-webui/models/faceswaplab/analysers/models/buffalo_l/w600k_r50.onnx recognition ['None', 3, 112, 112] 127.5 127.5
set det-size: (640, 640)
/Users/juanmesa/Documents/stable-diffusion-webui/venv/lib/python3.10/site-packages/insightface/utils/transform.py:68: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.
To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.
  P = np.linalg.lstsq(X_homo, Y)[0].T # Affine matrix. 3 x 4
set det-size: (640, 640)
set det-size: (640, 640)
set det-size: (640, 640)
2023-08-01 16:22:51,751 - FaceSwapLab - INFO - Process face 0
2023-08-01 16:22:51,752 - FaceSwapLab - INFO - Source Gender 0
2023-08-01 16:22:51,859 - FaceSwapLab - ERROR - Loading of swapping model failed, please check the requirements (On Windows, download and install Visual Studio. During the install, make sure to include the Python and C++ packages.)
2023-08-01 16:22:51,859 - FaceSwapLab - ERROR - Conversion failed Loading of swapping model failed
2023-08-01 16:22:51,860 - FaceSwapLab - ERROR - Failed to swap face in postprocess method : Loading of swapping model failed
Total progress: 100%|██████████████████████████████████████| 45/45 [01:09<00:00,  1.55s/it]
Total progress: 100%|██████████████████████████████████████| 45/45 [01:09<00:00,  1.53it/s]`

Considering this part: FaceSwapLab - ERROR - Loading of swapping model failed, please check the requirements (On Windows, download and install Visual Studio. During the install, make sure to include the Python and C++ packages.) Given that I'm not on Windows, how can I get the swapping model to load correctly?

The extension used to work when it was part of SD-roop-webui, but now that its independent, I don't know what went wrong.

I tried by installing each the the dependencies from requirements.txt independently, but still I'm getting the same error.

glucauze commented 1 year ago

Hmm, maybe there's a problem with the providers. That's surprising because I didn't change that part. In this file: https://github.com/glucauze/sd-webui-faceswaplab/blob/main/scripts/faceswaplab_swapping/swapper.py

The providers are set to ["CPUExecutionProvider"], and these are the versions being used.

Unfortunately, I don't have a Mac to test it on.

You could try looking at the dependencies in roop to see if anything makes more sense.

From what I see:

onnxruntime==1.15.1; python_version != '3.9' and sys_platform == 'darwin' and platform_machine != 'arm64'
onnxruntime-coreml==1.13.1; python_version == '3.9' and sys_platform == 'darwin' and platform_machine != 'arm64'
onnxruntime-silicon==1.13.1; sys_platform == 'darwin' and platform_machine == 'arm64'

These could be necessary on a Mac.

If you manage to get it working, I will modify the code accordingly.

mesax1 commented 1 year ago

Thanks! I tried that too, installed some of them that were apparently missing, but I'm still getting the same error :(

glucauze commented 1 year ago

Could you try to do that :

@lru_cache(maxsize=1)
def getFaceSwapModel(model_path: str) -> upscaled_inswapper.UpscaledINSwapper:
    """
    Retrieves the face swap model and initializes it if necessary.

    Args:
        model_path (str): Path to the face swap model.

    Returns:
        insightface.model_zoo.FaceModel: The face swap model.
    """
    try:
        # Initializes the face swap model using the specified model path.
        return upscaled_inswapper.UpscaledINSwapper(
            insightface.model_zoo.get_model(model_path, providers=providers)
        )
    except Exception as e:
        logger.error(
            "Loading of swapping model failed, please check the requirements (On Windows, download and install Visual Studio. During the install, make sure to include the Python and C++ packages.)"
        )
        import traceback
        traceback.print_exc()
        raise FaceModelException("Loading of swapping model failed")

I was stupid enough not to print the complete exception i need to investigate more.

Since it comes from the inswapper model and the analyzer seems to load, you'll have to check that the model isn't corrupted. On my computer, i have that :

sha1sum inswapper_128.onnx 
17a64851eaefd55ea597ee41e5c18409754244c5  inswapper_128.onnx
 sha256sum inswapper_128.onnx 
e4a3f08c753cb72d04e10aa0f7dbe3deebbf39567d4ead6dce08e98aa49e16af  inswapper_128.onnx
sha512sum inswapper_128.onnx 
4311f4ccd9da58ec544e912b32ac0cba95f5ab4b1a06ac367efd3e157396efbae1097f624f10e77dd811fbba0917fa7c96e73de44563aa6099e5f46830965069  inswapper_128.onnx
mesax1 commented 1 year ago

Maybe the model inswapper_128.onnx was corrupted. I deleted it, downloaded it again, and now it works! Thanks!

glucauze commented 1 year ago

Well, better that than something hard to fix. I've added a control and a warning in version 1.2. I still have a lot of things to test and correct before publishing it.