[{"data":1,"prerenderedAt":339},["ShallowReactive",2],{"content-query-ksuPaxgkcQ":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":333,"_id":334,"_source":335,"_file":336,"_stem":337,"_extension":338},"/technology-blogs/zh/613","zh",false,"","“中文版DALL-E”——清华40亿参数文本图像生成模型CogView论文分析","首个支持中文的VQ-VAE+Transformer的文本图像生成算法框架","2021-06-11","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/11/83e5f5f22b05488dbedb0d5c23ac35b7.png","technology-blogs","大V博文",{"type":15,"children":16,"toc":323},"root",[17,25,47,62,67,79,91,103,113,118,123,128,137,142,150,158,163,171,176,184,189,197,202,209,214,221,226,233,238,246,253,258,266,271,278,283,291,298,303,311,318],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"中文版dall-e清华40亿参数文本图像生成模型cogview论文分析",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29,31,39,41],{"type":24,"value":30},"作者：于璠 作者主页：",{"type":18,"tag":32,"props":33,"children":37},"a",{"href":34,"rel":35},"https://www.zhihu.com/people/yu-fan-42-9",[36],"nofollow",[38],{"type":24,"value":34},{"type":24,"value":40}," 文章来源：",{"type":18,"tag":32,"props":42,"children":45},{"href":43,"rel":44},"https://zhuanlan.zhihu.com/p/378567445",[36],[46],{"type":24,"value":43},{"type":18,"tag":26,"props":48,"children":49},{},[50,54,58],{"type":18,"tag":32,"props":51,"children":53},{"href":43,"rel":52},[36],[],{"type":18,"tag":32,"props":55,"children":57},{"href":43,"rel":56},[36],[],{"type":18,"tag":32,"props":59,"children":61},{"href":43,"rel":60},[36],[],{"type":18,"tag":26,"props":63,"children":64},{},[65],{"type":24,"value":66},"OpenAI在今年一月份发布了120亿参数规模的DALL-E模型，能够根据一段文本描述生成效果惊人的图像，不管多么天马行空的文字都能应对，被称之为“图像版的GPT-3“。而在最近，清华大学公布了中文文本生成图像的新框架——CogView，除了支持中文语言外，在多项性能指标上超越DALL-E，且能通过finetune适应多种不同的下游任务。CogView的论文、代码及Demo网站皆已公布，下面让我们来一探究竟吧!",{"type":18,"tag":26,"props":68,"children":69},{},[70,72],{"type":24,"value":71},"· 代码：",{"type":18,"tag":32,"props":73,"children":76},{"href":74,"rel":75},"https://link.zhihu.com/?target=https%3A//github.com/THUDM/CogView",[36],[77],{"type":24,"value":78},"https://github.com/THUDM/CogViewgithub.com",{"type":18,"tag":26,"props":80,"children":81},{},[82,84],{"type":24,"value":83},"· demo：",{"type":18,"tag":32,"props":85,"children":88},{"href":86,"rel":87},"https://link.zhihu.com/?target=https%3A//lab.aminer.cn/cogview/index.html",[36],[89],{"type":24,"value":90},"知乎 - 安全中心lab.aminer.cn",{"type":18,"tag":26,"props":92,"children":93},{},[94,96],{"type":24,"value":95},"论文链接：",{"type":18,"tag":32,"props":97,"children":100},{"href":98,"rel":99},"https://link.zhihu.com/?target=https%3A//arxiv.org/abs/2105.13290",[36],[101],{"type":24,"value":102},"https://arxiv.org/abs/2105.1329arxiv.org",{"type":18,"tag":104,"props":105,"children":107},"h2",{"id":106},"背景",[108],{"type":18,"tag":109,"props":110,"children":111},"strong",{},[112],{"type":24,"value":106},{"type":18,"tag":26,"props":114,"children":115},{},[116],{"type":24,"value":117},"文本图像生成(text-to-image)生成任务最早可以追溯到深度生成模型DRAW[1]的提出，而在之后很长一段时间里被GAN(Generative Adversarial Nets)类的方法所统治。虽然GAN天生对于生成类的任务有着很好的契合度，但是基于GAN的方法只在简单的或者特定领域中的数据集上取得了不错的效果，对于诸如MS COCO这类复杂且没有限定领域的数据集，其表现只能说差强人意。",{"type":18,"tag":26,"props":119,"children":120},{},[121],{"type":24,"value":122},"近几年随着NLP领域“自回归“(auto-regressive)模型的崛起，以GPT(Generative Pre-Training)[2]为代表的大规模预训练语言模型在各个语言类任务中展现出来惊人的实力。同时，这类自回归模型纷纷开始涉足CV类的任务，但是现实世界中的图像往往具有数以百万级的像素，如果没有合适的特征压缩方法，单靠语言模型是难以处理的，而VQ-VAE[3]恰好就是那个“合适”的图像特征压缩方法。",{"type":18,"tag":26,"props":124,"children":125},{},[126],{"type":24,"value":127},"VQ-VAE同VAE(Variational Auto-Encoder)最大的区别就在于，其隐变量 zz 的每一维都是离散的整数，这步离散化的操作是通过VQ-VAE结构中的embedding table实现的：具体操作就是将图片经过Encoder处理后得到的特征向量通过“查表操作“找到embedding table中与其最相近的一个embedding，并用该embedding的index进行离散化的表示。相比于直接进行降采样，VQ-VAE的离散化操作失真度低，同时保留了像素间的关系。",{"type":18,"tag":104,"props":129,"children":131},{"id":130},"cogview的结构",[132],{"type":18,"tag":109,"props":133,"children":134},{},[135],{"type":24,"value":136},"CogView的结构",{"type":18,"tag":26,"props":138,"children":139},{},[140],{"type":24,"value":141},"CogView整体的算法框架与DALL-E相比更为简洁，文本部分的token转换已经十分成熟，下文不再做进一步介绍；图像则是通过一个离散化的AE(Auto-Encoder)转换为token。文本token和图像token拼接到一起之后输入到GPT模型中生成图像。最后在处理文本图像生成类任务时，模型会通过计算一个Caption Score对生成图像进行排序，从而选择与文本最为匹配的图像作为结果。",{"type":18,"tag":26,"props":143,"children":144},{},[145],{"type":18,"tag":146,"props":147,"children":149},"img",{"alt":7,"src":148},"https://pic4.zhimg.com/80/v2-6b3e89986524e0e19e21de62cbdab36b_720w.jpg",[],{"type":18,"tag":26,"props":151,"children":152},{},[153],{"type":18,"tag":109,"props":154,"children":155},{},[156],{"type":24,"value":157},"Image Tokenizer",{"type":18,"tag":26,"props":159,"children":160},{},[161],{"type":24,"value":162},"前文提到CogView通过一个离散化的AE作为图像分词器，该结构同VQ-VAE或者是DALL-E中采用的d-VAE[4]的阶段一结构十分相似。具体而言，图像分词器首先通过Encoder 将H*W*3的图像转换为h*w*d特征向量，之后特征向量中每一个长度为d的向量都会通过“查表操作”转化为一个embedding的index，最终变为 h*w的量化向量。而Decoder 的作用是将量化后的特征重构为较为模糊的原始图像。整个Image Tokenizer的训练采用了VQ-VAE中的模式，这里不做过多介绍。",{"type":18,"tag":26,"props":164,"children":165},{},[166],{"type":18,"tag":109,"props":167,"children":168},{},[169],{"type":24,"value":170},"Transformer",{"type":18,"tag":26,"props":172,"children":173},{},[174],{"type":24,"value":175},"如图1所示，CogView的骨架网络是一个48层的GPT网络，隐层大小为2560，attention头部数量为40，总计达到了4亿的参数量。输入格式方面，利用4个分隔符来作为文本token和图像token的边界，文本token数量固定为1088，过长或者过短的情况下需要通过clip和pad的操作进行处理；图像token数量设置为1024。",{"type":18,"tag":104,"props":177,"children":179},{"id":178},"训练策略",[180],{"type":18,"tag":109,"props":181,"children":182},{},[183],{"type":24,"value":178},{"type":18,"tag":26,"props":185,"children":186},{},[187],{"type":24,"value":188},"大规模的文本图像生成预训练任务通常会由于训练数据之间的差异而变得不稳定，通常表现为Loss NaN和梯度弥散，CogView分别提出了PB-Relax(Precision Bottleneck Relaxation)和Sandwich-LN(Sandwich LayerNorm)来进行应对。",{"type":18,"tag":26,"props":190,"children":191},{},[192],{"type":18,"tag":109,"props":193,"children":194},{},[195],{"type":24,"value":196},"PB-Relax",{"type":18,"tag":26,"props":198,"children":199},{},[200],{"type":24,"value":201},"作者发现预训练过程中的数值溢出主要发生在Transformer的瓶颈层的最后一个LayerNorm或者attention处。经过进一步探索发现，网络深处的层输出过大会导致后续的LayerNorm溢出，于是利用了LayerNorm的如下性质对输入进行缩放：",{"type":18,"tag":26,"props":203,"children":204},{},[205],{"type":18,"tag":146,"props":206,"children":208},{"alt":7,"src":207},"https://pic4.zhimg.com/80/v2-2aaa404d5c004f98815c998942831ac7_720w.jpeg",[],{"type":18,"tag":26,"props":210,"children":211},{},[212],{"type":24,"value":213},"同时，attention分数的值过大也会导致数值溢出，这里利用了对 softmax的输入加上或者减去一个常数对于结果没有影响的性质，即：",{"type":18,"tag":26,"props":215,"children":216},{},[217],{"type":18,"tag":146,"props":218,"children":220},{"alt":7,"src":219},"https://pic3.zhimg.com/80/v2-58d5c03059b0709650a1d08bc338f0d2_720w.jpeg",[],{"type":18,"tag":26,"props":222,"children":223},{},[224],{"type":24,"value":225},"通过上述公式，attention层的计算改为如下公式：",{"type":18,"tag":26,"props":227,"children":228},{},[229],{"type":18,"tag":146,"props":230,"children":232},{"alt":7,"src":231},"https://pic1.zhimg.com/80/v2-309a8111e25ec5f36b498a42dc01ee00_720w.jpeg",[],{"type":18,"tag":26,"props":234,"children":235},{},[236],{"type":24,"value":237},"上述公式中的 alpha是一个非常大的数，通过除以 alpha 可以将attention分数的数值进行缩放，避免数值溢出。",{"type":18,"tag":26,"props":239,"children":240},{},[241],{"type":18,"tag":109,"props":242,"children":243},{},[244],{"type":24,"value":245},"Sandwich-LN",{"type":18,"tag":26,"props":247,"children":248},{},[249],{"type":18,"tag":146,"props":250,"children":252},{"alt":7,"src":251},"https://pic2.zhimg.com/80/v2-f6e05d59b6cd44d92eadf0152bbdafa9_720w.jpg",[],{"type":18,"tag":26,"props":254,"children":255},{},[256],{"type":24,"value":257},"顾名思义，Sandwich-LN就是堆叠成“三明治”形状的Layer Norm组合，CogView对Transformer的结构进行了改进，在残差分支处的FFN(全连接层)和attention头的前后各加上了一个Layer Norm来对中间特征进行正则。实验证明，Sandwich-LN相较于早期提出的Post-LN和Pre-LN[5]能使训练过程变得更稳定。",{"type":18,"tag":104,"props":259,"children":261},{"id":260},"多样化的能力",[262],{"type":18,"tag":109,"props":263,"children":264},{},[265],{"type":24,"value":260},{"type":18,"tag":26,"props":267,"children":268},{},[269],{"type":24,"value":270},"CogView训练完成之后，可以通过进一步的finetune来适应不同的下游任务，例如风格迁移，超分辨率，图像描述和排名，以及时装设计等等。",{"type":18,"tag":26,"props":272,"children":273},{},[274],{"type":18,"tag":146,"props":275,"children":277},{"alt":7,"src":276},"https://pic3.zhimg.com/80/v2-08bd71f8c3470aa727f2902ab51b071a_720w.jpg",[],{"type":18,"tag":26,"props":279,"children":280},{},[281],{"type":24,"value":282},"值得一提的是，CogView还提出了一种用来评估图像和文本之间关联度的指标Caption Score(CapS)来作为文本图像生成任务中对图像进行排名的依据。也正是通过CapS，CogView不像DALL-E一样需要通过外接CLIP模型[6]才能对生成的图像进行排序。",{"type":18,"tag":104,"props":284,"children":286},{"id":285},"公开数据集表现",[287],{"type":18,"tag":109,"props":288,"children":289},{},[290],{"type":24,"value":285},{"type":18,"tag":26,"props":292,"children":293},{},[294],{"type":18,"tag":146,"props":295,"children":297},{"alt":7,"src":296},"https://pic2.zhimg.com/80/v2-13eeccc0f1db6d3f73ef44a65916a299_720w.jpg",[],{"type":18,"tag":26,"props":299,"children":300},{},[301],{"type":24,"value":302},"如上图所示，CogView在MS COCO的文本图像生成任务上不管是FID(Fréchet Inception Distance)指标还是IS指标都要优于DALL-E，而模型的总参数量仅为DALL-E的1/3。",{"type":18,"tag":104,"props":304,"children":306},{"id":305},"总结",[307],{"type":18,"tag":109,"props":308,"children":309},{},[310],{"type":24,"value":305},{"type":18,"tag":26,"props":312,"children":313},{},[314],{"type":18,"tag":32,"props":315,"children":317},{"href":98,"rel":316},[36],[],{"type":18,"tag":26,"props":319,"children":320},{},[321],{"type":24,"value":322},"作为首个支持中文的VQ-VAE+Transformer的文本图像生成算法框架，CogView交出了一份令人满意的答卷。不管是在benchmark表现，通过finetune所能支持下游任务的种类，还是算法框架的简洁度上，CogView都要更胜DALL-E一筹。同时，从text-to-image领域的发展趋势看，“大规模，大数据，多模态“的AI发展浪潮迟早会席卷到各个领域。AI的未来，可能将会是一个拼数据、拼算力的战场。",{"title":7,"searchDepth":324,"depth":324,"links":325},4,[326,328,329,330,331,332],{"id":106,"depth":327,"text":106},2,{"id":130,"depth":327,"text":136},{"id":178,"depth":327,"text":178},{"id":260,"depth":327,"text":260},{"id":285,"depth":327,"text":285},{"id":305,"depth":327,"text":305},"markdown","content:technology-blogs:zh:613.md","content","technology-blogs/zh/613.md","technology-blogs/zh/613","md",1776506138670]