Tutorial: Implementation of Siamese Network on Caffe, Torch, Tensorflow
Tutorial: Implementation of Siamese Network with Caffe, Theano, PyTorch, Tensorflow
Updated on 2018-07-23 14:33:23
1. caffe version:
If you want to try this network, just do as the offical document said, like the following codes:
---
title: Siamese Network Tutorial
description: Train and test a siamese network on MNIST data.
category: example
include_in_docs: true
layout: default
priority:
--- # Siamese Network Training with Caffe
This example shows how you can use weight sharing and a contrastive loss
function to learn a model using a siamese network in Caffe. We will assume that you have caffe successfully compiled. If not, please refer
to the [Installation page](../../installation.html). This example builds on the
[MNIST tutorial](mnist.html) so it would be a good idea to read that before
continuing. *The guide specifies all paths and assumes all commands are executed from the
root caffe directory* ## Prepare Datasets You will first need to download and convert the data from the MNIST
website. To do this, simply run the following commands: ./data/mnist/get_mnist.sh
./examples/siamese/create_mnist_siamese.sh After running the script there should be two datasets,
`./examples/siamese/mnist_siamese_train_leveldb`, and
`./examples/siamese/mnist_siamese_test_leveldb`. ## The Model
First, we will define the model that we want to train using the siamese network.
We will use the convolutional net defined in
`./examples/siamese/mnist_siamese.prototxt`. This model is almost
exactly the same as the [LeNet model](mnist.html), the only difference is that
we have replaced the top layers that produced probabilities over the digit
classes with a linear "feature" layer that produces a dimensional vector. layer {
name: "feat"
type: "InnerProduct"
bottom: "ip2"
top: "feat"
param {
name: "feat_w"
lr_mult:
}
param {
name: "feat_b"
lr_mult:
}
inner_product_param {
num_output:
}
} ## Define the Siamese Network In this section we will define the siamese network used for training. The
resulting network is defined in
`./examples/siamese/mnist_siamese_train_test.prototxt`. ### Reading in the Pair Data We start with a data layer that reads from the LevelDB database we created
earlier. Each entry in this database contains the image data for a pair of
images (`pair_data`) and a binary label saying if they belong to the same class
or different classes (`sim`). layer {
name: "pair_data"
type: "Data"
top: "pair_data"
top: "sim"
include { phase: TRAIN }
transform_param {
scale: 0.00390625
}
data_param {
source: "examples/siamese/mnist_siamese_train_leveldb"
batch_size:
}
} In order to pack a pair of images into the same blob in the database we pack one
image per channel. We want to be able to work with these two images separately,
so we add a slice layer after the data layer. This takes the `pair_data` and
slices it along the channel dimension so that we have a single image in `data`
and its paired image in `data_p.` layer {
name: "slice_pair"
type: "Slice"
bottom: "pair_data"
top: "data"
top: "data_p"
slice_param {
slice_dim:
slice_point:
}
} ### Building the First Side of the Siamese Net Now we can specify the first side of the siamese net. This side operates on
`data` and produces `feat`. Starting from the net in
`./examples/siamese/mnist_siamese.prototxt` we add default weight fillers. Then
we name the parameters of the convolutional and inner product layers. Naming the
parameters allows Caffe to share the parameters between layers on both sides of
the siamese net. In the definition this looks like: ...
param { name: "conv1_w" ... }
param { name: "conv1_b" ... }
...
param { name: "conv2_w" ... }
param { name: "conv2_b" ... }
...
param { name: "ip1_w" ... }
param { name: "ip1_b" ... }
...
param { name: "ip2_w" ... }
param { name: "ip2_b" ... }
... ### Building the Second Side of the Siamese Net Now we need to create the second path that operates on `data_p` and produces
`feat_p`. This path is exactly the same as the first. So we can just copy and
paste it. Then we change the name of each layer, input, and output by appending
`_p` to differentiate the "paired" layers from the originals. ### Adding the Contrastive Loss Function To train the network we will optimize a contrastive loss function proposed in:
Raia Hadsell, Sumit Chopra, and Yann LeCun "Dimensionality Reduction by Learning
an Invariant Mapping". This loss function encourages matching pairs to be close
together in feature space while pushing non-matching pairs apart. This cost
function is implemented with the `CONTRASTIVE_LOSS` layer: layer {
name: "loss"
type: "ContrastiveLoss"
contrastive_loss_param {
margin: 1.0
}
bottom: "feat"
bottom: "feat_p"
bottom: "sim"
top: "loss"
} ## Define the Solver Nothing special needs to be done to the solver besides pointing it at the
correct model file. The solver is defined in
`./examples/siamese/mnist_siamese_solver.prototxt`. ## Training and Testing the Model Training the model is simple after you have written the network definition
protobuf and solver protobuf files. Simply run
`./examples/siamese/train_mnist_siamese.sh`: ./examples/siamese/train_mnist_siamese.sh # Plotting the results First, we can draw the model and siamese networks by running the following
commands that draw the DAGs defined in the .prototxt files: ./python/draw_net.py \
./examples/siamese/mnist_siamese.prototxt \
./examples/siamese/mnist_siamese.png ./python/draw_net.py \
./examples/siamese/mnist_siamese_train_test.prototxt \
./examples/siamese/mnist_siamese_train_test.png Second, we can load the learned model and plot the features using the iPython
notebook: ipython notebook ./examples/siamese/mnist_siamese.ipynb
If you want to shown the neural network in a image. first, you should install the following softwares:
1. sudo apt-get install graphviz
2. sudo pip install pydot2
then, you can draw the following graph using tool provided by python files.

If you want to know how to implement this on your own data. You should:
1. Preparing your data:
==>> positive and negative image pairs and corresponding label (1 and -1).
2. Convert the files into lmdb files
3. then just do as above mentioned.
==>> But I am still feel confused about how to deal with this whole process.
Will fill with this part later.
2. Siamese Lasagne Theano version :
# Run on GPU: THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python mnist_siamese_graph.py
from __future__ import print_function import sys
import os
import time
import numpy as np
import theano
import theano.tensor as T
import lasagne
import utils
from progressbar import AnimatedMarker, Bar, BouncingBar, Counter, ETA, \
FileTransferSpeed, FormatLabel, Percentage, \
ProgressBar, ReverseBar, RotatingMarker, \
SimpleProgress, Timer
import matplotlib.pyplot as plt
from matplotlib import gridspec
import cPickle as pickle
import time
from sklearn import metrics
from scipy import interpolate
from lasagne.regularization import regularize_layer_params_weighted, l2, l1
from lasagne.regularization import regularize_layer_params NUM_EPOCHS = 40
BATCH_SIZE = 100
LEARNING_RATE = 0.001
MOMENTUM = 0.9 # def build_cnn(input_var=None):
# net = lasagne.layers.InputLayer(shape=(None, 1, 64, 64),
# input_var=input_var)
# cnn1 = lasagne.layers.Conv2DLayer(
# net, num_filters=96, filter_size=(7, 7),
# nonlinearity=lasagne.nonlinearities.rectify,
# W=lasagne.init.GlorotNormal())
# pool1 = lasagne.layers.MaxPool2DLayer(cnn1, pool_size=(2, 2))
# cnn2 = lasagne.layers.Conv2DLayer(
# pool1, num_filters=64, filter_size=(6, 6),
# nonlinearity=lasagne.nonlinearities.rectify,
# W=lasagne.init.GlorotNormal())
# fc1 = lasagne.layers.DenseLayer(cnn2, num_units=128)
# # network = lasagne.layers.FlattenLayer(fc1)
# return fc1 def build_cnn(input_var=None):
net = lasagne.layers.InputLayer(shape=(None, 1, 64, 64),
input_var=input_var)
cnn1 = lasagne.layers.Conv2DLayer(
net, num_filters=96, filter_size=(7, 7),
nonlinearity=lasagne.nonlinearities.rectify,
stride = (3,3),
W=lasagne.init.GlorotNormal())
pool1 = lasagne.layers.MaxPool2DLayer(cnn1, pool_size=(2, 2))
cnn2 = lasagne.layers.Conv2DLayer(
pool1, num_filters=192, filter_size=(5, 5),
nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.GlorotNormal())
pool2 = lasagne.layers.MaxPool2DLayer(cnn2, pool_size=(2, 2))
cnn3 = lasagne.layers.Conv2DLayer(
pool2, num_filters=256, filter_size=(3, 3),
nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.GlorotNormal())
# fc1 = lasagne.layers.DenseLayer(cnn2, num_units=128)
network = lasagne.layers.FlattenLayer(cnn3)
return network def init_data(train,test):
dtrain = utils.load_brown_dataset("/home/vassilis/Datasets/"+train+"/")
dtest = utils.load_brown_dataset("/home/vassilis/Datasets/"+test+"/") dtrain['patches'] = dtrain['patches'].astype('float32')
dtest['patches'] = dtest['patches'].astype('float32') dtrain['patches'] /= 255
dtest['patches'] /= 255 mu = dtrain['patches'].mean()
dtrain['patches'] = dtrain['patches'] - mu
dtest['patches'] = dtest['patches'] - mu
return dtrain,dtest def eval_test(net,d):
bs = 100
pb = np.array_split(d['patches'],bs)
descrs = []
for i,minib in enumerate(pb):
dd = lasagne.layers.get_output(net,minib).eval()
descrs.append(dd) descrs = np.vstack(descrs)
dists = np.zeros(100000,)
lbls = np.zeros(100000,) for i in range(100000):
idx1 = d['testgt'][i][0]
idx2 = d['testgt'][i][1]
lbl = d['testgt'][i][2]
dists[i] = np.linalg.norm(descrs[idx1]-descrs[idx2])
lbls[i] = lbl
#print(dists[i],lbls[i])
fpr, tpr, thresholds = metrics.roc_curve(lbls, -dists, pos_label=1)
f = interpolate.interp1d(tpr, fpr)
fpr95 = f(0.95)
print('fpr95-> '+str(fpr95)) def main(num_epochs=NUM_EPOCHS):
widgets = ['Mini-batch training: ', Percentage(), ' ', Bar(),
' ', ETA(), ' ']
print("> Loading data...")
dtrain,dtest = init_data('liberty','notredame')
net = build_cnn() dtr = utils.gen_pairs(dtrain,1200000)
ntr = dtr.shape[0] X = T.tensor4()
y = T.ivector()
a = lasagne.layers.get_output(net,X) fx1 = a[1::2, :]
fx2 = a[::2, :]
d = T.sum(( fx1- fx2)**2, -1) l2_penalty = regularize_layer_params(net, l2) * 1e-3 loss = T.mean(y * d +
(1 - y) * T.maximum(0, 1 - d))+l2_penalty all_params = lasagne.layers.get_all_params(net)
updates = lasagne.updates.nesterov_momentum(
loss, all_params, LEARNING_RATE, MOMENTUM) trainf = theano.function([X, y], loss,updates=updates) num_batches = ntr // BATCH_SIZE
print(num_batches)
print("> Done loading data...")
print("> Started learning with "+str(num_batches)+" batches") shuf = np.random.permutation(ntr) X_tr = np.zeros((BATCH_SIZE*2,1,64,64)).astype('float32')
y_tr = np.zeros(BATCH_SIZE).astype('int32') for epoch in range(NUM_EPOCHS):
batch_train_losses = []
pbar = ProgressBar(widgets=widgets, maxval=num_batches).start()
for k in range(num_batches):
sh = shuf[k*BATCH_SIZE:k*BATCH_SIZE+BATCH_SIZE]
pbar.update(k)
# fill batch here
for s in range(0,BATCH_SIZE*2,2):
# idx1 = dtrain['traingt'][sh[s/2],0]
# idx2 = dtrain['traingt'][sh[s/2],1]
# lbl = dtrain['traingt'][sh[s/2],2] idx1 = dtr[sh[s/2]][0]
idx2 = dtr[sh[s/2]][1]
lbl = dtr[sh[s/2]][2] X_tr[s] = dtrain['patches'][idx1]
X_tr[s+1] = dtrain['patches'][idx2]
y_tr[s/2] = lbl batch_train_loss = trainf(X_tr,y_tr)
batch_train_losses.append(batch_train_loss)
avg_train_loss = np.mean(batch_train_losses)
pbar.finish()
print("> Epoch " + str(epoch) + ", loss: "+str(avg_train_loss)) eval_test(net,dtest) with open('net.pickle', 'wb') as f:
pickle.dump(net, f, -1) # netlayers = lasagne.layers.get_all_layers(net)
# print(netlayers)
# layer = netlayers[1]
# print(layer)
# print(layer.num_filters)
# W = layer.W.get_value()
# b = layer.b.get_value()
# f = [w + bb for w, bb in zip(W, b)]
# gs = gridspec.GridSpec(8, 12)
# for i in range(layer.num_filters):
# g = gs[i]
# ax = plt.subplot(g)
# ax.grid()
# ax.set_xticks([])
# ax.set_yticks([])
# ax.imshow(f[i][0])
# plt.show() if __name__ == '__main__':
main(sys.argv[1])
3. Tensorflow version :
Github link: https://github.com/ywpkwon/siamese_tf_mnist
4. PyTorch Version:

5.
Tutorial: Implementation of Siamese Network on Caffe, Torch, Tensorflow的更多相关文章
- Siamese Network理解
提起siamese network一般都会引用这两篇文章: <Learning a similarity metric discriminatively, with application to ...
- Siamese network 孪生神经网络
Siamese network 孪生神经网络 https://zhuanlan.zhihu.com/p/35040994 https://blog.csdn.net/shenziheng1/artic ...
- 深度学习框架caffe/CNTK/Tensorflow/Theano/Torch的对比
在单GPU下,所有这些工具集都调用cuDNN,因此只要外层的计算或者内存分配差异不大其性能表现都差不多. Caffe: 1)主流工业级深度学习工具,具有出色的卷积神经网络实现.在计算机视觉领域Caff ...
- Siamese Network简介
Siamese Network简介 Siamese Network 是一种神经网络的框架,而不是具体的某种网络,就像seq2seq一样,具体实现上可以使用RNN也可以使用CNN. 简单的说,Siame ...
- 跟我学算法-人脸识别(Siamese network) 推导
Siamese network 训练神经网络存在两种形式: 第一种:通过Siamese network 和 三元组损失函数 来训练图片之间的间隔 第二种: 通过Siamese network 和 si ...
- 一图看懂深度学习框架对比----Caffe Torch Theano TensorFlow
Caffe Torch Theano TensorFlow Language C++, Python Lua Python Python Pretrained Yes ++ Yes ++ Yes ...
- [转] Siamese network 孪生神经网络--一个简单神奇的结构
转自: 作者:fighting41love 链接:https://www.jianshu.com/p/92d7f6eaacf5 1.名字的由来 Siamese和Chinese有点像.Siam是古时候泰 ...
- Siamese network总结
前言: 本文介绍了Siamese (连体)网络的主要特点.训练和测试Siamese网络的步骤.Siamese网络的应用场合.Siamese网络的优缺点.为什么Siamese被称为One-shot分类 ...
- Caffe、TensorFlow、MXnet三个开源库对比
库名称 开发语言 支持接口 安装难度(ubuntu) 文档风格 示例 支持模型 上手难易 Caffe c++/cuda c++/python/matlab *** * *** CNN ** MXNet ...
随机推荐
- OC 反射-->动态创建类
系统方法 NSLog(@"%s", __func__); //打印出类的方法名称,如: //打印结果:2018-02-22 10:52:15.394575+0800 DemoRun ...
- C++笔试题2(基础题)
温馨提醒:此文续<C++笔试题(基础题)> (112)请写出下列程序的输出内容 代码如下: #include <iostream> using namespace std; c ...
- gene Ontology (基因本体论)
gene ontology为了查找某个研究领域的相关信息,生物学家往往要花费大量的时间,更糟糕的是,不同的生物学数据库可能会使用不同的术语,好比是一些方言一样,这让信息查找更加麻烦,尤其是使得机器查找 ...
- Sqoop与HDFS、Hive、Hbase等系统的数据同步操作
Sqoop与HDFS结合 下面我们结合 HDFS,介绍 Sqoop 从关系型数据库的导入和导出. Sqoop import 它的功能是将数据从关系型数据库导入 HDFS 中,其流程图如下所示. 我们来 ...
- flask模板应用-javaScript和CSS中jinja2
当程序逐渐变大时,很多时候我们需要在javaScript和CSS代码中使用jinja2提供的变量值,甚至是控制语句.比如,通过传入模板的theme_color变量来为页面设置主题色彩,或是根据用户是否 ...
- LINUX安装REDIS集群
linux安装单机版redis已经在另一篇文章说过了,下边来搞集群,环境是新浪云服务器: redis3.0以后开始支持集群. 前言:redis用什么做集群? 用一个叫redis-trib.rb的rub ...
- Codeforce 296A - Yaroslav and Permutations
Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring a ...
- 动手动脑-Java的方法重载
例: Using overloaded methods public class MethodOverload { public static void main(String[] args) { ...
- 从源码层面聊聊面试问烂了的 Spring AOP与SpringMVC
Spring AOP ,SpringMVC ,这两个应该是国内面试必问题,网上有很多答案,其实背背就可以.但今天笔者带大家一起深入浅出源码,看看他的原理.以期让印象更加深刻,面试的时候游刃有余. Sp ...
- POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫
POJ题目网址:http://poj.org/problem?id=1308 HDU题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1272 并查集的运用 ...