如何编写训练测试的prototxt配置文件---以Resnet为例
2016-12-12 21:27
501 查看
import os os.chdir('/home/wuwl/ResNet') import init_path import caffe import numpy as np import tools from caffe import layers as L,params as P,to_proto this_dir = os.path.abspath(".") def ResNet(split): train_data_file = this_dir + '/caffe-master/examples/cifar10/cifar10_train_lmdb' test_data_file = this_dir + '/caffe-master/examples/cifar10/cifar10_test_lmdb' mean_file = this_dir + '/caffe-master/examples/cifar10/mean.binaryproto' if split == 'train': data,labels = L.Data(source = train_data_file, #训练样本的路径 backend = P.Data.LMDB, #训练样本的格式 batch_size = 128, ntop = 2, # 输出的数目 transform_param = dict(mean_file = mean_file, crop_size = 28, #只有训练才旋转 mirror = True)) else: data,labels = L.Data(source = test_data_file, #测试样本的路径 backend = P.Data.LMDB, batch_size = 128, ntop = 2, transform_param = dict(mean_file = mean_file, crop_size = 28)) repeat = 3 scale,result = conv_BN_scale_relu(split,data,nout = 16,ks = 3,stride = 1,pad = 1) for i in range(repeat): projection_stride = 1 result = ResNet_block(split,result,nout = 16,ks = 3,stride = 1, projection_stride = projection_stride,pad = 1) for i in range(repeat): if i == 0: projection_stride = 2 #直通部分 else: projection_stride = 1 #正常卷积 result = ResNet_block(split,result,nout = 32,ks = 3,stride = 1, projection_stride = projection_stride,pad = 1) for i in range(repeat): if i == 0: projection_stride = 2 #直通部分 else: projection_stride = 1 #正常卷积 result = ResNet_block(split,result,nout = 64,ks = 3,stride = 1, projection_stride = projection_stride,pad = 1) pool = L.Pooling(result,pool = P.Pooling.AVE,global_pooling = True) IP = L.InnerProduct(pool,num_output = 10, weight_filler = dict(type = 'xavier'), bias_filler = dict(type = 'constant')) acc = L.Accuracy(IP,labels) loss = L.SoftmaxWithLoss(IP,labels) return to_proto(acc,loss) def conv_BN_scale_relu(split,bottom,nout,ks,stride,pad): conv = L.Convolution(bottom,kernel_size = ks,stride = stride,num_output = nout, pad = pad,bias_term = True, weight_filler = dict(type = 'xavier'), bias_filler = dict(type = 'constant')) if split == "train": use_global_stats = False else: use_global_stats = True BN = L.BatchNorm(conv,batch_norm_param = dict(use_global_stats = use_global_stats), in_place = True, param = [dict(lr_mult = 0,decay_mult = 0), dict(lr_mult = 0,decay_mult = 0), dict(lr_mult = 0,decay_mult = 0)]) scale = L.Scale(BN,scale_param = dict(bias_term = True),in_place = True) relu = L.ReLU(scale,in_place = True) return scale,relu def ResNet_block(split,bottom,nout,ks,stride,projection_stride,pad): if projection_stride == 1: scale0 = bottom else: scale0,relu0 = conv_BN_scale_relu(split,bottom,nout,1,projection_stride,0) scale1,relu1 = conv_BN_scale_relu(split,bottom,nout,ks,projection_stride,pad) scale2,relu2 = conv_BN_scale_relu(split,relu1,nout,ks,stride,pad) wise = L.Eltwise(scale2,scale0,operation = P.Eltwise.SUM) wise_relu = L.ReLU(wise,in_place = True) return wise_relu def make_net(): with open(this_dir + '/res_net_model/train.prototxt','w') as f: f.write(str(ResNet('train'))) with open(this_dir + '/res_net_model/test.prototxt','w') as f: f.write(str(ResNet('test'))) if __name__ == '__main__': make_net()
相关文章推荐
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- 基于pycaffe从零开始写mnist(第二篇)——生成训练网络结构文件(train.prototxt)+测试网络结构文件(test.prototxt)
- caffe配置文件 网络lenet-train-test.prototxt注释及说明
- 使用protobuf编写配置文件以及读写
- C#编程如何编写的自动读写配置文件.ini格式
- *_train_test.prototxt,*_deploy.prototxt,*_slover.prototxt文件编写时注意事项
- 如何对prototxt文件绘制网络的结构图
- Android编写测试数据库类时对AndroidMainfest文件进行配置
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- 如何备份微软的语音识别引擎的训练配置文件
- ZenCart如何整站转移到另一台服务器 从服务器空间上下的zencart 模板 放到 本地测试 修改 哪些配置文件内容??