首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当输入时我只有两个文件时,如何处理三元组丢失?

当输入时我只有两个文件时,如何处理三元组丢失?
EN

Stack Overflow用户
提问于 2019-06-22 23:12:10
回答 1查看 513关注 0票数 0

我正在实现一个连体网络,在这个网络中,我知道如何通过选择锚点来计算三重损失,通过将输入分成三部分(这是一个手工制作的特征向量),然后在训练时计算它。

代码语言:javascript
复制
anchor_output = ...  # shape [None, 128]
positive_output = ...  # shape [None, 128]
negative_output = ...  # shape [None, 128]

d_pos = tf.reduce_sum(tf.square(anchor_output - positive_output), 1)
d_neg = tf.reduce_sum(tf.square(anchor_output - negative_output), 1)

loss = tf.maximum(0., margin + d_pos - d_neg)
loss = tf.reduce_mean(loss)

但问题是,当在测试时,我将只有两个文件,积极和消极,然后我将如何处理(三元组,因为我需要一个多锚文件,但我的应用程序只有一张照片,并在数据库中进行比较,所以在这种情况下只有两个文件),我搜索了很多,但没有人提供代码来处理这个问题,只有实现三元组丢失的代码,而不是整个场景。我不想用对比损失

EN

Stack Overflow用户

发布于 2019-06-23 06:32:59

带有CIFAR10上测试代码的Colab笔记本电脑:https://colab.research.google.com/drive/1VgOTzr_VZNHkXh2z9IiTAcEgg5qr19y0

一般的想法是:

代码语言:javascript
复制
from tensorflow import keras
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K

img_width = 128
img_height = 128
img_colors = 3

margin = 1.0

VECTOR_SIZE = 32

def triplet_loss(y_true, y_pred):
  """ y_true is a dummy value that should be ignored

      Uses the inverse of the cosine similarity as a loss.
  """
  anchor_vec = y_pred[:, :VECTOR_SIZE]
  positive_vec = y_pred[:, VECTOR_SIZE:2*VECTOR_SIZE]
  negative_vec = y_pred[:, 2*VECTOR_SIZE:]
  d1 = keras.losses.cosine_proximity(anchor_vec, positive_vec)
  d2 = keras.losses.cosine_proximity(anchor_vec, negative_vec)
  return K.clip(d2 - d1 + margin, 0, None)


def make_image_model():
  """ Build a convolutional model that generates a vector
  """
  inp = Input(shape=(img_width, img_height, img_colors))
  l1 = Conv2D(8, (2, 2))(inp)
  l1 = MaxPooling2D()(l1)
  l2 = Conv2D(16, (2, 2))(l1)
  l2 = MaxPooling2D()(l2)
  l3 = Conv2D(16, (2, 2))(l2)
  l3 = MaxPooling2D()(l3)
  conv_out = Flatten()(l3)
  out = Dense(VECTOR_SIZE)(conv_out)
  model = Model(inp, out)
  return model

def make_siamese_model(img_model):
  """ Siamese model input are 3 images base, positive, negative
      output is a dummy variable that is ignored for the purposes of loss
      calculation.
  """
  anchor = Input(shape=(img_width, img_height, img_colors))
  positive = Input(shape=(img_width, img_height, img_colors))
  negative = Input(shape=(img_width, img_height, img_colors))
  anchor_vec = img_model(anchor)
  positive_vec = img_model(positive)
  negative_vec = img_model(negative)
  vecs = Concatenate(axis=1)([anchor_vec, positive_vec, negative_vec])
  model = Model([anchor, positive, negative], vecs)
  model.compile('adam', triplet_loss)
  return model

img_model = make_image_model()
train_model = make_siamese_model(img_model)
img_model.summary()
train_model.summary()

###
train_model.fit(X, dummy_y, ...)

img_model.save('image_model.h5')

###
# In order to use the model
vec_base = img_model.predict(base_image)
vec_test = img_model.predict(test_image)

比较vec_basevec_test的余弦相似度,以确定基准和测试是否在可接受的标准内。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56716616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档