本文主要是介绍【Diffusers 学习(1)】from_petrained() 中的 use_safetensors 有什么作用?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
use_safetensors(bool,可选,默认为None)
- 如果设置为 None,则在 safetensor 权重可用且已安装 safetensor 库的情况下下载这些权重。
- 如果设置为 True,则会从 safetensor 权重中强制加载模型。
- 如果设置为 False,则不会加载 safetensor 权重。
官方文档:https://huggingface.co/docs/diffusers/v0.27.2/en/api/models/overview#diffusers.ModelMixin.from_pretrained
那么什么情况下用 True?在 https://huggingface.co/diffusers/controlnet-depth-sdxl-1.0 中看到的 use_safetensors 都是 = True,如下
import torch
import numpy as np
from PIL import Imagefrom transformers import DPTFeatureExtractor, DPTForDepthEstimation
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
from diffusers.utils import load_imagedepth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to("cuda")
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
controlnet = ControlNetModel.from_pretrained("diffusers/controlnet-depth-sdxl-1.0",variant="fp16",use_safetensors=True,torch_dtype=torch.float16,
).to("cuda")
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16).to("cuda")
pipe = StableDiffusionXLControlNetPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0",controlnet=controlnet,vae=vae,variant="fp16",use_safetensors=True,torch_dtype=torch.float16,
).to("cuda")
pipe.enable_model_cpu_offload()def get_depth_map(image):image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")with torch.no_grad(), torch.autocast("cuda"):depth_map = depth_estimator(image).predicted_depthdepth_map = torch.nn.functional.interpolate(depth_map.unsqueeze(1),size=(1024, 1024),mode="bicubic",align_corners=False,)depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)depth_map = (depth_map - depth_min) / (depth_max - depth_min)image = torch.cat([depth_map] * 3, dim=1)image = image.permute(0, 2, 3, 1).cpu().numpy()[0]image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))return imageprompt = "stormtrooper lecture, photorealistic"
image = load_image("https://huggingface.co/lllyasviel/sd-controlnet-depth/resolve/main/images/stormtrooper.png")
controlnet_conditioning_scale = 0.5 # recommended for good generalizationdepth_image = get_depth_map(image)images = pipe(prompt, image=depth_image, num_inference_steps=30, controlnet_conditioning_scale=controlnet_conditioning_scale,
).images
images[0]images[0].save(f"stormtrooper.png")
这篇关于【Diffusers 学习(1)】from_petrained() 中的 use_safetensors 有什么作用?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!