from diffusers.models.attentionprocessor import AttnProcessor20…
- User
from diffusers.models.attention_processor import AttnProcessor2_0 pipe.unet.set_attn_processor(AttnProcessor2_0())
accelerator = Accelerator()
from diffusers.models.attention_processor import AttnProcessor2_0 pipe.unet.set_attn_processor(AttnProcessor2_0())
Use text_encoder if
--train_text_encoderwas used for the initial trainingunet, text_encoder = accelerator.prepare(pipe.unet, pipe.text_encoder)
#thêm thử nghiệm latent
vae = pipe.vae
images = []
def latents_callback(i, t, latents):
latents = 1 / 0.18215 * latents
decoder_output = vae.decode(latents)
if hasattr(decoder_output, 'sample'):
image = decoder_output.sample[0]
else:
# Handle the case when 'DecoderOutput' object has no attribute 'sample'
image = decoder_output # Or any other appropriate handling
image = (image / 2 + 0.5).clamp(0, 1)
image = image.cpu().permute(1, 2, 0).numpy()
images.extend(pipe.numpy_to_pil(image))
Khởi tạo các tham số
s1 = 0.9 s2 = 0.2 b1 = 1.2 b2 = 1.4
Chỉnh sửa model để kích hoạt freeu
model = pipe.unet model.freeu_enabled = True model.freeu_s1 = s1 model.freeu_s2 = s2 model.freeu_b1 = b1 model.freeu_b2 = b2
NEGATIVE_PROMPT="lowres,bad anatomy,bad hands,text,error,missing fingers,extra digit,fewer digits,cropped,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,username,blurry,((mutated hands and fingers)){{extra limb}},((nsfw))EasyNegative,Paintings,sketches,(worst quality:2),(low quality:2),(normal quality:2),lowers,normal quality,((monochrome)),((grayscales)),skin spots,acnes,skin blemishes,age spot,glans,(6 more fingers on one hand),(deformity),multiple breasts,(mutated hands and fingers:1.5 ),(long body :1.3),(mutation, poorly drawn :1.2),bad anatomy,liquid body,liquid tongue,disfigured,malformed,mutated,anatomical nonsense,text font ui,error,malformed hands,long neck,blurred,lowers,lowres,bad anatomy,bad proportions,bad shadow,uncoordinated body,unnatural body,fused breasts,bad breasts,huge breasts,poorly drawn breasts,extra breasts,liquid breasts,heavy breasts,missing breasts,huge haunch,huge thighs,huge calf,bad hands,fused hand,missing hand,disappearing arms,disappearing thigh,disappearing calf,disappearing legs,fused ears,bad ears,poorly drawn ears,extra ears,liquid ears,heavy ears,missing ears,fused animal ears,bad animal ears,poorly drawn animal ears,extra animal ears,liquid animal ears,heavy animal ears,missing animal ears,text,ui,error,missing fingers,missing limb,fused fingers,one hand with more than 5 fingers,one hand with less than 5 fingers,one hand with more than 5 digit,one hand with less than 5 digit,extra digit,fewer digits,fused digit,missing digit,bad digit,liquid digit,colorful tongue,black tongue,cropped,watermark,username,blurry,JPEG artifacts" prompt = "(Masterpiece, best quality, ultra realistic,32k, RAW photo, detail skin, 8k uhd, dslr, high quality, film grain:1.5),1girl, white ao dai, pants, long hair, looking at viewer, smile, black hair, long sleeves, dress, jewelry, standing, flower, earrings, outdoors, white dress, blurry, black eyes, blurry background, realistic, 16mm film live soft color" #prompt_tensor = pipe.text_encoder.encode(prompt).to("cuda")
- Assistant
- User
hãy xem lại mã và điều chỉnh lại như yêu cầu
- Assistant
- User
hình ảnh được tạo ra bởi SDXL hay có chấm đỏ đỏ trên tấm hình, vì sao lại vậy
- Assistant
Nếu bạn nhận thấy các chấm đỏ xuất hiện trong hình ảnh được tạo ra bởi mô hình Stable Diffusion, đặc biệt là phiên bản XL (Stable Diffusion XL hoặc SDXL), có thể có một số lý do tiềm ẩn:
-
Vấn Đề Với Việc Huấn Luyện Mô Hình: Trong quá trình huấn luyện, mô hình có thể "học" từ vài dữ liệu không mong muốn hoặc có nhiên liệu ở trong tập dữ liệu huấn luyện chứa các chấm hoặc đánh dấu màu đỏ. Điều này có thể khiến nó "nghĩ" rằng việc bao gồm các đánh dấu màu đỏ là một phần bình thường của quá trình tái tạo hình ảnh.
-
- User
model = pipe.unet model.post_processing['gaussian_blur'] = True model.post_processing['gaussian_blur_sigma'] = 1.0 AttributeError: 'UNet2DConditionModel' object has no attribute 'post_processing'
- Assistant
Dựa trên lỗi bạn đã gặp, có vẻ như bạn đang cố gắng áp dụng một lớp xử lý sau (post-processing) trực tiếp lên mô hình UNet trong pipeline của Diffusers, nhưng gặp phải vấn đề là đối tượng mô hình không có thuộc tính hoặc phương thức
post_processingmà bạn mong đợi. Điều này có thể là do thiết kế của bản thân Diffusers library mà bạn đang sử dụng không hỗ trợ phương thức này trực tiếp trong đối tượng mô hình mà bạn đang làm việc. - User
hãy sửa lại để nó không có lỗi và chạy đúng
- Assistant
Để tránh gặp lỗi và thực hiện Gaussian Blur như một bước post-processing sau khi đã tạo sinh hình ảnh bằng mô hình Stable Diffusion hoặc tương tự trong framework Diffusers, bạn cần phải sử dụng các thư viện xử lý ảnh ngoài như PIL (Python Imaging Library) hoặc OpenCV.
Điều quan trọng là phải xuất hình ảnh từ pipeline ở dạng mà bạn có thể áp dụng các kỹ thuật xử lý ảnh trên nó. Pipeline thường trả về ảnh dưới dạng một list của đối tượng PIL
Imagehoặc một tensor PyTorch.Dưới đây, tôi sẽ chỉ bạn cách áp dụng Gaussian Blur sử dụng PIL, vì nó không yêu cầu chuyển đổi ảnh từ tensor sang PIL
Image:from diffusers import StableDiffusionPipeline from PIL import ImageFilter import torch # Tải mô hình Stable Diffusion (để ý thay thế "your_token" bằng token hợp lệ của bạn) pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token="your_token").to("cuda") # Đặt input prompt (thay thế "a cat" bằng prompt của bạn) prompt = "a cat" # Tạo sinh hình ảnh và chuyển sang định dạng PIL with torch.no_grad(): image = pipe(prompt).images[0] # Lấy hình ảnh đầu tiên từ danh sách các hình ảnh được trả về # Áp dụng Gaussian Blur lên hình ảnh blurred_image = image.filter(ImageFilter.GaussianBlur(radius=1.0)) # Hiển thị hoặc Lưu hình ảnh blurred_image.show() # Áp dụng lưu hình ảnh nếu muốn # blurred_image.save("blurred_image.jpg") - User
---> 97 blurred_image = resized_images.filter(ImageFilter.GaussianBlur(radius=1.0)) 98 # Display resized image 99 blurred_image[0].show()
AttributeError: 'list' object has no attribute 'filter'
- Assistant
Dựa vào thông báo lỗi mà bạn đã cung cấp, lỗi xảy ra vì đối tượng
resized_imageslà một list, không phải là một instance củaImagetừ thư viện PIL. Phương thứcfilter()chỉ có thể được áp dụng trực tiếp lên một instance củaImage, không phải lên một list các instance. Đây là lý do bạn thấy thông báoAttributeError: 'list' object has no attribute 'filter'.Để giải quyết vấn đề này, bạn cần áp dụng Gaussian blur lên từng hình ảnh trong list
resized_imagesbằng cách sử dụng một vòng lặp. Dưới đây là một ví dụ:from PIL import ImageFilter # resized_images là một list chứa các instances của PIL Image. # Ví dụ, nếu chúng tôi muốn áp dụng Gaussian Blur lên từng hình ảnh: blurred_images = [img.filter(ImageFilter.GaussianBlur(radius=1.0)) for img in resized_images] # Để hiển thị hình ảnh đầu tiên sau khi được blur: blurred_images[0].show() # Nếu bạn muốn hiển thị tất cả hình ảnh blurred, bạn có thể sử dụng vòng lặp: #for img in blurred_images: # img.show()