【Diffusers 学习(1)】from_petrained() 中的 use_safetensors 有什么作用?

2024-04-18 23:52

本文主要是介绍【Diffusers 学习(1)】from_petrained() 中的 use_safetensors 有什么作用?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

use_safetensors(bool,可选,默认为None)

  1. 如果设置为 None,则在 safetensor 权重可用且已安装 safetensor 库的情况下下载这些权重。
  2. 如果设置为 True,则会从 safetensor 权重中强制加载模型。
  3. 如果设置为 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 有什么作用?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/916119

相关文章

MySQL日志UndoLog的作用

《MySQL日志UndoLog的作用》UndoLog是InnoDB用于事务回滚和MVCC的重要机制,本文主要介绍了MySQL日志UndoLog的作用,文中介绍的非常详细,对大家的学习或者工作具有一定的... 目录一、Undo Log 的作用二、Undo Log 的分类三、Undo Log 的存储四、Undo

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

JAVA transient 关键字作用详解

《JAVAtransient关键字作用详解》Java的transient关键字用于修饰成员变量,使其不参与序列化过程,通过自定义序列化方法,可以手动控制transient变量的序列化行为,本文给大... 目录一、transient关键字作用二、原理详解三、典型使用场景四、代码示例五、注意事项六、与 stat

Spring Boot/Spring MVC核心注解的作用详解

《SpringBoot/SpringMVC核心注解的作用详解》本文详细介绍了SpringBoot和SpringMVC中最常用的15个核心注解,涵盖了请求路由映射、参数绑定、RESTfulAPI、... 目录一、Spring/Spring MVC注解的核心作用二、请求映射与RESTful API注解系列2.1

C#中的sealed修饰符的作用详解

《C#中的sealed修饰符的作用详解》在C#中,sealed修饰符的作用是“密封”用来阻止类被继承,或者阻止方法/属性在派生类中被重写,接下来通过本文给大家介绍C#中的sealed修饰符的作用详解,... 目录✅ 1.sealed用于类(class)作用:示例:✅ 2.sealed用于方法或属性(必须与

Spring的基础事务注解@Transactional作用解读

《Spring的基础事务注解@Transactional作用解读》文章介绍了Spring框架中的事务管理,核心注解@Transactional用于声明事务,支持传播机制、隔离级别等配置,结合@Tran... 目录一、事务管理基础1.1 Spring事务的核心注解1.2 注解属性详解1.3 实现原理二、事务事

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断