{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "jg3_YfhpC1JG"
   },
   "source": [
    "# 基于图片相似度的图片搜索\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 简要介绍\n",
    "\n",
    "图片搜索是一种有着广泛的应用场景的深度学习技术的应用，目前，无论是工程图纸的检索，还是互联网上相似图片的搜索，都基于深度学习算法能够实现很好的基于给定图片，检索出跟该图片相似的图片的效果。\n",
    "\n",
    "本示例简要介绍如何通过飞桨开源框架，实现图片搜索的功能。其基本思路是，先将图片使用卷积神经网络转换为高维空间的向量表示，然后计算两张图片的高维空间的向量表示之间的相似程度(本示例中，我们使用余弦相似度)。在模型训练阶段，其训练目标是让同一类别的图片的相似程度尽可能的高，不同类别的图片的相似程度尽可能的低。在模型预测阶段，对于用户上传的一张图片，会计算其与图片库中图片的相似程度，返回给用户按照相似程度由高到低的图片的列表作为检索的结果。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 环境设置\n",
    "\n",
    "本示例基于飞桨开源框架2.0版本。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "id": "hhxOPw1SC1JI",
    "outputId": "6130c395-d0e3-4566-86c6-fd4de9aed371"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.0-beta0\n"
     ]
    }
   ],
   "source": [
    "import paddle\n",
    "import paddle.nn.functional as F\n",
    "import numpy as np\n",
    "import random\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image\n",
    "from collections import defaultdict\n",
    "\n",
    "paddle.disable_static()\n",
    "print(paddle.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据集\n",
    "\n",
    "本示例采用[CIFAR-10](https://www.cs.toronto.edu/~kriz/cifar.html)数据集。这是一个经典的数据集，由50000张图片的训练数据，和10000张图片的测试数据组成，其中每张图片是一个RGB的长和宽都为32的图片。使用`paddle.dataset.cifar`可以方便的完成数据的下载工作，把数据归一化到`(0, 1.0)`区间内，并提供迭代器供按顺序访问数据。我们会把训练数据和测试数据分别存放在两个`numpy`数组中，供后面的训练和评估来使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 68
    },
    "colab_type": "code",
    "id": "czLF_NIuC1JW",
    "outputId": "be9661e0-af81-4137-96fb-1dadb96c0726"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(50000, 3, 32, 32)\n",
      "(50000,)\n"
     ]
    }
   ],
   "source": [
    "cifar10_train = paddle.vision.datasets.cifar.Cifar10(mode='train', transform=None)\n",
    "x_train = np.zeros((50000, 3, 32, 32))\n",
    "y_train = np.zeros((50000, 1), dtype='int32')\n",
    "\n",
    "for i in range(len(cifar10_train)):\n",
    "    train_image, train_label = cifar10_train[i]\n",
    "    train_image = train_image.reshape((3,32,32 ))\n",
    "    \n",
    "    # normalize the data\n",
    "    x_train[i,:, :, :] = train_image / 255.\n",
    "    y_train[i, 0] = train_label\n",
    "\n",
    "y_train = np.squeeze(y_train)\n",
    "\n",
    "print(x_train.shape)\n",
    "print(y_train.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 68
    },
    "colab_type": "code",
    "id": "lJDRE4a9QaJo",
    "outputId": "ceed15eb-9c2c-4051-8009-0444136b5462"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10000, 3, 32, 32)\n",
      "(10000,)\n"
     ]
    }
   ],
   "source": [
    "cifar10_test = paddle.vision.datasets.cifar.Cifar10(mode='test', transform=None)\n",
    "x_test = np.zeros((10000, 3, 32, 32), dtype='float32')\n",
    "y_test = np.zeros((10000, 1), dtype='int64')\n",
    "\n",
    "for i in range(len(cifar10_test)):\n",
    "    test_image, test_label = cifar10_test[i]\n",
    "    test_image = test_image.reshape((3,32,32 )) \n",
    "   \n",
    "    # normalize the data\n",
    "    x_test[i,:, :, :] = test_image / 255.\n",
    "    y_test[i, 0] = test_label\n",
    "\n",
    "y_test = np.squeeze(y_test)\n",
    "\n",
    "print(x_test.shape)\n",
    "print(y_test.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据探索\n",
    "\n",
    "接下来我们随机从训练数据里找一些图片，浏览一下这些图片。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 357
    },
    "colab_type": "code",
    "id": "JhjoI3w7C1JY",
    "outputId": "4bd42a22-2a48-4296-f0af-5172d8f46793"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 225712 characters]\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=340x340 at 0x14A1BB9D0>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "height_width = 32\n",
    "\n",
    "def show_collage(examples):\n",
    "    box_size = height_width + 2\n",
    "    num_rows, num_cols = examples.shape[:2]\n",
    "\n",
    "    collage = Image.new(\n",
    "        mode=\"RGB\",\n",
    "        size=(num_cols * box_size, num_rows * box_size),\n",
    "        color=(255, 255, 255),\n",
    "    )\n",
    "    for row_idx in range(num_rows):\n",
    "        for col_idx in range(num_cols):\n",
    "            array = (np.array(examples[row_idx, col_idx]) * 255).astype(np.uint8)\n",
    "            array = array.transpose(1,2,0)\n",
    "            collage.paste(\n",
    "                Image.fromarray(array), (col_idx * box_size, row_idx * box_size)\n",
    "            )\n",
    "\n",
    "    collage = collage.resize((2 * num_cols * box_size, 2 * num_rows * box_size))\n",
    "    return collage\n",
    "\n",
    "sample_idxs = np.random.randint(0, 50000, size=(5, 5))\n",
    "examples = x_train[sample_idxs]\n",
    "show_collage(examples)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构建训练数据\n",
    "\n",
    "图片检索的模型的训练样本跟我们常见的分类任务的训练样本不太一样的地方在于，每个训练样本并不是一个`(image, class)`这样的形式。而是（image0, image1, similary_or_not)的形式，即，每一个训练样本由两张图片组成，而其`label`是这两张图片是否相似的标志位（0或者1）。\n",
    "\n",
    "很自然的我们能够想到，来自同一个类别的两张图片，是相似的图片，而来自不同类别的两张图片，应该是不相似的图片。\n",
    "\n",
    "为了能够方便的抽样出相似图片（以及不相似图片）的样本，我们先建立能够根据类别找到该类别下所有图片的索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "-L3amT6BC1Jm"
   },
   "outputs": [],
   "source": [
    "class_idx_to_train_idxs = defaultdict(list)\n",
    "for y_train_idx, y in enumerate(y_train):\n",
    "    class_idx_to_train_idxs[y].append(y_train_idx)\n",
    "\n",
    "class_idx_to_test_idxs = defaultdict(list)\n",
    "for y_test_idx, y in enumerate(y_test):\n",
    "    class_idx_to_test_idxs[y].append(y_test_idx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有了上面的索引，我们就可以为飞桨准备一个读取数据的迭代器。该迭代器每次生成`2 * number of classes`张图片，在CIFAR10数据集中，这会是20张图片。前10张图片，和后10张图片，分别是10个类别中每个类别随机抽出的一张图片。这样，在实际的训练过程中，我们就会有10张相似的图片和90张不相似的图片（前10张图片中的任意一张图片，都与后10张的对应位置的1张图片相似，而与其他9张图片不相似）。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Bj3vLn2rC1Jq"
   },
   "outputs": [],
   "source": [
    "num_classes = 10\n",
    "\n",
    "def reader_creator(num_batchs):\n",
    "    def reader():\n",
    "        iter_step = 0\n",
    "        while True:\n",
    "            if iter_step >= num_batchs:\n",
    "                break\n",
    "            iter_step += 1\n",
    "            x = np.empty((2, num_classes, 3, height_width, height_width), dtype=np.float32)\n",
    "            for class_idx in range(num_classes):\n",
    "                examples_for_class = class_idx_to_train_idxs[class_idx]\n",
    "                anchor_idx = random.choice(examples_for_class)\n",
    "                positive_idx = random.choice(examples_for_class)\n",
    "                while positive_idx == anchor_idx:\n",
    "                    positive_idx = random.choice(examples_for_class)\n",
    "                x[0, class_idx] = x_train[anchor_idx]\n",
    "                x[1, class_idx] = x_train[positive_idx]\n",
    "            yield x\n",
    "\n",
    "    return reader\n",
    "\n",
    "\n",
    "# num_batchs: how many batchs to generate\n",
    "def anchor_positive_pairs(num_batchs=100):\n",
    "    return reader_creator(num_batchs)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "zIxWRe9GC1Jt"
   },
   "outputs": [],
   "source": [
    "pairs_train_reader = anchor_positive_pairs(num_batchs=1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "拿出第一批次的图片，并可视化的展示出来，如下所示。（这样更容易理解训练样本的构成）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 153
    },
    "colab_type": "code",
    "id": "XeAYtBaSC1J1",
    "outputId": "f8e3ed0e-3f91-4e07-d83a-9c398d59ba59",
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 10, 3, 32, 32)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 186476 characters]\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=680x136 at 0x16C1DDA90>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "examples = next(pairs_train_reader())\n",
    "print(examples.shape)\n",
    "show_collage(examples)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 把图片转换为高维的向量表示的网络\n",
    "\n",
    "我们的目标是首先把图片转换为高维空间的表示，然后计算图片在高维空间表示时的相似度。\n",
    "下面的网络结构用来把一个形状为`(3, 32, 32)`的图片转换成形状为`(8,)`的向量。在有些资料中也会把这个转换成的向量称为`Embedding`，请注意，这与自然语言处理领域的词向量的区别。\n",
    "下面的模型由三个连续的卷积加一个全局均值池化，然后用一个线性全链接层映射到维数为8的向量空间。为了后续计算余弦相似度时的便利，我们还在最后用[l2_normalize](https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/l2_normalize_cn.html)做了归一化。（即，余弦相似度的分母部分）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "QKurk9QAC1J8"
   },
   "outputs": [],
   "source": [
    "class MyNet(paddle.nn.Layer):\n",
    "    def __init__(self):\n",
    "        super(MyNet, self).__init__()\n",
    "\n",
    "        self.conv1 = paddle.nn.Conv2d(in_channels=3, \n",
    "                                      out_channels=32, \n",
    "                                      kernel_size=(3, 3),\n",
    "                                      stride=2)\n",
    "         \n",
    "        self.conv2 = paddle.nn.Conv2d(in_channels=32, \n",
    "                                      out_channels=64, \n",
    "                                      kernel_size=(3,3), \n",
    "                                      stride=2)       \n",
    "        \n",
    "        self.conv3 = paddle.nn.Conv2d(in_channels=64, \n",
    "                                      out_channels=128, \n",
    "                                      kernel_size=(3,3),\n",
    "                                      stride=2)\n",
    "       \n",
    "        self.gloabl_pool = paddle.nn.AdaptiveAvgPool2d((1,1))\n",
    "\n",
    "        self.fc1 = paddle.nn.Linear(in_features=128, out_features=8)\n",
    "    def forward(self, x):\n",
    "        x = self.conv1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.conv2(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.conv3(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.gloabl_pool(x)\n",
    "        x = paddle.squeeze(x, axis=[2, 3])\n",
    "        x = self.fc1(x)\n",
    "        x = F.l2_normalize(x, axis=1)\n",
    "\n",
    "        return x\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在模型的训练过程中如下面的代码所示：\n",
    "\n",
    "- `inverse_temperature`参数起到的作用是让softmax在计算梯度时，能够处于梯度更显著的区域。（可以参考[attention is all you need](https://arxiv.org/abs/1706.03762)中，在点积之后的`scale`操作）。\n",
    "- 整个计算过程，会先用上面的网络分别计算前10张图片（anchors)的高维表示，和后10张图片的高维表示。然后再用[matmul](https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/matmul_cn.html)计算前10张图片分别与后10张图片的相似度。（所以`similarities`会是一个`(10, 10)`的Tensor）。\n",
    "- 为[softmax_with_cross_entropy](https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/softmax_with_cross_entropy_cn.html)构造类别标签时，则相应的，可以构造出来0 ~ num_classes的标签值，用来让学习的目标成为相似的图片的相似度尽可能的趋向于1.0，而不相似的图片的相似度尽可能的趋向于-1.0。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "colab_type": "code",
    "id": "v0qA9wX1C1KD",
    "outputId": "e8d1b3bd-5412-4081-f30a-d2303b041578"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "start training ... \n",
      "epoch: 0, batch_id: 0, loss is: [2.3078856]\n",
      "epoch: 0, batch_id: 500, loss is: [1.9325346]\n",
      "epoch: 1, batch_id: 0, loss is: [1.9889]\n",
      "epoch: 1, batch_id: 500, loss is: [2.0410695]\n",
      "epoch: 2, batch_id: 0, loss is: [2.2465641]\n",
      "epoch: 2, batch_id: 500, loss is: [1.8171736]\n",
      "epoch: 3, batch_id: 0, loss is: [1.9939486]\n",
      "epoch: 3, batch_id: 500, loss is: [2.1440036]\n",
      "epoch: 4, batch_id: 0, loss is: [2.1497147]\n",
      "epoch: 4, batch_id: 500, loss is: [2.3686018]\n",
      "epoch: 5, batch_id: 0, loss is: [1.938681]\n",
      "epoch: 5, batch_id: 500, loss is: [1.7729127]\n",
      "epoch: 6, batch_id: 0, loss is: [2.0061004]\n",
      "epoch: 6, batch_id: 500, loss is: [1.6132584]\n",
      "epoch: 7, batch_id: 0, loss is: [1.8874661]\n",
      "epoch: 7, batch_id: 500, loss is: [1.6153599]\n",
      "epoch: 8, batch_id: 0, loss is: [1.9407685]\n",
      "epoch: 8, batch_id: 500, loss is: [2.1532288]\n",
      "epoch: 9, batch_id: 0, loss is: [1.4792883]\n",
      "epoch: 9, batch_id: 500, loss is: [1.857158]\n",
      "epoch: 10, batch_id: 0, loss is: [2.1518302]\n",
      "epoch: 10, batch_id: 500, loss is: [1.790559]\n",
      "epoch: 11, batch_id: 0, loss is: [1.7292264]\n",
      "epoch: 11, batch_id: 500, loss is: [1.8555079]\n",
      "epoch: 12, batch_id: 0, loss is: [1.6968924]\n",
      "epoch: 12, batch_id: 500, loss is: [1.4554331]\n",
      "epoch: 13, batch_id: 0, loss is: [1.3950458]\n",
      "epoch: 13, batch_id: 500, loss is: [1.7197256]\n",
      "epoch: 14, batch_id: 0, loss is: [1.7336586]\n",
      "epoch: 14, batch_id: 500, loss is: [2.0465684]\n",
      "epoch: 15, batch_id: 0, loss is: [1.7675827]\n",
      "epoch: 15, batch_id: 500, loss is: [2.6443417]\n",
      "epoch: 16, batch_id: 0, loss is: [1.7331158]\n",
      "epoch: 16, batch_id: 500, loss is: [1.6207634]\n",
      "epoch: 17, batch_id: 0, loss is: [2.0908554]\n",
      "epoch: 17, batch_id: 500, loss is: [1.7711265]\n",
      "epoch: 18, batch_id: 0, loss is: [1.8717268]\n",
      "epoch: 18, batch_id: 500, loss is: [1.5269613]\n",
      "epoch: 19, batch_id: 0, loss is: [1.5681677]\n",
      "epoch: 19, batch_id: 500, loss is: [1.7821472]\n"
     ]
    }
   ],
   "source": [
    "def train(model):\n",
    "    print('start training ... ')\n",
    "    model.train()\n",
    "\n",
    "    inverse_temperature = paddle.to_tensor(np.array([1.0/0.2], dtype='float32'))\n",
    "\n",
    "    epoch_num = 20\n",
    "    \n",
    "    opt = paddle.optimizer.Adam(learning_rate=0.0001,\n",
    "                                parameters=model.parameters())\n",
    "    \n",
    "    for epoch in range(epoch_num):\n",
    "        for batch_id, data in enumerate(pairs_train_reader()):\n",
    "            anchors_data, positives_data = data[0], data[1]\n",
    "\n",
    "            anchors = paddle.to_tensor(anchors_data)\n",
    "            positives = paddle.to_tensor(positives_data)\n",
    "            \n",
    "            anchor_embeddings = model(anchors)\n",
    "            positive_embeddings = model(positives)\n",
    "      \n",
    "            similarities = paddle.matmul(anchor_embeddings, positive_embeddings, transpose_y=True) \n",
    "            similarities = paddle.multiply(similarities, inverse_temperature)\n",
    "            \n",
    "            sparse_labels = paddle.arange(0, num_classes, dtype='int64')\n",
    "            sparse_labels = paddle.reshape(sparse_labels, (num_classes, 1))\n",
    "\n",
    "            loss = F.softmax_with_cross_entropy(similarities, sparse_labels)\n",
    "            \n",
    "            avg_loss = paddle.mean(loss)\n",
    "            if batch_id % 500 == 0:\n",
    "                print(\"epoch: {}, batch_id: {}, loss is: {}\".format(epoch, batch_id, avg_loss.numpy()))\n",
    "            avg_loss.backward()\n",
    "            opt.step()\n",
    "            opt.clear_grad()\n",
    "\n",
    "model = MyNet()\n",
    "train(model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "v2izWWI9PutF"
   },
   "source": [
    "## 模型预测 \n",
    "\n",
    "前述的模型训练训练结束之后，我们就可以用该网络结构来计算出任意一张图片的高维向量表示（embedding)，通过计算该图片与图片库中其他图片的高维向量表示之间的相似度，就可以按照相似程度进行排序，排序越靠前，则相似程度越高。\n",
    "\n",
    "下面我们对测试集中所有的图片都两两计算相似度，然后选一部分相似的图片展示出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "IDE4kmUkPr_T"
   },
   "outputs": [],
   "source": [
    "near_neighbours_per_example = 10\n",
    "\n",
    "x_test_t = paddle.to_tensor(x_test)\n",
    "test_images_embeddings = model(x_test_t)\n",
    "similarities_matrix = paddle.matmul(test_images_embeddings, test_images_embeddings, transpose_y=True) \n",
    "\n",
    "indicies = paddle.argsort(similarities_matrix, descending=True)\n",
    "indicies = indicies.numpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "kgBsMRarC1KF"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1005044 characters]\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=748x680 at 0x16C1DDF10>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_collage_examples = 10\n",
    "\n",
    "examples = np.empty(\n",
    "    (\n",
    "        num_collage_examples,\n",
    "        near_neighbours_per_example + 1,\n",
    "        3,\n",
    "        height_width,\n",
    "        height_width,\n",
    "    ),\n",
    "    dtype=np.float32,\n",
    ")\n",
    "for row_idx in range(num_collage_examples):\n",
    "    examples[row_idx, 0] = x_test[row_idx]\n",
    "    anchor_near_neighbours = indicies[row_idx][1:near_neighbours_per_example+1]\n",
    "    for col_idx, nn_idx in enumerate(anchor_near_neighbours):\n",
    "        examples[row_idx, col_idx + 1] = x_test[nn_idx]\n",
    "\n",
    "show_collage(examples)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 328
    },
    "colab_type": "code",
    "id": "wrbuQDPHUBEi",
    "outputId": "3f46eb46-fa8e-4b9c-e11f-f9bbb1abf2d5"
   },
   "source": [
    "## The end\n",
    "\n",
    "上面展示的结果当中，每一行里其余的图片都是跟第一张图片按照相似度进行排序相似的图片。你也可以调整网络结构和超参数，以获得更好的结果。"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "name": "metric_learning.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
