Caffe提示shape mismatch 的原因及解决办法

来源:互联网 发布:网络购物的发展趋势 编辑:程序博客网 时间:2024/06/18 10:28

Caffe Shape Mismatch 原因?

在使用Caffe进行finetune时,有时候会出现 shape mismatch的错误,其实正如字面意思,就是维度不匹配。

Caffe读取caffemodel时,是按照参数名(name)进行匹配的,设你的模型变量集为A, 所要读取的caffemodel中的变量集为B,AB=C , 则训练时B中的C会从A中读取,即与caffemodel名称相同的变量会被读取。

所以当你的模型结构与所使用的caffemodel不同时,也能使用caffemodel中的参数初始化一部分。

当A和B中存在相同名称但是不同维度的变量时,就会出现shape mismatch错误。

怎么解决?

当然,最简单的就是你修改一下A中相应的变量的名称;
稍微麻烦一点的是删除B中相应的变量:
此处借鉴别人的方法,
举例:

import caffeif __name__=="__main__":    root = '/your working directory/'    caffe.set_mode_cpu    net = caffe.Net(root+'The_net_removed_target_variable(layer)_deploy.prototxt', root+'base_init.caffemodel', caffe.TEST)    #see the variable list in The_net_removed_target_variable(layer)_deploy.prototxt    for para in net.params.keys():         print(para)    net.save('removed_target_layer_init.caffemodel')

只需要在The_net_removed_target_variable(layer)_deploy.prototxt中删除你需要删除的变量(层)即可。

阅读全文
0 0