Code Monkey home page Code Monkey logo

skyworkaigc / skypaint-ai-diffusion Goto Github PK

View Code? Open in Web Editor NEW
655.0 9.0 38.0 7.92 MB

基于Stable Diffusion优化的AI绘画模型。支持输入中英文文本,可生成多种现代艺术风格的高质量图像。| An optimized text-to-image model based on Stable Diffusion. Both Chinese and English text inputs are available to generate images. The model can generate high-quality images in several modern art styles.

Home Page: https://sky-paint.singularity-ai.com/index.html#/

License: MIT License

dreambooth machine-learning text-to-image bert clip cv latent-diffusion openai pytorch ai-painting

skypaint-ai-diffusion's Introduction

SkyPaint-Chinese-EN-v-1.0

Hugging Face 模型主页:链接

SkyPaint是由奇点智源开发的中英双语文本生成图像的项目,目前还在持续更新优化中。

使用我们的模型,输入若干中文或英文的文本,便可让机器像人类画家一样,画出富有现代艺术风格的作品。下面是一些示例:

效果展示

中文

机械狗

城堡 大海 夕阳 宫崎骏动画

花落知多少

半鸡半人,强壮

鸡你太美

体验试用

请访问天工巧绘SkyPaint image

你也可以微信扫描二维码,在小程序上体验: gh_0e89c7c92d3f_430

模型优势

SkyPaint文本生成图片模型主要由两大部分组成,即提示词文本编码器模型和扩散模型两大部分。因此我们的优化也分为两步: 首先,基于OpenAI-CLIP优化了提示词文本编码器模型使得SkyPaint具有中英文识别能力, 然后,优化了扩散模型,使得SkyPaint具有现代艺术能力可以产生高质量图片。

模型功能

  • 支持汉语和英文以及中英文混合提示词输入
  • 支持生成现代艺术风格的高质量图片
  • 支持stable_diffusion_1.x官方模型及相关微调模型的英文提示词
  • 保留stable_diffusion提示词的使用习惯和方法

测试用例

模型下载地址 SkyPaint-v1.0

from diffusers import StableDiffusionPipeline

device = 'cuda'
pipe = StableDiffusionPipeline.from_pretrained("path_to_our_model").to(device)

prompts = [
    '机械狗',
    '城堡 大海 夕阳 宫崎骏动画',
    '花落知多少',
    '鸡你太美',
]

for prompt in prompts:
    prompt = 'sai-v1 art, ' + prompt
    image = pipe(prompt).images[0]  
    image.save("%s.jpg" % prompt)

————————————————————————————————————————————————

SkyCLIP模型简介

SkyCLIP是我们采用一种高效的训练中英双语CLIP模型的方法得到的CLIP模型,该方法仅需要使用文本数据即可实现对OpenAI-CLIP模型的高效蒸馏,大幅降低了数据门槛,同时训练所需算力要求相较于原始CLIP模型减少90%以上,方便开源社区可以进行复现/微调。该方法仅改变了OpenAI-CLIP的文本编码器,可搭配使用OpenAI-CLIP的图像编码器实现图文检索功能。

SkyCLIP训练数据来源

  • 中英文机器翻译任务平行语料
  • 联合国中英文平行语料
  • LAION中英文语料(部分)
  • Wukong中文语料(部分)
  • AI-Challenger翻译任务中英文语料
  • 古诗词中英文语料
  • 提示词手册/魔法书中常见词组合而成的中英文语料

SkyCLIP训练方法

将OpenAI-CLIP的text_encoder作为教师模型并且冻结参数,学生模型采用和教师模型同样大小的多语言BERT模型,训练时英文输入通过教师模型获取相应的t_en_hiddent_state,英文和中文分别通过学生模型获取相应s_en_hiddent_state,s_zh_hidden_state,采用l1、l2、cos距离等构造损失函数使得学生模型的中英文hiddent_state逐渐靠近教师模型的hiddent_state。由于平行语料的中文和英文存在天然的不等长性质,为了使得平行的中文和英文尽量接近,训练过程中我们还添加了中文解码器,使用学生模型的中英文hiddent_state作为解码器的hidden_state输入,通过翻译任务来辅助实现中文和英文的对齐目的。

SkyCLIP模型评估

目前我们主要评估了SkyCLIP在Flickr30K-CN的zero-shot表现,主要对比了若干具备中文能力的相关开源模型,为确保对比的公平性,具有多个模型尺寸的我们均选取基于OpenAI-CLIP ViT-L/14尺寸的模型,我们评估的流程参考了Chinese-CLIP所提供的评估脚本。

Flickr30K-CN Retrieval:

TaskText-to-ImageImage-to-Text MR
SetupZero-shotZero-shot
MetricR@1R@5R@10R@1R@5R@10
Taiyi-326M53.879.986.664.090.496.178.47
AltCLIP50.775.483.173.492.896.978.72
Wukong51.978.685.97594.497.780.57
R2D242.669.578.663.090.196.473.37
CN-CLIP68.189.794.580.296.698.287.87
SkyCLIP58.882.689.678.896.198.384.04

SkyCLIP计算图文相似度

from PIL import Image
import requests
import clip
import torch
from transformers import BertTokenizer
from transformers import CLIPProcessor, CLIPModel, CLIPTextModel
import numpy as np

query_texts = ['一个人', '一辆汽车', '两个男人', '两个女人']  # 这里是输入提示词,可以随意替换。
# 加载SkyCLIP 中英文双语 text_encoder
text_tokenizer = BertTokenizer.from_pretrained("./tokenizer")
text_encoder = CLIPTextModel.from_pretrained("./text_encoder").eval()
text = text_tokenizer(query_texts, return_tensors='pt', padding=True)['input_ids']

url = "http://images.cocodataset.org/val2017/000000040083.jpg"  #这里可以换成任意图片的url
# 加载CLIP的image encoder
clip_model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")
clip_text_proj = clip_model.text_projection
processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
image = processor(images=Image.open(requests.get(url, stream=True).raw), return_tensors="pt")

with torch.no_grad():
    image_features = clip_model.get_image_features(**image)
    text_features = text_encoder(text)[0]
    # sep_token对应于openai-clip的eot_token
    sep_index = torch.nonzero(text == student_tokenizer.sep_token_id)
    text_features = text_features[torch.arange(text.shape[0]), sep_index[:, 1]]
    # 乘text投影矩阵
    text_features = clip_text_proj(text_features)
    image_features = image_features / image_features.norm(dim=1, keepdim=True)
    text_features = text_features / text_features.norm(dim=1, keepdim=True)
    # 计算余弦相似度 logit_scale是尺度系数
    logit_scale = clip_model.logit_scale.exp()
    logits_per_image = logit_scale * image_features @ text_features.t()
    logits_per_text = logits_per_image.t()
    probs = logits_per_image.softmax(dim=-1).cpu().numpy()
    print(np.around(probs, 3))

扩散模型 Diffusion Model

我们的数据采用了筛选过的Laion数据集作为训练数据,同时在文本前面加上了 'sai-v1 art' 作为tag使模型能够更快速的学习到我们想要的风格及质量。 预训练模型采用了stable-diffusion-v1-5 作为预训练,使用了16块A100训练了50个小时。 目前模型还在持续优化中,后续会有更稳定的模型更新。

License

加入开发者群

微信扫码,加入开发者群——

paint

感兴趣别忘了star一下~

image

skypaint-ai-diffusion's People

Contributors

skyworkaigc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

skypaint-ai-diffusion's Issues

执行 pipe = StableDiffusionPipeline.from_pretrained("./SkyPaint").to(device) 出现如下错误

ValueError: The component <class 'transformers.models.clip.image_processing_clip.CLIPImageProcessor'> of <class 'diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline'> cannot be loaded as it does not seem to have any of the loading methods defined in {'ModelMixin': ['save_pretrained', 'from_pretrained'], 'SchedulerMixin': ['save_config', 'from_config'], 'DiffusionPipeline': ['save_pretrained', 'from_pretrained'], 'OnnxRuntimeModel': ['save_pretrained', 'from_pretrained'], 'PreTrainedTokenizer': ['save_pretrained', 'from_pretrained'], 'PreTrainedTokenizerFast': ['save_pretrained', 'from_pretrained'], 'PreTrainedModel': ['save_pretrained', 'from_pretrained'], 'FeatureExtractionMixin': ['save_pretrained', 'from_pretrained']}.

能否解答一下,非常感谢

[使用skyclip 计算image-text similarity的代码跑不通]

from PIL import Image
import requests
import clip
import torch
from transformers import BertTokenizer
from transformers import CLIPProcessor, CLIPModel, CLIPTextModel
import numpy as np

query_texts = ['一个人', '一辆汽车', '两个男人', '两个女人']  # 这里是输入提示词,可以随意替换。
# 加载SkyCLIP 中英文双语 text_encoder
text_tokenizer = BertTokenizer.from_pretrained("./tokenizer")
text_encoder = CLIPTextModel.from_pretrained("./text_encoder").eval()
text = text_tokenizer(query_texts, return_tensors='pt', padding=True)['input_ids']

url = "http://images.cocodataset.org/val2017/000000040083.jpg"  #这里可以换成任意图片的url
# 加载CLIP的image encoder
clip_model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")
clip_text_proj = clip_model.text_projection
processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
image = processor(images=Image.open(requests.get(url, stream=True).raw), return_tensors="pt")

with torch.no_grad():
   image_features = clip_model.get_image_features(**image)
   text_features = text_encoder(text)[0]
   # sep_token对应于openai-clip的eot_token
   sep_index = torch.nonzero(text == student_tokenizer.sep_token_id)
   text_features = text_features[torch.arange(text.shape[0]), sep_index[:, 1]]
   # 乘text投影矩阵
   text_features = clip_text_proj(text_features)
   image_features = image_features / image_features.norm(dim=1, keepdim=True)
   text_features = text_features / text_features.norm(dim=1, keepdim=True)
   # 计算余弦相似度 logit_scale是尺度系数
   logit_scale = clip_model.logit_scale.exp()
   logits_per_image = logit_scale * image_features @ text_features.t()
   logits_per_text = logits_per_image.t()
   probs = logits_per_image.softmax(dim=-1).cpu().numpy()
   print(np.around(probs, 3))

请问一下代码里 tokenizer 和 text_encoder分别是啥?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.