# 从图片相似度学习图片的表示

siamese.png

• 拿到3张图片A, B, C。其中A，B相似，A，C不相似。
• 学到A, B, C 的表示，使得A,B之间的距离尽量小，而A，C之间的距离尽量大。

``````def get_net(batch_size):
same = mx.sym.Variable('same')
diff = mx.sym.Variable('diff')
anchor = mx.sym.Variable('anchor')
one = mx.sym.Variable('one')
one = mx.sym.Reshape(data = one, shape = (-1, 1))
conv_weight = []
conv_bias = []
for i in range(3):
conv_weight.append(mx.sym.Variable('conv' + str(i) + '_weight'))
conv_bias.append(mx.sym.Variable('conv' + str(i) + '_bias'))
fc_weight = mx.sym.Variable('fc_weight')
fc_bias = mx.sym.Variable('fc_bias')
fa = get_conv(anchor, conv_weight, conv_bias, fc_weight, fc_bias)
fs = get_conv(same, conv_weight, conv_bias, fc_weight, fc_bias)
fd = get_conv(diff, conv_weight, conv_bias, fc_weight, fc_bias)

fs = fa - fs
fd = fa - fd
fs = fs * fs
fd = fd * fd
fs = mx.sym.sum(fs, axis = 1, keepdims = 1)
fd = mx.sym.sum(fd, axis = 1, keepdims = 1)
loss = fd - fs
loss = one - loss
loss = mx.sym.Activation(data = loss, act_type = 'relu')
return mx.sym.MakeLoss(loss)
``````

``````def get_conv(data, conv_weight, conv_bias, fc_weight, fc_bias):
cdata = data
ks = [5, 3, 3]
for i in range(3):
cdata = mx.sym.Convolution(data=cdata, kernel=(ks[i],ks[i]), num_filter=32,
weight = conv_weight[i], bias = conv_bias[i],
name = 'conv' + str(i))
cdata = mx.sym.Pooling(data=cdata, pool_type="avg", kernel=(2,2), stride=(1, 1))
cdata = mx.sym.Activation(data=cdata, act_type="relu")

cdata = mx.sym.Flatten(data = cdata)
cdata = mx.sym.FullyConnected(data = cdata, num_hidden = 1024,
weight = fc_weight, bias = fc_bias, name='fc')
cdata = mx.sym.L2Normalization(data = cdata)
return cdata
``````

Triple loss用的Simultaneous Feature Learning and Hash Coding with Deep Neural Networks里的定义：

Triple Loss

cifar_triple.png