利用python接口生成prototxt文件

import numpy as np
import matplotlib.pyplot as plt
import sys,os
caffe_root = '/home/tyd/caffe/'
sys.path.insert(0,caffe_root+'python')
from caffe import layers as L
from caffe import params as P
import caffe

def lenet(lmdb,batch_size):
    #our version of LeNet: a series of linear and simple nonlinear transformations
    n = caffe.NetSpec()
    n.data,n.label = L.Data(batch_size=batch_size,backend = P.Data.LMDB,source = lmdb,transform_param=dict(scale=1./255),ntop=2)
    n.conv1 = L.Convolution(n.data,kernel_size=5,num_output = 20,weight_filler=dict(type='xavier'))
    n.pool1 = L.Pooling(n.conv1,kernel_size=2,stride=2,pool = P.Pooling.MAZ)
    n.conv2 = L.Convolution(n.pool1,kernel_size=5,num_output=50,weight_filler=dict(type='xavier'))
    n.pool2= L.Pooling(n.conv2,kernel_size=2,stride=2,pool=P.Pooling.MAX)
    n.ip1=L.InnerProduct(n.pool2,num_output=500,weight_filler=dict(type='xavier'))
    n.relu1 = L.ReLU(n.ip1,in_place=True)
    n.ip2 = L.InnerProudct(n.relu1,num_output=10,weight_filler=dict(type='xvaier'))
    n.loss = L.SoftmaxWithLoss(n.ip2,n.label)
    retrun n.to_proto()
    
 with open('lenet_auto_train.prototxt','w') as f:
    f.write(str(lenet('examples/mnist/mnist_train_lmdb',64)))
    
 with open('lenet_auto_test.prototxt','w') as f:
    f.write(str(lenet('examples/mnist/mnist_test_lmdb',100)))
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python程序⽣成prototxt⽂件 ⼀、前⾔ 了解到上⼀篇笔记的内容,就可以尝试⾃⼰编写python程序⽣成prototxt⽂件了,当然也可以直接创建⽂件进⾏编写,不过显然,使⽤ python⽣成这个配置⽂件更为简洁。之前已说过cifar10是使⽤cifar10_quick_solver.prototxt配置⽂件来⽣成model。 cifar10_quick_solver.prototxt的内容如下: 从以上代码中可以看出,第四⾏的net参数,指定了训练时使⽤的prototxt⽂件。这个prototxt⽂件也是可以分开写的,分为train.prototxt和 test.prototxt。例如,第四⾏的配置可以改写为: ⼆、Pycaffe API⼩试 solver.prototxt⽂件如何⽣成,在后续的笔记中讲解,先学习如何使⽤python⽣成简单的train.prtotxt⽂件和test.prototxt⽂件。 1.Data Layer: # reduce the learning rate after 8 epochs (4000 iters) by a factor of 10 # The train/test net protocol buffer definition net: "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.001 momentum: 0.9 weight_decay: 0.004 # The learning rate policy lr_policy: "fixed" # Display every 100 iterations display: 100 # The maximum number of iterations max_iter: 4000 # snapshot intermediate results snapshot: 4000 snapshot_format: HDF5 snapshot_prefix: "examples/cifar10/cifar10_quick" # solver mode: CPU or GPU solver_mode: GPU 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 train_net = "examples/cifar10/cifar10_quick_train.prototxt" test_net = "examples/cifar10/cifar10_quick_test.prototxt" 1 2 运⾏结果: 2.Convolution Layer: 添加卷积层: 运⾏结果: # -*- coding: UTF-8 -*- import caffe #导⼊caffe包 caffe_root = "/home/Jack-Cui/caffe-master/my-caffe-project/" #my-caffe-project⽬录 train_lmdb = caffe_root + "img_train.lmdb" #train.lmdb⽂件的位置 mean_file = caffe_root + "mean.binaryproto" #均值⽂件的位置 #⽹络规范 net = caffe.NetSpec() #第⼀层Data层 net.data, net.label = caffe.layers.Data(source = train_lmdb, backend = caffe.params.Data.LMDB, batch_size = 64, ntop=2, transform_param = dict(crop_size = 40,mean_file = mean_file,m

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值