Unable to load with huggingface diffusers

#4
by JonnoFTW - opened

Using the following script adapted from app.py, I get an error about /vae/diffusion_pytorch_model.bin not being loadable:

CRITICAL:root:Unable to load weights from checkpoint file for '/home/jonno/.cache/huggingface/diffusers/models--hassanblend--hassanblend1.5/snapshots/c525df00b66126696b61fe7c453b588ba5b1b902/vae/diffusion_pytorch_model.bin' at '/home/jonno/.cache/huggingface/diffusers/models--hassanblend--hassanblend1.5/snapshots/c525df00b66126696b61fe7c453b588ba5b1b902/vae/diffusion_pytorch_model.bin'. If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.
Traceback (most recent call last):
  File "/home/jonno/diffusers/src/diffusers/models/modeling_utils.py", line 99, in load_state_dict
    return torch.load(checkpoint_file, map_location="cpu")
  File "/home/jonno/anaconda3/envs/dyn/lib/python3.8/site-packages/torch/serialization.py", line 705, in load
    with _open_zipfile_reader(opened_file) as opened_zipfile:
  File "/home/jonno/anaconda3/envs/dyn/lib/python3.8/site-packages/torch/serialization.py", line 242, in __init__
    super(_open_zipfile_reader, self).__init__(torch._C.PyTorchFileReader(name_or_buffer))
RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jonno/diffusers/src/diffusers/models/modeling_utils.py", line 105, in load_state_dict
    if f.read().startswith("version"):
  File "/home/jonno/anaconda3/envs/dyn/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 128: invalid start byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "sd/app.py", line 178, in generate_image
    self.pipe = get_pipe(model_name)
  File "sd/app.py", line 312, in get_pipe
    pipe = DiffusionPipeline.from_pretrained(
  File "/home/jonno/diffusers/src/diffusers/pipelines/pipeline_utils.py", line 735, in from_pretrained
    loaded_sub_model = load_method(os.path.join(cached_folder, name), **loading_kwargs)
  File "/home/jonno/diffusers/src/diffusers/models/modeling_utils.py", line 529, in from_pretrained
    state_dict = load_state_dict(model_file)
  File "/home/jonno/diffusers/src/diffusers/models/modeling_utils.py", line 117, in load_state_dict
    raise OSError(
OSError: Unable to load weights from checkpoint file for '/home/jonno/.cache/huggingface/diffusers/models--hassanblend--hassanblend1.5/snapshots/c525df00b66126696b61fe7c453b588ba5b1b902/vae/diffusion_pytorch_model.bin' at '/home/jonno/.cache/huggingface/diffusers/models--hassanblend--hassanblend1.5/snapshots/c525df00b66126696b61fe7c453b588ba5b1b902/vae/diffusion_pytorch_model.bin'. If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.

I've tried deleting and redownloading the file to no avail.

I have the latest master of diffusers:

$ pip freeze | grep -e 'diff\|torch'
-e git+https://github.com/huggingface/diffusers.git@9b37ed33b5fa09e594b38e4e6f7477beff3bd66a#egg=diffusers
pytorch-lightning==1.7.5
torch==1.12.1+cu116
torchaudio==0.12.1+cu116
torchdynamo==1.12.0
torchmetrics==0.9.3
torchvision==0.13.1+cu116
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import torch
from PIL import Image

model_id = 'hassanblend/hassanblend1.5'
prefix = ''

scheduler = DPMSolverMultistepScheduler(
    beta_start=0.00085,
    beta_end=0.012,
    beta_schedule="scaled_linear",
    num_train_timesteps=1000,
    trained_betas=None,
    prediction_type='epsilon',
    thresholding=False,
    algorithm_type="dpmsolver++",
    solver_type="midpoint",
    lower_order_final=True,
)

pipe = StableDiffusionPipeline.from_pretrained(
    model_id,
    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
    scheduler=scheduler
)


def replace_nsfw_images(results):
    for i in range(len(results.images)):
        if results.nsfw_content_detected[i]:
            results.images[i] = Image.open("nsfw.png")
    return results.images[0]


def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
    result = pipe(
        prompt,
        negative_prompt=neg_prompt,
        num_inference_steps=int(steps),
        guidance_scale=guidance,
        width=width,
        height=height,
        generator=generator)

    return replace_nsfw_images(result)


def error_str(error, title="Error"):
    return f"""#### {title}
            {error}""" if error else ""


def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="", auto_prefix=True):
    generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
    prompt = f"{prefix} {prompt}" if auto_prefix else prompt

    try:

        return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None
    except Exception as e:
        return None, error_str(e)
JonnoFTW changed discussion status to closed
JonnoFTW changed discussion status to open

Add in the VAE.

import torch
import diffusers

vae = diffusers.AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse", torch_dtype=torch.float16)
pipe = diffusers.StableDiffusionPipeline.from_pretrained(
    "hassanblend/HassanBlend1.5.1.2",
    torch_dtype=torch.float16,
    vae=vae,
)
pipe = pipe.to("cuda")

added the above code

hassanblend changed discussion status to closed

@hassanblend you forgot to update the image to image pipeline in app.py:

pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
  model_id,
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
  scheduler=scheduler,
  vae=vae
)

Thank you, added

Sign up or log in to comment