当前位置: 首页 > news >正文

深度学习系列51:hugging face加速库optimum

1. 普通模型

Optimum是huggingface transformers库的一个扩展包,用来提升模型在指定硬件上的训练和推理性能。Optimum支持多种硬件,不同硬件下的安卓方式如下:
在这里插入图片描述
如果是国内安装的话,记得加上-i https://pypi.tuna.tsinghua.edu.cn/simple。
hugging face目前是被墙的状态,在使用示例代码时,需要将模型离线下载下来使用。
如下图,模型离线下载下来后的测试代码如下:
在这里插入图片描述
对比原模型,提速约一倍:
在这里插入图片描述

2. stable diffusion

首先安装diffusion库:pip install optimum[diffusers]
下载模型文件:

hugging face上是如下这个:
在这里插入图片描述
unet模型有3个多G,下载好后按照上面文件夹的格式放在程序的目录下。接下来是代码:

from optimum.intel import OVStableDiffusionPipeline
model_id = "echarlaix/stable-diffusion-v1-5-openvino"
pipeline = OVStableDiffusionPipeline.from_pretrained(model_id)
prompt = "sailing ship in storm by Rembrandt"
images = pipeline(prompt).images

如果我们是从pytorch模型export进来的,注意保存一下ov的模型:

model_id = "runwayml/stable-diffusion-v1-5"
pipeline = OVStableDiffusionPipeline.from_pretrained(model_id, export=True)
# Don't forget to save the exported model
pipeline.save_pretrained("openvino-sd-v1-5")

然后固定尺寸,加速推理:

# Define the shapes related to the inputs and desired outputs
batch_size = 1
num_images_per_prompt = 1
height = 512
width = 512# Statically reshape the model
pipeline.reshape(batch_size=batch_size, height=height, width=width, num_images_per_prompt=num_images_per_prompt)
# Compile the model before the first inference
pipeline.compile()# Run inference
images = pipeline(prompt, height=height, width=width, num_images_per_prompt=num_images_per_prompt).images

如果要添加Textual Inversion

pipeline.clear_requests()# Load textual inversion into stable diffusion pipeline
pipeline.load_textual_inversion("sd-concepts-library/cat-toy", "<cat-toy>")# Compile the model before the first inference
pipeline.compile()
image2 = pipeline(prompt, num_inference_steps=50).images[0]
image2.save("stable_diffusion_v1_5_with_textual_inversion.png")

如果是image-to-image:

import requests
import torch
from PIL import Image
from io import BytesIO
from optimum.intel import OVStableDiffusionImg2ImgPipelinemodel_id = "runwayml/stable-diffusion-v1-5"
pipeline = OVStableDiffusionImg2ImgPipeline.from_pretrained(model_id, export=True)url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
response = requests.get(url)
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image = init_image.resize((768, 512))
prompt = "A fantasy landscape, trending on artstation"
image = pipeline(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5).images[0]
image.save("fantasy_landscape.png")

结果如图:
在这里插入图片描述

http://www.lryc.cn/news/194456.html

相关文章:

  • 【QT开发笔记-基础篇】| 第四章 事件QEvent | 4.6 定时器事件
  • 阿里云服务器ECS实例规格族c/g/r等字母说明
  • Everything和SVN结合使用-在Everything中显示SVN
  • 代码随想录算法训练营第五十二天| 123.买卖股票的最佳时机III 188.买卖股票的最佳时机IV
  • ②. GPT错误:图片尺寸写入excel权限错误
  • JQuery、JSON、AJAX、XML、IO流、多线程、反射核心知识点详解
  • 基于python的多种图像增强算法实现
  • Java前后端交互实现班级管理(查询)
  • 论文速递 | 8月下旬9月上旬Operations ResearchManagement Science文章精选
  • DataBinding使用报错
  • 08Maven中的继承和聚合的作用
  • Ansible运行临时命令及常用模块介绍
  • EtherCAT报文-APRD(自动增量读)抓包分析
  • 论文阅读:Seeing in Extra Darkness Using a Deep-Red Flash
  • 将license验证加入到系统中
  • 中断机制-interrupt和isInterrupted源码分析、中断协商案例
  • 我与COSCon的故事【时光的故事】
  • 【科学文献计量】利用pybibx分析Scopus文献数据集(EDA,N-Grams,Cluster,Network analysis,NLP)
  • -带你看懂11种API类型及应用-
  • 集成友盟qq互联分享,导出风险问题处理
  • 探索数字安全的卓越之选 - Digicert证书
  • 第五章 流程控制 Pro
  • CSS之实现线性渐变背景
  • 软考 系统架构设计师系列知识点之特定领域软件体系结构DSSA(7)
  • CentOS-7网卡重启后关闭的解决方法
  • Linux CentOS7 用户组管理
  • C++算法:前缀和基础
  • vue和react的区别
  • STM32 之 HAL 库串口 USART 丢数据及ORE卡死的解决方案
  • 递归最小二乘法RLS