Caffe: modify the caffe model using matlab

本文提供了一段用于灵活操作Caffe模型的代码,重点介绍了如何修改模型中常见层(如卷积层)的权重和偏置参数,以及其他层(如批归一化层)的额外参数。代码示例展示了如何将一个模型的参数复制到另一个模型中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Contact Me:
王雪豪 xuehaowang@buaa.edu.cn

For a flexible operation on the Caffe Model, I public the brief code to modify paras.

The common layers, such as Conv layers, have two group paras, i.e. weights and bias.

Other layers, such as batchnorm layer, have three group paras, i.e. weights, bias and value for test phase.

If you use this code, call attention to the number of paras of the layer you modifed.

clc; clear all;

addpath(genpath('/usr/home/caffe-master/matlab/'));

caffe.reset_all();
use_gpu=1;
gpu_id=0;
if use_gpu
  caffe.set_mode_gpu();
  caffe.set_device(gpu_id);
else
  caffe.set_mode_cpu();
end
% net = caffe.Net(model_config, model_file, 'test');

net = caffe.Net('./deploy_before.prototxt', './before.caffemodel', 'test');
net_after = caffe.Net('./deploy_after.prototxt', 'test');

net_after.params('conv_after', 1).set_data(net.params('conv_befor', 1).get_data()); % set weights
net_after.params('conv_after', 2).set_data(net.params('conv_befor', 2).get_data()); % set bias

net_after.save('net_modify.caffemodel');
caffe.reset_all();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值