人类的度量学习
项目描述
TensorFlow 相似性:人类的度量学习
TensorFlow Similarity 是一个用于相似性学习的TensorFlow库,其中包括自我监督学习、度量学习、相似性学习和对比学习等技术。TensorFlow Similarity 仍处于测试阶段,我们可能会推动重大更改。
介绍
Tensorflow Similarity 提供最先进的度量学习算法以及研究、训练、评估和服务基于相似性和对比的模型的所有必要组件。这些组件包括模型、损失、指标、采样器、可视化器和索引子系统,以使这一过程变得快速而简单。
使用 Tensorflow Similarity,您可以训练两种主要类型的模型:
-
自监督模型:用于学习未标记数据的一般数据表示,以提高标签较少的下游任务的准确性。例如,您可以使用 TensorFlow Similarity 支持的对比方法之一在大量未标记的图像上预训练模型,然后在小型标记数据集上对其进行微调以实现更高的准确度。要开始训练您自己的自我监督模型,请参阅此笔记本。
-
相似性模型:输出嵌入,允许您查找和聚类相似的示例,例如在大量示例中表示相同对象的图像。例如,如上所示,您可以训练一个相似性模型来从Oxford IIIT Pet Dataset中查找和聚类相似的、看不见的猫和狗图像,同时只对几个数据集类进行训练。要开始训练您自己的相似性模型,请参阅此笔记本。
什么是新的
-
【2022年5月】:0.16重大优化发布
- 感谢@chjort,增加了跨批内存(XBM)损失
- 感谢@dewball345,许多自我监督相关的改进
- 主要层和回调重构,使它们更快、更灵活。例如
EvalCallback(),现在支持拆分验证。有关完整更改,请参阅更改日志
-
[2022 年 1 月]:0.15 自我监督发布
- 增加了对自我监督对比学习的支持。包括 SimCLR、SimSiam 和 Barlow Twins。查看深入的hello world 笔记本以开始使用。
- 由于Abhishar Sinha添加了软最近邻损失
- 添加了 GenerlizedMeanPooling2D 支持,可提高 GlobalMeanPooling2D 的相似性匹配准确性。
- 许多速度优化和一般错误修复。
有关以前的更改和更多详细信息 - 请参阅更改日志
入门
安装
使用 pip 安装库。
注意:如果您已经安装了 tensorflow>=2.4,则可以省略 Tensorflow extra_require 键。
pip install --upgrade-strategy=only-if-needed tensorflow_similarity[tensorflow]
文档
详细的叙述笔记本是开始使用 TensorFlow Similarity 的好方法。可能有一个与您的数据或您的问题相似(如果不是,请告诉我们)。您可以通过单击 Google Colab 图标立即开始在 Google Colab 中使用示例。
具体功能的更多信息,可以查看API文档
要为项目做出贡献,请查看贡献指南
最小示例:MNIST 相似性
单击以展开并查看如何使用 TF.Similarity 在 mnist 上训练有监督的相似性模型
这是一个简单的示例,展示了如何在 MNIST 数据上训练 TensorFlow Similarity 模型。此示例说明了 TensorFlow Similarity 提供的一些主要组件以及它们如何组合在一起。更详细的介绍请参考hello_world notebook。
准备数据
TensorFlow Similarity 为各种数据集类型提供数据采样器,可平衡批次以确保更顺畅的训练。在此示例中,我们使用直接从 TensorFlow 数据集目录集成的多镜头采样器。
from tensorflow_similarity.samplers import TFDatasetMultiShotMemorySampler
# Data sampler that generates balanced batches from MNIST dataset
sampler = TFDatasetMultiShotMemorySampler(dataset_name='mnist', classes_per_batch=10)
Building a Similarity model
Building a TensorFlow Similarity model is similar to building a standard Keras model, except the output layer is usually a MetricEmbedding() layer that enforces L2 normalization and the model is instantiated as a specialized subclass SimilarityModel() that supports additional functionality.
from tensorflow.keras import layers
from tensorflow_similarity.layers import MetricEmbedding
from tensorflow_similarity.models import SimilarityModel
# Build a Similarity model using standard Keras layers
inputs = layers.Input(shape=(28, 28, 1))
x = layers.experimental.preprocessing.Rescaling(1/255)(inputs)
x = layers.Conv2D(64, 3, activation='relu')(x)
x = layers.Flatten()(x)
x = layers.Dense(64, activation='relu')(x)
outputs = MetricEmbedding(64)(x)
# Build a specialized Similarity model
model = SimilarityModel(inputs, outputs)
Training model via contrastive learning
To output a metric embedding, that are searchable via approximate nearest neighbor search, the model needs to be trained using a similarity loss. Here we are using the MultiSimilarityLoss(), which is one of the most efficient loss functions.
from tensorflow_similarity.losses import MultiSimilarityLoss
# Train Similarity model using contrastive loss
model.compile('adam', loss=MultiSimilarityLoss())
model.fit(sampler, epochs=5)
Building images index and querying it
Once the model is trained, reference examples must be indexed via the model index API to be searchable. After indexing, you can use the model lookup API to search the index for the K most similar items.
from tensorflow_similarity.visualization import viz_neigbors_imgs
# Index 100 embedded MNIST examples to make them searchable
sx, sy = sampler.get_slice(0,100)
model.index(x=sx, y=sy, data=sx)
# Find the top 5 most similar indexed MNIST examples for a given example
qx, qy = sampler.get_slice(3713, 1)
nns = model.single_lookup(qx[0])
# Visualize the query example and its top 5 neighbors
viz_neigbors_imgs(qx[0], qy[0], nns)
支持的算法
自我监督模型
- SimCLR
- 模拟暹罗
- 巴洛双胞胎
监督损失
- 三重损失
- PN损耗
- 多模拟损失
- 循环损失
- 软最近邻损失
指标
Tensorflow Similarity 提供了许多用于分类和检索评估的最常用指标。包含:
| 姓名 | 类型 | 描述 |
|---|---|---|
| 精确 | 分类 | |
| 记起 | 分类 | |
| F1分数 | 分类 | |
| 召回@K | 恢复 | |
| 二进制 NDCG | 恢复 |
引用
如果您在研究中使用 TensorFlow 相似性的任何部分,请引用此参考:
@article{EBSIM21,
title={TensorFlow Similarity: A Usable, High-Performance Metric Learning Library},
author={Elie Bursztein, James Long, Shun Lin, Owen Vallis, Francois Chollet},
journal={Fixme},
year={2021}
}
免责声明
这不是 Google 的官方产品。