Replies: 2 comments
-
Hi! can you post the full stack trace of the error? |
Beta Was this translation helpful? Give feedback.
0 replies
-
You need to write a callback function yourself or use a new class for from diffusers.callbacks import PipelineCallback
class SDInpaintingCFGCutoffCallback(PipelineCallback):
tensor_inputs = ["prompt_embeds", "mask"]
def callback_fn(self, pipeline, step_index, timestep, callback_kwargs):
cutoff_step_ratio = self.config.cutoff_step_ratio
cutoff_step_index = self.config.cutoff_step_index
cutoff_step = (
cutoff_step_index if cutoff_step_index is not None else int(pipeline.num_timesteps * cutoff_step_ratio)
)
if step_index == cutoff_step:
prompt_embeds = callback_kwargs[self.tensor_inputs[0]]
prompt_embeds = prompt_embeds[-1:]
mask = callback_kwargs[self.tensor_inputs[1]]
mask = mask[-1:]
pipeline._guidance_scale = 0.0
callback_kwargs[self.tensor_inputs[0]] = prompt_embeds
callback_kwargs[self.tensor_inputs[1]] = mask
return callback_kwargs |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello. I have problem with AI models.
My code:
import os
import os
from diffusers.callbacks import SDCFGCutoffCallback
from dotenv import load_dotenv
import torch
import numpy as np
from diffusers import AutoPipelineForInpainting, EulerDiscreteScheduler, ControlNetModel, AutoencoderKL, DDIMScheduler
from PIL import Image, ImageOps
import rembg
load_dotenv()
SAVE_PATH = os.getenv('SAVE_PATH')
KANDINSKY_MODEL_PATH = os.getenv('KANDINSKY_MODEL_PATH')
REV_ANIMATED_MODEL_PATH = os.getenv('REV_ANIMATED_MODEL_PATH')
CONTROLNET_MODEL_PATH = os.getenv('CONTROLNET_MODEL_PATH')
MMIX_MODEL_PATH = os.getenv('MMIX_MODEL_PATH')
SAM_MODEL_PATH = os.getenv('SAM_MODEL_PATH')
VAE_MODEL_PATH = os.getenv('VAE_MODEL_PATH')
CANNY_MODEL_PATH = os.getenv('CANNY_MODEL')
prompt = 'best quality, highres, high definition masterpiece, photorealistic, foggy winter forest'
negative_prompt = 'nsfw, people, worst quality, low quality, normal quality, lowres,watermark, monochrome, low resolutio, extra limbs'
num_inference_steps
= 40base_img = 'ai_image2-683x1024.png'
num_of_img_per_prompt = 1
callback = SDCFGCutoffCallback(cutoff_step_ratio=0.4)
def binary_mask(init_image):
input_array = np.array(init_image)
mask_array = rembg.remove(input_array, only_mask=True)
mask_image = Image.fromarray(mask_array)
mask_image = ImageOps.invert(mask_image)
return mask_image
def toPill(image):
minv = np.amin(image)
maxv = np.amax(image)
image = image - minv
image = image / (maxv - minv)
image = image * 255
img = Image.fromarray(image.astype(np.uint8))
return img
if __name__ == '__main__':
pipeline = AutoPipelineForInpainting.from_pretrained(
KANDINSKY_MODEL_PATH,
use_safetensors=True,
torch_dtype=torch.float32,
vae=AutoencoderKL.from_pretrained(
VAE_MODEL_PATH,
subfolder=None)
).to('cpu')
init_image = Image.open(base_img)
init_image = toPill(init_image)
bin_mask = binary_mask(init_image)
image = pipeline(
prompt=prompt,
scheduler=DDIMScheduler.from_config(pipeline.scheduler.config),
negative_prompt=negative_prompt,
width=680,
height=1024,
num_inference_steps=20,
image=init_image,
mask_image=bin_mask,
control_image=init_image,
guidance_scale=15,
strength=1,
callback_on_step_end=callback
).images[0]
image.save('test1.jpg')
And i can see this problem in terminal:
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 88 but got size 85 for tensor number 1 in the list.
Can anyone help me with that problem?
Beta Was this translation helpful? Give feedback.
All reactions