论文阅读:Siamese Neural Networks for One-shot Image Recognition
发布时间
阅读量:
阅读量
文章:Siamese神经网络在单样本图像识别中的应用研究。
代码:https://github.com/kevinzakka/one-shot-siamese
地址:https://www.cs.cmu.edu/~rsalakhu/papers/oneshot1.pdf
来源:出自第2015年国际机器学习大会(ICML)。
Approach

训练:在训练过程中:基于同类或异类样本对进行配对学习,在分类器训练阶段中旨在最小化同类数据之间的距离同时最大化类别间数据的距离。
网络架构

网络主要用了卷积、最大池化、ReLU等。
loss function

用的是二分类交叉熵,加了L2正则化项,防止过拟合。
optimization

用了反向传播算法。
代码
import torch
import torch.nn as nn
import torch.nn.functional as F
class SiameseNet(nn.Module):
def __init__(self):
super(SiameseNet, self).__init__()
self.conv1 = nn.Conv2d(1, 64, 10)
self.conv2 = nn.Conv2d(64, 128, 7)
self.conv3 = nn.Conv2d(128, 128, 4)
self.conv4 = nn.Conv2d(128, 256, 4)
self.fc1 = nn.Linear(9216, 4096)
self.fc2 = nn.Linear(4096, 1)
# weight init
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal(m.weight, mode='fan_in')
# for m in self.modules():
# if isinstance(m, nn.Conv2d):
# nn.init.normal(m.weight, 0, 1e-2)
# nn.init.normal(m.bias, 0.5, 1e-2)
# elif isinstance(m, nn.Linear):
# nn.init.normal(m.weight, 0, 2e-1)
# nn.init.normal(m.weight, 0, 1e-2)
def sub_forward(self, x):
out = F.relu(F.max_pool2d(self.conv1(x), 2))
out = F.relu(F.max_pool2d(self.conv2(out), 2))
out = F.relu(F.max_pool2d(self.conv3(out), 2))
out = F.relu(self.conv4(out))
out = out.view(out.shape[0], -1)
out = F.sigmoid(self.fc1(out))
return out
def forward(self, x1, x2):
# encode image pairs
h1 = self.sub_forward(x1)
h2 = self.sub_forward(x2)
# compute l1 distance
diff = torch.abs(h1 - h2)
# score the similarity between the 2 encodings
scores = self.fc2(diff)
# return scores (without sigmoid) and use bce_with_logits
# for increased numerical stability
return scores
参考
<>
全部评论 (0)
还没有任何评论哟~
