首页 \ 问答 \ 在Keras中实现一个Siamese NN(Implementing a Siamese NN in Keras)

在Keras中实现一个Siamese NN(Implementing a Siamese NN in Keras)

所以我试图实现关于Siamese神经网络的这篇文章:Sumit Chopra,Raia Hadsell和Yann LeCun(2005) 对歧视性学习相似性度量,应用于面对验证 。 不过,我使用CIFAR10数据集 ,但使用了10个类。

其中一条腿的规格为方便起见而被复制。 符号:C_x是一个卷积层,S_x是一个子采样层,F_x是一个完全连接的层; 与共享索引x:

  1. C1:特征映射:15,内核大小=(7,7)
  2. S2:特征映射:15,视野=(2,2)
  3. C3:特征映射:45,内核大小=(6,6)
  4. S4:特征映射:45,视场=(4,3)
  5. C5:特征映射:250,内核大小=(5,5)
  6. F6(完全连接层):不。 的单位= 50

我试过的

model = Sequential()

#C1
model.add(Convolution2D(15, 7, 7,
activation='relu',
border_mode='same',
input_shape=input_img_shape))
print("C1 shape: ", model.output_shape)

#S2
model.add(MaxPooling2D((2,2), border_mode='same'))
print("S2 shape: ", model.output_shape)
#...

#C5
model.add(Convolution2D(250, 5, 5, 
activation='relu', 
border_mode='same'))
print("C5 shape: ", model.output_shape)

#F6
model.add(Dense(50))

这会抛出一个很长的错误信息,我认为这是一个重塑错误。 错误的一个片段:

Exception: Input 0 is incompatible with layer dense_13: expected
ndim=2, found ndim=4

我知道这个问题在最后的Dense层中是孤立的,因为如果我将它注释掉,代码会顺利进行。 但我不确定我应该如何设计/指定我的最终完全连接图层,以便它与先前的卷积图层兼容?

我看过的一些地方

是一个相关的问题,虽然实现有些不同(在撰写本文时,似乎并没有keras中的'Siamese'核心层)。 我知道在Theano中也有实现,如果我不能在keras中实现 ,我会记住它。

谢谢!


So I'm trying to implement this paper about a Siamese neural network: Learning a similarity metric discriminatively, with application to face verification, by Sumit Chopra, Raia Hadsell and Yann LeCun (2005). I'm using the CIFAR10 dataset instead, though, with 10 classes.

The specifications of one of the legs is reproduced for convenience. Notation: C_x is a convolution layer, S_x is a subsampling layer and F_x is a fully connected layer; with a shared index x:

  1. C1: feature maps: 15, kernel size = (7, 7)
  2. S2: feature maps: 15, field-of-view = (2, 2)
  3. C3: feature maps: 45, kernel size = (6, 6)
  4. S4: feature maps: 45, field-of-view = (4, 3)
  5. C5: feature maps: 250, kernel size = (5, 5)
  6. F6 (fully connected layer): no. of units = 50

What I've Tried

model = Sequential()

#C1
model.add(Convolution2D(15, 7, 7,
activation='relu',
border_mode='same',
input_shape=input_img_shape))
print("C1 shape: ", model.output_shape)

#S2
model.add(MaxPooling2D((2,2), border_mode='same'))
print("S2 shape: ", model.output_shape)
#...

#C5
model.add(Convolution2D(250, 5, 5, 
activation='relu', 
border_mode='same'))
print("C5 shape: ", model.output_shape)

#F6
model.add(Dense(50))

This throws a long error message, which I believe is a reshape error. A snippet of the error:

Exception: Input 0 is incompatible with layer dense_13: expected
ndim=2, found ndim=4

I know that the problem is isolated in that final Dense layer, because the code proceeds smoothly if I comment it out. But I'm not sure exactly how I should then shape/specify my final fully connected layer so that it's compatible with the prior convolution layer?

Some Places I've Looked

This is a related problem, though the implementation is slightly different (it seems that there isn't a 'Siamese' core layer in keras at the time of this writing). I'm aware that there are also implementations in Theano, which I'll bear in mind if I'm just not able to do it in keras.

Thanks!


原文:https://stackoverflow.com/questions/38584268
更新时间:2022-10-05 08:10

最满意答案

set default to 后加存放位置和文件名 例如放d盘 study 文件 set default to d:\study

其他回答

其实不用争,一种体例如不美观10几年都可正常使用,证实无问题
原本还能用fullpath 哎 脑子都僵化了 哈哈
所有的相对路径,都放在主文件夹下面,直接挪用。强制性的,没筹议
sys(2003)返回在默认驱动器上的当前目录名称
对于项目最后编译为exe执行文件的,用sys(5)+sys(2003)没侍趵硎题但如不美观只是编译为.app文件,使用vfp执行的,使用膳缦沔体例获适当前运行目录的,会有问题,应用sys(16)

相关问答

更多
  • 相对路径:这个概念应该有一个参照目录,相对路径是以这个参照目录为基准定义的。 根路径:对于相对路径而言,就是那个参照目录。
  • 我这儿python 2.7不存在这个问题 在命令行下用python2.6运行 print "\n".join(__import__("sys").modules) 告诉我输出里面有没有string模块,有的话就说明python的模块导入机制将你的string.py忽略了。 from语法大概是这样: from MODULE import NAME [ as NEW_NAME] [, ...] 详细定义见这里: docs.python.org/release/2.6.7/reference/simple_stm ...
  • 比如你的网页中用的路径全是绝对路径,也就是根路径的话,比如是这个“C:\Program Files\Google\index.html”那么上传到服务器上路径也是这个,可是你的文件放到服务器上并不是在这个路径下,可能是在F盘,E盘或者其他的盘。那么你的这些路径就无法表达文件所在的具体位置了,找不到相应文件是唯一的结果。这就是所谓的“就要重新编排所有的超链接”。 为了解决这个问题就要用到相对路径。 相对路径只涉及上级和下级,而不管相对于根目录的情况。
  • 什么是root权限恐怕你没搞懂,ROOT就是 系统的超级用户一般命名为root。root是系统中唯一的超级用户,具有系统中所有的权限,如启动或停止一个进程,删除或增加用户,增加或者禁用硬件等等。 如果你新建一个root级别用户,那么这个root级别用户...
  • 对这个文件夹进行任何操作,如果找不到这个文件夹。java会报异常,用try{}catch(){}捕捉这个异常,catch里面就是你要的。
  • 建议定义入口文件,文件中所有目录的路径定义常量,写入绝对路径 如:$_SERVER['DOCUMENT_ROOT']."/文件夹路径/" 站点内所有路径都使用绝对路径
  • // 获取程序的基目录。 System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径。 System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName // 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。 System.Environment.CurrentDirectory // 获取应用程序的当前工作目录。 System.IO.Directory.GetCurrent ...
  • set default to 后加存放位置和文件名 例如放d盘 study 文件 set default to d:\study
  • ./表示相对于当前目录。 你是对的,编写路径的两种方式完全相同,两者都可以正常工作。 在脚本生成路径的情况下,使用./可能会有所帮助。 ./ means relative to the current directory. You are correct, the two ways of writing the path are exactly the same and both will work fine. Using ./ may help in the case that a script gene ...
  • 根据您的问题,我猜您是从脚本运行的页面的同一域中获取脚本,因此您可以使用location mdn对象的host和pathname属性,如下所示: var url = window.location.host + '/html5/i18n/resources-locale_' + localize.language + '.js'; 要么 var url = window.location.host + window.location.pathname + '/i18n/resources-locale_' ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)