SkyPaint文本生成图像模型主要由两部分组成,即提示词文本编码器模型和扩散模型。因此,我们的优化也分两步进行。首先,我们基于OpenAI-CLIP,优化提示词文本编码器模型,让SkyPaint具备识别中文和英文的能力,然后优化扩散模型,让SkyPaint具备现代艺术能力,并能产出高质量的图片。
[hidecontent type="logged" desc="隐藏内容:登录后可查看"]
下载型号: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是使用一种高效的中英双语CLIP模型训练方法得到的CLIP模型。该方法只需要使用文本数据就可以实现OpenAI-CLIP模型的高效蒸馏,大大降低了数据门槛。同时,训练要求与原有的CLIP模型相比,算力需求降低90%以上,便于开源社区复现/微调。该方法只改变了OpenAI-CLIP的文本编码器,可以配合OpenAI-CLIP的图像编码器实现图文检索功能。
使用OpenAI-CLIP的text_encoder作为教师模型,冻结参数。学生模型使用与教师模型大小相同的多语言 BERT 模型。训练时通过teacher模型获取英文输入获取对应的t_en_hiddent_state,通过student模型分别获取英文和中文。对应的s_en_hiddent_state、s_zh_hidden_state利用l1、l2、cos距离等构造损失函数,使得学生模型的中英文hidden_state逐渐接近教师模型的hidden_state。由于平行语料中的中文和英文天生不等长,为了让平行的中文和英文尽可能接近,我们在训练过程中还加入了中文解码器,并将student模型的中英文hidden_state作为decoder的hidden_state输入。翻译任务用于辅助中英文对齐。
目前我们主要评估SkyCLIP在Flickr30K-CN上的零镜头性能,主要比较几个相关开源模型的中文能力。对于L/14尺寸模型,我们的评测过程参考了Chinese-CLIP提供的评测脚本。
Flickr30K-CN 检索:
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))
我们的数据使用过滤后的Laion数据集作为训练数据,并在文本前面加上'sai-v1 art'作为标签,这样模型可以更快的学习到我们想要的风格和质量。预训练模型使用stable-diffusion-v1-5作为预训练,使用16个A100进行50小时的训练。目前模型还在优化中,后续会有更稳定的模型更新。
技术优势一:涵盖多种编程语言
不同的编程语言侧重于解决不同平台和环境下的问题,不同的编程语言有其存在的理由。Singularity SkyCode可以生成的代码不仅包括广泛使用的JavaScript、python、Java、C等,还涵盖了php、go、swift等十余种编程语言,让不同语言的用户都能体验到SkyCode强大的代码生成能力。
技术优势二:中文标注优化
在预训练大模型领域,一直是英文界的天下。基于GPT3的代码生成模型也存在同样的问题。依托深耕中文模型的经验,Singularity-AI根据中文特点优化创新了独特的中文编码方式,更符合中文语言习惯,使模型对中文标注的理解能力更好。
技术优势三:优秀的解决问题能力
在体现代码生成模型解决问题能力的HumanEval数据集上,SkyCode的解决问题能力也远高于其他开源模型。
Recommand
transformers>=4.18.0
# -*- coding: utf-8 -*-
from transformers import GPT2LMHeadModel
from transformers import AutoTokenizer
from transformers import TextGenerationPipeline
model = GPT2LMHeadModel.from_pretrained("SkyWork/SkyCode")
tokenizer = AutoTokenizer.from_pretrained("SkyWork/SkyCode", trust_remote_code=True)
text_generator = TextGenerationPipeline(model, tokenizer, device=0)
input_str = "if __name__"
max_new_tokens = 40
print(text_generator(input_str, max_new_tokens=max_new_tokens, do_sample=True))###
技术优势一:30多个进程的数据清洗
随着NLP技术的发展,预训练大型模型逐渐成为人工智能的核心技术之一。预训练大型模型通常需要大量文本进行训练,网络文本自然成为最重要的语料来源。训练语料的好坏无疑直接影响到模型的效果。为了训练出能力出众的模型,Singularity-AI在数据清洗上使用了30多个清洗流程。细节精益求精,铸就卓越典范效果。
技术优势二:针对中文优化创新的中文编码方式
在预训练大模型领域,一直以来都是英文界的天下,中文预训练大模型的重要性不言而喻。与英文不同,中文预训练大模型的中文输入法(拼音文本)应该明显不同。Singularity-AI根据汉语的特点,优化创新了独有的汉语编码方式,更符合汉语语言习惯,重建了更有利于模型理解的汉语词典。
Recommand
transformers>=4.18.0
# -*- coding: utf-8 -*-
from transformers import GPT2LMHeadModel
from transformers import AutoTokenizer
from transformers import TextGenerationPipeline
# 13Billions
model = GPT2LMHeadModel.from_pretrained("SkyWork/SkyText")
tokenizer = AutoTokenizer.from_pretrained("SkyWork/SkyText", trust_remote_code=True)
# or 2.6Billions
model = GPT2LMHeadModel.from_pretrained("SkyWork/SkyTextTiny")
tokenizer = AutoTokenizer.from_pretrained("SkyWork/SkyTextTiny", trust_remote_code=True)
text_generator = TextGenerationPipeline(model, tokenizer, device=0)
input_str = "Today is a "
max_new_tokens = 20
print(text_generator(input_str, max_new_tokens=max_new_tokens, do_sample=True))
conda create -n semantic
create new env conda info --envs
check environments activate semantic
激活它 cd [YOUR PATH]\semantic_score_clean\semantic_score_clean
导航到提取的“semantic_score_clean”文件夹 python -m pip uninstall numpy
删除默认的 numpy python -m pip install -r requirements.txt
安装要求 python semantic_score_api.py
使语义服务联机conda create -n userprofile
创建新的 conda info --envs
环境检查环境 activate userprofile
激活它 cd [YOUR PATH]\user_profile_clean\user_profile_clean
导航到提取的“user_profile_clean”文件夹 python -m pip uninstall numpy
删除默认的 numpy python -m pip install -r requirements.txt
安装要求 python -m pip install torch==1.11.0+cu115 -f https://download.pytorch.org/whl/torch_stable.html
下载 cuda 版本的 torch python server_v3.py
使 userprofile 服务在线\[YOUR PATH]\OpenAPIDemo\PythonDemo\main.py
填写密钥和秘密conda create -n talk
create new env activate talk
activate it cd \[YOUR PATH]\OpenAPIDemo\PythonDemo
navigate to your Demo path python -m pip install requests
Install requirements python main.py
Begin to chat(确保'Semantic'和'UserProfile'服务已经设置好)[/hidecontent]