在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:
- C1:特征映射:15,内核大小=(7,7)
- S2:特征映射:15,视野=(2,2)
- C3:特征映射:45,内核大小=(6,6)
- S4:特征映射:45,视场=(4,3)
- C5:特征映射:250,内核大小=(5,5)
- 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:
- C1: feature maps: 15, kernel size = (7, 7)
- S2: feature maps: 15, field-of-view = (2, 2)
- C3: feature maps: 45, kernel size = (6, 6)
- S4: feature maps: 45, field-of-view = (4, 3)
- C5: feature maps: 250, kernel size = (5, 5)
- 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
最满意答案
set default to 后加存放位置和文件名 例如放d盘 study 文件 set default to d:\study
其他回答
其实不用争,一种体例如不美观10几年都可正常使用,证实无问题
原本还能用fullpath 哎 脑子都僵化了 哈哈
所有的相对路径,都放在主文件夹下面,直接挪用。强制性的,没筹议
sys(2003)返回在默认驱动器上的当前目录名称
对于项目最后编译为exe执行文件的,用sys(5)+sys(2003)没侍趵硎题但如不美观只是编译为.app文件,使用vfp执行的,使用膳缦沔体例获适当前运行目录的,会有问题,应用sys(16)
相关问答
更多-
相对路径与根路径有什么区别[2024-01-03]
相对路径:这个概念应该有一个参照目录,相对路径是以这个参照目录为基准定义的。 根路径:对于相对路径而言,就是那个参照目录。 -
关于python相对路径,绝对路径的问题[2022-05-07]
我这儿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 ... -
相对路径与根路径有什么区别[2022-03-01]
比如你的网页中用的路径全是绝对路径,也就是根路径的话,比如是这个“C:\Program Files\Google\index.html”那么上传到服务器上路径也是这个,可是你的文件放到服务器上并不是在这个路径下,可能是在F盘,E盘或者其他的盘。那么你的这些路径就无法表达文件所在的具体位置了,找不到相应文件是唯一的结果。这就是所谓的“就要重新编排所有的超链接”。 为了解决这个问题就要用到相对路径。 相对路径只涉及上级和下级,而不管相对于根目录的情况。 -
有个疑问,phpmyadmin有root权限,怎么弄绝对路径,求助[2022-01-11]
什么是root权限恐怕你没搞懂,ROOT就是 系统的超级用户一般命名为root。root是系统中唯一的超级用户,具有系统中所有的权限,如启动或停止一个进程,删除或增加用户,增加或者禁用硬件等等。 如果你新建一个root级别用户,那么这个root级别用户... -
如何判断相对路径是否正确或是否存在?[2024-01-28]
对这个文件夹进行任何操作,如果找不到这个文件夹。java会报异常,用try{}catch(){}捕捉这个异常,catch里面就是你要的。 -
PHP的include的相对路径和绝对路径问题一般都怎么解决的[2022-02-13]
建议定义入口文件,文件中所有目录的路径定义常量,写入绝对路径 如:$_SERVER['DOCUMENT_ROOT']."/文件夹路径/" 站点内所有路径都使用绝对路径 -
vb.net 怎样使用相对路径[2022-07-24]
// 获取程序的基目录。 System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径。 System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName // 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。 System.Environment.CurrentDirectory // 获取应用程序的当前工作目录。 System.IO.Directory.GetCurrent ... -
VFP的相对路径怎么弄[2023-03-04]
set default to 后加存放位置和文件名 例如放d盘 study 文件 set default to d:\study -
使用“./”的相对路径(Relative path using “./”)[2023-07-28]
./表示相对于当前目录。 你是对的,编写路径的两种方式完全相同,两者都可以正常工作。 在脚本生成路径的情况下,使用./可能会有所帮助。 ./ 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 ... -
使用相对路径(Using relative path)[2022-05-21]
根据您的问题,我猜您是从脚本运行的页面的同一域中获取脚本,因此您可以使用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_' ...