Caffe代码解读1--从一段命令行开始
2018-01-01 00:53
501 查看
以下是我自己的理解,如果有不对,还请各位指教
我的使用环境是ubuntu16.04 + CUDA8.0,安装的是caffe ssd,使用的是caffe的python接口,任务是使用神经网络对模型进行训练
好了,交代完了大环境之后,我们就开始运行代码了。
打开控制台,输入两行命令:
cd /home/**(您服务器的名字)/caffe
python examples/ssd/ssd_pascal.py
第一行表示定位到caffe根目录下,第二行python表示后面的文件用python运行,也就是要运行ssd_pascal.py
接下来,我们就看一看ssd_pascal里面都运行了什么内容吧
先上一个完整的文件代码,里面有我自己写的注释,如果有哪里不对,还请各位指正
这个代码一上来定义了一个添加扩展层的函数AddExtraLayers(net, use_batchnorm=True, lr_mult=1),由于是定义,所以不会运行
然后定义了一大堆的参数,各种文件路径呀,训练/测试用的参数呀,检查文件路径对不对呀之类的,总之,很多很多,一直到make_if_not_exist(snapshot_dir)
才算是结束了。
接下来是这文件最重要的一部分了,创建网络!
依次创建train net, test net, deploy net, 创建这些网络的同时,对应生成了train.prototxt, test.prototxt, deploy.prototxt, 里面保存的都是这些网络的参数,我们也可以很直观的看到。三个网络创建完成之后,创建了solver,这其实不是一个网络,算是一个解决方法,可以理解为一个解题公式吧。solver里也包含了很多参数,这些参数也是至关重要的参数,并创建了solver.prototxt。总结一下这一段,作者将网络以参数的形式保存了下来,储存在prototxt文件中,等待被调用。
有些人可能第一次见到prototxt文件,没关系,直接用阅读代码的软件就能打开,如果想更深入了解的话,可以查找protocol buffer的相关资料。
接下来是一些判断,看是不是要接着以前的训练继续训练呀,要不要删除旧模型呀之类的
最后,作者又创建了一个文件,里面写下了caffe程序运行的参数,这个参数里就包含了作者在上面写的solver.prototxt,而solver.prototxt里面有包含了train.prototxt和test.prototxt,也就是这些文件最终都是要传个caffe主程序的。最后的最后,运行这个文件就好了,也就是python代码里说的job_file。
在这篇python文件中,作者以VGG16为基础,又添加了自己的网络,构建出新的网络(SSD),并创建了多个prototxt文件用来保存网络参数和训练参数之类的数据,最后调用caffe主程序去读入这些数据。
第一篇caffe解读就先写这么多吧,剩下的放在后面的文章中写。如果各位有什么不懂的地方,可以在底下给我留言,咱们一起探讨。
我的使用环境是ubuntu16.04 + CUDA8.0,安装的是caffe ssd,使用的是caffe的python接口,任务是使用神经网络对模型进行训练
好了,交代完了大环境之后,我们就开始运行代码了。
打开控制台,输入两行命令:
cd /home/**(您服务器的名字)/caffe
python examples/ssd/ssd_pascal.py
第一行表示定位到caffe根目录下,第二行python表示后面的文件用python运行,也就是要运行ssd_pascal.py
接下来,我们就看一看ssd_pascal里面都运行了什么内容吧
先上一个完整的文件代码,里面有我自己写的注释,如果有哪里不对,还请各位指正
from __future__ import print_function import caffe from caffe.model_libs import * from google.protobuf import text_format import math import os import shutil import stat import subprocess import sys # Add extra layers on top of a "base" network (e.g. VGGNet or Inception). def AddExtraLayers(net, use_batchnorm=True, lr_mult=1): use_relu = True # Add additional convolutional layers. # 19 x 19 from_layer = net.keys()[-1] # TODO(weiliu89): Construct the name using the last layer to avoid duplication. # 10 x 10 out_layer = "conv6_1" ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 1, 0, 1, lr_mult=lr_mult) from_layer = out_layer out_layer = "conv6_2" ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 512, 3, 1, 2, lr_mult=lr_mult) # 5 x 5 from_layer = out_layer out_layer = "conv7_1" ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 128, 1, 0, 1, lr_mult=lr_mult) from_layer = out_layer out_layer = "conv7_2" ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 3, 1, 2, lr_mult=lr_mult) # 3 x 3 from_layer = out_layer out_layer = "conv8_1" ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 128, 1, 0, 1, lr_mult=lr_mult) from_layer = out_layer out_layer = "conv8_2" ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 3, 0, 1, lr_mult=lr_mult) # 1 x 1 from_layer = out_layer out_layer = "conv9_1" ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 128, 1, 0, 1, lr_mult=lr_mult) from_layer = out_layer out_layer = "conv9_2" ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 3, 0, 1, lr_mult=lr_mult) return net ### Modify the following parameters accordingly ### # The directory which contains the caffe code. # We assume you are running the script at the CAFFE_ROOT. #getcwd()可以返回当前工作路径,caffe_root是一个字符串 #由于在执行这个ssd_pascal.py文件之前,我们通过命令行cd /home/**(您服务器的名字)/caffe #将路径定位到caffe根目录下,所以下面caffe_root保存的是caffe的根目录 caffe_root = os.getcwd() # Set true if you want to start training right after generating all files. run_soon = True#在生成配置文件之后,马上开始训练 # Set true if you want to load from most recently saved snapshot. # Otherwise, we will load from the pretrain_model defined below. resume_training = True#接着以前的训练继续训练 # If true, Remove old model files. remove_old_models = False#删除旧模型的文件 # The database file for training data. Created by data/VOC0712/create_data.sh train_data = "examples/VOC0712/VOC0712_trainval_lmdb"#训练集的文件路径 # The database file for testing data. Created by data/VOC0712/create_data.sh test_data = "examples/VOC0712/VOC0712_test_lmdb"#测试集的文件路径 # Specify the batch sampler. resize_width = 300 resize_height = 300 resize = "{}x{}".format(resize_width, resize_height) batch_sampler = [ { 'sampler': { }, 'max_trials': 1, 'max_sample': 1, }, { 'sampler': { 'min_scale': 0.3, 'max_scale': 1.0, 'min_aspect_ratio': 0.5, 'max_aspect_ratio': 2.0, }, 'sample_constraint': { 'min_jaccard_overlap': 0.1, }, 'max_trials': 50, 'max_sample': 1, }, { 'sampler': { 'min_scale': 0.3, 'max_scale': 1.0, 'min_aspect_ratio': 0.5, 'max_aspect_ratio': 2.0, }, 'sample_constraint': { 'min_jaccard_overlap': 0.3, }, 'max_trials': 50, 'max_sample': 1, }, { 'sampler': { 'min_scale': 0.3, 'max_scale': 1.0, 'min_aspect_ratio': 0.5, 'max_aspect_ratio': 2.0, }, 'sample_constraint': { 'min_jaccard_overlap': 0.5, }, 'max_trials': 50, 'max_sample': 1, }, { 'sampler': { 'min_scale': 0.3, 'max_scale': 1.0, 'min_aspect_ratio': 0.5, 'max_aspect_ratio': 2.0, }, 'sample_constraint': { 'min_jaccard_overlap': 0.7, }, 'max_trials': 50, 'max_sample': 1, }, { 'sampler': { 'min_scale': 0.3, 'max_scale': 1.0, 'min_aspect_ratio': 0.5, 'max_aspect_ratio': 2.0, }, 'sample_constraint': { 'min_jaccard_overlap': 0.9, }, 'max_trials': 50, 'max_sample': 1, }, { 'sampler': { 'min_scale': 0.3, 'max_scale': 1.0, 'min_aspect_ratio': 0.5, 'max_aspect_ratio': 2.0, }, 'sample_constraint': { 'max_jaccard_overlap': 1.0, }, 'max_trials': 50, 'max_sample': 1, }, ] train_transform_param = { 'mirror': True, 'mean_value': [104, 117, 123], 'resize_param': { 'prob': 1, 'resize_mode': P.Resize.WARP, 'height': resize_height, 'width': resize_width, 'interp_mode': [ P.Resize.LINEAR, P.Resize.AREA, P.Resize.NEAREST, P.Resize.CUBIC, P.Resize.LANCZOS4, ], }, 'distort_param': { 'brightness_prob': 0.5, 'brightness_delta': 32, 'contrast_prob': 0.5, 'contrast_lower': 0.5, 'contrast_upper': 1.5, 'hue_prob': 0.5, 'hue_delta': 18, 'saturation_prob': 0.5, 'saturation_lower': 0.5, 'saturation_upper': 1.5, 'random_order_prob': 0.0, }, 'expand_param': { 'prob': 0.5, 'max_expand_ratio': 4.0, }, 'emit_constraint': { 'emit_type': caffe_pb2.EmitConstraint.CENTER, } } test_transform_param = { 'mean_value': [104, 117, 123], 'resize_param': { 'prob': 1, 'resize_mode': P.Resize.WARP, 'height': resize_height, 'width': resize_width, 'interp_mode': [P.Resize.LINEAR], }, } # If true, use batch norm for all newly added layers. # Currently only the non batch norm version has been tested. use_batchnorm = False lr_mult = 1 # Use different initial learning rate. if use_batchnorm: base_lr = 0.0004 else: # A learning rate for batch_size = 1, num_gpus = 1. base_lr = 0.00004 # Modify the job name if you want. job_name = "SSD_{}".format(resize) # The name of the model. Modify it if you want. model_name = "VGG_VOC0712_{}".format(job_name) # Directory which stores the model .prototxt file. save_dir = "models/VGGNet/VOC0712/{}".format(job_name) # Directory which stores the snapshot of models. snapshot_dir = "models/VGGNet/VOC0712/{}".format(job_name) # Directory which stores the job script and log file. job_dir = "jobs/VGGNet/VOC0712/{}".format(job_name) # Directory which stores the detection results. output_result_dir = "{}/data/VOCdevkit/results/VOC2007/{}/Main".format(os.environ['HOME'], job_name) # model definition files. train_n cecd et_file = "{}/train.prototxt".format(save_dir) test_net_file = "{}/test.prototxt".format(save_dir) deploy_net_file = "{}/deploy.prototxt".format(save_dir) solver_file = "{}/solver.prototxt".format(save_dir) # snapshot prefix. snapshot_prefix = "{}/{}".format(snapshot_dir, model_name) # job script path. job_file = "{}/{}.sh".format(job_dir, model_name) # Stores the test image names and sizes. Created by data/VOC0712/create_list.sh name_size_file = "data/VOC0712/test_name_size.txt" # The pretrained model. We use the Fully convolutional reduced (atrous) VGGNet. pretrain_model = "models/VGGNet/VGG_ILSVRC_16_layers_fc_reduced.caffemodel" # Stores LabelMapItem. label_map_file = "data/VOC0712/labelmap_voc.prototxt" # MultiBoxLoss parameters. num_classes = 21 share_location = True background_label_id=0 train_on_diff_gt = True normalization_mode = P.Loss.VALID code_type = P.PriorBox.CENTER_SIZE ignore_cross_boundary_bbox = False mining_type = P.MultiBoxLoss.MAX_NEGATIVE neg_pos_ratio = 3. loc_weight = (neg_pos_ratio + 1.) / 4. multibox_loss_param = { 'loc_loss_type': P.MultiBoxLoss.SMOOTH_L1, 'conf_loss_type': P.MultiBoxLoss.SOFTMAX, 'loc_weight': loc_weight, 'num_classes': num_classes, 'share_location': share_location, 'match_type': P.MultiBoxLoss.PER_PREDICTION, 'overlap_threshold': 0.5, 'use_prior_for_matching': True, 'background_label_id': background_label_id, 'use_difficult_gt': train_on_diff_gt, 'mining_type': mining_type, 'neg_pos_ratio': neg_pos_ratio, 'neg_overlap': 0.5, 'code_type': code_type, 'ignore_cross_boundary_bbox': ignore_cross_boundary_bbox, } loss_param = { 'normalization': normalization_mode, } # parameters for generating priors. # minimum dimension of input image min_dim = 300 # conv4_3 ==> 38 x 38 # fc7 ==> 19 x 19 # conv6_2 ==> 10 x 10 # conv7_2 ==> 5 x 5 # conv8_2 ==> 3 x 3 # conv9_2 ==> 1 x 1 mbox_source_layers = ['conv4_3', 'fc7', 'conv6_2', 'conv7_2', 'conv8_2', 'conv9_2'] # in percent % min_ratio = 20 max_ratio = 90 step = int(math.floor((max_ratio - min_ratio) / (len(mbox_source_layers) - 2)))#(90-20)/(6-2) = 17 min_sizes = [] max_sizes = [] for ratio in xrange(min_ratio, max_ratio + 1, step): min_sizes.append(min_dim * ratio / 100.) # 60,111,162,213,264 max_sizes.append(min_dim * (ratio + step) / 100.)#111,162,213,264,315 min_sizes = [min_dim * 10 / 100.] + min_sizes#30, 60,111,162,213,264 max_sizes = [min_dim * 20 / 100.] + max_sizes#60,111,162,213,264,315 steps = [8, 16, 32, 64, 100, 300] aspect_ratios = [[2], [2, 3], [2, 3], [2, 3], [2], [2]] # L2 normalize conv4_3. normalizations = [20, -1, -1, -1, -1, -1] # variance used to encode/decode prior bboxes. if code_type == P.PriorBox.CENTER_SIZE: prior_variance = [0.1, 0.1, 0.2, 0.2] else: prior_variance = [0.1] flip = True clip = False # Solver parameters. # Defining which GPUs to use. gpus = "0,1,2,3" gpulist = gpus.split(",") num_gpus = len(gpulist) # Divide the mini-batch to different GPUs. batch_size = 32 accum_batch_size = 32 iter_size = accum_batch_size / batch_size solver_mode = P.Solver.CPU device_id = 0 batch_size_per_device = batch_size if num_gpus > 0: batch_size_per_device = int(math.ceil(float(batch_size) / num_gpus)) iter_size = int(math.ceil(float(accum_batch_size) / (batch_size_per_device * num_gpus))) solver_mode = P.Solver.GPU device_id = int(gpulist[0]) if normalization_mode == P.Loss.NONE: base_lr /= batch_size_per_device elif normalization_mode == P.Loss.VALID: base_lr *= 25. / loc_weight elif normalization_mode == P.Loss.FULL: # Roughly there are 2000 prior bboxes per image. # TODO(weiliu89): Estimate the exact # of priors. base_lr *= 2000. # Evaluate on whole test set. num_test_image = 4952 test_batch_size = 8 # Ideally test_batch_size should be divisible by num_test_image, # otherwise mAP will be slightly off the true value. test_iter = int(math.ceil(float(num_test_image) / test_batch_size)) solver_param = { # Train parameters 'base_lr': base_lr, 'weight_decay': 0.0005, 'lr_policy': "multistep", 'stepvalue': [80000, 100000, 120000], 'gamma': 0.1, 'momentum': 0.9, 'iter_size': iter_size, 'max_iter': 120000, 'snapshot': 80000, 'display': 10, 'average_loss': 10, 'type': "SGD", 'solver_mode': solver_mode, 'device_id': device_id, 'debug_info': False, 'snapshot_after_train': True, # Test parameters 'test_iter': [test_iter], 'test_interval': 10000, 'eval_type': "detection", 'ap_version': "11point", 'test_initialization': False, } # parameters for generating detection output. det_out_param = { 'num_classes': num_classes, 'share_location': share_location, 'background_label_id': background_label_id, 'nms_param': {'nms_threshold': 0.45, 'top_k': 400}, 'save_output_param': { 'output_directory': output_result_dir, 'output_name_prefix': "comp4_det_test_", 'output_format': "VOC", 'label_map_file': label_map_file, 'name_size_file': name_size_file, 'num_test_image': num_test_image, }, 'keep_top_k': 200, 'confidence_threshold': 0.01, 'code_type': code_type, } # parameters for evaluating detection results. det_eval_param = { 'num_classes': num_classes, 'background_label_id': background_label_id, 'overlap_threshold': 0.5, 'evaluate_difficult_gt': False, 'name_size_file': name_size_file, } ### Hopefully you don't need to change the following ### # Check file.检查文件路径是否存在, check_if_exist(train_data) check_if_exist(test_data) check_if_exist(label_map_file) check_if_exist(pretrain_model) #如果文件路径不存在,就创建路径 make_if_not_exist(save_dir) make_if_not_exist(job_dir) make_if_not_exist(snapshot_dir) ''' 下面是先建立net对象,然后将net的参数序列化,创建.prototxt文件 通过这个方式,创建了train.prototxt, test.prototxt, deploy.prototxt和solver.prototxt四个文件 这四个文件又会被复制到指定的路径下,也就是job_dir ''' # Create train net. #训练网络包括一部分的VGG16 net,然后再加上SSD需要的网络,所有的信息都保存在net对象里 #由于SSD需要在6个层中提取特征,所以创建了mbox_layers net = caffe.NetSpec() net.data, net.label = CreateAnnotatedDataLayer(train_data, batch_size=batch_size_per_device, train=True, output_label=True, label_map_file=label_map_file, transform_param=train_transform_param, batch_sampler=batch_sampler) VGGNetBody(net, from_layer='data', fully_conv=True, reduced=True, dilated=True, dropout=False) AddExtraLayers(net, use_batchnorm, lr_mult=lr_mult) mbox_layers = CreateMultiBoxHead(net, data_layer='data', from_layers=mbox_source_layers, use_batchnorm=use_batchnorm, min_sizes=min_sizes, max_sizes=max_sizes, aspect_ratios=aspect_ratios, steps=steps, normalizations=normalizations, num_classes=num_classes, share_location=share_location, flip=flip, clip=clip, prior_variance=prior_variance, kernel_size=3, pad=1, lr_mult=lr_mult) # Create the MultiBoxLossLayer. name = "mbox_loss" mbox_layers.append(net.label) net[name] = L.MultiBoxLoss(*mbox_layers, multibox_loss_param=multibox_loss_param, loss_param=loss_param, include=dict(phase=caffe_pb2.Phase.Value('TRAIN')), propagate_down=[True, True, False, False]) with open(train_net_file, 'w') as f:#这个文件应该是train.prototxt print('name: "{}_train"'.format(model_name), file=f)#写train_net_file文件的第一行 print(net.to_proto(), file=f)#将net对象里面的信息序列化,写入到train_net_file中 shutil.copy(train_net_file, job_dir)#写完之后将文件复制到job_dir路径下,文件名不变 # Create test net. #创建测试网络,方法和创建训练网络一样 net = caffe.NetSpec() net.data, net.label = CreateAnnotatedDataLayer(test_data, batch_size=test_batch_size, train=False, output_label=True, label_map_file=label_map_file, transform_param=test_transform_param) VGGNetBody(net, from_layer='data', fully_conv=True, reduced=True, dilated=True, dropout=False) AddExtraLayers(net, use_batchnorm, lr_mult=lr_mult) mbox_layers = CreateMultiBoxHead(net, data_layer='data', from_layers=mbox_source_layers, use_batchnorm=use_batchnorm, min_sizes=min_sizes, max_sizes=max_sizes, aspect_ratios=aspect_ratios, steps=steps, normalizations=normalizations, num_classes=num_classes, share_location=share_location, flip=flip, clip=clip, prior_variance=prior_variance, kernel_size=3, pad=1, lr_mult=lr_mult) conf_name = "mbox_conf" if multibox_loss_param["conf_loss_type"] == P.MultiBoxLoss.SOFTMAX: reshape_name = "{}_reshape".format(conf_name) net[reshape_name] = L.Reshape(net[conf_name], shape=dict(dim=[0, -1, num_classes])) softmax_name = "{}_softmax".format(conf_name) net[softmax_name] = L.Softmax(net[reshape_name], axis=2) flatten_name = "{}_flatten".format(conf_name) net[flatten_name] = L.Flatten(net[softmax_name], axis=1) mbox_layers[1] = net[flatten_name] elif multibox_loss_param["conf_loss_type"] == P.MultiBoxLoss.LOGISTIC: sigmoid_name = "{}_sigmoid".format(conf_name) net[sigmoid_name] = L.Sigmoid(net[conf_name]) mbox_layers[1] = net[sigmoid_name] net.detection_out = L.DetectionOutput(*mbox_layers, detection_output_param=det_out_param, include=dict(phase=caffe_pb2.Phase.Value('TEST'))) net.detection_eval = L.DetectionEvaluate(net.detection_out, net.label, detection_evaluate_param=det_eval_param, include=dict(phase=caffe_pb2.Phase.Value('TEST'))) with open(test_net_file, 'w') as f:#这个文件应该是test.prototxt print('name: "{}_test"'.format(model_name), file=f) print(net.to_proto(), file=f) shutil.copy(test_net_file, job_dir) # Create deploy net. # Remove the first and last layer from test net. deploy_net = net with open(deploy_net_file, 'w') as f:#这个文件应该是deploy.prototxt net_param = deploy_net.to_proto() # Remove the first (AnnotatedData) and last (DetectionEvaluate) layer from test net. del net_param.layer[0] del net_param.layer[-1] net_param.name = '{}_deploy'.format(model_name) net_param.input.extend(['data']) net_param.input_shape.extend([ caffe_pb2.BlobShape(dim=[1, 3, resize_height, resize_width])]) print(net_param, file=f) shutil.copy(deploy_net_file, job_dir) # Create solver. solver = caffe_pb2.SolverParameter( train_net=train_net_file, test_net=[test_net_file], snapshot_prefix=snapshot_prefix, **solver_param) with open(solver_file, 'w') as f:#这个文件应该是solver.prototxt print(solver, file=f) shutil.copy(solver_file, job_dir) '''----------------------------------------------------------------------------------------------------''' max_iter = 0 # Find most recent snapshot. for file in os.listdir(snapshot_dir): if file.endswith(".solverstate"): basename = os.path.splitext(file)[0] iter = int(basename.split("{}_iter_".format(model_name))[1]) if iter > max_iter: max_iter = iter train_src_param = '--weights="{}" \\\n'.format(pretrain_model) if resume_training:#如果接着以前的继续训练,就用snapshot参数 if max_iter > 0: train_src_param = '--snapshot="{}_iter_{}.solverstate" \\\n'.format(snapshot_prefix, max_iter) if remove_old_models: # Remove any snapshots smaller than max_iter. for file in os.listdir(snapshot_dir): if file.endswith(".solverstate"): basename = os.path.splitext(file)[0] iter = int(basename.split("{}_iter_".format(model_name))[1]) if max_iter > iter: os.remove("{}/{}".format(snapshot_dir, file)) if file.endswith(".caffemodel"): basename = os.path.splitext(file)[0] iter = int(basename.split("{}_iter_".format(model_name))[1]) if max_iter > iter: os.remove("{}/{}".format(snapshot_dir, file)) # Create job file. with open(job_file, 'w') as f: f.write('cd {}\n'.format(caffe_root))#在job_file文件下写命令行,跳到caffe根目录下 #运行这个路径下的caffe,这个caffe是由 #cd /home/**(您服务器的名字)/caffe #mkdir build #cd build #cmake ..(cmake和..中间有一个空格) #make all -j16("‐j16"是使用 CPU 的多核进行编译,可以极大地加速编译的速度) #make install #make runtest(这一步不是必须的) #make pycaffe(编译pycaffe) #这一串操作生成的文件 f.write('./build/tools/caffe train \\\n') f.write('--solver="{}" \\\n'.format(solver_file)) f.write(train_src_param) if solver_param['solver_mode'] == P.Solver.GPU:#使用GPU f.write('--gpu {} 2>&1 | tee {}/{}.log\n'.format(gpus, job_dir, model_name)) else: f.write('2>&1 | tee {}/{}.log\n'.format(job_dir, model_name)) # Copy the python script to job_dir. py_file = os.path.abspath(__file__)#返回当前的绝对路径,保存在py_file里面 shutil.copy(py_file, job_dir) # Run the job. #os.chmod()可以更改文件或是目录的权限 #stat.S_IRWXU表示用户对job_file文件有读写的全部权限 os.chmod(job_file, stat.S_IRWXU) if run_soon: subprocess.call(job_file, shell=True)#执行job_file文件,文件配置在上面已经写好,就是一些命令行
这个代码一上来定义了一个添加扩展层的函数AddExtraLayers(net, use_batchnorm=True, lr_mult=1),由于是定义,所以不会运行
然后定义了一大堆的参数,各种文件路径呀,训练/测试用的参数呀,检查文件路径对不对呀之类的,总之,很多很多,一直到make_if_not_exist(snapshot_dir)
才算是结束了。
接下来是这文件最重要的一部分了,创建网络!
依次创建train net, test net, deploy net, 创建这些网络的同时,对应生成了train.prototxt, test.prototxt, deploy.prototxt, 里面保存的都是这些网络的参数,我们也可以很直观的看到。三个网络创建完成之后,创建了solver,这其实不是一个网络,算是一个解决方法,可以理解为一个解题公式吧。solver里也包含了很多参数,这些参数也是至关重要的参数,并创建了solver.prototxt。总结一下这一段,作者将网络以参数的形式保存了下来,储存在prototxt文件中,等待被调用。
有些人可能第一次见到prototxt文件,没关系,直接用阅读代码的软件就能打开,如果想更深入了解的话,可以查找protocol buffer的相关资料。
接下来是一些判断,看是不是要接着以前的训练继续训练呀,要不要删除旧模型呀之类的
最后,作者又创建了一个文件,里面写下了caffe程序运行的参数,这个参数里就包含了作者在上面写的solver.prototxt,而solver.prototxt里面有包含了train.prototxt和test.prototxt,也就是这些文件最终都是要传个caffe主程序的。最后的最后,运行这个文件就好了,也就是python代码里说的job_file。
在这篇python文件中,作者以VGG16为基础,又添加了自己的网络,构建出新的网络(SSD),并创建了多个prototxt文件用来保存网络参数和训练参数之类的数据,最后调用caffe主程序去读入这些数据。
第一篇caffe解读就先写这么多吧,剩下的放在后面的文章中写。如果各位有什么不懂的地方,可以在底下给我留言,咱们一起探讨。
相关文章推荐
- Caffe代码解读(一):blob.hpp
- Caffe代码解读(七):其他常用层及参数
- caffe代码解读-mnist例子-前向计算
- 用于返回本周的开始日期及结束日期一段JS代码
- 一段好玩的Python代码解读
- JVM学习笔记 -- 从一段几乎所有人代码都会犯错的代码开始
- 一段代码 完全解读 SharedPreferedces。
- Caffe 代码解读之 softmax layer
- JVM学习笔记 -- 从一段几乎所有人代码都会犯错的代码开始
- Caffe源码解读(一):代码组织结构
- 从一段奇怪代码开始说
- Java入门学习-学会用txt文档写一段helloworld,并通过windows命令行编译和执行该Java代码
- 【深度学习】【Caffe源代码解读4】笔记22 Caffe框架中I/O模块的代码初探
- 开始学习PHP,新写一段小代码检测IE版本,求指导
- 一段js代码解读
- Caffe 代码解读之全连接层concat layer
- caffe for windows 模型中 solver.prototxt 代码解读
- Caffe代码解读:Protobuf
- #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #endif 解读
- 由Map的新特性解读一段代码