项目地址:
https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/inpainthttps://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/inpaint在云服务器端运行下面给出的Example的时候出现报错:
(myconda) root@wnyrpE:/mnt/Diffusers/inpainting# python3 SD_inpaint_eg.pyCouldn't connect to the Hub: (MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /api/models/runwayml/stable-diffusion-inpainting (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fa6c5357dc0>: Failed to establish a new connection: [Errno 110] Connection timed out'))"), '(Request ID: bebba422-c4d8-4797-a818-a00fe02957e8)').Will try to load from local cache.Traceback (most recent call last): File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/urllib3/connection.py", line 174, in _new_conn conn = connection.create_connection( File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/urllib3/util/connection.py", line 95, in create_connection raise err File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/urllib3/util/connection.py", line 85, in create_connection sock.connect(sa)TimeoutError: [Errno 110] Connection timed outDuring handling of the above exception, another exception occurred:Traceback (most recent call last): File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/urllib3/connectionpool.py", line 703, in urlopen httplib_response = self._make_request( File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/urllib3/connectionpool.py", line 386, in _make_request self._validate_conn(conn) File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/urllib3/connectionpool.py", line 1040, in _validate_conn conn.connect() File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/urllib3/connection.py", line 358, in connect self.sock = conn = self._new_conn() File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/urllib3/connection.py", line 186, in _new_conn raise NewConnectionError(urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x7fa6c5357dc0>: Failed to establish a new connection: [Errno 110] Connection timed outDuring handling of the above exception, another exception occurred:Traceback (most recent call last): File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/requests/adapters.py", line 440, in send resp = conn.urlopen( File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/urllib3/connectionpool.py", line 785, in urlopen retries = retries.increment( File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/urllib3/util/retry.py", line 592, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause))urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /api/models/runwayml/stable-diffusion-inpainting (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fa6c5357dc0>: Failed to establish a new connection: [Errno 110] Connection timed out'))During handling of the above exception, another exception occurred:Traceback (most recent call last): File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/diffusers/pipelines/pipeline_utils.py", line 1656, in download info = model_info(pretrained_model_name, token=token, revision=revision) File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/huggingface_hub/utils/_validators.py", line 118, in _inner_fn return fn(*args, **kwargs) File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/huggingface_hub/hf_api.py", line 2084, in model_info r = get_session().get(path, headers=headers, timeout=timeout, params=params) File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/requests/sessions.py", line 542, in get return self.request('GET', url, **kwargs) File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/requests/sessions.py", line 529, in request resp = self.send(prep, **send_kwargs) File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/requests/sessions.py", line 645, in send r = adapter.send(request, **kwargs) File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/huggingface_hub/utils/_http.py", line 67, in send return super().send(request, *args, **kwargs) File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/requests/adapters.py", line 519, in send raise ConnectionError(e, request=request)requests.exceptions.ConnectionError: (MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /api/models/runwayml/stable-diffusion-inpainting (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fa6c5357dc0>: Failed to establish a new connection: [Errno 110] Connection timed out'))"), '(Request ID: bebba422-c4d8-4797-a818-a00fe02957e8)')The above exception was the direct cause of the following exception:Traceback (most recent call last): File "SD_inpaint_eg.py", line 20, in <module> pipe = StableDiffusionInpaintPipeline.from_pretrained( File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/huggingface_hub/utils/_validators.py", line 118, in _inner_fn return fn(*args, **kwargs) File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/diffusers/pipelines/pipeline_utils.py", line 1096, in from_pretrained cached_folder = cls.download( File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/huggingface_hub/utils/_validators.py", line 118, in _inner_fn return fn(*args, **kwargs) File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/diffusers/pipelines/pipeline_utils.py", line 1905, in download raise EnvironmentError(OSError: Cannot load model runwayml/stable-diffusion-inpainting: model is not cached locally and an error occured while trying to fetch metadata from the Hub. Please check out the root cause in the stacktrace above.
原因是:国内无法服务器无法直接连接上huggingface。
解决办法是:开代理把模型下载到本地再上传到服务器端。(不管是下载到本地,还是直接同步到服务器端,只要是国内的网络都要把代理打开)
我运行的Example代码是:
import PILimport requestsimport torchfrom io import BytesIOfrom diffusers import StableDiffusionInpaintPipelinedef download_image(url): response = requests.get(url) return PIL.Image.open(BytesIO(response.content)).convert("RGB")img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"init_image = download_image(img_url).resize((512, 512))mask_image = download_image(mask_url).resize((512, 512))pipe = StableDiffusionInpaintPipeline.from_pretrained( "runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16)pipe = pipe.to("cuda")prompt = "Face of a yellow cat, high resolution, sitting on a park bench"image = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
从Example代码里面看到,应该是要下载runwayml/stable-diffusion-inpainting模型
pipe = StableDiffusionInpaintPipeline.from_pretrained( "runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16)pipe = pipe.to("cuda")
注意修改脚本中的代码:
pipe = StableDiffusionInpaintPipeline.from_pretrained( "/本地/模型/路径", torch_dtype=torch.float16)pipe = pipe.to("cuda")