惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Spread Privacy
Spread Privacy
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
DataBreaches.Net
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
L
LangChain Blog
Jina AI
Jina AI
MongoDB | Blog
MongoDB | Blog
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
T
Threatpost
V
V2EX
Security Archives - TechRepublic
Security Archives - TechRepublic
GbyAI
GbyAI
Scott Helme
Scott Helme
Cyberwarzone
Cyberwarzone
H
Help Net Security
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
T
Tenable Blog
S
Schneier on Security
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
F
Full Disclosure
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Security @ Cisco Blogs
A
Arctic Wolf
S
Securelist
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Tor Project blog
The Register - Security
The Register - Security
L
LINUX DO - 最新话题
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
L
LINUX DO - 热门话题
TaoSecurity Blog
TaoSecurity Blog
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
C
CERT Recently Published Vulnerability Notes

StudyingLover's Blog

Diffusion Policy笔记 rwkv笔记 act笔记 nanovllm-block_manager opencode多智能体 nanobot-pre-train nanobot-rl nanobot-sft nanobot-checkpoint_manager nanobot-gpt nanobot-mid-train Vision Mamba (Vim)笔记 BPE演示 最后一遍学习Transformer YOLOv5 目标检测笔记 下载根服务器解析记录 Dynaseal A Backend-Controlled LLM API Key Distribution Scheme with Constrained Invocation Parameters 判断链表有环 王道25数据结构勘误 关于perplexity的open-sourcing-r1-1776 AI为什么不像人类一样进行多轮对话 新博客改造日记和功能测试 linuxqq只显示登陆背景图 数字设计和计算机体系结构(机械工业出版社)勘误(自制) Dynaseal:面向未来端侧llm agent的llm api key分发机制 A Definitive Guide to Markdown Style This post is using MDX, Where you can embed JSX and Astro components RT-Patch学习 pydantic实现的LLM ReAct fastapi 和 uvicorn 设置监听 ipv6 pydantic+openai+json 控制大模型输出的最佳范式 解决 Matplotlib Scatter 不支持 Marker 列表的问题:mscatter 实现 roofline model zhipuAI接口兼容openai 在docker部署fastapi宝塔里使用nginx反代套上cloudflare获取请求的真实ip clion搭建libbpf-bootstrap开发环境 coze+coze-discord-proxy+ChatNextWebUI实现AI自由 安卓内核时间使用的是UTC时间 colab运行google最新开源模型Gemma Sora技术报告 视频生成模型作为世界模拟器 笔记 archlinux flutter开发踩坑 fastapi集成google auth登录 linux下NTFS磁盘报错输入输出错误 Venn-Abers 预测器 基于Venn-Abers预测器的系统日志异常检测方法_顾兆军 手机平板远程访问kvm虚拟机的windows phi-2弱智吧测评 poe的gemini pro或是百度开发 google gemini api使用 google gemini api申请 构建用于复杂数据处理的高效UDP服务器和客户端 matplotlib中文字体渲染 TruFor笔记和代码复现 深入分析:GitHub Trending 项目 "multipleWindow3dScene" pua大模型 ggml教程|mnist手写体识别量化推理 xgboost2.0最佳实践 xgboost使用GPU最佳实践 马踏棋盘 cloudlflare推理llama2 docker搭建elasticsearch并使用python连接 FreeU-文字生成图片的免费午餐笔记 使用xgboost的c接口推理模型 Archlinux使用CMake调用xgboost的c接口 m2cgen生成机器学习c语言推理代码 xgboost模型序列化存储并推理 speculative-sampling笔记 prompt2model笔记 RoboTAP笔记 自建obsidian同步服务 MediaPipe即将推出图像生成服务 Dual-Stream Diffusion Net for Text-to-Video Generation笔记 ViT在DDPM取代UNet(DiT) arch4edu搞崩了我的flutter LISA(推理分割)笔记 在终端绘制GPU显存使用曲线 GPTBot介绍 arch蓝牙无法连接 GPU部署llama-cpp-python(llama.cpp通用) 花式求GCD 使用llama构建一个蜜罐(前端) 使用llama构建一个蜜罐(后端) llama-cpp-python快速上手 快速上手llama2.c(更新版) Paper Gestalt笔记 DINO-v2笔记 快速上手llama2.c AnyDoor笔记 Archlinux安装scrcpy加载共享库出错 error while loading shared libraries:libusb-1.0.so.0:wrong ELF class:ELFCLASS32 npc_gzip笔记 python调用c++函数 Filesystem type ntfs3,ntfs not configured in kernel open_clip编码图像和文本 PicGo配置CloudflareR2图片储存 ArchlinuxGnome快捷键打开终端 clip-interrogator代码解析 GroundingDINO安装报错解决 2023华为鲲鹏畅想日暨西安高新国际会议中心零食午饭测评 RoboMaster开源仓库汇总(长期更新) 没有手都可以在腾讯云创建镜像
huggingface 和相关库
About the Author StudyingLover · 2023-05-09 · via StudyingLover's Blog

huggingface 和相关库

huggingface

Hugging Face是一个专注于自然语言处理(NLP)的开源平台,它旨在让NLP变得更加易用和普及。Hugging Face推出了多个库,例如Transformers,Datasets,Tokenizers和Accelerate,它们分别提供了预训练的模型,大规模的数据集,高效的分词器和分布式训练的工具。Hugging Face还拥有一个活跃的社区,其中有数千名研究人员,开发者和爱好者共同交流和贡献NLP的最新进展。 image.png

Hugging Face的名字来源于一个可爱的表情符号,它代表了平台的愿景:让人类和机器之间的交流更加自然和亲密。Hugging Face的核心产品是Transformers库,它包含了超过10000个预训练的模型,涵盖了各种NLP任务,如文本分类,问答,文本生成,情感分析等。Transformers库支持多种深度学习框架,如PyTorch,TensorFlow,JAX和Flax,并且可以轻松地在不同的设备上运行,如CPU,GPU和TPU。Hugging Face还提供了一个在线平台,Spaces,它可以让用户快速地部署和分享他们的模型和应用。 image.png

近年来,Hugging Face托管的模型已经不局限于NLP领域,而是涉及到了更多的领域,如计算机视觉(CV),语音识别(ASR),音乐生成(MG)等。这些模型都可以在Hugging Face的网站上找到,并且可以通过Transformers库或者其他的库来使用。Hugging Face还提供了一个数据集库,叫做Datasets,它包含了超过1000个数据集,覆盖了各种领域和语言。Datasets库可以帮助用户快速地加载,处理和缓存数据,以及进行数据分析和可视化。

Accelerate

Accelerate 是一个可以让训练变得更加简单的库,它可以通过几行代码来在分布式设备上运行相同的pytorch代码

可以通过pypi 和 conda安装

pip install accelerate
conda install -c conda-forge accelerate

你可能会遇到这种报错

WARNING: The scripts accelerate, accelerate-config and accelerate-launch are installed in '/home/ubuntu/.local/bin' which is not on PATH.  
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.  
WARNING: The script transformers-cli is installed in '/home/ubuntu/.local/bin' which is not on PATH.  
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.  
WARNING: The script ftfy is installed in '/home/ubuntu/.local/bin' which is not on PATH.  
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.  
WARNING: The script tensorboard is installed in '/home/ubuntu/.local/bin' which is not on PATH.  
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.  
WARNING: The script datasets-cli is installed in '/home/ubuntu/.local/bin' which is not on PATH.  
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

这里的依赖已经安装成功了,只是被安装到了未被添加到PATH的目录,接下来运行的时候只需要指明目录即可。例如下面我们要使用accelerate,正常的用法是

accelerate 你要执行的东西

我们只需要改成

/home/ubuntu/.local/bin/accelerate 你要执行的东西

通过accelerate config 命令可以配置当前文件夹启用。(如果啥都不知道就全部选No)

Transformers

Transformers 收集了所有的SOTA的NLP研究方法,并提供了对应的预训练模型和接口。Transformers 支持 PyTorch、TensorFlow 和 JAX 之间的框架互操作性。这提供了在模型生命周期的每个阶段使用不同框架的灵活性;在一个框架中用三行代码训练一个模型,然后将其加载到另一个框架中进行推理。模型还可以导出为 ONNX 和 TorchScript 等格式,以便在生产环境中部署。

安装非常简单

pip install 'transformers[torch]'

这回安装torch对应的api,当然也可以安装完整版

pip install 'transformers' 

21年冬天在家上网课的时候,我看到了这样的一个教程这篇文章是我用AI生成出来的 ,他就是使用了transformers 库构建了一个生成式语言模型。

Diffusers

Diffusers 收集了所有SOTA的扩散模型,用于生成图像、音频,甚至分子的 3D 结构。diffusers提供了扩散模型的完整pipeline ,包括DDPM,DDIM,stable_diffusion_2,VAE,controlnet等等,可以使用简单的几行代码完成推理。

安装和上面一样

pip install diffusers["torch"]

或者

pip install diffusers 

pipeline

pipeline 是diffusers 甚至huggingface各个库的一个重要概念,他封装了各个模型加载权重,构建网络结构,推理和训练的全部过程。

这里以stable diffusion 1.5为例,首先创建pipeline,并指明stable diffusion 的版本

from diffusers import DiffusionPipeline

model_id = "runwayml/stable-diffusion-v1-5"
pipeline = DiffusionPipeline.from_pretrained(model_id)

接下来给出提示(prompt)

prompt = "portrait photo of a old warrior chief"

为了加速推理,我们可以把数据放到gpu上

pipeline = pipeline.to("cuda")

设置生成器,并生成图像

generator = torch.Generator("cuda").manual_seed(0)
image = pipeline(prompt, generator=generator).images[0]
image

当然,huggingface推荐我们在float16上做推理

import torch

pipeline = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipeline = pipeline.to("cuda")
generator = torch.Generator("cuda").manual_seed(0)
image = pipeline(prompt, generator=generator).images[0]
image

LoRA

Low-Rank Adaptation of Large Language Models (LoRA)是一种训练方法,可以加速大型模型的训练,同时消耗更少的内存,最有用的例子莫过于生成人脸了。Diffusers 现在支持使用 LoRA 进行文本到图像生成DreamBooth微调。

DreamBooth是Google提出的微调技术,用于个性化文本到图像模型(如 Stable Diffusion),可以以在给定几张主题图像的情况下生成不同背景下主题的逼真图像。

这里 你可以找到完整的代码,在Google Drive 下载完整的图像用于训练

先设置基本信息,分别是模型名,示例图片和模型输出文件夹

export MODEL_NAME="runwayml/stable-diffusion-v1-5"
export INSTANCE_DIR="path-to-instance-images"
export OUTPUT_DIR="path-to-save-model"

接下来运行代码

accelerate launch train_dreambooth_lora.py \
  --pretrained_model_name_or_path=$MODEL_NAME  \
  --instance_data_dir=$INSTANCE_DIR \
  --output_dir=$OUTPUT_DIR \
  --instance_prompt="a photo of sks dog" \
  --resolution=512 \
  --train_batch_size=1 \
  --gradient_accumulation_steps=1 \
  --checkpointing_steps=100 \
  --learning_rate=1e-4 \
  --report_to="wandb" \
  --lr_scheduler="constant" \
  --lr_warmup_steps=0 \
  --max_train_steps=500 \
  --validation_prompt="A photo of sks dog in a bucket" \
  --validation_epochs=50 \
  --seed="0" \
  --push_to_hub

推理也是使用起来很简单的

import torch
from diffusers import StableDiffusionPipeline

pipe.unet.load_attn_procs(lora_model_path)
pipe.to("cuda")

image = pipe("A picture of a sks dog in a bucket.", num_inference_steps=25, guidance_scale=7.5).images[0]
image.save("bucket-dog.png")

[[ControlNet]]

可以看之前的文章ControlNet训练自己数据集

Gradio

gradio 是一个可以快速构建交互式网页的工具,Webui就是用它做出来的,使用他的核心代码就是

demo = gradio.Interface(fn, inputs, outputs, ···)
demo.launch()

传入一个函数和参数,获取返回值

剩下的就是你写好fn,设计一个好看的界面,然后launch就可以了。