如何使用pycaffe生成solver.prototxt文件并进行训练

发布时间:2021-12-04 15:55:09 作者:柒染
来源:亿速云 阅读:104

这篇文章将为大家详细讲解有关如何使用pycaffe生成solver.prototxt文件并进行训练,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

下面主要记录如何生成sovler文件,solver文件是训练的时候,需要用到的prototxt文件,它指明了train.prototxt和test.prototxt或train_test.prototxt。solver就是用来是loss最小化的优化方法。

一、solver.prototxt参数说明

    依然是以cifar10_quick_solver.prototxt为例,内容如下:

# reduce the learning rate after 8 epochs (4000 iters) by a factor of 10# The train/test net protocol buffer definitionnet: "examples/cifar10/cifar10_quick_train_test.prototxt"# test_iter specifies how many forward passes the test should carry out.# In the case of MNIST, we have test batch size 100 and 100 test iterations,# covering the full 10,000 testing images.test_iter: 100# Carry out testing every 500 training iterations.test_interval: 500# The base learning rate, momentum and the weight decay of the network.base_lr: 0.001momentum: 0.9weight_decay: 0.004# The learning rate policylr_policy: "fixed"# Display every 100 iterationsdisplay: 100# The maximum number of iterationsmax_iter: 4000# snapshot intermediate resultssnapshot: 4000snapshot_format: HDF5snapshot_prefix: "examples/cifar10/cifar10_quick"# solver mode: CPU or GPUsolver_mode: GPU

    这些参数,都是有根据进行设置的,从上到下依次进行说明:

    lr_prolicy参数说明:

二、使用python生成solver.prototxt文件

    以分析的cifar10_quick_solver.prototxt文件为例,使用python程序,生成这个文件。

1.代码如下:

# -*- coding: UTF-8 -*-import caffe                                                     #导入caffe包def write_sovler():my_project_root = "/home/Jack-Cui/caffe-master/my-caffe-project/"        #my-caffe-project目录sovler_string = caffe.proto.caffe_pb2.SolverParameter()                    #sovler存储solver_file = my_project_root + 'solver.prototxt'                        #sovler文件保存位置sovler_string.train_net = my_project_root + 'train.prototxt'            #train.prototxt位置指定sovler_string.test_net.append(my_project_root + 'test.prototxt')         #test.prototxt位置指定sovler_string.test_iter.append(100)                                        #测试迭代次数sovler_string.test_interval = 500                                        #每训练迭代test_interval次进行一次测试sovler_string.base_lr = 0.001                                            #基础学习率   sovler_string.momentum = 0.9                                            #动量sovler_string.weight_decay = 0.004                                        #权重衰减sovler_string.lr_policy = 'fixed'                                        #学习策略           sovler_string.display = 100                                                #每迭代display次显示结果sovler_string.max_iter = 4000                                            #最大迭代数sovler_string.snapshot = 4000                                             #保存临时模型的迭代数sovler_string.snapshot_format = 0                                        #临时模型的保存格式,0代表HDF5,1代表BINARYPROTOsovler_string.snapshot_prefix = 'examples/cifar10/cifar10_quick'        #模型前缀sovler_string.solver_mode = caffe.proto.caffe_pb2.SolverParameter.GPU    #优化模式with open(solver_file, 'w') as f:
        f.write(str(sovler_string))   

if __name__ == '__main__':
    write_sovler()

2.运行结果:

如何使用pycaffe生成solver.prototxt文件并进行训练

三、训练模型

    从第一篇笔记至此,我们已经了解到如何将jpg图片转换成Caffe使用的db(levelbd/lmdb)文件,如何计算数据均值,如何使用python生成solver.prototxt、train.prototxt、test.prototxt文件。接下来,就可以进行训练的最后一步,使用caffe提供的python接口训练生成模型。如果不进行可视化,只想得到一个最终的训练model,可以使用如下代码:

import caffe

my_project_root = "/home/Jack-Cui/caffe-master/my-caffe-project/"        #my-caffe-project目录solver_file = my_project_root + 'solver.prototxt'                        #sovler文件保存位置caffe.set_device(0)                                                      #选择GPU-0caffe.set_mode_gpu()
solver = caffe.SGDSolver(solver_file)
solver.solve()

     现在,如何训练生成模型的简单步骤已经讲完。接下来,以mnist实例,整合所学内容,训练生成model,并使用生成的model进行预测。

关于如何使用pycaffe生成solver.prototxt文件并进行训练就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 使用 tesseract-ocr生成训练库全部指令
  2. 如何使用keras进行多显卡训练

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

solver

上一篇:SQL Stream Builder是什么

下一篇:mysql中TIMESTAMPDIFF函数怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》