400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

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

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

成都创新互联从2013年成立,是专业互联网技术服务公司,拥有项目网站制作、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元抚顺县做网站,已为上家服务,为抚顺县各地企业和个人服务,联系电话:028-86922220

下面主要记录如何生成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文件并进行训练就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


当前名称:如何使用pycaffe生成solver.prototxt文件并进行训练
网站路径:http://mzwzsj.com/article/pshiji.html

其他资讯

让你的专属顾问为你服务