[GenerativeAI] Contrasive Learning and CLIP

Resources

自监督学习: 人工智能的未来

  • 自监督学习属于无监督学习,目前有“特定任务”和“表征学习”两个分支。
  • 自监督的“表征学习”有生成式对比式两种。

 

 

 

对比学习教程笔记


对比学习论文综述总结,看了朱毅老师在B站讲的对比学习论文综述,这里稍微总结一下。视频笔记

深度对比学习综述 [论文综述]

 
 

四阶段发展 since 2018

年份名字简介引用
2018 InstDisc 提出实例判别和memory bank做对比学习 citation
2018 CPC 对比预测编码,图像语音文本强化学习全都能做 citation
2019 InvaSpread 一个编码器的端到端对比学习 citation
2019 CMC 多视角下的对比学习 citation
2019 MoCov1 无监督训练效果也很好 citation
2020 SimCLRv1 简单的对比学习 (数据增强 + MLP head + 大batch训练久) citation
2020 MoCov2 MoCov1 + improvements from SimCLRv1 citation
2020 SimCLRv2 大的自监督预训练模型很适合做半监督学习 citation
2020 BYOL 不需要负样本的对比学习 citation
2020 SWaV 聚类对比学习 citation
2020 SimSiam 化繁为简的孪生表征学习 citation
2021 MoCov3 如何更稳定的自监督训练ViT citation
2021 DINO transformer加自监督在视觉也很香 citation

 

 

Ref: 自监督对比学习系列一 [非常好]

发现“数据增强”很重要,但只有两个会非常有效:crop and colour,这就是yolov5 (2020) 的内置特点。

 

MoCov2 的消融对比实验如下,说明“对比”需要与“MLP”相结合 才是最好的伴侣。

 

SWaV选择与聚类的中心做对比,引入了如下的c(centre)。另外,发现 multi-crop 也很有用,与yolov5有点类似。

 

  • SimSiam

不用负样本,为何模型训练没有坍塌?

BN的意义在于:当前图片与平均图片(类似聚类中心)在做隐式对比? No!

 

SimSiam三个牛逼之处:

    1. 不需要负样本。左脚踩右脚就上天啦!
    2. 不需要大的batch size。
    3. 不需要动量编码器 momentum encoders。

下游任务图表:https://youtu.be/1pvxufGRuW4?t=4555

 

  还没来得及继续发展,已被Transformer淹没。 

 

  • Mocov3: Self-supervised ResNet and ViT

Code: https://github.com/facebookresearch/moco-v3

 

  • DINO(segmentation效果非常好)

Emerging Properties in Self-Supervised Vision Transformers

centering:在一个minibatch里求均值,然后在用样本减去这个均值,作用 类似于BYOL中BN的操作。

 

与mocov3比较像。

 

总结一下

自监督在引入vit后,dino的发现对之后的segment anything带来了可能。

MAE火爆后,开始掩码学习。

 

 

 

 

CLIP (Contrastive Language–Image Pre-training) 精读


提出 Prompt Engineering

CLIP是一种基于对比学习的多模态模型,与CV中的一些对比学习方法如moco和simclr不同的是,CLIP的训练数据是文本-图像对:一张图像和它对应的文本描述,这里希望通过对比学习,模型能够学习到文本-图像对的匹配关系。

4亿个对儿 作为训练数据。

Prompt engineer: word (label) --> sentence.   # 与传统object classification的一个区别

Paper: https://openai.com/research/clip

Learning Transferable Visual Models From Natural Language Supervision (Feb 2021)

 

推理时,不得不tricky的一个地方是:把一个单词变成一个句子,然后再与需要分类的图片做相似度对比。从而引出了“prompt template”的概念。

 

题外话:大模型时代,如何训练呢?

How to Train Really Large Models on Many GPUs?(视频中提及的推荐博客,就职于OpenAI)

 

实战学习

同时期,OpenAI 发布了两大 multimodal 的成果。

Ref: [GenerativeAI] GAN to Diffusion  # 在该链接中再探讨sd。

OpenAI 的 multimodal 神經網路(上)DALL·E: 從文本創造圖片

Dall-E 1 was announced in January 2021, while Dall-E 2 came out in April 2022. With the original Dall-E, OpenAI used a dVAE to generate images. Dall-E 2 uses a diffusion model that can generate higher quality images.

 

这里重点关注下以下部分。

OpenAI 的 multimodal 神經網路 (下) CLIP: Connecting Text and Images

讓AI幫你穿搭!用CLIP實作一個時尚穿搭資料庫

 

  • 关键卖点

1. CLIP 很高效
這邊 OpenAI 做了兩個關鍵選擇,首先,他們本來考慮使用跟 VirTex 一樣的 image encoder + text decoder 的架構,但是當 scale 大的時候,就無法達到好的 performance。

後來採用了 contrasive objective 的方法,兩個都是 encoder。結果快 4 到 10 倍。第二個選擇是採用了 Visual Transformer,比 ResNet 快 3 倍。

2. CLIP 很有彈性也很通用
因為直接從自然語言學到超廣域的影像概念,CLIP 的泛用性遠遠超過一般用 ImageNet 訓練出來的 model。OpenAI 把 CLIP 放到 30 個不同領域的 dataset 去做 zero-shot classification 都有不錯的表現,特別值得一提的是 OCR,因為 ImageNet model 通常不適用 OCR(因為是完全不同的 domain)。

反应了文字与图片的对应能力是可行的!也是认知的内在秘密~

 

  • 限制

CLIP 雖然強大,還是有弱點的。

對於一些抽象任務或是系統性任務它就失敗了,比方說計算圖片裡面有幾個物體,或是問它圖片裡面最接近的兩台車有多近,它的答案基本上只比亂猜好一點。還有一些針對特定領域的 fine-grained classification 問題的表現也不好,比方說問它這台車的車型,它就做不好。

還有,如果是 pre-training 的 dataset 裡面缺乏的領域,那就真的做得不大好,比方說他雖然能做 OCR,但是 pretrain dataset 沒有 MNIST,給他 MNIST 的手寫文字辨識,他只能做到 88%,一般特定 model 就可以做到 99.75%。

 

看来,一旦有了不错的 CLIP模型,对于图文检索,就好办了。尤其是“一段文字输入 检索相关的站内图片”

 

  • 关键代码

Ref: Natural language image search with a Dual Encoder

    • Image Embeding

这里使用了Xception,但VIT可能会更好一些。

def create_vision_encoder(
    num_projection_layers, projection_dims, dropout_rate, trainable=False
):
    # Load the pre-trained Xception model to be used as the base encoder.
    xception = keras.applications.Xception(
        include_top=False, weights="imagenet", pooling="avg"
    )
    # Set the trainability of the base encoder.
    for layer in xception.layers:
        layer.trainable = trainable
    # Receive the images as inputs.
    inputs = layers.Input(shape=(299, 299, 3), name="image_input")
    # Preprocess the input image.
    xception_input = tf.keras.applications.xception.preprocess_input(inputs)
    # Generate the embeddings for the images using the xception model.
    embeddings = xception(xception_input)
    # Project the embeddings produced by the model.
    outputs = project_embeddings(
        embeddings, num_projection_layers, projection_dims, dropout_rate
    )
    # Create the vision encoder model.
    return keras.Model(inputs, outputs, name="vision_encoder")
View Code
    • Text Embedding 

 利用bert与训练模型生成词向量。

def create_text_encoder(
    num_projection_layers, projection_dims, dropout_rate, trainable=False
):
    # Load the BERT preprocessing module.
    preprocess = hub.KerasLayer(
        "https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2",
        name="text_preprocessing",
    )
    # Load the pre-trained BERT model to be used as the base encoder.
    bert = hub.KerasLayer(
        "https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-512_A-8/1",
        "bert",
    )
    # Set the trainability of the base encoder.
    bert.trainable = trainable
    # Receive the text as inputs.
    inputs = layers.Input(shape=(), dtype=tf.string, name="text_input")
    # Preprocess the text.
    bert_inputs = preprocess(inputs)
    # Generate embeddings for the preprocessed text using the BERT model.
    embeddings = bert(bert_inputs)["pooled_output"]
    # Project the embeddings produced by the model.
    outputs = project_embeddings(
        embeddings, num_projection_layers, projection_dims, dropout_rate
    )
    # Create the text encoder model.
    return keras.Model(inputs, outputs, name="text_encoder")
View Code

 

 

 

 

CLIP 改进工作(后续发展)


  1. 03:28 Lseg
  2. 18:27 GroupViT
  3. 37:43 ViLD
  4. 58:22 GLIP (June 2022)
  5. 00:00 CLIPasso
  6. 24:11 CLIP4Clip
  7. 37:40 ActionCLIP
  8. 54:23 CLIP-ViL
  9. 55:55 AudioCLIP
  10. 57:27 PointCLIP
  11. 59:06 DepthCLIP

 

Lseg

Language-driven Semantic Segmentation

 

GroupViT(CLIP在分割领域的应用)

GroupViT: Semantic Segmentation Emerges from Text Supervision

 

什么是GroupViT?

简而言之,通过一个tricky的 gumbel softmax 做聚类中心的分配,以便“可导”。

只使用了图像文本对儿

该模型的缺陷是:语义理解不太好,导致分类出错率高。而实则视觉分割其实做的不错。

 

ViLD(CLIP在检测领域的应用)

Open-vocabulary Object Detection via Vision and Language Knowledge Distillation

在CLIP出现的两个月后,就写出论文。

 

GLIP

Grounded Language-Image Pre-training

GLIP将 object detectionphrase grounding 结合起来进行预训练。

这有两个好处:

    1. GLIP可以同时从 detection 和 grounding 数据中训练学习,以改进两种任务,训练一个优秀的 grounding 模型;
    2. GLIP可以通过 self-training 的方式生成 grounding boxes(即伪标签)来利用大量的图像文本对数据,使学习到的视觉表征具有丰富的语义。

实验上,作者对27M grounding data 进行预训练(包括3M人工注释和24M网络爬取的图像文本对)。训练学习到的视觉表征在各种目标级别的识别任务中都具有较强的zero/few shot迁移能力。

重点来了,(下),轮廓到图像。

posted @ 2021-05-05 22:15  郝壹贰叁  阅读(111)  评论(0编辑  收藏  举报