【用Python学习Caffe】6. 权重预设、预训练及微调
来源:互联网 发布:js 设置radio checked 编辑:程序博客网 时间:2024/06/09 20:48
6. 权重预设、预训练及微调
通过上一节,我们将得到solver文件,得到该文件后,进行网络的训练及测试将变得非常简单。
在通过solver = caffe.SGDSolver(solver_proto)
初始化解决器后,训练一般有两种方式,一是通过solver.solve()
直接进行训练,二是通过solver.step(1)
进行单步训练。
6.1 solver直接训练及单步训练
if is_step==False: # 直接完成训练 solver.solve() else: # 迭代次数 max_iter = 10000 # 每隔100次收集一次数据 display = 100 # 每次测试进行100次解算,10000/100 test_iter = 100 # 每500次训练进行一次测试(100次解算),60000/64 test_interval = 500 # 初始化 train_loss = np.zeros(int(math.ceil(max_iter * 1.0 / display))) test_loss = np.zeros(int(math.ceil(max_iter * 1.0 / test_interval))) test_acc = np.zeros(int(math.ceil(max_iter * 1.0 / test_interval))) # iteration 0,不计入 solver.step(1) # 辅助变量 _train_loss = 0 _test_loss = 0 _accuracy = 0 # 分步训练 for it in range(max_iter): # 进行一次解算 solver.step(1) # 每迭代一次,训练batch_size张图片 _train_loss += solver.net.blobs['loss'].data # 最后一层的损失值 if it % display == 0: # 计算平均train loss train_loss[int(it / display)] = _train_loss / display _train_loss = 0 # 测试 if it % test_interval == 0: for test_it in range(test_iter): # 进行一次测试 solver.test_nets[0].forward() # 计算test loss _test_loss += solver.test_nets[0].blobs['loss'].data # 计算test accuracy _accuracy += solver.test_nets[0].blobs['accuracy'].data # 计算平均test loss test_loss[it / test_interval] = _test_loss / test_iter # 计算平均test accuracy test_acc[it / test_interval] = _accuracy / test_iter _test_loss = 0 _accuracy = 0
6.2 权重预设
当然很多情况下,我们可能需要从已经训练好的文件中重新开始训练。这种情况下,可以通过solver.net.copy_from(caffemodel)
来导入已训练模型,从已知的权重连接处开始新的训练。
6.3 截取已知网络作为初始训练权重
而有时,我们只需要截取某些其他网络结构的部分参数作为权重预值,比如说SSD会截取VGG网络前段部分的参数作为SSD的特征提取层的初始参数,来减少总体的训练时间,这时我们可以通过如下方法来解决
def crop_network(prune_proto, caffemodel, prune_caffemodel): # 截取已知网络的部分层 # caffemodel网络权重值并不要求其结构与proto相对应 # 网络只会取train_proto中定义的结构中权重作为网络的初始权重值 # 因此,当我们需要截取某些已训练网络的特定层作为新网络的某些层的权重初始值,只需要在其train_proto定义同名的层 # 之后caffe将在caffemodel中找到与train_proto定义的同名结构,并将其权重作为应用权重初始值。 # prune_deploy: 选择保留的网络结构层:prototxt # caffemodel: 已知网络的权重连接 # prune_caffemodel:截断网络的权重连接文件 net = caffe.Net(prune_proto, caffemodel, caffe.TEST) net.save(prune_caffemodel)
6.4 具体代码下载
GitHub仓库Caffe-Python-Tutorial中的train_val.py
项目地址:https://github.com/tostq/Caffe-Python-Tutorial
阅读全文
0 0
- 【用Python学习Caffe】6. 权重预设、预训练及微调
- 【用Python学习Caffe】6. 权重预设、预训练及微调
- 用python编写的caffe网络工具:包括网络训练、微调及中断后继续训练功能
- caffe的python接口学习(10):微调 & 直接训练
- caffe训练与微调时的区别
- Caffe 实例笔记 1 CaffeNet从训练到分类及可视化参数特征 微调
- 【用Python学习Caffe】8. 网络结构的权重共享量化
- Caffe微调
- tensorflow预训练简单模型及权重文件复用初始化复杂模型
- Caffe学习笔记(七)—— solver参数说明及利用自己的数据集对权值微调
- Caffe学习笔记(七)—— solver参数说明及利用自己的数据集对权值微调
- Caffe学习笔记—— solver参数说明及利用自己的数据集对权值微调
- 【用Python学习Caffe】3. 图像训练测试数据集LMDB的生成
- caffe调用之前的权重和接着断点继续训练
- caffe的python接口学习(3):训练模型(training)
- caffe的python接口学习(3):训练模型(training)
- Caffe-python interface 学习|网络训练、部署、测试
- Caffe-python interface 学习|网络训练、部署、测试
- 如何在linux中实现软件的开机自动启动
- Qt中使用new之后没有delete
- windows 7/10远程ubuntu 16.04LTS多用户访问
- 链表求和问题
- Golang 上传文件
- 【用Python学习Caffe】6. 权重预设、预训练及微调
- 测试菜鸟关于selenium的学习
- go 环境变量说明
- 使CPU运行的更快--Cache
- 交换排序之快速排序
- 优质内容很有必要
- css3 和 h5 几个实用的小知识
- 面向对象设计原则
- HashMap实现原理及源码分析