From 38cab75c0ddac39267a5547204575c565cb7f117 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 14 Mar 2014 16:15:42 -0700 Subject: [PATCH 01/60] fix caffe.proto style bugs --- src/caffe/proto/caffe.proto | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 77d6d571151..71dc82c4367 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -7,8 +7,8 @@ message BlobProto { optional int32 channels = 2 [default = 0]; optional int32 height = 3 [default = 0]; optional int32 width = 4 [default = 0]; - repeated float data = 5 [packed=true]; - repeated float diff = 6 [packed=true]; + repeated float data = 5 [packed = true]; + repeated float diff = 6 [packed = true]; } // The BlobProtoVector is simply a way to pass multiple blobproto instances @@ -69,7 +69,7 @@ message LayerParameter { // For data pre-processing, we can do simple scaling and subtracting the // data mean, if provided. Note that the mean subtraction is always carried // out before scaling. - optional float scale = 17 [ default = 1 ]; + optional float scale = 17 [default = 1]; optional string meanfile = 18; // For data layers, specify the batch size. optional uint32 batchsize = 19; @@ -90,7 +90,7 @@ message LayerParameter { // to avoid all asynchronous sgd clients to start at the same point. The skip // point would be set as rand_skip * rand(0,1). Note that rand_skip should not // be larger than the number of keys in the leveldb. - optional uint32 rand_skip = 53 [ default = 0 ]; + optional uint32 rand_skip = 53 [default = 0]; // Fields related to detection (det_*) // foreground (object) overlap threshold @@ -151,16 +151,16 @@ message NetParameter { // Whether the network will force every layer to carry out backward operation. // If set False, then whether to carry out backward is determined // automatically according to the net structure and learning rates. - optional bool force_backward = 5 [ default = false ]; + optional bool force_backward = 5 [default = false]; } message SolverParameter { optional string train_net = 1; // The proto file for the training net. optional string test_net = 2; // The proto file for the testing net. // The number of iterations for each testing phase. - optional int32 test_iter = 3 [ default = 0 ]; + optional int32 test_iter = 3 [default = 0]; // The number of iterations between two testing phases. - optional int32 test_interval = 4 [ default = 0 ]; + optional int32 test_interval = 4 [default = 0]; optional float base_lr = 5; // The base learning rate // the number of iterations between displaying info. If display = 0, no info // will be displayed. @@ -176,10 +176,10 @@ message SolverParameter { optional string snapshot_prefix = 15; // The prefix for the snapshot. // whether to snapshot diff in the results or not. Snapshotting diff will help // debugging but the final protocol buffer size will be much larger. - optional bool snapshot_diff = 16 [ default = false]; + optional bool snapshot_diff = 16 [default = false]; // the mode solver will use: 0 for CPU and 1 for GPU. Use GPU in default. optional int32 solver_mode = 17 [default = 1]; - // the device_id will that be used in GPU mode. Use device_id=0 in default. + // the device_id will that be used in GPU mode. Use device_id = 0 in default. optional int32 device_id = 18 [default = 0]; } From 53ca9cde3ff2ee91c230168e00d6f992ec04ffbd Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 14 Mar 2014 16:18:37 -0700 Subject: [PATCH 02/60] create file caffe.proto.v0 which duplicates current caffe.proto --- src/caffe/proto/caffe.proto.v0 | 156 +++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 src/caffe/proto/caffe.proto.v0 diff --git a/src/caffe/proto/caffe.proto.v0 b/src/caffe/proto/caffe.proto.v0 new file mode 100644 index 00000000000..d949a2652fa --- /dev/null +++ b/src/caffe/proto/caffe.proto.v0 @@ -0,0 +1,156 @@ +// Copyright 2013 Yangqing Jia + +package caffe; + +message BlobProto { + optional int32 num = 1 [default = 0]; + optional int32 channels = 2 [default = 0]; + optional int32 height = 3 [default = 0]; + optional int32 width = 4 [default = 0]; + repeated float data = 5 [packed = true]; + repeated float diff = 6 [packed = true]; +} + +// The BlobProtoVector is simply a way to pass multiple blobproto instances +// around. +message BlobProtoVector { + repeated BlobProto blobs = 1; +} + +message Datum { + optional int32 channels = 1; + optional int32 height = 2; + optional int32 width = 3; + // the actual image data, in bytes + optional bytes data = 4; + optional int32 label = 5; + // Optionally, the datum could also hold float data. + repeated float float_data = 6; +} + +message FillerParameter { + // The filler type. + optional string type = 1 [default = 'constant']; + optional float value = 2 [default = 0]; // the value in constant filler + optional float min = 3 [default = 0]; // the min value in uniform filler + optional float max = 4 [default = 1]; // the max value in uniform filler + optional float mean = 5 [default = 0]; // the mean value in gaussian filler + optional float std = 6 [default = 1]; // the std value in gaussian filler +} + +message LayerParameter { + optional string name = 1; // the layer name + optional string type = 2; // the string to specify the layer type + + // Parameters to specify layers with inner products. + optional uint32 num_output = 3; // The number of outputs for the layer + optional bool biasterm = 4 [default = true]; // whether to have bias terms + optional FillerParameter weight_filler = 5; // The filler for the weight + optional FillerParameter bias_filler = 6; // The filler for the bias + + optional uint32 pad = 7 [default = 0]; // The padding size + optional uint32 kernelsize = 8; // The kernel size + optional uint32 group = 9 [default = 1]; // The group size for group conv + optional uint32 stride = 10 [default = 1]; // The stride + enum PoolMethod { + MAX = 0; + AVE = 1; + STOCHASTIC = 2; + } + optional PoolMethod pool = 11 [default = MAX]; // The pooling method + optional float dropout_ratio = 12 [default = 0.5]; // dropout ratio + + optional uint32 local_size = 13 [default = 5]; // for local response norm + optional float alpha = 14 [default = 1.]; // for local response norm + optional float beta = 15 [default = 0.75]; // for local response norm + + // For data layers, specify the data source + optional string source = 16; + // For data pre-processing, we can do simple scaling and subtracting the + // data mean, if provided. Note that the mean subtraction is always carried + // out before scaling. + optional float scale = 17 [default = 1]; + optional string meanfile = 18; + // For data layers, specify the batch size. + optional uint32 batchsize = 19; + // For data layers, specify if we would like to randomly crop an image. + optional uint32 cropsize = 20 [default = 0]; + // For data layers, specify if we want to randomly mirror data. + optional bool mirror = 21 [default = false]; + + // The blobs containing the numeric parameters of the layer + repeated BlobProto blobs = 50; + // The ratio that is multiplied on the global learning rate. If you want to set + // the learning ratio for one blob, you need to set it for all blobs. + repeated float blobs_lr = 51; + // The weight decay that is multiplied on the global weight decay. + repeated float weight_decay = 52; + + // The rand_skip variable is for the data layer to skip a few data points + // to avoid all asynchronous sgd clients to start at the same point. The skip + // point would be set as rand_skip * rand(0,1). Note that rand_skip should not + // be larger than the number of keys in the leveldb. + optional uint32 rand_skip = 53 [default = 0]; + + // Concat Layer needs to specify the dimension along the concat will happen, + // the other dimensions must be the same for all the bottom blobs + // By default it will concatenate blobs along channels dimension + optional uint32 concat_dim = 65 [default = 1]; +} + +message LayerConnection { + optional LayerParameter layer = 1; // the layer parameter + repeated string bottom = 2; // the name of the bottom blobs + repeated string top = 3; // the name of the top blobs +} + +message NetParameter { + optional string name = 1; // consider giving the network a name + repeated LayerConnection layers = 2; // a bunch of layers. + // The input blobs to the network. + repeated string input = 3; + // The dim of the input blobs. For each input blob there should be four + // values specifying the num, channels, height and width of the input blob. + // Thus, there should be a total of (4 * #input) numbers. + repeated int32 input_dim = 4; + // Whether the network will force every layer to carry out backward operation. + // If set False, then whether to carry out backward is determined + // automatically according to the net structure and learning rates. + optional bool force_backward = 5 [default = false]; +} + +message SolverParameter { + optional string train_net = 1; // The proto file for the training net. + optional string test_net = 2; // The proto file for the testing net. + // The number of iterations for each testing phase. + optional int32 test_iter = 3 [default = 0]; + // The number of iterations between two testing phases. + optional int32 test_interval = 4 [default = 0]; + optional float base_lr = 5; // The base learning rate + // the number of iterations between displaying info. If display = 0, no info + // will be displayed. + optional int32 display = 6; + optional int32 max_iter = 7; // the maximum number of iterations + optional string lr_policy = 8; // The learning rate decay policy. + optional float gamma = 9; // The parameter to compute the learning rate. + optional float power = 10; // The parameter to compute the learning rate. + optional float momentum = 11; // The momentum value. + optional float weight_decay = 12; // The weight decay. + optional int32 stepsize = 13; // the stepsize for learning rate policy "step" + optional int32 snapshot = 14 [default = 0]; // The snapshot interval + optional string snapshot_prefix = 15; // The prefix for the snapshot. + // whether to snapshot diff in the results or not. Snapshotting diff will help + // debugging but the final protocol buffer size will be much larger. + optional bool snapshot_diff = 16 [default = false]; + // the mode solver will use: 0 for CPU and 1 for GPU. Use GPU in default. + optional int32 solver_mode = 17 [default = 1]; + // the device_id will that be used in GPU mode. Use device_id = 0 in default. + optional int32 device_id = 18 [default = 0]; +} + +// A message that stores the solver snapshots +message SolverState { + optional int32 iter = 1; // The current iteration + optional string learned_net = 2; // The file that stores the learned net. + repeated BlobProto history = 3; // The history for sgd solvers +} From 1dc33749134b7d936e231f4decbb02b27243ab7c Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 14 Mar 2014 16:44:52 -0700 Subject: [PATCH 03/60] move individual layer parameters to individual proto messages --- src/caffe/proto/caffe.proto | 126 ++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 57 deletions(-) diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 71dc82c4367..fb76979a6b7 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -42,56 +42,85 @@ message LayerParameter { optional string name = 1; // the layer name optional string type = 2; // the string to specify the layer type - // Parameters to specify layers with inner products. - optional uint32 num_output = 3; // The number of outputs for the layer - optional bool biasterm = 4 [default = true]; // whether to have bias terms - optional FillerParameter weight_filler = 5; // The filler for the weight - optional FillerParameter bias_filler = 6; // The filler for the bias - - optional uint32 pad = 7 [default = 0]; // The padding size - optional uint32 kernelsize = 8; // The kernel size - optional uint32 group = 9 [default = 1]; // The group size for group conv - optional uint32 stride = 10 [default = 1]; // The stride - enum PoolMethod { - MAX = 0; - AVE = 1; - STOCHASTIC = 2; - } - optional PoolMethod pool = 11 [default = MAX]; // The pooling method - optional float dropout_ratio = 12 [default = 0.5]; // dropout ratio - - optional uint32 local_size = 13 [default = 5]; // for local response norm - optional float alpha = 14 [default = 1.]; // for local response norm - optional float beta = 15 [default = 0.75]; // for local response norm - - // For data layers, specify the data source - optional string source = 16; - // For data pre-processing, we can do simple scaling and subtracting the - // data mean, if provided. Note that the mean subtraction is always carried - // out before scaling. - optional float scale = 17 [default = 1]; - optional string meanfile = 18; - // For data layers, specify the batch size. - optional uint32 batchsize = 19; - // For data layers, specify if we would like to randomly crop an image. - optional uint32 cropsize = 20 [default = 0]; - // For data layers, specify if we want to randomly mirror data. - optional bool mirror = 21 [default = false]; - // The blobs containing the numeric parameters of the layer - repeated BlobProto blobs = 50; + repeated BlobProto blobs = 3; // The ratio that is multiplied on the global learning rate. If you want to // set the learning ratio for one blob, you need to set it for all blobs. - repeated float blobs_lr = 51; + repeated float blobs_lr = 4; // The weight decay that is multiplied on the global weight decay. - repeated float weight_decay = 52; + repeated float weight_decay = 5; + + // Parameters for particular layer types. + optional DataParameter data_param = 6; + optional InnerProductParameter inner_product_param = 7; + optional ConvolutionParameter convolution_param = 8; + optional PoolParameter pool_param = 9; + optional DropoutParameter dropout_param = 10; + optional LRNParameter lrn_param = 11; +} +message DataParameter { + // Specify the data source. + optional string source = 1; + // For data pre-processing, we can do simple scaling and subtracting the + // data mean, if provided. Note that the mean subtraction is always carried + // out before scaling. + optional float scale = 2 [default = 1]; + optional string meanfile = 3; + // Specify the batch size. + optional uint32 batchsize = 4; + // Specify if we would like to randomly crop an image. + optional uint32 cropsize = 5 [default = 0]; + // Specify if we want to randomly mirror data. + optional bool mirror = 6 [default = false]; // The rand_skip variable is for the data layer to skip a few data points // to avoid all asynchronous sgd clients to start at the same point. The skip // point would be set as rand_skip * rand(0,1). Note that rand_skip should not // be larger than the number of keys in the leveldb. - optional uint32 rand_skip = 53 [default = 0]; + optional uint32 rand_skip = 7 [default = 0]; +} + +message InnerProductParameter { + optional uint32 num_output = 1; // The number of outputs for the layer + optional bool biasterm = 2 [default = true]; // whether to have bias terms + optional FillerParameter weight_filler = 3; // The filler for the weight + optional FillerParameter bias_filler = 4; // The filler for the bias +} +message ConvolutionParameter { + optional uint32 pad = 1 [default = 0]; // The padding size + optional uint32 kernelsize = 2; // The kernel size + optional uint32 group = 3 [default = 1]; // The group size for group conv + optional uint32 stride = 4 [default = 1]; // The stride +} + +message PoolParameter { + enum PoolMethod { + MAX = 0; + AVE = 1; + STOCHASTIC = 2; + } + optional PoolMethod pool = 1 [default = MAX]; // The pooling method +} + +message DropoutParameter { + optional float dropout_ratio = 1 [default = 0.5]; // dropout ratio +} + +message LRNParameter { + optional uint32 local_size = 1 [default = 5]; // for local response norm + optional float alpha = 2 [default = 1.]; // for local response norm + optional float beta = 3 [default = 0.75]; // for local response norm +} + +message ConcatParameter { + // Concat Layer needs to specify the dimension along the concat will happen, + // the other dimensions must be the same for all the bottom blobs + // By default it will concatenate blobs along channels dimension + optional uint32 concat_dim = 1 [default = 1]; +} + +message WindowDataParameter { // Fields related to detection (det_*) // foreground (object) overlap threshold optional float det_fg_threshold = 54 [default = 0.5]; @@ -110,23 +139,6 @@ message LayerParameter { // warp: cropped window is warped to a fixed size and aspect ratio // square: the tightest square around the window is cropped optional string det_crop_mode = 59 [default = "warp"]; - - // For ReshapeLayer, one needs to specify the new dimensions. - optional int32 new_num = 60 [default = 0]; - optional int32 new_channels = 61 [default = 0]; - optional int32 new_height = 62 [default = 0]; - optional int32 new_width = 63 [default = 0]; - - // Whether or not ImageLayer should shuffle the list of files at every epoch. - // It will also resize images if new_height or new_width are not zero. - optional bool shuffle_images = 64 [default = false]; - - // For ConcatLayer, one needs to specify the dimension for concatenation, and - // the other dimensions must be the same for all the bottom blobs. - // By default it will concatenate blobs along the channels dimension. - optional uint32 concat_dim = 65 [default = 1]; - - optional HDF5OutputParameter hdf5_output_param = 1001; } message HDF5OutputParameter { From 9d29f7217e45824df58256bde7695addbe5c7022 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 14 Mar 2014 16:50:47 -0700 Subject: [PATCH 04/60] move LayerParameter and individual layer param messages to bottom of caffe.proto --- src/caffe/proto/caffe.proto | 118 ++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index fb76979a6b7..e1b5e6eee6a 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -38,6 +38,63 @@ message FillerParameter { optional float std = 6 [default = 1]; // the std value in gaussian filler } +message LayerConnection { + optional LayerParameter layer = 1; // the layer parameter + repeated string bottom = 2; // the name of the bottom blobs + repeated string top = 3; // the name of the top blobs +} + +message NetParameter { + optional string name = 1; // consider giving the network a name + repeated LayerConnection layers = 2; // a bunch of layers. + // The input blobs to the network. + repeated string input = 3; + // The dim of the input blobs. For each input blob there should be four + // values specifying the num, channels, height and width of the input blob. + // Thus, there should be a total of (4 * #input) numbers. + repeated int32 input_dim = 4; + // Whether the network will force every layer to carry out backward operation. + // If set False, then whether to carry out backward is determined + // automatically according to the net structure and learning rates. + optional bool force_backward = 5 [default = false]; +} + +message SolverParameter { + optional string train_net = 1; // The proto file for the training net. + optional string test_net = 2; // The proto file for the testing net. + // The number of iterations for each testing phase. + optional int32 test_iter = 3 [default = 0]; + // The number of iterations between two testing phases. + optional int32 test_interval = 4 [default = 0]; + optional float base_lr = 5; // The base learning rate + // the number of iterations between displaying info. If display = 0, no info + // will be displayed. + optional int32 display = 6; + optional int32 max_iter = 7; // the maximum number of iterations + optional string lr_policy = 8; // The learning rate decay policy. + optional float gamma = 9; // The parameter to compute the learning rate. + optional float power = 10; // The parameter to compute the learning rate. + optional float momentum = 11; // The momentum value. + optional float weight_decay = 12; // The weight decay. + optional int32 stepsize = 13; // the stepsize for learning rate policy "step" + optional int32 snapshot = 14 [default = 0]; // The snapshot interval + optional string snapshot_prefix = 15; // The prefix for the snapshot. + // whether to snapshot diff in the results or not. Snapshotting diff will help + // debugging but the final protocol buffer size will be much larger. + optional bool snapshot_diff = 16 [default = false]; + // the mode solver will use: 0 for CPU and 1 for GPU. Use GPU in default. + optional int32 solver_mode = 17 [default = 1]; + // the device_id will that be used in GPU mode. Use device_id = 0 in default. + optional int32 device_id = 18 [default = 0]; +} + +// A message that stores the solver snapshots +message SolverState { + optional int32 iter = 1; // The current iteration + optional string learned_net = 2; // The file that stores the learned net. + repeated BlobProto history = 3; // The history for sgd solvers +} + message LayerParameter { optional string name = 1; // the layer name optional string type = 2; // the string to specify the layer type @@ -68,9 +125,9 @@ message DataParameter { optional float scale = 2 [default = 1]; optional string meanfile = 3; // Specify the batch size. - optional uint32 batchsize = 4; + optional uint32 batch_size = 4; // Specify if we would like to randomly crop an image. - optional uint32 cropsize = 5 [default = 0]; + optional uint32 crop_size = 5 [default = 0]; // Specify if we want to randomly mirror data. optional bool mirror = 6 [default = false]; // The rand_skip variable is for the data layer to skip a few data points @@ -144,60 +201,3 @@ message WindowDataParameter { message HDF5OutputParameter { optional string file_name = 1; } - -message LayerConnection { - optional LayerParameter layer = 1; // the layer parameter - repeated string bottom = 2; // the name of the bottom blobs - repeated string top = 3; // the name of the top blobs -} - -message NetParameter { - optional string name = 1; // consider giving the network a name - repeated LayerConnection layers = 2; // a bunch of layers. - // The input blobs to the network. - repeated string input = 3; - // The dim of the input blobs. For each input blob there should be four - // values specifying the num, channels, height and width of the input blob. - // Thus, there should be a total of (4 * #input) numbers. - repeated int32 input_dim = 4; - // Whether the network will force every layer to carry out backward operation. - // If set False, then whether to carry out backward is determined - // automatically according to the net structure and learning rates. - optional bool force_backward = 5 [default = false]; -} - -message SolverParameter { - optional string train_net = 1; // The proto file for the training net. - optional string test_net = 2; // The proto file for the testing net. - // The number of iterations for each testing phase. - optional int32 test_iter = 3 [default = 0]; - // The number of iterations between two testing phases. - optional int32 test_interval = 4 [default = 0]; - optional float base_lr = 5; // The base learning rate - // the number of iterations between displaying info. If display = 0, no info - // will be displayed. - optional int32 display = 6; - optional int32 max_iter = 7; // the maximum number of iterations - optional string lr_policy = 8; // The learning rate decay policy. - optional float gamma = 9; // The parameter to compute the learning rate. - optional float power = 10; // The parameter to compute the learning rate. - optional float momentum = 11; // The momentum value. - optional float weight_decay = 12; // The weight decay. - optional int32 stepsize = 13; // the stepsize for learning rate policy "step" - optional int32 snapshot = 14 [default = 0]; // The snapshot interval - optional string snapshot_prefix = 15; // The prefix for the snapshot. - // whether to snapshot diff in the results or not. Snapshotting diff will help - // debugging but the final protocol buffer size will be much larger. - optional bool snapshot_diff = 16 [default = false]; - // the mode solver will use: 0 for CPU and 1 for GPU. Use GPU in default. - optional int32 solver_mode = 17 [default = 1]; - // the device_id will that be used in GPU mode. Use device_id = 0 in default. - optional int32 device_id = 18 [default = 0]; -} - -// A message that stores the solver snapshots -message SolverState { - optional int32 iter = 1; // The current iteration - optional string learned_net = 2; // The file that stores the learned net. - repeated BlobProto history = 3; // The history for sgd solvers -} From 0fe0e156e5ccf479e7b0664397d3d5f04a453d06 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 14 Mar 2014 16:55:09 -0700 Subject: [PATCH 05/60] remove LayerConnection from proto, bottom and top now in LayerParameter --- src/caffe/proto/caffe.proto | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index e1b5e6eee6a..5d504151dd8 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -38,15 +38,9 @@ message FillerParameter { optional float std = 6 [default = 1]; // the std value in gaussian filler } -message LayerConnection { - optional LayerParameter layer = 1; // the layer parameter - repeated string bottom = 2; // the name of the bottom blobs - repeated string top = 3; // the name of the top blobs -} - message NetParameter { optional string name = 1; // consider giving the network a name - repeated LayerConnection layers = 2; // a bunch of layers. + repeated LayerParameter layers = 2; // a bunch of layers. // The input blobs to the network. repeated string input = 3; // The dim of the input blobs. For each input blob there should be four @@ -98,22 +92,24 @@ message SolverState { message LayerParameter { optional string name = 1; // the layer name optional string type = 2; // the string to specify the layer type + repeated string bottom = 3; // the name of the bottom blobs + repeated string top = 4; // the name of the top blobs // The blobs containing the numeric parameters of the layer - repeated BlobProto blobs = 3; + repeated BlobProto blobs = 5; // The ratio that is multiplied on the global learning rate. If you want to // set the learning ratio for one blob, you need to set it for all blobs. - repeated float blobs_lr = 4; + repeated float blobs_lr = 6; // The weight decay that is multiplied on the global weight decay. - repeated float weight_decay = 5; + repeated float weight_decay = 7; // Parameters for particular layer types. - optional DataParameter data_param = 6; - optional InnerProductParameter inner_product_param = 7; - optional ConvolutionParameter convolution_param = 8; - optional PoolParameter pool_param = 9; - optional DropoutParameter dropout_param = 10; - optional LRNParameter lrn_param = 11; + optional DataParameter data_param = 8; + optional InnerProductParameter inner_product_param = 9; + optional ConvolutionParameter convolution_param = 10; + optional PoolParameter pool_param = 11; + optional DropoutParameter dropout_param = 12; + optional LRNParameter lrn_param = 13; } message DataParameter { From f39fa7249c48db0d9b2b6027d794c797d46ab3ba Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 14 Mar 2014 17:13:59 -0700 Subject: [PATCH 06/60] NetParameter.layers -> layer --- src/caffe/proto/caffe.proto | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 5d504151dd8..c2775f8c810 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -40,7 +40,7 @@ message FillerParameter { message NetParameter { optional string name = 1; // consider giving the network a name - repeated LayerParameter layers = 2; // a bunch of layers. + repeated LayerParameter layer = 2; // a bunch of layers. // The input blobs to the network. repeated string input = 3; // The dim of the input blobs. For each input blob there should be four From e05d0abbd7a3f53bf1e35eb92556258729c4d884 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 14 Mar 2014 17:30:42 -0700 Subject: [PATCH 07/60] HDF5DataParameter message and concat_param --- src/caffe/proto/caffe.proto | 59 +++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index c2775f8c810..686cc08e2b2 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -105,13 +105,16 @@ message LayerParameter { // Parameters for particular layer types. optional DataParameter data_param = 8; - optional InnerProductParameter inner_product_param = 9; - optional ConvolutionParameter convolution_param = 10; - optional PoolParameter pool_param = 11; - optional DropoutParameter dropout_param = 12; - optional LRNParameter lrn_param = 13; + optional HDF5DataParameter hdf5_data_param = 9; + optional InnerProductParameter inner_product_param = 10; + optional ConvolutionParameter convolution_param = 11; + optional PoolingParameter pooling_param = 12; + optional DropoutParameter dropout_param = 13; + optional LRNParameter lrn_param = 14; + optional ConcatParameter concat_param = 15; } +// Message that stores parameters used by DataLayer message DataParameter { // Specify the data source. optional string source = 1; @@ -133,6 +136,15 @@ message DataParameter { optional uint32 rand_skip = 7 [default = 0]; } +// Message that stores parameters used by HDF5DataLayer +message HDF5DataParameter { + // Specify the data source. + optional string source = 1; + // Specify the batch size. + optional uint32 batch_size = 2; +} + +// Message that stores parameters used by InnerProductLayer message InnerProductParameter { optional uint32 num_output = 1; // The number of outputs for the layer optional bool biasterm = 2 [default = true]; // whether to have bias terms @@ -140,6 +152,7 @@ message InnerProductParameter { optional FillerParameter bias_filler = 4; // The filler for the bias } +// Message that stores parameters used by ConvolutionLayer message ConvolutionParameter { optional uint32 pad = 1 [default = 0]; // The padding size optional uint32 kernelsize = 2; // The kernel size @@ -147,7 +160,8 @@ message ConvolutionParameter { optional uint32 stride = 4 [default = 1]; // The stride } -message PoolParameter { +// Message that stores parameters used by PoolingLayer +message PoolingParameter { enum PoolMethod { MAX = 0; AVE = 1; @@ -156,23 +170,18 @@ message PoolParameter { optional PoolMethod pool = 1 [default = MAX]; // The pooling method } +// Message that stores parameters used by DropoutLayer message DropoutParameter { optional float dropout_ratio = 1 [default = 0.5]; // dropout ratio } +// Message that stores parameters used by LRNLayer message LRNParameter { optional uint32 local_size = 1 [default = 5]; // for local response norm optional float alpha = 2 [default = 1.]; // for local response norm optional float beta = 3 [default = 0.75]; // for local response norm } -message ConcatParameter { - // Concat Layer needs to specify the dimension along the concat will happen, - // the other dimensions must be the same for all the bottom blobs - // By default it will concatenate blobs along channels dimension - optional uint32 concat_dim = 1 [default = 1]; -} - message WindowDataParameter { // Fields related to detection (det_*) // foreground (object) overlap threshold @@ -197,3 +206,27 @@ message WindowDataParameter { message HDF5OutputParameter { optional string file_name = 1; } + +// Message that stores parameters used by ConcatLayer +message ConcatParameter { + // For ConcatLayer, one needs to specify the dimension for concatenation, and + // the other dimensions must be the same for all the bottom blobs. + // By default it will concatenate blobs along the channels dimension. + optional uint32 concat_dim = 1 [default = 1]; +} + + +// Message that stores parameters used by ReshapeLayer +message ReshapeParameter { + // For ReshapeLayer, one needs to specify the new dimensions. + optional int32 new_num = 1 [default = 0]; + optional int32 new_channels = 2 [default = 0]; + optional int32 new_height = 3 [default = 0]; + optional int32 new_width = 4 [default = 0]; +} + +message ImageDataParameter { + // Whether or not ImageLayer should shuffle the list of files at every epoch. + // It will also resize images if new_height or new_width are not zero. + optional bool shuffle_images = 64 [default = false]; +} From 195d46a661f345197a1ea4837e104af58fdedd1e Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 14 Mar 2014 17:57:26 -0700 Subject: [PATCH 08/60] add duplicated params from InnerProductParam to ConvolutionParam and PoolingParam etc, create InfogainLossParam --- src/caffe/proto/caffe.proto | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 686cc08e2b2..b6cf14451c2 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -112,6 +112,7 @@ message LayerParameter { optional DropoutParameter dropout_param = 13; optional LRNParameter lrn_param = 14; optional ConcatParameter concat_param = 15; + optional InfogainLossParameter infogain_loss_param = 16; } // Message that stores parameters used by DataLayer @@ -147,17 +148,21 @@ message HDF5DataParameter { // Message that stores parameters used by InnerProductLayer message InnerProductParameter { optional uint32 num_output = 1; // The number of outputs for the layer - optional bool biasterm = 2 [default = true]; // whether to have bias terms + optional bool bias_term = 2 [default = true]; // whether to have bias terms optional FillerParameter weight_filler = 3; // The filler for the weight optional FillerParameter bias_filler = 4; // The filler for the bias } // Message that stores parameters used by ConvolutionLayer message ConvolutionParameter { - optional uint32 pad = 1 [default = 0]; // The padding size - optional uint32 kernelsize = 2; // The kernel size - optional uint32 group = 3 [default = 1]; // The group size for group conv - optional uint32 stride = 4 [default = 1]; // The stride + optional uint32 num_output = 1; // The number of outputs for the layer + optional bool bias_term = 2 [default = true]; // whether to have bias terms + optional uint32 pad = 3 [default = 0]; // The padding size + optional uint32 kernel_size = 4; // The kernel size + optional uint32 group = 5 [default = 1]; // The group size for group conv + optional uint32 stride = 6 [default = 1]; // The stride + optional FillerParameter weight_filler = 7; // The filler for the weight + optional FillerParameter bias_filler = 8; // The filler for the bias } // Message that stores parameters used by PoolingLayer @@ -168,6 +173,8 @@ message PoolingParameter { STOCHASTIC = 2; } optional PoolMethod pool = 1 [default = MAX]; // The pooling method + optional uint32 kernel_size = 2; // The kernel size + optional uint32 stride = 3 [default = 1]; // The stride } // Message that stores parameters used by DropoutLayer @@ -215,7 +222,6 @@ message ConcatParameter { optional uint32 concat_dim = 1 [default = 1]; } - // Message that stores parameters used by ReshapeLayer message ReshapeParameter { // For ReshapeLayer, one needs to specify the new dimensions. @@ -225,8 +231,15 @@ message ReshapeParameter { optional int32 new_width = 4 [default = 0]; } +// Message that stores parameters used by ImageDataLayer message ImageDataParameter { // Whether or not ImageLayer should shuffle the list of files at every epoch. // It will also resize images if new_height or new_width are not zero. optional bool shuffle_images = 64 [default = false]; } + +// Message that stores parameters InfogainLossLayer +message InfogainLossParameter { + // Specify the infogain matrix source. + optional string source = 1; +} From ecb38730b46aa93622544699297f1f43f7ed59d2 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 14 Mar 2014 17:59:28 -0700 Subject: [PATCH 09/60] changes to layers etc to make 'make all' run successfully under new caffe.proto --- include/caffe/util/insert_splits.hpp | 2 +- include/caffe/vision_layers.hpp | 4 +- src/caffe/layers/concat_layer.cpp | 2 +- src/caffe/layers/conv_layer.cpp | 30 ++++---- src/caffe/layers/conv_layer.cu | 4 +- src/caffe/layers/data_layer.cpp | 91 +++++++++++++----------- src/caffe/layers/dropout_layer.cpp | 2 +- src/caffe/layers/hdf5_data_layer.cpp | 20 +++--- src/caffe/layers/hdf5_data_layer.cu | 4 +- src/caffe/layers/im2col_layer.cpp | 6 +- src/caffe/layers/inner_product_layer.cpp | 22 +++--- src/caffe/layers/inner_product_layer.cu | 4 +- src/caffe/layers/loss_layer.cpp | 3 +- src/caffe/layers/lrn_layer.cpp | 6 +- src/caffe/layers/pooling_layer.cpp | 23 +++--- src/caffe/layers/pooling_layer.cu | 16 ++--- src/caffe/net.cpp | 40 +++++------ src/caffe/util/insert_splits.cpp | 49 +++++++------ 18 files changed, 167 insertions(+), 161 deletions(-) diff --git a/include/caffe/util/insert_splits.hpp b/include/caffe/util/insert_splits.hpp index af824e63553..4f4ddc4f293 100644 --- a/include/caffe/util/insert_splits.hpp +++ b/include/caffe/util/insert_splits.hpp @@ -18,7 +18,7 @@ void insert_splits(const NetParameter& param, NetParameter* param_split); void configure_split_layer(const string& layer_name, const string& blob_name, const int blob_idx, const int split_count, - LayerConnection* split_layer_connection); + LayerParameter* split_layer_param); string get_split_layer_name(const string& layer_name, const string& blob_name, const int blob_idx); diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp index a8305abbd6e..5807938408a 100644 --- a/include/caffe/vision_layers.hpp +++ b/include/caffe/vision_layers.hpp @@ -197,7 +197,7 @@ class InnerProductLayer : public Layer { int M_; int K_; int N_; - bool biasterm_; + bool bias_term_; shared_ptr bias_multiplier_; }; @@ -342,7 +342,7 @@ class ConvolutionLayer : public Layer { int GROUP_; Blob col_buffer_; shared_ptr bias_multiplier_; - bool biasterm_; + bool bias_term_; int M_; int K_; int N_; diff --git a/src/caffe/layers/concat_layer.cpp b/src/caffe/layers/concat_layer.cpp index 2ce863b108f..8aded1bf764 100644 --- a/src/caffe/layers/concat_layer.cpp +++ b/src/caffe/layers/concat_layer.cpp @@ -15,7 +15,7 @@ void ConcatLayer::SetUp(const vector*>& bottom, "Concat Layer takes at least two blobs as input."; CHECK_EQ(top->size(), 1) << "Concat Layer takes a single blob as output."; - concat_dim_ = this->layer_param_.concat_dim(); + concat_dim_ = this->layer_param_.concat_param().concat_dim(); CHECK_GE(concat_dim_, 0) << "concat_dim should be >= 0"; CHECK_LE(concat_dim_, 1) << "For now concat_dim <=1, it can only concat num and channels"; diff --git a/src/caffe/layers/conv_layer.cpp b/src/caffe/layers/conv_layer.cpp index c3cacebc03e..b4948ca68c9 100644 --- a/src/caffe/layers/conv_layer.cpp +++ b/src/caffe/layers/conv_layer.cpp @@ -15,15 +15,15 @@ void ConvolutionLayer::SetUp(const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom.size(), 1) << "Conv Layer takes a single blob as input."; CHECK_EQ(top->size(), 1) << "Conv Layer takes a single blob as output."; - KSIZE_ = this->layer_param_.kernelsize(); - STRIDE_ = this->layer_param_.stride(); - GROUP_ = this->layer_param_.group(); - PAD_ = this->layer_param_.pad(); + KSIZE_ = this->layer_param_.convolution_param().kernel_size(); + STRIDE_ = this->layer_param_.convolution_param().stride(); + GROUP_ = this->layer_param_.convolution_param().group(); + PAD_ = this->layer_param_.convolution_param().pad(); NUM_ = bottom[0]->num(); CHANNELS_ = bottom[0]->channels(); HEIGHT_ = bottom[0]->height(); WIDTH_ = bottom[0]->width(); - NUM_OUTPUT_ = this->layer_param_.num_output(); + NUM_OUTPUT_ = this->layer_param_.convolution_param().num_output(); CHECK_GT(NUM_OUTPUT_, 0); CHECK_EQ(CHANNELS_ % GROUP_, 0); // The im2col result buffer would only hold one image at a time to avoid @@ -34,7 +34,7 @@ void ConvolutionLayer::SetUp(const vector*>& bottom, // Set the parameters CHECK_EQ(NUM_OUTPUT_ % GROUP_, 0) << "Number of output should be multiples of group."; - biasterm_ = this->layer_param_.biasterm(); + bias_term_ = this->layer_param_.convolution_param().bias_term(); // Figure out the dimensions for individual gemms. M_ = NUM_OUTPUT_ / GROUP_; K_ = CHANNELS_ * KSIZE_ * KSIZE_ / GROUP_; @@ -44,7 +44,7 @@ void ConvolutionLayer::SetUp(const vector*>& bottom, if (this->blobs_.size() > 0) { LOG(INFO) << "Skipping parameter initialization"; } else { - if (biasterm_) { + if (bias_term_) { this->blobs_.resize(2); } else { this->blobs_.resize(1); @@ -53,19 +53,19 @@ void ConvolutionLayer::SetUp(const vector*>& bottom, this->blobs_[0].reset( new Blob(NUM_OUTPUT_, CHANNELS_ / GROUP_, KSIZE_, KSIZE_)); // fill the weights - shared_ptr > weight_filler( - GetFiller(this->layer_param_.weight_filler())); + shared_ptr > weight_filler(GetFiller( + this->layer_param_.convolution_param().weight_filler())); weight_filler->Fill(this->blobs_[0].get()); // If necessary, intiialize and fill the bias term - if (biasterm_) { + if (bias_term_) { this->blobs_[1].reset(new Blob(1, 1, 1, NUM_OUTPUT_)); - shared_ptr > bias_filler( - GetFiller(this->layer_param_.bias_filler())); + shared_ptr > bias_filler(GetFiller( + this->layer_param_.convolution_param().bias_filler())); bias_filler->Fill(this->blobs_[1].get()); } } // Set up the bias filler - if (biasterm_) { + if (bias_term_) { bias_multiplier_.reset(new SyncedMemory(N_ * sizeof(Dtype))); Dtype* bias_multiplier_data = reinterpret_cast(bias_multiplier_->mutable_cpu_data()); @@ -97,7 +97,7 @@ Dtype ConvolutionLayer::Forward_cpu(const vector*>& bottom, (Dtype)0., top_data + (*top)[0]->offset(n) + top_offset * g); } // third, add bias - if (biasterm_) { + if (bias_term_) { caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, NUM_OUTPUT_, N_, 1, (Dtype)1., this->blobs_[1]->cpu_data(), reinterpret_cast(bias_multiplier_->cpu_data()), @@ -120,7 +120,7 @@ void ConvolutionLayer::Backward_cpu(const vector*>& top, // bias gradient if necessary Dtype* bias_diff = NULL; - if (biasterm_) { + if (bias_term_) { bias_diff = this->blobs_[1]->mutable_cpu_diff(); memset(bias_diff, 0, sizeof(Dtype) * this->blobs_[1]->count()); for (int n = 0; n < NUM_; ++n) { diff --git a/src/caffe/layers/conv_layer.cu b/src/caffe/layers/conv_layer.cu index da76b4dfb6b..28b3d94185f 100644 --- a/src/caffe/layers/conv_layer.cu +++ b/src/caffe/layers/conv_layer.cu @@ -31,7 +31,7 @@ Dtype ConvolutionLayer::Forward_gpu(const vector*>& bottom, (Dtype)0., top_data + (*top)[0]->offset(n) + top_offset * g); } // third, add bias - if (biasterm_) { + if (bias_term_) { caffe_gpu_gemm(CblasNoTrans, CblasNoTrans, NUM_OUTPUT_, N_, 1, (Dtype)1., this->blobs_[1]->gpu_data(), reinterpret_cast(bias_multiplier_->gpu_data()), @@ -54,7 +54,7 @@ void ConvolutionLayer::Backward_gpu(const vector*>& top, // bias gradient if necessary Dtype* bias_diff = NULL; - if (biasterm_) { + if (bias_term_) { bias_diff = this->blobs_[1]->mutable_gpu_diff(); CUDA_CHECK(cudaMemset(bias_diff, 0, sizeof(Dtype) * this->blobs_[1]->count())); diff --git a/src/caffe/layers/data_layer.cpp b/src/caffe/layers/data_layer.cpp index 1ab28c6dc62..7178214f9eb 100644 --- a/src/caffe/layers/data_layer.cpp +++ b/src/caffe/layers/data_layer.cpp @@ -24,13 +24,13 @@ void* DataLayerPrefetch(void* layer_pointer) { CHECK(layer->prefetch_data_); Dtype* top_data = layer->prefetch_data_->mutable_cpu_data(); Dtype* top_label = layer->prefetch_label_->mutable_cpu_data(); - const Dtype scale = layer->layer_param_.scale(); - const int batchsize = layer->layer_param_.batchsize(); - const int cropsize = layer->layer_param_.cropsize(); - const bool mirror = layer->layer_param_.mirror(); + const Dtype scale = layer->layer_param_.data_param().scale(); + const int batch_size = layer->layer_param_.data_param().batch_size(); + const int crop_size = layer->layer_param_.data_param().crop_size(); + const bool mirror = layer->layer_param_.data_param().mirror(); - if (mirror && cropsize == 0) { - LOG(FATAL) << "Current implementation requires mirror and cropsize to be " + if (mirror && crop_size == 0) { + LOG(FATAL) << "Current implementation requires mirror and crop_size to be " << "set at the same time."; } // datum scales @@ -39,33 +39,33 @@ void* DataLayerPrefetch(void* layer_pointer) { const int width = layer->datum_width_; const int size = layer->datum_size_; const Dtype* mean = layer->data_mean_.cpu_data(); - for (int itemid = 0; itemid < batchsize; ++itemid) { + for (int item_id = 0; item_id < batch_size; ++item_id) { // get a blob CHECK(layer->iter_); CHECK(layer->iter_->Valid()); datum.ParseFromString(layer->iter_->value().ToString()); const string& data = datum.data(); - if (cropsize) { + if (crop_size) { CHECK(data.size()) << "Image cropping only support uint8 data"; int h_off, w_off; // We only do random crop when we do training. if (Caffe::phase() == Caffe::TRAIN) { // NOLINT_NEXT_LINE(runtime/threadsafe_fn) - h_off = rand() % (height - cropsize); + h_off = rand() % (height - crop_size); // NOLINT_NEXT_LINE(runtime/threadsafe_fn) - w_off = rand() % (width - cropsize); + w_off = rand() % (width - crop_size); } else { - h_off = (height - cropsize) / 2; - w_off = (width - cropsize) / 2; + h_off = (height - crop_size) / 2; + w_off = (width - crop_size) / 2; } // NOLINT_NEXT_LINE(runtime/threadsafe_fn) if (mirror && rand() % 2) { // Copy mirrored version for (int c = 0; c < channels; ++c) { - for (int h = 0; h < cropsize; ++h) { - for (int w = 0; w < cropsize; ++w) { - top_data[((itemid * channels + c) * cropsize + h) * cropsize - + cropsize - 1 - w] = + for (int h = 0; h < crop_size; ++h) { + for (int w = 0; w < crop_size; ++w) { + top_data[((item_id * channels + c) * crop_size + h) * crop_size + + crop_size - 1 - w] = (static_cast( (uint8_t)data[(c * height + h + h_off) * width + w + w_off]) @@ -77,9 +77,10 @@ void* DataLayerPrefetch(void* layer_pointer) { } else { // Normal copy for (int c = 0; c < channels; ++c) { - for (int h = 0; h < cropsize; ++h) { - for (int w = 0; w < cropsize; ++w) { - top_data[((itemid * channels + c) * cropsize + h) * cropsize + w] + for (int h = 0; h < crop_size; ++h) { + for (int w = 0; w < crop_size; ++w) { + top_data[((item_id * channels + c) * crop_size + h) * crop_size + + w] = (static_cast( (uint8_t)data[(c * height + h + h_off) * width + w + w_off]) @@ -93,18 +94,18 @@ void* DataLayerPrefetch(void* layer_pointer) { // we will prefer to use data() first, and then try float_data() if (data.size()) { for (int j = 0; j < size; ++j) { - top_data[itemid * size + j] = + top_data[item_id * size + j] = (static_cast((uint8_t)data[j]) - mean[j]) * scale; } } else { for (int j = 0; j < size; ++j) { - top_data[itemid * size + j] = + top_data[item_id * size + j] = (datum.float_data(j) - mean[j]) * scale; } } } - top_label[itemid] = datum.label(); + top_label[item_id] = datum.label(); // go to the next iter layer->iter_->Next(); if (!layer->iter_->Valid()) { @@ -133,18 +134,19 @@ void DataLayer::SetUp(const vector*>& bottom, leveldb::Options options; options.create_if_missing = false; options.max_open_files = 100; - LOG(INFO) << "Opening leveldb " << this->layer_param_.source(); + LOG(INFO) << "Opening leveldb " << this->layer_param_.data_param().source(); leveldb::Status status = leveldb::DB::Open( - options, this->layer_param_.source(), &db_temp); + options, this->layer_param_.data_param().source(), &db_temp); CHECK(status.ok()) << "Failed to open leveldb " - << this->layer_param_.source() << std::endl << status.ToString(); + << this->layer_param_.data_param().source() << std::endl + << status.ToString(); db_.reset(db_temp); iter_.reset(db_->NewIterator(leveldb::ReadOptions())); iter_->SeekToFirst(); // Check if we would need to randomly skip a few data points - if (this->layer_param_.rand_skip()) { + if (this->layer_param_.data_param().rand_skip()) { // NOLINT_NEXT_LINE(runtime/threadsafe_fn) - unsigned int skip = rand() % this->layer_param_.rand_skip(); + unsigned int skip = rand() % this->layer_param_.data_param().rand_skip(); LOG(INFO) << "Skipping first " << skip << " data points."; while (skip-- > 0) { iter_->Next(); @@ -157,39 +159,42 @@ void DataLayer::SetUp(const vector*>& bottom, Datum datum; datum.ParseFromString(iter_->value().ToString()); // image - int cropsize = this->layer_param_.cropsize(); - if (cropsize > 0) { - (*top)[0]->Reshape( - this->layer_param_.batchsize(), datum.channels(), cropsize, cropsize); + int crop_size = this->layer_param_.data_param().crop_size(); + if (crop_size > 0) { + (*top)[0]->Reshape(this->layer_param_.data_param().batch_size(), + datum.channels(), crop_size, crop_size); prefetch_data_.reset(new Blob( - this->layer_param_.batchsize(), datum.channels(), cropsize, cropsize)); + this->layer_param_.data_param().batch_size(), datum.channels(), + crop_size, crop_size)); } else { (*top)[0]->Reshape( - this->layer_param_.batchsize(), datum.channels(), datum.height(), - datum.width()); + this->layer_param_.data_param().batch_size(), datum.channels(), + datum.height(), datum.width()); prefetch_data_.reset(new Blob( - this->layer_param_.batchsize(), datum.channels(), datum.height(), - datum.width())); + this->layer_param_.data_param().batch_size(), datum.channels(), + datum.height(), datum.width())); } LOG(INFO) << "output data size: " << (*top)[0]->num() << "," << (*top)[0]->channels() << "," << (*top)[0]->height() << "," << (*top)[0]->width(); // label - (*top)[1]->Reshape(this->layer_param_.batchsize(), 1, 1, 1); + (*top)[1]->Reshape(this->layer_param_.data_param().batch_size(), 1, 1, 1); prefetch_label_.reset( - new Blob(this->layer_param_.batchsize(), 1, 1, 1)); + new Blob(this->layer_param_.data_param().batch_size(), 1, 1, 1)); // datum size datum_channels_ = datum.channels(); datum_height_ = datum.height(); datum_width_ = datum.width(); datum_size_ = datum.channels() * datum.height() * datum.width(); - CHECK_GT(datum_height_, cropsize); - CHECK_GT(datum_width_, cropsize); + CHECK_GT(datum_height_, crop_size); + CHECK_GT(datum_width_, crop_size); // check if we want to have mean - if (this->layer_param_.has_meanfile()) { + if (this->layer_param_.data_param().has_meanfile()) { BlobProto blob_proto; - LOG(INFO) << "Loading mean file from" << this->layer_param_.meanfile(); - ReadProtoFromBinaryFile(this->layer_param_.meanfile().c_str(), &blob_proto); + LOG(INFO) << "Loading mean file from" + << this->layer_param_.data_param().meanfile(); + ReadProtoFromBinaryFile(this->layer_param_.data_param().meanfile().c_str(), + &blob_proto); data_mean_.FromProto(blob_proto); CHECK_EQ(data_mean_.num(), 1); CHECK_EQ(data_mean_.channels(), datum_channels_); diff --git a/src/caffe/layers/dropout_layer.cpp b/src/caffe/layers/dropout_layer.cpp index 78acf3a3c4e..5dbba5d550c 100644 --- a/src/caffe/layers/dropout_layer.cpp +++ b/src/caffe/layers/dropout_layer.cpp @@ -16,7 +16,7 @@ void DropoutLayer::SetUp(const vector*>& bottom, NeuronLayer::SetUp(bottom, top); // Set up the cache for random number generation rand_vec_.reset(new SyncedMemory(bottom[0]->count() * sizeof(int))); - threshold_ = this->layer_param_.dropout_ratio(); + threshold_ = this->layer_param_.dropout_param().dropout_ratio(); DCHECK(threshold_ > 0.); DCHECK(threshold_ < 1.); scale_ = 1. / (1. - threshold_); diff --git a/src/caffe/layers/hdf5_data_layer.cpp b/src/caffe/layers/hdf5_data_layer.cpp index 03baf5f5377..dc996883dde 100644 --- a/src/caffe/layers/hdf5_data_layer.cpp +++ b/src/caffe/layers/hdf5_data_layer.cpp @@ -56,16 +56,17 @@ void HDF5DataLayer::SetUp(const vector*>& bottom, CHECK_EQ(top->size(), 2) << "HDF5DataLayer takes two blobs as output."; // Read the source to parse the filenames. - LOG(INFO) << "Loading filename from " << this->layer_param_.source(); + const string& source = this->layer_param_.hdf5_data_param().source(); + LOG(INFO) << "Loading filename from " << source; hdf_filenames_.clear(); - std::ifstream myfile(this->layer_param_.source().c_str()); - if (myfile.is_open()) { + std::ifstream source_file(source.c_str()); + if (source_file.is_open()) { std::string line; - while (myfile >> line) { + while (source_file >> line) { hdf_filenames_.push_back(line); } } - myfile.close(); + source_file.close(); num_files_ = hdf_filenames_.size(); current_file_ = 0; LOG(INFO) << "Number of files: " << num_files_; @@ -75,9 +76,10 @@ void HDF5DataLayer::SetUp(const vector*>& bottom, current_row_ = 0; // Reshape blobs. - (*top)[0]->Reshape(this->layer_param_.batchsize(), data_blob_.channels(), + const int batch_size = this->layer_param_.hdf5_data_param().batch_size(); + (*top)[0]->Reshape(batch_size, data_blob_.channels(), data_blob_.width(), data_blob_.height()); - (*top)[1]->Reshape(this->layer_param_.batchsize(), label_blob_.channels(), + (*top)[1]->Reshape(batch_size, label_blob_.channels(), label_blob_.width(), label_blob_.height()); LOG(INFO) << "output data size: " << (*top)[0]->num() << "," << (*top)[0]->channels() << "," << (*top)[0]->height() << "," @@ -87,11 +89,11 @@ void HDF5DataLayer::SetUp(const vector*>& bottom, template Dtype HDF5DataLayer::Forward_cpu(const vector*>& bottom, vector*>* top) { - const int batchsize = this->layer_param_.batchsize(); + const int batch_size = this->layer_param_.hdf5_data_param().batch_size(); const int data_count = (*top)[0]->count() / (*top)[0]->num(); const int label_data_count = (*top)[1]->count() / (*top)[1]->num(); - for (int i = 0; i < batchsize; ++i, ++current_row_) { + for (int i = 0; i < batch_size; ++i, ++current_row_) { if (current_row_ == data_blob_.num()) { if (num_files_ > 1) { current_file_ += 1; diff --git a/src/caffe/layers/hdf5_data_layer.cu b/src/caffe/layers/hdf5_data_layer.cu index 2f743ef16d5..4a0733aac07 100644 --- a/src/caffe/layers/hdf5_data_layer.cu +++ b/src/caffe/layers/hdf5_data_layer.cu @@ -22,11 +22,11 @@ namespace caffe { template Dtype HDF5DataLayer::Forward_gpu(const vector*>& bottom, vector*>* top) { - const int batchsize = this->layer_param_.batchsize(); + const int batch_size = this->layer_param_.hdf5_data_param().batch_size(); const int data_count = (*top)[0]->count() / (*top)[0]->num(); const int label_data_count = (*top)[1]->count() / (*top)[1]->num(); - for (int i = 0; i < batchsize; ++i, ++current_row_) { + for (int i = 0; i < batch_size; ++i, ++current_row_) { if (current_row_ == data_blob_.num()) { if (num_files_ > 1) { current_file_ += 1; diff --git a/src/caffe/layers/im2col_layer.cpp b/src/caffe/layers/im2col_layer.cpp index fc9f52e745f..91c22488f04 100644 --- a/src/caffe/layers/im2col_layer.cpp +++ b/src/caffe/layers/im2col_layer.cpp @@ -14,9 +14,9 @@ void Im2colLayer::SetUp(const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom.size(), 1) << "Im2col Layer takes a single blob as input."; CHECK_EQ(top->size(), 1) << "Im2col Layer takes a single blob as output."; - KSIZE_ = this->layer_param_.kernelsize(); - STRIDE_ = this->layer_param_.stride(); - PAD_ = this->layer_param_.pad(); + KSIZE_ = this->layer_param_.convolution_param().kernel_size(); + STRIDE_ = this->layer_param_.convolution_param().stride(); + PAD_ = this->layer_param_.convolution_param().pad(); CHANNELS_ = bottom[0]->channels(); HEIGHT_ = bottom[0]->height(); WIDTH_ = bottom[0]->width(); diff --git a/src/caffe/layers/inner_product_layer.cpp b/src/caffe/layers/inner_product_layer.cpp index 9b2c7ef799a..c60261e9486 100644 --- a/src/caffe/layers/inner_product_layer.cpp +++ b/src/caffe/layers/inner_product_layer.cpp @@ -16,8 +16,8 @@ void InnerProductLayer::SetUp(const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom.size(), 1) << "IP Layer takes a single blob as input."; CHECK_EQ(top->size(), 1) << "IP Layer takes a single blob as output."; - const int num_output = this->layer_param_.num_output(); - biasterm_ = this->layer_param_.biasterm(); + const int num_output = this->layer_param_.inner_product_param().num_output(); + bias_term_ = this->layer_param_.inner_product_param().bias_term(); // Figure out the dimensions M_ = bottom[0]->num(); K_ = bottom[0]->count() / bottom[0]->num(); @@ -27,7 +27,7 @@ void InnerProductLayer::SetUp(const vector*>& bottom, if (this->blobs_.size() > 0) { LOG(INFO) << "Skipping parameter initialization"; } else { - if (biasterm_) { + if (bias_term_) { this->blobs_.resize(2); } else { this->blobs_.resize(1); @@ -35,19 +35,19 @@ void InnerProductLayer::SetUp(const vector*>& bottom, // Intialize the weight this->blobs_[0].reset(new Blob(1, 1, N_, K_)); // fill the weights - shared_ptr > weight_filler( - GetFiller(this->layer_param_.weight_filler())); + shared_ptr > weight_filler(GetFiller( + this->layer_param_.inner_product_param().weight_filler())); weight_filler->Fill(this->blobs_[0].get()); // If necessary, intiialize and fill the bias term - if (biasterm_) { + if (bias_term_) { this->blobs_[1].reset(new Blob(1, 1, 1, N_)); - shared_ptr > bias_filler( - GetFiller(this->layer_param_.bias_filler())); + shared_ptr > bias_filler(GetFiller( + this->layer_param_.inner_product_param().bias_filler())); bias_filler->Fill(this->blobs_[1].get()); } } // parameter initialization // Setting up the bias multiplier - if (biasterm_) { + if (bias_term_) { bias_multiplier_.reset(new SyncedMemory(M_ * sizeof(Dtype))); Dtype* bias_multiplier_data = reinterpret_cast(bias_multiplier_->mutable_cpu_data()); @@ -65,7 +65,7 @@ Dtype InnerProductLayer::Forward_cpu(const vector*>& bottom, const Dtype* weight = this->blobs_[0]->cpu_data(); caffe_cpu_gemm(CblasNoTrans, CblasTrans, M_, N_, K_, (Dtype)1., bottom_data, weight, (Dtype)0., top_data); - if (biasterm_) { + if (bias_term_) { caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, M_, N_, 1, (Dtype)1., reinterpret_cast(bias_multiplier_->cpu_data()), this->blobs_[1]->cpu_data(), (Dtype)1., top_data); @@ -82,7 +82,7 @@ void InnerProductLayer::Backward_cpu(const vector*>& top, // Gradient with respect to weight caffe_cpu_gemm(CblasTrans, CblasNoTrans, N_, K_, M_, (Dtype)1., top_diff, bottom_data, (Dtype)0., this->blobs_[0]->mutable_cpu_diff()); - if (biasterm_) { + if (bias_term_) { // Gradient with respect to bias caffe_cpu_gemv(CblasTrans, M_, N_, (Dtype)1., top_diff, reinterpret_cast(bias_multiplier_->cpu_data()), (Dtype)0., diff --git a/src/caffe/layers/inner_product_layer.cu b/src/caffe/layers/inner_product_layer.cu index ac0e0be12d6..f139c23c310 100644 --- a/src/caffe/layers/inner_product_layer.cu +++ b/src/caffe/layers/inner_product_layer.cu @@ -21,7 +21,7 @@ Dtype InnerProductLayer::Forward_gpu(const vector*>& bottom, const Dtype* weight = this->blobs_[0]->gpu_data(); caffe_gpu_gemm(CblasNoTrans, CblasTrans, M_, N_, K_, (Dtype)1., bottom_data, weight, (Dtype)0., top_data); - if (biasterm_) { + if (bias_term_) { caffe_gpu_gemm(CblasNoTrans, CblasNoTrans, M_, N_, 1, (Dtype)1., reinterpret_cast(bias_multiplier_->gpu_data()), this->blobs_[1]->gpu_data(), (Dtype)1., top_data); @@ -38,7 +38,7 @@ void InnerProductLayer::Backward_gpu(const vector*>& top, // Gradient with respect to weight caffe_gpu_gemm(CblasTrans, CblasNoTrans, N_, K_, M_, (Dtype)1., top_diff, bottom_data, (Dtype)0., this->blobs_[0]->mutable_gpu_diff()); - if (biasterm_) { + if (bias_term_) { // Gradient with respect to bias caffe_gpu_gemv(CblasTrans, M_, N_, (Dtype)1., top_diff, reinterpret_cast(bias_multiplier_->gpu_data()), diff --git a/src/caffe/layers/loss_layer.cpp b/src/caffe/layers/loss_layer.cpp index e232008266a..2945d268ef8 100644 --- a/src/caffe/layers/loss_layer.cpp +++ b/src/caffe/layers/loss_layer.cpp @@ -73,7 +73,8 @@ void InfogainLossLayer::SetUp( CHECK_EQ(bottom[1]->height(), 1); CHECK_EQ(bottom[1]->width(), 1); BlobProto blob_proto; - ReadProtoFromBinaryFile(this->layer_param_.source(), &blob_proto); + ReadProtoFromBinaryFile(this->layer_param_.infogain_loss_param().source(), + &blob_proto); infogain_.FromProto(blob_proto); CHECK_EQ(infogain_.num(), 1); CHECK_EQ(infogain_.channels(), 1); diff --git a/src/caffe/layers/lrn_layer.cpp b/src/caffe/layers/lrn_layer.cpp index c43aaf46517..e95bc8a61c9 100644 --- a/src/caffe/layers/lrn_layer.cpp +++ b/src/caffe/layers/lrn_layer.cpp @@ -21,10 +21,10 @@ void LRNLayer::SetUp(const vector*>& bottom, width_ = bottom[0]->width(); (*top)[0]->Reshape(num_, channels_, height_, width_); scale_.Reshape(num_, channels_, height_, width_); - size_ = this->layer_param_.local_size(); + size_ = this->layer_param_.lrn_param().local_size(); pre_pad_ = (size_ - 1) / 2; - alpha_ = this->layer_param_.alpha(); - beta_ = this->layer_param_.beta(); + alpha_ = this->layer_param_.lrn_param().alpha(); + beta_ = this->layer_param_.lrn_param().beta(); } template diff --git a/src/caffe/layers/pooling_layer.cpp b/src/caffe/layers/pooling_layer.cpp index 355ea290604..995579522d1 100644 --- a/src/caffe/layers/pooling_layer.cpp +++ b/src/caffe/layers/pooling_layer.cpp @@ -18,8 +18,8 @@ void PoolingLayer::SetUp(const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom.size(), 1) << "PoolingLayer takes a single blob as input."; CHECK_EQ(top->size(), 1) << "PoolingLayer takes a single blob as output."; - KSIZE_ = this->layer_param_.kernelsize(); - STRIDE_ = this->layer_param_.stride(); + KSIZE_ = this->layer_param_.pooling_param().kernel_size(); + STRIDE_ = this->layer_param_.pooling_param().stride(); CHANNELS_ = bottom[0]->channels(); HEIGHT_ = bottom[0]->height(); WIDTH_ = bottom[0]->width(); @@ -30,7 +30,8 @@ void PoolingLayer::SetUp(const vector*>& bottom, (*top)[0]->Reshape(bottom[0]->num(), CHANNELS_, POOLED_HEIGHT_, POOLED_WIDTH_); // If stochastic pooling, we will initialize the random index part. - if (this->layer_param_.pool() == LayerParameter_PoolMethod_STOCHASTIC) { + if (this->layer_param_.pooling_param().pool() == + PoolingParameter_PoolMethod_STOCHASTIC) { rand_idx_.Reshape(bottom[0]->num(), CHANNELS_, POOLED_HEIGHT_, POOLED_WIDTH_); } @@ -46,8 +47,8 @@ Dtype PoolingLayer::Forward_cpu(const vector*>& bottom, // Different pooling methods. We explicitly do the switch outside the for // loop to save time, although this results in more codes. int top_count = (*top)[0]->count(); - switch (this->layer_param_.pool()) { - case LayerParameter_PoolMethod_MAX: + switch (this->layer_param_.pooling_param().pool()) { + case PoolingParameter_PoolMethod_MAX: // Initialize for (int i = 0; i < top_count; ++i) { top_data[i] = -FLT_MAX; @@ -76,7 +77,7 @@ Dtype PoolingLayer::Forward_cpu(const vector*>& bottom, } } break; - case LayerParameter_PoolMethod_AVE: + case PoolingParameter_PoolMethod_AVE: for (int i = 0; i < top_count; ++i) { top_data[i] = 0; } @@ -105,7 +106,7 @@ Dtype PoolingLayer::Forward_cpu(const vector*>& bottom, } } break; - case LayerParameter_PoolMethod_STOCHASTIC: + case PoolingParameter_PoolMethod_STOCHASTIC: NOT_IMPLEMENTED; break; default: @@ -127,8 +128,8 @@ void PoolingLayer::Backward_cpu(const vector*>& top, // Different pooling methods. We explicitly do the switch outside the for // loop to save time, although this results in more codes. memset(bottom_diff, 0, (*bottom)[0]->count() * sizeof(Dtype)); - switch (this->layer_param_.pool()) { - case LayerParameter_PoolMethod_MAX: + switch (this->layer_param_.pooling_param().pool()) { + case PoolingParameter_PoolMethod_MAX: // The main loop for (int n = 0; n < top[0]->num(); ++n) { for (int c = 0; c < CHANNELS_; ++c) { @@ -156,7 +157,7 @@ void PoolingLayer::Backward_cpu(const vector*>& top, } } break; - case LayerParameter_PoolMethod_AVE: + case PoolingParameter_PoolMethod_AVE: // The main loop for (int n = 0; n < top[0]->num(); ++n) { for (int c = 0; c < CHANNELS_; ++c) { @@ -183,7 +184,7 @@ void PoolingLayer::Backward_cpu(const vector*>& top, } } break; - case LayerParameter_PoolMethod_STOCHASTIC: + case PoolingParameter_PoolMethod_STOCHASTIC: NOT_IMPLEMENTED; break; default: diff --git a/src/caffe/layers/pooling_layer.cu b/src/caffe/layers/pooling_layer.cu index e8dc2a28349..ab72279e827 100644 --- a/src/caffe/layers/pooling_layer.cu +++ b/src/caffe/layers/pooling_layer.cu @@ -140,22 +140,22 @@ Dtype PoolingLayer::Forward_gpu(const vector*>& bottom, const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* top_data = (*top)[0]->mutable_gpu_data(); int count = (*top)[0]->count(); - switch (this->layer_param_.pool()) { - case LayerParameter_PoolMethod_MAX: + switch (this->layer_param_.pooling_param().pool()) { + case PoolingParameter_PoolMethod_MAX: // NOLINT_NEXT_LINE(whitespace/operators) MaxPoolForward<<>>( count, bottom_data, bottom[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, top_data); break; - case LayerParameter_PoolMethod_AVE: + case PoolingParameter_PoolMethod_AVE: // NOLINT_NEXT_LINE(whitespace/operators) AvePoolForward<<>>( count, bottom_data, bottom[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, top_data); break; - case LayerParameter_PoolMethod_STOCHASTIC: + case PoolingParameter_PoolMethod_STOCHASTIC: if (Caffe::phase() == Caffe::TRAIN) { // We need to create the random index as well. CURAND_CHECK(curandGenerateUniform(Caffe::curand_generator(), @@ -286,22 +286,22 @@ void PoolingLayer::Backward_gpu(const vector*>& top, const Dtype* top_diff = top[0]->gpu_diff(); Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff(); int count = (*bottom)[0]->count(); - switch (this->layer_param_.pool()) { - case LayerParameter_PoolMethod_MAX: + switch (this->layer_param_.pooling_param().pool()) { + case PoolingParameter_PoolMethod_MAX: // NOLINT_NEXT_LINE(whitespace/operators) MaxPoolBackward<<>>( count, (*bottom)[0]->gpu_data(), top[0]->gpu_data(), top_diff, top[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, bottom_diff); break; - case LayerParameter_PoolMethod_AVE: + case PoolingParameter_PoolMethod_AVE: // NOLINT_NEXT_LINE(whitespace/operators) AvePoolBackward<<>>( count, top_diff, top[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, bottom_diff); break; - case LayerParameter_PoolMethod_STOCHASTIC: + case PoolingParameter_PoolMethod_STOCHASTIC: // NOLINT_NEXT_LINE(whitespace/operators) StoPoolBackward<<>>( count, rand_idx_.gpu_data(), top_diff, diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 1ba0e835cfd..22aa00485cc 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -38,7 +38,7 @@ void Net::Init(const NetParameter& in_param) { name_ = param.name(); map blob_name_to_idx; set available_blobs; - int num_layers = param.layers_size(); + int num_layers = param.layer_size(); CHECK_EQ(param.input_size() * 4, param.input_dim_size()) << "Incorrect bottom blob dimension specifications."; size_t memory_used = 0; @@ -61,21 +61,20 @@ void Net::Init(const NetParameter& in_param) { } DLOG(INFO) << "Memory required for Data" << memory_used*sizeof(Dtype); // For each layer, set up their input and output - bottom_vecs_.resize(param.layers_size()); - top_vecs_.resize(param.layers_size()); - bottom_id_vecs_.resize(param.layers_size()); - top_id_vecs_.resize(param.layers_size()); - for (int i = 0; i < param.layers_size(); ++i) { + bottom_vecs_.resize(param.layer_size()); + top_vecs_.resize(param.layer_size()); + bottom_id_vecs_.resize(param.layer_size()); + top_id_vecs_.resize(param.layer_size()); + for (int i = 0; i < param.layer_size(); ++i) { bool in_place = false; - const LayerConnection& layer_connection = param.layers(i); - const LayerParameter& layer_param = layer_connection.layer(); + const LayerParameter& layer_param = param.layer(i); layers_.push_back(shared_ptr >(GetLayer(layer_param))); layer_names_.push_back(layer_param.name()); LOG(INFO) << "Creating Layer " << layer_param.name(); bool need_backward = param.force_backward(); // Figure out this layer's input and output - for (int j = 0; j < layer_connection.bottom_size(); ++j) { - const string& blob_name = layer_connection.bottom(j); + for (int j = 0; j < layer_param.bottom_size(); ++j) { + const string& blob_name = layer_param.bottom(j); const int blob_id = blob_name_to_idx[blob_name]; if (available_blobs.find(blob_name) == available_blobs.end()) { LOG(FATAL) << "Unknown blob input " << blob_name << @@ -89,11 +88,11 @@ void Net::Init(const NetParameter& in_param) { need_backward |= blob_need_backward_[blob_id]; available_blobs.erase(blob_name); } - for (int j = 0; j < layer_connection.top_size(); ++j) { - const string& blob_name = layer_connection.top(j); + for (int j = 0; j < layer_param.top_size(); ++j) { + const string& blob_name = layer_param.top(j); // Check if we are doing in-place computation - if (layer_connection.bottom_size() > j && - blob_name == layer_connection.bottom(j)) { + if (layer_param.bottom_size() > j && + blob_name == layer_param.bottom(j)) { // In-place computation LOG(INFO) << layer_param.name() << " -> " << blob_name << " (in-place)"; in_place = true; @@ -270,9 +269,9 @@ void Net::Backward() { template void Net::CopyTrainedLayersFrom(const NetParameter& param) { - int num_source_layers = param.layers_size(); + int num_source_layers = param.layer_size(); for (int i = 0; i < num_source_layers; ++i) { - const LayerParameter& source_layer = param.layers(i).layer(); + const LayerParameter& source_layer = param.layer(i); const string& source_layer_name = source_layer.name(); int target_layer_id = 0; while (target_layer_id != layer_names_.size() && @@ -315,15 +314,14 @@ void Net::ToProto(NetParameter* param, bool write_diff) { } DLOG(INFO) << "Serializing " << layers_.size() << " layers"; for (int i = 0; i < layers_.size(); ++i) { - LayerConnection* layer_connection = param->add_layers(); + LayerParameter* layer_param = param->add_layer(); for (int j = 0; j < bottom_id_vecs_[i].size(); ++j) { - layer_connection->add_bottom(blob_names_[bottom_id_vecs_[i][j]]); + layer_param->add_bottom(blob_names_[bottom_id_vecs_[i][j]]); } for (int j = 0; j < top_id_vecs_[i].size(); ++j) { - layer_connection->add_top(blob_names_[top_id_vecs_[i][j]]); + layer_param->add_top(blob_names_[top_id_vecs_[i][j]]); } - LayerParameter* layer_parameter = layer_connection->mutable_layer(); - layers_[i]->ToProto(layer_parameter, write_diff); + layers_[i]->ToProto(layer_param, write_diff); } } diff --git a/src/caffe/util/insert_splits.cpp b/src/caffe/util/insert_splits.cpp index f72f20c0e25..2c8bdf17700 100644 --- a/src/caffe/util/insert_splits.cpp +++ b/src/caffe/util/insert_splits.cpp @@ -18,7 +18,7 @@ namespace caffe { void insert_splits(const NetParameter& param, NetParameter* param_split) { // Initialize by copying from the input NetParameter. param_split->CopyFrom(param); - param_split->clear_layers(); + param_split->clear_layer(); map > blob_name_to_last_top_idx; map, pair > bottom_idx_to_source_top_idx; map, int> top_idx_to_bottom_count; @@ -30,11 +30,11 @@ void insert_splits(const NetParameter& param, NetParameter* param_split) { const string& blob_name = param.input(i); blob_name_to_last_top_idx[blob_name] = make_pair(-1, i); } - for (int i = 0; i < param.layers_size(); ++i) { - const LayerConnection& layer_connection = param.layers(i); - layer_idx_to_layer_name[i] = layer_connection.layer().name(); - for (int j = 0; j < layer_connection.bottom_size(); ++j) { - const string& blob_name = layer_connection.bottom(j); + for (int i = 0; i < param.layer_size(); ++i) { + const LayerParameter& layer_param = param.layer(i); + layer_idx_to_layer_name[i] = layer_param.name(); + for (int j = 0; j < layer_param.bottom_size(); ++j) { + const string& blob_name = layer_param.bottom(j); if (blob_name_to_last_top_idx.find(blob_name) == blob_name_to_last_top_idx.end()) { LOG(FATAL) << "Unknown blob input " << blob_name << " to layer " << j; @@ -44,8 +44,8 @@ void insert_splits(const NetParameter& param, NetParameter* param_split) { bottom_idx_to_source_top_idx[bottom_idx] = top_idx; ++top_idx_to_bottom_count[top_idx]; } - for (int j = 0; j < layer_connection.top_size(); ++j) { - const string& blob_name = layer_connection.top(j); + for (int j = 0; j < layer_param.top_size(); ++j) { + const string& blob_name = layer_param.top(j); blob_name_to_last_top_idx[blob_name] = make_pair(i, j); } } @@ -56,36 +56,36 @@ void insert_splits(const NetParameter& param, NetParameter* param_split) { if (split_count > 1) { const string& layer_name = layer_idx_to_layer_name[-1]; const string& blob_name = param.input(i); - LayerConnection* split_layer_connection = param_split->add_layers(); + LayerParameter* split_layer_param = param_split->add_layer(); configure_split_layer(layer_name, blob_name, i, split_count, - split_layer_connection); + split_layer_param); } } - for (int i = 0; i < param.layers_size(); ++i) { - LayerConnection* layer_connection = param_split->add_layers(); - layer_connection->CopyFrom(param.layers(i)); + for (int i = 0; i < param.layer_size(); ++i) { + LayerParameter* layer_param = param_split->add_layer(); + layer_param->CopyFrom(param.layer(i)); // Replace any shared bottom blobs with split layer outputs. - for (int j = 0; j < layer_connection->bottom_size(); ++j) { + for (int j = 0; j < layer_param->bottom_size(); ++j) { const pair& top_idx = bottom_idx_to_source_top_idx[make_pair(i, j)]; const int split_count = top_idx_to_bottom_count[top_idx]; if (split_count > 1) { const string& layer_name = layer_idx_to_layer_name[top_idx.first]; - const string& blob_name = layer_connection->bottom(j); - layer_connection->set_bottom(j, get_split_blob_name(layer_name, + const string& blob_name = layer_param->bottom(j); + layer_param->set_bottom(j, get_split_blob_name(layer_name, blob_name, top_idx.second, top_idx_to_bottom_split_idx[top_idx]++)); } } // Create split layer for any top blobs used by other layers as bottom // blobs more than once. - for (int j = 0; j < layer_connection->top_size(); ++j) { + for (int j = 0; j < layer_param->top_size(); ++j) { const int split_count = top_idx_to_bottom_count[make_pair(i, j)]; if (split_count > 1) { const string& layer_name = layer_idx_to_layer_name[i]; - const string& blob_name = layer_connection->top(j); - LayerConnection* split_layer_connection = param_split->add_layers(); + const string& blob_name = layer_param->top(j); + LayerParameter* split_layer_param = param_split->add_layer(); configure_split_layer(layer_name, blob_name, j, split_count, - split_layer_connection); + split_layer_param); } } } @@ -93,15 +93,14 @@ void insert_splits(const NetParameter& param, NetParameter* param_split) { void configure_split_layer(const string& layer_name, const string& blob_name, const int blob_idx, const int split_count, - LayerConnection* split_layer_connection) { - split_layer_connection->Clear(); - split_layer_connection->add_bottom(blob_name); - LayerParameter* split_layer_param = split_layer_connection->mutable_layer(); + LayerParameter* split_layer_param) { + split_layer_param->Clear(); + split_layer_param->add_bottom(blob_name); split_layer_param->set_name( get_split_layer_name(layer_name, blob_name, blob_idx)); split_layer_param->set_type("split"); for (int k = 0; k < split_count; ++k) { - split_layer_connection->add_top( + split_layer_param->add_top( get_split_blob_name(layer_name, blob_name, blob_idx, k)); } } From 1816561936b8173f78659850de4e8e1da677bd7a Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 14 Mar 2014 20:56:07 -0700 Subject: [PATCH 10/60] update tests for new proto format; now they compile --- src/caffe/test/test_concat_layer.cpp | 2 +- src/caffe/test/test_convolution_layer.cpp | 98 ++++++++++++---------- src/caffe/test/test_data_layer.cpp | 5 +- src/caffe/test/test_hdf5data_layer.cpp | 19 +++-- src/caffe/test/test_im2col_layer.cpp | 30 ++++--- src/caffe/test/test_innerproduct_layer.cpp | 48 ++++++----- src/caffe/test/test_lrn_layer.cpp | 7 +- src/caffe/test/test_neuron_layer.cpp | 8 +- src/caffe/test/test_pooling_layer.cpp | 40 +++++---- src/caffe/test/test_stochastic_pooling.cpp | 29 +++---- 10 files changed, 165 insertions(+), 121 deletions(-) diff --git a/src/caffe/test/test_concat_layer.cpp b/src/caffe/test/test_concat_layer.cpp index 8ce7ce16fa9..72e3c902cf1 100644 --- a/src/caffe/test/test_concat_layer.cpp +++ b/src/caffe/test/test_concat_layer.cpp @@ -60,7 +60,7 @@ TYPED_TEST_CASE(ConcatLayerTest, Dtypes); TYPED_TEST(ConcatLayerTest, TestSetupNum) { LayerParameter layer_param; - layer_param.set_concat_dim(0); + layer_param.mutable_concat_param()->set_concat_dim(0); ConcatLayer layer(layer_param); layer.SetUp(this->blob_bottom_vec_1, &(this->blob_top_vec_)); EXPECT_EQ(this->blob_top_->num(), diff --git a/src/caffe/test/test_convolution_layer.cpp b/src/caffe/test/test_convolution_layer.cpp index db23680c3df..c8d79083513 100644 --- a/src/caffe/test/test_convolution_layer.cpp +++ b/src/caffe/test/test_convolution_layer.cpp @@ -46,9 +46,11 @@ TYPED_TEST_CASE(ConvolutionLayerTest, Dtypes); TYPED_TEST(ConvolutionLayerTest, TestSetup) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_num_output(4); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); + convolution_param->set_num_output(4); shared_ptr > layer( new ConvolutionLayer(layer_param)); layer->SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -57,8 +59,8 @@ TYPED_TEST(ConvolutionLayerTest, TestSetup) { EXPECT_EQ(this->blob_top_->height(), 2); EXPECT_EQ(this->blob_top_->width(), 2); // setting group should not change the shape - layer_param.set_num_output(3); - layer_param.set_group(3); + convolution_param->set_num_output(3); + convolution_param->set_group(3); layer.reset(new ConvolutionLayer(layer_param)); layer->SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); EXPECT_EQ(this->blob_top_->num(), 2); @@ -74,13 +76,15 @@ TYPED_TEST(ConvolutionLayerTest, TestSimpleConvolution) { ConstantFiller filler(filler_param); filler.Fill(this->blob_bottom_); LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_num_output(4); - layer_param.mutable_weight_filler()->set_type("constant"); - layer_param.mutable_weight_filler()->set_value(1); - layer_param.mutable_bias_filler()->set_type("constant"); - layer_param.mutable_bias_filler()->set_value(0.1); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); + convolution_param->set_num_output(4); + convolution_param->mutable_weight_filler()->set_type("constant"); + convolution_param->mutable_weight_filler()->set_value(1); + convolution_param->mutable_bias_filler()->set_type("constant"); + convolution_param->mutable_bias_filler()->set_value(0.1); shared_ptr > layer( new ConvolutionLayer(layer_param)); layer->SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -120,14 +124,16 @@ TYPED_TEST(ConvolutionLayerTest, TestSimpleConvolutionGroup) { } } LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_num_output(3); - layer_param.set_group(3); - layer_param.mutable_weight_filler()->set_type("constant"); - layer_param.mutable_weight_filler()->set_value(1); - layer_param.mutable_bias_filler()->set_type("constant"); - layer_param.mutable_bias_filler()->set_value(0.1); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); + convolution_param->set_num_output(3); + convolution_param->set_group(3); + convolution_param->mutable_weight_filler()->set_type("constant"); + convolution_param->mutable_weight_filler()->set_value(1); + convolution_param->mutable_bias_filler()->set_type("constant"); + convolution_param->mutable_bias_filler()->set_value(0.1); shared_ptr > layer( new ConvolutionLayer(layer_param)); layer->SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -167,11 +173,13 @@ TYPED_TEST(ConvolutionLayerTest, TestSimpleConvolutionGroup) { TYPED_TEST(ConvolutionLayerTest, TestCPUGradient) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_num_output(2); - layer_param.mutable_weight_filler()->set_type("gaussian"); - layer_param.mutable_bias_filler()->set_type("gaussian"); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); + convolution_param->set_num_output(2); + convolution_param->mutable_weight_filler()->set_type("gaussian"); + convolution_param->mutable_bias_filler()->set_type("gaussian"); Caffe::set_mode(Caffe::CPU); ConvolutionLayer layer(layer_param); GradientChecker checker(1e-2, 1e-3); @@ -181,12 +189,14 @@ TYPED_TEST(ConvolutionLayerTest, TestCPUGradient) { TYPED_TEST(ConvolutionLayerTest, TestCPUGradientGroup) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_num_output(3); - layer_param.set_group(3); - layer_param.mutable_weight_filler()->set_type("gaussian"); - layer_param.mutable_bias_filler()->set_type("gaussian"); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); + convolution_param->set_num_output(3); + convolution_param->set_group(3); + convolution_param->mutable_weight_filler()->set_type("gaussian"); + convolution_param->mutable_bias_filler()->set_type("gaussian"); Caffe::set_mode(Caffe::CPU); ConvolutionLayer layer(layer_param); GradientChecker checker(1e-2, 1e-3); @@ -196,11 +206,13 @@ TYPED_TEST(ConvolutionLayerTest, TestCPUGradientGroup) { TYPED_TEST(ConvolutionLayerTest, TestGPUGradient) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_num_output(2); - layer_param.mutable_weight_filler()->set_type("gaussian"); - layer_param.mutable_bias_filler()->set_type("gaussian"); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); + convolution_param->set_num_output(2); + convolution_param->mutable_weight_filler()->set_type("gaussian"); + convolution_param->mutable_bias_filler()->set_type("gaussian"); Caffe::set_mode(Caffe::GPU); ConvolutionLayer layer(layer_param); GradientChecker checker(1e-2, 1e-3); @@ -210,12 +222,14 @@ TYPED_TEST(ConvolutionLayerTest, TestGPUGradient) { TYPED_TEST(ConvolutionLayerTest, TestGPUGradientGroup) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_num_output(3); - layer_param.set_group(3); - layer_param.mutable_weight_filler()->set_type("gaussian"); - layer_param.mutable_bias_filler()->set_type("gaussian"); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); + convolution_param->set_num_output(3); + convolution_param->set_group(3); + convolution_param->mutable_weight_filler()->set_type("gaussian"); + convolution_param->mutable_bias_filler()->set_type("gaussian"); Caffe::set_mode(Caffe::GPU); ConvolutionLayer layer(layer_param); GradientChecker checker(1e-2, 1e-3); diff --git a/src/caffe/test/test_data_layer.cpp b/src/caffe/test/test_data_layer.cpp index 032b0eb3b7d..6b0838ab975 100644 --- a/src/caffe/test/test_data_layer.cpp +++ b/src/caffe/test/test_data_layer.cpp @@ -70,8 +70,9 @@ TYPED_TEST_CASE(DataLayerTest, Dtypes); TYPED_TEST(DataLayerTest, TestRead) { LayerParameter param; - param.set_batchsize(5); - param.set_source(this->filename); + DataParameter* data_param = param.mutable_data_param(); + data_param->set_batch_size(5); + data_param->set_source(this->filename); DataLayer layer(param); layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); EXPECT_EQ(this->blob_top_data_->num(), 5); diff --git a/src/caffe/test/test_hdf5data_layer.cpp b/src/caffe/test/test_hdf5data_layer.cpp index b03fe72636c..a0ed113b36e 100644 --- a/src/caffe/test/test_hdf5data_layer.cpp +++ b/src/caffe/test/test_hdf5data_layer.cpp @@ -57,9 +57,10 @@ TYPED_TEST(HDF5DataLayerTest, TestRead) { // The data file we are reading has 10 rows and 8 columns, // with values from 0 to 10*8 reshaped in row-major order. LayerParameter param; - int batchsize = 5; - param.set_batchsize(batchsize); - param.set_source(*(this->filename)); + HDF5DataParameter* hdf5_data_param = param.mutable_hdf5_data_param(); + int batch_size = 5; + hdf5_data_param->set_batch_size(batch_size); + hdf5_data_param->set_source(*(this->filename)); int num_rows = 10; int num_cols = 8; int height = 5; @@ -68,12 +69,12 @@ TYPED_TEST(HDF5DataLayerTest, TestRead) { // Test that the layer setup got the correct parameters. HDF5DataLayer layer(param); layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); - EXPECT_EQ(this->blob_top_data_->num(), batchsize); + EXPECT_EQ(this->blob_top_data_->num(), batch_size); EXPECT_EQ(this->blob_top_data_->channels(), num_cols); EXPECT_EQ(this->blob_top_data_->height(), height); EXPECT_EQ(this->blob_top_data_->width(), width); - EXPECT_EQ(this->blob_top_label_->num(), batchsize); + EXPECT_EQ(this->blob_top_label_->num(), batch_size); EXPECT_EQ(this->blob_top_label_->channels(), 1); EXPECT_EQ(this->blob_top_label_->height(), 1); EXPECT_EQ(this->blob_top_label_->width(), 1); @@ -94,20 +95,20 @@ TYPED_TEST(HDF5DataLayerTest, TestRead) { // On even iterations, we're reading the first half of the data. // On odd iterations, we're reading the second half of the data. - int label_offset = (iter % 2 == 0) ? 0 : batchsize; - int data_offset = (iter % 2 == 0) ? 0 : batchsize * data_size; + int label_offset = (iter % 2 == 0) ? 0 : batch_size; + int data_offset = (iter % 2 == 0) ? 0 : batch_size * data_size; // Every two iterations we are reading the second file, // which has the same labels, but data is offset by total data size, // which is 2000 (see generate_sample_data). int file_offset = (iter % 4 < 2) ? 0 : 2000; - for (int i = 0; i < batchsize; ++i) { + for (int i = 0; i < batch_size; ++i) { EXPECT_EQ( label_offset + i, this->blob_top_label_->cpu_data()[i]); } - for (int i = 0; i < batchsize; ++i) { + for (int i = 0; i < batch_size; ++i) { for (int j = 0; j < num_cols; ++j) { for (int h = 0; h < height; ++h) { for (int w = 0; w < width; ++w) { diff --git a/src/caffe/test/test_im2col_layer.cpp b/src/caffe/test/test_im2col_layer.cpp index 31a01157748..7f677ca03d6 100644 --- a/src/caffe/test/test_im2col_layer.cpp +++ b/src/caffe/test/test_im2col_layer.cpp @@ -42,8 +42,10 @@ TYPED_TEST_CASE(Im2colLayerTest, Dtypes); TYPED_TEST(Im2colLayerTest, TestSetup) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); Im2colLayer layer(layer_param); layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); EXPECT_EQ(this->blob_top_->num(), 2); @@ -54,8 +56,10 @@ TYPED_TEST(Im2colLayerTest, TestSetup) { TYPED_TEST(Im2colLayerTest, TestCPU) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); Im2colLayer layer(layer_param); Caffe::set_mode(Caffe::CPU); layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -69,8 +73,10 @@ TYPED_TEST(Im2colLayerTest, TestCPU) { TYPED_TEST(Im2colLayerTest, TestGPU) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); Im2colLayer layer(layer_param); Caffe::set_mode(Caffe::GPU); layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -84,8 +90,10 @@ TYPED_TEST(Im2colLayerTest, TestGPU) { TYPED_TEST(Im2colLayerTest, TestCPUGradient) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); Caffe::set_mode(Caffe::CPU); Im2colLayer layer(layer_param); GradientChecker checker(1e-2, 1e-2); @@ -95,8 +103,10 @@ TYPED_TEST(Im2colLayerTest, TestCPUGradient) { TYPED_TEST(Im2colLayerTest, TestGPUGradient) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); + ConvolutionParameter* convolution_param = + layer_param.mutable_convolution_param(); + convolution_param->set_kernel_size(3); + convolution_param->set_stride(2); Caffe::set_mode(Caffe::GPU); Im2colLayer layer(layer_param); GradientChecker checker(1e-2, 1e-2); diff --git a/src/caffe/test/test_innerproduct_layer.cpp b/src/caffe/test/test_innerproduct_layer.cpp index 5b1831725de..91917df6cae 100644 --- a/src/caffe/test/test_innerproduct_layer.cpp +++ b/src/caffe/test/test_innerproduct_layer.cpp @@ -42,7 +42,9 @@ TYPED_TEST_CASE(InnerProductLayerTest, Dtypes); TYPED_TEST(InnerProductLayerTest, TestSetUp) { LayerParameter layer_param; - layer_param.set_num_output(10); + InnerProductParameter* inner_product_param = + layer_param.mutable_inner_product_param(); + inner_product_param->set_num_output(10); shared_ptr > layer( new InnerProductLayer(layer_param)); layer->SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -54,12 +56,14 @@ TYPED_TEST(InnerProductLayerTest, TestSetUp) { TYPED_TEST(InnerProductLayerTest, TestCPU) { LayerParameter layer_param; + InnerProductParameter* inner_product_param = + layer_param.mutable_inner_product_param(); Caffe::set_mode(Caffe::CPU); - layer_param.set_num_output(10); - layer_param.mutable_weight_filler()->set_type("uniform"); - layer_param.mutable_bias_filler()->set_type("uniform"); - layer_param.mutable_bias_filler()->set_min(1); - layer_param.mutable_bias_filler()->set_max(2); + inner_product_param->set_num_output(10); + inner_product_param->mutable_weight_filler()->set_type("uniform"); + inner_product_param->mutable_bias_filler()->set_type("uniform"); + inner_product_param->mutable_bias_filler()->set_min(1); + inner_product_param->mutable_bias_filler()->set_max(2); shared_ptr > layer( new InnerProductLayer(layer_param)); layer->SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -74,12 +78,14 @@ TYPED_TEST(InnerProductLayerTest, TestCPU) { TYPED_TEST(InnerProductLayerTest, TestGPU) { if (sizeof(TypeParam) == 4 || CAFFE_TEST_CUDA_PROP.major >= 2) { LayerParameter layer_param; + InnerProductParameter* inner_product_param = + layer_param.mutable_inner_product_param(); Caffe::set_mode(Caffe::GPU); - layer_param.set_num_output(10); - layer_param.mutable_weight_filler()->set_type("uniform"); - layer_param.mutable_bias_filler()->set_type("uniform"); - layer_param.mutable_bias_filler()->set_min(1); - layer_param.mutable_bias_filler()->set_max(2); + inner_product_param->set_num_output(10); + inner_product_param->mutable_weight_filler()->set_type("uniform"); + inner_product_param->mutable_bias_filler()->set_type("uniform"); + inner_product_param->mutable_bias_filler()->set_min(1); + inner_product_param->mutable_bias_filler()->set_max(2); shared_ptr > layer( new InnerProductLayer(layer_param)); layer->SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -96,12 +102,14 @@ TYPED_TEST(InnerProductLayerTest, TestGPU) { TYPED_TEST(InnerProductLayerTest, TestCPUGradient) { LayerParameter layer_param; + InnerProductParameter* inner_product_param = + layer_param.mutable_inner_product_param(); Caffe::set_mode(Caffe::CPU); - layer_param.set_num_output(10); - layer_param.mutable_weight_filler()->set_type("gaussian"); - layer_param.mutable_bias_filler()->set_type("gaussian"); - layer_param.mutable_bias_filler()->set_min(1); - layer_param.mutable_bias_filler()->set_max(2); + inner_product_param->set_num_output(10); + inner_product_param->mutable_weight_filler()->set_type("gaussian"); + inner_product_param->mutable_bias_filler()->set_type("gaussian"); + inner_product_param->mutable_bias_filler()->set_min(1); + inner_product_param->mutable_bias_filler()->set_max(2); InnerProductLayer layer(layer_param); GradientChecker checker(1e-2, 1e-3); checker.CheckGradientExhaustive(&layer, &(this->blob_bottom_vec_), @@ -111,10 +119,12 @@ TYPED_TEST(InnerProductLayerTest, TestCPUGradient) { TYPED_TEST(InnerProductLayerTest, TestGPUGradient) { if (sizeof(TypeParam) == 4 || CAFFE_TEST_CUDA_PROP.major >= 2) { LayerParameter layer_param; + InnerProductParameter* inner_product_param = + layer_param.mutable_inner_product_param(); Caffe::set_mode(Caffe::GPU); - layer_param.set_num_output(10); - layer_param.mutable_weight_filler()->set_type("gaussian"); - layer_param.mutable_bias_filler()->set_type("gaussian"); + inner_product_param->set_num_output(10); + inner_product_param->mutable_weight_filler()->set_type("gaussian"); + inner_product_param->mutable_bias_filler()->set_type("gaussian"); InnerProductLayer layer(layer_param); GradientChecker checker(1e-2, 1e-2); checker.CheckGradient(&layer, &(this->blob_bottom_vec_), diff --git a/src/caffe/test/test_lrn_layer.cpp b/src/caffe/test/test_lrn_layer.cpp index a96684d2160..6ad6d020c03 100644 --- a/src/caffe/test/test_lrn_layer.cpp +++ b/src/caffe/test/test_lrn_layer.cpp @@ -54,9 +54,10 @@ void LRNLayerTest::ReferenceLRNForward( blob_bottom.height(), blob_bottom.width()); const Dtype* bottom_data = blob_bottom.cpu_data(); Dtype* top_data = blob_top->mutable_cpu_data(); - Dtype alpha = layer_param.alpha(); - Dtype beta = layer_param.beta(); - int size = layer_param.local_size(); + LRNParameter lrn_param = layer_param.lrn_param(); + Dtype alpha = lrn_param.alpha(); + Dtype beta = lrn_param.beta(); + int size = lrn_param.local_size(); for (int n = 0; n < blob_bottom.num(); ++n) { for (int c = 0; c < blob_bottom.channels(); ++c) { for (int h = 0; h < blob_bottom.height(); ++h) { diff --git a/src/caffe/test/test_neuron_layer.cpp b/src/caffe/test/test_neuron_layer.cpp index 105f321e7bb..cd733751a70 100644 --- a/src/caffe/test/test_neuron_layer.cpp +++ b/src/caffe/test/test_neuron_layer.cpp @@ -158,7 +158,7 @@ TYPED_TEST(NeuronLayerTest, TestDropoutCPU) { // Now, check values const TypeParam* bottom_data = this->blob_bottom_->cpu_data(); const TypeParam* top_data = this->blob_top_->cpu_data(); - float scale = 1. / (1. - layer_param.dropout_ratio()); + float scale = 1. / (1. - layer_param.dropout_param().dropout_ratio()); for (int i = 0; i < this->blob_bottom_->count(); ++i) { if (top_data[i] != 0) { EXPECT_EQ(top_data[i], bottom_data[i] * scale); @@ -187,7 +187,7 @@ TYPED_TEST(NeuronLayerTest, TestDropoutCPUTestPhase) { // Now, check values const TypeParam* bottom_data = this->blob_bottom_->cpu_data(); const TypeParam* top_data = this->blob_top_->cpu_data(); - float scale = 1. / (1. - layer_param.dropout_ratio()); + float scale = 1. / (1. - layer_param.dropout_param().dropout_ratio()); for (int i = 0; i < this->blob_bottom_->count(); ++i) { if (top_data[i] != 0) { EXPECT_EQ(top_data[i], bottom_data[i]); @@ -206,7 +206,7 @@ TYPED_TEST(NeuronLayerTest, TestDropoutGPU) { // Now, check values const TypeParam* bottom_data = this->blob_bottom_->cpu_data(); const TypeParam* top_data = this->blob_top_->cpu_data(); - float scale = 1. / (1. - layer_param.dropout_ratio()); + float scale = 1. / (1. - layer_param.dropout_param().dropout_ratio()); for (int i = 0; i < this->blob_bottom_->count(); ++i) { if (top_data[i] != 0) { EXPECT_EQ(top_data[i], bottom_data[i] * scale); @@ -241,7 +241,7 @@ TYPED_TEST(NeuronLayerTest, TestDropoutGPUTestPhase) { // Now, check values const TypeParam* bottom_data = this->blob_bottom_->cpu_data(); const TypeParam* top_data = this->blob_top_->cpu_data(); - float scale = 1. / (1. - layer_param.dropout_ratio()); + float scale = 1. / (1. - layer_param.dropout_param().dropout_ratio()); for (int i = 0; i < this->blob_bottom_->count(); ++i) { if (top_data[i] != 0) { EXPECT_EQ(top_data[i], bottom_data[i]); diff --git a/src/caffe/test/test_pooling_layer.cpp b/src/caffe/test/test_pooling_layer.cpp index 11b9ce23583..d1246a098c8 100644 --- a/src/caffe/test/test_pooling_layer.cpp +++ b/src/caffe/test/test_pooling_layer.cpp @@ -45,8 +45,9 @@ TYPED_TEST_CASE(PoolingLayerTest, Dtypes); TYPED_TEST(PoolingLayerTest, TestSetup) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); PoolingLayer layer(layer_param); layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); EXPECT_EQ(this->blob_top_->num(), this->blob_bottom_->num()); @@ -58,9 +59,10 @@ TYPED_TEST(PoolingLayerTest, TestSetup) { /* TYPED_TEST(PoolingLayerTest, PrintGPUBackward) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_pool(LayerParameter_PoolMethod_MAX); + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); + pooling_param->set_pool(PoolingParameter_PoolMethod_MAX); Caffe::set_mode(Caffe::GPU); PoolingLayer layer(layer_param); layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -84,9 +86,10 @@ TYPED_TEST(PoolingLayerTest, PrintGPUBackward) { TYPED_TEST(PoolingLayerTest, TestCPUGradientMax) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_pool(LayerParameter_PoolMethod_MAX); + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); + pooling_param->set_pool(PoolingParameter_PoolMethod_MAX); Caffe::set_mode(Caffe::CPU); PoolingLayer layer(layer_param); GradientChecker checker(1e-4, 1e-2); @@ -96,9 +99,10 @@ TYPED_TEST(PoolingLayerTest, TestCPUGradientMax) { TYPED_TEST(PoolingLayerTest, TestGPUGradientMax) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_pool(LayerParameter_PoolMethod_MAX); + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); + pooling_param->set_pool(PoolingParameter_PoolMethod_MAX); Caffe::set_mode(Caffe::GPU); PoolingLayer layer(layer_param); GradientChecker checker(1e-4, 1e-2); @@ -109,9 +113,10 @@ TYPED_TEST(PoolingLayerTest, TestGPUGradientMax) { TYPED_TEST(PoolingLayerTest, TestCPUGradientAve) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_pool(LayerParameter_PoolMethod_AVE); + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); + pooling_param->set_pool(PoolingParameter_PoolMethod_AVE); Caffe::set_mode(Caffe::CPU); PoolingLayer layer(layer_param); GradientChecker checker(1e-2, 1e-2); @@ -122,9 +127,10 @@ TYPED_TEST(PoolingLayerTest, TestCPUGradientAve) { TYPED_TEST(PoolingLayerTest, TestGPUGradientAve) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - layer_param.set_pool(LayerParameter_PoolMethod_AVE); + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); + pooling_param->set_pool(PoolingParameter_PoolMethod_AVE); Caffe::set_mode(Caffe::GPU); PoolingLayer layer(layer_param); GradientChecker checker(1e-2, 1e-2); diff --git a/src/caffe/test/test_stochastic_pooling.cpp b/src/caffe/test/test_stochastic_pooling.cpp index daf2c36d305..0ad8123f881 100644 --- a/src/caffe/test/test_stochastic_pooling.cpp +++ b/src/caffe/test/test_stochastic_pooling.cpp @@ -54,8 +54,9 @@ TYPED_TEST_CASE(StochasticPoolingLayerTest, Dtypes); TYPED_TEST(StochasticPoolingLayerTest, TestSetup) { LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); PoolingLayer layer(layer_param); layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); EXPECT_EQ(this->blob_top_->num(), this->blob_bottom_->num()); @@ -68,10 +69,10 @@ TYPED_TEST(StochasticPoolingLayerTest, TestStochasticGPU) { Caffe::set_mode(Caffe::GPU); Caffe::set_phase(Caffe::TRAIN); LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - - layer_param.set_pool(LayerParameter_PoolMethod_STOCHASTIC); + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); + pooling_param->set_pool(PoolingParameter_PoolMethod_STOCHASTIC); PoolingLayer layer(layer_param); layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -112,10 +113,10 @@ TYPED_TEST(StochasticPoolingLayerTest, TestStochasticGPUTestPhase) { Caffe::set_mode(Caffe::GPU); Caffe::set_phase(Caffe::TEST); LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - - layer_param.set_pool(LayerParameter_PoolMethod_STOCHASTIC); + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); + pooling_param->set_pool(PoolingParameter_PoolMethod_STOCHASTIC); PoolingLayer layer(layer_param); layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -150,10 +151,10 @@ TYPED_TEST(StochasticPoolingLayerTest, TestGradientGPU) { Caffe::set_mode(Caffe::GPU); Caffe::set_phase(Caffe::TRAIN); LayerParameter layer_param; - layer_param.set_kernelsize(3); - layer_param.set_stride(2); - - layer_param.set_pool(LayerParameter_PoolMethod_STOCHASTIC); + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); + pooling_param->set_pool(PoolingParameter_PoolMethod_STOCHASTIC); PoolingLayer layer(layer_param); GradientChecker checker(1e-4, 1e-2); // it is too expensive to call curand multiple times, so we don't do an From dbd616b74831d6efff3401c99e309017e3112627 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 14 Mar 2014 22:24:12 -0700 Subject: [PATCH 11/60] move caffe.proto.v0 -> deprecated/caffe.v0.proto and add separate target makefile target for it --- Makefile | 16 +++++++++++++++- .../caffe.v0.proto} | 0 2 files changed, 15 insertions(+), 1 deletion(-) rename src/caffe/proto/{caffe.proto.v0 => deprecated/caffe.v0.proto} (100%) diff --git a/Makefile b/Makefile index acef07d6837..26d37762bb9 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,9 @@ TOOL_SRCS := $(shell find tools -name "*.cpp") EXAMPLE_SRCS := $(shell find examples -name "*.cpp") # PROTO_SRCS are the protocol buffer definitions PROTO_SRCS := $(wildcard src/$(PROJECT)/proto/*.proto) +# DEPRECATED_PROTO_SRCS are protobuf definitions we no longer officially +# support, but keep around for upgrades etc. +DEPRECATED_PROTO_SRCS := $(wildcard src/$(PROJECT)/proto/deprecated/*.proto) # NONGEN_CXX_SRCS includes all source/header files except those generated # automatically (e.g., by proto). NONGEN_CXX_SRCS := $(shell find \ @@ -60,6 +63,10 @@ MAT$(PROJECT)_SO := matlab/$(PROJECT)/$(PROJECT) PROTO_GEN_HEADER := ${PROTO_SRCS:.proto=.pb.h} PROTO_GEN_CC := ${PROTO_SRCS:.proto=.pb.cc} PROTO_GEN_PY := ${PROTO_SRCS:.proto=_pb2.py} +# The generated files for deprecated protocol buffers +DEPRECATED_PROTO_GEN_HEADER := ${DEPRECATED_PROTO_SRCS:.proto=.pb.h} +DEPRECATED_PROTO_GEN_CC := ${DEPRECATED_PROTO_SRCS:.proto=.pb.cc} +DEPRECATED_PROTO_GEN_PY := ${DEPRECATED_PROTO_SRCS:.proto=_pb2.py} # The objects corresponding to the source files # These objects will be linked into the final shared library, so we # exclude the tool, example, and test objects. @@ -244,7 +251,7 @@ $(PROTO_GEN_PY): $(PROTO_SRCS) protoc --proto_path=src --python_out=python $(PROTO_SRCS) @echo -proto: $(PROTO_GEN_CC) +proto: $(PROTO_GEN_CC) $(DEPRECATED_PROTO_GEN_CC) $(PROTO_GEN_CC): $(PROTO_SRCS) protoc --proto_path=src --cpp_out=src $(PROTO_SRCS) @@ -252,9 +259,16 @@ $(PROTO_GEN_CC): $(PROTO_SRCS) cp $(PROTO_GEN_HEADER) include/$(PROJECT)/proto/ @echo +$(DEPRECATED_PROTO_GEN_CC): $(DEPRECATED_PROTO_SRCS) + protoc --proto_path=src --cpp_out=src $(DEPRECATED_PROTO_SRCS) + mkdir -p include/$(PROJECT)/proto/deprecated + cp $(DEPRECATED_PROTO_GEN_HEADER) include/$(PROJECT)/proto/deprecated/ + @echo + clean: @- $(RM) $(NAME) $(STATIC_NAME) @- $(RM) $(PROTO_GEN_HEADER) $(PROTO_GEN_CC) $(PROTO_GEN_PY) + @- $(RM) $(DEPRECATED_PROTO_GEN_HEADER) $(DEPRECATED_PROTO_GEN_CC) @- $(RM) include/$(PROJECT)/proto/$(PROJECT).pb.h @- $(RM) python/$(PROJECT)/proto/$(PROJECT)_pb2.py @- $(RM) python/$(PROJECT)/*.so diff --git a/src/caffe/proto/caffe.proto.v0 b/src/caffe/proto/deprecated/caffe.v0.proto similarity index 100% rename from src/caffe/proto/caffe.proto.v0 rename to src/caffe/proto/deprecated/caffe.v0.proto From e681c846d0fb7dd80cdffadaa470cb893d91bd6d Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sat, 15 Mar 2014 01:11:33 -0700 Subject: [PATCH 12/60] add v0->v1 'bridge' proto and add util that uses it --- Makefile | 8 +- include/caffe/util/upgrade_proto.hpp | 25 ++ src/caffe/layers/data_layer.cpp | 6 +- src/caffe/proto/caffe.proto | 2 +- .../{caffe.v0.proto => caffe.proto.v0} | 0 .../deprecated/caffe_v0_to_v1_bridge.proto | 71 +++++ src/caffe/util/upgrade_proto.cpp | 267 ++++++++++++++++++ 7 files changed, 369 insertions(+), 10 deletions(-) create mode 100644 include/caffe/util/upgrade_proto.hpp rename src/caffe/proto/deprecated/{caffe.v0.proto => caffe.proto.v0} (100%) create mode 100644 src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto create mode 100644 src/caffe/util/upgrade_proto.cpp diff --git a/Makefile b/Makefile index 26d37762bb9..ecde1dcba36 100644 --- a/Makefile +++ b/Makefile @@ -253,14 +253,10 @@ $(PROTO_GEN_PY): $(PROTO_SRCS) proto: $(PROTO_GEN_CC) $(DEPRECATED_PROTO_GEN_CC) -$(PROTO_GEN_CC): $(PROTO_SRCS) - protoc --proto_path=src --cpp_out=src $(PROTO_SRCS) +$(PROTO_GEN_CC): $(PROTO_SRCS) $(DEPRECATED_PROTO_SRCS) + protoc --proto_path=src --cpp_out=src $(PROTO_SRCS) $(DEPRECATED_PROTO_SRCS) mkdir -p include/$(PROJECT)/proto cp $(PROTO_GEN_HEADER) include/$(PROJECT)/proto/ - @echo - -$(DEPRECATED_PROTO_GEN_CC): $(DEPRECATED_PROTO_SRCS) - protoc --proto_path=src --cpp_out=src $(DEPRECATED_PROTO_SRCS) mkdir -p include/$(PROJECT)/proto/deprecated cp $(DEPRECATED_PROTO_GEN_HEADER) include/$(PROJECT)/proto/deprecated/ @echo diff --git a/include/caffe/util/upgrade_proto.hpp b/include/caffe/util/upgrade_proto.hpp new file mode 100644 index 00000000000..fc7ac876e87 --- /dev/null +++ b/include/caffe/util/upgrade_proto.hpp @@ -0,0 +1,25 @@ +// Copyright 2014 Jeff Donahue + +#ifndef CAFFE_UTIL_UPGRADE_PROTO_H_ +#define CAFFE_UTIL_UPGRADE_PROTO_H_ + +#include + +#include "google/protobuf/message.h" +#include "caffe/proto/caffe.pb.h" +#include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h" + +#include "boost/scoped_ptr.hpp" +#include "caffe/blob.hpp" + +using std::string; +// using ::google::protobuf::Message; + +namespace caffe { + +bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, + LayerParameter* layer_param); + +} // namespace caffe + +#endif // CAFFE_UTIL_UPGRADE_PROTO_H_ diff --git a/src/caffe/layers/data_layer.cpp b/src/caffe/layers/data_layer.cpp index 7178214f9eb..237ce3779d3 100644 --- a/src/caffe/layers/data_layer.cpp +++ b/src/caffe/layers/data_layer.cpp @@ -189,11 +189,11 @@ void DataLayer::SetUp(const vector*>& bottom, CHECK_GT(datum_height_, crop_size); CHECK_GT(datum_width_, crop_size); // check if we want to have mean - if (this->layer_param_.data_param().has_meanfile()) { + if (this->layer_param_.data_param().has_mean_file()) { BlobProto blob_proto; LOG(INFO) << "Loading mean file from" - << this->layer_param_.data_param().meanfile(); - ReadProtoFromBinaryFile(this->layer_param_.data_param().meanfile().c_str(), + << this->layer_param_.data_param().mean_file(); + ReadProtoFromBinaryFile(this->layer_param_.data_param().mean_file().c_str(), &blob_proto); data_mean_.FromProto(blob_proto); CHECK_EQ(data_mean_.num(), 1); diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index b6cf14451c2..4c6ca5dd788 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -123,7 +123,7 @@ message DataParameter { // data mean, if provided. Note that the mean subtraction is always carried // out before scaling. optional float scale = 2 [default = 1]; - optional string meanfile = 3; + optional string mean_file = 3; // Specify the batch size. optional uint32 batch_size = 4; // Specify if we would like to randomly crop an image. diff --git a/src/caffe/proto/deprecated/caffe.v0.proto b/src/caffe/proto/deprecated/caffe.proto.v0 similarity index 100% rename from src/caffe/proto/deprecated/caffe.v0.proto rename to src/caffe/proto/deprecated/caffe.proto.v0 diff --git a/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto b/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto new file mode 100644 index 00000000000..fd66c2dd5b2 --- /dev/null +++ b/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto @@ -0,0 +1,71 @@ +// Copyright 2013 Yangqing Jia + +import "caffe/proto/caffe.proto"; + +package caffe; + +message V0LayerParameter { + optional string name = 1; // the layer name + optional string type = 2; // the string to specify the layer type + + // Parameters to specify layers with inner products. + optional uint32 num_output = 3; // The number of outputs for the layer + optional bool biasterm = 4 [default = true]; // whether to have bias terms + optional FillerParameter weight_filler = 5; // The filler for the weight + optional FillerParameter bias_filler = 6; // The filler for the bias + + optional uint32 pad = 7 [default = 0]; // The padding size + optional uint32 kernelsize = 8; // The kernel size + optional uint32 group = 9 [default = 1]; // The group size for group conv + optional uint32 stride = 10 [default = 1]; // The stride + enum PoolMethod { + MAX = 0; + AVE = 1; + STOCHASTIC = 2; + } + optional PoolMethod pool = 11 [default = MAX]; // The pooling method + optional float dropout_ratio = 12 [default = 0.5]; // dropout ratio + + optional uint32 local_size = 13 [default = 5]; // for local response norm + optional float alpha = 14 [default = 1.]; // for local response norm + optional float beta = 15 [default = 0.75]; // for local response norm + + // For data layers, specify the data source + optional string source = 16; + // For data pre-processing, we can do simple scaling and subtracting the + // data mean, if provided. Note that the mean subtraction is always carried + // out before scaling. + optional float scale = 17 [default = 1]; + optional string meanfile = 18; + // For data layers, specify the batch size. + optional uint32 batchsize = 19; + // For data layers, specify if we would like to randomly crop an image. + optional uint32 cropsize = 20 [default = 0]; + // For data layers, specify if we want to randomly mirror data. + optional bool mirror = 21 [default = false]; + + // The blobs containing the numeric parameters of the layer + repeated BlobProto blobs = 50; + // The ratio that is multiplied on the global learning rate. If you want to set + // the learning ratio for one blob, you need to set it for all blobs. + repeated float blobs_lr = 51; + // The weight decay that is multiplied on the global weight decay. + repeated float weight_decay = 52; + + // The rand_skip variable is for the data layer to skip a few data points + // to avoid all asynchronous sgd clients to start at the same point. The skip + // point would be set as rand_skip * rand(0,1). Note that rand_skip should not + // be larger than the number of keys in the leveldb. + optional uint32 rand_skip = 53 [default = 0]; + + // Concat Layer needs to specify the dimension along the concat will happen, + // the other dimensions must be the same for all the bottom blobs + // By default it will concatenate blobs along channels dimension + optional uint32 concat_dim = 65 [default = 1]; +} + +message V0LayerConnection { + optional V0LayerParameter layer = 1; // the layer parameter + repeated string bottom = 2; // the name of the bottom blobs + repeated string top = 3; // the name of the top blobs +} diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp new file mode 100644 index 00000000000..8b9b17fa7ca --- /dev/null +++ b/src/caffe/util/upgrade_proto.cpp @@ -0,0 +1,267 @@ +// Copyright 2014 Jeff Donahue + +#include +#include +#include +#include +#include + +#include +#include +#include // NOLINT(readability/streams) + +#include "caffe/common.hpp" +#include "caffe/util/upgrade_proto.hpp" +#include "caffe/proto/caffe.pb.h" +#include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h" + +using std::fstream; +using std::ios; +using std::max; +using std::string; + +namespace caffe { + +bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, + LayerParameter* layer_param) { + bool full_compatibility = true; + layer_param->Clear(); + for (int i = 0; i < v0_layer_connection.bottom_size(); ++i) { + layer_param->add_bottom(v0_layer_connection.bottom(i)); + } + for (int i = 0; i < v0_layer_connection.top_size(); ++i) { + layer_param->add_top(v0_layer_connection.top(i)); + } + if (v0_layer_connection.has_layer()) { + const V0LayerParameter& v0_layer_param = v0_layer_connection.layer(); + if (v0_layer_param.has_name()) { + layer_param->set_name(v0_layer_param.name()); + } + const string& type = v0_layer_param.type(); + if (v0_layer_param.has_num_output()) { + if (type == "conv") { + layer_param->mutable_convolution_param()->set_num_output( + v0_layer_param.num_output()); + } else if (type == "innerproduct") { + layer_param->mutable_inner_product_param()->set_num_output( + v0_layer_param.num_output()); + } else { + LOG(ERROR) << "Unknown parameter num_output for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_biasterm()) { + if (type == "conv") { + layer_param->mutable_convolution_param()->set_bias_term( + v0_layer_param.biasterm()); + } else if (type == "innerproduct") { + layer_param->mutable_inner_product_param()->set_bias_term( + v0_layer_param.biasterm()); + } else { + LOG(ERROR) << "Unknown parameter biasterm for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_weight_filler()) { + if (type == "conv") { + layer_param->mutable_convolution_param()-> + mutable_weight_filler()->CopyFrom(v0_layer_param.weight_filler()); + } else if (type == "innerproduct") { + layer_param->mutable_inner_product_param()-> + mutable_weight_filler()->CopyFrom(v0_layer_param.weight_filler()); + } else { + LOG(ERROR) << "Unknown parameter weight_filler for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_bias_filler()) { + if (type == "conv") { + layer_param->mutable_convolution_param()-> + mutable_bias_filler()->CopyFrom(v0_layer_param.bias_filler()); + } else if (type == "innerproduct") { + layer_param->mutable_inner_product_param()-> + mutable_bias_filler()->CopyFrom(v0_layer_param.bias_filler()); + } else { + LOG(ERROR) << "Unknown parameter bias_filler for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_pad()) { + if (type == "conv") { + layer_param->mutable_convolution_param()->set_pad(v0_layer_param.pad()); + } else { + LOG(ERROR) << "Unknown parameter pad for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_kernelsize()) { + if (type == "conv") { + layer_param->mutable_convolution_param()->set_kernel_size( + v0_layer_param.kernelsize()); + } else if (type == "pool") { + layer_param->mutable_pooling_param()->set_kernel_size( + v0_layer_param.kernelsize()); + } else { + LOG(ERROR) << "Unknown parameter kernelsize for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_stride()) { + if (type == "conv") { + layer_param->mutable_convolution_param()->set_stride( + v0_layer_param.stride()); + } else if (type == "pool") { + layer_param->mutable_pooling_param()->set_stride( + v0_layer_param.stride()); + } else { + LOG(ERROR) << "Unknown parameter stride for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_pool()) { + if (type == "pool") { + V0LayerParameter_PoolMethod pool = v0_layer_param.pool(); + switch (pool) { + V0LayerParameter_PoolMethod_MAX: + layer_param->mutable_pooling_param()->set_pool( + PoolingParameter_PoolMethod_MAX); + break; + V0LayerParameter_PoolMethod_AVE: + layer_param->mutable_pooling_param()->set_pool( + PoolingParameter_PoolMethod_AVE); + break; + V0LayerParameter_PoolMethod_STOCHASTIC: + layer_param->mutable_pooling_param()->set_pool( + PoolingParameter_PoolMethod_STOCHASTIC); + break; + default: + LOG(ERROR) << "Unknown pool method " << pool; + full_compatibility = false; + } + } else { + LOG(ERROR) << "Unknown parameter pool for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_dropout_ratio()) { + if (type == "dropout_ratio") { + layer_param->mutable_dropout_param()->set_dropout_ratio( + v0_layer_param.dropout_ratio()); + } else { + LOG(ERROR) << "Unknown parameter dropout_ratio for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_local_size()) { + if (type == "lrn") { + layer_param->mutable_lrn_param()->set_local_size( + v0_layer_param.local_size()); + } else { + LOG(ERROR) << "Unknown parameter local_size for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_alpha()) { + if (type == "lrn") { + layer_param->mutable_lrn_param()->set_alpha(v0_layer_param.alpha()); + } else { + LOG(ERROR) << "Unknown parameter alpha for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_beta()) { + if (type == "lrn") { + layer_param->mutable_lrn_param()->set_beta(v0_layer_param.beta()); + } else { + LOG(ERROR) << "Unknown parameter beta for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_source()) { + if (type == "data") { + layer_param->mutable_data_param()->set_source(v0_layer_param.source()); + } else if (type == "hdf5_data") { + layer_param->mutable_hdf5_data_param()->set_source( + v0_layer_param.source()); + } else if (type == "infogain_loss") { + layer_param->mutable_infogain_loss_param()->set_source( + v0_layer_param.source()); + } else { + LOG(ERROR) << "Unknown parameter source for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_scale()) { + if (type == "data") { + layer_param->mutable_data_param()->set_scale(v0_layer_param.scale()); + } else { + LOG(ERROR) << "Unknown parameter scale for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_meanfile()) { + if (type == "data") { + layer_param->mutable_data_param()->set_mean_file(v0_layer_param.meanfile()); + } else { + LOG(ERROR) << "Unknown parameter meanfile for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_batchsize()) { + if (type == "data") { + layer_param->mutable_data_param()->set_batch_size( + v0_layer_param.batchsize()); + } else { + LOG(ERROR) << "Unknown parameter batchsize for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_cropsize()) { + if (type == "data") { + layer_param->mutable_data_param()->set_crop_size( + v0_layer_param.cropsize()); + } else { + LOG(ERROR) << "Unknown parameter cropsize for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_mirror()) { + if (type == "data") { + layer_param->mutable_data_param()->set_mirror(v0_layer_param.mirror()); + } else { + LOG(ERROR) << "Unknown parameter mirror for layer type " << type; + full_compatibility = false; + } + } + for (int i = 0; i < v0_layer_param.blobs_size(); ++i) { + layer_param->add_blobs()->CopyFrom(v0_layer_param.blobs(i)); + } + for (int i = 0; i < v0_layer_param.blobs_lr_size(); ++i) { + layer_param->add_blobs_lr(v0_layer_param.blobs_lr(i)); + } + for (int i = 0; i < v0_layer_param.weight_decay_size(); ++i) { + layer_param->add_weight_decay(v0_layer_param.weight_decay(i)); + } + if (v0_layer_param.has_rand_skip()) { + if (type == "data") { + layer_param->mutable_data_param()->set_rand_skip( + v0_layer_param.rand_skip()); + } else { + LOG(ERROR) << "Unknown parameter rand_skip for layer type " << type; + full_compatibility = false; + } + } + if (v0_layer_param.has_concat_dim()) { + if (type == "concat") { + layer_param->mutable_concat_param()->set_concat_dim( + v0_layer_param.concat_dim()); + } else { + LOG(ERROR) << "Unknown parameter concat_dim for layer type " << type; + full_compatibility = false; + } + } + } + return full_compatibility; +} + +} // namespace caffe From c6d47bca6a812e703b956ef438536305553dd604 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sat, 15 Mar 2014 01:44:03 -0700 Subject: [PATCH 13/60] caffe.proto: layer->layers --- src/caffe/net.cpp | 20 ++++++++++---------- src/caffe/proto/caffe.proto | 2 +- src/caffe/util/insert_splits.cpp | 16 ++++++++-------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 22aa00485cc..8a95a3d6892 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -38,7 +38,7 @@ void Net::Init(const NetParameter& in_param) { name_ = param.name(); map blob_name_to_idx; set available_blobs; - int num_layers = param.layer_size(); + int num_layers = param.layers_size(); CHECK_EQ(param.input_size() * 4, param.input_dim_size()) << "Incorrect bottom blob dimension specifications."; size_t memory_used = 0; @@ -61,13 +61,13 @@ void Net::Init(const NetParameter& in_param) { } DLOG(INFO) << "Memory required for Data" << memory_used*sizeof(Dtype); // For each layer, set up their input and output - bottom_vecs_.resize(param.layer_size()); - top_vecs_.resize(param.layer_size()); - bottom_id_vecs_.resize(param.layer_size()); - top_id_vecs_.resize(param.layer_size()); - for (int i = 0; i < param.layer_size(); ++i) { + bottom_vecs_.resize(param.layers_size()); + top_vecs_.resize(param.layers_size()); + bottom_id_vecs_.resize(param.layers_size()); + top_id_vecs_.resize(param.layers_size()); + for (int i = 0; i < param.layers_size(); ++i) { bool in_place = false; - const LayerParameter& layer_param = param.layer(i); + const LayerParameter& layer_param = param.layers(i); layers_.push_back(shared_ptr >(GetLayer(layer_param))); layer_names_.push_back(layer_param.name()); LOG(INFO) << "Creating Layer " << layer_param.name(); @@ -269,9 +269,9 @@ void Net::Backward() { template void Net::CopyTrainedLayersFrom(const NetParameter& param) { - int num_source_layers = param.layer_size(); + int num_source_layers = param.layers_size(); for (int i = 0; i < num_source_layers; ++i) { - const LayerParameter& source_layer = param.layer(i); + const LayerParameter& source_layer = param.layers(i); const string& source_layer_name = source_layer.name(); int target_layer_id = 0; while (target_layer_id != layer_names_.size() && @@ -314,7 +314,7 @@ void Net::ToProto(NetParameter* param, bool write_diff) { } DLOG(INFO) << "Serializing " << layers_.size() << " layers"; for (int i = 0; i < layers_.size(); ++i) { - LayerParameter* layer_param = param->add_layer(); + LayerParameter* layer_param = param->add_layers(); for (int j = 0; j < bottom_id_vecs_[i].size(); ++j) { layer_param->add_bottom(blob_names_[bottom_id_vecs_[i][j]]); } diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 4c6ca5dd788..b4872da1084 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -40,7 +40,7 @@ message FillerParameter { message NetParameter { optional string name = 1; // consider giving the network a name - repeated LayerParameter layer = 2; // a bunch of layers. + repeated LayerParameter layers = 2; // a bunch of layers. // The input blobs to the network. repeated string input = 3; // The dim of the input blobs. For each input blob there should be four diff --git a/src/caffe/util/insert_splits.cpp b/src/caffe/util/insert_splits.cpp index 2c8bdf17700..0e8ce07d145 100644 --- a/src/caffe/util/insert_splits.cpp +++ b/src/caffe/util/insert_splits.cpp @@ -18,7 +18,7 @@ namespace caffe { void insert_splits(const NetParameter& param, NetParameter* param_split) { // Initialize by copying from the input NetParameter. param_split->CopyFrom(param); - param_split->clear_layer(); + param_split->clear_layers(); map > blob_name_to_last_top_idx; map, pair > bottom_idx_to_source_top_idx; map, int> top_idx_to_bottom_count; @@ -30,8 +30,8 @@ void insert_splits(const NetParameter& param, NetParameter* param_split) { const string& blob_name = param.input(i); blob_name_to_last_top_idx[blob_name] = make_pair(-1, i); } - for (int i = 0; i < param.layer_size(); ++i) { - const LayerParameter& layer_param = param.layer(i); + for (int i = 0; i < param.layers_size(); ++i) { + const LayerParameter& layer_param = param.layers(i); layer_idx_to_layer_name[i] = layer_param.name(); for (int j = 0; j < layer_param.bottom_size(); ++j) { const string& blob_name = layer_param.bottom(j); @@ -56,14 +56,14 @@ void insert_splits(const NetParameter& param, NetParameter* param_split) { if (split_count > 1) { const string& layer_name = layer_idx_to_layer_name[-1]; const string& blob_name = param.input(i); - LayerParameter* split_layer_param = param_split->add_layer(); + LayerParameter* split_layer_param = param_split->add_layers(); configure_split_layer(layer_name, blob_name, i, split_count, split_layer_param); } } - for (int i = 0; i < param.layer_size(); ++i) { - LayerParameter* layer_param = param_split->add_layer(); - layer_param->CopyFrom(param.layer(i)); + for (int i = 0; i < param.layers_size(); ++i) { + LayerParameter* layer_param = param_split->add_layers(); + layer_param->CopyFrom(param.layers(i)); // Replace any shared bottom blobs with split layer outputs. for (int j = 0; j < layer_param->bottom_size(); ++j) { const pair& top_idx = @@ -83,7 +83,7 @@ void insert_splits(const NetParameter& param, NetParameter* param_split) { if (split_count > 1) { const string& layer_name = layer_idx_to_layer_name[i]; const string& blob_name = layer_param->top(j); - LayerParameter* split_layer_param = param_split->add_layer(); + LayerParameter* split_layer_param = param_split->add_layers(); configure_split_layer(layer_name, blob_name, j, split_count, split_layer_param); } From 69ddcc84a6c014bb1bb3d1d50ac52c75f26fc5ac Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sat, 15 Mar 2014 02:15:44 -0700 Subject: [PATCH 14/60] add V0NetParameter and UpgradeV0Net --- include/caffe/util/upgrade_proto.hpp | 2 ++ .../proto/deprecated/caffe_v0_to_v1_bridge.proto | 15 +++++++++++++++ src/caffe/util/upgrade_proto.cpp | 13 +++++++++++++ 3 files changed, 30 insertions(+) diff --git a/include/caffe/util/upgrade_proto.hpp b/include/caffe/util/upgrade_proto.hpp index fc7ac876e87..8b938294ad3 100644 --- a/include/caffe/util/upgrade_proto.hpp +++ b/include/caffe/util/upgrade_proto.hpp @@ -17,6 +17,8 @@ using std::string; namespace caffe { +bool UpgradeV0Net(const V0NetParameter& v0_net_param, NetParameter* net_param); + bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, LayerParameter* layer_param); diff --git a/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto b/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto index fd66c2dd5b2..782899ca06a 100644 --- a/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto +++ b/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto @@ -4,6 +4,21 @@ import "caffe/proto/caffe.proto"; package caffe; +message V0NetParameter { + optional string name = 1; // consider giving the network a name + repeated V0LayerConnection layers = 2; // a bunch of layers. + // The input blobs to the network. + repeated string input = 3; + // The dim of the input blobs. For each input blob there should be four + // values specifying the num, channels, height and width of the input blob. + // Thus, there should be a total of (4 * #input) numbers. + repeated int32 input_dim = 4; + // Whether the network will force every layer to carry out backward operation. + // If set False, then whether to carry out backward is determined + // automatically according to the net structure and learning rates. + optional bool force_backward = 5 [default = false]; +} + message V0LayerParameter { optional string name = 1; // the layer name optional string type = 2; // the string to specify the layer type diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 8b9b17fa7ca..4bd54dde3e9 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -22,6 +22,19 @@ using std::string; namespace caffe { +bool UpgradeV0Net(const V0NetParameter& v0_net_param, + NetParameter* net_param) { + bool full_compatibility = true; + net_param->Clear(); + if (v0_net_param.has_name()) { + net_param->set_name(v0_net_param.name()); + } + for (int i = 0; i < v0_net_param.layers_size(); ++i) { + full_compatibility &= UpgradeV0LayerConnection(v0_net_param.layers(i), + net_param->add_layers()); + } +} + bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, LayerParameter* layer_param) { bool full_compatibility = true; From be8d83603c559cb46170442c3c0cbd663492db05 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sat, 15 Mar 2014 02:33:40 -0700 Subject: [PATCH 15/60] make ReadProtoFromTextFile not die on parse failure; add ReadProtoFromTextFileOrDie which has the old functionality --- include/caffe/util/io.hpp | 17 ++++++++++++----- include/caffe/util/upgrade_proto.hpp | 1 - src/caffe/net.cpp | 2 +- src/caffe/solver.cpp | 4 ++-- src/caffe/util/io.cpp | 7 ++++--- tools/dump_network.cpp | 2 +- tools/finetune_net.cpp | 2 +- tools/net_speed_benchmark.cpp | 2 +- tools/test_net.cpp | 2 +- tools/train_net.cpp | 2 +- 10 files changed, 24 insertions(+), 17 deletions(-) diff --git a/include/caffe/util/io.hpp b/include/caffe/util/io.hpp index 89f9c18429e..11201c13d97 100644 --- a/include/caffe/util/io.hpp +++ b/include/caffe/util/io.hpp @@ -19,11 +19,18 @@ using ::google::protobuf::Message; namespace caffe { -void ReadProtoFromTextFile(const char* filename, - Message* proto); -inline void ReadProtoFromTextFile(const string& filename, - Message* proto) { - ReadProtoFromTextFile(filename.c_str(), proto); +bool ReadProtoFromTextFile(const char* filename, Message* proto); + +inline bool ReadProtoFromTextFile(const string& filename, Message* proto) { + return ReadProtoFromTextFile(filename.c_str(), proto); +} + +inline void ReadProtoFromTextFileOrDie(const char* filename, Message* proto) { + CHECK(ReadProtoFromTextFile(filename, proto)); +} + +inline void ReadProtoFromTextFileOrDie(const string& filename, Message* proto) { + ReadProtoFromTextFileOrDie(filename.c_str(), proto); } void WriteProtoToTextFile(const Message& proto, const char* filename); diff --git a/include/caffe/util/upgrade_proto.hpp b/include/caffe/util/upgrade_proto.hpp index 8b938294ad3..ed9a82982fd 100644 --- a/include/caffe/util/upgrade_proto.hpp +++ b/include/caffe/util/upgrade_proto.hpp @@ -13,7 +13,6 @@ #include "caffe/blob.hpp" using std::string; -// using ::google::protobuf::Message; namespace caffe { diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 8a95a3d6892..7310a1ae079 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -25,7 +25,7 @@ Net::Net(const NetParameter& param) { template Net::Net(const string& param_file) { NetParameter param; - ReadProtoFromTextFile(param_file, ¶m); + ReadProtoFromTextFileOrDie(param_file, ¶m); Init(param); } diff --git a/src/caffe/solver.cpp b/src/caffe/solver.cpp index 5fa150cbc07..6d31290c806 100644 --- a/src/caffe/solver.cpp +++ b/src/caffe/solver.cpp @@ -22,13 +22,13 @@ Solver::Solver(const SolverParameter& param) : param_(param), net_(), test_net_() { // Scaffolding code NetParameter train_net_param; - ReadProtoFromTextFile(param_.train_net(), &train_net_param); + ReadProtoFromTextFileOrDie(param_.train_net(), &train_net_param); LOG(INFO) << "Creating training net."; net_.reset(new Net(train_net_param)); if (param_.has_test_net()) { LOG(INFO) << "Creating testing net."; NetParameter test_net_param; - ReadProtoFromTextFile(param_.test_net(), &test_net_param); + ReadProtoFromTextFileOrDie(param_.test_net(), &test_net_param); test_net_.reset(new Net(test_net_param)); CHECK_GT(param_.test_iter(), 0); CHECK_GT(param_.test_interval(), 0); diff --git a/src/caffe/util/io.cpp b/src/caffe/util/io.cpp index fdad21d6cac..fe452674dc8 100644 --- a/src/caffe/util/io.cpp +++ b/src/caffe/util/io.cpp @@ -29,17 +29,18 @@ using google::protobuf::io::ZeroCopyInputStream; using google::protobuf::io::CodedInputStream; using google::protobuf::io::ZeroCopyOutputStream; using google::protobuf::io::CodedOutputStream; +using google::protobuf::Message; namespace caffe { -void ReadProtoFromTextFile(const char* filename, - ::google::protobuf::Message* proto) { +bool ReadProtoFromTextFile(const char* filename, Message* proto) { int fd = open(filename, O_RDONLY); CHECK_NE(fd, -1) << "File not found: " << filename; FileInputStream* input = new FileInputStream(fd); - CHECK(google::protobuf::TextFormat::Parse(input, proto)); + bool success = google::protobuf::TextFormat::Parse(input, proto); delete input; close(fd); + return success; } void WriteProtoToTextFile(const Message& proto, const char* filename) { diff --git a/tools/dump_network.cpp b/tools/dump_network.cpp index f5c36820083..cee8bc85ea1 100644 --- a/tools/dump_network.cpp +++ b/tools/dump_network.cpp @@ -38,7 +38,7 @@ int main(int argc, char** argv) { // We directly load the net param from trained file ReadProtoFromBinaryFile(argv[2], &net_param); } else { - ReadProtoFromTextFile(argv[1], &net_param); + ReadProtoFromTextFileOrDie(argv[1], &net_param); } ReadProtoFromBinaryFile(argv[2], &trained_net_param); diff --git a/tools/finetune_net.cpp b/tools/finetune_net.cpp index db96b02ba87..c1cd788a1fb 100644 --- a/tools/finetune_net.cpp +++ b/tools/finetune_net.cpp @@ -20,7 +20,7 @@ int main(int argc, char** argv) { } SolverParameter solver_param; - ReadProtoFromTextFile(argv[1], &solver_param); + ReadProtoFromTextFileOrDie(argv[1], &solver_param); LOG(INFO) << "Starting Optimization"; SGDSolver solver(solver_param); diff --git a/tools/net_speed_benchmark.cpp b/tools/net_speed_benchmark.cpp index f52aac9cba7..d6c0cdbd439 100644 --- a/tools/net_speed_benchmark.cpp +++ b/tools/net_speed_benchmark.cpp @@ -50,7 +50,7 @@ int main(int argc, char** argv) { Caffe::set_phase(Caffe::TRAIN); NetParameter net_param; - ReadProtoFromTextFile(argv[1], + ReadProtoFromTextFileOrDie(argv[1], &net_param); Net caffe_net(net_param); diff --git a/tools/test_net.cpp b/tools/test_net.cpp index 0abfbf68b27..0b49e54a210 100644 --- a/tools/test_net.cpp +++ b/tools/test_net.cpp @@ -35,7 +35,7 @@ int main(int argc, char** argv) { } NetParameter test_net_param; - ReadProtoFromTextFile(argv[1], &test_net_param); + ReadProtoFromTextFileOrDie(argv[1], &test_net_param); Net caffe_test_net(test_net_param); NetParameter trained_net_param; ReadProtoFromBinaryFile(argv[2], &trained_net_param); diff --git a/tools/train_net.cpp b/tools/train_net.cpp index 751a70445e1..7c6f23e6240 100644 --- a/tools/train_net.cpp +++ b/tools/train_net.cpp @@ -21,7 +21,7 @@ int main(int argc, char** argv) { } SolverParameter solver_param; - ReadProtoFromTextFile(argv[1], &solver_param); + ReadProtoFromTextFileOrDie(argv[1], &solver_param); LOG(INFO) << "Starting Optimization"; SGDSolver solver(solver_param); From 62d7f5c7c0806b7af40a58022f7c5d94db34412a Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sat, 15 Mar 2014 04:04:18 -0700 Subject: [PATCH 16/60] add deprecated protos to PROTO_OBJS in makefile so things compile; other minor cleanup of includes etc --- Makefile | 2 +- include/caffe/util/upgrade_proto.hpp | 8 ---- src/caffe/net.cpp | 19 ++++++++- src/caffe/util/upgrade_proto.cpp | 64 ++++++++++++++-------------- 4 files changed, 51 insertions(+), 42 deletions(-) diff --git a/Makefile b/Makefile index ecde1dcba36..b54e3173b65 100644 --- a/Makefile +++ b/Makefile @@ -73,6 +73,7 @@ DEPRECATED_PROTO_GEN_PY := ${DEPRECATED_PROTO_SRCS:.proto=_pb2.py} CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o}) CU_OBJS := $(addprefix $(BUILD_DIR)/, ${CU_SRCS:.cu=.cuo}) PROTO_OBJS := $(addprefix $(BUILD_DIR)/, ${PROTO_GEN_CC:.cc=.o}) +PROTO_OBJS += $(addprefix $(BUILD_DIR)/, ${DEPRECATED_PROTO_GEN_CC:.cc=.o}) OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS) # tool, example, and test objects TOOL_OBJS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o}) @@ -264,7 +265,6 @@ $(PROTO_GEN_CC): $(PROTO_SRCS) $(DEPRECATED_PROTO_SRCS) clean: @- $(RM) $(NAME) $(STATIC_NAME) @- $(RM) $(PROTO_GEN_HEADER) $(PROTO_GEN_CC) $(PROTO_GEN_PY) - @- $(RM) $(DEPRECATED_PROTO_GEN_HEADER) $(DEPRECATED_PROTO_GEN_CC) @- $(RM) include/$(PROJECT)/proto/$(PROJECT).pb.h @- $(RM) python/$(PROJECT)/proto/$(PROJECT)_pb2.py @- $(RM) python/$(PROJECT)/*.so diff --git a/include/caffe/util/upgrade_proto.hpp b/include/caffe/util/upgrade_proto.hpp index ed9a82982fd..35de7360a6e 100644 --- a/include/caffe/util/upgrade_proto.hpp +++ b/include/caffe/util/upgrade_proto.hpp @@ -3,17 +3,9 @@ #ifndef CAFFE_UTIL_UPGRADE_PROTO_H_ #define CAFFE_UTIL_UPGRADE_PROTO_H_ -#include - -#include "google/protobuf/message.h" #include "caffe/proto/caffe.pb.h" #include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h" -#include "boost/scoped_ptr.hpp" -#include "caffe/blob.hpp" - -using std::string; - namespace caffe { bool UpgradeV0Net(const V0NetParameter& v0_net_param, NetParameter* net_param); diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 7310a1ae079..a16afdc5323 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -5,11 +5,14 @@ #include #include +#include "caffe/common.hpp" #include "caffe/proto/caffe.pb.h" +#include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h" #include "caffe/layer.hpp" #include "caffe/net.hpp" #include "caffe/util/io.hpp" #include "caffe/util/insert_splits.hpp" +#include "caffe/util/upgrade_proto.hpp" using std::pair; using std::map; @@ -25,7 +28,21 @@ Net::Net(const NetParameter& param) { template Net::Net(const string& param_file) { NetParameter param; - ReadProtoFromTextFileOrDie(param_file, ¶m); + if (!ReadProtoFromTextFile(param_file, ¶m)) { + // Failed to parse file as NetParameter; try to parse as a V0NetParameter + // instead. + V0NetParameter v0_param; + CHECK(ReadProtoFromTextFile(param_file, &v0_param)) + << "Failed to parse NetParameter file: " << param_file; + LOG(ERROR) << "Parsed file as V0NetParameter: " << param_file; + LOG(ERROR) << "Note that future Caffe releases will not support " + << "V0NetParameter; use tools/upgrade_net_param.testbin to upgrade " + << "this and any other network proto files to the new format."; + if (!UpgradeV0Net(v0_param, ¶m)) { + LOG(ERROR) << "Warning: had one or more problems upgrading " + << "V0NetParameter to NetParameter (see above); continuing anyway."; + } + } Init(param); } diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 4bd54dde3e9..7f7213590e6 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -1,43 +1,43 @@ // Copyright 2014 Jeff Donahue -#include -#include #include #include #include -#include #include -#include // NOLINT(readability/streams) #include "caffe/common.hpp" #include "caffe/util/upgrade_proto.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h" -using std::fstream; -using std::ios; -using std::max; using std::string; namespace caffe { bool UpgradeV0Net(const V0NetParameter& v0_net_param, NetParameter* net_param) { - bool full_compatibility = true; + bool is_fully_compatible = true; net_param->Clear(); if (v0_net_param.has_name()) { net_param->set_name(v0_net_param.name()); } for (int i = 0; i < v0_net_param.layers_size(); ++i) { - full_compatibility &= UpgradeV0LayerConnection(v0_net_param.layers(i), + is_fully_compatible &= UpgradeV0LayerConnection(v0_net_param.layers(i), net_param->add_layers()); } + for (int i = 0; i < v0_net_param.input_dim_size(); ++i) { + net_param->add_input_dim(v0_net_param.input_dim(i)); + } + if (v0_net_param.has_force_backward()) { + net_param->set_force_backward(v0_net_param.force_backward()); + } + return is_fully_compatible; } bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, LayerParameter* layer_param) { - bool full_compatibility = true; + bool is_fully_compatible = true; layer_param->Clear(); for (int i = 0; i < v0_layer_connection.bottom_size(); ++i) { layer_param->add_bottom(v0_layer_connection.bottom(i)); @@ -60,7 +60,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, v0_layer_param.num_output()); } else { LOG(ERROR) << "Unknown parameter num_output for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_biasterm()) { @@ -72,7 +72,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, v0_layer_param.biasterm()); } else { LOG(ERROR) << "Unknown parameter biasterm for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_weight_filler()) { @@ -84,7 +84,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, mutable_weight_filler()->CopyFrom(v0_layer_param.weight_filler()); } else { LOG(ERROR) << "Unknown parameter weight_filler for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_bias_filler()) { @@ -96,7 +96,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, mutable_bias_filler()->CopyFrom(v0_layer_param.bias_filler()); } else { LOG(ERROR) << "Unknown parameter bias_filler for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_pad()) { @@ -104,7 +104,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, layer_param->mutable_convolution_param()->set_pad(v0_layer_param.pad()); } else { LOG(ERROR) << "Unknown parameter pad for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_kernelsize()) { @@ -116,7 +116,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, v0_layer_param.kernelsize()); } else { LOG(ERROR) << "Unknown parameter kernelsize for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_stride()) { @@ -128,7 +128,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, v0_layer_param.stride()); } else { LOG(ERROR) << "Unknown parameter stride for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_pool()) { @@ -149,11 +149,11 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, break; default: LOG(ERROR) << "Unknown pool method " << pool; - full_compatibility = false; + is_fully_compatible = false; } } else { LOG(ERROR) << "Unknown parameter pool for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_dropout_ratio()) { @@ -162,7 +162,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, v0_layer_param.dropout_ratio()); } else { LOG(ERROR) << "Unknown parameter dropout_ratio for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_local_size()) { @@ -171,7 +171,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, v0_layer_param.local_size()); } else { LOG(ERROR) << "Unknown parameter local_size for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_alpha()) { @@ -179,7 +179,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, layer_param->mutable_lrn_param()->set_alpha(v0_layer_param.alpha()); } else { LOG(ERROR) << "Unknown parameter alpha for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_beta()) { @@ -187,7 +187,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, layer_param->mutable_lrn_param()->set_beta(v0_layer_param.beta()); } else { LOG(ERROR) << "Unknown parameter beta for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_source()) { @@ -201,7 +201,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, v0_layer_param.source()); } else { LOG(ERROR) << "Unknown parameter source for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_scale()) { @@ -209,7 +209,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, layer_param->mutable_data_param()->set_scale(v0_layer_param.scale()); } else { LOG(ERROR) << "Unknown parameter scale for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_meanfile()) { @@ -217,7 +217,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, layer_param->mutable_data_param()->set_mean_file(v0_layer_param.meanfile()); } else { LOG(ERROR) << "Unknown parameter meanfile for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_batchsize()) { @@ -226,7 +226,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, v0_layer_param.batchsize()); } else { LOG(ERROR) << "Unknown parameter batchsize for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_cropsize()) { @@ -235,7 +235,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, v0_layer_param.cropsize()); } else { LOG(ERROR) << "Unknown parameter cropsize for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_mirror()) { @@ -243,7 +243,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, layer_param->mutable_data_param()->set_mirror(v0_layer_param.mirror()); } else { LOG(ERROR) << "Unknown parameter mirror for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } for (int i = 0; i < v0_layer_param.blobs_size(); ++i) { @@ -261,7 +261,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, v0_layer_param.rand_skip()); } else { LOG(ERROR) << "Unknown parameter rand_skip for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } if (v0_layer_param.has_concat_dim()) { @@ -270,11 +270,11 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, v0_layer_param.concat_dim()); } else { LOG(ERROR) << "Unknown parameter concat_dim for layer type " << type; - full_compatibility = false; + is_fully_compatible = false; } } } - return full_compatibility; + return is_fully_compatible; } } // namespace caffe From 7e25c8ce40bd16ff240393f2c663069565f9fb08 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sat, 15 Mar 2014 04:12:45 -0700 Subject: [PATCH 17/60] make solver use upgrade_proto (by constructing net with a string) and fix upgrade_proto bugs --- src/caffe/solver.cpp | 8 ++------ src/caffe/util/upgrade_proto.cpp | 9 ++++++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/caffe/solver.cpp b/src/caffe/solver.cpp index 6d31290c806..2954bf96478 100644 --- a/src/caffe/solver.cpp +++ b/src/caffe/solver.cpp @@ -21,15 +21,11 @@ template Solver::Solver(const SolverParameter& param) : param_(param), net_(), test_net_() { // Scaffolding code - NetParameter train_net_param; - ReadProtoFromTextFileOrDie(param_.train_net(), &train_net_param); LOG(INFO) << "Creating training net."; - net_.reset(new Net(train_net_param)); + net_.reset(new Net(param_.train_net())); if (param_.has_test_net()) { LOG(INFO) << "Creating testing net."; - NetParameter test_net_param; - ReadProtoFromTextFileOrDie(param_.test_net(), &test_net_param); - test_net_.reset(new Net(test_net_param)); + test_net_.reset(new Net(param_.test_net())); CHECK_GT(param_.test_iter(), 0); CHECK_GT(param_.test_interval(), 0); } diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 7f7213590e6..9645639c06c 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -51,6 +51,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, layer_param->set_name(v0_layer_param.name()); } const string& type = v0_layer_param.type(); + if (v0_layer_param.has_type()) { + layer_param->set_type(type); + } if (v0_layer_param.has_num_output()) { if (type == "conv") { layer_param->mutable_convolution_param()->set_num_output( @@ -135,15 +138,15 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, if (type == "pool") { V0LayerParameter_PoolMethod pool = v0_layer_param.pool(); switch (pool) { - V0LayerParameter_PoolMethod_MAX: + case V0LayerParameter_PoolMethod_MAX: layer_param->mutable_pooling_param()->set_pool( PoolingParameter_PoolMethod_MAX); break; - V0LayerParameter_PoolMethod_AVE: + case V0LayerParameter_PoolMethod_AVE: layer_param->mutable_pooling_param()->set_pool( PoolingParameter_PoolMethod_AVE); break; - V0LayerParameter_PoolMethod_STOCHASTIC: + case V0LayerParameter_PoolMethod_STOCHASTIC: layer_param->mutable_pooling_param()->set_pool( PoolingParameter_PoolMethod_STOCHASTIC); break; From 17764c3dba0cd8017dcb4e9ee709da05c9206926 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 01:21:06 -0700 Subject: [PATCH 18/60] function to upgrade padding layers --- include/caffe/util/upgrade_proto.hpp | 10 ++++++ src/caffe/util/upgrade_proto.cpp | 49 +++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/include/caffe/util/upgrade_proto.hpp b/include/caffe/util/upgrade_proto.hpp index 35de7360a6e..b2504a30a8e 100644 --- a/include/caffe/util/upgrade_proto.hpp +++ b/include/caffe/util/upgrade_proto.hpp @@ -8,8 +8,18 @@ namespace caffe { +// Perform all necessary transformations to upgrade a V0NetParameter into a +// NetParameter (including upgrading padding layers and LayerParameters). bool UpgradeV0Net(const V0NetParameter& v0_net_param, NetParameter* net_param); +// Upgrade V0NetParameter with padding layers to pad-aware conv layers. +// For any padding layer, remove it and put its pad parameter in any layers +// taking its top blob as input. +// Error if any of these above layers are not-conv layers. +void UpgradeV0PaddingLayers(const V0NetParameter& param, + V0NetParameter* param_upgraded_pad); + +// Upgrade a single V0LayerConnection to the new LayerParameter format. bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, LayerParameter* layer_param); diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 9645639c06c..b6faca52f5f 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -11,12 +11,17 @@ #include "caffe/proto/caffe.pb.h" #include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h" +using std::map; using std::string; namespace caffe { -bool UpgradeV0Net(const V0NetParameter& v0_net_param, +bool UpgradeV0Net(const V0NetParameter& v0_net_param_padding_layers, NetParameter* net_param) { + // First upgrade padding layers to padded conv layers. + V0NetParameter v0_net_param; + UpgradeV0PaddingLayers(v0_net_param_padding_layers, &v0_net_param); + // Now upgrade layer parameters. bool is_fully_compatible = true; net_param->Clear(); if (v0_net_param.has_name()) { @@ -35,6 +40,48 @@ bool UpgradeV0Net(const V0NetParameter& v0_net_param, return is_fully_compatible; } +void UpgradeV0PaddingLayers(const V0NetParameter& param, + V0NetParameter* param_upgraded_pad) { + // Copy everything other than the layers from the original param. + param_upgraded_pad->Clear(); + param_upgraded_pad->CopyFrom(param); + param_upgraded_pad->clear_layers(); + // Figure out which layer each bottom blob comes from. + map blob_name_to_last_top_idx; + for (int i = 0; i < param.input_size(); ++i) { + const string& blob_name = param.input(i); + blob_name_to_last_top_idx[blob_name] = -1; + } + for (int i = 0; i < param.layers_size(); ++i) { + const V0LayerConnection& layer_connection = param.layers(i); + const V0LayerParameter& layer_param = layer_connection.layer(); + // Add the layer to the new net, unless it's a padding layer. + if (layer_param.type() != "padding") { + param_upgraded_pad->add_layers()->CopyFrom(layer_connection); + } + for (int j = 0; j < layer_connection.bottom_size(); ++j) { + const string& blob_name = layer_connection.bottom(j); + if (blob_name_to_last_top_idx.find(blob_name) == + blob_name_to_last_top_idx.end()) { + LOG(FATAL) << "Unknown blob input " << blob_name << " to layer " << j; + } + const int top_idx = blob_name_to_last_top_idx[blob_name]; + V0LayerParameter source_layer = param.layers(top_idx).layer(); + if (source_layer.type() == "padding") { + CHECK_EQ(layer_param.type(), "conv") << "Padding layer input to " + "non-convolutional layer type " << layer_param.type(); + int layer_index = param_upgraded_pad->layers_size() - 1; + param_upgraded_pad->mutable_layers(layer_index)->mutable_layer() + ->set_pad(source_layer.pad()); + } + } + for (int j = 0; j < layer_connection.top_size(); ++j) { + const string& blob_name = layer_connection.top(j); + blob_name_to_last_top_idx[blob_name] = i; + } + } +} + bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, LayerParameter* layer_param) { bool is_fully_compatible = true; From 19cc990848631044e25dd88ccf2d6b1dfbbfcbcb Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 01:40:03 -0700 Subject: [PATCH 19/60] set correct bottom blob name in upgraded conv layer --- src/caffe/util/upgrade_proto.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index b6faca52f5f..3c53ee8091e 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -66,13 +66,25 @@ void UpgradeV0PaddingLayers(const V0NetParameter& param, LOG(FATAL) << "Unknown blob input " << blob_name << " to layer " << j; } const int top_idx = blob_name_to_last_top_idx[blob_name]; - V0LayerParameter source_layer = param.layers(top_idx).layer(); - if (source_layer.type() == "padding") { + V0LayerConnection source_layer = param.layers(top_idx); + if (source_layer.layer().type() == "padding") { + // This layer has a padding layer as input -- check that it is a conv + // layer and takes only one input. Also check that the padding layer + // input has only one input and one output. Other cases have undefined + // behavior in Caffe. CHECK_EQ(layer_param.type(), "conv") << "Padding layer input to " "non-convolutional layer type " << layer_param.type(); + CHECK_EQ(layer_connection.bottom_size(), 1) + << "Conv Layer takes a single blob as input."; + CHECK_EQ(source_layer.bottom_size(), 1) + << "Padding Layer takes a single blob as input."; + CHECK_EQ(source_layer.top_size(), 1) + << "Padding Layer produces a single blob as output."; int layer_index = param_upgraded_pad->layers_size() - 1; param_upgraded_pad->mutable_layers(layer_index)->mutable_layer() - ->set_pad(source_layer.pad()); + ->set_pad(source_layer.layer().pad()); + param_upgraded_pad->mutable_layers(layer_index) + ->set_bottom(j, source_layer.bottom(0)); } } for (int j = 0; j < layer_connection.top_size(); ++j) { From 72d3183d2873abe74035c979c6f06500d2532597 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 01:58:29 -0700 Subject: [PATCH 20/60] imagenet padding upgrade test --- src/caffe/test/test_upgrade_proto.cpp | 751 ++++++++++++++++++++++++++ 1 file changed, 751 insertions(+) create mode 100644 src/caffe/test/test_upgrade_proto.cpp diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp new file mode 100644 index 00000000000..e446ea05294 --- /dev/null +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -0,0 +1,751 @@ +// Copyright 2014 Jeff Donahue + +#include +#include +#include + +#include "cuda_runtime.h" +#include "google/protobuf/text_format.h" +#include "gtest/gtest.h" +#include "caffe/blob.hpp" +#include "caffe/common.hpp" +#include "caffe/util/upgrade_proto.hpp" + +#include "caffe/test/test_caffe_main.hpp" + +using std::string; + +namespace caffe { + +template +class PaddingLayerUpgradeTest : public ::testing::Test { + protected: + void RunPaddingUpgradeTest( + const string& input_param_string, const string& output_param_string) { + // Test that UpgradeV0PaddingLayers called on the proto specified by + // input_param_string results in the proto specified by + // output_param_string. + V0NetParameter input_param; + CHECK(google::protobuf::TextFormat::ParseFromString( + input_param_string, &input_param)); + V0NetParameter expected_output_param; + CHECK(google::protobuf::TextFormat::ParseFromString( + output_param_string, &expected_output_param)); + V0NetParameter actual_output_param; + UpgradeV0PaddingLayers(input_param, &actual_output_param); + EXPECT_EQ(expected_output_param.DebugString(), + actual_output_param.DebugString()); + // Also test idempotence. + V0NetParameter double_pad_upgrade_param; + UpgradeV0PaddingLayers(actual_output_param, &double_pad_upgrade_param); + EXPECT_EQ(actual_output_param.DebugString(), + double_pad_upgrade_param.DebugString()); + } +}; + +typedef ::testing::Types PaddingUpgradeDtypes; +TYPED_TEST_CASE(PaddingLayerUpgradeTest, PaddingUpgradeDtypes); + +TYPED_TEST(PaddingLayerUpgradeTest, TestImageNet) { + const string& input_proto = + "name: 'CaffeNet' " + "layers { " + " layer { " + " name: 'data' " + " type: 'data' " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batchsize: 256 " + " cropsize: 227 " + " mirror: true " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'conv' " + " num_output: 96 " + " kernelsize: 11 " + " stride: 4 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'data' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'relu1' " + " type: 'relu' " + " } " + " bottom: 'conv1' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'pool1' " + " type: 'pool' " + " pool: MAX " + " kernelsize: 3 " + " stride: 2 " + " } " + " bottom: 'conv1' " + " top: 'pool1' " + "} " + "layers { " + " layer { " + " name: 'norm1' " + " type: 'lrn' " + " local_size: 5 " + " alpha: 0.0001 " + " beta: 0.75 " + " } " + " bottom: 'pool1' " + " top: 'norm1' " + "} " + "layers { " + " layer { " + " name: 'pad2' " + " type: 'padding' " + " pad: 2 " + " } " + " bottom: 'norm1' " + " top: 'pad2' " + "} " + "layers { " + " layer { " + " name: 'conv2' " + " type: 'conv' " + " num_output: 256 " + " group: 2 " + " kernelsize: 5 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad2' " + " top: 'conv2' " + "} " + "layers { " + " layer { " + " name: 'relu2' " + " type: 'relu' " + " } " + " bottom: 'conv2' " + " top: 'conv2' " + "} " + "layers { " + " layer { " + " name: 'pool2' " + " type: 'pool' " + " pool: MAX " + " kernelsize: 3 " + " stride: 2 " + " } " + " bottom: 'conv2' " + " top: 'pool2' " + "} " + "layers { " + " layer { " + " name: 'norm2' " + " type: 'lrn' " + " local_size: 5 " + " alpha: 0.0001 " + " beta: 0.75 " + " } " + " bottom: 'pool2' " + " top: 'norm2' " + "} " + "layers { " + " layer { " + " name: 'pad3' " + " type: 'padding' " + " pad: 1 " + " } " + " bottom: 'norm2' " + " top: 'pad3' " + "} " + "layers { " + " layer { " + " name: 'conv3' " + " type: 'conv' " + " num_output: 384 " + " kernelsize: 3 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad3' " + " top: 'conv3' " + "} " + "layers { " + " layer { " + " name: 'relu3' " + " type: 'relu' " + " } " + " bottom: 'conv3' " + " top: 'conv3' " + "} " + "layers { " + " layer { " + " name: 'pad4' " + " type: 'padding' " + " pad: 1 " + " } " + " bottom: 'conv3' " + " top: 'pad4' " + "} " + "layers { " + " layer { " + " name: 'conv4' " + " type: 'conv' " + " num_output: 384 " + " group: 2 " + " kernelsize: 3 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad4' " + " top: 'conv4' " + "} " + "layers { " + " layer { " + " name: 'relu4' " + " type: 'relu' " + " } " + " bottom: 'conv4' " + " top: 'conv4' " + "} " + "layers { " + " layer { " + " name: 'pad5' " + " type: 'padding' " + " pad: 1 " + " } " + " bottom: 'conv4' " + " top: 'pad5' " + "} " + "layers { " + " layer { " + " name: 'conv5' " + " type: 'conv' " + " num_output: 256 " + " group: 2 " + " kernelsize: 3 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad5' " + " top: 'conv5' " + "} " + "layers { " + " layer { " + " name: 'relu5' " + " type: 'relu' " + " } " + " bottom: 'conv5' " + " top: 'conv5' " + "} " + "layers { " + " layer { " + " name: 'pool5' " + " type: 'pool' " + " kernelsize: 3 " + " pool: MAX " + " stride: 2 " + " } " + " bottom: 'conv5' " + " top: 'pool5' " + "} " + "layers { " + " layer { " + " name: 'fc6' " + " type: 'innerproduct' " + " num_output: 4096 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.005 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pool5' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'relu6' " + " type: 'relu' " + " } " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'drop6' " + " type: 'dropout' " + " dropout_ratio: 0.5 " + " } " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'fc7' " + " type: 'innerproduct' " + " num_output: 4096 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.005 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'fc6' " + " top: 'fc7' " + "} " + "layers { " + " layer { " + " name: 'relu7' " + " type: 'relu' " + " } " + " bottom: 'fc7' " + " top: 'fc7' " + "} " + "layers { " + " layer { " + " name: 'drop7' " + " type: 'dropout' " + " dropout_ratio: 0.5 " + " } " + " bottom: 'fc7' " + " top: 'fc7' " + "} " + "layers { " + " layer { " + " name: 'fc8' " + " type: 'innerproduct' " + " num_output: 1000 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0 " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'fc7' " + " top: 'fc8' " + "} " + "layers { " + " layer { " + " name: 'loss' " + " type: 'softmax_loss' " + " } " + " bottom: 'fc8' " + " bottom: 'label' " + "} "; + const string& expected_output_proto = + "name: 'CaffeNet' " + "layers { " + " layer { " + " name: 'data' " + " type: 'data' " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batchsize: 256 " + " cropsize: 227 " + " mirror: true " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'conv' " + " num_output: 96 " + " kernelsize: 11 " + " stride: 4 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'data' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'relu1' " + " type: 'relu' " + " } " + " bottom: 'conv1' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'pool1' " + " type: 'pool' " + " pool: MAX " + " kernelsize: 3 " + " stride: 2 " + " } " + " bottom: 'conv1' " + " top: 'pool1' " + "} " + "layers { " + " layer { " + " name: 'norm1' " + " type: 'lrn' " + " local_size: 5 " + " alpha: 0.0001 " + " beta: 0.75 " + " } " + " bottom: 'pool1' " + " top: 'norm1' " + "} " + "layers { " + " layer { " + " name: 'conv2' " + " type: 'conv' " + " num_output: 256 " + " group: 2 " + " kernelsize: 5 " + " pad: 2 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'norm1' " + " top: 'conv2' " + "} " + "layers { " + " layer { " + " name: 'relu2' " + " type: 'relu' " + " } " + " bottom: 'conv2' " + " top: 'conv2' " + "} " + "layers { " + " layer { " + " name: 'pool2' " + " type: 'pool' " + " pool: MAX " + " kernelsize: 3 " + " stride: 2 " + " } " + " bottom: 'conv2' " + " top: 'pool2' " + "} " + "layers { " + " layer { " + " name: 'norm2' " + " type: 'lrn' " + " local_size: 5 " + " alpha: 0.0001 " + " beta: 0.75 " + " } " + " bottom: 'pool2' " + " top: 'norm2' " + "} " + "layers { " + " layer { " + " name: 'conv3' " + " type: 'conv' " + " num_output: 384 " + " kernelsize: 3 " + " pad: 1 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'norm2' " + " top: 'conv3' " + "} " + "layers { " + " layer { " + " name: 'relu3' " + " type: 'relu' " + " } " + " bottom: 'conv3' " + " top: 'conv3' " + "} " + "layers { " + " layer { " + " name: 'conv4' " + " type: 'conv' " + " num_output: 384 " + " group: 2 " + " kernelsize: 3 " + " pad: 1 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'conv3' " + " top: 'conv4' " + "} " + "layers { " + " layer { " + " name: 'relu4' " + " type: 'relu' " + " } " + " bottom: 'conv4' " + " top: 'conv4' " + "} " + "layers { " + " layer { " + " name: 'conv5' " + " type: 'conv' " + " num_output: 256 " + " group: 2 " + " kernelsize: 3 " + " pad: 1 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'conv4' " + " top: 'conv5' " + "} " + "layers { " + " layer { " + " name: 'relu5' " + " type: 'relu' " + " } " + " bottom: 'conv5' " + " top: 'conv5' " + "} " + "layers { " + " layer { " + " name: 'pool5' " + " type: 'pool' " + " kernelsize: 3 " + " pool: MAX " + " stride: 2 " + " } " + " bottom: 'conv5' " + " top: 'pool5' " + "} " + "layers { " + " layer { " + " name: 'fc6' " + " type: 'innerproduct' " + " num_output: 4096 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.005 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pool5' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'relu6' " + " type: 'relu' " + " } " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'drop6' " + " type: 'dropout' " + " dropout_ratio: 0.5 " + " } " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'fc7' " + " type: 'innerproduct' " + " num_output: 4096 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.005 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'fc6' " + " top: 'fc7' " + "} " + "layers { " + " layer { " + " name: 'relu7' " + " type: 'relu' " + " } " + " bottom: 'fc7' " + " top: 'fc7' " + "} " + "layers { " + " layer { " + " name: 'drop7' " + " type: 'dropout' " + " dropout_ratio: 0.5 " + " } " + " bottom: 'fc7' " + " top: 'fc7' " + "} " + "layers { " + " layer { " + " name: 'fc8' " + " type: 'innerproduct' " + " num_output: 1000 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0 " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'fc7' " + " top: 'fc8' " + "} " + "layers { " + " layer { " + " name: 'loss' " + " type: 'softmax_loss' " + " } " + " bottom: 'fc8' " + " bottom: 'label' " + "} "; + this->RunPaddingUpgradeTest(input_proto, expected_output_proto); +} + +} // namespace caffe From 1a68b3db5d8a6318cc003745eaa8491412accdab Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 02:08:09 -0700 Subject: [PATCH 21/60] more padding layer upgrade tests --- src/caffe/test/test_upgrade_proto.cpp | 343 ++++++++++++++++++++++++++ 1 file changed, 343 insertions(+) diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index e446ea05294..d35b8b4b2bb 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -46,6 +46,349 @@ class PaddingLayerUpgradeTest : public ::testing::Test { typedef ::testing::Types PaddingUpgradeDtypes; TYPED_TEST_CASE(PaddingLayerUpgradeTest, PaddingUpgradeDtypes); +TYPED_TEST(PaddingLayerUpgradeTest, TestSimple) { + const string& input_proto = + "name: 'CaffeNet' " + "layers { " + " layer { " + " name: 'data' " + " type: 'data' " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batchsize: 256 " + " cropsize: 227 " + " mirror: true " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " layer { " + " name: 'pad1' " + " type: 'padding' " + " pad: 2 " + " } " + " bottom: 'data' " + " top: 'pad1' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'conv' " + " num_output: 96 " + " kernelsize: 11 " + " stride: 4 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad1' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'innerproduct' " + " num_output: 1000 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0 " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'conv1' " + " top: 'fc8' " + "} " + "layers { " + " layer { " + " name: 'loss' " + " type: 'softmax_loss' " + " } " + " bottom: 'fc8' " + " bottom: 'label' " + "} "; + const string& expected_output_proto = + "name: 'CaffeNet' " + "layers { " + " layer { " + " name: 'data' " + " type: 'data' " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batchsize: 256 " + " cropsize: 227 " + " mirror: true " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'conv' " + " num_output: 96 " + " kernelsize: 11 " + " stride: 4 " + " pad: 2 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'data' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'innerproduct' " + " num_output: 1000 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0 " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'conv1' " + " top: 'fc8' " + "} " + "layers { " + " layer { " + " name: 'loss' " + " type: 'softmax_loss' " + " } " + " bottom: 'fc8' " + " bottom: 'label' " + "} "; + this->RunPaddingUpgradeTest(input_proto, expected_output_proto); +} + +TYPED_TEST(PaddingLayerUpgradeTest, TestTwoTops) { + const string& input_proto = + "name: 'CaffeNet' " + "layers { " + " layer { " + " name: 'data' " + " type: 'data' " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batchsize: 256 " + " cropsize: 227 " + " mirror: true " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " layer { " + " name: 'pad1' " + " type: 'padding' " + " pad: 2 " + " } " + " bottom: 'data' " + " top: 'pad1' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'conv' " + " num_output: 96 " + " kernelsize: 11 " + " stride: 4 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad1' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'innerproduct' " + " num_output: 1000 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0 " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'conv1' " + " top: 'fc8' " + "} " + "layers { " + " layer { " + " name: 'conv2' " + " type: 'conv' " + " num_output: 96 " + " kernelsize: 11 " + " stride: 4 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad1' " + " top: 'conv2' " + "} " + "layers { " + " layer { " + " name: 'loss' " + " type: 'softmax_loss' " + " } " + " bottom: 'fc8' " + " bottom: 'label' " + "} "; + const string& expected_output_proto = + "name: 'CaffeNet' " + "layers { " + " layer { " + " name: 'data' " + " type: 'data' " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batchsize: 256 " + " cropsize: 227 " + " mirror: true " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'conv' " + " num_output: 96 " + " kernelsize: 11 " + " stride: 4 " + " pad: 2 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'data' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'innerproduct' " + " num_output: 1000 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0 " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'conv1' " + " top: 'fc8' " + "} " + "layers { " + " layer { " + " name: 'conv2' " + " type: 'conv' " + " num_output: 96 " + " kernelsize: 11 " + " stride: 4 " + " pad: 2 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'data' " + " top: 'conv2' " + "} " + "layers { " + " layer { " + " name: 'loss' " + " type: 'softmax_loss' " + " } " + " bottom: 'fc8' " + " bottom: 'label' " + "} "; + this->RunPaddingUpgradeTest(input_proto, expected_output_proto); +} + TYPED_TEST(PaddingLayerUpgradeTest, TestImageNet) { const string& input_proto = "name: 'CaffeNet' " From 3b09e5076ce62962309fad4a1caf6c38966b4f37 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 03:04:35 -0700 Subject: [PATCH 22/60] add imagenet upgrade test and fix bug in upgrade_proto --- src/caffe/test/test_upgrade_proto.cpp | 867 +++++++++++++++++++++++++- src/caffe/util/upgrade_proto.cpp | 11 +- 2 files changed, 873 insertions(+), 5 deletions(-) diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index d35b8b4b2bb..3d64fe6c7ea 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -96,7 +96,7 @@ TYPED_TEST(PaddingLayerUpgradeTest, TestSimple) { "} " "layers { " " layer { " - " name: 'conv1' " + " name: 'fc8' " " type: 'innerproduct' " " num_output: 1000 " " weight_filler { " @@ -164,7 +164,7 @@ TYPED_TEST(PaddingLayerUpgradeTest, TestSimple) { "} " "layers { " " layer { " - " name: 'conv1' " + " name: 'fc8' " " type: 'innerproduct' " " num_output: 1000 " " weight_filler { " @@ -244,7 +244,7 @@ TYPED_TEST(PaddingLayerUpgradeTest, TestTwoTops) { "} " "layers { " " layer { " - " name: 'conv1' " + " name: 'fc8' " " type: 'innerproduct' " " num_output: 1000 " " weight_filler { " @@ -335,7 +335,7 @@ TYPED_TEST(PaddingLayerUpgradeTest, TestTwoTops) { "} " "layers { " " layer { " - " name: 'conv1' " + " name: 'fc8' " " type: 'innerproduct' " " num_output: 1000 " " weight_filler { " @@ -1091,4 +1091,863 @@ TYPED_TEST(PaddingLayerUpgradeTest, TestImageNet) { this->RunPaddingUpgradeTest(input_proto, expected_output_proto); } +template +class V0UpgradeTest : public ::testing::Test { + protected: + void RunV0UpgradeTest( + const string& input_param_string, const string& output_param_string) { + // Test that UpgradeV0Net called on the V0NetParameter proto specified by + // input_param_string results in the NetParameter proto specified by + // output_param_string. + V0NetParameter input_param; + CHECK(google::protobuf::TextFormat::ParseFromString( + input_param_string, &input_param)); + NetParameter expected_output_param; + CHECK(google::protobuf::TextFormat::ParseFromString( + output_param_string, &expected_output_param)); + NetParameter actual_output_param; + UpgradeV0Net(input_param, &actual_output_param); + CHECK_EQ(expected_output_param.DebugString(), + actual_output_param.DebugString()); + EXPECT_EQ(expected_output_param.DebugString(), + actual_output_param.DebugString()); + } +}; + +typedef ::testing::Types V0UpgradeDtypes; +TYPED_TEST_CASE(V0UpgradeTest, V0UpgradeDtypes); + +TYPED_TEST(V0UpgradeTest, TestSimple) { + const string& input_proto = + "name: 'CaffeNet' " + "layers { " + " layer { " + " name: 'data' " + " type: 'data' " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batchsize: 256 " + " cropsize: 227 " + " mirror: true " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " layer { " + " name: 'pad1' " + " type: 'padding' " + " pad: 2 " + " } " + " bottom: 'data' " + " top: 'pad1' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'conv' " + " num_output: 96 " + " kernelsize: 11 " + " stride: 4 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad1' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'fc8' " + " type: 'innerproduct' " + " num_output: 1000 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0 " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'conv1' " + " top: 'fc8' " + "} " + "layers { " + " layer { " + " name: 'loss' " + " type: 'softmax_loss' " + " } " + " bottom: 'fc8' " + " bottom: 'label' " + "} "; + const string& expected_output_proto = + "name: 'CaffeNet' " + "layers { " + " name: 'data' " + " type: 'data' " + " data_param { " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " mean_file: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batch_size: 256 " + " crop_size: 227 " + " mirror: true " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " name: 'conv1' " + " type: 'conv' " + " convolution_param { " + " num_output: 96 " + " kernel_size: 11 " + " stride: 4 " + " pad: 2 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'data' " + " top: 'conv1' " + "} " + "layers { " + " name: 'fc8' " + " type: 'innerproduct' " + " inner_product_param { " + " num_output: 1000 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0 " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'conv1' " + " top: 'fc8' " + "} " + "layers { " + " name: 'loss' " + " type: 'softmax_loss' " + " bottom: 'fc8' " + " bottom: 'label' " + "} "; + this->RunV0UpgradeTest(input_proto, expected_output_proto); +} + +TYPED_TEST(V0UpgradeTest, TestImageNet) { + const string& input_proto = + "name: 'CaffeNet' " + "layers { " + " layer { " + " name: 'data' " + " type: 'data' " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batchsize: 256 " + " cropsize: 227 " + " mirror: true " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'conv' " + " num_output: 96 " + " kernelsize: 11 " + " stride: 4 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'data' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'relu1' " + " type: 'relu' " + " } " + " bottom: 'conv1' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'pool1' " + " type: 'pool' " + " pool: MAX " + " kernelsize: 3 " + " stride: 2 " + " } " + " bottom: 'conv1' " + " top: 'pool1' " + "} " + "layers { " + " layer { " + " name: 'norm1' " + " type: 'lrn' " + " local_size: 5 " + " alpha: 0.0001 " + " beta: 0.75 " + " } " + " bottom: 'pool1' " + " top: 'norm1' " + "} " + "layers { " + " layer { " + " name: 'pad2' " + " type: 'padding' " + " pad: 2 " + " } " + " bottom: 'norm1' " + " top: 'pad2' " + "} " + "layers { " + " layer { " + " name: 'conv2' " + " type: 'conv' " + " num_output: 256 " + " group: 2 " + " kernelsize: 5 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad2' " + " top: 'conv2' " + "} " + "layers { " + " layer { " + " name: 'relu2' " + " type: 'relu' " + " } " + " bottom: 'conv2' " + " top: 'conv2' " + "} " + "layers { " + " layer { " + " name: 'pool2' " + " type: 'pool' " + " pool: MAX " + " kernelsize: 3 " + " stride: 2 " + " } " + " bottom: 'conv2' " + " top: 'pool2' " + "} " + "layers { " + " layer { " + " name: 'norm2' " + " type: 'lrn' " + " local_size: 5 " + " alpha: 0.0001 " + " beta: 0.75 " + " } " + " bottom: 'pool2' " + " top: 'norm2' " + "} " + "layers { " + " layer { " + " name: 'pad3' " + " type: 'padding' " + " pad: 1 " + " } " + " bottom: 'norm2' " + " top: 'pad3' " + "} " + "layers { " + " layer { " + " name: 'conv3' " + " type: 'conv' " + " num_output: 384 " + " kernelsize: 3 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad3' " + " top: 'conv3' " + "} " + "layers { " + " layer { " + " name: 'relu3' " + " type: 'relu' " + " } " + " bottom: 'conv3' " + " top: 'conv3' " + "} " + "layers { " + " layer { " + " name: 'pad4' " + " type: 'padding' " + " pad: 1 " + " } " + " bottom: 'conv3' " + " top: 'pad4' " + "} " + "layers { " + " layer { " + " name: 'conv4' " + " type: 'conv' " + " num_output: 384 " + " group: 2 " + " kernelsize: 3 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad4' " + " top: 'conv4' " + "} " + "layers { " + " layer { " + " name: 'relu4' " + " type: 'relu' " + " } " + " bottom: 'conv4' " + " top: 'conv4' " + "} " + "layers { " + " layer { " + " name: 'pad5' " + " type: 'padding' " + " pad: 1 " + " } " + " bottom: 'conv4' " + " top: 'pad5' " + "} " + "layers { " + " layer { " + " name: 'conv5' " + " type: 'conv' " + " num_output: 256 " + " group: 2 " + " kernelsize: 3 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pad5' " + " top: 'conv5' " + "} " + "layers { " + " layer { " + " name: 'relu5' " + " type: 'relu' " + " } " + " bottom: 'conv5' " + " top: 'conv5' " + "} " + "layers { " + " layer { " + " name: 'pool5' " + " type: 'pool' " + " kernelsize: 3 " + " pool: MAX " + " stride: 2 " + " } " + " bottom: 'conv5' " + " top: 'pool5' " + "} " + "layers { " + " layer { " + " name: 'fc6' " + " type: 'innerproduct' " + " num_output: 4096 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.005 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'pool5' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'relu6' " + " type: 'relu' " + " } " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'drop6' " + " type: 'dropout' " + " dropout_ratio: 0.5 " + " } " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'fc7' " + " type: 'innerproduct' " + " num_output: 4096 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.005 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'fc6' " + " top: 'fc7' " + "} " + "layers { " + " layer { " + " name: 'relu7' " + " type: 'relu' " + " } " + " bottom: 'fc7' " + " top: 'fc7' " + "} " + "layers { " + " layer { " + " name: 'drop7' " + " type: 'dropout' " + " dropout_ratio: 0.5 " + " } " + " bottom: 'fc7' " + " top: 'fc7' " + "} " + "layers { " + " layer { " + " name: 'fc8' " + " type: 'innerproduct' " + " num_output: 1000 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0 " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'fc7' " + " top: 'fc8' " + "} " + "layers { " + " layer { " + " name: 'loss' " + " type: 'softmax_loss' " + " } " + " bottom: 'fc8' " + " bottom: 'label' " + "} "; + const string& expected_output_proto = + "name: 'CaffeNet' " + "layers { " + " name: 'data' " + " type: 'data' " + " data_param { " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " mean_file: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batch_size: 256 " + " crop_size: 227 " + " mirror: true " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " name: 'conv1' " + " type: 'conv' " + " convolution_param { " + " num_output: 96 " + " kernel_size: 11 " + " stride: 4 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'data' " + " top: 'conv1' " + "} " + "layers { " + " name: 'relu1' " + " type: 'relu' " + " bottom: 'conv1' " + " top: 'conv1' " + "} " + "layers { " + " name: 'pool1' " + " type: 'pool' " + " pooling_param { " + " pool: MAX " + " kernel_size: 3 " + " stride: 2 " + " } " + " bottom: 'conv1' " + " top: 'pool1' " + "} " + "layers { " + " name: 'norm1' " + " type: 'lrn' " + " lrn_param { " + " local_size: 5 " + " alpha: 0.0001 " + " beta: 0.75 " + " } " + " bottom: 'pool1' " + " top: 'norm1' " + "} " + "layers { " + " name: 'conv2' " + " type: 'conv' " + " convolution_param { " + " num_output: 256 " + " group: 2 " + " kernel_size: 5 " + " pad: 2 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'norm1' " + " top: 'conv2' " + "} " + "layers { " + " name: 'relu2' " + " type: 'relu' " + " bottom: 'conv2' " + " top: 'conv2' " + "} " + "layers { " + " name: 'pool2' " + " type: 'pool' " + " pooling_param { " + " pool: MAX " + " kernel_size: 3 " + " stride: 2 " + " } " + " bottom: 'conv2' " + " top: 'pool2' " + "} " + "layers { " + " name: 'norm2' " + " type: 'lrn' " + " lrn_param { " + " local_size: 5 " + " alpha: 0.0001 " + " beta: 0.75 " + " } " + " bottom: 'pool2' " + " top: 'norm2' " + "} " + "layers { " + " name: 'conv3' " + " type: 'conv' " + " convolution_param { " + " num_output: 384 " + " kernel_size: 3 " + " pad: 1 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0. " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'norm2' " + " top: 'conv3' " + "} " + "layers { " + " name: 'relu3' " + " type: 'relu' " + " bottom: 'conv3' " + " top: 'conv3' " + "} " + "layers { " + " name: 'conv4' " + " type: 'conv' " + " convolution_param { " + " num_output: 384 " + " group: 2 " + " kernel_size: 3 " + " pad: 1 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'conv3' " + " top: 'conv4' " + "} " + "layers { " + " name: 'relu4' " + " type: 'relu' " + " bottom: 'conv4' " + " top: 'conv4' " + "} " + "layers { " + " name: 'conv5' " + " type: 'conv' " + " convolution_param { " + " num_output: 256 " + " group: 2 " + " kernel_size: 3 " + " pad: 1 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'conv4' " + " top: 'conv5' " + "} " + "layers { " + " name: 'relu5' " + " type: 'relu' " + " bottom: 'conv5' " + " top: 'conv5' " + "} " + "layers { " + " name: 'pool5' " + " type: 'pool' " + " pooling_param { " + " kernel_size: 3 " + " pool: MAX " + " stride: 2 " + " } " + " bottom: 'conv5' " + " top: 'pool5' " + "} " + "layers { " + " name: 'fc6' " + " type: 'innerproduct' " + " inner_product_param { " + " num_output: 4096 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.005 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'pool5' " + " top: 'fc6' " + "} " + "layers { " + " name: 'relu6' " + " type: 'relu' " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " name: 'drop6' " + " type: 'dropout' " + " dropout_param { " + " dropout_ratio: 0.5 " + " } " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " name: 'fc7' " + " type: 'innerproduct' " + " inner_product_param { " + " num_output: 4096 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.005 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'fc6' " + " top: 'fc7' " + "} " + "layers { " + " name: 'relu7' " + " type: 'relu' " + " bottom: 'fc7' " + " top: 'fc7' " + "} " + "layers { " + " name: 'drop7' " + " type: 'dropout' " + " dropout_param { " + " dropout_ratio: 0.5 " + " } " + " bottom: 'fc7' " + " top: 'fc7' " + "} " + "layers { " + " name: 'fc8' " + " type: 'innerproduct' " + " inner_product_param { " + " num_output: 1000 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 0 " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'fc7' " + " top: 'fc8' " + "} " + "layers { " + " name: 'loss' " + " type: 'softmax_loss' " + " bottom: 'fc8' " + " bottom: 'label' " + "} "; + this->RunV0UpgradeTest(input_proto, expected_output_proto); +} + + } // namespace caffe diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 3c53ee8091e..cc7ed51bfe7 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -181,6 +181,15 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, is_fully_compatible = false; } } + if (v0_layer_param.has_group()) { + if (type == "conv") { + layer_param->mutable_convolution_param()->set_group( + v0_layer_param.group()); + } else { + LOG(ERROR) << "Unknown parameter group for layer type " << type; + is_fully_compatible = false; + } + } if (v0_layer_param.has_stride()) { if (type == "conv") { layer_param->mutable_convolution_param()->set_stride( @@ -219,7 +228,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, } } if (v0_layer_param.has_dropout_ratio()) { - if (type == "dropout_ratio") { + if (type == "dropout") { layer_param->mutable_dropout_param()->set_dropout_ratio( v0_layer_param.dropout_ratio()); } else { From 24e5e52356a3040e909e9aa0a4819f7f9c194c67 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 03:30:49 -0700 Subject: [PATCH 23/60] add test which includes upgraded params --- src/caffe/test/test_upgrade_proto.cpp | 287 +++++++++++++++++++++++++- src/caffe/util/upgrade_proto.cpp | 3 + 2 files changed, 289 insertions(+), 1 deletion(-) diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index 3d64fe6c7ea..d6ca6d0752e 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -1263,6 +1263,292 @@ TYPED_TEST(V0UpgradeTest, TestSimple) { this->RunV0UpgradeTest(input_proto, expected_output_proto); } +TYPED_TEST(V0UpgradeTest, TestAllParams) { + const string& input_proto = + "name: 'CaffeNet' " + "layers { " + " layer { " + " name: 'data' " + " type: 'data' " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batchsize: 256 " + " cropsize: 227 " + " mirror: true " + " scale: 0.25 " + " rand_skip: 73 " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " layer { " + " name: 'hdf5data' " + " type: 'hdf5_data' " + " source: '/my/hdf5/data' " + " batchsize: 256 " + " } " + " top: 'hdf5data' " + "} " + "layers { " + " layer { " + " name: 'conv1' " + " type: 'conv' " + " num_output: 96 " + " biasterm: false " + " pad: 4 " + " kernelsize: 11 " + " stride: 4 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 3. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'data' " + " top: 'conv1' " + "} " + "layers { " + " layer { " + " name: 'pool1ave' " + " type: 'pool' " + " pool: AVE " + " kernelsize: 3 " + " stride: 2 " + " } " + " bottom: 'conv1' " + " top: 'pool1ave' " + "} " + "layers { " + " layer { " + " name: 'pool1stoch' " + " type: 'pool' " + " pool: STOCHASTIC " + " kernelsize: 4 " + " stride: 5 " + " } " + " bottom: 'conv1' " + " top: 'pool1stoch' " + "} " + "layers { " + " layer { " + " name: 'concat' " + " type: 'concat' " + " concat_dim: 2 " + " } " + " bottom: 'pool1ave' " + " bottom: 'pool1stoch' " + " top: 'pool1concat' " + "} " + "layers { " + " layer { " + " name: 'norm1' " + " type: 'lrn' " + " local_size: 5 " + " alpha: 0.0001 " + " beta: 0.75 " + " } " + " bottom: 'pool1concat' " + " top: 'norm1' " + "} " + "layers { " + " layer { " + " name: 'fc6' " + " type: 'innerproduct' " + " num_output: 4096 " + " biasterm: false " + " weight_filler { " + " type: 'gaussian' " + " std: 0.005 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " } " + " bottom: 'norm1' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'relu6' " + " type: 'relu' " + " } " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'drop6' " + " type: 'dropout' " + " dropout_ratio: 0.2 " + " } " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " layer { " + " name: 'loss' " + " type: 'infogain_loss' " + " source: '/my/infogain/matrix' " + " } " + " bottom: 'fc6' " + " bottom: 'label' " + "} "; + const string& expected_output_proto = + "name: 'CaffeNet' " + "layers { " + " name: 'data' " + " type: 'data' " + " data_param { " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " mean_file: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batch_size: 256 " + " crop_size: 227 " + " mirror: true " + " scale: 0.25 " + " rand_skip: 73 " + " } " + " top: 'data' " + " top: 'label' " + "} " + "layers { " + " name: 'hdf5data' " + " type: 'hdf5_data' " + " hdf5_data_param { " + " source: '/my/hdf5/data' " + " batch_size: 256 " + " } " + " top: 'hdf5data' " + "} " + "layers { " + " name: 'conv1' " + " type: 'conv' " + " convolution_param { " + " num_output: 96 " + " bias_term: false " + " pad: 4 " + " kernel_size: 11 " + " stride: 4 " + " weight_filler { " + " type: 'gaussian' " + " std: 0.01 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 3. " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'data' " + " top: 'conv1' " + "} " + "layers { " + " name: 'pool1ave' " + " type: 'pool' " + " pooling_param { " + " pool: AVE " + " kernel_size: 3 " + " stride: 2 " + " } " + " bottom: 'conv1' " + " top: 'pool1ave' " + "} " + "layers { " + " name: 'pool1stoch' " + " type: 'pool' " + " pooling_param { " + " pool: STOCHASTIC " + " kernel_size: 4 " + " stride: 5 " + " } " + " bottom: 'conv1' " + " top: 'pool1stoch' " + "} " + "layers { " + " name: 'concat' " + " type: 'concat' " + " concat_param { " + " concat_dim: 2 " + " } " + " bottom: 'pool1ave' " + " bottom: 'pool1stoch' " + " top: 'pool1concat' " + "} " + "layers { " + " name: 'norm1' " + " type: 'lrn' " + " lrn_param { " + " local_size: 5 " + " alpha: 0.0001 " + " beta: 0.75 " + " } " + " bottom: 'pool1concat' " + " top: 'norm1' " + "} " + "layers { " + " name: 'fc6' " + " type: 'innerproduct' " + " inner_product_param { " + " num_output: 4096 " + " bias_term: false " + " weight_filler { " + " type: 'gaussian' " + " std: 0.005 " + " } " + " bias_filler { " + " type: 'constant' " + " value: 1. " + " } " + " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'norm1' " + " top: 'fc6' " + "} " + "layers { " + " name: 'relu6' " + " type: 'relu' " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " name: 'drop6' " + " type: 'dropout' " + " dropout_param { " + " dropout_ratio: 0.2 " + " } " + " bottom: 'fc6' " + " top: 'fc6' " + "} " + "layers { " + " name: 'loss' " + " type: 'infogain_loss' " + " infogain_loss_param { " + " source: '/my/infogain/matrix' " + " } " + " bottom: 'fc6' " + " bottom: 'label' " + "} "; + this->RunV0UpgradeTest(input_proto, expected_output_proto); +} + TYPED_TEST(V0UpgradeTest, TestImageNet) { const string& input_proto = "name: 'CaffeNet' " @@ -1949,5 +2235,4 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { this->RunV0UpgradeTest(input_proto, expected_output_proto); } - } // namespace caffe diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index cc7ed51bfe7..94546ac590e 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -295,6 +295,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, if (type == "data") { layer_param->mutable_data_param()->set_batch_size( v0_layer_param.batchsize()); + } else if (type == "hdf5_data") { + layer_param->mutable_hdf5_data_param()->set_batch_size( + v0_layer_param.batchsize()); } else { LOG(ERROR) << "Unknown parameter batchsize for layer type " << type; is_fully_compatible = false; From b95dc5fa24649cfc2b144cf8ff8fe0348fc5fcdf Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 03:45:09 -0700 Subject: [PATCH 24/60] fix insert_splits for new layer param format --- include/caffe/util/insert_splits.hpp | 8 +- src/caffe/net.cpp | 2 +- src/caffe/test/test_split_layer.cpp | 664 ++++++++++----------------- src/caffe/util/insert_splits.cpp | 19 +- 4 files changed, 259 insertions(+), 434 deletions(-) diff --git a/include/caffe/util/insert_splits.hpp b/include/caffe/util/insert_splits.hpp index 4f4ddc4f293..e25cdd7faf1 100644 --- a/include/caffe/util/insert_splits.hpp +++ b/include/caffe/util/insert_splits.hpp @@ -14,16 +14,16 @@ namespace caffe { // Copy NetParameters with SplitLayers added to replace any shared bottom // blobs with unique bottom blobs provided by the SplitLayer. -void insert_splits(const NetParameter& param, NetParameter* param_split); +void InsertSplits(const NetParameter& param, NetParameter* param_split); -void configure_split_layer(const string& layer_name, const string& blob_name, +void ConfigureSplitLayer(const string& layer_name, const string& blob_name, const int blob_idx, const int split_count, LayerParameter* split_layer_param); -string get_split_layer_name(const string& layer_name, const string& blob_name, +string SplitLayerName(const string& layer_name, const string& blob_name, const int blob_idx); -string get_split_blob_name(const string& layer_name, const string& blob_name, +string SplitBlobName(const string& layer_name, const string& blob_name, const int blob_idx, const int split_idx); } // namespace caffe diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index a16afdc5323..06db0ab7fa0 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -50,7 +50,7 @@ template void Net::Init(const NetParameter& in_param) { // Create a copy of in_param with splits added where necessary. NetParameter param; - insert_splits(in_param, ¶m); + InsertSplits(in_param, ¶m); // Basically, build all the layers and set up its connections. name_ = param.name(); map blob_name_to_idx; diff --git a/src/caffe/test/test_split_layer.cpp b/src/caffe/test/test_split_layer.cpp index 2c7c986e99b..6d3d6128f08 100644 --- a/src/caffe/test/test_split_layer.cpp +++ b/src/caffe/test/test_split_layer.cpp @@ -160,7 +160,7 @@ class SplitLayerInsertionTest : public ::testing::Test { protected: void RunInsertionTest( const string& input_param_string, const string& output_param_string) { - // Test that insert_splits called on the proto specified by + // Test that InsertSplits called on the proto specified by // input_param_string results in the proto specified by // output_param_string. NetParameter input_param; @@ -170,12 +170,12 @@ class SplitLayerInsertionTest : public ::testing::Test { CHECK(google::protobuf::TextFormat::ParseFromString( output_param_string, &expected_output_param)); NetParameter actual_output_param; - insert_splits(input_param, &actual_output_param); + InsertSplits(input_param, &actual_output_param); EXPECT_EQ(expected_output_param.DebugString(), actual_output_param.DebugString()); // Also test idempotence. NetParameter double_split_insert_param; - insert_splits(actual_output_param, &double_split_insert_param); + InsertSplits(actual_output_param, &double_split_insert_param); EXPECT_EQ(actual_output_param.DebugString(), double_split_insert_param.DebugString()); } @@ -188,26 +188,20 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion1) { const string& input_proto = "name: 'TestNetwork' " "layers: { " - " layer { " - " name: 'data' " - " type: 'data' " - " } " + " name: 'data' " + " type: 'data' " " top: 'data' " " top: 'label' " "} " "layers: { " - " layer { " - " name: 'innerprod' " - " type: 'inner_product' " - " } " + " name: 'innerprod' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod' " "} " "layers: { " - " layer { " - " name: 'loss' " - " type: 'softmax_with_loss' " - " } " + " name: 'loss' " + " type: 'softmax_with_loss' " " bottom: 'innerprod' " " bottom: 'label' " "} "; @@ -218,43 +212,33 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion2) { const string& input_proto = "name: 'TestNetwork' " "layers: { " - " layer { " - " name: 'data' " - " type: 'data' " - " } " + " name: 'data' " + " type: 'data' " " top: 'data' " " top: 'label' " "} " "layers: { " - " layer { " - " name: 'data_split' " - " type: 'split' " - " } " + " name: 'data_split' " + " type: 'split' " " bottom: 'data' " " top: 'data_split_0' " " top: 'data_split_1' " "} " "layers: { " - " layer { " - " name: 'innerprod1' " - " type: 'inner_product' " - " } " + " name: 'innerprod1' " + " type: 'inner_product' " " bottom: 'data_split_0' " " top: 'innerprod1' " "} " "layers: { " - " layer { " - " name: 'innerprod2' " - " type: 'inner_product' " - " } " + " name: 'innerprod2' " + " type: 'inner_product' " " bottom: 'data_split_1' " " top: 'innerprod2' " "} " "layers: { " - " layer { " - " name: 'loss' " - " type: 'euclidean_loss' " - " } " + " name: 'loss' " + " type: 'euclidean_loss' " " bottom: 'innerprod1' " " bottom: 'innerprod2' " "} "; @@ -265,24 +249,24 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { const string& input_proto = "name: 'CaffeNet' " "layers { " - " layer { " - " name: 'data' " - " type: 'data' " + " name: 'data' " + " type: 'data' " + " data_param { " " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " - " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " - " batchsize: 256 " - " cropsize: 227 " + " mean_file: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batch_size: 256 " + " crop_size: 227 " " mirror: true " " } " " top: 'data' " " top: 'label' " "} " "layers { " - " layer { " - " name: 'conv1' " - " type: 'conv' " + " name: 'conv1' " + " type: 'conv' " + " convolution_param { " " num_output: 96 " - " kernelsize: 11 " + " kernel_size: 11 " " stride: 4 " " weight_filler { " " type: 'gaussian' " @@ -292,37 +276,35 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { " type: 'constant' " " value: 0. " " } " - " blobs_lr: 1. " - " blobs_lr: 2. " - " weight_decay: 1. " - " weight_decay: 0. " " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " " bottom: 'data' " " top: 'conv1' " "} " "layers { " - " layer { " - " name: 'relu1' " - " type: 'relu' " - " } " + " name: 'relu1' " + " type: 'relu' " " bottom: 'conv1' " " top: 'conv1' " "} " "layers { " - " layer { " - " name: 'pool1' " - " type: 'pool' " + " name: 'pool1' " + " type: 'pool' " + " pooling_param { " " pool: MAX " - " kernelsize: 3 " + " kernel_size: 3 " " stride: 2 " " } " " bottom: 'conv1' " " top: 'pool1' " "} " "layers { " - " layer { " - " name: 'norm1' " - " type: 'lrn' " + " name: 'norm1' " + " type: 'lrn' " + " lrn_param { " " local_size: 5 " " alpha: 0.0001 " " beta: 0.75 " @@ -331,21 +313,13 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { " top: 'norm1' " "} " "layers { " - " layer { " - " name: 'pad2' " - " type: 'padding' " - " pad: 2 " - " } " - " bottom: 'norm1' " - " top: 'pad2' " - "} " - "layers { " - " layer { " - " name: 'conv2' " - " type: 'conv' " + " name: 'conv2' " + " type: 'conv' " + " convolution_param { " " num_output: 256 " " group: 2 " - " kernelsize: 5 " + " kernel_size: 5 " + " pad: 2 " " weight_filler { " " type: 'gaussian' " " std: 0.01 " @@ -354,37 +328,35 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { " type: 'constant' " " value: 1. " " } " - " blobs_lr: 1. " - " blobs_lr: 2. " - " weight_decay: 1. " - " weight_decay: 0. " " } " - " bottom: 'pad2' " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'norm1' " " top: 'conv2' " "} " "layers { " - " layer { " - " name: 'relu2' " - " type: 'relu' " - " } " + " name: 'relu2' " + " type: 'relu' " " bottom: 'conv2' " " top: 'conv2' " "} " "layers { " - " layer { " - " name: 'pool2' " - " type: 'pool' " + " name: 'pool2' " + " type: 'pool' " + " pooling_param { " " pool: MAX " - " kernelsize: 3 " + " kernel_size: 3 " " stride: 2 " " } " " bottom: 'conv2' " " top: 'pool2' " "} " "layers { " - " layer { " - " name: 'norm2' " - " type: 'lrn' " + " name: 'norm2' " + " type: 'lrn' " + " lrn_param { " " local_size: 5 " " alpha: 0.0001 " " beta: 0.75 " @@ -393,20 +365,12 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { " top: 'norm2' " "} " "layers { " - " layer { " - " name: 'pad3' " - " type: 'padding' " - " pad: 1 " - " } " - " bottom: 'norm2' " - " top: 'pad3' " - "} " - "layers { " - " layer { " - " name: 'conv3' " - " type: 'conv' " + " name: 'conv3' " + " type: 'conv' " + " convolution_param { " " num_output: 384 " - " kernelsize: 3 " + " kernel_size: 3 " + " pad: 1 " " weight_filler { " " type: 'gaussian' " " std: 0.01 " @@ -415,38 +379,28 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { " type: 'constant' " " value: 0. " " } " - " blobs_lr: 1. " - " blobs_lr: 2. " - " weight_decay: 1. " - " weight_decay: 0. " " } " - " bottom: 'pad3' " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'norm2' " " top: 'conv3' " "} " "layers { " - " layer { " - " name: 'relu3' " - " type: 'relu' " - " } " + " name: 'relu3' " + " type: 'relu' " " bottom: 'conv3' " " top: 'conv3' " "} " "layers { " - " layer { " - " name: 'pad4' " - " type: 'padding' " - " pad: 1 " - " } " - " bottom: 'conv3' " - " top: 'pad4' " - "} " - "layers { " - " layer { " - " name: 'conv4' " - " type: 'conv' " + " name: 'conv4' " + " type: 'conv' " + " convolution_param { " " num_output: 384 " " group: 2 " - " kernelsize: 3 " + " kernel_size: 3 " + " pad: 1 " " weight_filler { " " type: 'gaussian' " " std: 0.01 " @@ -455,38 +409,28 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { " type: 'constant' " " value: 1. " " } " - " blobs_lr: 1. " - " blobs_lr: 2. " - " weight_decay: 1. " - " weight_decay: 0. " " } " - " bottom: 'pad4' " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'conv3' " " top: 'conv4' " "} " "layers { " - " layer { " - " name: 'relu4' " - " type: 'relu' " - " } " + " name: 'relu4' " + " type: 'relu' " " bottom: 'conv4' " " top: 'conv4' " "} " "layers { " - " layer { " - " name: 'pad5' " - " type: 'padding' " - " pad: 1 " - " } " - " bottom: 'conv4' " - " top: 'pad5' " - "} " - "layers { " - " layer { " - " name: 'conv5' " - " type: 'conv' " + " name: 'conv5' " + " type: 'conv' " + " convolution_param { " " num_output: 256 " " group: 2 " - " kernelsize: 3 " + " kernel_size: 3 " + " pad: 1 " " weight_filler { " " type: 'gaussian' " " std: 0.01 " @@ -495,27 +439,25 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { " type: 'constant' " " value: 1. " " } " - " blobs_lr: 1. " - " blobs_lr: 2. " - " weight_decay: 1. " - " weight_decay: 0. " " } " - " bottom: 'pad5' " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " + " bottom: 'conv4' " " top: 'conv5' " "} " "layers { " - " layer { " - " name: 'relu5' " - " type: 'relu' " - " } " + " name: 'relu5' " + " type: 'relu' " " bottom: 'conv5' " " top: 'conv5' " "} " "layers { " - " layer { " - " name: 'pool5' " - " type: 'pool' " - " kernelsize: 3 " + " name: 'pool5' " + " type: 'pool' " + " pooling_param { " + " kernel_size: 3 " " pool: MAX " " stride: 2 " " } " @@ -523,9 +465,9 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { " top: 'pool5' " "} " "layers { " - " layer { " - " name: 'fc6' " - " type: 'innerproduct' " + " name: 'fc6' " + " type: 'innerproduct' " + " inner_product_param { " " num_output: 4096 " " weight_filler { " " type: 'gaussian' " @@ -535,35 +477,33 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { " type: 'constant' " " value: 1. " " } " - " blobs_lr: 1. " - " blobs_lr: 2. " - " weight_decay: 1. " - " weight_decay: 0. " " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " " bottom: 'pool5' " " top: 'fc6' " "} " "layers { " - " layer { " - " name: 'relu6' " - " type: 'relu' " - " } " + " name: 'relu6' " + " type: 'relu' " " bottom: 'fc6' " " top: 'fc6' " "} " "layers { " - " layer { " - " name: 'drop6' " - " type: 'dropout' " + " name: 'drop6' " + " type: 'dropout' " + " dropout_param { " " dropout_ratio: 0.5 " " } " " bottom: 'fc6' " " top: 'fc6' " "} " "layers { " - " layer { " - " name: 'fc7' " - " type: 'innerproduct' " + " name: 'fc7' " + " type: 'innerproduct' " + " inner_product_param { " " num_output: 4096 " " weight_filler { " " type: 'gaussian' " @@ -573,35 +513,33 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { " type: 'constant' " " value: 1. " " } " - " blobs_lr: 1. " - " blobs_lr: 2. " - " weight_decay: 1. " - " weight_decay: 0. " " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " " bottom: 'fc6' " " top: 'fc7' " "} " "layers { " - " layer { " - " name: 'relu7' " - " type: 'relu' " - " } " + " name: 'relu7' " + " type: 'relu' " " bottom: 'fc7' " " top: 'fc7' " "} " "layers { " - " layer { " - " name: 'drop7' " - " type: 'dropout' " + " name: 'drop7' " + " type: 'dropout' " + " dropout_param { " " dropout_ratio: 0.5 " " } " " bottom: 'fc7' " " top: 'fc7' " "} " "layers { " - " layer { " - " name: 'fc8' " - " type: 'innerproduct' " + " name: 'fc8' " + " type: 'innerproduct' " + " inner_product_param { " " num_output: 1000 " " weight_filler { " " type: 'gaussian' " @@ -611,57 +549,47 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { " type: 'constant' " " value: 0 " " } " - " blobs_lr: 1. " - " blobs_lr: 2. " - " weight_decay: 1. " - " weight_decay: 0. " " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " " bottom: 'fc7' " " top: 'fc8' " "} " "layers { " - " layer { " - " name: 'loss' " - " type: 'softmax_loss' " - " } " + " name: 'loss' " + " type: 'softmax_loss' " " bottom: 'fc8' " " bottom: 'label' " "} "; this->RunInsertionTest(input_proto, input_proto); } -TYPED_TEST(SplitLayerInsertionTest, TestInsertionWithInPlace) { +TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionWithInPlace) { const string& input_proto = "name: 'TestNetwork' " "layers: { " - " layer { " - " name: 'data' " - " type: 'data' " - " } " + " name: 'data' " + " type: 'data' " " top: 'data' " " top: 'label' " "} " "layers: { " - " layer { " - " name: 'innerprod' " - " type: 'inner_product' " - " } " + " name: 'innerprod' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod' " "} " "layers: { " - " layer { " - " name: 'relu' " - " type: 'relu' " - " } " + " name: 'relu' " + " type: 'relu' " " bottom: 'innerprod' " " top: 'innerprod' " "} " "layers: { " - " layer { " - " name: 'loss' " - " type: 'softmax_with_loss' " - " } " + " name: 'loss' " + " type: 'softmax_with_loss' " " bottom: 'innerprod' " " bottom: 'label' " "} "; @@ -672,119 +600,91 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertion) { const string& input_proto = "name: 'TestNetwork' " "layers: { " - " layer { " - " name: 'data' " - " type: 'data' " - " } " + " name: 'data' " + " type: 'data' " " top: 'data' " " top: 'label' " "} " "layers: { " - " layer { " - " name: 'innerprod1' " - " type: 'inner_product' " - " } " + " name: 'innerprod1' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " - " layer { " - " name: 'innerprod2' " - " type: 'inner_product' " - " } " + " name: 'innerprod2' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod2' " "} " "layers: { " - " layer { " - " name: 'innerprod3' " - " type: 'inner_product' " - " } " + " name: 'innerprod3' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod3' " "} " "layers: { " - " layer { " - " name: 'loss1' " - " type: 'euclidean_loss' " - " } " + " name: 'loss1' " + " type: 'euclidean_loss' " " bottom: 'innerprod1' " " bottom: 'innerprod2' " "} " "layers: { " - " layer { " - " name: 'loss2' " - " type: 'euclidean_loss' " - " } " + " name: 'loss2' " + " type: 'euclidean_loss' " " bottom: 'innerprod2' " " bottom: 'innerprod3' " "} "; const string& expected_output_proto = "name: 'TestNetwork' " "layers: { " - " layer { " - " name: 'data' " - " type: 'data' " - " } " + " name: 'data' " + " type: 'data' " " top: 'data' " " top: 'label' " "} " "layers: { " - " layer { " - " name: 'data_data_0_split' " - " type: 'split' " - " } " + " name: 'data_data_0_split' " + " type: 'split' " " bottom: 'data' " " top: 'data' " " top: 'data_data_0_split_1' " " top: 'data_data_0_split_2' " "} " "layers: { " - " layer { " - " name: 'innerprod1' " - " type: 'inner_product' " - " } " + " name: 'innerprod1' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " - " layer { " - " name: 'innerprod2' " - " type: 'inner_product' " - " } " + " name: 'innerprod2' " + " type: 'inner_product' " " bottom: 'data_data_0_split_1' " " top: 'innerprod2' " "} " "layers: { " - " layer { " - " name: 'innerprod2_innerprod2_0_split' " - " type: 'split' " - " } " + " name: 'innerprod2_innerprod2_0_split' " + " type: 'split' " " bottom: 'innerprod2' " " top: 'innerprod2' " " top: 'innerprod2_innerprod2_0_split_1' " "} " "layers: { " - " layer { " - " name: 'innerprod3' " - " type: 'inner_product' " - " } " + " name: 'innerprod3' " + " type: 'inner_product' " " bottom: 'data_data_0_split_2' " " top: 'innerprod3' " "} " "layers: { " - " layer { " - " name: 'loss1' " - " type: 'euclidean_loss' " - " } " + " name: 'loss1' " + " type: 'euclidean_loss' " " bottom: 'innerprod1' " " bottom: 'innerprod2' " "} " "layers: { " - " layer { " - " name: 'loss2' " - " type: 'euclidean_loss' " - " } " + " name: 'loss2' " + " type: 'euclidean_loss' " " bottom: 'innerprod2_innerprod2_0_split_1' " " bottom: 'innerprod3' " "} "; @@ -795,134 +695,102 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertionTwoTop) { const string& input_proto = "name: 'TestNetwork' " "layers: { " - " layer { " - " name: 'data' " - " type: 'data' " - " } " + " name: 'data' " + " type: 'data' " " top: 'data' " " top: 'label' " "} " "layers: { " - " layer { " - " name: 'innerprod1' " - " type: 'inner_product' " - " } " + " name: 'innerprod1' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " - " layer { " - " name: 'innerprod2' " - " type: 'inner_product' " - " } " + " name: 'innerprod2' " + " type: 'inner_product' " " bottom: 'label' " " top: 'innerprod2' " "} " "layers: { " - " layer { " - " name: 'innerprod3' " - " type: 'inner_product' " - " } " + " name: 'innerprod3' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod3' " "} " "layers: { " - " layer { " - " name: 'innerprod4' " - " type: 'inner_product' " - " } " + " name: 'innerprod4' " + " type: 'inner_product' " " bottom: 'label' " " top: 'innerprod4' " "} " "layers: { " - " layer { " - " name: 'loss1' " - " type: 'euclidean_loss' " - " } " + " name: 'loss1' " + " type: 'euclidean_loss' " " bottom: 'innerprod1' " " bottom: 'innerprod3' " "} " "layers: { " - " layer { " - " name: 'loss2' " - " type: 'euclidean_loss' " - " } " + " name: 'loss2' " + " type: 'euclidean_loss' " " bottom: 'innerprod2' " " bottom: 'innerprod4' " "} "; const string& expected_output_proto = "name: 'TestNetwork' " "layers: { " - " layer { " - " name: 'data' " - " type: 'data' " - " } " + " name: 'data' " + " type: 'data' " " top: 'data' " " top: 'label' " "} " "layers: { " - " layer { " - " name: 'data_data_0_split' " - " type: 'split' " - " } " + " name: 'data_data_0_split' " + " type: 'split' " " bottom: 'data' " " top: 'data' " " top: 'data_data_0_split_1' " "} " "layers: { " - " layer { " - " name: 'label_data_1_split' " - " type: 'split' " - " } " + " name: 'label_data_1_split' " + " type: 'split' " " bottom: 'label' " " top: 'label' " " top: 'label_data_1_split_1' " "} " "layers: { " - " layer { " - " name: 'innerprod1' " - " type: 'inner_product' " - " } " + " name: 'innerprod1' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " - " layer { " - " name: 'innerprod2' " - " type: 'inner_product' " - " } " + " name: 'innerprod2' " + " type: 'inner_product' " " bottom: 'label' " " top: 'innerprod2' " "} " "layers: { " - " layer { " - " name: 'innerprod3' " - " type: 'inner_product' " - " } " + " name: 'innerprod3' " + " type: 'inner_product' " " bottom: 'data_data_0_split_1' " " top: 'innerprod3' " "} " "layers: { " - " layer { " - " name: 'innerprod4' " - " type: 'inner_product' " - " } " + " name: 'innerprod4' " + " type: 'inner_product' " " bottom: 'label_data_1_split_1' " " top: 'innerprod4' " "} " "layers: { " - " layer { " - " name: 'loss1' " - " type: 'euclidean_loss' " - " } " + " name: 'loss1' " + " type: 'euclidean_loss' " " bottom: 'innerprod1' " " bottom: 'innerprod3' " "} " "layers: { " - " layer { " - " name: 'loss2' " - " type: 'euclidean_loss' " - " } " + " name: 'loss2' " + " type: 'euclidean_loss' " " bottom: 'innerprod2' " " bottom: 'innerprod4' " "} "; @@ -938,26 +806,20 @@ TYPED_TEST(SplitLayerInsertionTest, TestInputInsertion) { "input_dim: 227 " "input_dim: 227 " "layers: { " - " layer { " - " name: 'innerprod1' " - " type: 'inner_product' " - " } " + " name: 'innerprod1' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " - " layer { " - " name: 'innerprod2' " - " type: 'inner_product' " - " } " + " name: 'innerprod2' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod2' " "} " "layers: { " - " layer { " - " name: 'loss' " - " type: 'euclidean_loss' " - " } " + " name: 'loss' " + " type: 'euclidean_loss' " " bottom: 'innerprod1' " " bottom: 'innerprod2' " "} "; @@ -969,35 +831,27 @@ TYPED_TEST(SplitLayerInsertionTest, TestInputInsertion) { "input_dim: 227 " "input_dim: 227 " "layers: { " - " layer { " - " name: 'data_input_0_split' " - " type: 'split' " - " } " + " name: 'data_input_0_split' " + " type: 'split' " " bottom: 'data' " " top: 'data' " " top: 'data_input_0_split_1' " "} " "layers: { " - " layer { " - " name: 'innerprod1' " - " type: 'inner_product' " - " } " + " name: 'innerprod1' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " - " layer { " - " name: 'innerprod2' " - " type: 'inner_product' " - " } " + " name: 'innerprod2' " + " type: 'inner_product' " " bottom: 'data_input_0_split_1' " " top: 'innerprod2' " "} " "layers: { " - " layer { " - " name: 'loss' " - " type: 'euclidean_loss' " - " } " + " name: 'loss' " + " type: 'euclidean_loss' " " bottom: 'innerprod1' " " bottom: 'innerprod2' " "} "; @@ -1008,118 +862,90 @@ TYPED_TEST(SplitLayerInsertionTest, TestWithInPlace) { const string& input_proto = "name: 'TestNetwork' " "layers: { " - " layer { " - " name: 'data' " - " type: 'data' " - " } " + " name: 'data' " + " type: 'data' " " top: 'data' " " top: 'label' " "} " "layers: { " - " layer { " - " name: 'innerprod1' " - " type: 'inner_product' " - " } " + " name: 'innerprod1' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " - " layer { " - " name: 'relu1' " - " type: 'relu' " - " } " + " name: 'relu1' " + " type: 'relu' " " bottom: 'innerprod1' " " top: 'innerprod1' " "} " "layers: { " - " layer { " - " name: 'innerprod2' " - " type: 'inner_product' " - " } " + " name: 'innerprod2' " + " type: 'inner_product' " " bottom: 'innerprod1' " " top: 'innerprod2' " "} " "layers: { " - " layer { " - " name: 'loss1' " - " type: 'euclidean_loss' " - " } " + " name: 'loss1' " + " type: 'euclidean_loss' " " bottom: 'innerprod1' " " bottom: 'label' " "} " "layers: { " - " layer { " - " name: 'loss2' " - " type: 'euclidean_loss' " - " } " + " name: 'loss2' " + " type: 'euclidean_loss' " " bottom: 'innerprod2' " " bottom: 'data' " "} "; const string& expected_output_proto = "name: 'TestNetwork' " "layers: { " - " layer { " - " name: 'data' " - " type: 'data' " - " } " + " name: 'data' " + " type: 'data' " " top: 'data' " " top: 'label' " "} " "layers: { " - " layer { " - " name: 'data_data_0_split' " - " type: 'split' " - " } " + " name: 'data_data_0_split' " + " type: 'split' " " bottom: 'data' " " top: 'data' " " top: 'data_data_0_split_1' " "} " "layers: { " - " layer { " - " name: 'innerprod1' " - " type: 'inner_product' " - " } " + " name: 'innerprod1' " + " type: 'inner_product' " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " - " layer { " - " name: 'relu1' " - " type: 'relu' " - " } " + " name: 'relu1' " + " type: 'relu' " " bottom: 'innerprod1' " " top: 'innerprod1' " "} " "layers: { " - " layer { " - " name: 'innerprod1_relu1_0_split' " - " type: 'split' " - " } " + " name: 'innerprod1_relu1_0_split' " + " type: 'split' " " bottom: 'innerprod1' " " top: 'innerprod1' " " top: 'innerprod1_relu1_0_split_1' " "} " "layers: { " - " layer { " - " name: 'innerprod2' " - " type: 'inner_product' " - " } " + " name: 'innerprod2' " + " type: 'inner_product' " " bottom: 'innerprod1' " " top: 'innerprod2' " "} " "layers: { " - " layer { " - " name: 'loss1' " - " type: 'euclidean_loss' " - " } " + " name: 'loss1' " + " type: 'euclidean_loss' " " bottom: 'innerprod1_relu1_0_split_1' " " bottom: 'label' " "} " "layers: { " - " layer { " - " name: 'loss2' " - " type: 'euclidean_loss' " - " } " + " name: 'loss2' " + " type: 'euclidean_loss' " " bottom: 'innerprod2' " " bottom: 'data_data_0_split_1' " "} "; diff --git a/src/caffe/util/insert_splits.cpp b/src/caffe/util/insert_splits.cpp index 0e8ce07d145..05c1e9655b7 100644 --- a/src/caffe/util/insert_splits.cpp +++ b/src/caffe/util/insert_splits.cpp @@ -15,7 +15,7 @@ using std::make_pair; namespace caffe { -void insert_splits(const NetParameter& param, NetParameter* param_split) { +void InsertSplits(const NetParameter& param, NetParameter* param_split) { // Initialize by copying from the input NetParameter. param_split->CopyFrom(param); param_split->clear_layers(); @@ -57,7 +57,7 @@ void insert_splits(const NetParameter& param, NetParameter* param_split) { const string& layer_name = layer_idx_to_layer_name[-1]; const string& blob_name = param.input(i); LayerParameter* split_layer_param = param_split->add_layers(); - configure_split_layer(layer_name, blob_name, i, split_count, + ConfigureSplitLayer(layer_name, blob_name, i, split_count, split_layer_param); } } @@ -72,7 +72,7 @@ void insert_splits(const NetParameter& param, NetParameter* param_split) { if (split_count > 1) { const string& layer_name = layer_idx_to_layer_name[top_idx.first]; const string& blob_name = layer_param->bottom(j); - layer_param->set_bottom(j, get_split_blob_name(layer_name, + layer_param->set_bottom(j, SplitBlobName(layer_name, blob_name, top_idx.second, top_idx_to_bottom_split_idx[top_idx]++)); } } @@ -84,28 +84,27 @@ void insert_splits(const NetParameter& param, NetParameter* param_split) { const string& layer_name = layer_idx_to_layer_name[i]; const string& blob_name = layer_param->top(j); LayerParameter* split_layer_param = param_split->add_layers(); - configure_split_layer(layer_name, blob_name, j, split_count, + ConfigureSplitLayer(layer_name, blob_name, j, split_count, split_layer_param); } } } } -void configure_split_layer(const string& layer_name, const string& blob_name, +void ConfigureSplitLayer(const string& layer_name, const string& blob_name, const int blob_idx, const int split_count, LayerParameter* split_layer_param) { split_layer_param->Clear(); split_layer_param->add_bottom(blob_name); - split_layer_param->set_name( - get_split_layer_name(layer_name, blob_name, blob_idx)); + split_layer_param->set_name(SplitLayerName(layer_name, blob_name, blob_idx)); split_layer_param->set_type("split"); for (int k = 0; k < split_count; ++k) { split_layer_param->add_top( - get_split_blob_name(layer_name, blob_name, blob_idx, k)); + SplitBlobName(layer_name, blob_name, blob_idx, k)); } } -string get_split_layer_name(const string& layer_name, const string& blob_name, +string SplitLayerName(const string& layer_name, const string& blob_name, const int blob_idx) { ostringstream split_layer_name; split_layer_name << blob_name << "_" << layer_name << "_" << blob_idx @@ -113,7 +112,7 @@ string get_split_layer_name(const string& layer_name, const string& blob_name, return split_layer_name.str(); } -string get_split_blob_name(const string& layer_name, const string& blob_name, +string SplitBlobName(const string& layer_name, const string& blob_name, const int blob_idx, const int split_idx) { // 0th split top blob is given the same name as the bottom blob so that // computation is done 'in-place', saving a bit of time and memory. From 461b8745bd29ec43d5f46a65eba9825d6ee8e4eb Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 04:35:31 -0700 Subject: [PATCH 25/60] add upgrade_net_proto tool --- src/caffe/net.cpp | 2 +- src/caffe/util/upgrade_proto.cpp | 7 ++++- tools/upgrade_net_proto.cpp | 45 ++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tools/upgrade_net_proto.cpp diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 06db0ab7fa0..d7c922c1cbf 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -36,7 +36,7 @@ Net::Net(const string& param_file) { << "Failed to parse NetParameter file: " << param_file; LOG(ERROR) << "Parsed file as V0NetParameter: " << param_file; LOG(ERROR) << "Note that future Caffe releases will not support " - << "V0NetParameter; use tools/upgrade_net_param.testbin to upgrade " + << "V0NetParameter; use tools/upgrade_net_proto.testbin to upgrade " << "this and any other network proto files to the new format."; if (!UpgradeV0Net(v0_param, ¶m)) { LOG(ERROR) << "Warning: had one or more problems upgrading " diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 94546ac590e..02482529e19 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include "caffe/common.hpp" @@ -66,6 +67,9 @@ void UpgradeV0PaddingLayers(const V0NetParameter& param, LOG(FATAL) << "Unknown blob input " << blob_name << " to layer " << j; } const int top_idx = blob_name_to_last_top_idx[blob_name]; + if (top_idx == -1) { + continue; + } V0LayerConnection source_layer = param.layers(top_idx); if (source_layer.layer().type() == "padding") { // This layer has a padding layer as input -- check that it is a conv @@ -285,7 +289,8 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, } if (v0_layer_param.has_meanfile()) { if (type == "data") { - layer_param->mutable_data_param()->set_mean_file(v0_layer_param.meanfile()); + layer_param->mutable_data_param()->set_mean_file( + v0_layer_param.meanfile()); } else { LOG(ERROR) << "Unknown parameter meanfile for layer type " << type; is_fully_compatible = false; diff --git a/tools/upgrade_net_proto.cpp b/tools/upgrade_net_proto.cpp new file mode 100644 index 00000000000..8c50c51fbfc --- /dev/null +++ b/tools/upgrade_net_proto.cpp @@ -0,0 +1,45 @@ +// Copyright 2014 Jeff Donahue +// +// This is a script to upgrade "V0" network prototxts to the new format. +// Usage: +// upgrade_net_proto v0_net_proto_file_in net_proto_file_out + +#include +#include // NOLINT(readability/streams) +#include // NOLINT(readability/streams) + +#include "caffe/caffe.hpp" +#include "caffe/util/io.hpp" +#include "caffe/util/upgrade_proto.hpp" + +using std::ofstream; + +using namespace caffe; // NOLINT(build/namespaces) + +int main(int argc, char** argv) { + ::google::InitGoogleLogging(argv[0]); + if (argc < 3) { + LOG(ERROR) << "Usage: " + << "upgrade_net_proto v0_net_proto_file_in net_proto_file_out"; + return 0; + } + + V0NetParameter v0_net_param; + CHECK(ReadProtoFromTextFile(argv[1], &v0_net_param)) + << "Failed to parse input V0NetParameter file: " << argv[1]; + NetParameter upgraded_net_param; + bool success = UpgradeV0Net(v0_net_param, &upgraded_net_param); + if (!success) { + LOG(ERROR) << "Encountered one or more problems upgrading net param " + << "proto; see above."; + } + // TODO(jdonahue): figure out why WriteProtoToTextFile doesn't work + // (no file is created). + // WriteProtoToTextFile(upgraded_net_param, argv[2]); + ofstream output_proto; + output_proto.open(argv[2]); + output_proto << upgraded_net_param.DebugString(); + output_proto.close(); + LOG(ERROR) << "Wrote upgraded NetParameter proto to " << argv[2]; + return !success; +} From 6de9f0a1c5a930dd9f8406ec16a10f16fbe117a9 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 05:18:55 -0700 Subject: [PATCH 26/60] add test for input/input_dim and fix bug, wasn't copying input --- src/caffe/test/test_upgrade_proto.cpp | 12 ++++++++++-- src/caffe/util/upgrade_proto.cpp | 3 +++ tools/upgrade_net_proto.cpp | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index d6ca6d0752e..8538f114395 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -1107,8 +1107,6 @@ class V0UpgradeTest : public ::testing::Test { output_param_string, &expected_output_param)); NetParameter actual_output_param; UpgradeV0Net(input_param, &actual_output_param); - CHECK_EQ(expected_output_param.DebugString(), - actual_output_param.DebugString()); EXPECT_EQ(expected_output_param.DebugString(), actual_output_param.DebugString()); } @@ -1266,6 +1264,11 @@ TYPED_TEST(V0UpgradeTest, TestSimple) { TYPED_TEST(V0UpgradeTest, TestAllParams) { const string& input_proto = "name: 'CaffeNet' " + "input: 'input_data' " + "input_dim: 64 " + "input_dim: 3 " + "input_dim: 32 " + "input_dim: 32 " "layers { " " layer { " " name: 'data' " @@ -1408,6 +1411,11 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} "; const string& expected_output_proto = "name: 'CaffeNet' " + "input: 'input_data' " + "input_dim: 64 " + "input_dim: 3 " + "input_dim: 32 " + "input_dim: 32 " "layers { " " name: 'data' " " type: 'data' " diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 02482529e19..38bc2166e92 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -32,6 +32,9 @@ bool UpgradeV0Net(const V0NetParameter& v0_net_param_padding_layers, is_fully_compatible &= UpgradeV0LayerConnection(v0_net_param.layers(i), net_param->add_layers()); } + for (int i = 0; i < v0_net_param.input_size(); ++i) { + net_param->add_input(v0_net_param.input(i)); + } for (int i = 0; i < v0_net_param.input_dim_size(); ++i) { net_param->add_input_dim(v0_net_param.input_dim(i)); } diff --git a/tools/upgrade_net_proto.cpp b/tools/upgrade_net_proto.cpp index 8c50c51fbfc..a416878bfa7 100644 --- a/tools/upgrade_net_proto.cpp +++ b/tools/upgrade_net_proto.cpp @@ -18,7 +18,7 @@ using namespace caffe; // NOLINT(build/namespaces) int main(int argc, char** argv) { ::google::InitGoogleLogging(argv[0]); - if (argc < 3) { + if (argc != 3) { LOG(ERROR) << "Usage: " << "upgrade_net_proto v0_net_proto_file_in net_proto_file_out"; return 0; From 6179062dc633199bdb4aca597d813e172efc07e2 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 05:24:53 -0700 Subject: [PATCH 27/60] put inputs before layers in the proto so they print in that order --- src/caffe/proto/caffe.proto | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index b4872da1084..16626ebaea6 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -40,13 +40,13 @@ message FillerParameter { message NetParameter { optional string name = 1; // consider giving the network a name - repeated LayerParameter layers = 2; // a bunch of layers. // The input blobs to the network. - repeated string input = 3; + repeated string input = 2; // The dim of the input blobs. For each input blob there should be four // values specifying the num, channels, height and width of the input blob. // Thus, there should be a total of (4 * #input) numbers. - repeated int32 input_dim = 4; + repeated int32 input_dim = 3; + repeated LayerParameter layers = 4; // a bunch of layers. // Whether the network will force every layer to carry out backward operation. // If set False, then whether to carry out backward is determined // automatically according to the net structure and learning rates. From 2e330581c5970a76f9fcc9ebb903cb07adf097ec Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 06:20:55 -0700 Subject: [PATCH 28/60] LayerType enum --- include/caffe/util/upgrade_proto.hpp | 4 + src/caffe/layer_factory.cpp | 59 +++++---- src/caffe/proto/caffe.proto | 25 +++- src/caffe/test/test_split_layer.cpp | 174 +++++++++++++------------- src/caffe/test/test_upgrade_proto.cpp | 170 +++++++++++++++++++------ src/caffe/util/insert_splits.cpp | 2 +- src/caffe/util/upgrade_proto.cpp | 51 +++++++- 7 files changed, 324 insertions(+), 161 deletions(-) diff --git a/include/caffe/util/upgrade_proto.hpp b/include/caffe/util/upgrade_proto.hpp index b2504a30a8e..2fa45d234dc 100644 --- a/include/caffe/util/upgrade_proto.hpp +++ b/include/caffe/util/upgrade_proto.hpp @@ -6,6 +6,8 @@ #include "caffe/proto/caffe.pb.h" #include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h" +using std::string; + namespace caffe { // Perform all necessary transformations to upgrade a V0NetParameter into a @@ -23,6 +25,8 @@ void UpgradeV0PaddingLayers(const V0NetParameter& param, bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, LayerParameter* layer_param); +LayerParameter_LayerType UpgradeV0LayerType(const string& type); + } // namespace caffe #endif // CAFFE_UTIL_UPGRADE_PROTO_H_ diff --git a/src/caffe/layer_factory.cpp b/src/caffe/layer_factory.cpp index 90785af4656..22594113779 100644 --- a/src/caffe/layer_factory.cpp +++ b/src/caffe/layer_factory.cpp @@ -18,59 +18,56 @@ namespace caffe { // but we will leave it this way for now. template Layer* GetLayer(const LayerParameter& param) { - const std::string& type = param.type(); - if (type == "accuracy") { + const LayerParameter_LayerType& type = param.type(); + switch (type) { + case LayerParameter_LayerType_ACCURACY: return new AccuracyLayer(param); - } else if (type == "bnll") { + case LayerParameter_LayerType_BNLL: return new BNLLLayer(param); - } else if (type == "concat") { - return new ConcatLayer(param); - } else if (type == "conv") { + case LayerParameter_LayerType_CONVOLUTION: return new ConvolutionLayer(param); - } else if (type == "data") { + case LayerParameter_LayerType_CONCAT: + return new ConcatLayer(param); + case LayerParameter_LayerType_DATA: return new DataLayer(param); - } else if (type == "dropout") { + case LayerParameter_LayerType_DROPOUT: return new DropoutLayer(param); - } else if (type == "euclidean_loss") { + case LayerParameter_LayerType_EUCLIDEAN_LOSS: return new EuclideanLossLayer(param); - } else if (type == "flatten") { + case LayerParameter_LayerType_FLATTEN: return new FlattenLayer(param); - } else if (type == "hdf5_data") { + case LayerParameter_LayerType_HDF5_DATA: return new HDF5DataLayer(param); - } else if (type == "hdf5_output") { + case LayerParameter_LayerType_HDF5_OUTPUT: return new HDF5OutputLayer(param); - } else if (type == "images") { - return new ImagesLayer(param); - } else if (type == "im2col") { + case LayerParameter_LayerType_IM2COL: return new Im2colLayer(param); - } else if (type == "infogain_loss") { + case LayerParameter_LayerType_INFOGAIN_LOSS: return new InfogainLossLayer(param); - } else if (type == "innerproduct") { + case LayerParameter_LayerType_INNER_PRODUCT: return new InnerProductLayer(param); - } else if (type == "lrn") { + case LayerParameter_LayerType_LRN: return new LRNLayer(param); - } else if (type == "multinomial_logistic_loss") { + case LayerParameter_LayerType_MULTINOMIAL_LOGISTIC_LOSS: return new MultinomialLogisticLossLayer(param); - } else if (type == "padding") { - return new PaddingLayer(param); - } else if (type == "pool") { + case LayerParameter_LayerType_POOLING: return new PoolingLayer(param); - } else if (type == "relu") { + case LayerParameter_LayerType_RELU: return new ReLULayer(param); - } else if (type == "sigmoid") { + case LayerParameter_LayerType_SIGMOID: return new SigmoidLayer(param); - } else if (type == "softmax") { + case LayerParameter_LayerType_SOFTMAX: return new SoftmaxLayer(param); - } else if (type == "softmax_loss") { + case LayerParameter_LayerType_SOFTMAX_LOSS: return new SoftmaxWithLossLayer(param); - } else if (type == "split") { + case LayerParameter_LayerType_SPLIT: return new SplitLayer(param); - } else if (type == "tanh") { + case LayerParameter_LayerType_TANH: return new TanHLayer(param); - } else if (type == "window_data") { + case LayerParameter_LayerType_WINDOW_DATA: return new WindowDataLayer(param); - } else { - LOG(FATAL) << "Unknown layer name: " << type; + default: + LOG(FATAL) << "Unknown layer type: " << type; } // just to suppress old compiler warnings. return (Layer*)(NULL); diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 16626ebaea6..094971340b8 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -91,7 +91,30 @@ message SolverState { message LayerParameter { optional string name = 1; // the layer name - optional string type = 2; // the string to specify the layer type + enum LayerType { + ACCURACY = 0; + BNLL = 1; + CONVOLUTION = 2; + CONCAT = 3; + DATA = 4; + DROPOUT = 5; + EUCLIDEAN_LOSS = 6; + FLATTEN = 7; + HDF5_DATA = 8; + IM2COL = 9; + INFOGAIN_LOSS = 10; + INNER_PRODUCT = 11; + LRN = 12; + MULTINOMIAL_LOGISTIC_LOSS = 13; + POOLING = 14; + RELU = 15; + SIGMOID = 16; + SOFTMAX = 17; + SOFTMAX_LOSS = 18; + SPLIT = 19; + TANH = 20; + } + optional LayerType type = 2; // the layer type from the enum above repeated string bottom = 3; // the name of the bottom blobs repeated string top = 4; // the name of the top blobs diff --git a/src/caffe/test/test_split_layer.cpp b/src/caffe/test/test_split_layer.cpp index 6d3d6128f08..3c17b0e2450 100644 --- a/src/caffe/test/test_split_layer.cpp +++ b/src/caffe/test/test_split_layer.cpp @@ -189,19 +189,19 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion1) { "name: 'TestNetwork' " "layers: { " " name: 'data' " - " type: 'data' " + " type: DATA " " top: 'data' " " top: 'label' " "} " "layers: { " " name: 'innerprod' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod' " "} " "layers: { " " name: 'loss' " - " type: 'softmax_with_loss' " + " type: SOFTMAX_LOSS " " bottom: 'innerprod' " " bottom: 'label' " "} "; @@ -213,32 +213,32 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion2) { "name: 'TestNetwork' " "layers: { " " name: 'data' " - " type: 'data' " + " type: DATA " " top: 'data' " " top: 'label' " "} " "layers: { " " name: 'data_split' " - " type: 'split' " + " type: SPLIT " " bottom: 'data' " " top: 'data_split_0' " " top: 'data_split_1' " "} " "layers: { " " name: 'innerprod1' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data_split_0' " " top: 'innerprod1' " "} " "layers: { " " name: 'innerprod2' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data_split_1' " " top: 'innerprod2' " "} " "layers: { " " name: 'loss' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod1' " " bottom: 'innerprod2' " "} "; @@ -250,7 +250,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "name: 'CaffeNet' " "layers { " " name: 'data' " - " type: 'data' " + " type: DATA " " data_param { " " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " " mean_file: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " @@ -263,7 +263,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'conv1' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 96 " " kernel_size: 11 " @@ -286,13 +286,13 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'relu1' " - " type: 'relu' " + " type: RELU " " bottom: 'conv1' " " top: 'conv1' " "} " "layers { " " name: 'pool1' " - " type: 'pool' " + " type: POOLING " " pooling_param { " " pool: MAX " " kernel_size: 3 " @@ -303,7 +303,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'norm1' " - " type: 'lrn' " + " type: LRN " " lrn_param { " " local_size: 5 " " alpha: 0.0001 " @@ -314,7 +314,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'conv2' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 256 " " group: 2 " @@ -338,13 +338,13 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'relu2' " - " type: 'relu' " + " type: RELU " " bottom: 'conv2' " " top: 'conv2' " "} " "layers { " " name: 'pool2' " - " type: 'pool' " + " type: POOLING " " pooling_param { " " pool: MAX " " kernel_size: 3 " @@ -355,7 +355,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'norm2' " - " type: 'lrn' " + " type: LRN " " lrn_param { " " local_size: 5 " " alpha: 0.0001 " @@ -366,7 +366,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'conv3' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 384 " " kernel_size: 3 " @@ -389,13 +389,13 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'relu3' " - " type: 'relu' " + " type: RELU " " bottom: 'conv3' " " top: 'conv3' " "} " "layers { " " name: 'conv4' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 384 " " group: 2 " @@ -419,13 +419,13 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'relu4' " - " type: 'relu' " + " type: RELU " " bottom: 'conv4' " " top: 'conv4' " "} " "layers { " " name: 'conv5' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 256 " " group: 2 " @@ -449,13 +449,13 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'relu5' " - " type: 'relu' " + " type: RELU " " bottom: 'conv5' " " top: 'conv5' " "} " "layers { " " name: 'pool5' " - " type: 'pool' " + " type: POOLING " " pooling_param { " " kernel_size: 3 " " pool: MAX " @@ -466,7 +466,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'fc6' " - " type: 'innerproduct' " + " type: INNER_PRODUCT " " inner_product_param { " " num_output: 4096 " " weight_filler { " @@ -487,13 +487,13 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'relu6' " - " type: 'relu' " + " type: RELU " " bottom: 'fc6' " " top: 'fc6' " "} " "layers { " " name: 'drop6' " - " type: 'dropout' " + " type: DROPOUT " " dropout_param { " " dropout_ratio: 0.5 " " } " @@ -502,7 +502,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'fc7' " - " type: 'innerproduct' " + " type: INNER_PRODUCT " " inner_product_param { " " num_output: 4096 " " weight_filler { " @@ -523,13 +523,13 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'relu7' " - " type: 'relu' " + " type: RELU " " bottom: 'fc7' " " top: 'fc7' " "} " "layers { " " name: 'drop7' " - " type: 'dropout' " + " type: DROPOUT " " dropout_param { " " dropout_ratio: 0.5 " " } " @@ -538,7 +538,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'fc8' " - " type: 'innerproduct' " + " type: INNER_PRODUCT " " inner_product_param { " " num_output: 1000 " " weight_filler { " @@ -559,7 +559,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { "} " "layers { " " name: 'loss' " - " type: 'softmax_loss' " + " type: SOFTMAX_LOSS " " bottom: 'fc8' " " bottom: 'label' " "} "; @@ -571,25 +571,25 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionWithInPlace) { "name: 'TestNetwork' " "layers: { " " name: 'data' " - " type: 'data' " + " type: DATA " " top: 'data' " " top: 'label' " "} " "layers: { " " name: 'innerprod' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod' " "} " "layers: { " " name: 'relu' " - " type: 'relu' " + " type: RELU " " bottom: 'innerprod' " " top: 'innerprod' " "} " "layers: { " " name: 'loss' " - " type: 'softmax_with_loss' " + " type: SOFTMAX_LOSS " " bottom: 'innerprod' " " bottom: 'label' " "} "; @@ -601,37 +601,37 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertion) { "name: 'TestNetwork' " "layers: { " " name: 'data' " - " type: 'data' " + " type: DATA " " top: 'data' " " top: 'label' " "} " "layers: { " " name: 'innerprod1' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " " name: 'innerprod2' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod2' " "} " "layers: { " " name: 'innerprod3' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod3' " "} " "layers: { " " name: 'loss1' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod1' " " bottom: 'innerprod2' " "} " "layers: { " " name: 'loss2' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod2' " " bottom: 'innerprod3' " "} "; @@ -639,13 +639,13 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertion) { "name: 'TestNetwork' " "layers: { " " name: 'data' " - " type: 'data' " + " type: DATA " " top: 'data' " " top: 'label' " "} " "layers: { " " name: 'data_data_0_split' " - " type: 'split' " + " type: SPLIT " " bottom: 'data' " " top: 'data' " " top: 'data_data_0_split_1' " @@ -653,38 +653,38 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertion) { "} " "layers: { " " name: 'innerprod1' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " " name: 'innerprod2' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data_data_0_split_1' " " top: 'innerprod2' " "} " "layers: { " " name: 'innerprod2_innerprod2_0_split' " - " type: 'split' " + " type: SPLIT " " bottom: 'innerprod2' " " top: 'innerprod2' " " top: 'innerprod2_innerprod2_0_split_1' " "} " "layers: { " " name: 'innerprod3' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data_data_0_split_2' " " top: 'innerprod3' " "} " "layers: { " " name: 'loss1' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod1' " " bottom: 'innerprod2' " "} " "layers: { " " name: 'loss2' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod2_innerprod2_0_split_1' " " bottom: 'innerprod3' " "} "; @@ -696,43 +696,43 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertionTwoTop) { "name: 'TestNetwork' " "layers: { " " name: 'data' " - " type: 'data' " + " type: DATA " " top: 'data' " " top: 'label' " "} " "layers: { " " name: 'innerprod1' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " " name: 'innerprod2' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'label' " " top: 'innerprod2' " "} " "layers: { " " name: 'innerprod3' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod3' " "} " "layers: { " " name: 'innerprod4' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'label' " " top: 'innerprod4' " "} " "layers: { " " name: 'loss1' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod1' " " bottom: 'innerprod3' " "} " "layers: { " " name: 'loss2' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod2' " " bottom: 'innerprod4' " "} "; @@ -740,57 +740,57 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertionTwoTop) { "name: 'TestNetwork' " "layers: { " " name: 'data' " - " type: 'data' " + " type: DATA " " top: 'data' " " top: 'label' " "} " "layers: { " " name: 'data_data_0_split' " - " type: 'split' " + " type: SPLIT " " bottom: 'data' " " top: 'data' " " top: 'data_data_0_split_1' " "} " "layers: { " " name: 'label_data_1_split' " - " type: 'split' " + " type: SPLIT " " bottom: 'label' " " top: 'label' " " top: 'label_data_1_split_1' " "} " "layers: { " " name: 'innerprod1' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " " name: 'innerprod2' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'label' " " top: 'innerprod2' " "} " "layers: { " " name: 'innerprod3' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data_data_0_split_1' " " top: 'innerprod3' " "} " "layers: { " " name: 'innerprod4' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'label_data_1_split_1' " " top: 'innerprod4' " "} " "layers: { " " name: 'loss1' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod1' " " bottom: 'innerprod3' " "} " "layers: { " " name: 'loss2' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod2' " " bottom: 'innerprod4' " "} "; @@ -807,19 +807,19 @@ TYPED_TEST(SplitLayerInsertionTest, TestInputInsertion) { "input_dim: 227 " "layers: { " " name: 'innerprod1' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " " name: 'innerprod2' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod2' " "} " "layers: { " " name: 'loss' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod1' " " bottom: 'innerprod2' " "} "; @@ -832,26 +832,26 @@ TYPED_TEST(SplitLayerInsertionTest, TestInputInsertion) { "input_dim: 227 " "layers: { " " name: 'data_input_0_split' " - " type: 'split' " + " type: SPLIT " " bottom: 'data' " " top: 'data' " " top: 'data_input_0_split_1' " "} " "layers: { " " name: 'innerprod1' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " " name: 'innerprod2' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data_input_0_split_1' " " top: 'innerprod2' " "} " "layers: { " " name: 'loss' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod1' " " bottom: 'innerprod2' " "} "; @@ -863,37 +863,37 @@ TYPED_TEST(SplitLayerInsertionTest, TestWithInPlace) { "name: 'TestNetwork' " "layers: { " " name: 'data' " - " type: 'data' " + " type: DATA " " top: 'data' " " top: 'label' " "} " "layers: { " " name: 'innerprod1' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " " name: 'relu1' " - " type: 'relu' " + " type: RELU " " bottom: 'innerprod1' " " top: 'innerprod1' " "} " "layers: { " " name: 'innerprod2' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'innerprod1' " " top: 'innerprod2' " "} " "layers: { " " name: 'loss1' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod1' " " bottom: 'label' " "} " "layers: { " " name: 'loss2' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod2' " " bottom: 'data' " "} "; @@ -901,51 +901,51 @@ TYPED_TEST(SplitLayerInsertionTest, TestWithInPlace) { "name: 'TestNetwork' " "layers: { " " name: 'data' " - " type: 'data' " + " type: DATA " " top: 'data' " " top: 'label' " "} " "layers: { " " name: 'data_data_0_split' " - " type: 'split' " + " type: SPLIT " " bottom: 'data' " " top: 'data' " " top: 'data_data_0_split_1' " "} " "layers: { " " name: 'innerprod1' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'data' " " top: 'innerprod1' " "} " "layers: { " " name: 'relu1' " - " type: 'relu' " + " type: RELU " " bottom: 'innerprod1' " " top: 'innerprod1' " "} " "layers: { " " name: 'innerprod1_relu1_0_split' " - " type: 'split' " + " type: SPLIT " " bottom: 'innerprod1' " " top: 'innerprod1' " " top: 'innerprod1_relu1_0_split_1' " "} " "layers: { " " name: 'innerprod2' " - " type: 'inner_product' " + " type: INNER_PRODUCT " " bottom: 'innerprod1' " " top: 'innerprod2' " "} " "layers: { " " name: 'loss1' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod1_relu1_0_split_1' " " bottom: 'label' " "} " "layers: { " " name: 'loss2' " - " type: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " " bottom: 'innerprod2' " " bottom: 'data_data_0_split_1' " "} "; diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index 8538f114395..07c67bf6711 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -1196,7 +1196,7 @@ TYPED_TEST(V0UpgradeTest, TestSimple) { "name: 'CaffeNet' " "layers { " " name: 'data' " - " type: 'data' " + " type: DATA " " data_param { " " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " " mean_file: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " @@ -1209,7 +1209,7 @@ TYPED_TEST(V0UpgradeTest, TestSimple) { "} " "layers { " " name: 'conv1' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 96 " " kernel_size: 11 " @@ -1233,7 +1233,7 @@ TYPED_TEST(V0UpgradeTest, TestSimple) { "} " "layers { " " name: 'fc8' " - " type: 'innerproduct' " + " type: INNER_PRODUCT " " inner_product_param { " " num_output: 1000 " " weight_filler { " @@ -1254,7 +1254,7 @@ TYPED_TEST(V0UpgradeTest, TestSimple) { "} " "layers { " " name: 'loss' " - " type: 'softmax_loss' " + " type: SOFTMAX_LOSS " " bottom: 'fc8' " " bottom: 'label' " "} "; @@ -1408,6 +1408,60 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { " } " " bottom: 'fc6' " " bottom: 'label' " + "} " + "layers { " + " layer { " + " name: 'accuracy' " + " type: 'accuracy' " + " } " + "} " + "layers { " + " layer { " + " name: 'bnll' " + " type: 'bnll' " + " } " + "} " + "layers { " + " layer { " + " name: 'flatten' " + " type: 'flatten' " + " } " + "} " + "layers { " + " layer { " + " name: 'im2col' " + " type: 'im2col' " + " } " + "} " + "layers { " + " layer { " + " name: 'multinomial_logistic_loss' " + " type: 'multinomial_logistic_loss' " + " } " + "} " + "layers { " + " layer { " + " name: 'sigmoid' " + " type: 'sigmoid' " + " } " + "} " + "layers { " + " layer { " + " name: 'softmax' " + " type: 'softmax' " + " } " + "} " + "layers { " + " layer { " + " name: 'split' " + " type: 'split' " + " } " + "} " + "layers { " + " layer { " + " name: 'tanh' " + " type: 'tanh' " + " } " "} "; const string& expected_output_proto = "name: 'CaffeNet' " @@ -1418,7 +1472,7 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "input_dim: 32 " "layers { " " name: 'data' " - " type: 'data' " + " type: DATA " " data_param { " " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " " mean_file: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " @@ -1433,7 +1487,7 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " name: 'hdf5data' " - " type: 'hdf5_data' " + " type: HDF5_DATA " " hdf5_data_param { " " source: '/my/hdf5/data' " " batch_size: 256 " @@ -1442,7 +1496,7 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " name: 'conv1' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 96 " " bias_term: false " @@ -1467,7 +1521,7 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " name: 'pool1ave' " - " type: 'pool' " + " type: POOLING " " pooling_param { " " pool: AVE " " kernel_size: 3 " @@ -1478,7 +1532,7 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " name: 'pool1stoch' " - " type: 'pool' " + " type: POOLING " " pooling_param { " " pool: STOCHASTIC " " kernel_size: 4 " @@ -1489,7 +1543,7 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " name: 'concat' " - " type: 'concat' " + " type: CONCAT " " concat_param { " " concat_dim: 2 " " } " @@ -1499,7 +1553,7 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " name: 'norm1' " - " type: 'lrn' " + " type: LRN " " lrn_param { " " local_size: 5 " " alpha: 0.0001 " @@ -1510,7 +1564,7 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " name: 'fc6' " - " type: 'innerproduct' " + " type: INNER_PRODUCT " " inner_product_param { " " num_output: 4096 " " bias_term: false " @@ -1532,13 +1586,13 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " name: 'relu6' " - " type: 'relu' " + " type: RELU " " bottom: 'fc6' " " top: 'fc6' " "} " "layers { " " name: 'drop6' " - " type: 'dropout' " + " type: DROPOUT " " dropout_param { " " dropout_ratio: 0.2 " " } " @@ -1546,13 +1600,49 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { " top: 'fc6' " "} " "layers { " - " name: 'loss' " - " type: 'infogain_loss' " + " name: 'loss' " + " type: INFOGAIN_LOSS " " infogain_loss_param { " " source: '/my/infogain/matrix' " " } " " bottom: 'fc6' " " bottom: 'label' " + "} " + "layers { " + " name: 'accuracy' " + " type: ACCURACY " + "} " + "layers { " + " name: 'bnll' " + " type: BNLL " + "} " + "layers { " + " name: 'flatten' " + " type: FLATTEN " + "} " + "layers { " + " name: 'im2col' " + " type: IM2COL " + "} " + "layers { " + " name: 'multinomial_logistic_loss' " + " type: MULTINOMIAL_LOGISTIC_LOSS " + "} " + "layers { " + " name: 'sigmoid' " + " type: SIGMOID " + "} " + "layers { " + " name: 'softmax' " + " type: SOFTMAX " + "} " + "layers { " + " name: 'split' " + " type: SPLIT " + "} " + "layers { " + " name: 'tanh' " + " type: TANH " "} "; this->RunV0UpgradeTest(input_proto, expected_output_proto); } @@ -1927,7 +2017,7 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "name: 'CaffeNet' " "layers { " " name: 'data' " - " type: 'data' " + " type: DATA " " data_param { " " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " " mean_file: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " @@ -1940,7 +2030,7 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'conv1' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 96 " " kernel_size: 11 " @@ -1963,13 +2053,13 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'relu1' " - " type: 'relu' " + " type: RELU " " bottom: 'conv1' " " top: 'conv1' " "} " "layers { " " name: 'pool1' " - " type: 'pool' " + " type: POOLING " " pooling_param { " " pool: MAX " " kernel_size: 3 " @@ -1980,7 +2070,7 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'norm1' " - " type: 'lrn' " + " type: LRN " " lrn_param { " " local_size: 5 " " alpha: 0.0001 " @@ -1991,7 +2081,7 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'conv2' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 256 " " group: 2 " @@ -2015,13 +2105,13 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'relu2' " - " type: 'relu' " + " type: RELU " " bottom: 'conv2' " " top: 'conv2' " "} " "layers { " " name: 'pool2' " - " type: 'pool' " + " type: POOLING " " pooling_param { " " pool: MAX " " kernel_size: 3 " @@ -2032,7 +2122,7 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'norm2' " - " type: 'lrn' " + " type: LRN " " lrn_param { " " local_size: 5 " " alpha: 0.0001 " @@ -2043,7 +2133,7 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'conv3' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 384 " " kernel_size: 3 " @@ -2066,13 +2156,13 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'relu3' " - " type: 'relu' " + " type: RELU " " bottom: 'conv3' " " top: 'conv3' " "} " "layers { " " name: 'conv4' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 384 " " group: 2 " @@ -2096,13 +2186,13 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'relu4' " - " type: 'relu' " + " type: RELU " " bottom: 'conv4' " " top: 'conv4' " "} " "layers { " " name: 'conv5' " - " type: 'conv' " + " type: CONVOLUTION " " convolution_param { " " num_output: 256 " " group: 2 " @@ -2126,13 +2216,13 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'relu5' " - " type: 'relu' " + " type: RELU " " bottom: 'conv5' " " top: 'conv5' " "} " "layers { " " name: 'pool5' " - " type: 'pool' " + " type: POOLING " " pooling_param { " " kernel_size: 3 " " pool: MAX " @@ -2143,7 +2233,7 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'fc6' " - " type: 'innerproduct' " + " type: INNER_PRODUCT " " inner_product_param { " " num_output: 4096 " " weight_filler { " @@ -2164,13 +2254,13 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'relu6' " - " type: 'relu' " + " type: RELU " " bottom: 'fc6' " " top: 'fc6' " "} " "layers { " " name: 'drop6' " - " type: 'dropout' " + " type: DROPOUT " " dropout_param { " " dropout_ratio: 0.5 " " } " @@ -2179,7 +2269,7 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'fc7' " - " type: 'innerproduct' " + " type: INNER_PRODUCT " " inner_product_param { " " num_output: 4096 " " weight_filler { " @@ -2200,13 +2290,13 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'relu7' " - " type: 'relu' " + " type: RELU " " bottom: 'fc7' " " top: 'fc7' " "} " "layers { " " name: 'drop7' " - " type: 'dropout' " + " type: DROPOUT " " dropout_param { " " dropout_ratio: 0.5 " " } " @@ -2215,7 +2305,7 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'fc8' " - " type: 'innerproduct' " + " type: INNER_PRODUCT " " inner_product_param { " " num_output: 1000 " " weight_filler { " @@ -2236,7 +2326,7 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { "} " "layers { " " name: 'loss' " - " type: 'softmax_loss' " + " type: SOFTMAX_LOSS " " bottom: 'fc8' " " bottom: 'label' " "} "; diff --git a/src/caffe/util/insert_splits.cpp b/src/caffe/util/insert_splits.cpp index 05c1e9655b7..b9aeb37c71b 100644 --- a/src/caffe/util/insert_splits.cpp +++ b/src/caffe/util/insert_splits.cpp @@ -97,7 +97,7 @@ void ConfigureSplitLayer(const string& layer_name, const string& blob_name, split_layer_param->Clear(); split_layer_param->add_bottom(blob_name); split_layer_param->set_name(SplitLayerName(layer_name, blob_name, blob_idx)); - split_layer_param->set_type("split"); + split_layer_param->set_type(LayerParameter_LayerType_SPLIT); for (int k = 0; k < split_count; ++k) { split_layer_param->add_top( SplitBlobName(layer_name, blob_name, blob_idx, k)); diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 38bc2166e92..9aefcc1b196 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -118,7 +118,7 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, } const string& type = v0_layer_param.type(); if (v0_layer_param.has_type()) { - layer_param->set_type(type); + layer_param->set_type(UpgradeV0LayerType(type)); } if (v0_layer_param.has_num_output()) { if (type == "conv") { @@ -359,4 +359,53 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, return is_fully_compatible; } +LayerParameter_LayerType UpgradeV0LayerType(const string& type) { + if (type == "accuracy") { + return LayerParameter_LayerType_ACCURACY; + } else if (type == "bnll") { + return LayerParameter_LayerType_BNLL; + } else if (type == "conv") { + return LayerParameter_LayerType_CONVOLUTION; + } else if (type == "concat") { + return LayerParameter_LayerType_CONCAT; + } else if (type == "data") { + return LayerParameter_LayerType_DATA; + } else if (type == "dropout") { + return LayerParameter_LayerType_DROPOUT; + } else if (type == "euclidean_loss") { + return LayerParameter_LayerType_EUCLIDEAN_LOSS; + } else if (type == "flatten") { + return LayerParameter_LayerType_FLATTEN; + } else if (type == "hdf5_data") { + return LayerParameter_LayerType_HDF5_DATA; + } else if (type == "im2col") { + return LayerParameter_LayerType_IM2COL; + } else if (type == "infogain_loss") { + return LayerParameter_LayerType_INFOGAIN_LOSS; + } else if (type == "innerproduct") { + return LayerParameter_LayerType_INNER_PRODUCT; + } else if (type == "lrn") { + return LayerParameter_LayerType_LRN; + } else if (type == "multinomial_logistic_loss") { + return LayerParameter_LayerType_MULTINOMIAL_LOGISTIC_LOSS; + } else if (type == "pool") { + return LayerParameter_LayerType_POOLING; + } else if (type == "relu") { + return LayerParameter_LayerType_RELU; + } else if (type == "sigmoid") { + return LayerParameter_LayerType_SIGMOID; + } else if (type == "softmax") { + return LayerParameter_LayerType_SOFTMAX; + } else if (type == "softmax_loss") { + return LayerParameter_LayerType_SOFTMAX_LOSS; + } else if (type == "split") { + return LayerParameter_LayerType_SPLIT; + } else if (type == "tanh") { + return LayerParameter_LayerType_TANH; + } else { + LOG(FATAL) << "Unknown layer name: " << type; + return LayerParameter_LayerType(-1); + } +} + } // namespace caffe From 1fbb5bb3c8459bb5bf4e20da8f907c4dd3fd0b66 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 06:22:29 -0700 Subject: [PATCH 29/60] convert existing models to new format (used tools/upgrade_net_proto with no manual editing) --- examples/cifar10/cifar10_full_test.prototxt | 293 ++++++++-------- examples/cifar10/cifar10_full_train.prototxt | 287 ++++++++-------- examples/cifar10/cifar10_quick_test.prototxt | 302 ++++++++--------- examples/cifar10/cifar10_quick_train.prototxt | 297 ++++++++--------- examples/imagenet/imagenet_deploy.prototxt | 298 ++++++++--------- examples/imagenet/imagenet_train.prototxt | 314 +++++++++--------- examples/imagenet/imagenet_val.prototxt | 242 +++++++------- examples/lenet/lenet.prototxt | 101 +++--- examples/lenet/lenet_test.prototxt | 102 +++--- examples/lenet/lenet_train.prototxt | 112 +++---- 10 files changed, 1112 insertions(+), 1236 deletions(-) diff --git a/examples/cifar10/cifar10_full_test.prototxt b/examples/cifar10/cifar10_full_test.prototxt index a77c7d268da..ada373a55cb 100644 --- a/examples/cifar10/cifar10_full_test.prototxt +++ b/examples/cifar10/cifar10_full_test.prototxt @@ -1,193 +1,178 @@ name: "CIFAR10_full_test" layers { - layer { - name: "cifar" - type: "data" - source: "cifar10-leveldb/cifar-test-leveldb" - meanfile: "mean.binaryproto" - batchsize: 100 - } - top: "data" - top: "label" + name: "cifar" + type: DATA + top: "data" + top: "label" + data_param { + source: "cifar10-leveldb/cifar-test-leveldb" + mean_file: "mean.binaryproto" + batch_size: 100 + } } -# ------------------------ layer 1 ----------------------------- layers { - layer { - name: "conv1" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.0001 - } - bias_filler { - type: "constant" - } - blobs_lr: 1. - blobs_lr: 2. - } - bottom: "data" - top: "conv1" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.0001 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "pool1" - type: "pool" - kernelsize: 3 - stride: 2 - pool: MAX - } - bottom: "conv1" - top: "pool1" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { + pool: MAX + kernel_size: 3 + stride: 2 + } } layers { - layer { - name: "relu1" - type: "relu" - } - bottom: "pool1" - top: "pool1" + name: "relu1" + type: RELU + bottom: "pool1" + top: "pool1" } layers { - layer { - name: "norm1" - type: "lrn" + name: "norm1" + type: LRN + bottom: "pool1" + top: "norm1" + lrn_param { local_size: 3 - alpha: 0.00005 + alpha: 5e-05 beta: 0.75 } - bottom: "pool1" - top: "norm1" } -# --------------------------- layer 2 ------------------------ layers { - layer { - name: "conv2" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.01 - } - bias_filler { - type: "constant" - } - blobs_lr: 1. - blobs_lr: 2. - } - bottom: "norm1" - top: "conv2" + name: "conv2" + type: CONVOLUTION + bottom: "norm1" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.01 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "relu2" - type: "relu" - } - bottom: "conv2" - top: "conv2" + name: "relu2" + type: RELU + bottom: "conv2" + top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv2" - top: "pool2" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } layers { - layer { - name: "norm2" - type: "lrn" + name: "norm2" + type: LRN + bottom: "pool2" + top: "norm2" + lrn_param { local_size: 3 - alpha: 0.00005 + alpha: 5e-05 beta: 0.75 } - bottom: "pool2" - top: "norm2" } -#-----------------------layer 3------------------------- layers { - layer { - name: "conv3" - type: "conv" - num_output: 64 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.01 - } - bias_filler { - type: "constant" - } - } - bottom: "norm2" - top: "conv3" + name: "conv3" + type: CONVOLUTION + bottom: "norm2" + top: "conv3" + convolution_param { + num_output: 64 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.01 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "relu3" - type: "relu" - } - bottom: "conv3" - top: "conv3" + name: "relu3" + type: RELU + bottom: "conv3" + top: "conv3" } layers { - layer { - name: "pool3" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv3" - top: "pool3" + name: "pool3" + type: POOLING + bottom: "conv3" + top: "pool3" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } -#--------------------------layer 4------------------------ layers { - layer { - name: "ip1" - type: "innerproduct" - num_output: 10 - weight_filler { - type: "gaussian" - std: 0.01 - } - bias_filler { - type: "constant" - } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 250. - weight_decay: 0. - } - bottom: "pool3" - top: "ip1" + name: "ip1" + type: INNER_PRODUCT + bottom: "pool3" + top: "ip1" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 250 + weight_decay: 0 + inner_product_param { + num_output: 10 + weight_filler { + type: "gaussian" + std: 0.01 + } + bias_filler { + type: "constant" + } + } } -#-----------------------output------------------------ layers { - layer { - name: "prob" - type: "softmax" - } - bottom: "ip1" - top: "prob" + name: "prob" + type: SOFTMAX + bottom: "ip1" + top: "prob" } layers { - layer { - name: "accuracy" - type: "accuracy" - } + name: "accuracy" + type: ACCURACY bottom: "prob" bottom: "label" top: "accuracy" diff --git a/examples/cifar10/cifar10_full_train.prototxt b/examples/cifar10/cifar10_full_train.prototxt index 28e4612c04e..56c9306e2dc 100644 --- a/examples/cifar10/cifar10_full_train.prototxt +++ b/examples/cifar10/cifar10_full_train.prototxt @@ -1,185 +1,172 @@ name: "CIFAR10_full_train" layers { - layer { - name: "cifar" - type: "data" - source: "cifar10-leveldb/cifar-train-leveldb" - meanfile: "mean.binaryproto" - batchsize: 100 - } - top: "data" - top: "label" + name: "cifar" + type: DATA + top: "data" + top: "label" + data_param { + source: "cifar10-leveldb/cifar-train-leveldb" + mean_file: "mean.binaryproto" + batch_size: 100 + } } -# ------------------------ layer 1 ----------------------------- layers { - layer { - name: "conv1" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.0001 - } - bias_filler { - type: "constant" - } - blobs_lr: 1. - blobs_lr: 2. - } - bottom: "data" - top: "conv1" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.0001 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "pool1" - type: "pool" - kernelsize: 3 - stride: 2 - pool: MAX - } - bottom: "conv1" - top: "pool1" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { + pool: MAX + kernel_size: 3 + stride: 2 + } } layers { - layer { - name: "relu1" - type: "relu" - } - bottom: "pool1" - top: "pool1" + name: "relu1" + type: RELU + bottom: "pool1" + top: "pool1" } layers { - layer { - name: "norm1" - type: "lrn" + name: "norm1" + type: LRN + bottom: "pool1" + top: "norm1" + lrn_param { local_size: 3 - alpha: 0.00005 + alpha: 5e-05 beta: 0.75 } - bottom: "pool1" - top: "norm1" } -# --------------------------- layer 2 ------------------------ layers { - layer { - name: "conv2" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.01 - } - bias_filler { - type: "constant" - } - blobs_lr: 1. - blobs_lr: 2. - } - bottom: "norm1" - top: "conv2" + name: "conv2" + type: CONVOLUTION + bottom: "norm1" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.01 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "relu2" - type: "relu" - } - bottom: "conv2" - top: "conv2" + name: "relu2" + type: RELU + bottom: "conv2" + top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv2" - top: "pool2" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } layers { - layer { - name: "norm2" - type: "lrn" + name: "norm2" + type: LRN + bottom: "pool2" + top: "norm2" + lrn_param { local_size: 3 - alpha: 0.00005 + alpha: 5e-05 beta: 0.75 } - bottom: "pool2" - top: "norm2" } -#-----------------------layer 3------------------------- layers { - layer { - name: "conv3" - type: "conv" - num_output: 64 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.01 - } - bias_filler { - type: "constant" - } - } - bottom: "norm2" - top: "conv3" + name: "conv3" + type: CONVOLUTION + bottom: "norm2" + top: "conv3" + convolution_param { + num_output: 64 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.01 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "relu3" - type: "relu" - } - bottom: "conv3" - top: "conv3" + name: "relu3" + type: RELU + bottom: "conv3" + top: "conv3" } layers { - layer { - name: "pool3" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv3" - top: "pool3" + name: "pool3" + type: POOLING + bottom: "conv3" + top: "pool3" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } -#--------------------------layer 4------------------------ layers { - layer { - name: "ip1" - type: "innerproduct" - num_output: 10 - weight_filler { - type: "gaussian" - std: 0.01 - } - bias_filler { - type: "constant" - } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 250. - weight_decay: 0. - } - bottom: "pool3" - top: "ip1" + name: "ip1" + type: INNER_PRODUCT + bottom: "pool3" + top: "ip1" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 250 + weight_decay: 0 + inner_product_param { + num_output: 10 + weight_filler { + type: "gaussian" + std: 0.01 + } + bias_filler { + type: "constant" + } + } } -#-----------------------output------------------------ layers { - layer { - name: "loss" - type: "softmax_loss" - } - bottom: "ip1" - bottom: "label" + name: "loss" + type: SOFTMAX_LOSS + bottom: "ip1" + bottom: "label" } diff --git a/examples/cifar10/cifar10_quick_test.prototxt b/examples/cifar10/cifar10_quick_test.prototxt index a937df57d00..f03cffc253e 100644 --- a/examples/cifar10/cifar10_quick_test.prototxt +++ b/examples/cifar10/cifar10_quick_test.prototxt @@ -1,191 +1,175 @@ # quick config name: "CIFAR10_quick_test" layers { - layer { - name: "cifar" - type: "data" - source: "cifar10-leveldb/cifar-test-leveldb" - meanfile: "mean.binaryproto" - batchsize: 100 - } - top: "data" - top: "label" + name: "cifar" + type: DATA + top: "data" + top: "label" + data_param { + source: "cifar10-leveldb/cifar-test-leveldb" + mean_file: "mean.binaryproto" + batch_size: 100 + } } -# ------------------------ layer 1 ----------------------------- layers { - layer { - name: "conv1" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.0001 - } - bias_filler { - type: "constant" - } - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "data" - top: "conv1" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.0001 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "pool1" - type: "pool" - kernelsize: 3 - stride: 2 - pool: MAX - } - bottom: "conv1" - top: "pool1" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { + pool: MAX + kernel_size: 3 + stride: 2 + } } layers { - layer { - name: "relu1" - type: "relu" - } - bottom: "pool1" - top: "pool1" + name: "relu1" + type: RELU + bottom: "pool1" + top: "pool1" } -# --------------------------- layer 2 ------------------------ layers { - layer { - name: "conv2" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.01 - } - bias_filler { - type: "constant" - } - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "pool1" - top: "conv2" + name: "conv2" + type: CONVOLUTION + bottom: "pool1" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.01 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "relu2" - type: "relu" - } - bottom: "conv2" - top: "conv2" + name: "relu2" + type: RELU + bottom: "conv2" + top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv2" - top: "pool2" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } -#-----------------------layer 3------------------------- layers { - layer { - name: "conv3" - type: "conv" - num_output: 64 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.01 - } - bias_filler { - type: "constant" - } - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "pool2" - top: "conv3" + name: "conv3" + type: CONVOLUTION + bottom: "pool2" + top: "conv3" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 64 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.01 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "relu3" - type: "relu" - } - bottom: "conv3" - top: "conv3" + name: "relu3" + type: RELU + bottom: "conv3" + top: "conv3" } layers { - layer { - name: "pool3" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv3" - top: "pool3" + name: "pool3" + type: POOLING + bottom: "conv3" + top: "pool3" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } -#--------------------------layer 4------------------------ layers { - layer { - name: "ip1" - type: "innerproduct" - num_output: 64 - weight_filler { - type: "gaussian" - std: 0.1 - } - bias_filler { - type: "constant" - } - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "pool3" - top: "ip1" + name: "ip1" + type: INNER_PRODUCT + bottom: "pool3" + top: "ip1" + blobs_lr: 1 + blobs_lr: 2 + inner_product_param { + num_output: 64 + weight_filler { + type: "gaussian" + std: 0.1 + } + bias_filler { + type: "constant" + } + } } -#--------------------------layer 5------------------------ layers { - layer { - name: "ip2" - type: "innerproduct" - num_output: 10 - weight_filler { - type: "gaussian" - std: 0.1 - } - bias_filler { - type: "constant" - } - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "ip1" - top: "ip2" + name: "ip2" + type: INNER_PRODUCT + bottom: "ip1" + top: "ip2" + blobs_lr: 1 + blobs_lr: 2 + inner_product_param { + num_output: 10 + weight_filler { + type: "gaussian" + std: 0.1 + } + bias_filler { + type: "constant" + } + } } -#-----------------------output------------------------ layers { - layer { - name: "prob" - type: "softmax" - } - bottom: "ip2" - top: "prob" + name: "prob" + type: SOFTMAX + bottom: "ip2" + top: "prob" } layers { - layer { - name: "accuracy" - type: "accuracy" - } + name: "accuracy" + type: ACCURACY bottom: "prob" bottom: "label" top: "accuracy" diff --git a/examples/cifar10/cifar10_quick_train.prototxt b/examples/cifar10/cifar10_quick_train.prototxt index 2d3a10a6c7f..de5b6c32c5d 100644 --- a/examples/cifar10/cifar10_quick_train.prototxt +++ b/examples/cifar10/cifar10_quick_train.prototxt @@ -1,183 +1,168 @@ -# quick config name: "CIFAR10_quick_train" layers { - layer { - name: "cifar" - type: "data" - source: "cifar10-leveldb/cifar-train-leveldb" - meanfile: "mean.binaryproto" - batchsize: 100 - } - top: "data" - top: "label" + name: "cifar" + type: DATA + top: "data" + top: "label" + data_param { + source: "cifar10-leveldb/cifar-train-leveldb" + mean_file: "mean.binaryproto" + batch_size: 100 + } } -# ------------------------ layer 1 ----------------------------- layers { - layer { - name: "conv1" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.0001 - } - bias_filler { - type: "constant" - } - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "data" - top: "conv1" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.0001 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "pool1" - type: "pool" - kernelsize: 3 - stride: 2 - pool: MAX - } - bottom: "conv1" - top: "pool1" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { + pool: MAX + kernel_size: 3 + stride: 2 + } } layers { - layer { - name: "relu1" - type: "relu" - } - bottom: "pool1" - top: "pool1" + name: "relu1" + type: RELU + bottom: "pool1" + top: "pool1" } -# --------------------------- layer 2 ------------------------ layers { - layer { - name: "conv2" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.01 - } - bias_filler { - type: "constant" - } - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "pool1" - top: "conv2" + name: "conv2" + type: CONVOLUTION + bottom: "pool1" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.01 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "relu2" - type: "relu" - } - bottom: "conv2" - top: "conv2" + name: "relu2" + type: RELU + bottom: "conv2" + top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv2" - top: "pool2" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } -#-----------------------layer 3------------------------- layers { - layer { - name: "conv3" - type: "conv" - num_output: 64 - kernelsize: 5 - pad: 2 - stride: 1 - weight_filler { - type: "gaussian" - std: 0.01 - } - bias_filler { - type: "constant" - } - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "pool2" - top: "conv3" + name: "conv3" + type: CONVOLUTION + bottom: "pool2" + top: "conv3" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 64 + pad: 2 + kernel_size: 5 + stride: 1 + weight_filler { + type: "gaussian" + std: 0.01 + } + bias_filler { + type: "constant" + } + } } layers { - layer { - name: "relu3" - type: "relu" - } - bottom: "conv3" - top: "conv3" + name: "relu3" + type: RELU + bottom: "conv3" + top: "conv3" } layers { - layer { - name: "pool3" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv3" - top: "pool3" + name: "pool3" + type: POOLING + bottom: "conv3" + top: "pool3" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } -#--------------------------layer 4------------------------ layers { - layer { - name: "ip1" - type: "innerproduct" - num_output: 64 - weight_filler { - type: "gaussian" - std: 0.1 - } - bias_filler { - type: "constant" - } - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "pool3" - top: "ip1" + name: "ip1" + type: INNER_PRODUCT + bottom: "pool3" + top: "ip1" + blobs_lr: 1 + blobs_lr: 2 + inner_product_param { + num_output: 64 + weight_filler { + type: "gaussian" + std: 0.1 + } + bias_filler { + type: "constant" + } + } } -#--------------------------layer 5------------------------ layers { - layer { - name: "ip2" - type: "innerproduct" - num_output: 10 - weight_filler { - type: "gaussian" - std: 0.1 - } - bias_filler { - type: "constant" - } - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "ip1" - top: "ip2" + name: "ip2" + type: INNER_PRODUCT + bottom: "ip1" + top: "ip2" + blobs_lr: 1 + blobs_lr: 2 + inner_product_param { + num_output: 10 + weight_filler { + type: "gaussian" + std: 0.1 + } + bias_filler { + type: "constant" + } + } } -#-----------------------output------------------------ layers { - layer { - name: "loss" - type: "softmax_loss" - } - bottom: "ip2" - bottom: "label" + name: "loss" + type: SOFTMAX_LOSS + bottom: "ip2" + bottom: "label" } diff --git a/examples/imagenet/imagenet_deploy.prototxt b/examples/imagenet/imagenet_deploy.prototxt index 0b1f41ab914..37ab9221da3 100644 --- a/examples/imagenet/imagenet_deploy.prototxt +++ b/examples/imagenet/imagenet_deploy.prototxt @@ -5,11 +5,17 @@ input_dim: 3 input_dim: 227 input_dim: 227 layers { - layer { - name: "conv1" - type: "conv" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 96 - kernelsize: 11 + kernel_size: 11 stride: 4 weight_filler { type: "gaussian" @@ -17,210 +23,200 @@ layers { } bias_filler { type: "constant" - value: 0. + value: 0 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "data" - top: "conv1" } layers { - layer { - name: "relu1" - type: "relu" - } + name: "relu1" + type: RELU bottom: "conv1" top: "conv1" } layers { - layer { - name: "pool1" - type: "pool" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv1" - top: "pool1" } layers { - layer { - name: "norm1" - type: "lrn" + name: "norm1" + type: LRN + bottom: "pool1" + top: "norm1" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool1" - top: "norm1" } layers { - layer { - name: "conv2" - type: "conv" + name: "conv2" + type: CONVOLUTION + bottom: "norm1" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 256 - group: 2 - kernelsize: 5 pad: 2 + kernel_size: 5 + group: 2 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "norm1" - top: "conv2" } layers { - layer { - name: "relu2" - type: "relu" - } + name: "relu2" + type: RELU bottom: "conv2" top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv2" - top: "pool2" } layers { - layer { - name: "norm2" - type: "lrn" + name: "norm2" + type: LRN + bottom: "pool2" + top: "norm2" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool2" - top: "norm2" } layers { - layer { - name: "conv3" - type: "conv" + name: "conv3" + type: CONVOLUTION + bottom: "norm2" + top: "conv3" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 384 - kernelsize: 3 pad: 1 + kernel_size: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 0. + value: 0 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "norm2" - top: "conv3" } layers { - layer { - name: "relu3" - type: "relu" - } + name: "relu3" + type: RELU bottom: "conv3" top: "conv3" } layers { - layer { - name: "conv4" - type: "conv" + name: "conv4" + type: CONVOLUTION + bottom: "conv3" + top: "conv4" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 384 - group: 2 - kernelsize: 3 pad: 1 + kernel_size: 3 + group: 2 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "conv3" - top: "conv4" } layers { - layer { - name: "relu4" - type: "relu" - } + name: "relu4" + type: RELU bottom: "conv4" top: "conv4" } layers { - layer { - name: "conv5" - type: "conv" + name: "conv5" + type: CONVOLUTION + bottom: "conv4" + top: "conv5" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 256 - group: 2 - kernelsize: 3 pad: 1 + kernel_size: 3 + group: 2 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "conv4" - top: "conv5" } layers { - layer { - name: "relu5" - type: "relu" - } + name: "relu5" + type: RELU bottom: "conv5" top: "conv5" } layers { - layer { - name: "pool5" - type: "pool" - kernelsize: 3 + name: "pool5" + type: POOLING + bottom: "conv5" + top: "pool5" + pooling_param { pool: MAX + kernel_size: 3 stride: 2 } - bottom: "conv5" - top: "pool5" } layers { - layer { - name: "fc6" - type: "innerproduct" + name: "fc6" + type: INNER_PRODUCT + bottom: "pool5" + top: "fc6" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 4096 weight_filler { type: "gaussian" @@ -228,37 +224,35 @@ layers { } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pool5" - top: "fc6" } layers { - layer { - name: "relu6" - type: "relu" - } + name: "relu6" + type: RELU bottom: "fc6" top: "fc6" } layers { - layer { - name: "drop6" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop6" + type: DROPOUT bottom: "fc6" top: "fc6" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc7" - type: "innerproduct" + name: "fc7" + type: INNER_PRODUCT + bottom: "fc6" + top: "fc7" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 4096 weight_filler { type: "gaussian" @@ -266,37 +260,35 @@ layers { } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "fc6" - top: "fc7" } layers { - layer { - name: "relu7" - type: "relu" - } + name: "relu7" + type: RELU bottom: "fc7" top: "fc7" } layers { - layer { - name: "drop7" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop7" + type: DROPOUT bottom: "fc7" top: "fc7" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc8" - type: "innerproduct" + name: "fc8" + type: INNER_PRODUCT + bottom: "fc7" + top: "fc8" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 1000 weight_filler { type: "gaussian" @@ -306,19 +298,11 @@ layers { type: "constant" value: 0 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "fc7" - top: "fc8" } layers { - layer { - name: "prob" - type: "softmax" - } + name: "prob" + type: SOFTMAX bottom: "fc8" top: "prob" } diff --git a/examples/imagenet/imagenet_train.prototxt b/examples/imagenet/imagenet_train.prototxt index 9764687c35f..b34a9b49b07 100644 --- a/examples/imagenet/imagenet_train.prototxt +++ b/examples/imagenet/imagenet_train.prototxt @@ -1,23 +1,29 @@ name: "CaffeNet" layers { - layer { - name: "data" - type: "data" + name: "data" + type: DATA + top: "data" + top: "label" + data_param { source: "ilvsrc12_train_leveldb" - meanfile: "../../data/ilsvrc12/imagenet_mean.binaryproto" - batchsize: 256 - cropsize: 227 + mean_file: "../../data/ilsvrc12/imagenet_mean.binaryproto" + batch_size: 256 + crop_size: 227 mirror: true } - top: "data" - top: "label" } layers { - layer { - name: "conv1" - type: "conv" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 96 - kernelsize: 11 + kernel_size: 11 stride: 4 weight_filler { type: "gaussian" @@ -25,210 +31,200 @@ layers { } bias_filler { type: "constant" - value: 0. + value: 0 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "data" - top: "conv1" } layers { - layer { - name: "relu1" - type: "relu" - } + name: "relu1" + type: RELU bottom: "conv1" top: "conv1" } layers { - layer { - name: "pool1" - type: "pool" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv1" - top: "pool1" } layers { - layer { - name: "norm1" - type: "lrn" + name: "norm1" + type: LRN + bottom: "pool1" + top: "norm1" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool1" - top: "norm1" } layers { - layer { - name: "conv2" - type: "conv" + name: "conv2" + type: CONVOLUTION + bottom: "norm1" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 256 - group: 2 - kernelsize: 5 pad: 2 + kernel_size: 5 + group: 2 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "norm1" - top: "conv2" } layers { - layer { - name: "relu2" - type: "relu" - } + name: "relu2" + type: RELU bottom: "conv2" top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv2" - top: "pool2" } layers { - layer { - name: "norm2" - type: "lrn" + name: "norm2" + type: LRN + bottom: "pool2" + top: "norm2" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool2" - top: "norm2" } layers { - layer { - name: "conv3" - type: "conv" + name: "conv3" + type: CONVOLUTION + bottom: "norm2" + top: "conv3" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 384 - kernelsize: 3 pad: 1 + kernel_size: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 0. + value: 0 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "norm2" - top: "conv3" } layers { - layer { - name: "relu3" - type: "relu" - } + name: "relu3" + type: RELU bottom: "conv3" top: "conv3" } layers { - layer { - name: "conv4" - type: "conv" + name: "conv4" + type: CONVOLUTION + bottom: "conv3" + top: "conv4" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 384 - group: 2 - kernelsize: 3 pad: 1 + kernel_size: 3 + group: 2 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "conv3" - top: "conv4" } layers { - layer { - name: "relu4" - type: "relu" - } + name: "relu4" + type: RELU bottom: "conv4" top: "conv4" } layers { - layer { - name: "conv5" - type: "conv" + name: "conv5" + type: CONVOLUTION + bottom: "conv4" + top: "conv5" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 256 - group: 2 - kernelsize: 3 pad: 1 + kernel_size: 3 + group: 2 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "conv4" - top: "conv5" } layers { - layer { - name: "relu5" - type: "relu" - } + name: "relu5" + type: RELU bottom: "conv5" top: "conv5" } layers { - layer { - name: "pool5" - type: "pool" - kernelsize: 3 + name: "pool5" + type: POOLING + bottom: "conv5" + top: "pool5" + pooling_param { pool: MAX + kernel_size: 3 stride: 2 } - bottom: "conv5" - top: "pool5" } layers { - layer { - name: "fc6" - type: "innerproduct" + name: "fc6" + type: INNER_PRODUCT + bottom: "pool5" + top: "fc6" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 4096 weight_filler { type: "gaussian" @@ -236,37 +232,35 @@ layers { } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pool5" - top: "fc6" } layers { - layer { - name: "relu6" - type: "relu" - } + name: "relu6" + type: RELU bottom: "fc6" top: "fc6" } layers { - layer { - name: "drop6" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop6" + type: DROPOUT bottom: "fc6" top: "fc6" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc7" - type: "innerproduct" + name: "fc7" + type: INNER_PRODUCT + bottom: "fc6" + top: "fc7" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 4096 weight_filler { type: "gaussian" @@ -274,37 +268,35 @@ layers { } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "fc6" - top: "fc7" } layers { - layer { - name: "relu7" - type: "relu" - } + name: "relu7" + type: RELU bottom: "fc7" top: "fc7" } layers { - layer { - name: "drop7" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop7" + type: DROPOUT bottom: "fc7" top: "fc7" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc8" - type: "innerproduct" + name: "fc8" + type: INNER_PRODUCT + bottom: "fc7" + top: "fc8" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 1000 weight_filler { type: "gaussian" @@ -314,19 +306,11 @@ layers { type: "constant" value: 0 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "fc7" - top: "fc8" } layers { - layer { - name: "loss" - type: "softmax_loss" - } + name: "loss" + type: SOFTMAX_LOSS bottom: "fc8" bottom: "label" } diff --git a/examples/imagenet/imagenet_val.prototxt b/examples/imagenet/imagenet_val.prototxt index a004b74f626..2f1ead7c14a 100644 --- a/examples/imagenet/imagenet_val.prototxt +++ b/examples/imagenet/imagenet_val.prototxt @@ -1,244 +1,226 @@ name: "CaffeNet" layers { - layer { - name: "data" - type: "data" + name: "data" + type: DATA + top: "data" + top: "label" + data_param { source: "ilvsrc12_val_leveldb" - meanfile: "../../data/ilsvrc12/imagenet_mean.binaryproto" - batchsize: 50 - cropsize: 227 + mean_file: "../../data/ilsvrc12/imagenet_mean.binaryproto" + batch_size: 50 + crop_size: 227 mirror: false } - top: "data" - top: "label" } layers { - layer { - name: "conv1" - type: "conv" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + convolution_param { num_output: 96 - kernelsize: 11 + kernel_size: 11 stride: 4 } - bottom: "data" - top: "conv1" } layers { - layer { - name: "relu1" - type: "relu" - } + name: "relu1" + type: RELU bottom: "conv1" top: "conv1" } layers { - layer { - name: "pool1" - type: "pool" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv1" - top: "pool1" } layers { - layer { - name: "norm1" - type: "lrn" + name: "norm1" + type: LRN + bottom: "pool1" + top: "norm1" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool1" - top: "norm1" } layers { - layer { - name: "conv2" - type: "conv" + name: "conv2" + type: CONVOLUTION + bottom: "norm1" + top: "conv2" + convolution_param { num_output: 256 - group: 2 - kernelsize: 5 pad: 2 + kernel_size: 5 + group: 2 } - bottom: "norm1" - top: "conv2" } layers { - layer { - name: "relu2" - type: "relu" - } + name: "relu2" + type: RELU bottom: "conv2" top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv2" - top: "pool2" } layers { - layer { - name: "norm2" - type: "lrn" + name: "norm2" + type: LRN + bottom: "pool2" + top: "norm2" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool2" - top: "norm2" } layers { - layer { - name: "conv3" - type: "conv" + name: "conv3" + type: CONVOLUTION + bottom: "norm2" + top: "conv3" + convolution_param { num_output: 384 - kernelsize: 3 pad: 1 + kernel_size: 3 } - bottom: "norm2" - top: "conv3" } layers { - layer { - name: "relu3" - type: "relu" - } + name: "relu3" + type: RELU bottom: "conv3" top: "conv3" } layers { - layer { - name: "conv4" - type: "conv" + name: "conv4" + type: CONVOLUTION + bottom: "conv3" + top: "conv4" + convolution_param { num_output: 384 - group: 2 - kernelsize: 3 pad: 1 + kernel_size: 3 + group: 2 } - bottom: "conv3" - top: "conv4" } layers { - layer { - name: "relu4" - type: "relu" - } + name: "relu4" + type: RELU bottom: "conv4" top: "conv4" } layers { - layer { - name: "conv5" - type: "conv" + name: "conv5" + type: CONVOLUTION + bottom: "conv4" + top: "conv5" + convolution_param { num_output: 256 - group: 2 - kernelsize: 3 pad: 1 + kernel_size: 3 + group: 2 } - bottom: "conv4" - top: "conv5" } layers { - layer { - name: "relu5" - type: "relu" - } + name: "relu5" + type: RELU bottom: "conv5" top: "conv5" } layers { - layer { - name: "pool5" - type: "pool" - kernelsize: 3 + name: "pool5" + type: POOLING + bottom: "conv5" + top: "pool5" + pooling_param { pool: MAX + kernel_size: 3 stride: 2 } - bottom: "conv5" - top: "pool5" } layers { - layer { - name: "fc6" - type: "innerproduct" - num_output: 4096 - } + name: "fc6" + type: INNER_PRODUCT bottom: "pool5" top: "fc6" + inner_product_param { + num_output: 4096 + } } layers { - layer { - name: "relu6" - type: "relu" - } + name: "relu6" + type: RELU bottom: "fc6" top: "fc6" } layers { - layer { - name: "drop6" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop6" + type: DROPOUT bottom: "fc6" top: "fc6" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc7" - type: "innerproduct" - num_output: 4096 - } + name: "fc7" + type: INNER_PRODUCT bottom: "fc6" top: "fc7" + inner_product_param { + num_output: 4096 + } } layers { - layer { - name: "relu7" - type: "relu" - } + name: "relu7" + type: RELU bottom: "fc7" top: "fc7" } layers { - layer { - name: "drop7" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop7" + type: DROPOUT bottom: "fc7" top: "fc7" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc8" - type: "innerproduct" - num_output: 1000 - } + name: "fc8" + type: INNER_PRODUCT bottom: "fc7" top: "fc8" + inner_product_param { + num_output: 1000 + } } layers { - layer { - name: "prob" - type: "softmax" - } + name: "prob" + type: SOFTMAX bottom: "fc8" top: "prob" } layers { - layer { - name: "accuracy" - type: "accuracy" - } + name: "accuracy" + type: ACCURACY bottom: "prob" bottom: "label" top: "accuracy" diff --git a/examples/lenet/lenet.prototxt b/examples/lenet/lenet.prototxt index 4c49745e809..491fad1b1c0 100644 --- a/examples/lenet/lenet.prototxt +++ b/examples/lenet/lenet.prototxt @@ -4,13 +4,16 @@ input_dim: 64 input_dim: 1 input_dim: 28 input_dim: 28 -# N.B. input should be 0/1 = mnist raw data scaled by 0.00390625 layers { - layer { - name: "conv1" - type: "conv" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { num_output: 20 - kernelsize: 5 + kernel_size: 5 stride: 1 weight_filler { type: "xavier" @@ -18,29 +21,29 @@ layers { bias_filler { type: "constant" } - blobs_lr: 1. - blobs_lr: 2. } - bottom: "data" - top: "conv1" } layers { - layer { - name: "pool1" - type: "pool" - kernelsize: 2 - stride: 2 - pool: MAX - } + name: "pool1" + type: POOLING bottom: "conv1" top: "pool1" + pooling_param { + pool: MAX + kernel_size: 2 + stride: 2 + } } layers { - layer { - name: "conv2" - type: "conv" + name: "conv2" + type: CONVOLUTION + bottom: "pool1" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { num_output: 50 - kernelsize: 5 + kernel_size: 5 stride: 1 weight_filler { type: "xavier" @@ -48,27 +51,27 @@ layers { bias_filler { type: "constant" } - blobs_lr: 1. - blobs_lr: 2. } - bottom: "pool1" - top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" - kernelsize: 2 - stride: 2 - pool: MAX - } + name: "pool2" + type: POOLING bottom: "conv2" top: "pool2" + pooling_param { + pool: MAX + kernel_size: 2 + stride: 2 + } } layers { - layer { - name: "ip1" - type: "innerproduct" + name: "ip1" + type: INNER_PRODUCT + bottom: "pool2" + top: "ip1" + blobs_lr: 1 + blobs_lr: 2 + inner_product_param { num_output: 500 weight_filler { type: "xavier" @@ -76,24 +79,22 @@ layers { bias_filler { type: "constant" } - blobs_lr: 1. - blobs_lr: 2. } - bottom: "pool2" - top: "ip1" } layers { - layer { - name: "relu1" - type: "relu" - } + name: "relu1" + type: RELU bottom: "ip1" top: "ip1" } layers { - layer { - name: "ip2" - type: "innerproduct" + name: "ip2" + type: INNER_PRODUCT + bottom: "ip1" + top: "ip2" + blobs_lr: 1 + blobs_lr: 2 + inner_product_param { num_output: 10 weight_filler { type: "xavier" @@ -101,17 +102,11 @@ layers { bias_filler { type: "constant" } - blobs_lr: 1. - blobs_lr: 2. } - bottom: "ip1" - top: "ip2" } layers { - layer { - name: "prob" - type: "softmax" - } + name: "prob" + type: SOFTMAX bottom: "ip2" top: "prob" } diff --git a/examples/lenet/lenet_test.prototxt b/examples/lenet/lenet_test.prototxt index 676a2a6ab7d..3b59b75513d 100644 --- a/examples/lenet/lenet_test.prototxt +++ b/examples/lenet/lenet_test.prototxt @@ -1,21 +1,23 @@ name: "LeNet-test" layers { - layer { - name: "mnist" - type: "data" + name: "mnist" + type: DATA + top: "data" + top: "label" + data_param { source: "mnist-test-leveldb" - batchsize: 100 scale: 0.00390625 + batch_size: 100 } - top: "data" - top: "label" } layers { - layer { - name: "conv1" - type: "conv" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + convolution_param { num_output: 20 - kernelsize: 5 + kernel_size: 5 stride: 1 weight_filler { type: "xavier" @@ -24,26 +26,26 @@ layers { type: "constant" } } - bottom: "data" - top: "conv1" } layers { - layer { - name: "pool1" - type: "pool" - kernelsize: 2 - stride: 2 - pool: MAX - } + name: "pool1" + type: POOLING bottom: "conv1" top: "pool1" + pooling_param { + pool: MAX + kernel_size: 2 + stride: 2 + } } layers { - layer { - name: "conv2" - type: "conv" + name: "conv2" + type: CONVOLUTION + bottom: "pool1" + top: "conv2" + convolution_param { num_output: 50 - kernelsize: 5 + kernel_size: 5 stride: 1 weight_filler { type: "xavier" @@ -52,24 +54,24 @@ layers { type: "constant" } } - bottom: "pool1" - top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" - kernelsize: 2 - stride: 2 - pool: MAX - } + name: "pool2" + type: POOLING bottom: "conv2" top: "pool2" + pooling_param { + pool: MAX + kernel_size: 2 + stride: 2 + } } layers { - layer { - name: "ip1" - type: "innerproduct" + name: "ip1" + type: INNER_PRODUCT + bottom: "pool2" + top: "ip1" + inner_product_param { num_output: 500 weight_filler { type: "xavier" @@ -78,21 +80,19 @@ layers { type: "constant" } } - bottom: "pool2" - top: "ip1" } layers { - layer { - name: "relu1" - type: "relu" - } + name: "relu1" + type: RELU bottom: "ip1" top: "ip1" } layers { - layer { - name: "ip2" - type: "innerproduct" + name: "ip2" + type: INNER_PRODUCT + bottom: "ip1" + top: "ip2" + inner_product_param { num_output: 10 weight_filler { type: "xavier" @@ -101,22 +101,16 @@ layers { type: "constant" } } - bottom: "ip1" - top: "ip2" } layers { - layer { - name: "prob" - type: "softmax" - } + name: "prob" + type: SOFTMAX bottom: "ip2" top: "prob" } layers { - layer { - name: "accuracy" - type: "accuracy" - } + name: "accuracy" + type: ACCURACY bottom: "prob" bottom: "label" top: "accuracy" diff --git a/examples/lenet/lenet_train.prototxt b/examples/lenet/lenet_train.prototxt index f5877ae4804..e8a1e74e40b 100644 --- a/examples/lenet/lenet_train.prototxt +++ b/examples/lenet/lenet_train.prototxt @@ -1,21 +1,25 @@ name: "LeNet" layers { - layer { - name: "mnist" - type: "data" + name: "mnist" + type: DATA + top: "data" + top: "label" + data_param { source: "mnist-train-leveldb" - batchsize: 64 scale: 0.00390625 + batch_size: 64 } - top: "data" - top: "label" } layers { - layer { - name: "conv1" - type: "conv" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { num_output: 20 - kernelsize: 5 + kernel_size: 5 stride: 1 weight_filler { type: "xavier" @@ -23,29 +27,29 @@ layers { bias_filler { type: "constant" } - blobs_lr: 1. - blobs_lr: 2. } - bottom: "data" - top: "conv1" } layers { - layer { - name: "pool1" - type: "pool" - kernelsize: 2 - stride: 2 - pool: MAX - } + name: "pool1" + type: POOLING bottom: "conv1" top: "pool1" + pooling_param { + pool: MAX + kernel_size: 2 + stride: 2 + } } layers { - layer { - name: "conv2" - type: "conv" + name: "conv2" + type: CONVOLUTION + bottom: "pool1" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { num_output: 50 - kernelsize: 5 + kernel_size: 5 stride: 1 weight_filler { type: "xavier" @@ -53,27 +57,27 @@ layers { bias_filler { type: "constant" } - blobs_lr: 1. - blobs_lr: 2. } - bottom: "pool1" - top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" - kernelsize: 2 - stride: 2 - pool: MAX - } + name: "pool2" + type: POOLING bottom: "conv2" top: "pool2" + pooling_param { + pool: MAX + kernel_size: 2 + stride: 2 + } } layers { - layer { - name: "ip1" - type: "innerproduct" + name: "ip1" + type: INNER_PRODUCT + bottom: "pool2" + top: "ip1" + blobs_lr: 1 + blobs_lr: 2 + inner_product_param { num_output: 500 weight_filler { type: "xavier" @@ -81,24 +85,22 @@ layers { bias_filler { type: "constant" } - blobs_lr: 1. - blobs_lr: 2. } - bottom: "pool2" - top: "ip1" } layers { - layer { - name: "relu1" - type: "relu" - } + name: "relu1" + type: RELU bottom: "ip1" top: "ip1" } layers { - layer { - name: "ip2" - type: "innerproduct" + name: "ip2" + type: INNER_PRODUCT + bottom: "ip1" + top: "ip2" + blobs_lr: 1 + blobs_lr: 2 + inner_product_param { num_output: 10 weight_filler { type: "xavier" @@ -106,17 +108,11 @@ layers { bias_filler { type: "constant" } - blobs_lr: 1. - blobs_lr: 2. } - bottom: "ip1" - top: "ip2" } layers { - layer { - name: "loss" - type: "softmax_loss" - } + name: "loss" + type: SOFTMAX_LOSS bottom: "ip2" bottom: "label" } From 35ad409068991f6fa5fd3bfa7f29a2be69b7f9b7 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 11:35:01 -0700 Subject: [PATCH 30/60] fix post-rebase param bugs --- src/caffe/layers/hdf5_data_layer.cpp | 4 ---- src/caffe/layers/hdf5_data_layer.cu | 2 -- src/caffe/test/test_protobuf.cpp | 2 +- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/caffe/layers/hdf5_data_layer.cpp b/src/caffe/layers/hdf5_data_layer.cpp index dc996883dde..035eea84bd2 100644 --- a/src/caffe/layers/hdf5_data_layer.cpp +++ b/src/caffe/layers/hdf5_data_layer.cpp @@ -97,21 +97,17 @@ Dtype HDF5DataLayer::Forward_cpu(const vector*>& bottom, if (current_row_ == data_blob_.num()) { if (num_files_ > 1) { current_file_ += 1; - if (current_file_ == num_files_) { current_file_ = 0; LOG(INFO) << "looping around to first file"; } - load_hdf5_file_data(hdf_filenames_[current_file_].c_str()); } current_row_ = 0; } - memcpy(&(*top)[0]->mutable_cpu_data()[i * data_count], &data_blob_.cpu_data()[current_row_ * data_count], sizeof(Dtype) * data_count); - memcpy(&(*top)[1]->mutable_cpu_data()[i * label_data_count], &label_blob_.cpu_data()[current_row_ * label_data_count], sizeof(Dtype) * label_data_count); diff --git a/src/caffe/layers/hdf5_data_layer.cu b/src/caffe/layers/hdf5_data_layer.cu index 4a0733aac07..e8d09c01ef5 100644 --- a/src/caffe/layers/hdf5_data_layer.cu +++ b/src/caffe/layers/hdf5_data_layer.cu @@ -40,13 +40,11 @@ Dtype HDF5DataLayer::Forward_gpu(const vector*>& bottom, } current_row_ = 0; } - CUDA_CHECK(cudaMemcpy( &(*top)[0]->mutable_gpu_data()[i * data_count], &data_blob_.cpu_data()[current_row_ * data_count], sizeof(Dtype) * data_count, cudaMemcpyHostToDevice)); - CUDA_CHECK(cudaMemcpy( &(*top)[1]->mutable_gpu_data()[i * label_data_count], &label_blob_.cpu_data()[current_row_ * label_data_count], diff --git a/src/caffe/test/test_protobuf.cpp b/src/caffe/test/test_protobuf.cpp index 0618d8c5d80..2db2fed429b 100644 --- a/src/caffe/test/test_protobuf.cpp +++ b/src/caffe/test/test_protobuf.cpp @@ -16,7 +16,7 @@ class ProtoTest : public ::testing::Test {}; TEST_F(ProtoTest, TestSerialization) { LayerParameter param; param.set_name("test"); - param.set_type("dummy"); + param.set_type(LayerParameter_LayerType_SPLIT); std::cout << "Printing in binary format." << std::endl; std::cout << param.SerializeAsString() << std::endl; std::cout << "Printing in text format." << std::endl; From 2e3e065697c34662715ce2fc7ef9b558f0143471 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 12:21:24 -0700 Subject: [PATCH 31/60] fix lint errors --- include/caffe/util/upgrade_proto.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/caffe/util/upgrade_proto.hpp b/include/caffe/util/upgrade_proto.hpp index 2fa45d234dc..18b006849a7 100644 --- a/include/caffe/util/upgrade_proto.hpp +++ b/include/caffe/util/upgrade_proto.hpp @@ -3,6 +3,8 @@ #ifndef CAFFE_UTIL_UPGRADE_PROTO_H_ #define CAFFE_UTIL_UPGRADE_PROTO_H_ +#include + #include "caffe/proto/caffe.pb.h" #include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h" From e0fbd487851b7c96b75f4bd455cb3f62cd2d7337 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 17 Mar 2014 12:38:11 -0700 Subject: [PATCH 32/60] fix layertype alphabetization --- src/caffe/layer_factory.cpp | 4 +- src/caffe/proto/caffe.proto | 74 +++++++++++++++++++++++--------- src/caffe/util/upgrade_proto.cpp | 4 +- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/src/caffe/layer_factory.cpp b/src/caffe/layer_factory.cpp index 22594113779..1fc79fcbef1 100644 --- a/src/caffe/layer_factory.cpp +++ b/src/caffe/layer_factory.cpp @@ -24,10 +24,10 @@ Layer* GetLayer(const LayerParameter& param) { return new AccuracyLayer(param); case LayerParameter_LayerType_BNLL: return new BNLLLayer(param); - case LayerParameter_LayerType_CONVOLUTION: - return new ConvolutionLayer(param); case LayerParameter_LayerType_CONCAT: return new ConcatLayer(param); + case LayerParameter_LayerType_CONVOLUTION: + return new ConvolutionLayer(param); case LayerParameter_LayerType_DATA: return new DataLayer(param); case LayerParameter_LayerType_DROPOUT: diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 094971340b8..b69298b7085 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -91,11 +91,16 @@ message SolverState { message LayerParameter { optional string name = 1; // the layer name + + // Add new LayerTypes to the enum below in lexicographical order, starting + // with the next available ID in the comment line above the enum. + // Update the next available ID when you add a new LayerType. + // LayerType next available ID: 21 enum LayerType { ACCURACY = 0; BNLL = 1; - CONVOLUTION = 2; - CONCAT = 3; + CONCAT = 2; + CONVOLUTION = 3; DATA = 4; DROPOUT = 5; EUCLIDEAN_LOSS = 6; @@ -115,6 +120,7 @@ message LayerParameter { TANH = 20; } optional LayerType type = 2; // the layer type from the enum above + repeated string bottom = 3; // the name of the bottom blobs repeated string top = 4; // the name of the top blobs @@ -127,15 +133,35 @@ message LayerParameter { repeated float weight_decay = 7; // Parameters for particular layer types. - optional DataParameter data_param = 8; - optional HDF5DataParameter hdf5_data_param = 9; - optional InnerProductParameter inner_product_param = 10; - optional ConvolutionParameter convolution_param = 11; - optional PoolingParameter pooling_param = 12; - optional DropoutParameter dropout_param = 13; - optional LRNParameter lrn_param = 14; - optional ConcatParameter concat_param = 15; - optional InfogainLossParameter infogain_loss_param = 16; + optional ConcatParameter concat_param = 8; + optional ConvolutionParameter convolution_param = 9; + optional DataParameter data_param = 10; + optional DropoutParameter dropout_param = 11; + optional HDF5DataParameter hdf5_data_param = 12; + optional InfogainLossParameter infogain_loss_param = 13; + optional InnerProductParameter inner_product_param = 14; + optional LRNParameter lrn_param = 15; + optional PoolingParameter pooling_param = 16; +} + +// Message that stores parameters used by ConcatLayer +message ConcatParameter { + // Concat Layer needs to specify the dimension along the concat will happen, + // the other dimensions must be the same for all the bottom blobs + // By default it will concatenate blobs along channels dimension + optional uint32 concat_dim = 1 [default = 1]; +} + +// Message that stores parameters used by ConvolutionLayer +message ConvolutionParameter { + optional uint32 num_output = 1; // The number of outputs for the layer + optional bool bias_term = 2 [default = true]; // whether to have bias terms + optional uint32 pad = 3 [default = 0]; // The padding size + optional uint32 kernel_size = 4; // The kernel size + optional uint32 group = 5 [default = 1]; // The group size for group conv + optional uint32 stride = 6 [default = 1]; // The stride + optional FillerParameter weight_filler = 7; // The filler for the weight + optional FillerParameter bias_filler = 8; // The filler for the bias } // Message that stores parameters used by DataLayer @@ -160,6 +186,11 @@ message DataParameter { optional uint32 rand_skip = 7 [default = 0]; } +// Message that stores parameters used by DropoutLayer +message DropoutParameter { + optional float dropout_ratio = 1 [default = 0.5]; // dropout ratio +} + // Message that stores parameters used by HDF5DataLayer message HDF5DataParameter { // Specify the data source. @@ -168,6 +199,12 @@ message HDF5DataParameter { optional uint32 batch_size = 2; } +// Message that stores parameters used by InfogainLossLayer +message InfogainLossParameter { + // Specify the infogain matrix source. + optional string source = 1; +} + // Message that stores parameters used by InnerProductLayer message InnerProductParameter { optional uint32 num_output = 1; // The number of outputs for the layer @@ -176,16 +213,11 @@ message InnerProductParameter { optional FillerParameter bias_filler = 4; // The filler for the bias } -// Message that stores parameters used by ConvolutionLayer -message ConvolutionParameter { - optional uint32 num_output = 1; // The number of outputs for the layer - optional bool bias_term = 2 [default = true]; // whether to have bias terms - optional uint32 pad = 3 [default = 0]; // The padding size - optional uint32 kernel_size = 4; // The kernel size - optional uint32 group = 5 [default = 1]; // The group size for group conv - optional uint32 stride = 6 [default = 1]; // The stride - optional FillerParameter weight_filler = 7; // The filler for the weight - optional FillerParameter bias_filler = 8; // The filler for the bias +// Message that stores parameters used by LRNLayer +message LRNParameter { + optional uint32 local_size = 1 [default = 5]; // for local response norm + optional float alpha = 2 [default = 1.]; // for local response norm + optional float beta = 3 [default = 0.75]; // for local response norm } // Message that stores parameters used by PoolingLayer diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 9aefcc1b196..ef24a60ca5e 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -364,10 +364,10 @@ LayerParameter_LayerType UpgradeV0LayerType(const string& type) { return LayerParameter_LayerType_ACCURACY; } else if (type == "bnll") { return LayerParameter_LayerType_BNLL; - } else if (type == "conv") { - return LayerParameter_LayerType_CONVOLUTION; } else if (type == "concat") { return LayerParameter_LayerType_CONCAT; + } else if (type == "conv") { + return LayerParameter_LayerType_CONVOLUTION; } else if (type == "data") { return LayerParameter_LayerType_DATA; } else if (type == "dropout") { From a566570c23fee9f48d8422bd10c6129262df6ddf Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Tue, 18 Mar 2014 13:40:39 -0700 Subject: [PATCH 33/60] rebase and fix stuff, incorporate image and padding layers --- src/caffe/layer_factory.cpp | 10 +- src/caffe/layers/images_layer.cpp | 113 ++++++++++---------- src/caffe/layers/padding_layer.cpp | 2 +- src/caffe/proto/caffe.proto | 145 +++++++++++++------------- src/caffe/test/test_images_layer.cpp | 25 +++-- src/caffe/test/test_padding_layer.cpp | 12 ++- 6 files changed, 162 insertions(+), 145 deletions(-) diff --git a/src/caffe/layer_factory.cpp b/src/caffe/layer_factory.cpp index 1fc79fcbef1..542f716e456 100644 --- a/src/caffe/layer_factory.cpp +++ b/src/caffe/layer_factory.cpp @@ -9,6 +9,7 @@ #include "caffe/vision_layers.hpp" #include "caffe/proto/caffe.pb.h" +using std::string; namespace caffe { @@ -18,6 +19,7 @@ namespace caffe { // but we will leave it this way for now. template Layer* GetLayer(const LayerParameter& param) { + const string& name = param.name(); const LayerParameter_LayerType& type = param.type(); switch (type) { case LayerParameter_LayerType_ACCURACY: @@ -40,6 +42,8 @@ Layer* GetLayer(const LayerParameter& param) { return new HDF5DataLayer(param); case LayerParameter_LayerType_HDF5_OUTPUT: return new HDF5OutputLayer(param); + case LayerParameter_LayerType_IMAGE_DATA: + return new ImagesLayer(param); case LayerParameter_LayerType_IM2COL: return new Im2colLayer(param); case LayerParameter_LayerType_INFOGAIN_LOSS: @@ -50,6 +54,8 @@ Layer* GetLayer(const LayerParameter& param) { return new LRNLayer(param); case LayerParameter_LayerType_MULTINOMIAL_LOGISTIC_LOSS: return new MultinomialLogisticLossLayer(param); + case LayerParameter_LayerType_PADDING: + return new PaddingLayer(param); case LayerParameter_LayerType_POOLING: return new PoolingLayer(param); case LayerParameter_LayerType_RELU: @@ -66,8 +72,10 @@ Layer* GetLayer(const LayerParameter& param) { return new TanHLayer(param); case LayerParameter_LayerType_WINDOW_DATA: return new WindowDataLayer(param); + case LayerParameter_LayerType_NONE: + LOG(FATAL) << "Layer " << name << " has unspecified type."; default: - LOG(FATAL) << "Unknown layer type: " << type; + LOG(FATAL) << "Layer " << name << " has unknown type " << type; } // just to suppress old compiler warnings. return (Layer*)(NULL); diff --git a/src/caffe/layers/images_layer.cpp b/src/caffe/layers/images_layer.cpp index 5154f9a3b61..63f79ca0cd2 100644 --- a/src/caffe/layers/images_layer.cpp +++ b/src/caffe/layers/images_layer.cpp @@ -28,15 +28,16 @@ void* ImagesLayerPrefetch(void* layer_pointer) { CHECK(layer->prefetch_data_); Dtype* top_data = layer->prefetch_data_->mutable_cpu_data(); Dtype* top_label = layer->prefetch_label_->mutable_cpu_data(); - const Dtype scale = layer->layer_param_.scale(); - const int batchsize = layer->layer_param_.batchsize(); - const int cropsize = layer->layer_param_.cropsize(); - const bool mirror = layer->layer_param_.mirror(); - const int new_height = layer->layer_param_.new_height(); - const int new_width = layer->layer_param_.new_height(); - - if (mirror && cropsize == 0) { - LOG(FATAL) << "Current implementation requires mirror and cropsize to be " + ImageDataParameter image_data_param = layer->layer_param_.image_data_param(); + const Dtype scale = image_data_param.scale(); + const int batch_size = image_data_param.batch_size(); + const int crop_size = image_data_param.crop_size(); + const bool mirror = image_data_param.mirror(); + const int new_height = image_data_param.new_height(); + const int new_width = image_data_param.new_width(); + + if (mirror && crop_size == 0) { + LOG(FATAL) << "Current implementation requires mirror and crop_size to be " << "set at the same time."; } // datum scales @@ -46,7 +47,7 @@ void* ImagesLayerPrefetch(void* layer_pointer) { const int size = layer->datum_size_; const int lines_size = layer->lines_.size(); const Dtype* mean = layer->data_mean_.cpu_data(); - for (int itemid = 0; itemid < batchsize; ++itemid) { + for (int item_id = 0; item_id < batch_size; ++item_id) { // get a blob CHECK_GT(lines_size, layer->lines_id_); if (!ReadImageToDatum(layer->lines_[layer->lines_id_].first, @@ -55,27 +56,27 @@ void* ImagesLayerPrefetch(void* layer_pointer) { continue; } const string& data = datum.data(); - if (cropsize) { + if (crop_size) { CHECK(data.size()) << "Image cropping only support uint8 data"; int h_off, w_off; // We only do random crop when we do training. if (Caffe::phase() == Caffe::TRAIN) { // NOLINT_NEXT_LINE(runtime/threadsafe_fn) - h_off = rand() % (height - cropsize); + h_off = rand() % (height - crop_size); // NOLINT_NEXT_LINE(runtime/threadsafe_fn) - w_off = rand() % (width - cropsize); + w_off = rand() % (width - crop_size); } else { - h_off = (height - cropsize) / 2; - w_off = (width - cropsize) / 2; + h_off = (height - crop_size) / 2; + w_off = (width - crop_size) / 2; } // NOLINT_NEXT_LINE(runtime/threadsafe_fn) if (mirror && rand() % 2) { // Copy mirrored version for (int c = 0; c < channels; ++c) { - for (int h = 0; h < cropsize; ++h) { - for (int w = 0; w < cropsize; ++w) { - top_data[((itemid * channels + c) * cropsize + h) * cropsize - + cropsize - 1 - w] = + for (int h = 0; h < crop_size; ++h) { + for (int w = 0; w < crop_size; ++w) { + top_data[((item_id * channels + c) * crop_size + h) * crop_size + + crop_size - 1 - w] = (static_cast( (uint8_t)data[(c * height + h + h_off) * width + w + w_off]) @@ -87,9 +88,10 @@ void* ImagesLayerPrefetch(void* layer_pointer) { } else { // Normal copy for (int c = 0; c < channels; ++c) { - for (int h = 0; h < cropsize; ++h) { - for (int w = 0; w < cropsize; ++w) { - top_data[((itemid * channels + c) * cropsize + h) * cropsize + w] + for (int h = 0; h < crop_size; ++h) { + for (int w = 0; w < crop_size; ++w) { + top_data[((item_id * channels + c) * crop_size + h) + * crop_size + w] = (static_cast( (uint8_t)data[(c * height + h + h_off) * width + w + w_off]) @@ -103,25 +105,25 @@ void* ImagesLayerPrefetch(void* layer_pointer) { // Just copy the whole data if (data.size()) { for (int j = 0; j < size; ++j) { - top_data[itemid * size + j] = + top_data[item_id * size + j] = (static_cast((uint8_t)data[j]) - mean[j]) * scale; } } else { for (int j = 0; j < size; ++j) { - top_data[itemid * size + j] = + top_data[item_id * size + j] = (datum.float_data(j) - mean[j]) * scale; } } } - top_label[itemid] = datum.label(); + top_label[item_id] = datum.label(); // go to the next iter layer->lines_id_++; if (layer->lines_id_ >= lines_size) { // We have reached the end. Restart from the first. DLOG(INFO) << "Restarting data prefetching from start."; layer->lines_id_ = 0; - if (layer->layer_param_.shuffle_images()) { + if (layer->layer_param_.image_data_param().shuffle()) { std::random_shuffle(layer->lines_.begin(), layer->lines_.end()); } } @@ -141,22 +143,22 @@ void ImagesLayer::SetUp(const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom.size(), 0) << "Input Layer takes no input blobs."; CHECK_EQ(top->size(), 2) << "Input Layer takes two blobs as output."; - const int new_height = this->layer_param_.new_height(); - const int new_width = this->layer_param_.new_height(); + const int new_height = this->layer_param_.image_data_param().new_height(); + const int new_width = this->layer_param_.image_data_param().new_height(); CHECK((new_height == 0 && new_width == 0) || - (new_height > 0 && new_width > 0)) << - "Current implementation requires new_height and new_width to be set" - "at the same time."; + (new_height > 0 && new_width > 0)) << "Current implementation requires " + "new_height and new_width to be set at the same time."; // Read the file with filenames and labels - LOG(INFO) << "Opening file " << this->layer_param_.source(); - std::ifstream infile(this->layer_param_.source().c_str()); + const string& source = this->layer_param_.image_data_param().source(); + LOG(INFO) << "Opening file " << source; + std::ifstream infile(source.c_str()); string filename; int label; while (infile >> filename >> label) { lines_.push_back(std::make_pair(filename, label)); } - if (this->layer_param_.shuffle_images()) { + if (this->layer_param_.image_data_param().shuffle()) { // randomly shuffle data LOG(INFO) << "Shuffling data"; std::random_shuffle(lines_.begin(), lines_.end()); @@ -165,9 +167,10 @@ void ImagesLayer::SetUp(const vector*>& bottom, lines_id_ = 0; // Check if we would need to randomly skip a few data points - if (this->layer_param_.rand_skip()) { + if (this->layer_param_.image_data_param().rand_skip()) { // NOLINT_NEXT_LINE(runtime/threadsafe_fn) - unsigned int skip = rand() % this->layer_param_.rand_skip(); + unsigned int skip = rand() % + this->layer_param_.image_data_param().rand_skip(); LOG(INFO) << "Skipping first " << skip << " data points."; CHECK_GT(lines_.size(), skip) << "Not enought points to skip"; lines_id_ = skip; @@ -177,39 +180,37 @@ void ImagesLayer::SetUp(const vector*>& bottom, CHECK(ReadImageToDatum(lines_[lines_id_].first, lines_[lines_id_].second, new_height, new_width, &datum)); // image - int cropsize = this->layer_param_.cropsize(); - if (cropsize > 0) { - (*top)[0]->Reshape( - this->layer_param_.batchsize(), datum.channels(), cropsize, cropsize); - prefetch_data_.reset(new Blob( - this->layer_param_.batchsize(), datum.channels(), cropsize, cropsize)); + const int crop_size = this->layer_param_.image_data_param().crop_size(); + const int batch_size = this->layer_param_.image_data_param().batch_size(); + const string& mean_file = this->layer_param_.image_data_param().mean_file(); + if (crop_size > 0) { + (*top)[0]->Reshape(batch_size, datum.channels(), crop_size, crop_size); + prefetch_data_.reset(new Blob(batch_size, datum.channels(), + crop_size, crop_size)); } else { - (*top)[0]->Reshape( - this->layer_param_.batchsize(), datum.channels(), datum.height(), - datum.width()); - prefetch_data_.reset(new Blob( - this->layer_param_.batchsize(), datum.channels(), datum.height(), - datum.width())); + (*top)[0]->Reshape(batch_size, datum.channels(), datum.height(), + datum.width()); + prefetch_data_.reset(new Blob(batch_size, datum.channels(), + datum.height(), datum.width())); } LOG(INFO) << "output data size: " << (*top)[0]->num() << "," << (*top)[0]->channels() << "," << (*top)[0]->height() << "," << (*top)[0]->width(); // label - (*top)[1]->Reshape(this->layer_param_.batchsize(), 1, 1, 1); - prefetch_label_.reset( - new Blob(this->layer_param_.batchsize(), 1, 1, 1)); + (*top)[1]->Reshape(batch_size, 1, 1, 1); + prefetch_label_.reset(new Blob(batch_size, 1, 1, 1)); // datum size datum_channels_ = datum.channels(); datum_height_ = datum.height(); datum_width_ = datum.width(); datum_size_ = datum.channels() * datum.height() * datum.width(); - CHECK_GT(datum_height_, cropsize); - CHECK_GT(datum_width_, cropsize); + CHECK_GT(datum_height_, crop_size); + CHECK_GT(datum_width_, crop_size); // check if we want to have mean - if (this->layer_param_.has_meanfile()) { + if (this->layer_param_.image_data_param().has_mean_file()) { BlobProto blob_proto; - LOG(INFO) << "Loading mean file from" << this->layer_param_.meanfile(); - ReadProtoFromBinaryFile(this->layer_param_.meanfile().c_str(), &blob_proto); + LOG(INFO) << "Loading mean file from" << mean_file; + ReadProtoFromBinaryFile(mean_file.c_str(), &blob_proto); data_mean_.FromProto(blob_proto); CHECK_EQ(data_mean_.num(), 1); CHECK_EQ(data_mean_.channels(), datum_channels_); diff --git a/src/caffe/layers/padding_layer.cpp b/src/caffe/layers/padding_layer.cpp index 6b22638554b..61fc58c318d 100644 --- a/src/caffe/layers/padding_layer.cpp +++ b/src/caffe/layers/padding_layer.cpp @@ -16,7 +16,7 @@ void PaddingLayer::SetUp(const vector*>& bottom, "convolutions and WILL BE REMOVED. Please update your model " "prototxt to replace padding layers with pad fields. " "See https://github.com/BVLC/caffe/pull/128."; - PAD_ = this->layer_param_.pad(); + PAD_ = this->layer_param_.padding_param().pad(); CHECK_EQ(bottom.size(), 1) << "Padding Layer takes a single blob as input."; CHECK_EQ(top->size(), 1) << "Padding Layer takes a single blob as output."; NUM_ = bottom[0]->num(); diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index b69298b7085..90113c96d02 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -92,32 +92,41 @@ message SolverState { message LayerParameter { optional string name = 1; // the layer name - // Add new LayerTypes to the enum below in lexicographical order, starting - // with the next available ID in the comment line above the enum. - // Update the next available ID when you add a new LayerType. - // LayerType next available ID: 21 + // Add new LayerTypes to the enum below in lexicographical order (other than + // starting with NONE), starting with the next available ID in the comment + // line above the enum. Update the next available ID when you add a new + // LayerType. + // + // LayerType next available ID: 25 enum LayerType { - ACCURACY = 0; - BNLL = 1; - CONCAT = 2; - CONVOLUTION = 3; - DATA = 4; - DROPOUT = 5; - EUCLIDEAN_LOSS = 6; - FLATTEN = 7; - HDF5_DATA = 8; - IM2COL = 9; - INFOGAIN_LOSS = 10; - INNER_PRODUCT = 11; - LRN = 12; - MULTINOMIAL_LOGISTIC_LOSS = 13; - POOLING = 14; - RELU = 15; - SIGMOID = 16; - SOFTMAX = 17; - SOFTMAX_LOSS = 18; - SPLIT = 19; - TANH = 20; + // "NONE" layer type is 0th enum element so that we don't cause confusion + // by defaulting to an existent LayerType (instead, should usually error if + // the type is unspecified). + NONE = 0; + ACCURACY = 1; + BNLL = 2; + CONCAT = 3; + CONVOLUTION = 4; + DATA = 5; + DROPOUT = 6; + EUCLIDEAN_LOSS = 7; + FLATTEN = 8; + HDF5_DATA = 9; + IM2COL = 10; + IMAGE_DATA = 11; + INFOGAIN_LOSS = 12; + INNER_PRODUCT = 13; + LRN = 14; + MULTINOMIAL_LOGISTIC_LOSS = 15; + PADDING = 16; + POOLING = 17; + RELU = 18; + SIGMOID = 19; + SOFTMAX = 20; + SOFTMAX_LOSS = 21; + SPLIT = 22; + TANH = 23; + WINDOW_DATA = 24; } optional LayerType type = 2; // the layer type from the enum above @@ -138,10 +147,12 @@ message LayerParameter { optional DataParameter data_param = 10; optional DropoutParameter dropout_param = 11; optional HDF5DataParameter hdf5_data_param = 12; - optional InfogainLossParameter infogain_loss_param = 13; - optional InnerProductParameter inner_product_param = 14; - optional LRNParameter lrn_param = 15; - optional PoolingParameter pooling_param = 16; + optional ImageDataParameter image_data_param = 13; + optional InfogainLossParameter infogain_loss_param = 14; + optional InnerProductParameter inner_product_param = 15; + optional LRNParameter lrn_param = 16; + optional PaddingParameter padding_param = 17; + optional PoolingParameter pooling_param = 18; } // Message that stores parameters used by ConcatLayer @@ -199,7 +210,34 @@ message HDF5DataParameter { optional uint32 batch_size = 2; } -// Message that stores parameters used by InfogainLossLayer +// Message that stores parameters used by ImageDataLayer +message ImageDataParameter { + // Specify the data source. + optional string source = 1; + // For data pre-processing, we can do simple scaling and subtracting the + // data mean, if provided. Note that the mean subtraction is always carried + // out before scaling. + optional float scale = 2 [default = 1]; + optional string mean_file = 3; + // Specify the batch size. + optional uint32 batch_size = 4; + // Specify if we would like to randomly crop an image. + optional uint32 crop_size = 5 [default = 0]; + // Specify if we want to randomly mirror data. + optional bool mirror = 6 [default = false]; + // The rand_skip variable is for the data layer to skip a few data points + // to avoid all asynchronous sgd clients to start at the same point. The skip + // point would be set as rand_skip * rand(0,1). Note that rand_skip should not + // be larger than the number of keys in the leveldb. + optional uint32 rand_skip = 7 [default = 0]; + // Whether or not ImageLayer should shuffle the list of files at every epoch. + optional bool shuffle = 8 [default = false]; + // It will also resize images if new_height or new_width are not zero. + optional uint32 new_height = 9 [default = 0]; + optional uint32 new_width = 10 [default = 0]; +} + +// Message that stores parameters InfogainLossLayer message InfogainLossParameter { // Specify the infogain matrix source. optional string source = 1; @@ -220,6 +258,11 @@ message LRNParameter { optional float beta = 3 [default = 0.75]; // for local response norm } +// Message that stores parameters used by PaddingLayer +message PaddingParameter { + optional uint32 pad = 1 [default = 0]; // The padding size +} + // Message that stores parameters used by PoolingLayer message PoolingParameter { enum PoolMethod { @@ -232,18 +275,6 @@ message PoolingParameter { optional uint32 stride = 3 [default = 1]; // The stride } -// Message that stores parameters used by DropoutLayer -message DropoutParameter { - optional float dropout_ratio = 1 [default = 0.5]; // dropout ratio -} - -// Message that stores parameters used by LRNLayer -message LRNParameter { - optional uint32 local_size = 1 [default = 5]; // for local response norm - optional float alpha = 2 [default = 1.]; // for local response norm - optional float beta = 3 [default = 0.75]; // for local response norm -} - message WindowDataParameter { // Fields related to detection (det_*) // foreground (object) overlap threshold @@ -268,33 +299,3 @@ message WindowDataParameter { message HDF5OutputParameter { optional string file_name = 1; } - -// Message that stores parameters used by ConcatLayer -message ConcatParameter { - // For ConcatLayer, one needs to specify the dimension for concatenation, and - // the other dimensions must be the same for all the bottom blobs. - // By default it will concatenate blobs along the channels dimension. - optional uint32 concat_dim = 1 [default = 1]; -} - -// Message that stores parameters used by ReshapeLayer -message ReshapeParameter { - // For ReshapeLayer, one needs to specify the new dimensions. - optional int32 new_num = 1 [default = 0]; - optional int32 new_channels = 2 [default = 0]; - optional int32 new_height = 3 [default = 0]; - optional int32 new_width = 4 [default = 0]; -} - -// Message that stores parameters used by ImageDataLayer -message ImageDataParameter { - // Whether or not ImageLayer should shuffle the list of files at every epoch. - // It will also resize images if new_height or new_width are not zero. - optional bool shuffle_images = 64 [default = false]; -} - -// Message that stores parameters InfogainLossLayer -message InfogainLossParameter { - // Specify the infogain matrix source. - optional string source = 1; -} diff --git a/src/caffe/test/test_images_layer.cpp b/src/caffe/test/test_images_layer.cpp index e8ed7c18fc9..0cd1001a089 100644 --- a/src/caffe/test/test_images_layer.cpp +++ b/src/caffe/test/test_images_layer.cpp @@ -55,9 +55,10 @@ TYPED_TEST_CASE(ImagesLayerTest, Dtypes); TYPED_TEST(ImagesLayerTest, TestRead) { LayerParameter param; - param.set_batchsize(5); - param.set_source(this->filename); - param.set_shuffle_images(false); + ImageDataParameter* image_data_param = param.mutable_image_data_param(); + image_data_param->set_batch_size(5); + image_data_param->set_source(this->filename); + image_data_param->set_shuffle(false); ImagesLayer layer(param); layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); EXPECT_EQ(this->blob_top_data_->num(), 5); @@ -79,11 +80,12 @@ TYPED_TEST(ImagesLayerTest, TestRead) { TYPED_TEST(ImagesLayerTest, TestResize) { LayerParameter param; - param.set_batchsize(5); - param.set_source(this->filename); - param.set_new_height(256); - param.set_new_width(256); - param.set_shuffle_images(false); + ImageDataParameter* image_data_param = param.mutable_image_data_param(); + image_data_param->set_batch_size(5); + image_data_param->set_source(this->filename); + image_data_param->set_new_height(256); + image_data_param->set_new_width(256); + image_data_param->set_shuffle(false); ImagesLayer layer(param); layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); EXPECT_EQ(this->blob_top_data_->num(), 5); @@ -105,9 +107,10 @@ TYPED_TEST(ImagesLayerTest, TestResize) { TYPED_TEST(ImagesLayerTest, TestShuffle) { LayerParameter param; - param.set_batchsize(5); - param.set_source(this->filename); - param.set_shuffle_images(true); + ImageDataParameter* image_data_param = param.mutable_image_data_param(); + image_data_param->set_batch_size(5); + image_data_param->set_source(this->filename); + image_data_param->set_shuffle(true); ImagesLayer layer(param); layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); EXPECT_EQ(this->blob_top_data_->num(), 5); diff --git a/src/caffe/test/test_padding_layer.cpp b/src/caffe/test/test_padding_layer.cpp index c775f3be351..59b012ce034 100644 --- a/src/caffe/test/test_padding_layer.cpp +++ b/src/caffe/test/test_padding_layer.cpp @@ -42,7 +42,8 @@ TYPED_TEST_CASE(PaddingLayerTest, Dtypes); TYPED_TEST(PaddingLayerTest, TestCPU) { LayerParameter layer_param; - layer_param.set_pad(1); + PaddingParameter* padding_param = layer_param.mutable_padding_param(); + padding_param->set_pad(1); Caffe::set_mode(Caffe::CPU); PaddingLayer layer(layer_param); layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -65,7 +66,8 @@ TYPED_TEST(PaddingLayerTest, TestCPU) { TYPED_TEST(PaddingLayerTest, TestCPUGrad) { LayerParameter layer_param; - layer_param.set_pad(1); + PaddingParameter* padding_param = layer_param.mutable_padding_param(); + padding_param->set_pad(1); Caffe::set_mode(Caffe::CPU); PaddingLayer layer(layer_param); GradientChecker checker(1e-2, 1e-3); @@ -76,7 +78,8 @@ TYPED_TEST(PaddingLayerTest, TestCPUGrad) { TYPED_TEST(PaddingLayerTest, TestGPU) { if (CAFFE_TEST_CUDA_PROP.major >= 2) { LayerParameter layer_param; - layer_param.set_pad(1); + PaddingParameter* padding_param = layer_param.mutable_padding_param(); + padding_param->set_pad(1); Caffe::set_mode(Caffe::GPU); PaddingLayer layer(layer_param); layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); @@ -103,7 +106,8 @@ TYPED_TEST(PaddingLayerTest, TestGPU) { TYPED_TEST(PaddingLayerTest, TestGPUGrad) { if (CAFFE_TEST_CUDA_PROP.major >= 2) { LayerParameter layer_param; - layer_param.set_pad(1); + PaddingParameter* padding_param = layer_param.mutable_padding_param(); + padding_param->set_pad(1); Caffe::set_mode(Caffe::GPU); PaddingLayer layer(layer_param); GradientChecker checker(1e-2, 1e-3); From 9291f21730213aafad1462937410405968cca955 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 13:45:38 -0700 Subject: [PATCH 34/60] incorporate WindowDataLayer --- src/caffe/layers/window_data_layer.cpp | 108 +++++++++++++------------ src/caffe/proto/caffe.proto | 34 +++++--- 2 files changed, 80 insertions(+), 62 deletions(-) diff --git a/src/caffe/layers/window_data_layer.cpp b/src/caffe/layers/window_data_layer.cpp index bf62bcb49c2..9346c6f0574 100644 --- a/src/caffe/layers/window_data_layer.cpp +++ b/src/caffe/layers/window_data_layer.cpp @@ -26,7 +26,7 @@ using std::pair; // caffe.proto > LayerParameter // 'source' field specifies the window_file -// 'cropsize' indicates the desired warped size +// 'crop_size' indicates the desired warped size namespace caffe { @@ -40,27 +40,28 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { Dtype* top_data = layer->prefetch_data_->mutable_cpu_data(); Dtype* top_label = layer->prefetch_label_->mutable_cpu_data(); - const Dtype scale = layer->layer_param_.scale(); - const int batchsize = layer->layer_param_.batchsize(); - const int cropsize = layer->layer_param_.cropsize(); - const int context_pad = layer->layer_param_.det_context_pad(); - const bool mirror = layer->layer_param_.mirror(); - const float fg_fraction = layer->layer_param_.det_fg_fraction(); + const Dtype scale = layer->layer_param_.window_data_param().scale(); + const int batch_size = layer->layer_param_.window_data_param().batch_size(); + const int crop_size = layer->layer_param_.window_data_param().crop_size(); + const int context_pad = layer->layer_param_.window_data_param().context_pad(); + const bool mirror = layer->layer_param_.window_data_param().mirror(); + const float fg_fraction = + layer->layer_param_.window_data_param().fg_fraction(); const Dtype* mean = layer->data_mean_.cpu_data(); - const int mean_off = (layer->data_mean_.width() - cropsize) / 2; + const int mean_off = (layer->data_mean_.width() - crop_size) / 2; const int mean_width = layer->data_mean_.width(); const int mean_height = layer->data_mean_.height(); - cv::Size cv_crop_size(cropsize, cropsize); - const string& crop_mode = layer->layer_param_.det_crop_mode(); + cv::Size cv_crop_size(crop_size, crop_size); + const string& crop_mode = layer->layer_param_.window_data_param().crop_mode(); bool use_square = (crop_mode == "square") ? true : false; // zero out batch memset(top_data, 0, sizeof(Dtype)*layer->prefetch_data_->count()); - const int num_fg = static_cast(static_cast(batchsize) + const int num_fg = static_cast(static_cast(batch_size) * fg_fraction); - const int num_samples[2] = { batchsize - num_fg, num_fg }; + const int num_samples[2] = { batch_size - num_fg, num_fg }; int itemid = 0; // sample from bg set then fg set @@ -100,10 +101,10 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { int pad_h = 0; if (context_pad > 0 || use_square) { // scale factor by which to expand the original region - // such that after warping the expanded region to cropsize x cropsize + // such that after warping the expanded region to crop_size x crop_size // there's exactly context_pad amount of padding on each side - Dtype context_scale = static_cast(cropsize) / - static_cast(cropsize - 2*context_pad); + Dtype context_scale = static_cast(crop_size) / + static_cast(crop_size - 2*context_pad); // compute the expanded region Dtype half_height = static_cast(y2-y1+1)/2.0; @@ -147,9 +148,9 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { // scale factors that would be used to warp the unclipped // expanded region Dtype scale_x = - static_cast(cropsize)/static_cast(unclipped_width); + static_cast(crop_size)/static_cast(unclipped_width); Dtype scale_y = - static_cast(cropsize)/static_cast(unclipped_height); + static_cast(crop_size)/static_cast(unclipped_height); // size to warp the clipped expanded region to cv_crop_size.width = @@ -169,13 +170,13 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { pad_w = pad_x1; } - // ensure that the warped, clipped region plus the padding - // fits in the cropsize x cropsize image (it might not due to rounding) - if (pad_h + cv_crop_size.height > cropsize) { - cv_crop_size.height = cropsize - pad_h; + // ensure that the warped, clipped region plus the padding fits in the + // crop_size x crop_size image (it might not due to rounding) + if (pad_h + cv_crop_size.height > crop_size) { + cv_crop_size.height = crop_size - pad_h; } - if (pad_w + cv_crop_size.width > cropsize) { - cv_crop_size.width = cropsize - pad_w; + if (pad_w + cv_crop_size.width > crop_size) { + cv_crop_size.width = crop_size - pad_w; } } @@ -196,8 +197,8 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { Dtype pixel = static_cast(cv_cropped_img.at(h, w)[c]); - top_data[((itemid * channels + c) * cropsize + h + pad_h) - * cropsize + w + pad_w] + top_data[((itemid * channels + c) * crop_size + h + pad_h) + * crop_size + w + pad_w] = (pixel - mean[(c * mean_height + h + mean_off + pad_h) * mean_width + w + mean_off + pad_w]) @@ -231,11 +232,11 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { string("_data.txt")).c_str(), std::ofstream::out | std::ofstream::binary); for (int c = 0; c < channels; ++c) { - for (int h = 0; h < cropsize; ++h) { - for (int w = 0; w < cropsize; ++w) { + for (int h = 0; h < crop_size; ++h) { + for (int w = 0; w < crop_size; ++w) { top_data_file.write(reinterpret_cast( - &top_data[((itemid * channels + c) * cropsize + h) - * cropsize + w]), + &top_data[((itemid * channels + c) * crop_size + h) + * crop_size + w]), sizeof(Dtype)); } } @@ -278,15 +279,15 @@ void WindowDataLayer::SetUp(const vector*>& bottom, LOG(INFO) << "Window data layer:" << std::endl << " foreground (object) overlap threshold: " - << this->layer_param_.det_fg_threshold() << std::endl + << this->layer_param_.window_data_param().fg_threshold() << std::endl << " background (non-object) overlap threshold: " - << this->layer_param_.det_bg_threshold() << std::endl + << this->layer_param_.window_data_param().bg_threshold() << std::endl << " foreground sampling fraction: " - << this->layer_param_.det_fg_fraction(); + << this->layer_param_.window_data_param().fg_fraction(); - std::ifstream infile(this->layer_param_.source().c_str()); + std::ifstream infile(this->layer_param_.window_data_param().source().c_str()); CHECK(infile.good()) << "Failed to open window file " - << this->layer_param_.source() << std::endl; + << this->layer_param_.window_data_param().source() << std::endl; map label_hist; label_hist.insert(std::make_pair(0, 0)); @@ -307,6 +308,10 @@ void WindowDataLayer::SetUp(const vector*>& bottom, // read each box int num_windows; infile >> num_windows; + const float fg_threshold = + this->layer_param_.window_data_param().fg_threshold(); + const float bg_threshold = + this->layer_param_.window_data_param().bg_threshold(); for (int i = 0; i < num_windows; ++i) { int label, x1, y1, x2, y2; float overlap; @@ -322,13 +327,13 @@ void WindowDataLayer::SetUp(const vector*>& bottom, window[WindowDataLayer::Y2] = y2; // add window to foreground list or background list - if (overlap >= this->layer_param_.det_fg_threshold()) { + if (overlap >= fg_threshold) { int label = window[WindowDataLayer::LABEL]; CHECK_GT(label, 0); fg_windows_.push_back(window); label_hist.insert(std::make_pair(label, 0)); label_hist[label]++; - } else if (overlap < this->layer_param_.det_bg_threshold()) { + } else if (overlap < bg_threshold) { // background window, force label and overlap to 0 window[WindowDataLayer::LABEL] = 0; window[WindowDataLayer::OVERLAP] = 0; @@ -356,38 +361,41 @@ void WindowDataLayer::SetUp(const vector*>& bottom, } LOG(INFO) << "Amount of context padding: " - << this->layer_param_.det_context_pad(); + << this->layer_param_.window_data_param().context_pad(); - LOG(INFO) << "Crop mode: " << this->layer_param_.det_crop_mode(); + LOG(INFO) << "Crop mode: " + << this->layer_param_.window_data_param().crop_mode(); // image - int cropsize = this->layer_param_.cropsize(); - CHECK_GT(cropsize, 0); - (*top)[0]->Reshape( - this->layer_param_.batchsize(), channels, cropsize, cropsize); - prefetch_data_.reset(new Blob( - this->layer_param_.batchsize(), channels, cropsize, cropsize)); + int crop_size = this->layer_param_.window_data_param().crop_size(); + CHECK_GT(crop_size, 0); + const int batch_size = this->layer_param_.window_data_param().batch_size(); + (*top)[0]->Reshape(batch_size, channels, crop_size, crop_size); + prefetch_data_.reset( + new Blob(batch_size, channels, crop_size, crop_size)); LOG(INFO) << "output data size: " << (*top)[0]->num() << "," << (*top)[0]->channels() << "," << (*top)[0]->height() << "," << (*top)[0]->width(); // label - (*top)[1]->Reshape(this->layer_param_.batchsize(), 1, 1, 1); + (*top)[1]->Reshape(batch_size, 1, 1, 1); prefetch_label_.reset( - new Blob(this->layer_param_.batchsize(), 1, 1, 1)); + new Blob(batch_size, 1, 1, 1)); // check if we want to have mean - if (this->layer_param_.has_meanfile()) { + if (this->layer_param_.window_data_param().has_mean_file()) { + const string& mean_file = + this->layer_param_.window_data_param().mean_file(); + LOG(INFO) << "Loading mean file from" << mean_file; BlobProto blob_proto; - LOG(INFO) << "Loading mean file from" << this->layer_param_.meanfile(); - ReadProtoFromBinaryFile(this->layer_param_.meanfile().c_str(), &blob_proto); + ReadProtoFromBinaryFile(mean_file, &blob_proto); data_mean_.FromProto(blob_proto); CHECK_EQ(data_mean_.num(), 1); CHECK_EQ(data_mean_.width(), data_mean_.height()); CHECK_EQ(data_mean_.channels(), channels); } else { // Simply initialize an all-empty mean. - data_mean_.Reshape(1, channels, cropsize, cropsize); + data_mean_.Reshape(1, channels, crop_size, crop_size); } // Now, start the prefetch thread. Before calling prefetch, we make two // cpu_data calls so that the prefetch thread does not accidentally make diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 90113c96d02..51ea340c6b7 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -153,6 +153,7 @@ message LayerParameter { optional LRNParameter lrn_param = 16; optional PaddingParameter padding_param = 17; optional PoolingParameter pooling_param = 18; + optional WindowDataParameter window_data_param = 19; } // Message that stores parameters used by ConcatLayer @@ -275,25 +276,34 @@ message PoolingParameter { optional uint32 stride = 3 [default = 1]; // The stride } +// Message that stores parameters used by WindowDataLayer message WindowDataParameter { - // Fields related to detection (det_*) - // foreground (object) overlap threshold - optional float det_fg_threshold = 54 [default = 0.5]; - // background (non-object) overlap threshold - optional float det_bg_threshold = 55 [default = 0.5]; + // Specify the data source. + optional string source = 1; + // For data pre-processing, we can do simple scaling and subtracting the + // data mean, if provided. Note that the mean subtraction is always carried + // out before scaling. + optional float scale = 2 [default = 1]; + optional string mean_file = 3; + // Specify the batch size. + optional uint32 batch_size = 4; + // Specify if we would like to randomly crop an image. + optional uint32 crop_size = 5 [default = 0]; + // Specify if we want to randomly mirror data. + optional bool mirror = 6 [default = false]; + // Foreground (object) overlap threshold + optional float fg_threshold = 7 [default = 0.5]; + // Background (non-object) overlap threshold + optional float bg_threshold = 8 [default = 0.5]; // Fraction of batch that should be foreground objects - optional float det_fg_fraction = 56 [default = 0.25]; - - // optional bool OBSOLETE_can_clobber = 57 [ default = true ]; - + optional float fg_fraction = 9 [default = 0.25]; // Amount of contextual padding to add around a window // (used only by the window_data_layer) - optional uint32 det_context_pad = 58 [default = 0]; - + optional uint32 context_pad = 10 [default = 0]; // Mode for cropping out a detection window // warp: cropped window is warped to a fixed size and aspect ratio // square: the tightest square around the window is cropped - optional string det_crop_mode = 59 [default = "warp"]; + optional string crop_mode = 11 [default = "warp"]; } message HDF5OutputParameter { From a0cba163bba67f709197dfcb98f92a55f4225d71 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 14:04:11 -0700 Subject: [PATCH 35/60] fix test_net for refactor --- src/caffe/test/test_net.cpp | 58 +++++++++++++++---------------------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/src/caffe/test/test_net.cpp b/src/caffe/test/test_net.cpp index 77b3516a980..4c7f0e7f7ac 100644 --- a/src/caffe/test/test_net.cpp +++ b/src/caffe/test/test_net.cpp @@ -18,17 +18,14 @@ namespace caffe { template class NetTest : public ::testing::Test { protected: - NetTest() : filename(NULL) { - } - virtual void SetUp() { // Create the leveldb - filename = tmpnam(NULL); // get temp name - LOG(INFO) << "Using temporary leveldb " << filename; + filename_ = tmpnam(NULL); // get temp name + LOG(INFO) << "Using temporary leveldb " << filename_; leveldb::DB* db; leveldb::Options options; options.error_if_exists = true; options.create_if_missing = true; - leveldb::Status status = leveldb::DB::Open(options, filename, &db); + leveldb::Status status = leveldb::DB::Open(options, filename_, &db); CHECK(status.ok()); for (int i = 0; i < 5; ++i) { Datum datum; @@ -49,19 +46,19 @@ class NetTest : public ::testing::Test { const string& proto_prefix = "name: 'TestNetwork' " "layers: { " - " layer { " - " name: 'data' " - " type: 'data' "; + " name: 'data' " + " type: DATA " + " data_param { "; const string& proto_suffix = - " batchsize: 1 " + " batch_size: 1 " " } " " top: 'data' " " top: 'label' " "} " "layers: { " - " layer { " - " name: 'innerproduct' " - " type: 'innerproduct' " + " name: 'innerproduct' " + " type: INNER_PRODUCT " + " inner_product_param { " " num_output: 1000 " " weight_filler { " " type: 'gaussian' " @@ -71,31 +68,26 @@ class NetTest : public ::testing::Test { " type: 'constant' " " value: 0 " " } " - " blobs_lr: 1. " - " blobs_lr: 2. " - " weight_decay: 1. " - " weight_decay: 0. " " } " + " blobs_lr: 1. " + " blobs_lr: 2. " + " weight_decay: 1. " + " weight_decay: 0. " " bottom: 'data' " " top: 'innerproduct' " "} " "layers: { " - " layer { " - " name: 'loss' " - " type: 'softmax_loss' " - " } " + " name: 'loss' " + " type: SOFTMAX_LOSS " " bottom: 'innerproduct' " " bottom: 'label' " "} "; - proto = proto_prefix + "source: '" + string(this->filename) + + proto_ = proto_prefix + "source: '" + string(this->filename_) + "' " + proto_suffix; } - virtual ~NetTest() { - } - - char* filename; - string proto; + char* filename_; + string proto_; }; typedef ::testing::Types Dtypes; @@ -103,8 +95,7 @@ TYPED_TEST_CASE(NetTest, Dtypes); TYPED_TEST(NetTest, TestHasBlob) { NetParameter param; - CHECK(google::protobuf::TextFormat::ParseFromString(this->proto, - ¶m)); + CHECK(google::protobuf::TextFormat::ParseFromString(this->proto_, ¶m)); Net net(param); EXPECT_TRUE(net.has_blob("data")); EXPECT_TRUE(net.has_blob("label")); @@ -114,8 +105,7 @@ TYPED_TEST(NetTest, TestHasBlob) { TYPED_TEST(NetTest, TestGetBlob) { NetParameter param; - CHECK(google::protobuf::TextFormat::ParseFromString(this->proto, - ¶m)); + CHECK(google::protobuf::TextFormat::ParseFromString(this->proto_, ¶m)); Net net(param); EXPECT_EQ(net.blob_by_name("data"), net.blobs()[0]); EXPECT_EQ(net.blob_by_name("label"), net.blobs()[1]); @@ -125,8 +115,7 @@ TYPED_TEST(NetTest, TestGetBlob) { TYPED_TEST(NetTest, TestHasLayer) { NetParameter param; - CHECK(google::protobuf::TextFormat::ParseFromString(this->proto, - ¶m)); + CHECK(google::protobuf::TextFormat::ParseFromString(this->proto_, ¶m)); Net net(param); EXPECT_TRUE(net.has_layer("data")); EXPECT_TRUE(net.has_layer("innerproduct")); @@ -136,8 +125,7 @@ TYPED_TEST(NetTest, TestHasLayer) { TYPED_TEST(NetTest, TestGetLayerByName) { NetParameter param; - CHECK(google::protobuf::TextFormat::ParseFromString(this->proto, - ¶m)); + CHECK(google::protobuf::TextFormat::ParseFromString(this->proto_, ¶m)); Net net(param); EXPECT_EQ(net.layer_by_name("data"), net.layers()[0]); EXPECT_EQ(net.layer_by_name("innerproduct"), net.layers()[1]); From dc1b25e34f058c25086996025877d9a61798ea99 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 14:33:42 -0700 Subject: [PATCH 36/60] remove padding layer --- include/caffe/vision_layers.hpp | 27 ------ src/caffe/layer_factory.cpp | 2 - src/caffe/layers/padding_layer.cpp | 74 ---------------- src/caffe/layers/padding_layer.cu | 82 ----------------- src/caffe/proto/caffe.proto | 27 +++--- src/caffe/test/test_padding_layer.cpp | 121 -------------------------- 6 files changed, 10 insertions(+), 323 deletions(-) delete mode 100644 src/caffe/layers/padding_layer.cpp delete mode 100644 src/caffe/layers/padding_layer.cu delete mode 100644 src/caffe/test/test_padding_layer.cpp diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp index 5807938408a..110b272de14 100644 --- a/include/caffe/vision_layers.hpp +++ b/include/caffe/vision_layers.hpp @@ -202,33 +202,6 @@ class InnerProductLayer : public Layer { }; -template -class PaddingLayer : public Layer { - public: - explicit PaddingLayer(const LayerParameter& param) - : Layer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - unsigned int PAD_; - int NUM_; - int CHANNEL_; - int HEIGHT_IN_; - int WIDTH_IN_; - int HEIGHT_OUT_; - int WIDTH_OUT_; -}; - - template class LRNLayer : public Layer { public: diff --git a/src/caffe/layer_factory.cpp b/src/caffe/layer_factory.cpp index 542f716e456..f86e12ef94d 100644 --- a/src/caffe/layer_factory.cpp +++ b/src/caffe/layer_factory.cpp @@ -54,8 +54,6 @@ Layer* GetLayer(const LayerParameter& param) { return new LRNLayer(param); case LayerParameter_LayerType_MULTINOMIAL_LOGISTIC_LOSS: return new MultinomialLogisticLossLayer(param); - case LayerParameter_LayerType_PADDING: - return new PaddingLayer(param); case LayerParameter_LayerType_POOLING: return new PoolingLayer(param); case LayerParameter_LayerType_RELU: diff --git a/src/caffe/layers/padding_layer.cpp b/src/caffe/layers/padding_layer.cpp deleted file mode 100644 index 61fc58c318d..00000000000 --- a/src/caffe/layers/padding_layer.cpp +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2014 BVLC and contributors. - -#include // NOLINT(readability/streams) -#include - -#include "caffe/layer.hpp" -#include "caffe/vision_layers.hpp" - -namespace caffe { - -template -void PaddingLayer::SetUp(const vector*>& bottom, - vector*>* top) { - // DEPRECATION - LOG(WARNING) << "Padding layers are deprecated in favor of padding-aware " - "convolutions and WILL BE REMOVED. Please update your model " - "prototxt to replace padding layers with pad fields. " - "See https://github.com/BVLC/caffe/pull/128."; - PAD_ = this->layer_param_.padding_param().pad(); - CHECK_EQ(bottom.size(), 1) << "Padding Layer takes a single blob as input."; - CHECK_EQ(top->size(), 1) << "Padding Layer takes a single blob as output."; - NUM_ = bottom[0]->num(); - CHANNEL_ = bottom[0]->channels(); - HEIGHT_IN_ = bottom[0]->height(); - WIDTH_IN_ = bottom[0]->width(); - HEIGHT_OUT_ = HEIGHT_IN_ + PAD_ * 2; - WIDTH_OUT_ = WIDTH_IN_ + PAD_ * 2; - (*top)[0]->Reshape(NUM_, CHANNEL_, HEIGHT_OUT_, WIDTH_OUT_); -} - -template -Dtype PaddingLayer::Forward_cpu(const vector*>& bottom, - vector*>* top) { - Dtype* top_data = (*top)[0]->mutable_cpu_data(); - const Dtype* bottom_data = bottom[0]->cpu_data(); - memset(top_data, 0, sizeof(Dtype) * (*top)[0]->count()); - // In short, top[n, c, h, w] = bottom[n, c, h-pad, w-pad] if in range - for (int n = 0; n < NUM_; ++n) { - for (int c = 0; c < CHANNEL_; ++c) { - for (int h = 0; h < HEIGHT_IN_; ++h) { - // copy the width part - memcpy( - top_data + ((n * CHANNEL_ + c) * HEIGHT_OUT_ + h + PAD_) - * WIDTH_OUT_ + PAD_, - bottom_data + ((n * CHANNEL_ + c) * HEIGHT_IN_ + h) * WIDTH_IN_, - sizeof(Dtype) * WIDTH_IN_); - } - } - } - return Dtype(0.); -} - -template -void PaddingLayer::Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { - const Dtype* top_diff = top[0]->cpu_diff(); - Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff(); - for (int n = 0; n < NUM_; ++n) { - for (int c = 0; c < CHANNEL_; ++c) { - for (int h = 0; h < HEIGHT_IN_; ++h) { - // copy the width part - memcpy( - bottom_diff + ((n * CHANNEL_ + c) * HEIGHT_IN_ + h) * WIDTH_IN_, - top_diff + ((n * CHANNEL_ + c) * HEIGHT_OUT_ + h + PAD_) - * WIDTH_OUT_ + PAD_, - sizeof(Dtype) * WIDTH_IN_); - } - } - } -} - -INSTANTIATE_CLASS(PaddingLayer); - -} // namespace caffe diff --git a/src/caffe/layers/padding_layer.cu b/src/caffe/layers/padding_layer.cu deleted file mode 100644 index 8023fef0347..00000000000 --- a/src/caffe/layers/padding_layer.cu +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright 2014 BVLC and contributors. - -#include // NOLINT(readability/streams) -#include - -#include "caffe/layer.hpp" -#include "caffe/vision_layers.hpp" - -namespace caffe { - -template -__global__ void PaddingForward(const int count, const Dtype* in, Dtype* out, - const int num, const int channel, const int height_in, const int width_in, - const int pad) { - CUDA_KERNEL_LOOP(index, count) { - int height_out = height_in + pad + pad; - int width_out = width_in + pad + pad; - int w = index % width_in; - index /= width_in; - int h = index % height_in; - index /= height_in; - int c = index % channel; - index /= channel; - out[((index * channel + c) * height_out + h + pad) * width_out + pad + w] = - in[((index * channel + c) * height_in + h) * width_in + w]; - } -} - -template -Dtype PaddingLayer::Forward_gpu(const vector*>& bottom, - vector*>* top) { - const Dtype* bottom_data = bottom[0]->gpu_data(); - Dtype* top_data = (*top)[0]->mutable_gpu_data(); - const int count = bottom[0]->count(); - // First, set all data to be zero for the boundary pixels - CUDA_CHECK(cudaMemset(top_data, 0, sizeof(Dtype) * (*top)[0]->count())); - // NOLINT_NEXT_LINE(whitespace/operators) - PaddingForward<<>>( - count, bottom_data, top_data, NUM_, CHANNEL_, HEIGHT_IN_, WIDTH_IN_, - PAD_); - CUDA_POST_KERNEL_CHECK; - return Dtype(0); -} - -template -__global__ void PaddingBackward(const int count, const Dtype* in, Dtype* out, - const int num, const int channel, const int height_in, const int width_in, - const int pad) { - CUDA_KERNEL_LOOP(index, count) { - int height_out = height_in + pad + pad; - int width_out = width_in + pad + pad; - int w = index % width_in; - index /= width_in; - int h = index % height_in; - index /= height_in; - int c = index % channel; - index /= channel; - out[((index * channel + c) * height_in + h) * width_in + w] = - in[((index * channel + c) * height_out + h + pad) * - width_out + pad + w]; - } -} - -template -void PaddingLayer::Backward_gpu(const vector*>& top, - const bool propagate_down, - vector*>* bottom) { - if (propagate_down) { - const Dtype* top_diff = top[0]->gpu_diff(); - Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff(); - const int count = (*bottom)[0]->count(); - // NOLINT_NEXT_LINE(whitespace/operators) - PaddingBackward<<>>( - count, top_diff, bottom_diff, NUM_, CHANNEL_, HEIGHT_IN_, WIDTH_IN_, - PAD_); - CUDA_POST_KERNEL_CHECK; - } -} - -INSTANTIATE_CLASS(PaddingLayer); - -} // namespace caffe diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 51ea340c6b7..34d59f9f3cd 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -118,15 +118,14 @@ message LayerParameter { INNER_PRODUCT = 13; LRN = 14; MULTINOMIAL_LOGISTIC_LOSS = 15; - PADDING = 16; - POOLING = 17; - RELU = 18; - SIGMOID = 19; - SOFTMAX = 20; - SOFTMAX_LOSS = 21; - SPLIT = 22; - TANH = 23; - WINDOW_DATA = 24; + POOLING = 16; + RELU = 17; + SIGMOID = 18; + SOFTMAX = 19; + SOFTMAX_LOSS = 20; + SPLIT = 21; + TANH = 22; + WINDOW_DATA = 23; } optional LayerType type = 2; // the layer type from the enum above @@ -151,9 +150,8 @@ message LayerParameter { optional InfogainLossParameter infogain_loss_param = 14; optional InnerProductParameter inner_product_param = 15; optional LRNParameter lrn_param = 16; - optional PaddingParameter padding_param = 17; - optional PoolingParameter pooling_param = 18; - optional WindowDataParameter window_data_param = 19; + optional PoolingParameter pooling_param = 17; + optional WindowDataParameter window_data_param = 18; } // Message that stores parameters used by ConcatLayer @@ -259,11 +257,6 @@ message LRNParameter { optional float beta = 3 [default = 0.75]; // for local response norm } -// Message that stores parameters used by PaddingLayer -message PaddingParameter { - optional uint32 pad = 1 [default = 0]; // The padding size -} - // Message that stores parameters used by PoolingLayer message PoolingParameter { enum PoolMethod { diff --git a/src/caffe/test/test_padding_layer.cpp b/src/caffe/test/test_padding_layer.cpp deleted file mode 100644 index 59b012ce034..00000000000 --- a/src/caffe/test/test_padding_layer.cpp +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright 2014 BVLC and contributors. - -#include -#include -#include - -#include "gtest/gtest.h" -#include "caffe/blob.hpp" -#include "caffe/common.hpp" -#include "caffe/filler.hpp" -#include "caffe/vision_layers.hpp" -#include "caffe/test/test_gradient_check_util.hpp" - -#include "caffe/test/test_caffe_main.hpp" - -namespace caffe { - -extern cudaDeviceProp CAFFE_TEST_CUDA_PROP; - -template -class PaddingLayerTest : public ::testing::Test { - protected: - PaddingLayerTest() - : blob_bottom_(new Blob(2, 3, 4, 5)), - blob_top_(new Blob()) { - // fill the values - FillerParameter filler_param; - GaussianFiller filler(filler_param); - filler.Fill(this->blob_bottom_); - blob_bottom_vec_.push_back(blob_bottom_); - blob_top_vec_.push_back(blob_top_); - } - virtual ~PaddingLayerTest() { delete blob_bottom_; delete blob_top_; } - Blob* const blob_bottom_; - Blob* const blob_top_; - vector*> blob_bottom_vec_; - vector*> blob_top_vec_; -}; - -typedef ::testing::Types Dtypes; -TYPED_TEST_CASE(PaddingLayerTest, Dtypes); - -TYPED_TEST(PaddingLayerTest, TestCPU) { - LayerParameter layer_param; - PaddingParameter* padding_param = layer_param.mutable_padding_param(); - padding_param->set_pad(1); - Caffe::set_mode(Caffe::CPU); - PaddingLayer layer(layer_param); - layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); - layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_)); - EXPECT_EQ(this->blob_top_->num(), 2); - EXPECT_EQ(this->blob_top_->channels(), 3); - EXPECT_EQ(this->blob_top_->height(), 6); - EXPECT_EQ(this->blob_top_->width(), 7); - for (int n = 0; n < 2; ++n) { - for (int c = 0; c < 3; ++c) { - for (int h = 0; h < 4; ++h) { - for (int w = 0; w < 5; ++w) { - EXPECT_EQ(this->blob_bottom_->data_at(n, c, h, w), - this->blob_top_->data_at(n, c, h + 1, w + 1)); - } - } - } - } -} - -TYPED_TEST(PaddingLayerTest, TestCPUGrad) { - LayerParameter layer_param; - PaddingParameter* padding_param = layer_param.mutable_padding_param(); - padding_param->set_pad(1); - Caffe::set_mode(Caffe::CPU); - PaddingLayer layer(layer_param); - GradientChecker checker(1e-2, 1e-3); - checker.CheckGradientExhaustive(&layer, &(this->blob_bottom_vec_), - &(this->blob_top_vec_)); -} - -TYPED_TEST(PaddingLayerTest, TestGPU) { - if (CAFFE_TEST_CUDA_PROP.major >= 2) { - LayerParameter layer_param; - PaddingParameter* padding_param = layer_param.mutable_padding_param(); - padding_param->set_pad(1); - Caffe::set_mode(Caffe::GPU); - PaddingLayer layer(layer_param); - layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); - layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_)); - EXPECT_EQ(this->blob_top_->num(), 2); - EXPECT_EQ(this->blob_top_->channels(), 3); - EXPECT_EQ(this->blob_top_->height(), 6); - EXPECT_EQ(this->blob_top_->width(), 7); - for (int n = 0; n < 2; ++n) { - for (int c = 0; c < 3; ++c) { - for (int h = 0; h < 4; ++h) { - for (int w = 0; w < 5; ++w) { - EXPECT_EQ(this->blob_bottom_->data_at(n, c, h, w), - this->blob_top_->data_at(n, c, h + 1, w + 1)); - } - } - } - } - } else { - LOG(ERROR) << "Skipping test (gpu version too low)."; - } -} - -TYPED_TEST(PaddingLayerTest, TestGPUGrad) { - if (CAFFE_TEST_CUDA_PROP.major >= 2) { - LayerParameter layer_param; - PaddingParameter* padding_param = layer_param.mutable_padding_param(); - padding_param->set_pad(1); - Caffe::set_mode(Caffe::GPU); - PaddingLayer layer(layer_param); - GradientChecker checker(1e-2, 1e-3); - checker.CheckGradientExhaustive(&layer, &(this->blob_bottom_vec_), - &(this->blob_top_vec_)); - } else { - LOG(ERROR) << "Skipping test (gpu version too low)."; - } -} - -} // namespace caffe From fb25211a41babff5d4a94594199dd440744e1dff Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 14:48:31 -0700 Subject: [PATCH 37/60] some cleanup - lowercase layer class member variable names --- include/caffe/vision_layers.hpp | 70 +++++++------- src/caffe/layers/concat_layer.cpp | 28 +++--- src/caffe/layers/concat_layer.cu | 16 ++-- src/caffe/layers/conv_layer.cpp | 71 +++++++------- src/caffe/layers/conv_layer.cu | 28 +++--- src/caffe/layers/im2col_layer.cpp | 26 ++--- src/caffe/layers/im2col_layer.cu | 8 +- src/caffe/layers/pooling_layer.cpp | 106 ++++++++++---------- src/caffe/layers/pooling_layer.cu | 128 ++++++++++++------------- src/caffe/layers/window_data_layer.cpp | 12 +-- 10 files changed, 248 insertions(+), 245 deletions(-) diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp index 110b272de14..d80809b95a8 100644 --- a/include/caffe/vision_layers.hpp +++ b/include/caffe/vision_layers.hpp @@ -64,7 +64,6 @@ class TanHLayer : public NeuronLayer { vector*>* top); virtual Dtype Forward_gpu(const vector*>& bottom, vector*>* top); - virtual void Backward_cpu(const vector*>& top, const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, @@ -82,7 +81,6 @@ class SigmoidLayer : public NeuronLayer { vector*>* top); virtual Dtype Forward_gpu(const vector*>& bottom, vector*>* top); - virtual void Backward_cpu(const vector*>& top, const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, @@ -101,7 +99,6 @@ class BNLLLayer : public NeuronLayer { vector*>* top); virtual Dtype Forward_gpu(const vector*>& bottom, vector*>* top); - virtual void Backward_cpu(const vector*>& top, const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, @@ -122,11 +119,11 @@ class DropoutLayer : public NeuronLayer { vector*>* top); virtual Dtype Forward_gpu(const vector*>& bottom, vector*>* top); - virtual void Backward_cpu(const vector*>& top, const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); + shared_ptr rand_vec_; float threshold_; float scale_; @@ -151,6 +148,7 @@ class SplitLayer : public Layer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); + int count_; }; @@ -172,6 +170,7 @@ class FlattenLayer : public Layer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); + int count_; }; @@ -194,6 +193,7 @@ class InnerProductLayer : public Layer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); + int M_; int K_; int N_; @@ -219,6 +219,7 @@ class LRNLayer : public Layer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); + // scale_ stores the intermediate summing results Blob scale_; int size_; @@ -249,12 +250,13 @@ class Im2colLayer : public Layer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - int KSIZE_; - int STRIDE_; - int CHANNELS_; - int HEIGHT_; - int WIDTH_; - int PAD_; + + int kernel_size_; + int stride_; + int channels_; + int height_; + int width_; + int pad_; }; template @@ -274,13 +276,14 @@ class PoolingLayer : public Layer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - int KSIZE_; - int STRIDE_; - int CHANNELS_; - int HEIGHT_; - int WIDTH_; - int POOLED_HEIGHT_; - int POOLED_WIDTH_; + + int kernel_size_; + int stride_; + int channels_; + int height_; + int width_; + int pooled_height_; + int pooled_width_; Blob rand_idx_; }; @@ -302,17 +305,16 @@ class ConvolutionLayer : public Layer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - Blob col_bob_; - int KSIZE_; - int STRIDE_; - int NUM_; - int CHANNELS_; - int PAD_; - int HEIGHT_; - int WIDTH_; - int NUM_OUTPUT_; - int GROUP_; + int kernel_size_; + int stride_; + int num_; + int channels_; + int pad_; + int height_; + int width_; + int num_output_; + int group_; Blob col_buffer_; shared_ptr bias_multiplier_; bool bias_term_; @@ -338,13 +340,13 @@ class ConcatLayer : public Layer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - Blob col_bob_; - int COUNT_; - int NUM_; - int CHANNELS_; - int HEIGHT_; - int WIDTH_; + Blob col_bob_; + int count_; + int num_; + int channels_; + int height_; + int width_; int concat_dim_; }; @@ -449,7 +451,6 @@ class HDF5DataLayer : public Layer { unsigned int num_files_; unsigned int current_file_; hsize_t current_row_; - Blob data_blob_; Blob label_blob_; }; @@ -603,6 +604,7 @@ class EuclideanLossLayer : public Layer { const bool propagate_down, vector*>* bottom); // virtual void Backward_gpu(const vector*>& top, // const bool propagate_down, vector*>* bottom); + Blob difference_; }; diff --git a/src/caffe/layers/concat_layer.cpp b/src/caffe/layers/concat_layer.cpp index 8aded1bf764..4bbce133c51 100644 --- a/src/caffe/layers/concat_layer.cpp +++ b/src/caffe/layers/concat_layer.cpp @@ -20,25 +20,25 @@ void ConcatLayer::SetUp(const vector*>& bottom, CHECK_LE(concat_dim_, 1) << "For now concat_dim <=1, it can only concat num and channels"; // Intialize with the first blob - COUNT_ = bottom[0]->count(); - NUM_ = bottom[0]->num(); - CHANNELS_ = bottom[0]->channels(); - HEIGHT_ = bottom[0]->height(); - WIDTH_ = bottom[0]->width(); + count_ = bottom[0]->count(); + num_ = bottom[0]->num(); + channels_ = bottom[0]->channels(); + height_ = bottom[0]->height(); + width_ = bottom[0]->width(); for (int i = 1; i < bottom.size(); ++i) { - COUNT_ += bottom[i]->count(); + count_ += bottom[i]->count(); if (concat_dim_== 0) { - NUM_ += bottom[i]->num(); + num_ += bottom[i]->num(); } else if (concat_dim_ == 1) { - CHANNELS_ += bottom[i]->channels(); + channels_ += bottom[i]->channels(); } else if (concat_dim_ == 2) { - HEIGHT_ += bottom[i]->height(); + height_ += bottom[i]->height(); } else if (concat_dim_ == 3) { - WIDTH_ += bottom[i]->width(); + width_ += bottom[i]->width(); } } - (*top)[0]->Reshape(NUM_, CHANNELS_, HEIGHT_, WIDTH_); - CHECK_EQ(COUNT_, (*top)[0]->count()); + (*top)[0]->Reshape(num_, channels_, height_, width_); + CHECK_EQ(count_, (*top)[0]->count()); } template @@ -59,7 +59,7 @@ Dtype ConcatLayer::Forward_cpu(const vector*>& bottom, const Dtype* bottom_data = bottom[i]->cpu_data(); int num_elem = bottom[i]->channels()*bottom[i]->height()*bottom[i]->width(); - for (int n = 0; n < NUM_; ++n) { + for (int n = 0; n < num_; ++n) { caffe_copy(num_elem, bottom_data+bottom[i]->offset(n), top_data+(*top)[0]->offset(n, offset_channel)); } @@ -91,7 +91,7 @@ void ConcatLayer::Backward_cpu(const vector*>& top, Blob* blob = (*bottom)[i]; Dtype* bottom_diff = blob->mutable_cpu_diff(); int num_elem = blob->channels()*blob->height()*blob->width(); - for (int n = 0; n < NUM_; ++n) { + for (int n = 0; n < num_; ++n) { caffe_copy(num_elem, top_diff+top[0]->offset(n, offset_channel), bottom_diff+blob->offset(n)); } diff --git a/src/caffe/layers/concat_layer.cu b/src/caffe/layers/concat_layer.cu index 9270f1cb270..2820bf0dfdf 100644 --- a/src/caffe/layers/concat_layer.cu +++ b/src/caffe/layers/concat_layer.cu @@ -17,7 +17,7 @@ Dtype ConcatLayer::Forward_gpu(const vector*>& bottom, for (int i = 0; i < bottom.size(); ++i) { const Dtype* bottom_data = bottom[i]->gpu_data(); caffe_gpu_copy(bottom[i]->count(), bottom_data, - top_data+(*top)[0]->offset(offset_num)); + top_data + (*top)[0]->offset(offset_num)); offset_num += bottom[i]->num(); } } else if (concat_dim_ == 1) { @@ -25,10 +25,10 @@ Dtype ConcatLayer::Forward_gpu(const vector*>& bottom, for (int i = 0; i < bottom.size(); ++i) { const Dtype* bottom_data = bottom[i]->gpu_data(); int num_elem = - bottom[i]->channels()*bottom[i]->height()*bottom[i]->width(); - for (int n = 0; n < NUM_; ++n) { + bottom[i]->channels() * bottom[i]->height() * bottom[i]->width(); + for (int n = 0; n < num_; ++n) { caffe_gpu_copy(num_elem, bottom_data+bottom[i]->offset(n), - top_data+(*top)[0]->offset(n, offset_channel)); + top_data + (*top)[0]->offset(n, offset_channel)); } offset_channel += bottom[i]->channels(); } @@ -49,7 +49,7 @@ void ConcatLayer::Backward_gpu(const vector*>& top, Blob* blob = (*bottom)[i]; Dtype* bottom_diff = blob->mutable_gpu_diff(); caffe_gpu_copy(blob->count(), - top_diff+top[0]->offset(offset_num), bottom_diff); + top_diff + top[0]->offset(offset_num), bottom_diff); offset_num += blob->num(); } } else if (concat_dim_ == 1) { @@ -58,9 +58,9 @@ void ConcatLayer::Backward_gpu(const vector*>& top, Blob* blob = (*bottom)[i]; Dtype* bottom_diff = blob->mutable_gpu_diff(); int num_elem = blob->channels()*blob->height()*blob->width(); - for (int n = 0; n < NUM_; ++n) { - caffe_gpu_copy(num_elem, top_diff+top[0]->offset(n, offset_channel), - bottom_diff+blob->offset(n)); + for (int n = 0; n < num_; ++n) { + caffe_gpu_copy(num_elem, top_diff + top[0]->offset(n, offset_channel), + bottom_diff + blob->offset(n)); } offset_channel += blob->channels(); } diff --git a/src/caffe/layers/conv_layer.cpp b/src/caffe/layers/conv_layer.cpp index b4948ca68c9..55966b54bde 100644 --- a/src/caffe/layers/conv_layer.cpp +++ b/src/caffe/layers/conv_layer.cpp @@ -15,31 +15,32 @@ void ConvolutionLayer::SetUp(const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom.size(), 1) << "Conv Layer takes a single blob as input."; CHECK_EQ(top->size(), 1) << "Conv Layer takes a single blob as output."; - KSIZE_ = this->layer_param_.convolution_param().kernel_size(); - STRIDE_ = this->layer_param_.convolution_param().stride(); - GROUP_ = this->layer_param_.convolution_param().group(); - PAD_ = this->layer_param_.convolution_param().pad(); - NUM_ = bottom[0]->num(); - CHANNELS_ = bottom[0]->channels(); - HEIGHT_ = bottom[0]->height(); - WIDTH_ = bottom[0]->width(); - NUM_OUTPUT_ = this->layer_param_.convolution_param().num_output(); - CHECK_GT(NUM_OUTPUT_, 0); - CHECK_EQ(CHANNELS_ % GROUP_, 0); + kernel_size_ = this->layer_param_.convolution_param().kernel_size(); + stride_ = this->layer_param_.convolution_param().stride(); + group_ = this->layer_param_.convolution_param().group(); + pad_ = this->layer_param_.convolution_param().pad(); + num_ = bottom[0]->num(); + channels_ = bottom[0]->channels(); + height_ = bottom[0]->height(); + width_ = bottom[0]->width(); + num_output_ = this->layer_param_.convolution_param().num_output(); + CHECK_GT(num_output_, 0); + CHECK_EQ(channels_ % group_, 0); // The im2col result buffer would only hold one image at a time to avoid // overly large memory usage. - int height_out = (HEIGHT_ + 2 * PAD_ - KSIZE_) / STRIDE_ + 1; - int width_out = (WIDTH_ + 2 * PAD_ - KSIZE_) / STRIDE_ + 1; - col_buffer_.Reshape(1, CHANNELS_ * KSIZE_ * KSIZE_, height_out, width_out); + int height_out = (height_ + 2 * pad_ - kernel_size_) / stride_ + 1; + int width_out = (width_ + 2 * pad_ - kernel_size_) / stride_ + 1; + col_buffer_.Reshape( + 1, channels_ * kernel_size_ * kernel_size_, height_out, width_out); // Set the parameters - CHECK_EQ(NUM_OUTPUT_ % GROUP_, 0) + CHECK_EQ(num_output_ % group_, 0) << "Number of output should be multiples of group."; bias_term_ = this->layer_param_.convolution_param().bias_term(); // Figure out the dimensions for individual gemms. - M_ = NUM_OUTPUT_ / GROUP_; - K_ = CHANNELS_ * KSIZE_ * KSIZE_ / GROUP_; + M_ = num_output_ / group_; + K_ = channels_ * kernel_size_ * kernel_size_ / group_; N_ = height_out * width_out; - (*top)[0]->Reshape(bottom[0]->num(), NUM_OUTPUT_, height_out, width_out); + (*top)[0]->Reshape(bottom[0]->num(), num_output_, height_out, width_out); // Check if we need to set up the weights if (this->blobs_.size() > 0) { LOG(INFO) << "Skipping parameter initialization"; @@ -50,15 +51,15 @@ void ConvolutionLayer::SetUp(const vector*>& bottom, this->blobs_.resize(1); } // Intialize the weight - this->blobs_[0].reset( - new Blob(NUM_OUTPUT_, CHANNELS_ / GROUP_, KSIZE_, KSIZE_)); + this->blobs_[0].reset(new Blob( + num_output_, channels_ / group_, kernel_size_, kernel_size_)); // fill the weights shared_ptr > weight_filler(GetFiller( this->layer_param_.convolution_param().weight_filler())); weight_filler->Fill(this->blobs_[0].get()); // If necessary, intiialize and fill the bias term if (bias_term_) { - this->blobs_[1].reset(new Blob(1, 1, 1, NUM_OUTPUT_)); + this->blobs_[1].reset(new Blob(1, 1, 1, num_output_)); shared_ptr > bias_filler(GetFiller( this->layer_param_.convolution_param().bias_filler())); bias_filler->Fill(this->blobs_[1].get()); @@ -86,19 +87,19 @@ Dtype ConvolutionLayer::Forward_cpu(const vector*>& bottom, int weight_offset = M_ * K_; int col_offset = K_ * N_; int top_offset = M_ * N_; - for (int n = 0; n < NUM_; ++n) { + for (int n = 0; n < num_; ++n) { // First, im2col - im2col_cpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); + im2col_cpu(bottom_data + bottom[0]->offset(n), channels_, height_, + width_, kernel_size_, pad_, stride_, col_data); // Second, innerproduct with groups - for (int g = 0; g < GROUP_; ++g) { + for (int g = 0; g < group_; ++g) { caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, M_, N_, K_, (Dtype)1., weight + weight_offset * g, col_data + col_offset * g, (Dtype)0., top_data + (*top)[0]->offset(n) + top_offset * g); } // third, add bias if (bias_term_) { - caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, NUM_OUTPUT_, + caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, num_output_, N_, 1, (Dtype)1., this->blobs_[1]->cpu_data(), reinterpret_cast(bias_multiplier_->cpu_data()), (Dtype)1., top_data + (*top)[0]->offset(n)); @@ -123,8 +124,8 @@ void ConvolutionLayer::Backward_cpu(const vector*>& top, if (bias_term_) { bias_diff = this->blobs_[1]->mutable_cpu_diff(); memset(bias_diff, 0, sizeof(Dtype) * this->blobs_[1]->count()); - for (int n = 0; n < NUM_; ++n) { - caffe_cpu_gemv(CblasNoTrans, NUM_OUTPUT_, N_, + for (int n = 0; n < num_; ++n) { + caffe_cpu_gemv(CblasNoTrans, num_output_, N_, 1., top_diff + top[0]->offset(n), reinterpret_cast(bias_multiplier_->cpu_data()), 1., bias_diff); @@ -135,13 +136,13 @@ void ConvolutionLayer::Backward_cpu(const vector*>& top, int col_offset = K_ * N_; int top_offset = M_ * N_; memset(weight_diff, 0, sizeof(Dtype) * this->blobs_[0]->count()); - for (int n = 0; n < NUM_; ++n) { + for (int n = 0; n < num_; ++n) { // since we saved memory in the forward pass by not storing all col data, // we will need to recompute them. - im2col_cpu(bottom_data + (*bottom)[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); + im2col_cpu(bottom_data + (*bottom)[0]->offset(n), channels_, height_, + width_, kernel_size_, pad_, stride_, col_data); // gradient w.r.t. weight. Note that we will accumulate diffs. - for (int g = 0; g < GROUP_; ++g) { + for (int g = 0; g < group_; ++g) { caffe_cpu_gemm(CblasNoTrans, CblasTrans, M_, K_, N_, (Dtype)1., top_diff + top[0]->offset(n) + top_offset * g, col_data + col_offset * g, (Dtype)1., @@ -149,15 +150,15 @@ void ConvolutionLayer::Backward_cpu(const vector*>& top, } // gradient w.r.t. bottom data, if necessary if (propagate_down) { - for (int g = 0; g < GROUP_; ++g) { + for (int g = 0; g < group_; ++g) { caffe_cpu_gemm(CblasTrans, CblasNoTrans, K_, N_, M_, (Dtype)1., weight + weight_offset * g, top_diff + top[0]->offset(n) + top_offset * g, (Dtype)0., col_diff + col_offset * g); } // col2im back to the data - col2im_cpu(col_diff, CHANNELS_, HEIGHT_, WIDTH_, KSIZE_, PAD_, STRIDE_, - bottom_diff + (*bottom)[0]->offset(n)); + col2im_cpu(col_diff, channels_, height_, width_, kernel_size_, pad_, + stride_, bottom_diff + (*bottom)[0]->offset(n)); } } } diff --git a/src/caffe/layers/conv_layer.cu b/src/caffe/layers/conv_layer.cu index 28b3d94185f..51f5d159879 100644 --- a/src/caffe/layers/conv_layer.cu +++ b/src/caffe/layers/conv_layer.cu @@ -20,19 +20,19 @@ Dtype ConvolutionLayer::Forward_gpu(const vector*>& bottom, int weight_offset = M_ * K_; int col_offset = K_ * N_; int top_offset = M_ * N_; - for (int n = 0; n < NUM_; ++n) { + for (int n = 0; n < num_; ++n) { // First, im2col - im2col_gpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); + im2col_gpu(bottom_data + bottom[0]->offset(n), channels_, height_, + width_, kernel_size_, pad_, stride_, col_data); // Second, innerproduct with groups - for (int g = 0; g < GROUP_; ++g) { + for (int g = 0; g < group_; ++g) { caffe_gpu_gemm(CblasNoTrans, CblasNoTrans, M_, N_, K_, (Dtype)1., weight + weight_offset * g, col_data + col_offset * g, (Dtype)0., top_data + (*top)[0]->offset(n) + top_offset * g); } // third, add bias if (bias_term_) { - caffe_gpu_gemm(CblasNoTrans, CblasNoTrans, NUM_OUTPUT_, + caffe_gpu_gemm(CblasNoTrans, CblasNoTrans, num_output_, N_, 1, (Dtype)1., this->blobs_[1]->gpu_data(), reinterpret_cast(bias_multiplier_->gpu_data()), (Dtype)1., top_data + (*top)[0]->offset(n)); @@ -58,8 +58,8 @@ void ConvolutionLayer::Backward_gpu(const vector*>& top, bias_diff = this->blobs_[1]->mutable_gpu_diff(); CUDA_CHECK(cudaMemset(bias_diff, 0, sizeof(Dtype) * this->blobs_[1]->count())); - for (int n = 0; n < NUM_; ++n) { - caffe_gpu_gemv(CblasNoTrans, NUM_OUTPUT_, N_, + for (int n = 0; n < num_; ++n) { + caffe_gpu_gemv(CblasNoTrans, num_output_, N_, 1., top_diff + top[0]->offset(n), reinterpret_cast(bias_multiplier_->gpu_data()), 1., bias_diff); @@ -71,13 +71,13 @@ void ConvolutionLayer::Backward_gpu(const vector*>& top, int top_offset = M_ * N_; CUDA_CHECK(cudaMemset(weight_diff, 0, sizeof(Dtype) * this->blobs_[0]->count())); - for (int n = 0; n < NUM_; ++n) { + for (int n = 0; n < num_; ++n) { // since we saved memory in the forward pass by not storing all col data, // we will need to recompute them. - im2col_gpu(bottom_data + (*bottom)[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); + im2col_gpu(bottom_data + (*bottom)[0]->offset(n), channels_, height_, + width_, kernel_size_, pad_, stride_, col_data); // gradient w.r.t. weight. Note that we will accumulate diffs. - for (int g = 0; g < GROUP_; ++g) { + for (int g = 0; g < group_; ++g) { caffe_gpu_gemm(CblasNoTrans, CblasTrans, M_, K_, N_, (Dtype)1., top_diff + top[0]->offset(n) + top_offset * g, col_data + col_offset * g, (Dtype)1., @@ -85,15 +85,15 @@ void ConvolutionLayer::Backward_gpu(const vector*>& top, } // gradient w.r.t. bottom data, if necessary if (propagate_down) { - for (int g = 0; g < GROUP_; ++g) { + for (int g = 0; g < group_; ++g) { caffe_gpu_gemm(CblasTrans, CblasNoTrans, K_, N_, M_, (Dtype)1., weight + weight_offset * g, top_diff + top[0]->offset(n) + top_offset * g, (Dtype)0., col_diff + col_offset * g); } // col2im back to the data - col2im_gpu(col_diff, CHANNELS_, HEIGHT_, WIDTH_, KSIZE_, PAD_, STRIDE_, - bottom_diff + (*bottom)[0]->offset(n)); + col2im_gpu(col_diff, channels_, height_, width_, kernel_size_, pad_, + stride_, bottom_diff + (*bottom)[0]->offset(n)); } } } diff --git a/src/caffe/layers/im2col_layer.cpp b/src/caffe/layers/im2col_layer.cpp index 91c22488f04..749ea3c2d6a 100644 --- a/src/caffe/layers/im2col_layer.cpp +++ b/src/caffe/layers/im2col_layer.cpp @@ -14,15 +14,15 @@ void Im2colLayer::SetUp(const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom.size(), 1) << "Im2col Layer takes a single blob as input."; CHECK_EQ(top->size(), 1) << "Im2col Layer takes a single blob as output."; - KSIZE_ = this->layer_param_.convolution_param().kernel_size(); - STRIDE_ = this->layer_param_.convolution_param().stride(); - PAD_ = this->layer_param_.convolution_param().pad(); - CHANNELS_ = bottom[0]->channels(); - HEIGHT_ = bottom[0]->height(); - WIDTH_ = bottom[0]->width(); - (*top)[0]->Reshape(bottom[0]->num(), CHANNELS_ * KSIZE_ * KSIZE_, - (HEIGHT_ + 2 * PAD_ - KSIZE_) / STRIDE_ + 1, - (WIDTH_ + 2 * PAD_ - KSIZE_) / STRIDE_ + 1); + kernel_size_ = this->layer_param_.convolution_param().kernel_size(); + stride_ = this->layer_param_.convolution_param().stride(); + pad_ = this->layer_param_.convolution_param().pad(); + channels_ = bottom[0]->channels(); + height_ = bottom[0]->height(); + width_ = bottom[0]->width(); + (*top)[0]->Reshape(bottom[0]->num(), channels_ * kernel_size_ * kernel_size_, + (height_ + 2 * pad_ - kernel_size_) / stride_ + 1, + (width_ + 2 * pad_ - kernel_size_) / stride_ + 1); } template @@ -31,8 +31,8 @@ Dtype Im2colLayer::Forward_cpu(const vector*>& bottom, const Dtype* bottom_data = bottom[0]->cpu_data(); Dtype* top_data = (*top)[0]->mutable_cpu_data(); for (int n = 0; n < bottom[0]->num(); ++n) { - im2col_cpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, top_data + (*top)[0]->offset(n)); + im2col_cpu(bottom_data + bottom[0]->offset(n), channels_, height_, + width_, kernel_size_, pad_, stride_, top_data + (*top)[0]->offset(n)); } return Dtype(0.); } @@ -43,8 +43,8 @@ void Im2colLayer::Backward_cpu(const vector*>& top, const Dtype* top_diff = top[0]->cpu_diff(); Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff(); for (int n = 0; n < top[0]->num(); ++n) { - col2im_cpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); + col2im_cpu(top_diff + top[0]->offset(n), channels_, height_, width_, + kernel_size_, pad_, stride_, bottom_diff + (*bottom)[0]->offset(n)); } } diff --git a/src/caffe/layers/im2col_layer.cu b/src/caffe/layers/im2col_layer.cu index 4074d9e424d..26bc1b97959 100644 --- a/src/caffe/layers/im2col_layer.cu +++ b/src/caffe/layers/im2col_layer.cu @@ -15,8 +15,8 @@ Dtype Im2colLayer::Forward_gpu(const vector*>& bottom, const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* top_data = (*top)[0]->mutable_gpu_data(); for (int n = 0; n < bottom[0]->num(); ++n) { - im2col_gpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, top_data + (*top)[0]->offset(n)); + im2col_gpu(bottom_data + bottom[0]->offset(n), channels_, height_, + width_, kernel_size_, pad_, stride_, top_data + (*top)[0]->offset(n)); } return Dtype(0.); } @@ -27,8 +27,8 @@ void Im2colLayer::Backward_gpu(const vector*>& top, const Dtype* top_diff = top[0]->gpu_diff(); Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff(); for (int n = 0; n < top[0]->num(); ++n) { - col2im_gpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); + col2im_gpu(top_diff + top[0]->offset(n), channels_, height_, width_, + kernel_size_, pad_, stride_, bottom_diff + (*bottom)[0]->offset(n)); } } diff --git a/src/caffe/layers/pooling_layer.cpp b/src/caffe/layers/pooling_layer.cpp index 995579522d1..a186741232f 100644 --- a/src/caffe/layers/pooling_layer.cpp +++ b/src/caffe/layers/pooling_layer.cpp @@ -18,22 +18,22 @@ void PoolingLayer::SetUp(const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom.size(), 1) << "PoolingLayer takes a single blob as input."; CHECK_EQ(top->size(), 1) << "PoolingLayer takes a single blob as output."; - KSIZE_ = this->layer_param_.pooling_param().kernel_size(); - STRIDE_ = this->layer_param_.pooling_param().stride(); - CHANNELS_ = bottom[0]->channels(); - HEIGHT_ = bottom[0]->height(); - WIDTH_ = bottom[0]->width(); - POOLED_HEIGHT_ = static_cast( - ceil(static_cast(HEIGHT_ - KSIZE_) / STRIDE_)) + 1; - POOLED_WIDTH_ = static_cast( - ceil(static_cast(WIDTH_ - KSIZE_) / STRIDE_)) + 1; - (*top)[0]->Reshape(bottom[0]->num(), CHANNELS_, POOLED_HEIGHT_, - POOLED_WIDTH_); + kernel_size_ = this->layer_param_.pooling_param().kernel_size(); + stride_ = this->layer_param_.pooling_param().stride(); + channels_ = bottom[0]->channels(); + height_ = bottom[0]->height(); + width_ = bottom[0]->width(); + pooled_height_ = static_cast( + ceil(static_cast(height_ - kernel_size_) / stride_)) + 1; + pooled_width_ = static_cast( + ceil(static_cast(width_ - kernel_size_) / stride_)) + 1; + (*top)[0]->Reshape(bottom[0]->num(), channels_, pooled_height_, + pooled_width_); // If stochastic pooling, we will initialize the random index part. if (this->layer_param_.pooling_param().pool() == PoolingParameter_PoolMethod_STOCHASTIC) { - rand_idx_.Reshape(bottom[0]->num(), CHANNELS_, POOLED_HEIGHT_, - POOLED_WIDTH_); + rand_idx_.Reshape(bottom[0]->num(), channels_, pooled_height_, + pooled_width_); } } @@ -55,18 +55,18 @@ Dtype PoolingLayer::Forward_cpu(const vector*>& bottom, } // The main loop for (int n = 0; n < bottom[0]->num(); ++n) { - for (int c = 0; c < CHANNELS_; ++c) { - for (int ph = 0; ph < POOLED_HEIGHT_; ++ph) { - for (int pw = 0; pw < POOLED_WIDTH_; ++pw) { - int hstart = ph * STRIDE_; - int wstart = pw * STRIDE_; - int hend = min(hstart + KSIZE_, HEIGHT_); - int wend = min(wstart + KSIZE_, WIDTH_); + for (int c = 0; c < channels_; ++c) { + for (int ph = 0; ph < pooled_height_; ++ph) { + for (int pw = 0; pw < pooled_width_; ++pw) { + int hstart = ph * stride_; + int wstart = pw * stride_; + int hend = min(hstart + kernel_size_, height_); + int wend = min(wstart + kernel_size_, width_); for (int h = hstart; h < hend; ++h) { for (int w = wstart; w < wend; ++w) { - top_data[ph * POOLED_WIDTH_ + pw] = - max(top_data[ph * POOLED_WIDTH_ + pw], - bottom_data[h * WIDTH_ + w]); + top_data[ph * pooled_width_ + pw] = + max(top_data[ph * pooled_width_ + pw], + bottom_data[h * width_ + w]); } } } @@ -83,20 +83,20 @@ Dtype PoolingLayer::Forward_cpu(const vector*>& bottom, } // The main loop for (int n = 0; n < bottom[0]->num(); ++n) { - for (int c = 0; c < CHANNELS_; ++c) { - for (int ph = 0; ph < POOLED_HEIGHT_; ++ph) { - for (int pw = 0; pw < POOLED_WIDTH_; ++pw) { - int hstart = ph * STRIDE_; - int wstart = pw * STRIDE_; - int hend = min(hstart + KSIZE_, HEIGHT_); - int wend = min(wstart + KSIZE_, WIDTH_); + for (int c = 0; c < channels_; ++c) { + for (int ph = 0; ph < pooled_height_; ++ph) { + for (int pw = 0; pw < pooled_width_; ++pw) { + int hstart = ph * stride_; + int wstart = pw * stride_; + int hend = min(hstart + kernel_size_, height_); + int wend = min(wstart + kernel_size_, width_); for (int h = hstart; h < hend; ++h) { for (int w = wstart; w < wend; ++w) { - top_data[ph * POOLED_WIDTH_ + pw] += - bottom_data[h * WIDTH_ + w]; + top_data[ph * pooled_width_ + pw] += + bottom_data[h * width_ + w]; } } - top_data[ph * POOLED_WIDTH_ + pw] /= + top_data[ph * pooled_width_ + pw] /= (hend - hstart) * (wend - wstart); } } @@ -132,19 +132,19 @@ void PoolingLayer::Backward_cpu(const vector*>& top, case PoolingParameter_PoolMethod_MAX: // The main loop for (int n = 0; n < top[0]->num(); ++n) { - for (int c = 0; c < CHANNELS_; ++c) { - for (int ph = 0; ph < POOLED_HEIGHT_; ++ph) { - for (int pw = 0; pw < POOLED_WIDTH_; ++pw) { - int hstart = ph * STRIDE_; - int wstart = pw * STRIDE_; - int hend = min(hstart + KSIZE_, HEIGHT_); - int wend = min(wstart + KSIZE_, WIDTH_); + for (int c = 0; c < channels_; ++c) { + for (int ph = 0; ph < pooled_height_; ++ph) { + for (int pw = 0; pw < pooled_width_; ++pw) { + int hstart = ph * stride_; + int wstart = pw * stride_; + int hend = min(hstart + kernel_size_, height_); + int wend = min(wstart + kernel_size_, width_); for (int h = hstart; h < hend; ++h) { for (int w = wstart; w < wend; ++w) { - bottom_diff[h * WIDTH_ + w] += - top_diff[ph * POOLED_WIDTH_ + pw] * - (bottom_data[h * WIDTH_ + w] == - top_data[ph * POOLED_WIDTH_ + pw]); + bottom_diff[h * width_ + w] += + top_diff[ph * pooled_width_ + pw] * + (bottom_data[h * width_ + w] == + top_data[ph * pooled_width_ + pw]); } } } @@ -160,18 +160,18 @@ void PoolingLayer::Backward_cpu(const vector*>& top, case PoolingParameter_PoolMethod_AVE: // The main loop for (int n = 0; n < top[0]->num(); ++n) { - for (int c = 0; c < CHANNELS_; ++c) { - for (int ph = 0; ph < POOLED_HEIGHT_; ++ph) { - for (int pw = 0; pw < POOLED_WIDTH_; ++pw) { - int hstart = ph * STRIDE_; - int wstart = pw * STRIDE_; - int hend = min(hstart + KSIZE_, HEIGHT_); - int wend = min(wstart + KSIZE_, WIDTH_); + for (int c = 0; c < channels_; ++c) { + for (int ph = 0; ph < pooled_height_; ++ph) { + for (int pw = 0; pw < pooled_width_; ++pw) { + int hstart = ph * stride_; + int wstart = pw * stride_; + int hend = min(hstart + kernel_size_, height_); + int wend = min(wstart + kernel_size_, width_); int poolsize = (hend - hstart) * (wend - wstart); for (int h = hstart; h < hend; ++h) { for (int w = wstart; w < wend; ++w) { - bottom_diff[h * WIDTH_ + w] += - top_diff[ph * POOLED_WIDTH_ + pw] / poolsize; + bottom_diff[h * width_ + w] += + top_diff[ph * pooled_width_ + pw] / poolsize; } } } diff --git a/src/caffe/layers/pooling_layer.cu b/src/caffe/layers/pooling_layer.cu index ab72279e827..7adf348be34 100644 --- a/src/caffe/layers/pooling_layer.cu +++ b/src/caffe/layers/pooling_layer.cu @@ -16,17 +16,17 @@ namespace caffe { template __global__ void MaxPoolForward(const int nthreads, const Dtype* bottom_data, const int num, const int channels, const int height, - const int width, const int pooled_height, const int pooled_width, - const int ksize, const int stride, Dtype* top_data) { + const int width, const int pooled_height_, const int pooled_width, + const int kernel_size, const int stride, Dtype* top_data) { CUDA_KERNEL_LOOP(index, nthreads) { int pw = index % pooled_width; - int ph = (index / pooled_width) % pooled_height; - int c = (index / pooled_width / pooled_height) % channels; - int n = index / pooled_width / pooled_height / channels; + int ph = (index / pooled_width) % pooled_height_; + int c = (index / pooled_width / pooled_height_) % channels; + int n = index / pooled_width / pooled_height_ / channels; int hstart = ph * stride; - int hend = min(hstart + ksize, height); + int hend = min(hstart + kernel_size, height); int wstart = pw * stride; - int wend = min(wstart + ksize, width); + int wend = min(wstart + kernel_size, width); Dtype maxval = -FLT_MAX; bottom_data += (n * channels + c) * height * width; for (int h = hstart; h < hend; ++h) { @@ -41,17 +41,17 @@ __global__ void MaxPoolForward(const int nthreads, const Dtype* bottom_data, template __global__ void AvePoolForward(const int nthreads, const Dtype* bottom_data, const int num, const int channels, const int height, - const int width, const int pooled_height, const int pooled_width, - const int ksize, const int stride, Dtype* top_data) { + const int width, const int pooled_height_, const int pooled_width, + const int kernel_size, const int stride, Dtype* top_data) { CUDA_KERNEL_LOOP(index, nthreads) { int pw = index % pooled_width; - int ph = (index / pooled_width) % pooled_height; - int c = (index / pooled_width / pooled_height) % channels; - int n = index / pooled_width / pooled_height / channels; + int ph = (index / pooled_width) % pooled_height_; + int c = (index / pooled_width / pooled_height_) % channels; + int n = index / pooled_width / pooled_height_ / channels; int hstart = ph * stride; - int hend = min(hstart + ksize, height); + int hend = min(hstart + kernel_size, height); int wstart = pw * stride; - int wend = min(wstart + ksize, width); + int wend = min(wstart + kernel_size, width); Dtype aveval = 0; bottom_data += (n * channels + c) * height * width; for (int h = hstart; h < hend; ++h) { @@ -67,17 +67,17 @@ template __global__ void StoPoolForwardTrain(const int nthreads, const Dtype* bottom_data, const int num, const int channels, const int height, - const int width, const int pooled_height, const int pooled_width, - const int ksize, const int stride, float* rand_idx, Dtype* top_data) { + const int width, const int pooled_height_, const int pooled_width, + const int kernel_size, const int stride, float* rand_idx, Dtype* top_data) { CUDA_KERNEL_LOOP(index, nthreads) { int pw = index % pooled_width; - int ph = (index / pooled_width) % pooled_height; - int c = (index / pooled_width / pooled_height) % channels; - int n = index / pooled_width / pooled_height / channels; + int ph = (index / pooled_width) % pooled_height_; + int c = (index / pooled_width / pooled_height_) % channels; + int n = index / pooled_width / pooled_height_ / channels; int hstart = ph * stride; - int hend = min(hstart + ksize, height); + int hend = min(hstart + kernel_size, height); int wstart = pw * stride; - int wend = min(wstart + ksize, width); + int wend = min(wstart + kernel_size, width); Dtype cumsum = 0.; bottom_data += (n * channels + c) * height * width; // First pass: get sum @@ -107,17 +107,17 @@ template __global__ void StoPoolForwardTest(const int nthreads, const Dtype* bottom_data, const int num, const int channels, const int height, - const int width, const int pooled_height, const int pooled_width, - const int ksize, const int stride, Dtype* top_data) { + const int width, const int pooled_height_, const int pooled_width, + const int kernel_size, const int stride, Dtype* top_data) { CUDA_KERNEL_LOOP(index, nthreads) { int pw = index % pooled_width; - int ph = (index / pooled_width) % pooled_height; - int c = (index / pooled_width / pooled_height) % channels; - int n = index / pooled_width / pooled_height / channels; + int ph = (index / pooled_width) % pooled_height_; + int c = (index / pooled_width / pooled_height_) % channels; + int n = index / pooled_width / pooled_height_ / channels; int hstart = ph * stride; - int hend = min(hstart + ksize, height); + int hend = min(hstart + kernel_size, height); int wstart = pw * stride; - int wend = min(wstart + ksize, width); + int wend = min(wstart + kernel_size, width); // We set cumsum to be 0 to avoid divide-by-zero problems Dtype cumsum = FLT_MIN; Dtype cumvalues = 0.; @@ -144,15 +144,15 @@ Dtype PoolingLayer::Forward_gpu(const vector*>& bottom, case PoolingParameter_PoolMethod_MAX: // NOLINT_NEXT_LINE(whitespace/operators) MaxPoolForward<<>>( - count, bottom_data, bottom[0]->num(), CHANNELS_, - HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, + count, bottom_data, bottom[0]->num(), channels_, + height_, width_, pooled_height_, pooled_width_, kernel_size_, stride_, top_data); break; case PoolingParameter_PoolMethod_AVE: // NOLINT_NEXT_LINE(whitespace/operators) AvePoolForward<<>>( - count, bottom_data, bottom[0]->num(), CHANNELS_, - HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, + count, bottom_data, bottom[0]->num(), channels_, + height_, width_, pooled_height_, pooled_width_, kernel_size_, stride_, top_data); break; case PoolingParameter_PoolMethod_STOCHASTIC: @@ -163,15 +163,15 @@ Dtype PoolingLayer::Forward_gpu(const vector*>& bottom, // NOLINT_NEXT_LINE(whitespace/operators) StoPoolForwardTrain<<>>( - count, bottom_data, bottom[0]->num(), CHANNELS_, - HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, + count, bottom_data, bottom[0]->num(), channels_, + height_, width_, pooled_height_, pooled_width_, kernel_size_, stride_, rand_idx_.mutable_gpu_data(), top_data); } else { // NOLINT_NEXT_LINE(whitespace/operators) StoPoolForwardTest<<>>( - count, bottom_data, bottom[0]->num(), CHANNELS_, - HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, + count, bottom_data, bottom[0]->num(), channels_, + height_, width_, pooled_height_, pooled_width_, kernel_size_, stride_, top_data); } break; @@ -186,8 +186,8 @@ template __global__ void MaxPoolBackward(const int nthreads, const Dtype* bottom_data, const Dtype* top_data, const Dtype* top_diff, const int num, const int channels, const int height, - const int width, const int pooled_height, const int pooled_width, - const int ksize, const int stride, Dtype* bottom_diff) { + const int width, const int pooled_height_, const int pooled_width, + const int kernel_size, const int stride, Dtype* bottom_diff) { CUDA_KERNEL_LOOP(index, nthreads) { // find out the local index // find out the local offset @@ -195,15 +195,15 @@ __global__ void MaxPoolBackward(const int nthreads, const Dtype* bottom_data, int h = (index / width) % height; int c = (index / width / height) % channels; int n = index / width / height / channels; - int phstart = (h < ksize) ? 0 : (h - ksize) / stride + 1; - int phend = min(h / stride + 1, pooled_height); - int pwstart = (w < ksize) ? 0 : (w - ksize) / stride + 1; + int phstart = (h < kernel_size) ? 0 : (h - kernel_size) / stride + 1; + int phend = min(h / stride + 1, pooled_height_); + int pwstart = (w < kernel_size) ? 0 : (w - kernel_size) / stride + 1; int pwend = min(w / stride + 1, pooled_width); Dtype gradient = 0; Dtype bottom_datum = bottom_data[((n * channels + c) * height + h) * width + w]; - top_data += (n * channels + c) * pooled_height * pooled_width; - top_diff += (n * channels + c) * pooled_height * pooled_width; + top_data += (n * channels + c) * pooled_height_ * pooled_width; + top_diff += (n * channels + c) * pooled_height_ * pooled_width; for (int ph = phstart; ph < phend; ++ph) { for (int pw = pwstart; pw < pwend; ++pw) { gradient += top_diff[ph * pooled_width + pw] * @@ -218,8 +218,8 @@ __global__ void MaxPoolBackward(const int nthreads, const Dtype* bottom_data, template __global__ void AvePoolBackward(const int nthreads, const Dtype* top_diff, const int num, const int channels, const int height, - const int width, const int pooled_height, const int pooled_width, - const int ksize, const int stride, Dtype* bottom_diff) { + const int width, const int pooled_height_, const int pooled_width, + const int kernel_size, const int stride, Dtype* bottom_diff) { CUDA_KERNEL_LOOP(index, nthreads) { // find out the local index // find out the local offset @@ -227,17 +227,17 @@ __global__ void AvePoolBackward(const int nthreads, const Dtype* top_diff, int h = (index / width) % height; int c = (index / width / height) % channels; int n = index / width / height / channels; - int phstart = (h < ksize) ? 0 : (h - ksize) / stride + 1; - int phend = min(h / stride + 1, pooled_height); - int pwstart = (w < ksize) ? 0 : (w - ksize) / stride + 1; + int phstart = (h < kernel_size) ? 0 : (h - kernel_size) / stride + 1; + int phend = min(h / stride + 1, pooled_height_); + int pwstart = (w < kernel_size) ? 0 : (w - kernel_size) / stride + 1; int pwend = min(w / stride + 1, pooled_width); Dtype gradient = 0; - top_diff += (n * channels + c) * pooled_height * pooled_width; + top_diff += (n * channels + c) * pooled_height_ * pooled_width; for (int ph = phstart; ph < phend; ++ph) { for (int pw = pwstart; pw < pwend; ++pw) { // figure out the pooling size - int poolsize = (min(ph * stride + ksize, height) - ph * stride) * - (min(pw * stride + ksize, width) - pw * stride); + int poolsize = (min(ph * stride + kernel_size, height) - ph * stride) * + (min(pw * stride + kernel_size, width) - pw * stride); gradient += top_diff[ph * pooled_width + pw] / poolsize; } } @@ -250,8 +250,8 @@ template __global__ void StoPoolBackward(const int nthreads, const float* rand_idx, const Dtype* top_diff, const int num, const int channels, const int height, - const int width, const int pooled_height, const int pooled_width, - const int ksize, const int stride, Dtype* bottom_diff) { + const int width, const int pooled_height_, const int pooled_width, + const int kernel_size, const int stride, Dtype* bottom_diff) { CUDA_KERNEL_LOOP(index, nthreads) { // find out the local index // find out the local offset @@ -259,13 +259,13 @@ __global__ void StoPoolBackward(const int nthreads, int h = (index / width) % height; int c = (index / width / height) % channels; int n = index / width / height / channels; - int phstart = (h < ksize) ? 0 : (h - ksize) / stride + 1; - int phend = min(h / stride + 1, pooled_height); - int pwstart = (w < ksize) ? 0 : (w - ksize) / stride + 1; + int phstart = (h < kernel_size) ? 0 : (h - kernel_size) / stride + 1; + int phend = min(h / stride + 1, pooled_height_); + int pwstart = (w < kernel_size) ? 0 : (w - kernel_size) / stride + 1; int pwend = min(w / stride + 1, pooled_width); Dtype gradient = 0; - rand_idx += (n * channels + c) * pooled_height * pooled_width; - top_diff += (n * channels + c) * pooled_height * pooled_width; + rand_idx += (n * channels + c) * pooled_height_ * pooled_width; + top_diff += (n * channels + c) * pooled_height_ * pooled_width; for (int ph = phstart; ph < phend; ++ph) { for (int pw = pwstart; pw < pwend; ++pw) { gradient += top_diff[ph * pooled_width + pw] * @@ -291,22 +291,22 @@ void PoolingLayer::Backward_gpu(const vector*>& top, // NOLINT_NEXT_LINE(whitespace/operators) MaxPoolBackward<<>>( count, (*bottom)[0]->gpu_data(), top[0]->gpu_data(), top_diff, - top[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, - POOLED_WIDTH_, KSIZE_, STRIDE_, bottom_diff); + top[0]->num(), channels_, height_, width_, pooled_height_, + pooled_width_, kernel_size_, stride_, bottom_diff); break; case PoolingParameter_PoolMethod_AVE: // NOLINT_NEXT_LINE(whitespace/operators) AvePoolBackward<<>>( - count, top_diff, top[0]->num(), CHANNELS_, - HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, + count, top_diff, top[0]->num(), channels_, + height_, width_, pooled_height_, pooled_width_, kernel_size_, stride_, bottom_diff); break; case PoolingParameter_PoolMethod_STOCHASTIC: // NOLINT_NEXT_LINE(whitespace/operators) StoPoolBackward<<>>( count, rand_idx_.gpu_data(), top_diff, - top[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, - POOLED_WIDTH_, KSIZE_, STRIDE_, bottom_diff); + top[0]->num(), channels_, height_, width_, pooled_height_, + pooled_width_, kernel_size_, stride_, bottom_diff); break; default: LOG(FATAL) << "Unknown pooling method."; diff --git a/src/caffe/layers/window_data_layer.cpp b/src/caffe/layers/window_data_layer.cpp index 9346c6f0574..6f6017566bb 100644 --- a/src/caffe/layers/window_data_layer.cpp +++ b/src/caffe/layers/window_data_layer.cpp @@ -63,7 +63,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { * fg_fraction); const int num_samples[2] = { batch_size - num_fg, num_fg }; - int itemid = 0; + int item_id = 0; // sample from bg set then fg set for (int is_fg = 0; is_fg < 2; ++is_fg) { for (int dummy = 0; dummy < num_samples[is_fg]; ++dummy) { @@ -197,7 +197,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { Dtype pixel = static_cast(cv_cropped_img.at(h, w)[c]); - top_data[((itemid * channels + c) * crop_size + h + pad_h) + top_data[((item_id * channels + c) * crop_size + h + pad_h) * crop_size + w + pad_w] = (pixel - mean[(c * mean_height + h + mean_off + pad_h) @@ -208,7 +208,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { } // get window label - top_label[itemid] = window[WindowDataLayer::LABEL]; + top_label[item_id] = window[WindowDataLayer::LABEL]; #if 0 // useful debugging code for dumping transformed windows to disk @@ -225,7 +225,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { << window[WindowDataLayer::X2]+1 << std::endl << window[WindowDataLayer::Y2]+1 << std::endl << do_mirror << std::endl - << top_label[itemid] << std::endl + << top_label[item_id] << std::endl << is_fg << std::endl; inf.close(); std::ofstream top_data_file((string("dump/") + file_id + @@ -235,7 +235,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { for (int h = 0; h < crop_size; ++h) { for (int w = 0; w < crop_size; ++w) { top_data_file.write(reinterpret_cast( - &top_data[((itemid * channels + c) * crop_size + h) + &top_data[((item_id * channels + c) * crop_size + h) * crop_size + w]), sizeof(Dtype)); } @@ -244,7 +244,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { top_data_file.close(); #endif - itemid++; + item_id++; } } From a68395c2ea2b6092b71c2c98a84920e730044fd0 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 15:01:55 -0700 Subject: [PATCH 38/60] alphabetize classes in vision_layers.hpp --- include/caffe/vision_layers.hpp | 372 +++++++++++++++----------------- 1 file changed, 178 insertions(+), 194 deletions(-) diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp index d80809b95a8..079537e9aa0 100644 --- a/include/caffe/vision_layers.hpp +++ b/include/caffe/vision_layers.hpp @@ -34,11 +34,10 @@ class NeuronLayer : public Layer { vector*>* top); }; - template -class ReLULayer : public NeuronLayer { +class BNLLLayer : public NeuronLayer { public: - explicit ReLULayer(const LayerParameter& param) + explicit BNLLLayer(const LayerParameter& param) : NeuronLayer(param) {} protected: @@ -46,7 +45,6 @@ class ReLULayer : public NeuronLayer { vector*>* top); virtual Dtype Forward_gpu(const vector*>& bottom, vector*>* top); - virtual void Backward_cpu(const vector*>& top, const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, @@ -54,10 +52,12 @@ class ReLULayer : public NeuronLayer { }; template -class TanHLayer : public NeuronLayer { +class DropoutLayer : public NeuronLayer { public: - explicit TanHLayer(const LayerParameter& param) + explicit DropoutLayer(const LayerParameter& param) : NeuronLayer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); protected: virtual Dtype Forward_cpu(const vector*>& bottom, @@ -68,12 +68,17 @@ class TanHLayer : public NeuronLayer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); + + shared_ptr rand_vec_; + float threshold_; + float scale_; + unsigned int uint_thres_; }; template -class SigmoidLayer : public NeuronLayer { +class ReLULayer : public NeuronLayer { public: - explicit SigmoidLayer(const LayerParameter& param) + explicit ReLULayer(const LayerParameter& param) : NeuronLayer(param) {} protected: @@ -81,17 +86,17 @@ class SigmoidLayer : public NeuronLayer { vector*>* top); virtual Dtype Forward_gpu(const vector*>& bottom, vector*>* top); + virtual void Backward_cpu(const vector*>& top, const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); }; - template -class BNLLLayer : public NeuronLayer { +class SigmoidLayer : public NeuronLayer { public: - explicit BNLLLayer(const LayerParameter& param) + explicit SigmoidLayer(const LayerParameter& param) : NeuronLayer(param) {} protected: @@ -105,14 +110,11 @@ class BNLLLayer : public NeuronLayer { const bool propagate_down, vector*>* bottom); }; - template -class DropoutLayer : public NeuronLayer { +class TanHLayer : public NeuronLayer { public: - explicit DropoutLayer(const LayerParameter& param) + explicit TanHLayer(const LayerParameter& param) : NeuronLayer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); protected: virtual Dtype Forward_cpu(const vector*>& bottom, @@ -123,18 +125,13 @@ class DropoutLayer : public NeuronLayer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - - shared_ptr rand_vec_; - float threshold_; - float scale_; - unsigned int uint_thres_; }; template -class SplitLayer : public Layer { +class AccuracyLayer : public Layer { public: - explicit SplitLayer(const LayerParameter& param) + explicit AccuracyLayer(const LayerParameter& param) : Layer(param) {} virtual void SetUp(const vector*>& bottom, vector*>* top); @@ -142,21 +139,17 @@ class SplitLayer : public Layer { protected: virtual Dtype Forward_cpu(const vector*>& bottom, vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); + // The accuracy layer should not be used to compute backward operations. virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - - int count_; + const bool propagate_down, vector*>* bottom) { + NOT_IMPLEMENTED; + } }; - template -class FlattenLayer : public Layer { +class ConcatLayer : public Layer { public: - explicit FlattenLayer(const LayerParameter& param) + explicit ConcatLayer(const LayerParameter& param) : Layer(param) {} virtual void SetUp(const vector*>& bottom, vector*>* top); @@ -171,14 +164,19 @@ class FlattenLayer : public Layer { virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); + Blob col_bob_; int count_; + int num_; + int channels_; + int height_; + int width_; + int concat_dim_; }; - template -class InnerProductLayer : public Layer { +class ConvolutionLayer : public Layer { public: - explicit InnerProductLayer(const LayerParameter& param) + explicit ConvolutionLayer(const LayerParameter& param) : Layer(param) {} virtual void SetUp(const vector*>& bottom, vector*>* top); @@ -188,25 +186,41 @@ class InnerProductLayer : public Layer { vector*>* top); virtual Dtype Forward_gpu(const vector*>& bottom, vector*>* top); - virtual void Backward_cpu(const vector*>& top, const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); + int kernel_size_; + int stride_; + int num_; + int channels_; + int pad_; + int height_; + int width_; + int num_output_; + int group_; + Blob col_buffer_; + shared_ptr bias_multiplier_; + bool bias_term_; int M_; int K_; int N_; - bool bias_term_; - shared_ptr bias_multiplier_; }; +// This function is used to create a pthread that prefetches the data. +template +void* DataLayerPrefetch(void* layer_pointer); template -class LRNLayer : public Layer { +class DataLayer : public Layer { + // The function used to perform prefetching. + friend void* DataLayerPrefetch(void* layer_pointer); + public: - explicit LRNLayer(const LayerParameter& param) + explicit DataLayer(const LayerParameter& param) : Layer(param) {} + virtual ~DataLayer(); virtual void SetUp(const vector*>& bottom, vector*>* top); @@ -216,53 +230,49 @@ class LRNLayer : public Layer { virtual Dtype Forward_gpu(const vector*>& bottom, vector*>* top); virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); + const bool propagate_down, vector*>* bottom) { return; } virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); + const bool propagate_down, vector*>* bottom) { return; } - // scale_ stores the intermediate summing results - Blob scale_; - int size_; - int pre_pad_; - Dtype alpha_; - Dtype beta_; - int num_; - int channels_; - int height_; - int width_; + shared_ptr db_; + shared_ptr iter_; + int datum_channels_; + int datum_height_; + int datum_width_; + int datum_size_; + pthread_t thread_; + shared_ptr > prefetch_data_; + shared_ptr > prefetch_label_; + Blob data_mean_; }; - template -class Im2colLayer : public Layer { +class EuclideanLossLayer : public Layer { public: - explicit Im2colLayer(const LayerParameter& param) - : Layer(param) {} + explicit EuclideanLossLayer(const LayerParameter& param) + : Layer(param), difference_() {} virtual void SetUp(const vector*>& bottom, vector*>* top); protected: + // The loss layer will do nothing during forward - all computation are + // carried out in the backward pass. virtual Dtype Forward_cpu(const vector*>& bottom, vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); + // virtual Dtype Forward_gpu(const vector*>& bottom, + // vector*>* top); virtual void Backward_cpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); + // virtual void Backward_gpu(const vector*>& top, + // const bool propagate_down, vector*>* bottom); - int kernel_size_; - int stride_; - int channels_; - int height_; - int width_; - int pad_; + Blob difference_; }; template -class PoolingLayer : public Layer { +class FlattenLayer : public Layer { public: - explicit PoolingLayer(const LayerParameter& param) + explicit FlattenLayer(const LayerParameter& param) : Layer(param) {} virtual void SetUp(const vector*>& bottom, vector*>* top); @@ -277,22 +287,15 @@ class PoolingLayer : public Layer { virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - int kernel_size_; - int stride_; - int channels_; - int height_; - int width_; - int pooled_height_; - int pooled_width_; - Blob rand_idx_; + int count_; }; - template -class ConvolutionLayer : public Layer { +class HDF5DataLayer : public Layer { public: - explicit ConvolutionLayer(const LayerParameter& param) + explicit HDF5DataLayer(const LayerParameter& param) : Layer(param) {} + virtual ~HDF5DataLayer(); virtual void SetUp(const vector*>& bottom, vector*>* top); @@ -305,28 +308,20 @@ class ConvolutionLayer : public Layer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); + virtual void load_hdf5_file_data(const char* filename); - int kernel_size_; - int stride_; - int num_; - int channels_; - int pad_; - int height_; - int width_; - int num_output_; - int group_; - Blob col_buffer_; - shared_ptr bias_multiplier_; - bool bias_term_; - int M_; - int K_; - int N_; + std::vector hdf_filenames_; + unsigned int num_files_; + unsigned int current_file_; + hsize_t current_row_; + Blob data_blob_; + Blob label_blob_; }; template -class ConcatLayer : public Layer { +class Im2colLayer : public Layer { public: - explicit ConcatLayer(const LayerParameter& param) + explicit Im2colLayer(const LayerParameter& param) : Layer(param) {} virtual void SetUp(const vector*>& bottom, vector*>* top); @@ -341,28 +336,27 @@ class ConcatLayer : public Layer { virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - Blob col_bob_; - int count_; - int num_; + int kernel_size_; + int stride_; int channels_; int height_; int width_; - int concat_dim_; + int pad_; }; // This function is used to create a pthread that prefetches the data. template -void* DataLayerPrefetch(void* layer_pointer); +void* ImagesLayerPrefetch(void* layer_pointer); template -class DataLayer : public Layer { +class ImagesLayer : public Layer { // The function used to perform prefetching. - friend void* DataLayerPrefetch(void* layer_pointer); + friend void* ImagesLayerPrefetch(void* layer_pointer); public: - explicit DataLayer(const LayerParameter& param) + explicit ImagesLayer(const LayerParameter& param) : Layer(param) {} - virtual ~DataLayer(); + virtual ~ImagesLayer(); virtual void SetUp(const vector*>& bottom, vector*>* top); @@ -376,8 +370,8 @@ class DataLayer : public Layer { virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom) { return; } - shared_ptr db_; - shared_ptr iter_; + vector > lines_; + int lines_id_; int datum_channels_; int datum_height_; int datum_width_; @@ -388,51 +382,34 @@ class DataLayer : public Layer { Blob data_mean_; }; -// This function is used to create a pthread that prefetches the data. -template -void* ImagesLayerPrefetch(void* layer_pointer); - template -class ImagesLayer : public Layer { - // The function used to perform prefetching. - friend void* ImagesLayerPrefetch(void* layer_pointer); - +class InfogainLossLayer : public Layer { public: - explicit ImagesLayer(const LayerParameter& param) - : Layer(param) {} - virtual ~ImagesLayer(); + explicit InfogainLossLayer(const LayerParameter& param) + : Layer(param), infogain_() {} virtual void SetUp(const vector*>& bottom, vector*>* top); protected: + // The loss layer will do nothing during forward - all computation are + // carried out in the backward pass. virtual Dtype Forward_cpu(const vector*>& bottom, vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); + // virtual Dtype Forward_gpu(const vector*>& bottom, + // vector*>* top); virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { return; } - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { return; } + const bool propagate_down, vector*>* bottom); + // virtual void Backward_gpu(const vector*>& top, + // const bool propagate_down, vector*>* bottom); - vector > lines_; - int lines_id_; - int datum_channels_; - int datum_height_; - int datum_width_; - int datum_size_; - pthread_t thread_; - shared_ptr > prefetch_data_; - shared_ptr > prefetch_label_; - Blob data_mean_; + Blob infogain_; }; - template -class HDF5DataLayer : public Layer { +class InnerProductLayer : public Layer { public: - explicit HDF5DataLayer(const LayerParameter& param) + explicit InnerProductLayer(const LayerParameter& param) : Layer(param) {} - virtual ~HDF5DataLayer(); virtual void SetUp(const vector*>& bottom, vector*>* top); @@ -445,17 +422,14 @@ class HDF5DataLayer : public Layer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - virtual void load_hdf5_file_data(const char* filename); - std::vector hdf_filenames_; - unsigned int num_files_; - unsigned int current_file_; - hsize_t current_row_; - Blob data_blob_; - Blob label_blob_; + int M_; + int K_; + int N_; + bool bias_term_; + shared_ptr bias_multiplier_; }; - template class HDF5OutputLayer : public Layer { public: @@ -482,11 +456,10 @@ class HDF5OutputLayer : public Layer { Blob label_blob_; }; - template -class SoftmaxLayer : public Layer { +class LRNLayer : public Layer { public: - explicit SoftmaxLayer(const LayerParameter& param) + explicit LRNLayer(const LayerParameter& param) : Layer(param) {} virtual void SetUp(const vector*>& bottom, vector*>* top); @@ -499,15 +472,20 @@ class SoftmaxLayer : public Layer { virtual void Backward_cpu(const vector*>& top, const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); + const bool propagate_down, vector*>* bottom); - // sum_multiplier is just used to carry out sum using blas - Blob sum_multiplier_; - // scale is an intermediate blob to hold temporary results. + // scale_ stores the intermediate summing results Blob scale_; + int size_; + int pre_pad_; + Dtype alpha_; + Dtype beta_; + int num_; + int channels_; + int height_; + int width_; }; - template class MultinomialLogisticLossLayer : public Layer { public: @@ -530,28 +508,56 @@ class MultinomialLogisticLossLayer : public Layer { }; template -class InfogainLossLayer : public Layer { +class PoolingLayer : public Layer { public: - explicit InfogainLossLayer(const LayerParameter& param) - : Layer(param), infogain_() {} + explicit PoolingLayer(const LayerParameter& param) + : Layer(param) {} virtual void SetUp(const vector*>& bottom, vector*>* top); protected: - // The loss layer will do nothing during forward - all computation are - // carried out in the backward pass. virtual Dtype Forward_cpu(const vector*>& bottom, vector*>* top); - // virtual Dtype Forward_gpu(const vector*>& bottom, - // vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); virtual void Backward_cpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - // virtual void Backward_gpu(const vector*>& top, - // const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); - Blob infogain_; + int kernel_size_; + int stride_; + int channels_; + int height_; + int width_; + int pooled_height_; + int pooled_width_; + Blob rand_idx_; }; +template +class SoftmaxLayer : public Layer { + public: + explicit SoftmaxLayer(const LayerParameter& param) + : Layer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + + // sum_multiplier is just used to carry out sum using blas + Blob sum_multiplier_; + // scale is an intermediate blob to hold temporary results. + Blob scale_; +}; // SoftmaxWithLossLayer is a layer that implements softmax and then computes // the loss - it is preferred over softmax + multinomiallogisticloss in the @@ -584,47 +590,25 @@ class SoftmaxWithLossLayer : public Layer { vector*> softmax_top_vec_; }; - template -class EuclideanLossLayer : public Layer { +class SplitLayer : public Layer { public: - explicit EuclideanLossLayer(const LayerParameter& param) - : Layer(param), difference_() {} + explicit SplitLayer(const LayerParameter& param) + : Layer(param) {} virtual void SetUp(const vector*>& bottom, vector*>* top); protected: - // The loss layer will do nothing during forward - all computation are - // carried out in the backward pass. virtual Dtype Forward_cpu(const vector*>& bottom, vector*>* top); - // virtual Dtype Forward_gpu(const vector*>& bottom, - // vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); virtual void Backward_cpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - // virtual void Backward_gpu(const vector*>& top, - // const bool propagate_down, vector*>* bottom); - - Blob difference_; -}; - - -template -class AccuracyLayer : public Layer { - public: - explicit AccuracyLayer(const LayerParameter& param) - : Layer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - // The accuracy layer should not be used to compute backward operations. - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { - NOT_IMPLEMENTED; - } + int count_; }; // This function is used to create a pthread that prefetches the window data. From 2aea6bb3ce084f8be09183380941040352702dd9 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 15:14:08 -0700 Subject: [PATCH 39/60] some naming standardization: ImagesLayer -> ImageDataLayer (like other data layers), and load_hdf5_file_data -> LoadHDF5FileData --- docs/feature_extraction.md | 4 ++-- include/caffe/vision_layers.hpp | 18 +++++---------- src/caffe/layer_factory.cpp | 2 +- src/caffe/layers/hdf5_data_layer.cpp | 6 ++--- src/caffe/layers/hdf5_data_layer.cu | 2 +- ...{images_layer.cpp => image_data_layer.cpp} | 18 +++++++-------- .../{images_layer.cu => image_data_layer.cu} | 0 ...es_layer.cpp => test_image_data_layer.cpp} | 23 +++++++++++-------- 8 files changed, 35 insertions(+), 38 deletions(-) rename src/caffe/layers/{images_layer.cpp => image_data_layer.cpp} (94%) rename src/caffe/layers/{images_layer.cu => image_data_layer.cu} (100%) rename src/caffe/test/{test_images_layer.cpp => test_image_data_layer.cpp} (89%) diff --git a/docs/feature_extraction.md b/docs/feature_extraction.md index 13639fbf8fd..fa23e9c8708 100644 --- a/docs/feature_extraction.md +++ b/docs/feature_extraction.md @@ -22,7 +22,7 @@ We're going to use the images that ship with caffe. find `pwd`/examples/images -type f -exec echo {} \; > examples/_temp/temp.txt -The `ImagesLayer` we'll use expects labels after each filenames, so let's add a 0 to the end of each line +The `ImageDataLayer` we'll use expects labels after each filenames, so let's add a 0 to the end of each line sed "s/$/ 0/" examples/_temp/temp.txt > examples/_temp/file_list.txt @@ -37,7 +37,7 @@ Download the mean image of the ILSVRC dataset. We will use `data/ilsvrc212/imagenet_mean.binaryproto` in the network definition prototxt. Let's copy and modify the network definition. -We'll be using the `ImagesLayer`, which will load and resize images for us. +We'll be using the `ImageDataLayer`, which will load and resize images for us. cp examples/feature_extraction/imagenet_val.prototxt examples/_temp diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp index 079537e9aa0..870bb22fb94 100644 --- a/include/caffe/vision_layers.hpp +++ b/include/caffe/vision_layers.hpp @@ -255,8 +255,6 @@ class EuclideanLossLayer : public Layer { vector*>* top); protected: - // The loss layer will do nothing during forward - all computation are - // carried out in the backward pass. virtual Dtype Forward_cpu(const vector*>& bottom, vector*>* top); // virtual Dtype Forward_gpu(const vector*>& bottom, @@ -308,7 +306,7 @@ class HDF5DataLayer : public Layer { const bool propagate_down, vector*>* bottom); virtual void Backward_gpu(const vector*>& top, const bool propagate_down, vector*>* bottom); - virtual void load_hdf5_file_data(const char* filename); + virtual void LoadHDF5FileData(const char* filename); std::vector hdf_filenames_; unsigned int num_files_; @@ -346,17 +344,17 @@ class Im2colLayer : public Layer { // This function is used to create a pthread that prefetches the data. template -void* ImagesLayerPrefetch(void* layer_pointer); +void* ImageDataLayerPrefetch(void* layer_pointer); template -class ImagesLayer : public Layer { +class ImageDataLayer : public Layer { // The function used to perform prefetching. - friend void* ImagesLayerPrefetch(void* layer_pointer); + friend void* ImageDataLayerPrefetch(void* layer_pointer); public: - explicit ImagesLayer(const LayerParameter& param) + explicit ImageDataLayer(const LayerParameter& param) : Layer(param) {} - virtual ~ImagesLayer(); + virtual ~ImageDataLayer(); virtual void SetUp(const vector*>& bottom, vector*>* top); @@ -391,8 +389,6 @@ class InfogainLossLayer : public Layer { vector*>* top); protected: - // The loss layer will do nothing during forward - all computation are - // carried out in the backward pass. virtual Dtype Forward_cpu(const vector*>& bottom, vector*>* top); // virtual Dtype Forward_gpu(const vector*>& bottom, @@ -495,8 +491,6 @@ class MultinomialLogisticLossLayer : public Layer { vector*>* top); protected: - // The loss layer will do nothing during forward - all computation are - // carried out in the backward pass. virtual Dtype Forward_cpu(const vector*>& bottom, vector*>* top); // virtual Dtype Forward_gpu(const vector*>& bottom, diff --git a/src/caffe/layer_factory.cpp b/src/caffe/layer_factory.cpp index f86e12ef94d..f3e52a68237 100644 --- a/src/caffe/layer_factory.cpp +++ b/src/caffe/layer_factory.cpp @@ -43,7 +43,7 @@ Layer* GetLayer(const LayerParameter& param) { case LayerParameter_LayerType_HDF5_OUTPUT: return new HDF5OutputLayer(param); case LayerParameter_LayerType_IMAGE_DATA: - return new ImagesLayer(param); + return new ImageDataLayer(param); case LayerParameter_LayerType_IM2COL: return new Im2colLayer(param); case LayerParameter_LayerType_INFOGAIN_LOSS: diff --git a/src/caffe/layers/hdf5_data_layer.cpp b/src/caffe/layers/hdf5_data_layer.cpp index 035eea84bd2..cff4f7c7318 100644 --- a/src/caffe/layers/hdf5_data_layer.cpp +++ b/src/caffe/layers/hdf5_data_layer.cpp @@ -26,7 +26,7 @@ HDF5DataLayer::~HDF5DataLayer() { } // Load data and label from HDF5 filename into the class property blobs. template -void HDF5DataLayer::load_hdf5_file_data(const char* filename) { +void HDF5DataLayer::LoadHDF5FileData(const char* filename) { LOG(INFO) << "Loading HDF5 file" << filename; hid_t file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT); if (file_id < 0) { @@ -72,7 +72,7 @@ void HDF5DataLayer::SetUp(const vector*>& bottom, LOG(INFO) << "Number of files: " << num_files_; // Load the first HDF5 file and initialize the line counter. - load_hdf5_file_data(hdf_filenames_[current_file_].c_str()); + LoadHDF5FileData(hdf_filenames_[current_file_].c_str()); current_row_ = 0; // Reshape blobs. @@ -101,7 +101,7 @@ Dtype HDF5DataLayer::Forward_cpu(const vector*>& bottom, current_file_ = 0; LOG(INFO) << "looping around to first file"; } - load_hdf5_file_data(hdf_filenames_[current_file_].c_str()); + LoadHDF5FileData(hdf_filenames_[current_file_].c_str()); } current_row_ = 0; } diff --git a/src/caffe/layers/hdf5_data_layer.cu b/src/caffe/layers/hdf5_data_layer.cu index e8d09c01ef5..9c5bb5a818f 100644 --- a/src/caffe/layers/hdf5_data_layer.cu +++ b/src/caffe/layers/hdf5_data_layer.cu @@ -36,7 +36,7 @@ Dtype HDF5DataLayer::Forward_gpu(const vector*>& bottom, LOG(INFO) << "looping around to first file"; } - load_hdf5_file_data(hdf_filenames_[current_file_].c_str()); + LoadHDF5FileData(hdf_filenames_[current_file_].c_str()); } current_row_ = 0; } diff --git a/src/caffe/layers/images_layer.cpp b/src/caffe/layers/image_data_layer.cpp similarity index 94% rename from src/caffe/layers/images_layer.cpp rename to src/caffe/layers/image_data_layer.cpp index 63f79ca0cd2..8c23cc41053 100644 --- a/src/caffe/layers/images_layer.cpp +++ b/src/caffe/layers/image_data_layer.cpp @@ -19,10 +19,10 @@ using std::pair; namespace caffe { template -void* ImagesLayerPrefetch(void* layer_pointer) { +void* ImageDataLayerPrefetch(void* layer_pointer) { CHECK(layer_pointer); - ImagesLayer* layer = - reinterpret_cast*>(layer_pointer); + ImageDataLayer* layer = + reinterpret_cast*>(layer_pointer); CHECK(layer); Datum datum; CHECK(layer->prefetch_data_); @@ -133,13 +133,13 @@ void* ImagesLayerPrefetch(void* layer_pointer) { } template -ImagesLayer::~ImagesLayer() { +ImageDataLayer::~ImageDataLayer() { // Finally, join the thread CHECK(!pthread_join(thread_, NULL)) << "Pthread joining failed."; } template -void ImagesLayer::SetUp(const vector*>& bottom, +void ImageDataLayer::SetUp(const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom.size(), 0) << "Input Layer takes no input blobs."; CHECK_EQ(top->size(), 2) << "Input Layer takes two blobs as output."; @@ -228,13 +228,13 @@ void ImagesLayer::SetUp(const vector*>& bottom, prefetch_label_->mutable_cpu_data(); data_mean_.cpu_data(); DLOG(INFO) << "Initializing prefetch"; - CHECK(!pthread_create(&thread_, NULL, ImagesLayerPrefetch, + CHECK(!pthread_create(&thread_, NULL, ImageDataLayerPrefetch, reinterpret_cast(this))) << "Pthread execution failed."; DLOG(INFO) << "Prefetch initialized."; } template -Dtype ImagesLayer::Forward_cpu(const vector*>& bottom, +Dtype ImageDataLayer::Forward_cpu(const vector*>& bottom, vector*>* top) { // First, join the thread CHECK(!pthread_join(thread_, NULL)) << "Pthread joining failed."; @@ -244,11 +244,11 @@ Dtype ImagesLayer::Forward_cpu(const vector*>& bottom, memcpy((*top)[1]->mutable_cpu_data(), prefetch_label_->cpu_data(), sizeof(Dtype) * prefetch_label_->count()); // Start a new prefetch thread - CHECK(!pthread_create(&thread_, NULL, ImagesLayerPrefetch, + CHECK(!pthread_create(&thread_, NULL, ImageDataLayerPrefetch, reinterpret_cast(this))) << "Pthread execution failed."; return Dtype(0.); } -INSTANTIATE_CLASS(ImagesLayer); +INSTANTIATE_CLASS(ImageDataLayer); } // namespace caffe diff --git a/src/caffe/layers/images_layer.cu b/src/caffe/layers/image_data_layer.cu similarity index 100% rename from src/caffe/layers/images_layer.cu rename to src/caffe/layers/image_data_layer.cu diff --git a/src/caffe/test/test_images_layer.cpp b/src/caffe/test/test_image_data_layer.cpp similarity index 89% rename from src/caffe/test/test_images_layer.cpp rename to src/caffe/test/test_image_data_layer.cpp index 0cd1001a089..9a6271cbea8 100644 --- a/src/caffe/test/test_images_layer.cpp +++ b/src/caffe/test/test_image_data_layer.cpp @@ -22,9 +22,9 @@ namespace caffe { extern cudaDeviceProp CAFFE_TEST_CUDA_PROP; template -class ImagesLayerTest : public ::testing::Test { +class ImageDataLayerTest : public ::testing::Test { protected: - ImagesLayerTest() + ImageDataLayerTest() : blob_top_data_(new Blob()), blob_top_label_(new Blob()), filename(NULL) {} @@ -41,7 +41,10 @@ class ImagesLayerTest : public ::testing::Test { outfile.close(); } - virtual ~ImagesLayerTest() { delete blob_top_data_; delete blob_top_label_; } + virtual ~ImageDataLayerTest() { + delete blob_top_data_; + delete blob_top_label_; + } char* filename; Blob* const blob_top_data_; @@ -51,15 +54,15 @@ class ImagesLayerTest : public ::testing::Test { }; typedef ::testing::Types Dtypes; -TYPED_TEST_CASE(ImagesLayerTest, Dtypes); +TYPED_TEST_CASE(ImageDataLayerTest, Dtypes); -TYPED_TEST(ImagesLayerTest, TestRead) { +TYPED_TEST(ImageDataLayerTest, TestRead) { LayerParameter param; ImageDataParameter* image_data_param = param.mutable_image_data_param(); image_data_param->set_batch_size(5); image_data_param->set_source(this->filename); image_data_param->set_shuffle(false); - ImagesLayer layer(param); + ImageDataLayer layer(param); layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); EXPECT_EQ(this->blob_top_data_->num(), 5); EXPECT_EQ(this->blob_top_data_->channels(), 3); @@ -78,7 +81,7 @@ TYPED_TEST(ImagesLayerTest, TestRead) { } } -TYPED_TEST(ImagesLayerTest, TestResize) { +TYPED_TEST(ImageDataLayerTest, TestResize) { LayerParameter param; ImageDataParameter* image_data_param = param.mutable_image_data_param(); image_data_param->set_batch_size(5); @@ -86,7 +89,7 @@ TYPED_TEST(ImagesLayerTest, TestResize) { image_data_param->set_new_height(256); image_data_param->set_new_width(256); image_data_param->set_shuffle(false); - ImagesLayer layer(param); + ImageDataLayer layer(param); layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); EXPECT_EQ(this->blob_top_data_->num(), 5); EXPECT_EQ(this->blob_top_data_->channels(), 3); @@ -105,13 +108,13 @@ TYPED_TEST(ImagesLayerTest, TestResize) { } } -TYPED_TEST(ImagesLayerTest, TestShuffle) { +TYPED_TEST(ImageDataLayerTest, TestShuffle) { LayerParameter param; ImageDataParameter* image_data_param = param.mutable_image_data_param(); image_data_param->set_batch_size(5); image_data_param->set_source(this->filename); image_data_param->set_shuffle(true); - ImagesLayer layer(param); + ImageDataLayer layer(param); layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); EXPECT_EQ(this->blob_top_data_->num(), 5); EXPECT_EQ(this->blob_top_data_->channels(), 3); From cdbb77c694e9b267df6f4059467390a8a8627fad Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 15:20:43 -0700 Subject: [PATCH 40/60] make test_protobuf use NONE for dummy layer instead of SPLIT --- src/caffe/test/test_protobuf.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/caffe/test/test_protobuf.cpp b/src/caffe/test/test_protobuf.cpp index 2db2fed429b..182af2e4611 100644 --- a/src/caffe/test/test_protobuf.cpp +++ b/src/caffe/test/test_protobuf.cpp @@ -16,7 +16,7 @@ class ProtoTest : public ::testing::Test {}; TEST_F(ProtoTest, TestSerialization) { LayerParameter param; param.set_name("test"); - param.set_type(LayerParameter_LayerType_SPLIT); + param.set_type(LayerParameter_LayerType_NONE); std::cout << "Printing in binary format." << std::endl; std::cout << param.SerializeAsString() << std::endl; std::cout << "Printing in text format." << std::endl; From 4667f0eec4083c5cb709b02aebb572e65e58fc27 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 15:37:43 -0700 Subject: [PATCH 41/60] update deprecated protos to latest dev versions --- src/caffe/proto/deprecated/caffe.proto.v0 | 39 ++++++++++++++++--- .../deprecated/caffe_v0_to_v1_bridge.proto | 39 ++++++++++++++++--- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/src/caffe/proto/deprecated/caffe.proto.v0 b/src/caffe/proto/deprecated/caffe.proto.v0 index d949a2652fa..f1ef1901bd2 100644 --- a/src/caffe/proto/deprecated/caffe.proto.v0 +++ b/src/caffe/proto/deprecated/caffe.proto.v0 @@ -80,8 +80,8 @@ message LayerParameter { // The blobs containing the numeric parameters of the layer repeated BlobProto blobs = 50; - // The ratio that is multiplied on the global learning rate. If you want to set - // the learning ratio for one blob, you need to set it for all blobs. + // The ratio that is multiplied on the global learning rate. If you want to + // set the learning ratio for one blob, you need to set it for all blobs. repeated float blobs_lr = 51; // The weight decay that is multiplied on the global weight decay. repeated float weight_decay = 52; @@ -92,9 +92,38 @@ message LayerParameter { // be larger than the number of keys in the leveldb. optional uint32 rand_skip = 53 [default = 0]; - // Concat Layer needs to specify the dimension along the concat will happen, - // the other dimensions must be the same for all the bottom blobs - // By default it will concatenate blobs along channels dimension + // Fields related to detection (det_*) + // foreground (object) overlap threshold + optional float det_fg_threshold = 54 [default = 0.5]; + // background (non-object) overlap threshold + optional float det_bg_threshold = 55 [default = 0.5]; + // Fraction of batch that should be foreground objects + optional float det_fg_fraction = 56 [default = 0.25]; + + // optional bool OBSOLETE_can_clobber = 57 [default = true]; + + // Amount of contextual padding to add around a window + // (used only by the window_data_layer) + optional uint32 det_context_pad = 58 [default = 0]; + + // Mode for cropping out a detection window + // warp: cropped window is warped to a fixed size and aspect ratio + // square: the tightest square around the window is cropped + optional string det_crop_mode = 59 [default = "warp"]; + + // For ReshapeLayer, one needs to specify the new dimensions. + optional int32 new_num = 60 [default = 0]; + optional int32 new_channels = 61 [default = 0]; + optional int32 new_height = 62 [default = 0]; + optional int32 new_width = 63 [default = 0]; + + // Whether or not ImageLayer should shuffle the list of files at every epoch. + // It will also resize images if new_height or new_width are not zero. + optional bool shuffle_images = 64 [default = false]; + + // For ConcatLayer, one needs to specify the dimension for concatenation, and + // the other dimensions must be the same for all the bottom blobs. + // By default it will concatenate blobs along the channels dimension. optional uint32 concat_dim = 65 [default = 1]; } diff --git a/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto b/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto index 782899ca06a..edf05cb48a4 100644 --- a/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto +++ b/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto @@ -61,8 +61,8 @@ message V0LayerParameter { // The blobs containing the numeric parameters of the layer repeated BlobProto blobs = 50; - // The ratio that is multiplied on the global learning rate. If you want to set - // the learning ratio for one blob, you need to set it for all blobs. + // The ratio that is multiplied on the global learning rate. If you want to + // set the learning ratio for one blob, you need to set it for all blobs. repeated float blobs_lr = 51; // The weight decay that is multiplied on the global weight decay. repeated float weight_decay = 52; @@ -73,9 +73,38 @@ message V0LayerParameter { // be larger than the number of keys in the leveldb. optional uint32 rand_skip = 53 [default = 0]; - // Concat Layer needs to specify the dimension along the concat will happen, - // the other dimensions must be the same for all the bottom blobs - // By default it will concatenate blobs along channels dimension + // Fields related to detection (det_*) + // foreground (object) overlap threshold + optional float det_fg_threshold = 54 [default = 0.5]; + // background (non-object) overlap threshold + optional float det_bg_threshold = 55 [default = 0.5]; + // Fraction of batch that should be foreground objects + optional float det_fg_fraction = 56 [default = 0.25]; + + // optional bool OBSOLETE_can_clobber = 57 [default = true]; + + // Amount of contextual padding to add around a window + // (used only by the window_data_layer) + optional uint32 det_context_pad = 58 [default = 0]; + + // Mode for cropping out a detection window + // warp: cropped window is warped to a fixed size and aspect ratio + // square: the tightest square around the window is cropped + optional string det_crop_mode = 59 [default = "warp"]; + + // For ReshapeLayer, one needs to specify the new dimensions. + optional int32 new_num = 60 [default = 0]; + optional int32 new_channels = 61 [default = 0]; + optional int32 new_height = 62 [default = 0]; + optional int32 new_width = 63 [default = 0]; + + // Whether or not ImageLayer should shuffle the list of files at every epoch. + // It will also resize images if new_height or new_width are not zero. + optional bool shuffle_images = 64 [default = false]; + + // For ConcatLayer, one needs to specify the dimension for concatenation, and + // the other dimensions must be the same for all the bottom blobs. + // By default it will concatenate blobs along the channels dimension. optional uint32 concat_dim = 65 [default = 1]; } From 335697f1cbe125592483b76676546a25abb57b1f Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 15:53:35 -0700 Subject: [PATCH 42/60] incorporate WindowDataLayer into V0Upgrade and add tests --- src/caffe/test/test_upgrade_proto.cpp | 57 +++++++++++++++++++++ src/caffe/util/upgrade_proto.cpp | 71 ++++++++++++++++++++++++++- 2 files changed, 127 insertions(+), 1 deletion(-) diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index 07c67bf6711..f8cb07cb266 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -1261,6 +1261,7 @@ TYPED_TEST(V0UpgradeTest, TestSimple) { this->RunV0UpgradeTest(input_proto, expected_output_proto); } +// Test any layer or parameter upgrades not covered by other tests. TYPED_TEST(V0UpgradeTest, TestAllParams) { const string& input_proto = "name: 'CaffeNet' " @@ -1286,6 +1287,24 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " layer { " + " name: 'window_data' " + " type: 'window_data' " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batchsize: 256 " + " cropsize: 227 " + " mirror: true " + " det_fg_threshold: 0.25 " + " det_bg_threshold: 0.75 " + " det_fg_fraction: 0.5 " + " det_context_pad: 16 " + " det_crop_mode: 'square' " + " } " + " top: 'window_data' " + " top: 'window_label' " + "} " + "layers { " + " layer { " " name: 'hdf5data' " " type: 'hdf5_data' " " source: '/my/hdf5/data' " @@ -1423,6 +1442,12 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " layer { " + " name: 'euclidean_loss' " + " type: 'euclidean_loss' " + " } " + "} " + "layers { " + " layer { " " name: 'flatten' " " type: 'flatten' " " } " @@ -1435,6 +1460,12 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " layer { " + " name: 'images' " + " type: 'images' " + " } " + "} " + "layers { " + " layer { " " name: 'multinomial_logistic_loss' " " type: 'multinomial_logistic_loss' " " } " @@ -1486,6 +1517,24 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { " top: 'label' " "} " "layers { " + " name: 'window_data' " + " type: WINDOW_DATA " + " window_data_param { " + " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " + " mean_file: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batch_size: 256 " + " crop_size: 227 " + " mirror: true " + " fg_threshold: 0.25 " + " bg_threshold: 0.75 " + " fg_fraction: 0.5 " + " context_pad: 16 " + " crop_mode: 'square' " + " } " + " top: 'window_data' " + " top: 'window_label' " + "} " + "layers { " " name: 'hdf5data' " " type: HDF5_DATA " " hdf5_data_param { " @@ -1617,6 +1666,10 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { " type: BNLL " "} " "layers { " + " name: 'euclidean_loss' " + " type: EUCLIDEAN_LOSS " + "} " + "layers { " " name: 'flatten' " " type: FLATTEN " "} " @@ -1625,6 +1678,10 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { " type: IM2COL " "} " "layers { " + " name: 'images' " + " type: IMAGE_DATA " + "} " + "layers { " " name: 'multinomial_logistic_loss' " " type: MULTINOMIAL_LOGISTIC_LOSS " "} " diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index ef24a60ca5e..cb9fd7a9433 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -274,6 +274,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, } else if (type == "hdf5_data") { layer_param->mutable_hdf5_data_param()->set_source( v0_layer_param.source()); + } else if (type == "window_data") { + layer_param->mutable_window_data_param()->set_source( + v0_layer_param.source()); } else if (type == "infogain_loss") { layer_param->mutable_infogain_loss_param()->set_source( v0_layer_param.source()); @@ -294,6 +297,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, if (type == "data") { layer_param->mutable_data_param()->set_mean_file( v0_layer_param.meanfile()); + } else if (type == "window_data") { + layer_param->mutable_window_data_param()->set_mean_file( + v0_layer_param.meanfile()); } else { LOG(ERROR) << "Unknown parameter meanfile for layer type " << type; is_fully_compatible = false; @@ -306,6 +312,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, } else if (type == "hdf5_data") { layer_param->mutable_hdf5_data_param()->set_batch_size( v0_layer_param.batchsize()); + } else if (type == "window_data") { + layer_param->mutable_window_data_param()->set_batch_size( + v0_layer_param.batchsize()); } else { LOG(ERROR) << "Unknown parameter batchsize for layer type " << type; is_fully_compatible = false; @@ -315,6 +324,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, if (type == "data") { layer_param->mutable_data_param()->set_crop_size( v0_layer_param.cropsize()); + } else if (type == "window_data") { + layer_param->mutable_window_data_param()->set_crop_size( + v0_layer_param.cropsize()); } else { LOG(ERROR) << "Unknown parameter cropsize for layer type " << type; is_fully_compatible = false; @@ -323,6 +335,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, if (v0_layer_param.has_mirror()) { if (type == "data") { layer_param->mutable_data_param()->set_mirror(v0_layer_param.mirror()); + } else if (type == "window_data") { + layer_param->mutable_window_data_param()->set_mirror( + v0_layer_param.mirror()); } else { LOG(ERROR) << "Unknown parameter mirror for layer type " << type; is_fully_compatible = false; @@ -355,6 +370,56 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, is_fully_compatible = false; } } + if (v0_layer_param.has_det_fg_threshold()) { + if (type == "window_data") { + layer_param->mutable_window_data_param()->set_fg_threshold( + v0_layer_param.det_fg_threshold()); + } else { + LOG(ERROR) << "Unknown parameter det_fg_threshold for layer type " + << type; + is_fully_compatible = false; + } + } + if (v0_layer_param.has_det_bg_threshold()) { + if (type == "window_data") { + layer_param->mutable_window_data_param()->set_bg_threshold( + v0_layer_param.det_bg_threshold()); + } else { + LOG(ERROR) << "Unknown parameter det_bg_threshold for layer type " + << type; + is_fully_compatible = false; + } + } + if (v0_layer_param.has_det_fg_fraction()) { + if (type == "window_data") { + layer_param->mutable_window_data_param()->set_fg_fraction( + v0_layer_param.det_fg_fraction()); + } else { + LOG(ERROR) << "Unknown parameter det_fg_fraction for layer type " + << type; + is_fully_compatible = false; + } + } + if (v0_layer_param.has_det_context_pad()) { + if (type == "window_data") { + layer_param->mutable_window_data_param()->set_context_pad( + v0_layer_param.det_context_pad()); + } else { + LOG(ERROR) << "Unknown parameter det_context_pad for layer type " + << type; + is_fully_compatible = false; + } + } + if (v0_layer_param.has_det_crop_mode()) { + if (type == "window_data") { + layer_param->mutable_window_data_param()->set_crop_mode( + v0_layer_param.det_crop_mode()); + } else { + LOG(ERROR) << "Unknown parameter det_crop_mode for layer type " + << type; + is_fully_compatible = false; + } + } } return is_fully_compatible; } @@ -380,6 +445,8 @@ LayerParameter_LayerType UpgradeV0LayerType(const string& type) { return LayerParameter_LayerType_HDF5_DATA; } else if (type == "im2col") { return LayerParameter_LayerType_IM2COL; + } else if (type == "images") { + return LayerParameter_LayerType_IMAGE_DATA; } else if (type == "infogain_loss") { return LayerParameter_LayerType_INFOGAIN_LOSS; } else if (type == "innerproduct") { @@ -402,9 +469,11 @@ LayerParameter_LayerType UpgradeV0LayerType(const string& type) { return LayerParameter_LayerType_SPLIT; } else if (type == "tanh") { return LayerParameter_LayerType_TANH; + } else if (type == "window_data") { + return LayerParameter_LayerType_WINDOW_DATA; } else { LOG(FATAL) << "Unknown layer name: " << type; - return LayerParameter_LayerType(-1); + return LayerParameter_LayerType_NONE; } } From 76e22cef47892d8b532463f80d8e9ae5f4e71a01 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 15:58:06 -0700 Subject: [PATCH 43/60] fix upgrade_net_proto name --- src/caffe/net.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index d7c922c1cbf..3483509c9ba 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -36,7 +36,7 @@ Net::Net(const string& param_file) { << "Failed to parse NetParameter file: " << param_file; LOG(ERROR) << "Parsed file as V0NetParameter: " << param_file; LOG(ERROR) << "Note that future Caffe releases will not support " - << "V0NetParameter; use tools/upgrade_net_proto.testbin to upgrade " + << "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade " << "this and any other network proto files to the new format."; if (!UpgradeV0Net(v0_param, ¶m)) { LOG(ERROR) << "Warning: had one or more problems upgrading " From 1b30e0ad48f1f837445cb230518b1a72a0870153 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 16:05:41 -0700 Subject: [PATCH 44/60] upgrade_net_proto: allow input files already in new proto format --- tools/upgrade_net_proto.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tools/upgrade_net_proto.cpp b/tools/upgrade_net_proto.cpp index a416878bfa7..49c27b52cc6 100644 --- a/tools/upgrade_net_proto.cpp +++ b/tools/upgrade_net_proto.cpp @@ -24,14 +24,20 @@ int main(int argc, char** argv) { return 0; } - V0NetParameter v0_net_param; - CHECK(ReadProtoFromTextFile(argv[1], &v0_net_param)) - << "Failed to parse input V0NetParameter file: " << argv[1]; + bool success = true; + // First, check whether the input file is already in the new format. NetParameter upgraded_net_param; - bool success = UpgradeV0Net(v0_net_param, &upgraded_net_param); - if (!success) { - LOG(ERROR) << "Encountered one or more problems upgrading net param " - << "proto; see above."; + if (ReadProtoFromTextFile(argv[1], &upgraded_net_param)) { + LOG(ERROR) << "File already in new proto format: " << argv[1]; + } else { + V0NetParameter v0_net_param; + CHECK(ReadProtoFromTextFile(argv[1], &v0_net_param)) + << "Failed to parse input V0NetParameter file: " << argv[1]; + success = UpgradeV0Net(v0_net_param, &upgraded_net_param); + if (!success) { + LOG(ERROR) << "Encountered one or more problems upgrading net param " + << "proto; see above."; + } } // TODO(jdonahue): figure out why WriteProtoToTextFile doesn't work // (no file is created). From fafa50564b6837a8b07938e9dd703f747b3f7277 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 21 Mar 2014 16:42:33 -0700 Subject: [PATCH 45/60] upgrade remaining prototxts --- examples/cifar10/cifar10_full.prototxt | 213 +++++------ examples/cifar10/cifar10_quick.prototxt | 210 +++++------ examples/cifar10/cifar10_quick_test.prototxt | 1 - .../feature_extraction/imagenet_val.prototxt | 239 ++++++------ .../pascal_finetune_train.prototxt | 350 ++++++++--------- .../pascal_finetune_val.prototxt | 356 ++++++++---------- 6 files changed, 607 insertions(+), 762 deletions(-) diff --git a/examples/cifar10/cifar10_full.prototxt b/examples/cifar10/cifar10_full.prototxt index 64fb2a8de85..237a7a0a0ed 100644 --- a/examples/cifar10/cifar10_full.prototxt +++ b/examples/cifar10/cifar10_full.prototxt @@ -6,148 +6,135 @@ input_dim: 1 input_dim: 3 input_dim: 32 input_dim: 32 -# ------------------------ layer 1 ----------------------------- layers { - layer { - name: "conv1" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "data" - top: "conv1" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + } } layers { - layer { - name: "pool1" - type: "pool" - kernelsize: 3 - stride: 2 - pool: MAX - } - bottom: "conv1" - top: "pool1" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { + pool: MAX + kernel_size: 3 + stride: 2 + } } layers { - layer { - name: "relu1" - type: "relu" - } - bottom: "pool1" - top: "pool1" + name: "relu1" + type: RELU + bottom: "pool1" + top: "pool1" } layers { - layer { - name: "norm1" - type: "lrn" + name: "norm1" + type: LRN + bottom: "pool1" + top: "norm1" + lrn_param { local_size: 3 - alpha: 0.00005 + alpha: 5e-05 beta: 0.75 } - bottom: "pool1" - top: "norm1" } -# --------------------------- layer 2 ------------------------ layers { - layer { - name: "conv2" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - blobs_lr: 1. - blobs_lr: 2. - } - bottom: "norm1" - top: "conv2" + name: "conv2" + type: CONVOLUTION + bottom: "norm1" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + } } layers { - layer { - name: "relu2" - type: "relu" - } - bottom: "conv2" - top: "conv2" + name: "relu2" + type: RELU + bottom: "conv2" + top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv2" - top: "pool2" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } layers { - layer { - name: "norm2" - type: "lrn" + name: "norm2" + type: LRN + bottom: "pool2" + top: "norm2" + lrn_param { local_size: 3 - alpha: 0.00005 + alpha: 5e-05 beta: 0.75 } - bottom: "pool2" - top: "norm2" } -#-----------------------layer 3------------------------- layers { - layer { - name: "conv3" - type: "conv" - num_output: 64 - kernelsize: 5 - pad: 2 - stride: 1 - } - bottom: "norm2" - top: "conv3" + name: "conv3" + type: CONVOLUTION + bottom: "norm2" + top: "conv3" + convolution_param { + num_output: 64 + pad: 2 + kernel_size: 5 + stride: 1 + } } layers { - layer { - name: "relu3" - type: "relu" - } - bottom: "conv3" - top: "conv3" + name: "relu3" + type: RELU + bottom: "conv3" + top: "conv3" } layers { - layer { - name: "pool3" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv3" - top: "pool3" + name: "pool3" + type: POOLING + bottom: "conv3" + top: "pool3" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } -#--------------------------layer 4------------------------ layers { - layer { - name: "ip1" - type: "innerproduct" - num_output: 10 - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 250. - weight_decay: 0. - } - bottom: "pool3" - top: "ip1" + name: "ip1" + type: INNER_PRODUCT + bottom: "pool3" + top: "ip1" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 250 + weight_decay: 0 + inner_product_param { + num_output: 10 + } } -#-----------------------output------------------------ layers { - layer { - name: "prob" - type: "softmax" - } - bottom: "ip1" - top: "prob" + name: "prob" + type: SOFTMAX + bottom: "ip1" + top: "prob" } diff --git a/examples/cifar10/cifar10_quick.prototxt b/examples/cifar10/cifar10_quick.prototxt index 6161caa10e8..505158f7a34 100644 --- a/examples/cifar10/cifar10_quick.prototxt +++ b/examples/cifar10/cifar10_quick.prototxt @@ -1,143 +1,127 @@ name: "CIFAR10_quick_test" -# N.B. input image must be in CIFAR-10 format -# as described at http://www.cs.toronto.edu/~kriz/cifar.html input: "data" input_dim: 1 input_dim: 3 input_dim: 32 input_dim: 32 -# ------------------------ layer 1 ----------------------------- layers { - layer { - name: "conv1" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "data" - top: "conv1" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + } } layers { - layer { - name: "pool1" - type: "pool" - kernelsize: 3 - stride: 2 - pool: MAX - } - bottom: "conv1" - top: "pool1" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { + pool: MAX + kernel_size: 3 + stride: 2 + } } layers { - layer { - name: "relu1" - type: "relu" - } - bottom: "pool1" - top: "pool1" + name: "relu1" + type: RELU + bottom: "pool1" + top: "pool1" } -# --------------------------- layer 2 ------------------------ layers { - layer { - name: "conv2" - type: "conv" - num_output: 32 - kernelsize: 5 - pad: 2 - stride: 1 - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "pool1" - top: "conv2" + name: "conv2" + type: CONVOLUTION + bottom: "pool1" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 32 + pad: 2 + kernel_size: 5 + stride: 1 + } } layers { - layer { - name: "relu2" - type: "relu" - } - bottom: "conv2" - top: "conv2" + name: "relu2" + type: RELU + bottom: "conv2" + top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv2" - top: "pool2" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } -#-----------------------layer 3------------------------- layers { - layer { - name: "conv3" - type: "conv" - num_output: 64 - kernelsize: 5 - pad: 2 - stride: 1 - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "pool2" - top: "conv3" + name: "conv3" + type: CONVOLUTION + bottom: "pool2" + top: "conv3" + blobs_lr: 1 + blobs_lr: 2 + convolution_param { + num_output: 64 + pad: 2 + kernel_size: 5 + stride: 1 + } } layers { - layer { - name: "relu3" - type: "relu" - } - bottom: "conv3" - top: "conv3" + name: "relu3" + type: RELU + bottom: "conv3" + top: "conv3" } layers { - layer { - name: "pool3" - type: "pool" - kernelsize: 3 - stride: 2 - pool: AVE - } - bottom: "conv3" - top: "pool3" + name: "pool3" + type: POOLING + bottom: "conv3" + top: "pool3" + pooling_param { + pool: AVE + kernel_size: 3 + stride: 2 + } } -#--------------------------layer 4------------------------ layers { - layer { - name: "ip1" - type: "innerproduct" - num_output: 64 - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "pool3" - top: "ip1" + name: "ip1" + type: INNER_PRODUCT + bottom: "pool3" + top: "ip1" + blobs_lr: 1 + blobs_lr: 2 + inner_product_param { + num_output: 64 + } } -#--------------------------layer 5------------------------ layers { - layer { - name: "ip2" - type: "innerproduct" - num_output: 10 - blobs_lr: 1.0 - blobs_lr: 2.0 - } - bottom: "ip1" - top: "ip2" + name: "ip2" + type: INNER_PRODUCT + bottom: "ip1" + top: "ip2" + blobs_lr: 1 + blobs_lr: 2 + inner_product_param { + num_output: 10 + } } -#-----------------------output------------------------ layers { - layer { - name: "prob" - type: "softmax" - } - bottom: "ip2" - top: "prob" + name: "prob" + type: SOFTMAX + bottom: "ip2" + top: "prob" } diff --git a/examples/cifar10/cifar10_quick_test.prototxt b/examples/cifar10/cifar10_quick_test.prototxt index f03cffc253e..a154b9a0ea7 100644 --- a/examples/cifar10/cifar10_quick_test.prototxt +++ b/examples/cifar10/cifar10_quick_test.prototxt @@ -1,4 +1,3 @@ -# quick config name: "CIFAR10_quick_test" layers { name: "cifar" diff --git a/examples/feature_extraction/imagenet_val.prototxt b/examples/feature_extraction/imagenet_val.prototxt index c7b26509125..a3626651b5e 100644 --- a/examples/feature_extraction/imagenet_val.prototxt +++ b/examples/feature_extraction/imagenet_val.prototxt @@ -1,246 +1,219 @@ name: "CaffeNet" layers { - layer { - name: "data" - type: "images" - source: "$CAFFE_DIR/examples/_temp/file_list.txt" - meanfile: "$CAFFE_DIR/data/ilsvrc12/imagenet_mean.binaryproto" - batchsize: 50 - new_height: 256 - new_width: 256 - mirror: false - cropsize: 227 - } + name: "data" + type: IMAGE_DATA top: "data" top: "label" } layers { - layer { - name: "conv1" - type: "conv" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + convolution_param { num_output: 96 - kernelsize: 11 + kernel_size: 11 stride: 4 } - bottom: "data" - top: "conv1" } layers { - layer { - name: "relu1" - type: "relu" - } + name: "relu1" + type: RELU bottom: "conv1" top: "conv1" } layers { - layer { - name: "pool1" - type: "pool" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv1" - top: "pool1" } layers { - layer { - name: "norm1" - type: "lrn" + name: "norm1" + type: LRN + bottom: "pool1" + top: "norm1" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool1" - top: "norm1" } layers { - layer { - name: "conv2" - type: "conv" + name: "conv2" + type: CONVOLUTION + bottom: "norm1" + top: "conv2" + convolution_param { num_output: 256 - group: 2 - kernelsize: 5 pad: 2 + kernel_size: 5 + group: 2 } - bottom: "norm1" - top: "conv2" } layers { - layer { - name: "relu2" - type: "relu" - } + name: "relu2" + type: RELU bottom: "conv2" top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv2" - top: "pool2" } layers { - layer { - name: "norm2" - type: "lrn" + name: "norm2" + type: LRN + bottom: "pool2" + top: "norm2" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool2" - top: "norm2" } layers { - layer { - name: "conv3" - type: "conv" + name: "conv3" + type: CONVOLUTION + bottom: "norm2" + top: "conv3" + convolution_param { num_output: 384 - kernelsize: 3 pad: 1 + kernel_size: 3 } - bottom: "norm2" - top: "conv3" } layers { - layer { - name: "relu3" - type: "relu" - } + name: "relu3" + type: RELU bottom: "conv3" top: "conv3" } layers { - layer { - name: "conv4" - type: "conv" + name: "conv4" + type: CONVOLUTION + bottom: "conv3" + top: "conv4" + convolution_param { num_output: 384 - group: 2 - kernelsize: 3 pad: 1 + kernel_size: 3 + group: 2 } - bottom: "conv3" - top: "conv4" } layers { - layer { - name: "relu4" - type: "relu" - } + name: "relu4" + type: RELU bottom: "conv4" top: "conv4" } layers { - layer { - name: "conv5" - type: "conv" + name: "conv5" + type: CONVOLUTION + bottom: "conv4" + top: "conv5" + convolution_param { num_output: 256 - group: 2 - kernelsize: 3 pad: 1 + kernel_size: 3 + group: 2 } - bottom: "conv4" - top: "conv5" } layers { - layer { - name: "relu5" - type: "relu" - } + name: "relu5" + type: RELU bottom: "conv5" top: "conv5" } layers { - layer { - name: "pool5" - type: "pool" - kernelsize: 3 + name: "pool5" + type: POOLING + bottom: "conv5" + top: "pool5" + pooling_param { pool: MAX + kernel_size: 3 stride: 2 } - bottom: "conv5" - top: "pool5" } layers { - layer { - name: "fc6" - type: "innerproduct" - num_output: 4096 - } + name: "fc6" + type: INNER_PRODUCT bottom: "pool5" top: "fc6" + inner_product_param { + num_output: 4096 + } } layers { - layer { - name: "relu6" - type: "relu" - } + name: "relu6" + type: RELU bottom: "fc6" top: "fc6" } layers { - layer { - name: "drop6" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop6" + type: DROPOUT bottom: "fc6" top: "fc6" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc7" - type: "innerproduct" - num_output: 4096 - } + name: "fc7" + type: INNER_PRODUCT bottom: "fc6" top: "fc7" + inner_product_param { + num_output: 4096 + } } layers { - layer { - name: "relu7" - type: "relu" - } + name: "relu7" + type: RELU bottom: "fc7" top: "fc7" } layers { - layer { - name: "drop7" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop7" + type: DROPOUT bottom: "fc7" top: "fc7" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc8" - type: "innerproduct" - num_output: 1000 - } + name: "fc8" + type: INNER_PRODUCT bottom: "fc7" top: "fc8" + inner_product_param { + num_output: 1000 + } } layers { - layer { - name: "prob" - type: "softmax" - } + name: "prob" + type: SOFTMAX bottom: "fc8" top: "prob" } layers { - layer { - name: "accuracy" - type: "accuracy" - } + name: "accuracy" + type: ACCURACY bottom: "prob" bottom: "label" top: "accuracy" diff --git a/examples/pascal-finetuning/pascal_finetune_train.prototxt b/examples/pascal-finetuning/pascal_finetune_train.prototxt index ac847813454..dfc60fe4b8a 100644 --- a/examples/pascal-finetuning/pascal_finetune_train.prototxt +++ b/examples/pascal-finetuning/pascal_finetune_train.prototxt @@ -1,28 +1,34 @@ name: "CaffeNet" layers { - layer { - name: "data" - type: "window_data" + name: "data" + type: WINDOW_DATA + top: "data" + top: "label" + window_data_param { source: "window_file_2007_trainval.txt" - meanfile: "../../data/ilsvrc12/imagenet_mean.binaryproto" - batchsize: 128 - cropsize: 227 + mean_file: "../../data/ilsvrc12/imagenet_mean.binaryproto" + batch_size: 128 + crop_size: 227 mirror: true - det_context_pad: 16 - det_crop_mode: "warp" - det_fg_threshold: 0.5 - det_bg_threshold: 0.5 - det_fg_fraction: 0.25 + fg_threshold: 0.5 + bg_threshold: 0.5 + fg_fraction: 0.25 + context_pad: 16 + crop_mode: "warp" } - top: "data" - top: "label" } layers { - layer { - name: "conv1" - type: "conv" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 96 - kernelsize: 11 + kernel_size: 11 stride: 4 weight_filler { type: "gaussian" @@ -30,242 +36,200 @@ layers { } bias_filler { type: "constant" - value: 0. + value: 0 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "data" - top: "conv1" } layers { - layer { - name: "relu1" - type: "relu" - } + name: "relu1" + type: RELU bottom: "conv1" top: "conv1" } layers { - layer { - name: "pool1" - type: "pool" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv1" - top: "pool1" } layers { - layer { - name: "norm1" - type: "lrn" + name: "norm1" + type: LRN + bottom: "pool1" + top: "norm1" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool1" - top: "norm1" } layers { - layer { - name: "pad2" - type: "padding" - pad: 2 - } + name: "conv2" + type: CONVOLUTION bottom: "norm1" - top: "pad2" -} -layers { - layer { - name: "conv2" - type: "conv" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 256 + pad: 2 + kernel_size: 5 group: 2 - kernelsize: 5 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pad2" - top: "conv2" } layers { - layer { - name: "relu2" - type: "relu" - } + name: "relu2" + type: RELU bottom: "conv2" top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv2" - top: "pool2" } layers { - layer { - name: "norm2" - type: "lrn" + name: "norm2" + type: LRN + bottom: "pool2" + top: "norm2" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool2" - top: "norm2" } layers { - layer { - name: "pad3" - type: "padding" - pad: 1 - } + name: "conv3" + type: CONVOLUTION bottom: "norm2" - top: "pad3" -} -layers { - layer { - name: "conv3" - type: "conv" + top: "conv3" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 384 - kernelsize: 3 + pad: 1 + kernel_size: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 0. + value: 0 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pad3" - top: "conv3" } layers { - layer { - name: "relu3" - type: "relu" - } + name: "relu3" + type: RELU bottom: "conv3" top: "conv3" } layers { - layer { - name: "pad4" - type: "padding" - pad: 1 - } + name: "conv4" + type: CONVOLUTION bottom: "conv3" - top: "pad4" -} -layers { - layer { - name: "conv4" - type: "conv" + top: "conv4" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 384 + pad: 1 + kernel_size: 3 group: 2 - kernelsize: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pad4" - top: "conv4" } layers { - layer { - name: "relu4" - type: "relu" - } + name: "relu4" + type: RELU bottom: "conv4" top: "conv4" } layers { - layer { - name: "pad5" - type: "padding" - pad: 1 - } + name: "conv5" + type: CONVOLUTION bottom: "conv4" - top: "pad5" -} -layers { - layer { - name: "conv5" - type: "conv" + top: "conv5" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 256 + pad: 1 + kernel_size: 3 group: 2 - kernelsize: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pad5" - top: "conv5" } layers { - layer { - name: "relu5" - type: "relu" - } + name: "relu5" + type: RELU bottom: "conv5" top: "conv5" } layers { - layer { - name: "pool5" - type: "pool" - kernelsize: 3 + name: "pool5" + type: POOLING + bottom: "conv5" + top: "pool5" + pooling_param { pool: MAX + kernel_size: 3 stride: 2 } - bottom: "conv5" - top: "pool5" } layers { - layer { - name: "fc6" - type: "innerproduct" + name: "fc6" + type: INNER_PRODUCT + bottom: "pool5" + top: "fc6" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 4096 weight_filler { type: "gaussian" @@ -273,37 +237,35 @@ layers { } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pool5" - top: "fc6" } layers { - layer { - name: "relu6" - type: "relu" - } + name: "relu6" + type: RELU bottom: "fc6" top: "fc6" } layers { - layer { - name: "drop6" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop6" + type: DROPOUT bottom: "fc6" top: "fc6" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc7" - type: "innerproduct" + name: "fc7" + type: INNER_PRODUCT + bottom: "fc6" + top: "fc7" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 4096 weight_filler { type: "gaussian" @@ -311,37 +273,35 @@ layers { } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "fc6" - top: "fc7" } layers { - layer { - name: "relu7" - type: "relu" - } + name: "relu7" + type: RELU bottom: "fc7" top: "fc7" } layers { - layer { - name: "drop7" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop7" + type: DROPOUT bottom: "fc7" top: "fc7" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc8_pascal" - type: "innerproduct" + name: "fc8_pascal" + type: INNER_PRODUCT + bottom: "fc7" + top: "fc8_pascal" + blobs_lr: 10 + blobs_lr: 20 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 21 weight_filler { type: "gaussian" @@ -351,19 +311,11 @@ layers { type: "constant" value: 0 } - blobs_lr: 10. - blobs_lr: 20. - weight_decay: 1. - weight_decay: 0. } - bottom: "fc7" - top: "fc8_pascal" } layers { - layer { - name: "loss" - type: "softmax_loss" - } + name: "loss" + type: SOFTMAX_LOSS bottom: "fc8_pascal" bottom: "label" } diff --git a/examples/pascal-finetuning/pascal_finetune_val.prototxt b/examples/pascal-finetuning/pascal_finetune_val.prototxt index a11033ad1e2..ff898fe7376 100644 --- a/examples/pascal-finetuning/pascal_finetune_val.prototxt +++ b/examples/pascal-finetuning/pascal_finetune_val.prototxt @@ -1,28 +1,34 @@ name: "CaffeNet" layers { - layer { - name: "data" - type: "window_data" + name: "data" + type: WINDOW_DATA + top: "data" + top: "label" + window_data_param { source: "window_file_2007_test.txt" - meanfile: "../../data/ilsvrc12/imagenet_mean.binaryproto" - batchsize: 128 - cropsize: 227 + mean_file: "../../data/ilsvrc12/imagenet_mean.binaryproto" + batch_size: 128 + crop_size: 227 mirror: true - det_context_pad: 16 - det_crop_mode: "warp" - det_fg_threshold: 0.5 - det_bg_threshold: 0.5 - det_fg_fraction: 0.25 + fg_threshold: 0.5 + bg_threshold: 0.5 + fg_fraction: 0.25 + context_pad: 16 + crop_mode: "warp" } - top: "data" - top: "label" } layers { - layer { - name: "conv1" - type: "conv" + name: "conv1" + type: CONVOLUTION + bottom: "data" + top: "conv1" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 96 - kernelsize: 11 + kernel_size: 11 stride: 4 weight_filler { type: "gaussian" @@ -30,242 +36,200 @@ layers { } bias_filler { type: "constant" - value: 0. + value: 0 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "data" - top: "conv1" } layers { - layer { - name: "relu1" - type: "relu" - } + name: "relu1" + type: RELU bottom: "conv1" top: "conv1" } layers { - layer { - name: "pool1" - type: "pool" + name: "pool1" + type: POOLING + bottom: "conv1" + top: "pool1" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv1" - top: "pool1" } layers { - layer { - name: "norm1" - type: "lrn" + name: "norm1" + type: LRN + bottom: "pool1" + top: "norm1" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool1" - top: "norm1" } layers { - layer { - name: "pad2" - type: "padding" - pad: 2 - } + name: "conv2" + type: CONVOLUTION bottom: "norm1" - top: "pad2" -} -layers { - layer { - name: "conv2" - type: "conv" + top: "conv2" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 256 + pad: 2 + kernel_size: 5 group: 2 - kernelsize: 5 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pad2" - top: "conv2" } layers { - layer { - name: "relu2" - type: "relu" - } + name: "relu2" + type: RELU bottom: "conv2" top: "conv2" } layers { - layer { - name: "pool2" - type: "pool" + name: "pool2" + type: POOLING + bottom: "conv2" + top: "pool2" + pooling_param { pool: MAX - kernelsize: 3 + kernel_size: 3 stride: 2 } - bottom: "conv2" - top: "pool2" } layers { - layer { - name: "norm2" - type: "lrn" + name: "norm2" + type: LRN + bottom: "pool2" + top: "norm2" + lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } - bottom: "pool2" - top: "norm2" } layers { - layer { - name: "pad3" - type: "padding" - pad: 1 - } + name: "conv3" + type: CONVOLUTION bottom: "norm2" - top: "pad3" -} -layers { - layer { - name: "conv3" - type: "conv" + top: "conv3" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 384 - kernelsize: 3 + pad: 1 + kernel_size: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 0. + value: 0 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pad3" - top: "conv3" } layers { - layer { - name: "relu3" - type: "relu" - } + name: "relu3" + type: RELU bottom: "conv3" top: "conv3" } layers { - layer { - name: "pad4" - type: "padding" - pad: 1 - } + name: "conv4" + type: CONVOLUTION bottom: "conv3" - top: "pad4" -} -layers { - layer { - name: "conv4" - type: "conv" + top: "conv4" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 384 + pad: 1 + kernel_size: 3 group: 2 - kernelsize: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pad4" - top: "conv4" } layers { - layer { - name: "relu4" - type: "relu" - } + name: "relu4" + type: RELU bottom: "conv4" top: "conv4" } layers { - layer { - name: "pad5" - type: "padding" - pad: 1 - } + name: "conv5" + type: CONVOLUTION bottom: "conv4" - top: "pad5" -} -layers { - layer { - name: "conv5" - type: "conv" + top: "conv5" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + convolution_param { num_output: 256 + pad: 1 + kernel_size: 3 group: 2 - kernelsize: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pad5" - top: "conv5" } layers { - layer { - name: "relu5" - type: "relu" - } + name: "relu5" + type: RELU bottom: "conv5" top: "conv5" } layers { - layer { - name: "pool5" - type: "pool" - kernelsize: 3 + name: "pool5" + type: POOLING + bottom: "conv5" + top: "pool5" + pooling_param { pool: MAX + kernel_size: 3 stride: 2 } - bottom: "conv5" - top: "pool5" } layers { - layer { - name: "fc6" - type: "innerproduct" + name: "fc6" + type: INNER_PRODUCT + bottom: "pool5" + top: "fc6" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 4096 weight_filler { type: "gaussian" @@ -273,37 +237,35 @@ layers { } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "pool5" - top: "fc6" } layers { - layer { - name: "relu6" - type: "relu" - } + name: "relu6" + type: RELU bottom: "fc6" top: "fc6" } layers { - layer { - name: "drop6" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop6" + type: DROPOUT bottom: "fc6" top: "fc6" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc7" - type: "innerproduct" + name: "fc7" + type: INNER_PRODUCT + bottom: "fc6" + top: "fc7" + blobs_lr: 1 + blobs_lr: 2 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 4096 weight_filler { type: "gaussian" @@ -311,37 +273,35 @@ layers { } bias_filler { type: "constant" - value: 1. + value: 1 } - blobs_lr: 1. - blobs_lr: 2. - weight_decay: 1. - weight_decay: 0. } - bottom: "fc6" - top: "fc7" } layers { - layer { - name: "relu7" - type: "relu" - } + name: "relu7" + type: RELU bottom: "fc7" top: "fc7" } layers { - layer { - name: "drop7" - type: "dropout" - dropout_ratio: 0.5 - } + name: "drop7" + type: DROPOUT bottom: "fc7" top: "fc7" + dropout_param { + dropout_ratio: 0.5 + } } layers { - layer { - name: "fc8_pascal" - type: "innerproduct" + name: "fc8_pascal" + type: INNER_PRODUCT + bottom: "fc7" + top: "fc8_pascal" + blobs_lr: 10 + blobs_lr: 20 + weight_decay: 1 + weight_decay: 0 + inner_product_param { num_output: 21 weight_filler { type: "gaussian" @@ -351,27 +311,17 @@ layers { type: "constant" value: 0 } - blobs_lr: 10. - blobs_lr: 20. - weight_decay: 1. - weight_decay: 0. } - bottom: "fc7" - top: "fc8_pascal" } layers { - layer { - name: "prob" - type: "softmax" - } + name: "prob" + type: SOFTMAX bottom: "fc8_pascal" top: "prob" } layers { - layer { - name: "accuracy" - type: "accuracy" - } + name: "accuracy" + type: ACCURACY bottom: "prob" bottom: "label" top: "accuracy" From c1fa11d11e30124e524cdbdfc2a954d53939801d Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sun, 23 Mar 2014 14:14:17 -0700 Subject: [PATCH 46/60] upgrade images layer --- src/caffe/test/test_upgrade_proto.cpp | 36 +++++++++++++++ src/caffe/util/upgrade_proto.cpp | 66 +++++++++++++++++++++++---- 2 files changed, 93 insertions(+), 9 deletions(-) diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index f8cb07cb266..82034065d40 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -1287,6 +1287,24 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " layer { " + " name: 'images' " + " type: 'images' " + " source: '/home/jiayq/Data/ILSVRC12/train-images' " + " meanfile: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batchsize: 256 " + " cropsize: 227 " + " mirror: true " + " scale: 0.25 " + " rand_skip: 73 " + " shuffle_images: true " + " new_height: 40 " + " new_width: 30 " + " } " + " top: 'images_data' " + " top: 'images_label' " + "} " + "layers { " + " layer { " " name: 'window_data' " " type: 'window_data' " " source: '/home/jiayq/Data/ILSVRC12/train-leveldb' " @@ -1517,6 +1535,24 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { " top: 'label' " "} " "layers { " + " name: 'images' " + " type: IMAGE_DATA " + " image_data_param { " + " source: '/home/jiayq/Data/ILSVRC12/train-images' " + " mean_file: '/home/jiayq/Data/ILSVRC12/image_mean.binaryproto' " + " batch_size: 256 " + " crop_size: 227 " + " mirror: true " + " scale: 0.25 " + " rand_skip: 73 " + " shuffle: true " + " new_height: 40 " + " new_width: 30 " + " } " + " top: 'images_data' " + " top: 'images_label' " + "} " + "layers { " " name: 'window_data' " " type: WINDOW_DATA " " window_data_param { " diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index cb9fd7a9433..48813d83f53 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -120,6 +120,15 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, if (v0_layer_param.has_type()) { layer_param->set_type(UpgradeV0LayerType(type)); } + for (int i = 0; i < v0_layer_param.blobs_size(); ++i) { + layer_param->add_blobs()->CopyFrom(v0_layer_param.blobs(i)); + } + for (int i = 0; i < v0_layer_param.blobs_lr_size(); ++i) { + layer_param->add_blobs_lr(v0_layer_param.blobs_lr(i)); + } + for (int i = 0; i < v0_layer_param.weight_decay_size(); ++i) { + layer_param->add_weight_decay(v0_layer_param.weight_decay(i)); + } if (v0_layer_param.has_num_output()) { if (type == "conv") { layer_param->mutable_convolution_param()->set_num_output( @@ -274,6 +283,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, } else if (type == "hdf5_data") { layer_param->mutable_hdf5_data_param()->set_source( v0_layer_param.source()); + } else if (type == "images") { + layer_param->mutable_image_data_param()->set_source( + v0_layer_param.source()); } else if (type == "window_data") { layer_param->mutable_window_data_param()->set_source( v0_layer_param.source()); @@ -288,6 +300,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, if (v0_layer_param.has_scale()) { if (type == "data") { layer_param->mutable_data_param()->set_scale(v0_layer_param.scale()); + } else if (type == "images") { + layer_param->mutable_image_data_param()->set_scale( + v0_layer_param.scale()); } else { LOG(ERROR) << "Unknown parameter scale for layer type " << type; is_fully_compatible = false; @@ -297,6 +312,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, if (type == "data") { layer_param->mutable_data_param()->set_mean_file( v0_layer_param.meanfile()); + } else if (type == "images") { + layer_param->mutable_image_data_param()->set_mean_file( + v0_layer_param.meanfile()); } else if (type == "window_data") { layer_param->mutable_window_data_param()->set_mean_file( v0_layer_param.meanfile()); @@ -312,6 +330,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, } else if (type == "hdf5_data") { layer_param->mutable_hdf5_data_param()->set_batch_size( v0_layer_param.batchsize()); + } else if (type == "images") { + layer_param->mutable_image_data_param()->set_batch_size( + v0_layer_param.batchsize()); } else if (type == "window_data") { layer_param->mutable_window_data_param()->set_batch_size( v0_layer_param.batchsize()); @@ -324,6 +345,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, if (type == "data") { layer_param->mutable_data_param()->set_crop_size( v0_layer_param.cropsize()); + } else if (type == "images") { + layer_param->mutable_image_data_param()->set_crop_size( + v0_layer_param.cropsize()); } else if (type == "window_data") { layer_param->mutable_window_data_param()->set_crop_size( v0_layer_param.cropsize()); @@ -335,6 +359,9 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, if (v0_layer_param.has_mirror()) { if (type == "data") { layer_param->mutable_data_param()->set_mirror(v0_layer_param.mirror()); + } else if (type == "images") { + layer_param->mutable_image_data_param()->set_mirror( + v0_layer_param.mirror()); } else if (type == "window_data") { layer_param->mutable_window_data_param()->set_mirror( v0_layer_param.mirror()); @@ -343,24 +370,45 @@ bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, is_fully_compatible = false; } } - for (int i = 0; i < v0_layer_param.blobs_size(); ++i) { - layer_param->add_blobs()->CopyFrom(v0_layer_param.blobs(i)); - } - for (int i = 0; i < v0_layer_param.blobs_lr_size(); ++i) { - layer_param->add_blobs_lr(v0_layer_param.blobs_lr(i)); - } - for (int i = 0; i < v0_layer_param.weight_decay_size(); ++i) { - layer_param->add_weight_decay(v0_layer_param.weight_decay(i)); - } if (v0_layer_param.has_rand_skip()) { if (type == "data") { layer_param->mutable_data_param()->set_rand_skip( v0_layer_param.rand_skip()); + } else if (type == "images") { + layer_param->mutable_image_data_param()->set_rand_skip( + v0_layer_param.rand_skip()); } else { LOG(ERROR) << "Unknown parameter rand_skip for layer type " << type; is_fully_compatible = false; } } + if (v0_layer_param.has_shuffle_images()) { + if (type == "images") { + layer_param->mutable_image_data_param()->set_shuffle( + v0_layer_param.shuffle_images()); + } else { + LOG(ERROR) << "Unknown parameter shuffle for layer type " << type; + is_fully_compatible = false; + } + } + if (v0_layer_param.has_new_height()) { + if (type == "images") { + layer_param->mutable_image_data_param()->set_new_height( + v0_layer_param.new_height()); + } else { + LOG(ERROR) << "Unknown parameter new_height for layer type " << type; + is_fully_compatible = false; + } + } + if (v0_layer_param.has_new_width()) { + if (type == "images") { + layer_param->mutable_image_data_param()->set_new_width( + v0_layer_param.new_width()); + } else { + LOG(ERROR) << "Unknown parameter new_width for layer type " << type; + is_fully_compatible = false; + } + } if (v0_layer_param.has_concat_dim()) { if (type == "concat") { layer_param->mutable_concat_param()->set_concat_dim( From 17a59c30485d3a3832533bf606a32fb827fb7ea9 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sun, 23 Mar 2014 14:26:31 -0700 Subject: [PATCH 47/60] make all tools backwards compatible with v0 net param --- include/caffe/net.hpp | 3 ++ include/caffe/util/io.hpp | 19 ++++++--- src/caffe/layers/data_layer.cpp | 7 ++-- src/caffe/layers/window_data_layer.cpp | 2 +- src/caffe/net.cpp | 58 +++++++++++++++++++------- src/caffe/util/io.cpp | 5 ++- tools/dump_network.cpp | 13 ++---- tools/extract_features.cpp | 11 +---- tools/net_speed_benchmark.cpp | 5 +-- tools/test_net.cpp | 4 +- 10 files changed, 74 insertions(+), 53 deletions(-) diff --git a/include/caffe/net.hpp b/include/caffe/net.hpp index b97c1cc0f32..7fe2c5b1634 100644 --- a/include/caffe/net.hpp +++ b/include/caffe/net.hpp @@ -58,6 +58,9 @@ class Net { // trained layers from another net parameter instance. void CopyTrainedLayersFrom(const NetParameter& param); void CopyTrainedLayersFrom(const string trained_filename); + // Read parameters from a file into a NetParameter proto message. + void ReadParamsFromTextFile(const string& param_file, NetParameter* param); + void ReadParamsFromBinaryFile(const string& param_file, NetParameter* param); // Writes the net to a proto. void ToProto(NetParameter* param, bool write_diff = false); diff --git a/include/caffe/util/io.hpp b/include/caffe/util/io.hpp index 11201c13d97..056b573db4c 100644 --- a/include/caffe/util/io.hpp +++ b/include/caffe/util/io.hpp @@ -38,13 +38,22 @@ inline void WriteProtoToTextFile(const Message& proto, const string& filename) { WriteProtoToTextFile(proto, filename.c_str()); } -void ReadProtoFromBinaryFile(const char* filename, - Message* proto); -inline void ReadProtoFromBinaryFile(const string& filename, - Message* proto) { - ReadProtoFromBinaryFile(filename.c_str(), proto); +bool ReadProtoFromBinaryFile(const char* filename, Message* proto); + +inline bool ReadProtoFromBinaryFile(const string& filename, Message* proto) { + return ReadProtoFromBinaryFile(filename.c_str(), proto); +} + +inline void ReadProtoFromBinaryFileOrDie(const char* filename, Message* proto) { + CHECK(ReadProtoFromBinaryFile(filename, proto)); } +inline void ReadProtoFromBinaryFileOrDie(const string& filename, + Message* proto) { + ReadProtoFromBinaryFileOrDie(filename.c_str(), proto); +} + + void WriteProtoToBinaryFile(const Message& proto, const char* filename); inline void WriteProtoToBinaryFile( const Message& proto, const string& filename) { diff --git a/src/caffe/layers/data_layer.cpp b/src/caffe/layers/data_layer.cpp index 237ce3779d3..399f771fd68 100644 --- a/src/caffe/layers/data_layer.cpp +++ b/src/caffe/layers/data_layer.cpp @@ -190,11 +190,10 @@ void DataLayer::SetUp(const vector*>& bottom, CHECK_GT(datum_width_, crop_size); // check if we want to have mean if (this->layer_param_.data_param().has_mean_file()) { + const string& mean_file = this->layer_param_.data_param().mean_file(); + LOG(INFO) << "Loading mean file from" << mean_file; BlobProto blob_proto; - LOG(INFO) << "Loading mean file from" - << this->layer_param_.data_param().mean_file(); - ReadProtoFromBinaryFile(this->layer_param_.data_param().mean_file().c_str(), - &blob_proto); + ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto); data_mean_.FromProto(blob_proto); CHECK_EQ(data_mean_.num(), 1); CHECK_EQ(data_mean_.channels(), datum_channels_); diff --git a/src/caffe/layers/window_data_layer.cpp b/src/caffe/layers/window_data_layer.cpp index 6f6017566bb..7c3e24b3b9c 100644 --- a/src/caffe/layers/window_data_layer.cpp +++ b/src/caffe/layers/window_data_layer.cpp @@ -388,7 +388,7 @@ void WindowDataLayer::SetUp(const vector*>& bottom, this->layer_param_.window_data_param().mean_file(); LOG(INFO) << "Loading mean file from" << mean_file; BlobProto blob_proto; - ReadProtoFromBinaryFile(mean_file, &blob_proto); + ReadProtoFromBinaryFileOrDie(mean_file, &blob_proto); data_mean_.FromProto(blob_proto); CHECK_EQ(data_mean_.num(), 1); CHECK_EQ(data_mean_.width(), data_mean_.height()); diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 3483509c9ba..405cf1bcd25 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -28,21 +28,7 @@ Net::Net(const NetParameter& param) { template Net::Net(const string& param_file) { NetParameter param; - if (!ReadProtoFromTextFile(param_file, ¶m)) { - // Failed to parse file as NetParameter; try to parse as a V0NetParameter - // instead. - V0NetParameter v0_param; - CHECK(ReadProtoFromTextFile(param_file, &v0_param)) - << "Failed to parse NetParameter file: " << param_file; - LOG(ERROR) << "Parsed file as V0NetParameter: " << param_file; - LOG(ERROR) << "Note that future Caffe releases will not support " - << "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade " - << "this and any other network proto files to the new format."; - if (!UpgradeV0Net(v0_param, ¶m)) { - LOG(ERROR) << "Warning: had one or more problems upgrading " - << "V0NetParameter to NetParameter (see above); continuing anyway."; - } - } + ReadParamsFromTextFile(param_file, ¶m); Init(param); } @@ -317,10 +303,50 @@ void Net::CopyTrainedLayersFrom(const NetParameter& param) { template void Net::CopyTrainedLayersFrom(const string trained_filename) { NetParameter param; - ReadProtoFromBinaryFile(trained_filename, ¶m); + ReadParamsFromBinaryFile(trained_filename, ¶m); CopyTrainedLayersFrom(param); } +template +void Net::ReadParamsFromTextFile(const string& param_file, + NetParameter* param) { + if (!ReadProtoFromTextFile(param_file, param)) { + // Failed to parse file as NetParameter; try to parse as a V0NetParameter + // instead. + V0NetParameter v0_param; + CHECK(ReadProtoFromTextFile(param_file, &v0_param)) + << "Failed to parse NetParameter file: " << param_file; + LOG(ERROR) << "Parsed file as V0NetParameter: " << param_file; + LOG(ERROR) << "Note that future Caffe releases will not support " + << "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade " + << "this and any other network proto files to the new format."; + if (!UpgradeV0Net(v0_param, param)) { + LOG(ERROR) << "Warning: had one or more problems upgrading " + << "V0NetParameter to NetParameter (see above); continuing anyway."; + } + } +} + +template +void Net::ReadParamsFromBinaryFile(const string& param_file, + NetParameter* param) { + if (!ReadProtoFromBinaryFile(param_file, param)) { + // Failed to parse file as NetParameter; try to parse as a V0NetParameter + // instead. + V0NetParameter v0_param; + CHECK(ReadProtoFromBinaryFile(param_file, &v0_param)) + << "Failed to parse NetParameter file: " << param_file; + LOG(ERROR) << "Parsed file as V0NetParameter: " << param_file; + LOG(ERROR) << "Note that future Caffe releases will not support " + << "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade " + << "this and any other network proto files to the new format."; + if (!UpgradeV0Net(v0_param, param)) { + LOG(ERROR) << "Warning: had one or more problems upgrading " + << "V0NetParameter to NetParameter (see above); continuing anyway."; + } + } +} + template void Net::ToProto(NetParameter* param, bool write_diff) { param->Clear(); diff --git a/src/caffe/util/io.cpp b/src/caffe/util/io.cpp index fe452674dc8..e1e3c3a3fe5 100644 --- a/src/caffe/util/io.cpp +++ b/src/caffe/util/io.cpp @@ -51,18 +51,19 @@ void WriteProtoToTextFile(const Message& proto, const char* filename) { close(fd); } -void ReadProtoFromBinaryFile(const char* filename, Message* proto) { +bool ReadProtoFromBinaryFile(const char* filename, Message* proto) { int fd = open(filename, O_RDONLY); CHECK_NE(fd, -1) << "File not found: " << filename; ZeroCopyInputStream* raw_input = new FileInputStream(fd); CodedInputStream* coded_input = new CodedInputStream(raw_input); coded_input->SetTotalBytesLimit(536870912, 268435456); - CHECK(proto->ParseFromCodedStream(coded_input)); + bool success = proto->ParseFromCodedStream(coded_input); delete coded_input; delete raw_input; close(fd); + return success; } void WriteProtoToBinaryFile(const Message& proto, const char* filename) { diff --git a/tools/dump_network.cpp b/tools/dump_network.cpp index cee8bc85ea1..f29e150b048 100644 --- a/tools/dump_network.cpp +++ b/tools/dump_network.cpp @@ -31,16 +31,14 @@ int main(int argc, char** argv) { Caffe::set_mode(Caffe::GPU); Caffe::set_phase(Caffe::TEST); - NetParameter net_param; - NetParameter trained_net_param; - + shared_ptr > caffe_net; if (strcmp(argv[1], "none") == 0) { // We directly load the net param from trained file - ReadProtoFromBinaryFile(argv[2], &net_param); + caffe_net.reset(new Net(argv[2])); } else { - ReadProtoFromTextFileOrDie(argv[1], &net_param); + caffe_net.reset(new Net(argv[1])); } - ReadProtoFromBinaryFile(argv[2], &trained_net_param); + caffe_net->CopyTrainedLayersFrom(argv[2]); vector* > input_vec; shared_ptr > input_blob(new Blob()); @@ -51,9 +49,6 @@ int main(int argc, char** argv) { input_vec.push_back(input_blob.get()); } - shared_ptr > caffe_net(new Net(net_param)); - caffe_net->CopyTrainedLayersFrom(trained_net_param); - string output_prefix(argv[4]); // Run the network without training. LOG(ERROR) << "Performing Forward"; diff --git a/tools/extract_features.cpp b/tools/extract_features.cpp index 4274db44619..03938266fc9 100644 --- a/tools/extract_features.cpp +++ b/tools/extract_features.cpp @@ -56,12 +56,8 @@ int feature_extraction_pipeline(int argc, char** argv) { } Caffe::set_phase(Caffe::TEST); - NetParameter pretrained_net_param; - arg_pos = 0; // the name of the executable string pretrained_binary_proto(argv[++arg_pos]); - ReadProtoFromBinaryFile(pretrained_binary_proto.c_str(), - &pretrained_net_param); // Expected prototxt contains at least one data layer such as // the layer data_layer_name and one feature blob such as the @@ -90,13 +86,10 @@ int feature_extraction_pipeline(int argc, char** argv) { top: "fc7" } */ - NetParameter feature_extraction_net_param; string feature_extraction_proto(argv[++arg_pos]); - ReadProtoFromTextFile(feature_extraction_proto, - &feature_extraction_net_param); shared_ptr > feature_extraction_net( - new Net(feature_extraction_net_param)); - feature_extraction_net->CopyTrainedLayersFrom(pretrained_net_param); + new Net(feature_extraction_proto)); + feature_extraction_net->CopyTrainedLayersFrom(pretrained_binary_proto); string extract_feature_blob_name(argv[++arg_pos]); CHECK(feature_extraction_net->has_blob(extract_feature_blob_name)) diff --git a/tools/net_speed_benchmark.cpp b/tools/net_speed_benchmark.cpp index d6c0cdbd439..36a00779f60 100644 --- a/tools/net_speed_benchmark.cpp +++ b/tools/net_speed_benchmark.cpp @@ -49,10 +49,7 @@ int main(int argc, char** argv) { } Caffe::set_phase(Caffe::TRAIN); - NetParameter net_param; - ReadProtoFromTextFileOrDie(argv[1], - &net_param); - Net caffe_net(net_param); + Net caffe_net(argv[1]); // Run the network without training. LOG(ERROR) << "Performing Forward"; diff --git a/tools/test_net.cpp b/tools/test_net.cpp index 0b49e54a210..559fa730667 100644 --- a/tools/test_net.cpp +++ b/tools/test_net.cpp @@ -34,9 +34,7 @@ int main(int argc, char** argv) { Caffe::set_mode(Caffe::CPU); } - NetParameter test_net_param; - ReadProtoFromTextFileOrDie(argv[1], &test_net_param); - Net caffe_test_net(test_net_param); + Net caffe_test_net(argv[1]); NetParameter trained_net_param; ReadProtoFromBinaryFile(argv[2], &trained_net_param); caffe_test_net.CopyTrainedLayersFrom(trained_net_param); From 85b9e8219713aa240aa12f3e60c22189caf82d2b Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sun, 23 Mar 2014 17:34:39 -0700 Subject: [PATCH 48/60] regenerate imagenet_val feature extraction prototxt with missing IMAGE_DATA params --- examples/feature_extraction/imagenet_val.prototxt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/examples/feature_extraction/imagenet_val.prototxt b/examples/feature_extraction/imagenet_val.prototxt index a3626651b5e..14bfe770ef8 100644 --- a/examples/feature_extraction/imagenet_val.prototxt +++ b/examples/feature_extraction/imagenet_val.prototxt @@ -4,6 +4,15 @@ layers { type: IMAGE_DATA top: "data" top: "label" + image_data_param { + source: "$CAFFE_DIR/examples/_temp/file_list.txt" + mean_file: "$CAFFE_DIR/data/ilsvrc12/imagenet_mean.binaryproto" + batch_size: 50 + crop_size: 227 + mirror: false + new_height: 256 + new_width: 256 + } } layers { name: "conv1" From 927642e40853db87d6ad674b592c1c96f5472311 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sun, 23 Mar 2014 21:06:09 -0700 Subject: [PATCH 49/60] allow upgrade_net_proto to also read/write binary protos (e.g. saved models) --- tools/upgrade_net_proto.cpp | 43 +++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/tools/upgrade_net_proto.cpp b/tools/upgrade_net_proto.cpp index 49c27b52cc6..165d4ea90d9 100644 --- a/tools/upgrade_net_proto.cpp +++ b/tools/upgrade_net_proto.cpp @@ -25,27 +25,48 @@ int main(int argc, char** argv) { } bool success = true; - // First, check whether the input file is already in the new format. NetParameter upgraded_net_param; + bool is_binary = false; + bool is_already_upgraded = false; + // First, check whether the input file is already in the new format. if (ReadProtoFromTextFile(argv[1], &upgraded_net_param)) { - LOG(ERROR) << "File already in new proto format: " << argv[1]; + is_already_upgraded = true; + } else if (ReadProtoFromBinaryFile(argv[1], &upgraded_net_param)) { + is_already_upgraded = true; + is_binary = true; } else { V0NetParameter v0_net_param; - CHECK(ReadProtoFromTextFile(argv[1], &v0_net_param)) - << "Failed to parse input V0NetParameter file: " << argv[1]; + if (ReadProtoFromTextFile(argv[1], &v0_net_param)) { + LOG(ERROR) << "Successfully parsed file as V0NetParameter prototxt: " + << argv[1]; + } else if (ReadProtoFromBinaryFile(argv[1], &v0_net_param)) { + LOG(ERROR) << "Successfully parsed file as V0NetParameter binary proto: " + << argv[1]; + is_binary = true; + } else { + LOG(FATAL) << "Failed to parse input V0NetParameter file: " << argv[1]; + return 1; + } success = UpgradeV0Net(v0_net_param, &upgraded_net_param); if (!success) { LOG(ERROR) << "Encountered one or more problems upgrading net param " << "proto; see above."; } } - // TODO(jdonahue): figure out why WriteProtoToTextFile doesn't work - // (no file is created). - // WriteProtoToTextFile(upgraded_net_param, argv[2]); - ofstream output_proto; - output_proto.open(argv[2]); - output_proto << upgraded_net_param.DebugString(); - output_proto.close(); + if (is_already_upgraded) { + LOG(ERROR) << "File already in V1 proto format: " << argv[1]; + } + if (is_binary) { + WriteProtoToBinaryFile(upgraded_net_param, argv[2]); + } else { + // TODO(jdonahue): figure out why WriteProtoToTextFile doesn't work + // (no file is created). + // WriteProtoToTextFile(upgraded_net_param, argv[2]); + ofstream output_proto; + output_proto.open(argv[2]); + output_proto << upgraded_net_param.DebugString(); + output_proto.close(); + } LOG(ERROR) << "Wrote upgraded NetParameter proto to " << argv[2]; return !success; } From 23bfeeb14381aa31e7d355afeddf9676ca1a3347 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sun, 23 Mar 2014 21:53:59 -0700 Subject: [PATCH 50/60] add NetParameter required version number as breaking change for V0NetParameter --- examples/cifar10/cifar10_full.prototxt | 1 + examples/cifar10/cifar10_full_test.prototxt | 1 + examples/cifar10/cifar10_full_train.prototxt | 1 + examples/cifar10/cifar10_quick.prototxt | 3 +++ examples/cifar10/cifar10_quick_test.prototxt | 2 ++ examples/cifar10/cifar10_quick_train.prototxt | 2 ++ examples/feature_extraction/imagenet_val.prototxt | 1 + examples/imagenet/imagenet_deploy.prototxt | 1 + examples/imagenet/imagenet_train.prototxt | 1 + examples/imagenet/imagenet_val.prototxt | 1 + examples/imagenet/resume_training.sh | 2 +- examples/lenet/lenet.prototxt | 2 ++ examples/lenet/lenet_test.prototxt | 1 + examples/lenet/lenet_train.prototxt | 1 + .../pascal_finetune_train.prototxt | 1 + .../pascal-finetuning/pascal_finetune_val.prototxt | 1 + src/caffe/net.cpp | 4 ++++ src/caffe/proto/caffe.proto | 14 +++++++++----- src/caffe/test/test_net.cpp | 1 + src/caffe/test/test_split_layer.cpp | 12 ++++++++++++ src/caffe/test/test_upgrade_proto.cpp | 3 +++ src/caffe/util/upgrade_proto.cpp | 1 + 22 files changed, 51 insertions(+), 6 deletions(-) diff --git a/examples/cifar10/cifar10_full.prototxt b/examples/cifar10/cifar10_full.prototxt index 237a7a0a0ed..12559316e3f 100644 --- a/examples/cifar10/cifar10_full.prototxt +++ b/examples/cifar10/cifar10_full.prototxt @@ -1,3 +1,4 @@ +version: 1 name: "CIFAR10_full_deploy" # N.B. input image must be in CIFAR-10 format # as described at http://www.cs.toronto.edu/~kriz/cifar.html diff --git a/examples/cifar10/cifar10_full_test.prototxt b/examples/cifar10/cifar10_full_test.prototxt index ada373a55cb..aff99389525 100644 --- a/examples/cifar10/cifar10_full_test.prototxt +++ b/examples/cifar10/cifar10_full_test.prototxt @@ -1,3 +1,4 @@ +version: 1 name: "CIFAR10_full_test" layers { name: "cifar" diff --git a/examples/cifar10/cifar10_full_train.prototxt b/examples/cifar10/cifar10_full_train.prototxt index 56c9306e2dc..2f50bdaa464 100644 --- a/examples/cifar10/cifar10_full_train.prototxt +++ b/examples/cifar10/cifar10_full_train.prototxt @@ -1,3 +1,4 @@ +version: 1 name: "CIFAR10_full_train" layers { name: "cifar" diff --git a/examples/cifar10/cifar10_quick.prototxt b/examples/cifar10/cifar10_quick.prototxt index 505158f7a34..ec8c5b2b757 100644 --- a/examples/cifar10/cifar10_quick.prototxt +++ b/examples/cifar10/cifar10_quick.prototxt @@ -1,4 +1,7 @@ +version: 1 name: "CIFAR10_quick_test" +# N.B. input image must be in CIFAR-10 format +# as described at http://www.cs.toronto.edu/~kriz/cifar.html input: "data" input_dim: 1 input_dim: 3 diff --git a/examples/cifar10/cifar10_quick_test.prototxt b/examples/cifar10/cifar10_quick_test.prototxt index a154b9a0ea7..fb34844fa87 100644 --- a/examples/cifar10/cifar10_quick_test.prototxt +++ b/examples/cifar10/cifar10_quick_test.prototxt @@ -1,3 +1,5 @@ +# quick config +version: 1 name: "CIFAR10_quick_test" layers { name: "cifar" diff --git a/examples/cifar10/cifar10_quick_train.prototxt b/examples/cifar10/cifar10_quick_train.prototxt index de5b6c32c5d..62b45bf2175 100644 --- a/examples/cifar10/cifar10_quick_train.prototxt +++ b/examples/cifar10/cifar10_quick_train.prototxt @@ -1,3 +1,5 @@ +# quick config +version: 1 name: "CIFAR10_quick_train" layers { name: "cifar" diff --git a/examples/feature_extraction/imagenet_val.prototxt b/examples/feature_extraction/imagenet_val.prototxt index 14bfe770ef8..a8235b267e6 100644 --- a/examples/feature_extraction/imagenet_val.prototxt +++ b/examples/feature_extraction/imagenet_val.prototxt @@ -1,3 +1,4 @@ +version: 1 name: "CaffeNet" layers { name: "data" diff --git a/examples/imagenet/imagenet_deploy.prototxt b/examples/imagenet/imagenet_deploy.prototxt index 37ab9221da3..a7959f2beef 100644 --- a/examples/imagenet/imagenet_deploy.prototxt +++ b/examples/imagenet/imagenet_deploy.prototxt @@ -1,3 +1,4 @@ +version: 1 name: "CaffeNet" input: "data" input_dim: 10 diff --git a/examples/imagenet/imagenet_train.prototxt b/examples/imagenet/imagenet_train.prototxt index b34a9b49b07..7d3d3e4f583 100644 --- a/examples/imagenet/imagenet_train.prototxt +++ b/examples/imagenet/imagenet_train.prototxt @@ -1,3 +1,4 @@ +version: 1 name: "CaffeNet" layers { name: "data" diff --git a/examples/imagenet/imagenet_val.prototxt b/examples/imagenet/imagenet_val.prototxt index 2f1ead7c14a..5561fe4b937 100644 --- a/examples/imagenet/imagenet_val.prototxt +++ b/examples/imagenet/imagenet_val.prototxt @@ -1,3 +1,4 @@ +version: 1 name: "CaffeNet" layers { name: "data" diff --git a/examples/imagenet/resume_training.sh b/examples/imagenet/resume_training.sh index 2b3b4031b5d..6f5d0caeb25 100755 --- a/examples/imagenet/resume_training.sh +++ b/examples/imagenet/resume_training.sh @@ -3,6 +3,6 @@ TOOLS=../../build/tools GLOG_logtostderr=1 $TOOLS/train_net.bin \ - imagenet_solver.prototxt caffe_imagenet_train_10000.solverstate + imagenet_solver.prototxt caffe_imagenet_train_310000.solverstate echo "Done." diff --git a/examples/lenet/lenet.prototxt b/examples/lenet/lenet.prototxt index 491fad1b1c0..d4506a1aeeb 100644 --- a/examples/lenet/lenet.prototxt +++ b/examples/lenet/lenet.prototxt @@ -1,9 +1,11 @@ +version: 1 name: "LeNet" input: "data" input_dim: 64 input_dim: 1 input_dim: 28 input_dim: 28 +# N.B. input should be 0/1 = mnist raw data scaled by 0.00390625 layers { name: "conv1" type: CONVOLUTION diff --git a/examples/lenet/lenet_test.prototxt b/examples/lenet/lenet_test.prototxt index 3b59b75513d..5025ba4f83f 100644 --- a/examples/lenet/lenet_test.prototxt +++ b/examples/lenet/lenet_test.prototxt @@ -1,3 +1,4 @@ +version: 1 name: "LeNet-test" layers { name: "mnist" diff --git a/examples/lenet/lenet_train.prototxt b/examples/lenet/lenet_train.prototxt index e8a1e74e40b..12637bd1d59 100644 --- a/examples/lenet/lenet_train.prototxt +++ b/examples/lenet/lenet_train.prototxt @@ -1,3 +1,4 @@ +version: 1 name: "LeNet" layers { name: "mnist" diff --git a/examples/pascal-finetuning/pascal_finetune_train.prototxt b/examples/pascal-finetuning/pascal_finetune_train.prototxt index dfc60fe4b8a..757f671320a 100644 --- a/examples/pascal-finetuning/pascal_finetune_train.prototxt +++ b/examples/pascal-finetuning/pascal_finetune_train.prototxt @@ -1,3 +1,4 @@ +version: 1 name: "CaffeNet" layers { name: "data" diff --git a/examples/pascal-finetuning/pascal_finetune_val.prototxt b/examples/pascal-finetuning/pascal_finetune_val.prototxt index ff898fe7376..72db955155b 100644 --- a/examples/pascal-finetuning/pascal_finetune_val.prototxt +++ b/examples/pascal-finetuning/pascal_finetune_val.prototxt @@ -1,3 +1,4 @@ +version: 1 name: "CaffeNet" layers { name: "data" diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 405cf1bcd25..580f9240278 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -18,6 +18,8 @@ using std::pair; using std::map; using std::set; +const int kNetParameterVersionNumber = 1; + namespace caffe { template @@ -325,6 +327,7 @@ void Net::ReadParamsFromTextFile(const string& param_file, << "V0NetParameter to NetParameter (see above); continuing anyway."; } } + CHECK_EQ(param->version(), kNetParameterVersionNumber); } template @@ -345,6 +348,7 @@ void Net::ReadParamsFromBinaryFile(const string& param_file, << "V0NetParameter to NetParameter (see above); continuing anyway."; } } + CHECK_EQ(param->version(), kNetParameterVersionNumber); } template diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 34d59f9f3cd..a9cfa840dc5 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -39,18 +39,22 @@ message FillerParameter { } message NetParameter { - optional string name = 1; // consider giving the network a name + // The NetParameter version number; currently only version 1 is supported. + // (The version number should only be bumped for breaking changes, which + // ideally should never happen.) + required int32 version = 1 [default = 1]; + optional string name = 2; // consider giving the network a name // The input blobs to the network. - repeated string input = 2; + repeated string input = 3; // The dim of the input blobs. For each input blob there should be four // values specifying the num, channels, height and width of the input blob. // Thus, there should be a total of (4 * #input) numbers. - repeated int32 input_dim = 3; - repeated LayerParameter layers = 4; // a bunch of layers. + repeated int32 input_dim = 4; + repeated LayerParameter layers = 5; // a bunch of layers. // Whether the network will force every layer to carry out backward operation. // If set False, then whether to carry out backward is determined // automatically according to the net structure and learning rates. - optional bool force_backward = 5 [default = false]; + optional bool force_backward = 6 [default = false]; } message SolverParameter { diff --git a/src/caffe/test/test_net.cpp b/src/caffe/test/test_net.cpp index 4c7f0e7f7ac..025b3facdf6 100644 --- a/src/caffe/test/test_net.cpp +++ b/src/caffe/test/test_net.cpp @@ -44,6 +44,7 @@ class NetTest : public ::testing::Test { delete db; const string& proto_prefix = + "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " diff --git a/src/caffe/test/test_split_layer.cpp b/src/caffe/test/test_split_layer.cpp index 3c17b0e2450..45b06de252f 100644 --- a/src/caffe/test/test_split_layer.cpp +++ b/src/caffe/test/test_split_layer.cpp @@ -186,6 +186,7 @@ TYPED_TEST_CASE(SplitLayerInsertionTest, InsertionDtypes); TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion1) { const string& input_proto = + "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -210,6 +211,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion1) { TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion2) { const string& input_proto = + "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -247,6 +249,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion2) { TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { const string& input_proto = + "version: 1 " "name: 'CaffeNet' " "layers { " " name: 'data' " @@ -568,6 +571,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionWithInPlace) { const string& input_proto = + "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -598,6 +602,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionWithInPlace) { TYPED_TEST(SplitLayerInsertionTest, TestInsertion) { const string& input_proto = + "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -636,6 +641,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertion) { " bottom: 'innerprod3' " "} "; const string& expected_output_proto = + "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -693,6 +699,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertion) { TYPED_TEST(SplitLayerInsertionTest, TestInsertionTwoTop) { const string& input_proto = + "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -737,6 +744,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertionTwoTop) { " bottom: 'innerprod4' " "} "; const string& expected_output_proto = + "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -799,6 +807,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertionTwoTop) { TYPED_TEST(SplitLayerInsertionTest, TestInputInsertion) { const string& input_proto = + "version: 1 " "name: 'TestNetwork' " "input: 'data' " "input_dim: 10 " @@ -824,6 +833,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestInputInsertion) { " bottom: 'innerprod2' " "} "; const string& expected_output_proto = + "version: 1 " "name: 'TestNetwork' " "input: 'data' " "input_dim: 10 " @@ -860,6 +870,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestInputInsertion) { TYPED_TEST(SplitLayerInsertionTest, TestWithInPlace) { const string& input_proto = + "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -898,6 +909,7 @@ TYPED_TEST(SplitLayerInsertionTest, TestWithInPlace) { " bottom: 'data' " "} "; const string& expected_output_proto = + "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index 82034065d40..f36a4da946d 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -1193,6 +1193,7 @@ TYPED_TEST(V0UpgradeTest, TestSimple) { " bottom: 'label' " "} "; const string& expected_output_proto = + "version: 1 " "name: 'CaffeNet' " "layers { " " name: 'data' " @@ -1513,6 +1514,7 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { " } " "} "; const string& expected_output_proto = + "version: 1 " "name: 'CaffeNet' " "input: 'input_data' " "input_dim: 64 " @@ -2107,6 +2109,7 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { " bottom: 'label' " "} "; const string& expected_output_proto = + "version: 1 " "name: 'CaffeNet' " "layers { " " name: 'data' " diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 48813d83f53..fb86f0e8207 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -25,6 +25,7 @@ bool UpgradeV0Net(const V0NetParameter& v0_net_param_padding_layers, // Now upgrade layer parameters. bool is_fully_compatible = true; net_param->Clear(); + net_param->set_version(1); if (v0_net_param.has_name()) { net_param->set_name(v0_net_param.name()); } From 8198585b4a670ee2d261d436ebecbb63688da617 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sun, 23 Mar 2014 21:06:09 -0700 Subject: [PATCH 51/60] rollback previous commit adding version number to NetParameter -- going a different route --- examples/cifar10/cifar10_full.prototxt | 1 - examples/cifar10/cifar10_full_test.prototxt | 1 - examples/cifar10/cifar10_full_train.prototxt | 1 - examples/cifar10/cifar10_quick.prototxt | 3 --- examples/cifar10/cifar10_quick_test.prototxt | 2 -- examples/cifar10/cifar10_quick_train.prototxt | 2 -- examples/feature_extraction/imagenet_val.prototxt | 1 - examples/imagenet/imagenet_deploy.prototxt | 1 - examples/imagenet/imagenet_train.prototxt | 1 - examples/imagenet/imagenet_val.prototxt | 1 - examples/imagenet/resume_training.sh | 2 +- examples/lenet/lenet.prototxt | 2 -- examples/lenet/lenet_test.prototxt | 1 - examples/lenet/lenet_train.prototxt | 1 - .../pascal_finetune_train.prototxt | 1 - .../pascal-finetuning/pascal_finetune_val.prototxt | 1 - src/caffe/net.cpp | 4 ---- src/caffe/proto/caffe.proto | 14 +++++--------- src/caffe/test/test_net.cpp | 1 - src/caffe/test/test_split_layer.cpp | 12 ------------ src/caffe/test/test_upgrade_proto.cpp | 3 --- src/caffe/util/upgrade_proto.cpp | 1 - 22 files changed, 6 insertions(+), 51 deletions(-) diff --git a/examples/cifar10/cifar10_full.prototxt b/examples/cifar10/cifar10_full.prototxt index 12559316e3f..237a7a0a0ed 100644 --- a/examples/cifar10/cifar10_full.prototxt +++ b/examples/cifar10/cifar10_full.prototxt @@ -1,4 +1,3 @@ -version: 1 name: "CIFAR10_full_deploy" # N.B. input image must be in CIFAR-10 format # as described at http://www.cs.toronto.edu/~kriz/cifar.html diff --git a/examples/cifar10/cifar10_full_test.prototxt b/examples/cifar10/cifar10_full_test.prototxt index aff99389525..ada373a55cb 100644 --- a/examples/cifar10/cifar10_full_test.prototxt +++ b/examples/cifar10/cifar10_full_test.prototxt @@ -1,4 +1,3 @@ -version: 1 name: "CIFAR10_full_test" layers { name: "cifar" diff --git a/examples/cifar10/cifar10_full_train.prototxt b/examples/cifar10/cifar10_full_train.prototxt index 2f50bdaa464..56c9306e2dc 100644 --- a/examples/cifar10/cifar10_full_train.prototxt +++ b/examples/cifar10/cifar10_full_train.prototxt @@ -1,4 +1,3 @@ -version: 1 name: "CIFAR10_full_train" layers { name: "cifar" diff --git a/examples/cifar10/cifar10_quick.prototxt b/examples/cifar10/cifar10_quick.prototxt index ec8c5b2b757..505158f7a34 100644 --- a/examples/cifar10/cifar10_quick.prototxt +++ b/examples/cifar10/cifar10_quick.prototxt @@ -1,7 +1,4 @@ -version: 1 name: "CIFAR10_quick_test" -# N.B. input image must be in CIFAR-10 format -# as described at http://www.cs.toronto.edu/~kriz/cifar.html input: "data" input_dim: 1 input_dim: 3 diff --git a/examples/cifar10/cifar10_quick_test.prototxt b/examples/cifar10/cifar10_quick_test.prototxt index fb34844fa87..a154b9a0ea7 100644 --- a/examples/cifar10/cifar10_quick_test.prototxt +++ b/examples/cifar10/cifar10_quick_test.prototxt @@ -1,5 +1,3 @@ -# quick config -version: 1 name: "CIFAR10_quick_test" layers { name: "cifar" diff --git a/examples/cifar10/cifar10_quick_train.prototxt b/examples/cifar10/cifar10_quick_train.prototxt index 62b45bf2175..de5b6c32c5d 100644 --- a/examples/cifar10/cifar10_quick_train.prototxt +++ b/examples/cifar10/cifar10_quick_train.prototxt @@ -1,5 +1,3 @@ -# quick config -version: 1 name: "CIFAR10_quick_train" layers { name: "cifar" diff --git a/examples/feature_extraction/imagenet_val.prototxt b/examples/feature_extraction/imagenet_val.prototxt index a8235b267e6..14bfe770ef8 100644 --- a/examples/feature_extraction/imagenet_val.prototxt +++ b/examples/feature_extraction/imagenet_val.prototxt @@ -1,4 +1,3 @@ -version: 1 name: "CaffeNet" layers { name: "data" diff --git a/examples/imagenet/imagenet_deploy.prototxt b/examples/imagenet/imagenet_deploy.prototxt index a7959f2beef..37ab9221da3 100644 --- a/examples/imagenet/imagenet_deploy.prototxt +++ b/examples/imagenet/imagenet_deploy.prototxt @@ -1,4 +1,3 @@ -version: 1 name: "CaffeNet" input: "data" input_dim: 10 diff --git a/examples/imagenet/imagenet_train.prototxt b/examples/imagenet/imagenet_train.prototxt index 7d3d3e4f583..b34a9b49b07 100644 --- a/examples/imagenet/imagenet_train.prototxt +++ b/examples/imagenet/imagenet_train.prototxt @@ -1,4 +1,3 @@ -version: 1 name: "CaffeNet" layers { name: "data" diff --git a/examples/imagenet/imagenet_val.prototxt b/examples/imagenet/imagenet_val.prototxt index 5561fe4b937..2f1ead7c14a 100644 --- a/examples/imagenet/imagenet_val.prototxt +++ b/examples/imagenet/imagenet_val.prototxt @@ -1,4 +1,3 @@ -version: 1 name: "CaffeNet" layers { name: "data" diff --git a/examples/imagenet/resume_training.sh b/examples/imagenet/resume_training.sh index 6f5d0caeb25..2b3b4031b5d 100755 --- a/examples/imagenet/resume_training.sh +++ b/examples/imagenet/resume_training.sh @@ -3,6 +3,6 @@ TOOLS=../../build/tools GLOG_logtostderr=1 $TOOLS/train_net.bin \ - imagenet_solver.prototxt caffe_imagenet_train_310000.solverstate + imagenet_solver.prototxt caffe_imagenet_train_10000.solverstate echo "Done." diff --git a/examples/lenet/lenet.prototxt b/examples/lenet/lenet.prototxt index d4506a1aeeb..491fad1b1c0 100644 --- a/examples/lenet/lenet.prototxt +++ b/examples/lenet/lenet.prototxt @@ -1,11 +1,9 @@ -version: 1 name: "LeNet" input: "data" input_dim: 64 input_dim: 1 input_dim: 28 input_dim: 28 -# N.B. input should be 0/1 = mnist raw data scaled by 0.00390625 layers { name: "conv1" type: CONVOLUTION diff --git a/examples/lenet/lenet_test.prototxt b/examples/lenet/lenet_test.prototxt index 5025ba4f83f..3b59b75513d 100644 --- a/examples/lenet/lenet_test.prototxt +++ b/examples/lenet/lenet_test.prototxt @@ -1,4 +1,3 @@ -version: 1 name: "LeNet-test" layers { name: "mnist" diff --git a/examples/lenet/lenet_train.prototxt b/examples/lenet/lenet_train.prototxt index 12637bd1d59..e8a1e74e40b 100644 --- a/examples/lenet/lenet_train.prototxt +++ b/examples/lenet/lenet_train.prototxt @@ -1,4 +1,3 @@ -version: 1 name: "LeNet" layers { name: "mnist" diff --git a/examples/pascal-finetuning/pascal_finetune_train.prototxt b/examples/pascal-finetuning/pascal_finetune_train.prototxt index 757f671320a..dfc60fe4b8a 100644 --- a/examples/pascal-finetuning/pascal_finetune_train.prototxt +++ b/examples/pascal-finetuning/pascal_finetune_train.prototxt @@ -1,4 +1,3 @@ -version: 1 name: "CaffeNet" layers { name: "data" diff --git a/examples/pascal-finetuning/pascal_finetune_val.prototxt b/examples/pascal-finetuning/pascal_finetune_val.prototxt index 72db955155b..ff898fe7376 100644 --- a/examples/pascal-finetuning/pascal_finetune_val.prototxt +++ b/examples/pascal-finetuning/pascal_finetune_val.prototxt @@ -1,4 +1,3 @@ -version: 1 name: "CaffeNet" layers { name: "data" diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 580f9240278..405cf1bcd25 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -18,8 +18,6 @@ using std::pair; using std::map; using std::set; -const int kNetParameterVersionNumber = 1; - namespace caffe { template @@ -327,7 +325,6 @@ void Net::ReadParamsFromTextFile(const string& param_file, << "V0NetParameter to NetParameter (see above); continuing anyway."; } } - CHECK_EQ(param->version(), kNetParameterVersionNumber); } template @@ -348,7 +345,6 @@ void Net::ReadParamsFromBinaryFile(const string& param_file, << "V0NetParameter to NetParameter (see above); continuing anyway."; } } - CHECK_EQ(param->version(), kNetParameterVersionNumber); } template diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index a9cfa840dc5..34d59f9f3cd 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -39,22 +39,18 @@ message FillerParameter { } message NetParameter { - // The NetParameter version number; currently only version 1 is supported. - // (The version number should only be bumped for breaking changes, which - // ideally should never happen.) - required int32 version = 1 [default = 1]; - optional string name = 2; // consider giving the network a name + optional string name = 1; // consider giving the network a name // The input blobs to the network. - repeated string input = 3; + repeated string input = 2; // The dim of the input blobs. For each input blob there should be four // values specifying the num, channels, height and width of the input blob. // Thus, there should be a total of (4 * #input) numbers. - repeated int32 input_dim = 4; - repeated LayerParameter layers = 5; // a bunch of layers. + repeated int32 input_dim = 3; + repeated LayerParameter layers = 4; // a bunch of layers. // Whether the network will force every layer to carry out backward operation. // If set False, then whether to carry out backward is determined // automatically according to the net structure and learning rates. - optional bool force_backward = 6 [default = false]; + optional bool force_backward = 5 [default = false]; } message SolverParameter { diff --git a/src/caffe/test/test_net.cpp b/src/caffe/test/test_net.cpp index 025b3facdf6..4c7f0e7f7ac 100644 --- a/src/caffe/test/test_net.cpp +++ b/src/caffe/test/test_net.cpp @@ -44,7 +44,6 @@ class NetTest : public ::testing::Test { delete db; const string& proto_prefix = - "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " diff --git a/src/caffe/test/test_split_layer.cpp b/src/caffe/test/test_split_layer.cpp index 45b06de252f..3c17b0e2450 100644 --- a/src/caffe/test/test_split_layer.cpp +++ b/src/caffe/test/test_split_layer.cpp @@ -186,7 +186,6 @@ TYPED_TEST_CASE(SplitLayerInsertionTest, InsertionDtypes); TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion1) { const string& input_proto = - "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -211,7 +210,6 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion1) { TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion2) { const string& input_proto = - "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -249,7 +247,6 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertion2) { TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { const string& input_proto = - "version: 1 " "name: 'CaffeNet' " "layers { " " name: 'data' " @@ -571,7 +568,6 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionImageNet) { TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionWithInPlace) { const string& input_proto = - "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -602,7 +598,6 @@ TYPED_TEST(SplitLayerInsertionTest, TestNoInsertionWithInPlace) { TYPED_TEST(SplitLayerInsertionTest, TestInsertion) { const string& input_proto = - "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -641,7 +636,6 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertion) { " bottom: 'innerprod3' " "} "; const string& expected_output_proto = - "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -699,7 +693,6 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertion) { TYPED_TEST(SplitLayerInsertionTest, TestInsertionTwoTop) { const string& input_proto = - "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -744,7 +737,6 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertionTwoTop) { " bottom: 'innerprod4' " "} "; const string& expected_output_proto = - "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -807,7 +799,6 @@ TYPED_TEST(SplitLayerInsertionTest, TestInsertionTwoTop) { TYPED_TEST(SplitLayerInsertionTest, TestInputInsertion) { const string& input_proto = - "version: 1 " "name: 'TestNetwork' " "input: 'data' " "input_dim: 10 " @@ -833,7 +824,6 @@ TYPED_TEST(SplitLayerInsertionTest, TestInputInsertion) { " bottom: 'innerprod2' " "} "; const string& expected_output_proto = - "version: 1 " "name: 'TestNetwork' " "input: 'data' " "input_dim: 10 " @@ -870,7 +860,6 @@ TYPED_TEST(SplitLayerInsertionTest, TestInputInsertion) { TYPED_TEST(SplitLayerInsertionTest, TestWithInPlace) { const string& input_proto = - "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " @@ -909,7 +898,6 @@ TYPED_TEST(SplitLayerInsertionTest, TestWithInPlace) { " bottom: 'data' " "} "; const string& expected_output_proto = - "version: 1 " "name: 'TestNetwork' " "layers: { " " name: 'data' " diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index f36a4da946d..82034065d40 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -1193,7 +1193,6 @@ TYPED_TEST(V0UpgradeTest, TestSimple) { " bottom: 'label' " "} "; const string& expected_output_proto = - "version: 1 " "name: 'CaffeNet' " "layers { " " name: 'data' " @@ -1514,7 +1513,6 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { " } " "} "; const string& expected_output_proto = - "version: 1 " "name: 'CaffeNet' " "input: 'input_data' " "input_dim: 64 " @@ -2109,7 +2107,6 @@ TYPED_TEST(V0UpgradeTest, TestImageNet) { " bottom: 'label' " "} "; const string& expected_output_proto = - "version: 1 " "name: 'CaffeNet' " "layers { " " name: 'data' " diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index fb86f0e8207..48813d83f53 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -25,7 +25,6 @@ bool UpgradeV0Net(const V0NetParameter& v0_net_param_padding_layers, // Now upgrade layer parameters. bool is_fully_compatible = true; net_param->Clear(); - net_param->set_version(1); if (v0_net_param.has_name()) { net_param->set_name(v0_net_param.name()); } From b7444d63e2e3f0328527fe084b24c984f8ea3f59 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Thu, 27 Mar 2014 12:34:40 -0700 Subject: [PATCH 52/60] some post rebase fixes -- copyright, hdf5_output layer (still need to incorporate into util/upgrade_proto) --- include/caffe/util/upgrade_proto.hpp | 2 +- src/caffe/layers/image_data_layer.cu | 6 ++-- src/caffe/proto/caffe.proto | 51 ++++++++++++++------------- src/caffe/test/test_upgrade_proto.cpp | 2 +- src/caffe/util/upgrade_proto.cpp | 2 +- tools/upgrade_net_proto.cpp | 2 +- 6 files changed, 34 insertions(+), 31 deletions(-) diff --git a/include/caffe/util/upgrade_proto.hpp b/include/caffe/util/upgrade_proto.hpp index 18b006849a7..bcf2d601795 100644 --- a/include/caffe/util/upgrade_proto.hpp +++ b/include/caffe/util/upgrade_proto.hpp @@ -1,4 +1,4 @@ -// Copyright 2014 Jeff Donahue +// Copyright 2014 BVLC and contributors. #ifndef CAFFE_UTIL_UPGRADE_PROTO_H_ #define CAFFE_UTIL_UPGRADE_PROTO_H_ diff --git a/src/caffe/layers/image_data_layer.cu b/src/caffe/layers/image_data_layer.cu index 9a73daaed27..7b4952d7768 100644 --- a/src/caffe/layers/image_data_layer.cu +++ b/src/caffe/layers/image_data_layer.cu @@ -22,7 +22,7 @@ using std::pair; namespace caffe { template -Dtype ImagesLayer::Forward_gpu(const vector*>& bottom, +Dtype ImageDataLayer::Forward_gpu(const vector*>& bottom, vector*>* top) { // First, join the thread CHECK(!pthread_join(thread_, NULL)) << "Pthread joining failed."; @@ -34,11 +34,11 @@ Dtype ImagesLayer::Forward_gpu(const vector*>& bottom, prefetch_label_->cpu_data(), sizeof(Dtype) * prefetch_label_->count(), cudaMemcpyHostToDevice)); // Start a new prefetch thread - CHECK(!pthread_create(&thread_, NULL, ImagesLayerPrefetch, + CHECK(!pthread_create(&thread_, NULL, ImageDataLayerPrefetch, reinterpret_cast(this))) << "Pthread execution failed."; return Dtype(0.); } -INSTANTIATE_CLASS(ImagesLayer); +INSTANTIATE_CLASS(ImageDataLayer); } // namespace caffe diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 34d59f9f3cd..7575040537f 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -112,20 +112,21 @@ message LayerParameter { EUCLIDEAN_LOSS = 7; FLATTEN = 8; HDF5_DATA = 9; - IM2COL = 10; - IMAGE_DATA = 11; - INFOGAIN_LOSS = 12; - INNER_PRODUCT = 13; - LRN = 14; - MULTINOMIAL_LOGISTIC_LOSS = 15; - POOLING = 16; - RELU = 17; - SIGMOID = 18; - SOFTMAX = 19; - SOFTMAX_LOSS = 20; - SPLIT = 21; - TANH = 22; - WINDOW_DATA = 23; + HDF5_OUTPUT = 10; + IM2COL = 11; + IMAGE_DATA = 12; + INFOGAIN_LOSS = 13; + INNER_PRODUCT = 14; + LRN = 15; + MULTINOMIAL_LOGISTIC_LOSS = 16; + POOLING = 17; + RELU = 18; + SIGMOID = 19; + SOFTMAX = 20; + SOFTMAX_LOSS = 21; + SPLIT = 22; + TANH = 23; + WINDOW_DATA = 24; } optional LayerType type = 2; // the layer type from the enum above @@ -146,12 +147,13 @@ message LayerParameter { optional DataParameter data_param = 10; optional DropoutParameter dropout_param = 11; optional HDF5DataParameter hdf5_data_param = 12; - optional ImageDataParameter image_data_param = 13; - optional InfogainLossParameter infogain_loss_param = 14; - optional InnerProductParameter inner_product_param = 15; - optional LRNParameter lrn_param = 16; - optional PoolingParameter pooling_param = 17; - optional WindowDataParameter window_data_param = 18; + optional HDF5OutputParameter hdf5_output_param = 13; + optional ImageDataParameter image_data_param = 14; + optional InfogainLossParameter infogain_loss_param = 15; + optional InnerProductParameter inner_product_param = 16; + optional LRNParameter lrn_param = 17; + optional PoolingParameter pooling_param = 18; + optional WindowDataParameter window_data_param = 19; } // Message that stores parameters used by ConcatLayer @@ -209,6 +211,11 @@ message HDF5DataParameter { optional uint32 batch_size = 2; } +// Message that stores parameters used by HDF5OutputLayer +message HDF5OutputParameter { + optional string file_name = 1; +} + // Message that stores parameters used by ImageDataLayer message ImageDataParameter { // Specify the data source. @@ -298,7 +305,3 @@ message WindowDataParameter { // square: the tightest square around the window is cropped optional string crop_mode = 11 [default = "warp"]; } - -message HDF5OutputParameter { - optional string file_name = 1; -} diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index 82034065d40..4854eb1941d 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -1,4 +1,4 @@ -// Copyright 2014 Jeff Donahue +// Copyright 2014 BVLC and contributors. #include #include diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 48813d83f53..27298f3db38 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -1,4 +1,4 @@ -// Copyright 2014 Jeff Donahue +// Copyright 2014 BVLC and contributors. #include #include diff --git a/tools/upgrade_net_proto.cpp b/tools/upgrade_net_proto.cpp index 165d4ea90d9..41e41a99561 100644 --- a/tools/upgrade_net_proto.cpp +++ b/tools/upgrade_net_proto.cpp @@ -1,4 +1,4 @@ -// Copyright 2014 Jeff Donahue +// Copyright 2014 BVLC and contributors. // // This is a script to upgrade "V0" network prototxts to the new format. // Usage: From 57167a047cec62c9a000dbc84033150cd854a7c8 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Thu, 27 Mar 2014 14:58:45 -0700 Subject: [PATCH 53/60] cleaner version of refactoring with fields added to LayerConnection (which retains an optional V0LayerParameter field for legacy support) and LayerConnection renamed to LayerParameter --- Makefile | 16 +- include/caffe/util/upgrade_proto.hpp | 17 +- src/caffe/net.cpp | 53 ++--- src/caffe/proto/caffe.proto | 143 +++++++++++--- src/caffe/proto/deprecated/caffe.proto.v0 | 185 ------------------ .../deprecated/caffe_v0_to_v1_bridge.proto | 115 ----------- src/caffe/test/test_upgrade_proto.cpp | 12 +- src/caffe/util/upgrade_proto.cpp | 30 +-- tools/upgrade_net_proto.cpp | 72 ------- tools/upgrade_net_proto_binary.cpp | 46 +++++ tools/upgrade_net_proto_text.cpp | 52 +++++ 11 files changed, 286 insertions(+), 455 deletions(-) delete mode 100644 src/caffe/proto/deprecated/caffe.proto.v0 delete mode 100644 src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto delete mode 100644 tools/upgrade_net_proto.cpp create mode 100644 tools/upgrade_net_proto_binary.cpp create mode 100644 tools/upgrade_net_proto_text.cpp diff --git a/Makefile b/Makefile index b54e3173b65..acef07d6837 100644 --- a/Makefile +++ b/Makefile @@ -34,9 +34,6 @@ TOOL_SRCS := $(shell find tools -name "*.cpp") EXAMPLE_SRCS := $(shell find examples -name "*.cpp") # PROTO_SRCS are the protocol buffer definitions PROTO_SRCS := $(wildcard src/$(PROJECT)/proto/*.proto) -# DEPRECATED_PROTO_SRCS are protobuf definitions we no longer officially -# support, but keep around for upgrades etc. -DEPRECATED_PROTO_SRCS := $(wildcard src/$(PROJECT)/proto/deprecated/*.proto) # NONGEN_CXX_SRCS includes all source/header files except those generated # automatically (e.g., by proto). NONGEN_CXX_SRCS := $(shell find \ @@ -63,17 +60,12 @@ MAT$(PROJECT)_SO := matlab/$(PROJECT)/$(PROJECT) PROTO_GEN_HEADER := ${PROTO_SRCS:.proto=.pb.h} PROTO_GEN_CC := ${PROTO_SRCS:.proto=.pb.cc} PROTO_GEN_PY := ${PROTO_SRCS:.proto=_pb2.py} -# The generated files for deprecated protocol buffers -DEPRECATED_PROTO_GEN_HEADER := ${DEPRECATED_PROTO_SRCS:.proto=.pb.h} -DEPRECATED_PROTO_GEN_CC := ${DEPRECATED_PROTO_SRCS:.proto=.pb.cc} -DEPRECATED_PROTO_GEN_PY := ${DEPRECATED_PROTO_SRCS:.proto=_pb2.py} # The objects corresponding to the source files # These objects will be linked into the final shared library, so we # exclude the tool, example, and test objects. CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o}) CU_OBJS := $(addprefix $(BUILD_DIR)/, ${CU_SRCS:.cu=.cuo}) PROTO_OBJS := $(addprefix $(BUILD_DIR)/, ${PROTO_GEN_CC:.cc=.o}) -PROTO_OBJS += $(addprefix $(BUILD_DIR)/, ${DEPRECATED_PROTO_GEN_CC:.cc=.o}) OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS) # tool, example, and test objects TOOL_OBJS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o}) @@ -252,14 +244,12 @@ $(PROTO_GEN_PY): $(PROTO_SRCS) protoc --proto_path=src --python_out=python $(PROTO_SRCS) @echo -proto: $(PROTO_GEN_CC) $(DEPRECATED_PROTO_GEN_CC) +proto: $(PROTO_GEN_CC) -$(PROTO_GEN_CC): $(PROTO_SRCS) $(DEPRECATED_PROTO_SRCS) - protoc --proto_path=src --cpp_out=src $(PROTO_SRCS) $(DEPRECATED_PROTO_SRCS) +$(PROTO_GEN_CC): $(PROTO_SRCS) + protoc --proto_path=src --cpp_out=src $(PROTO_SRCS) mkdir -p include/$(PROJECT)/proto cp $(PROTO_GEN_HEADER) include/$(PROJECT)/proto/ - mkdir -p include/$(PROJECT)/proto/deprecated - cp $(DEPRECATED_PROTO_GEN_HEADER) include/$(PROJECT)/proto/deprecated/ @echo clean: diff --git a/include/caffe/util/upgrade_proto.hpp b/include/caffe/util/upgrade_proto.hpp index bcf2d601795..94ebd73f2e6 100644 --- a/include/caffe/util/upgrade_proto.hpp +++ b/include/caffe/util/upgrade_proto.hpp @@ -6,26 +6,29 @@ #include #include "caffe/proto/caffe.pb.h" -#include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h" using std::string; namespace caffe { +// Return true iff any layer contains parameters specified using +// deprecated V0LayerParameter. +bool NetNeedsUpgrade(const NetParameter& net_param); + // Perform all necessary transformations to upgrade a V0NetParameter into a // NetParameter (including upgrading padding layers and LayerParameters). -bool UpgradeV0Net(const V0NetParameter& v0_net_param, NetParameter* net_param); +bool UpgradeV0Net(const NetParameter& v0_net_param, NetParameter* net_param); -// Upgrade V0NetParameter with padding layers to pad-aware conv layers. +// Upgrade NetParameter with padding layers to pad-aware conv layers. // For any padding layer, remove it and put its pad parameter in any layers // taking its top blob as input. // Error if any of these above layers are not-conv layers. -void UpgradeV0PaddingLayers(const V0NetParameter& param, - V0NetParameter* param_upgraded_pad); +void UpgradeV0PaddingLayers(const NetParameter& param, + NetParameter* param_upgraded_pad); // Upgrade a single V0LayerConnection to the new LayerParameter format. -bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, - LayerParameter* layer_param); +bool UpgradeLayerParameter(const LayerParameter& v0_layer_connection, + LayerParameter* layer_param); LayerParameter_LayerType UpgradeV0LayerType(const string& type); diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 405cf1bcd25..24b1df4dded 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -7,7 +7,6 @@ #include "caffe/common.hpp" #include "caffe/proto/caffe.pb.h" -#include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h" #include "caffe/layer.hpp" #include "caffe/net.hpp" #include "caffe/util/io.hpp" @@ -310,40 +309,48 @@ void Net::CopyTrainedLayersFrom(const string trained_filename) { template void Net::ReadParamsFromTextFile(const string& param_file, NetParameter* param) { - if (!ReadProtoFromTextFile(param_file, param)) { - // Failed to parse file as NetParameter; try to parse as a V0NetParameter - // instead. - V0NetParameter v0_param; - CHECK(ReadProtoFromTextFile(param_file, &v0_param)) - << "Failed to parse NetParameter file: " << param_file; - LOG(ERROR) << "Parsed file as V0NetParameter: " << param_file; - LOG(ERROR) << "Note that future Caffe releases will not support " - << "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade " - << "this and any other network proto files to the new format."; - if (!UpgradeV0Net(v0_param, param)) { + CHECK(ReadProtoFromTextFile(param_file, param)) + << "Failed to parse NetParameter file: " << param_file; + if (NetNeedsUpgrade(*param)) { + // NetParameter was specified using the old style (V0LayerParameter); try to + // upgrade it. + LOG(ERROR) << "Attempting to upgrade input file specified using deprecated " + << "V0LayerParameter: " << param_file; + NetParameter original_param(*param); + if (!UpgradeV0Net(original_param, param)) { LOG(ERROR) << "Warning: had one or more problems upgrading " << "V0NetParameter to NetParameter (see above); continuing anyway."; + } else { + LOG(INFO) << "Successfully upgraded file specified using deprecated " + << "V0LayerParameter"; } + LOG(ERROR) << "Note that future Caffe releases will not support " + << "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade " + << "this and any other network proto files to the new format."; } } template void Net::ReadParamsFromBinaryFile(const string& param_file, NetParameter* param) { - if (!ReadProtoFromBinaryFile(param_file, param)) { - // Failed to parse file as NetParameter; try to parse as a V0NetParameter - // instead. - V0NetParameter v0_param; - CHECK(ReadProtoFromBinaryFile(param_file, &v0_param)) - << "Failed to parse NetParameter file: " << param_file; - LOG(ERROR) << "Parsed file as V0NetParameter: " << param_file; - LOG(ERROR) << "Note that future Caffe releases will not support " - << "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade " - << "this and any other network proto files to the new format."; - if (!UpgradeV0Net(v0_param, param)) { + CHECK(ReadProtoFromBinaryFile(param_file, param)) + << "Failed to parse NetParameter file: " << param_file; + if (NetNeedsUpgrade(*param)) { + // NetParameter was specified using the old style (V0LayerParameter); try to + // upgrade it. + LOG(ERROR) << "Attempting to upgrade input file specified using deprecated " + << "V0LayerParameter: " << param_file; + NetParameter original_param(*param); + if (!UpgradeV0Net(original_param, param)) { LOG(ERROR) << "Warning: had one or more problems upgrading " << "V0NetParameter to NetParameter (see above); continuing anyway."; + } else { + LOG(INFO) << "Successfully upgraded file specified using deprecated " + << "V0LayerParameter"; } + LOG(ERROR) << "Note that future Caffe releases will not support " + << "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade " + << "this and any other network proto files to the new format."; } } diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 7575040537f..1166b5982cb 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -40,13 +40,13 @@ message FillerParameter { message NetParameter { optional string name = 1; // consider giving the network a name + repeated LayerParameter layers = 2; // a bunch of layers. // The input blobs to the network. - repeated string input = 2; + repeated string input = 3; // The dim of the input blobs. For each input blob there should be four // values specifying the num, channels, height and width of the input blob. // Thus, there should be a total of (4 * #input) numbers. - repeated int32 input_dim = 3; - repeated LayerParameter layers = 4; // a bunch of layers. + repeated int32 input_dim = 4; // Whether the network will force every layer to carry out backward operation. // If set False, then whether to carry out backward is determined // automatically according to the net structure and learning rates. @@ -90,7 +90,9 @@ message SolverState { } message LayerParameter { - optional string name = 1; // the layer name + repeated string bottom = 2; // the name of the bottom blobs + repeated string top = 3; // the name of the top blobs + optional string name = 4; // the layer name // Add new LayerTypes to the enum below in lexicographical order (other than // starting with NONE), starting with the next available ID in the comment @@ -128,32 +130,34 @@ message LayerParameter { TANH = 23; WINDOW_DATA = 24; } - optional LayerType type = 2; // the layer type from the enum above - - repeated string bottom = 3; // the name of the bottom blobs - repeated string top = 4; // the name of the top blobs + optional LayerType type = 5; // the layer type from the enum above // The blobs containing the numeric parameters of the layer - repeated BlobProto blobs = 5; + repeated BlobProto blobs = 6; // The ratio that is multiplied on the global learning rate. If you want to // set the learning ratio for one blob, you need to set it for all blobs. - repeated float blobs_lr = 6; + repeated float blobs_lr = 7; // The weight decay that is multiplied on the global weight decay. - repeated float weight_decay = 7; + repeated float weight_decay = 8; // Parameters for particular layer types. - optional ConcatParameter concat_param = 8; - optional ConvolutionParameter convolution_param = 9; - optional DataParameter data_param = 10; - optional DropoutParameter dropout_param = 11; - optional HDF5DataParameter hdf5_data_param = 12; - optional HDF5OutputParameter hdf5_output_param = 13; - optional ImageDataParameter image_data_param = 14; - optional InfogainLossParameter infogain_loss_param = 15; - optional InnerProductParameter inner_product_param = 16; - optional LRNParameter lrn_param = 17; - optional PoolingParameter pooling_param = 18; - optional WindowDataParameter window_data_param = 19; + optional ConcatParameter concat_param = 9; + optional ConvolutionParameter convolution_param = 10; + optional DataParameter data_param = 11; + optional DropoutParameter dropout_param = 12; + optional HDF5DataParameter hdf5_data_param = 13; + optional HDF5OutputParameter hdf5_output_param = 14; + optional ImageDataParameter image_data_param = 15; + optional InfogainLossParameter infogain_loss_param = 16; + optional InnerProductParameter inner_product_param = 17; + optional LRNParameter lrn_param = 18; + optional PoolingParameter pooling_param = 19; + optional WindowDataParameter window_data_param = 20; + + // The layer parameters specified as a deprecated V0LayerParameter. + // This should never be used by any code except to upgrade to the new + // LayerParameter specification. + optional V0LayerParameter layer = 1; } // Message that stores parameters used by ConcatLayer @@ -305,3 +309,96 @@ message WindowDataParameter { // square: the tightest square around the window is cropped optional string crop_mode = 11 [default = "warp"]; } + +// DEPRECATED: V0LayerParameter is the old way of specifying layer parameters +// in Caffe. We keep this message type around for legacy support. +message V0LayerParameter { + optional string name = 1; // the layer name + optional string type = 2; // the string to specify the layer type + + // Parameters to specify layers with inner products. + optional uint32 num_output = 3; // The number of outputs for the layer + optional bool biasterm = 4 [default = true]; // whether to have bias terms + optional FillerParameter weight_filler = 5; // The filler for the weight + optional FillerParameter bias_filler = 6; // The filler for the bias + + optional uint32 pad = 7 [default = 0]; // The padding size + optional uint32 kernelsize = 8; // The kernel size + optional uint32 group = 9 [default = 1]; // The group size for group conv + optional uint32 stride = 10 [default = 1]; // The stride + enum PoolMethod { + MAX = 0; + AVE = 1; + STOCHASTIC = 2; + } + optional PoolMethod pool = 11 [default = MAX]; // The pooling method + optional float dropout_ratio = 12 [default = 0.5]; // dropout ratio + + optional uint32 local_size = 13 [default = 5]; // for local response norm + optional float alpha = 14 [default = 1.]; // for local response norm + optional float beta = 15 [default = 0.75]; // for local response norm + + // For data layers, specify the data source + optional string source = 16; + // For data pre-processing, we can do simple scaling and subtracting the + // data mean, if provided. Note that the mean subtraction is always carried + // out before scaling. + optional float scale = 17 [default = 1]; + optional string meanfile = 18; + // For data layers, specify the batch size. + optional uint32 batchsize = 19; + // For data layers, specify if we would like to randomly crop an image. + optional uint32 cropsize = 20 [default = 0]; + // For data layers, specify if we want to randomly mirror data. + optional bool mirror = 21 [default = false]; + + // The blobs containing the numeric parameters of the layer + repeated BlobProto blobs = 50; + // The ratio that is multiplied on the global learning rate. If you want to + // set the learning ratio for one blob, you need to set it for all blobs. + repeated float blobs_lr = 51; + // The weight decay that is multiplied on the global weight decay. + repeated float weight_decay = 52; + + // The rand_skip variable is for the data layer to skip a few data points + // to avoid all asynchronous sgd clients to start at the same point. The skip + // point would be set as rand_skip * rand(0,1). Note that rand_skip should not + // be larger than the number of keys in the leveldb. + optional uint32 rand_skip = 53 [default = 0]; + + // Fields related to detection (det_*) + // foreground (object) overlap threshold + optional float det_fg_threshold = 54 [default = 0.5]; + // background (non-object) overlap threshold + optional float det_bg_threshold = 55 [default = 0.5]; + // Fraction of batch that should be foreground objects + optional float det_fg_fraction = 56 [default = 0.25]; + + // optional bool OBSOLETE_can_clobber = 57 [default = true]; + + // Amount of contextual padding to add around a window + // (used only by the window_data_layer) + optional uint32 det_context_pad = 58 [default = 0]; + + // Mode for cropping out a detection window + // warp: cropped window is warped to a fixed size and aspect ratio + // square: the tightest square around the window is cropped + optional string det_crop_mode = 59 [default = "warp"]; + + // For ReshapeLayer, one needs to specify the new dimensions. + optional int32 new_num = 60 [default = 0]; + optional int32 new_channels = 61 [default = 0]; + optional int32 new_height = 62 [default = 0]; + optional int32 new_width = 63 [default = 0]; + + // Whether or not ImageLayer should shuffle the list of files at every epoch. + // It will also resize images if new_height or new_width are not zero. + optional bool shuffle_images = 64 [default = false]; + + // For ConcatLayer, one needs to specify the dimension for concatenation, and + // the other dimensions must be the same for all the bottom blobs. + // By default it will concatenate blobs along the channels dimension. + optional uint32 concat_dim = 65 [default = 1]; + + optional HDF5OutputParameter hdf5_output_param = 1001; +} diff --git a/src/caffe/proto/deprecated/caffe.proto.v0 b/src/caffe/proto/deprecated/caffe.proto.v0 deleted file mode 100644 index f1ef1901bd2..00000000000 --- a/src/caffe/proto/deprecated/caffe.proto.v0 +++ /dev/null @@ -1,185 +0,0 @@ -// Copyright 2013 Yangqing Jia - -package caffe; - -message BlobProto { - optional int32 num = 1 [default = 0]; - optional int32 channels = 2 [default = 0]; - optional int32 height = 3 [default = 0]; - optional int32 width = 4 [default = 0]; - repeated float data = 5 [packed = true]; - repeated float diff = 6 [packed = true]; -} - -// The BlobProtoVector is simply a way to pass multiple blobproto instances -// around. -message BlobProtoVector { - repeated BlobProto blobs = 1; -} - -message Datum { - optional int32 channels = 1; - optional int32 height = 2; - optional int32 width = 3; - // the actual image data, in bytes - optional bytes data = 4; - optional int32 label = 5; - // Optionally, the datum could also hold float data. - repeated float float_data = 6; -} - -message FillerParameter { - // The filler type. - optional string type = 1 [default = 'constant']; - optional float value = 2 [default = 0]; // the value in constant filler - optional float min = 3 [default = 0]; // the min value in uniform filler - optional float max = 4 [default = 1]; // the max value in uniform filler - optional float mean = 5 [default = 0]; // the mean value in gaussian filler - optional float std = 6 [default = 1]; // the std value in gaussian filler -} - -message LayerParameter { - optional string name = 1; // the layer name - optional string type = 2; // the string to specify the layer type - - // Parameters to specify layers with inner products. - optional uint32 num_output = 3; // The number of outputs for the layer - optional bool biasterm = 4 [default = true]; // whether to have bias terms - optional FillerParameter weight_filler = 5; // The filler for the weight - optional FillerParameter bias_filler = 6; // The filler for the bias - - optional uint32 pad = 7 [default = 0]; // The padding size - optional uint32 kernelsize = 8; // The kernel size - optional uint32 group = 9 [default = 1]; // The group size for group conv - optional uint32 stride = 10 [default = 1]; // The stride - enum PoolMethod { - MAX = 0; - AVE = 1; - STOCHASTIC = 2; - } - optional PoolMethod pool = 11 [default = MAX]; // The pooling method - optional float dropout_ratio = 12 [default = 0.5]; // dropout ratio - - optional uint32 local_size = 13 [default = 5]; // for local response norm - optional float alpha = 14 [default = 1.]; // for local response norm - optional float beta = 15 [default = 0.75]; // for local response norm - - // For data layers, specify the data source - optional string source = 16; - // For data pre-processing, we can do simple scaling and subtracting the - // data mean, if provided. Note that the mean subtraction is always carried - // out before scaling. - optional float scale = 17 [default = 1]; - optional string meanfile = 18; - // For data layers, specify the batch size. - optional uint32 batchsize = 19; - // For data layers, specify if we would like to randomly crop an image. - optional uint32 cropsize = 20 [default = 0]; - // For data layers, specify if we want to randomly mirror data. - optional bool mirror = 21 [default = false]; - - // The blobs containing the numeric parameters of the layer - repeated BlobProto blobs = 50; - // The ratio that is multiplied on the global learning rate. If you want to - // set the learning ratio for one blob, you need to set it for all blobs. - repeated float blobs_lr = 51; - // The weight decay that is multiplied on the global weight decay. - repeated float weight_decay = 52; - - // The rand_skip variable is for the data layer to skip a few data points - // to avoid all asynchronous sgd clients to start at the same point. The skip - // point would be set as rand_skip * rand(0,1). Note that rand_skip should not - // be larger than the number of keys in the leveldb. - optional uint32 rand_skip = 53 [default = 0]; - - // Fields related to detection (det_*) - // foreground (object) overlap threshold - optional float det_fg_threshold = 54 [default = 0.5]; - // background (non-object) overlap threshold - optional float det_bg_threshold = 55 [default = 0.5]; - // Fraction of batch that should be foreground objects - optional float det_fg_fraction = 56 [default = 0.25]; - - // optional bool OBSOLETE_can_clobber = 57 [default = true]; - - // Amount of contextual padding to add around a window - // (used only by the window_data_layer) - optional uint32 det_context_pad = 58 [default = 0]; - - // Mode for cropping out a detection window - // warp: cropped window is warped to a fixed size and aspect ratio - // square: the tightest square around the window is cropped - optional string det_crop_mode = 59 [default = "warp"]; - - // For ReshapeLayer, one needs to specify the new dimensions. - optional int32 new_num = 60 [default = 0]; - optional int32 new_channels = 61 [default = 0]; - optional int32 new_height = 62 [default = 0]; - optional int32 new_width = 63 [default = 0]; - - // Whether or not ImageLayer should shuffle the list of files at every epoch. - // It will also resize images if new_height or new_width are not zero. - optional bool shuffle_images = 64 [default = false]; - - // For ConcatLayer, one needs to specify the dimension for concatenation, and - // the other dimensions must be the same for all the bottom blobs. - // By default it will concatenate blobs along the channels dimension. - optional uint32 concat_dim = 65 [default = 1]; -} - -message LayerConnection { - optional LayerParameter layer = 1; // the layer parameter - repeated string bottom = 2; // the name of the bottom blobs - repeated string top = 3; // the name of the top blobs -} - -message NetParameter { - optional string name = 1; // consider giving the network a name - repeated LayerConnection layers = 2; // a bunch of layers. - // The input blobs to the network. - repeated string input = 3; - // The dim of the input blobs. For each input blob there should be four - // values specifying the num, channels, height and width of the input blob. - // Thus, there should be a total of (4 * #input) numbers. - repeated int32 input_dim = 4; - // Whether the network will force every layer to carry out backward operation. - // If set False, then whether to carry out backward is determined - // automatically according to the net structure and learning rates. - optional bool force_backward = 5 [default = false]; -} - -message SolverParameter { - optional string train_net = 1; // The proto file for the training net. - optional string test_net = 2; // The proto file for the testing net. - // The number of iterations for each testing phase. - optional int32 test_iter = 3 [default = 0]; - // The number of iterations between two testing phases. - optional int32 test_interval = 4 [default = 0]; - optional float base_lr = 5; // The base learning rate - // the number of iterations between displaying info. If display = 0, no info - // will be displayed. - optional int32 display = 6; - optional int32 max_iter = 7; // the maximum number of iterations - optional string lr_policy = 8; // The learning rate decay policy. - optional float gamma = 9; // The parameter to compute the learning rate. - optional float power = 10; // The parameter to compute the learning rate. - optional float momentum = 11; // The momentum value. - optional float weight_decay = 12; // The weight decay. - optional int32 stepsize = 13; // the stepsize for learning rate policy "step" - optional int32 snapshot = 14 [default = 0]; // The snapshot interval - optional string snapshot_prefix = 15; // The prefix for the snapshot. - // whether to snapshot diff in the results or not. Snapshotting diff will help - // debugging but the final protocol buffer size will be much larger. - optional bool snapshot_diff = 16 [default = false]; - // the mode solver will use: 0 for CPU and 1 for GPU. Use GPU in default. - optional int32 solver_mode = 17 [default = 1]; - // the device_id will that be used in GPU mode. Use device_id = 0 in default. - optional int32 device_id = 18 [default = 0]; -} - -// A message that stores the solver snapshots -message SolverState { - optional int32 iter = 1; // The current iteration - optional string learned_net = 2; // The file that stores the learned net. - repeated BlobProto history = 3; // The history for sgd solvers -} diff --git a/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto b/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto deleted file mode 100644 index edf05cb48a4..00000000000 --- a/src/caffe/proto/deprecated/caffe_v0_to_v1_bridge.proto +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright 2013 Yangqing Jia - -import "caffe/proto/caffe.proto"; - -package caffe; - -message V0NetParameter { - optional string name = 1; // consider giving the network a name - repeated V0LayerConnection layers = 2; // a bunch of layers. - // The input blobs to the network. - repeated string input = 3; - // The dim of the input blobs. For each input blob there should be four - // values specifying the num, channels, height and width of the input blob. - // Thus, there should be a total of (4 * #input) numbers. - repeated int32 input_dim = 4; - // Whether the network will force every layer to carry out backward operation. - // If set False, then whether to carry out backward is determined - // automatically according to the net structure and learning rates. - optional bool force_backward = 5 [default = false]; -} - -message V0LayerParameter { - optional string name = 1; // the layer name - optional string type = 2; // the string to specify the layer type - - // Parameters to specify layers with inner products. - optional uint32 num_output = 3; // The number of outputs for the layer - optional bool biasterm = 4 [default = true]; // whether to have bias terms - optional FillerParameter weight_filler = 5; // The filler for the weight - optional FillerParameter bias_filler = 6; // The filler for the bias - - optional uint32 pad = 7 [default = 0]; // The padding size - optional uint32 kernelsize = 8; // The kernel size - optional uint32 group = 9 [default = 1]; // The group size for group conv - optional uint32 stride = 10 [default = 1]; // The stride - enum PoolMethod { - MAX = 0; - AVE = 1; - STOCHASTIC = 2; - } - optional PoolMethod pool = 11 [default = MAX]; // The pooling method - optional float dropout_ratio = 12 [default = 0.5]; // dropout ratio - - optional uint32 local_size = 13 [default = 5]; // for local response norm - optional float alpha = 14 [default = 1.]; // for local response norm - optional float beta = 15 [default = 0.75]; // for local response norm - - // For data layers, specify the data source - optional string source = 16; - // For data pre-processing, we can do simple scaling and subtracting the - // data mean, if provided. Note that the mean subtraction is always carried - // out before scaling. - optional float scale = 17 [default = 1]; - optional string meanfile = 18; - // For data layers, specify the batch size. - optional uint32 batchsize = 19; - // For data layers, specify if we would like to randomly crop an image. - optional uint32 cropsize = 20 [default = 0]; - // For data layers, specify if we want to randomly mirror data. - optional bool mirror = 21 [default = false]; - - // The blobs containing the numeric parameters of the layer - repeated BlobProto blobs = 50; - // The ratio that is multiplied on the global learning rate. If you want to - // set the learning ratio for one blob, you need to set it for all blobs. - repeated float blobs_lr = 51; - // The weight decay that is multiplied on the global weight decay. - repeated float weight_decay = 52; - - // The rand_skip variable is for the data layer to skip a few data points - // to avoid all asynchronous sgd clients to start at the same point. The skip - // point would be set as rand_skip * rand(0,1). Note that rand_skip should not - // be larger than the number of keys in the leveldb. - optional uint32 rand_skip = 53 [default = 0]; - - // Fields related to detection (det_*) - // foreground (object) overlap threshold - optional float det_fg_threshold = 54 [default = 0.5]; - // background (non-object) overlap threshold - optional float det_bg_threshold = 55 [default = 0.5]; - // Fraction of batch that should be foreground objects - optional float det_fg_fraction = 56 [default = 0.25]; - - // optional bool OBSOLETE_can_clobber = 57 [default = true]; - - // Amount of contextual padding to add around a window - // (used only by the window_data_layer) - optional uint32 det_context_pad = 58 [default = 0]; - - // Mode for cropping out a detection window - // warp: cropped window is warped to a fixed size and aspect ratio - // square: the tightest square around the window is cropped - optional string det_crop_mode = 59 [default = "warp"]; - - // For ReshapeLayer, one needs to specify the new dimensions. - optional int32 new_num = 60 [default = 0]; - optional int32 new_channels = 61 [default = 0]; - optional int32 new_height = 62 [default = 0]; - optional int32 new_width = 63 [default = 0]; - - // Whether or not ImageLayer should shuffle the list of files at every epoch. - // It will also resize images if new_height or new_width are not zero. - optional bool shuffle_images = 64 [default = false]; - - // For ConcatLayer, one needs to specify the dimension for concatenation, and - // the other dimensions must be the same for all the bottom blobs. - // By default it will concatenate blobs along the channels dimension. - optional uint32 concat_dim = 65 [default = 1]; -} - -message V0LayerConnection { - optional V0LayerParameter layer = 1; // the layer parameter - repeated string bottom = 2; // the name of the bottom blobs - repeated string top = 3; // the name of the top blobs -} diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index 4854eb1941d..e5a9478c811 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -25,18 +25,18 @@ class PaddingLayerUpgradeTest : public ::testing::Test { // Test that UpgradeV0PaddingLayers called on the proto specified by // input_param_string results in the proto specified by // output_param_string. - V0NetParameter input_param; + NetParameter input_param; CHECK(google::protobuf::TextFormat::ParseFromString( input_param_string, &input_param)); - V0NetParameter expected_output_param; + NetParameter expected_output_param; CHECK(google::protobuf::TextFormat::ParseFromString( output_param_string, &expected_output_param)); - V0NetParameter actual_output_param; + NetParameter actual_output_param; UpgradeV0PaddingLayers(input_param, &actual_output_param); EXPECT_EQ(expected_output_param.DebugString(), actual_output_param.DebugString()); // Also test idempotence. - V0NetParameter double_pad_upgrade_param; + NetParameter double_pad_upgrade_param; UpgradeV0PaddingLayers(actual_output_param, &double_pad_upgrade_param); EXPECT_EQ(actual_output_param.DebugString(), double_pad_upgrade_param.DebugString()); @@ -1096,10 +1096,10 @@ class V0UpgradeTest : public ::testing::Test { protected: void RunV0UpgradeTest( const string& input_param_string, const string& output_param_string) { - // Test that UpgradeV0Net called on the V0NetParameter proto specified by + // Test that UpgradeV0Net called on the NetParameter proto specified by // input_param_string results in the NetParameter proto specified by // output_param_string. - V0NetParameter input_param; + NetParameter input_param; CHECK(google::protobuf::TextFormat::ParseFromString( input_param_string, &input_param)); NetParameter expected_output_param; diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 27298f3db38..8c7934e988e 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -10,17 +10,25 @@ #include "caffe/common.hpp" #include "caffe/util/upgrade_proto.hpp" #include "caffe/proto/caffe.pb.h" -#include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h" using std::map; using std::string; namespace caffe { -bool UpgradeV0Net(const V0NetParameter& v0_net_param_padding_layers, +bool NetNeedsUpgrade(const NetParameter& net_param) { + for (int i = 0; i < net_param.layers_size(); ++i) { + if (net_param.layers(i).has_layer()) { + return true; + } + } + return false; +} + +bool UpgradeV0Net(const NetParameter& v0_net_param_padding_layers, NetParameter* net_param) { // First upgrade padding layers to padded conv layers. - V0NetParameter v0_net_param; + NetParameter v0_net_param; UpgradeV0PaddingLayers(v0_net_param_padding_layers, &v0_net_param); // Now upgrade layer parameters. bool is_fully_compatible = true; @@ -29,8 +37,8 @@ bool UpgradeV0Net(const V0NetParameter& v0_net_param_padding_layers, net_param->set_name(v0_net_param.name()); } for (int i = 0; i < v0_net_param.layers_size(); ++i) { - is_fully_compatible &= UpgradeV0LayerConnection(v0_net_param.layers(i), - net_param->add_layers()); + is_fully_compatible &= UpgradeLayerParameter(v0_net_param.layers(i), + net_param->add_layers()); } for (int i = 0; i < v0_net_param.input_size(); ++i) { net_param->add_input(v0_net_param.input(i)); @@ -44,8 +52,8 @@ bool UpgradeV0Net(const V0NetParameter& v0_net_param_padding_layers, return is_fully_compatible; } -void UpgradeV0PaddingLayers(const V0NetParameter& param, - V0NetParameter* param_upgraded_pad) { +void UpgradeV0PaddingLayers(const NetParameter& param, + NetParameter* param_upgraded_pad) { // Copy everything other than the layers from the original param. param_upgraded_pad->Clear(); param_upgraded_pad->CopyFrom(param); @@ -57,7 +65,7 @@ void UpgradeV0PaddingLayers(const V0NetParameter& param, blob_name_to_last_top_idx[blob_name] = -1; } for (int i = 0; i < param.layers_size(); ++i) { - const V0LayerConnection& layer_connection = param.layers(i); + const LayerParameter& layer_connection = param.layers(i); const V0LayerParameter& layer_param = layer_connection.layer(); // Add the layer to the new net, unless it's a padding layer. if (layer_param.type() != "padding") { @@ -73,7 +81,7 @@ void UpgradeV0PaddingLayers(const V0NetParameter& param, if (top_idx == -1) { continue; } - V0LayerConnection source_layer = param.layers(top_idx); + LayerParameter source_layer = param.layers(top_idx); if (source_layer.layer().type() == "padding") { // This layer has a padding layer as input -- check that it is a conv // layer and takes only one input. Also check that the padding layer @@ -101,8 +109,8 @@ void UpgradeV0PaddingLayers(const V0NetParameter& param, } } -bool UpgradeV0LayerConnection(const V0LayerConnection& v0_layer_connection, - LayerParameter* layer_param) { +bool UpgradeLayerParameter(const LayerParameter& v0_layer_connection, + LayerParameter* layer_param) { bool is_fully_compatible = true; layer_param->Clear(); for (int i = 0; i < v0_layer_connection.bottom_size(); ++i) { diff --git a/tools/upgrade_net_proto.cpp b/tools/upgrade_net_proto.cpp deleted file mode 100644 index 41e41a99561..00000000000 --- a/tools/upgrade_net_proto.cpp +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2014 BVLC and contributors. -// -// This is a script to upgrade "V0" network prototxts to the new format. -// Usage: -// upgrade_net_proto v0_net_proto_file_in net_proto_file_out - -#include -#include // NOLINT(readability/streams) -#include // NOLINT(readability/streams) - -#include "caffe/caffe.hpp" -#include "caffe/util/io.hpp" -#include "caffe/util/upgrade_proto.hpp" - -using std::ofstream; - -using namespace caffe; // NOLINT(build/namespaces) - -int main(int argc, char** argv) { - ::google::InitGoogleLogging(argv[0]); - if (argc != 3) { - LOG(ERROR) << "Usage: " - << "upgrade_net_proto v0_net_proto_file_in net_proto_file_out"; - return 0; - } - - bool success = true; - NetParameter upgraded_net_param; - bool is_binary = false; - bool is_already_upgraded = false; - // First, check whether the input file is already in the new format. - if (ReadProtoFromTextFile(argv[1], &upgraded_net_param)) { - is_already_upgraded = true; - } else if (ReadProtoFromBinaryFile(argv[1], &upgraded_net_param)) { - is_already_upgraded = true; - is_binary = true; - } else { - V0NetParameter v0_net_param; - if (ReadProtoFromTextFile(argv[1], &v0_net_param)) { - LOG(ERROR) << "Successfully parsed file as V0NetParameter prototxt: " - << argv[1]; - } else if (ReadProtoFromBinaryFile(argv[1], &v0_net_param)) { - LOG(ERROR) << "Successfully parsed file as V0NetParameter binary proto: " - << argv[1]; - is_binary = true; - } else { - LOG(FATAL) << "Failed to parse input V0NetParameter file: " << argv[1]; - return 1; - } - success = UpgradeV0Net(v0_net_param, &upgraded_net_param); - if (!success) { - LOG(ERROR) << "Encountered one or more problems upgrading net param " - << "proto; see above."; - } - } - if (is_already_upgraded) { - LOG(ERROR) << "File already in V1 proto format: " << argv[1]; - } - if (is_binary) { - WriteProtoToBinaryFile(upgraded_net_param, argv[2]); - } else { - // TODO(jdonahue): figure out why WriteProtoToTextFile doesn't work - // (no file is created). - // WriteProtoToTextFile(upgraded_net_param, argv[2]); - ofstream output_proto; - output_proto.open(argv[2]); - output_proto << upgraded_net_param.DebugString(); - output_proto.close(); - } - LOG(ERROR) << "Wrote upgraded NetParameter proto to " << argv[2]; - return !success; -} diff --git a/tools/upgrade_net_proto_binary.cpp b/tools/upgrade_net_proto_binary.cpp new file mode 100644 index 00000000000..5ec14fcf6ae --- /dev/null +++ b/tools/upgrade_net_proto_binary.cpp @@ -0,0 +1,46 @@ +// Copyright 2014 BVLC and contributors. +// +// This is a script to upgrade "V0" network prototxts to the new format. +// Usage: +// upgrade_net_proto_binary v0_net_proto_file_in net_proto_file_out + +#include +#include // NOLINT(readability/streams) +#include // NOLINT(readability/streams) + +#include "caffe/caffe.hpp" +#include "caffe/util/io.hpp" +#include "caffe/util/upgrade_proto.hpp" + +using std::ofstream; + +using namespace caffe; // NOLINT(build/namespaces) + +int main(int argc, char** argv) { + ::google::InitGoogleLogging(argv[0]); + if (argc != 3) { + LOG(ERROR) << "Usage: " + << "upgrade_net_proto v0_net_proto_file_in net_proto_file_out"; + return 1; + } + + NetParameter net_param; + if (!ReadProtoFromBinaryFile(argv[1], &net_param)) { + LOG(ERROR) << "Failed to parse input binary file as NetParameter: " + << argv[1]; + return 2; + } + bool need_upgrade = NetNeedsUpgrade(net_param); + bool success = true; + if (need_upgrade) { + NetParameter v0_net_param(net_param); + success = UpgradeV0Net(v0_net_param, &net_param); + } else { + LOG(ERROR) << "File already in V1 proto format: " << argv[1]; + } + + WriteProtoToBinaryFile(net_param, argv[2]); + + LOG(ERROR) << "Wrote upgraded NetParameter binary proto to " << argv[2]; + return !success; +} diff --git a/tools/upgrade_net_proto_text.cpp b/tools/upgrade_net_proto_text.cpp new file mode 100644 index 00000000000..71abaa57fd7 --- /dev/null +++ b/tools/upgrade_net_proto_text.cpp @@ -0,0 +1,52 @@ +// Copyright 2014 BVLC and contributors. +// +// This is a script to upgrade "V0" network prototxts to the new format. +// Usage: +// upgrade_net_proto_text v0_net_proto_file_in net_proto_file_out + +#include +#include // NOLINT(readability/streams) +#include // NOLINT(readability/streams) + +#include "caffe/caffe.hpp" +#include "caffe/util/io.hpp" +#include "caffe/util/upgrade_proto.hpp" + +using std::ofstream; + +using namespace caffe; // NOLINT(build/namespaces) + +int main(int argc, char** argv) { + ::google::InitGoogleLogging(argv[0]); + if (argc != 3) { + LOG(ERROR) << "Usage: " + << "upgrade_net_proto v0_net_proto_file_in net_proto_file_out"; + return 1; + } + + NetParameter net_param; + if (!ReadProtoFromTextFile(argv[1], &net_param)) { + LOG(ERROR) << "Failed to parse input text file as NetParameter: " + << argv[1]; + return 2; + } + bool need_upgrade = NetNeedsUpgrade(net_param); + bool success = true; + if (need_upgrade) { + NetParameter v0_net_param(net_param); + success = UpgradeV0Net(v0_net_param, &net_param); + } else { + LOG(ERROR) << "File already in V1 proto format: " << argv[1]; + } + + // TODO(jdonahue): figure out why WriteProtoToTextFile doesn't work + // (no file is created). + // WriteProtoToTextFile(upgraded_net_param, argv[2]); + ofstream output_proto; + output_proto.open(argv[2]); + output_proto << net_param.DebugString(); + output_proto.close(); + + LOG(ERROR) << "Wrote upgraded NetParameter text proto to " << argv[2]; + return !success; +} From 1d27409dc2c7f8f0071f1a8a0a55344519f333e8 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Thu, 27 Mar 2014 15:11:22 -0700 Subject: [PATCH 54/60] add support for hdf5 output layer --- src/caffe/test/test_upgrade_proto.cpp | 16 ++++++++++++++++ src/caffe/util/upgrade_proto.cpp | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp index e5a9478c811..323a8aed61c 100644 --- a/src/caffe/test/test_upgrade_proto.cpp +++ b/src/caffe/test/test_upgrade_proto.cpp @@ -1472,6 +1472,15 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { "} " "layers { " " layer { " + " name: 'hdf5_output' " + " type: 'hdf5_output' " + " hdf5_output_param { " + " file_name: '/my/hdf5/output/file' " + " } " + " } " + "} " + "layers { " + " layer { " " name: 'im2col' " " type: 'im2col' " " } " @@ -1710,6 +1719,13 @@ TYPED_TEST(V0UpgradeTest, TestAllParams) { " type: FLATTEN " "} " "layers { " + " name: 'hdf5_output' " + " type: HDF5_OUTPUT " + " hdf5_output_param { " + " file_name: '/my/hdf5/output/file' " + " } " + "} " + "layers { " " name: 'im2col' " " type: IM2COL " "} " diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 8c7934e988e..0371d7632af 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -476,6 +476,16 @@ bool UpgradeLayerParameter(const LayerParameter& v0_layer_connection, is_fully_compatible = false; } } + if (v0_layer_param.has_hdf5_output_param()) { + if (type == "hdf5_output") { + layer_param->mutable_hdf5_output_param()->CopyFrom( + v0_layer_param.hdf5_output_param()); + } else { + LOG(ERROR) << "Unknown parameter hdf5_output_param for layer type " + << type; + is_fully_compatible = false; + } + } } return is_fully_compatible; } @@ -499,6 +509,8 @@ LayerParameter_LayerType UpgradeV0LayerType(const string& type) { return LayerParameter_LayerType_FLATTEN; } else if (type == "hdf5_data") { return LayerParameter_LayerType_HDF5_DATA; + } else if (type == "hdf5_output") { + return LayerParameter_LayerType_HDF5_OUTPUT; } else if (type == "im2col") { return LayerParameter_LayerType_IM2COL; } else if (type == "images") { From 12d88d52a702dbdbb69f10a2ec434fb240ed80a3 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Thu, 27 Mar 2014 15:53:10 -0700 Subject: [PATCH 55/60] minor cleanup --- include/caffe/vision_layers.hpp | 52 ++++++++++++++++----------------- src/caffe/proto/caffe.proto | 2 +- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp index 870bb22fb94..4f6dfa70be2 100644 --- a/include/caffe/vision_layers.hpp +++ b/include/caffe/vision_layers.hpp @@ -288,6 +288,32 @@ class FlattenLayer : public Layer { int count_; }; +template +class HDF5OutputLayer : public Layer { + public: + explicit HDF5OutputLayer(const LayerParameter& param); + virtual ~HDF5OutputLayer(); + virtual void SetUp(const vector*>& bottom, + vector*>* top); + inline std::string file_name() const { return file_name_; } + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void SaveBlobs(); + + std::string file_name_; + hid_t file_id_; + Blob data_blob_; + Blob label_blob_; +}; + template class HDF5DataLayer : public Layer { public: @@ -426,32 +452,6 @@ class InnerProductLayer : public Layer { shared_ptr bias_multiplier_; }; -template -class HDF5OutputLayer : public Layer { - public: - explicit HDF5OutputLayer(const LayerParameter& param); - virtual ~HDF5OutputLayer(); - virtual void SetUp(const vector*>& bottom, - vector*>* top); - inline std::string file_name() const { return file_name_; } - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void SaveBlobs(); - - std::string file_name_; - hid_t file_id_; - Blob data_blob_; - Blob label_blob_; -}; - template class LRNLayer : public Layer { public: diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 1166b5982cb..1a31109f784 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -154,7 +154,7 @@ message LayerParameter { optional PoolingParameter pooling_param = 19; optional WindowDataParameter window_data_param = 20; - // The layer parameters specified as a deprecated V0LayerParameter. + // DEPRECATED: The layer parameters specified as a V0LayerParameter. // This should never be used by any code except to upgrade to the new // LayerParameter specification. optional V0LayerParameter layer = 1; From 56ce56266cd70b5376f90395e156d817179e3c3a Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Thu, 27 Mar 2014 16:07:07 -0700 Subject: [PATCH 56/60] fix upgrade_net_proto names --- src/caffe/net.cpp | 8 ++++---- tools/upgrade_net_proto_binary.cpp | 2 +- tools/upgrade_net_proto_text.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 24b1df4dded..060d0000e51 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -325,8 +325,8 @@ void Net::ReadParamsFromTextFile(const string& param_file, << "V0LayerParameter"; } LOG(ERROR) << "Note that future Caffe releases will not support " - << "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade " - << "this and any other network proto files to the new format."; + << "V0NetParameter; use ./build/tools/upgrade_net_proto_text.bin to " + << "upgrade this and any other network proto files to the new format."; } } @@ -349,8 +349,8 @@ void Net::ReadParamsFromBinaryFile(const string& param_file, << "V0LayerParameter"; } LOG(ERROR) << "Note that future Caffe releases will not support " - << "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade " - << "this and any other network proto files to the new format."; + << "V0NetParameter; use ./build/tools/upgrade_net_proto_binary.bin to " + << "upgrade this and any other network proto files to the new format."; } } diff --git a/tools/upgrade_net_proto_binary.cpp b/tools/upgrade_net_proto_binary.cpp index 5ec14fcf6ae..928fc52dc27 100644 --- a/tools/upgrade_net_proto_binary.cpp +++ b/tools/upgrade_net_proto_binary.cpp @@ -20,7 +20,7 @@ int main(int argc, char** argv) { ::google::InitGoogleLogging(argv[0]); if (argc != 3) { LOG(ERROR) << "Usage: " - << "upgrade_net_proto v0_net_proto_file_in net_proto_file_out"; + << "upgrade_net_proto_binary v0_net_proto_file_in net_proto_file_out"; return 1; } diff --git a/tools/upgrade_net_proto_text.cpp b/tools/upgrade_net_proto_text.cpp index 71abaa57fd7..b8296c3e42c 100644 --- a/tools/upgrade_net_proto_text.cpp +++ b/tools/upgrade_net_proto_text.cpp @@ -20,7 +20,7 @@ int main(int argc, char** argv) { ::google::InitGoogleLogging(argv[0]); if (argc != 3) { LOG(ERROR) << "Usage: " - << "upgrade_net_proto v0_net_proto_file_in net_proto_file_out"; + << "upgrade_net_proto_text v0_net_proto_file_in net_proto_file_out"; return 1; } From e20c43abc1d39299aa125ebf26f1cec2b8ad3043 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Thu, 27 Mar 2014 16:37:20 -0700 Subject: [PATCH 57/60] update docs (and a couple comments) for refactored layerparam --- docs/imagenet_training.md | 2 +- docs/mnist_prototxt.md | 60 ++++++++++++-------------- src/caffe/layers/window_data_layer.cpp | 2 +- src/caffe/layers/window_data_layer.cu | 4 +- tools/extract_features.cpp | 36 ++++++++-------- 5 files changed, 50 insertions(+), 54 deletions(-) diff --git a/docs/imagenet_training.md b/docs/imagenet_training.md index fbf1e1359ab..9e0076cf65f 100644 --- a/docs/imagenet_training.md +++ b/docs/imagenet_training.md @@ -55,7 +55,7 @@ Network Definition The network definition follows strictly the one in Krizhevsky et al. You can find the detailed definition at `examples/imagenet/imagenet_train.prototxt`. Note the paths in the data layer - if you have not followed the exact paths in this guide you will need to change the following lines: source: "ilvsrc12_train_leveldb" - meanfile: "../../data/ilsvrc12/imagenet_mean.binaryproto" + mean_file: "../../data/ilsvrc12/imagenet_mean.binaryproto" to point to your own leveldb and image mean. Likewise, do the same for `examples/imagenet/imagenet_val.prototxt`. diff --git a/docs/mnist_prototxt.md b/docs/mnist_prototxt.md index 5ed2f23b2d6..aaff2b00953 100644 --- a/docs/mnist_prototxt.md +++ b/docs/mnist_prototxt.md @@ -17,11 +17,11 @@ Writing the Data Layer Currently, we will read the MNIST data from the leveldb we created earlier in the demo. This is defined by a data layer: layers { - layer { - name: "mnist" - type: "data" + name: "mnist" + type: DATA + data_param { source: "mnist-train-leveldb" - batchsize: 64 + batch_size: 64 scale: 0.00390625 } top: "data" @@ -35,9 +35,11 @@ Writing the Convolution Layer Let's define the first convolution layer: layers { - layer { - name: "conv1" - type: "conv" + name: "conv1" + type: CONVOLUTION + blobs_lr: 1. + blobs_lr: 2. + convolution_param { num_output: 20 kernelsize: 5 stride: 1 @@ -47,8 +49,6 @@ Let's define the first convolution layer: bias_filler { type: "constant" } - blobs_lr: 1. - blobs_lr: 2. } bottom: "data" top: "conv1" @@ -65,10 +65,10 @@ Writing the Pooling Layer Phew. Pooling layers are actually much easier to define: layers { - layer { - name: "pool1" - type: "pool" - kernelsize: 2 + name: "pool1" + type: POOLING + pooling_param { + kernel_size: 2 stride: 2 pool: MAX } @@ -82,12 +82,14 @@ Similarly, you can write up the second convolution and pooling layers. Check `da Writing the Fully Connected Layer ---------------------------------- -Writing a fully connected layers is also simple: +Writing a fully connected layer is also simple: layers { - layer { - name: "ip1" - type: "innerproduct" + name: "ip1" + type: INNER_PRODUCT + blobs_lr: 1. + blobs_lr: 2. + inner_product_param { num_output: 500 weight_filler { type: "xavier" @@ -95,8 +97,6 @@ Writing a fully connected layers is also simple: bias_filler { type: "constant" } - blobs_lr: 1. - blobs_lr: 2. } bottom: "pool2" top: "ip1" @@ -109,10 +109,8 @@ Writing the ReLU Layer A ReLU Layer is also simple: layers { - layer { - name: "relu1" - type: "relu" - } + name: "relu1" + type: RELU bottom: "ip1" top: "ip1" } @@ -122,9 +120,11 @@ Since ReLU is an element-wise operation, we can do *in-place* operations to save After the ReLU layer, we will write another innerproduct layer: layers { - layer { - name: "ip2" - type: "innerproduct" + name: "ip2" + type: INNER_PRODUCT + blobs_lr: 1. + blobs_lr: 2. + inner_product_param { num_output: 10 weight_filler { type: "xavier" @@ -132,8 +132,6 @@ After the ReLU layer, we will write another innerproduct layer: bias_filler { type: "constant" } - blobs_lr: 1. - blobs_lr: 2. } bottom: "ip1" top: "ip2" @@ -144,10 +142,8 @@ Writing the Loss Layer Finally, we will write the loss! layers { - layer { - name: "loss" - type: "softmax_loss" - } + name: "loss" + type: SOFTMAX_LOSS bottom: "ip2" bottom: "label" } diff --git a/src/caffe/layers/window_data_layer.cpp b/src/caffe/layers/window_data_layer.cpp index 7c3e24b3b9c..838f150ee40 100644 --- a/src/caffe/layers/window_data_layer.cpp +++ b/src/caffe/layers/window_data_layer.cpp @@ -24,7 +24,7 @@ using std::string; using std::map; using std::pair; -// caffe.proto > LayerParameter +// caffe.proto > LayerParameter > WindowDataParameter // 'source' field specifies the window_file // 'crop_size' indicates the desired warped size diff --git a/src/caffe/layers/window_data_layer.cu b/src/caffe/layers/window_data_layer.cu index d46b5e02552..69614625f7d 100644 --- a/src/caffe/layers/window_data_layer.cu +++ b/src/caffe/layers/window_data_layer.cu @@ -16,9 +16,9 @@ using std::string; using std::map; using std::pair; -// caffe.proto > LayerParameter +// caffe.proto > LayerParameter > WindowDataParameter // 'source' field specifies the window_file -// 'cropsize' indicates the desired warped size +// 'crop_size' indicates the desired warped size namespace caffe { diff --git a/tools/extract_features.cpp b/tools/extract_features.cpp index 03938266fc9..cdad6676d7f 100644 --- a/tools/extract_features.cpp +++ b/tools/extract_features.cpp @@ -64,26 +64,26 @@ int feature_extraction_pipeline(int argc, char** argv) { // fc7 top blob to extract features. /* layers { - layer { - name: "data_layer_name" - type: "data" - source: "/path/to/your/images/to/extract/feature/images_leveldb" - meanfile: "/path/to/your/image_mean.binaryproto" - batchsize: 128 - cropsize: 227 - mirror: false - } - top: "data_blob_name" - top: "label_blob_name" + name: "data_layer_name" + type: DATA + data_param { + source: "/path/to/your/images/to/extract/feature/images_leveldb" + mean_file: "/path/to/your/image_mean.binaryproto" + batch_size: 128 + crop_size: 227 + mirror: false + } + top: "data_blob_name" + top: "label_blob_name" } layers { - layer { - name: "drop7" - type: "dropout" - dropout_ratio: 0.5 - } - bottom: "fc7" - top: "fc7" + name: "drop7" + type: DROPOUT + dropout_param { + dropout_ratio: 0.5 + } + bottom: "fc7" + top: "fc7" } */ string feature_extraction_proto(argv[++arg_pos]); From 48e2e8ea529fa975eb5f8d8da0399911719509e4 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Thu, 27 Mar 2014 18:24:51 -0700 Subject: [PATCH 58/60] add NetParameterPrettyPrint so that upgrade tool prints inputs before layers --- include/caffe/util/upgrade_proto.hpp | 6 ++++++ src/caffe/proto/caffe_pretty_print.proto | 18 ++++++++++++++++++ src/caffe/util/upgrade_proto.cpp | 20 ++++++++++++++++++++ tools/upgrade_net_proto_text.cpp | 9 +++++++-- 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/caffe/proto/caffe_pretty_print.proto diff --git a/include/caffe/util/upgrade_proto.hpp b/include/caffe/util/upgrade_proto.hpp index 94ebd73f2e6..bcd90124fb3 100644 --- a/include/caffe/util/upgrade_proto.hpp +++ b/include/caffe/util/upgrade_proto.hpp @@ -6,6 +6,7 @@ #include #include "caffe/proto/caffe.pb.h" +#include "caffe/proto/caffe_pretty_print.pb.h" using std::string; @@ -32,6 +33,11 @@ bool UpgradeLayerParameter(const LayerParameter& v0_layer_connection, LayerParameter_LayerType UpgradeV0LayerType(const string& type); +// Convert a NetParameter to NetParameterPrettyPrint used for dumping to +// proto text files. +void NetParameterToPrettyPrint(const NetParameter& param, + NetParameterPrettyPrint* pretty_param); + } // namespace caffe #endif // CAFFE_UTIL_UPGRADE_PROTO_H_ diff --git a/src/caffe/proto/caffe_pretty_print.proto b/src/caffe/proto/caffe_pretty_print.proto new file mode 100644 index 00000000000..cfdce82c79f --- /dev/null +++ b/src/caffe/proto/caffe_pretty_print.proto @@ -0,0 +1,18 @@ +// Copyright 2014 BVLC and contributors. + +package caffe; + +import "caffe/proto/caffe.proto"; + +// A near-duplicate of NetParameter with fields re-numbered to beautify +// automatic prototext dumps. The main practical purpose is to print inputs +// before layers, because having inputs at the end looks weird. +// NetParameterPrettyPrint should never be used in code except for conversion +// FROM NetParameter and subsequent dumping to proto text file. +message NetParameterPrettyPrint { + optional string name = 1; + optional bool force_backward = 2 [default = false]; + repeated string input = 3; + repeated int32 input_dim = 4; + repeated LayerParameter layers = 5; +} diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 0371d7632af..306f960ff5f 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -545,4 +545,24 @@ LayerParameter_LayerType UpgradeV0LayerType(const string& type) { } } +void NetParameterToPrettyPrint(const NetParameter& param, + NetParameterPrettyPrint* pretty_param) { + pretty_param->Clear(); + if (param.has_name()) { + pretty_param->set_name(param.name()); + } + if (param.has_force_backward()) { + pretty_param->set_force_backward(param.force_backward()); + } + for (int i = 0; i < param.input_size(); ++i) { + pretty_param->add_input(param.input(i)); + } + for (int i = 0; i < param.input_dim_size(); ++i) { + pretty_param->add_input_dim(param.input_dim(i)); + } + for (int i = 0; i < param.layers_size(); ++i) { + pretty_param->add_layers()->CopyFrom(param.layers(i)); + } +} + } // namespace caffe diff --git a/tools/upgrade_net_proto_text.cpp b/tools/upgrade_net_proto_text.cpp index b8296c3e42c..aefdc7e2961 100644 --- a/tools/upgrade_net_proto_text.cpp +++ b/tools/upgrade_net_proto_text.cpp @@ -39,12 +39,17 @@ int main(int argc, char** argv) { LOG(ERROR) << "File already in V1 proto format: " << argv[1]; } + // Convert to a NetParameterPrettyPrint to print fields in desired + // order. + NetParameterPrettyPrint net_param_pretty; + NetParameterToPrettyPrint(net_param, &net_param_pretty); + // TODO(jdonahue): figure out why WriteProtoToTextFile doesn't work // (no file is created). - // WriteProtoToTextFile(upgraded_net_param, argv[2]); + // WriteProtoToTextFile(net_param_pretty, argv[2]); ofstream output_proto; output_proto.open(argv[2]); - output_proto << net_param.DebugString(); + output_proto << net_param_pretty.DebugString(); output_proto.close(); LOG(ERROR) << "Wrote upgraded NetParameter text proto to " << argv[2]; From 9f9215933d23c0043c5218ef7f7dcf2c38539114 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Thu, 27 Mar 2014 21:25:26 -0700 Subject: [PATCH 59/60] move ReadNetParamsFrom{Text,Binary}File into util --- include/caffe/net.hpp | 3 -- include/caffe/util/upgrade_proto.hpp | 6 ++++ src/caffe/net.cpp | 52 ++-------------------------- src/caffe/util/upgrade_proto.cpp | 47 +++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 53 deletions(-) diff --git a/include/caffe/net.hpp b/include/caffe/net.hpp index 7fe2c5b1634..b97c1cc0f32 100644 --- a/include/caffe/net.hpp +++ b/include/caffe/net.hpp @@ -58,9 +58,6 @@ class Net { // trained layers from another net parameter instance. void CopyTrainedLayersFrom(const NetParameter& param); void CopyTrainedLayersFrom(const string trained_filename); - // Read parameters from a file into a NetParameter proto message. - void ReadParamsFromTextFile(const string& param_file, NetParameter* param); - void ReadParamsFromBinaryFile(const string& param_file, NetParameter* param); // Writes the net to a proto. void ToProto(NetParameter* param, bool write_diff = false); diff --git a/include/caffe/util/upgrade_proto.hpp b/include/caffe/util/upgrade_proto.hpp index bcd90124fb3..a1ac060970f 100644 --- a/include/caffe/util/upgrade_proto.hpp +++ b/include/caffe/util/upgrade_proto.hpp @@ -38,6 +38,12 @@ LayerParameter_LayerType UpgradeV0LayerType(const string& type); void NetParameterToPrettyPrint(const NetParameter& param, NetParameterPrettyPrint* pretty_param); +// Read parameters from a file into a NetParameter proto message. +void ReadNetParamsFromTextFileOrDie(const string& param_file, + NetParameter* param); +void ReadNetParamsFromBinaryFileOrDie(const string& param_file, + NetParameter* param); + } // namespace caffe #endif // CAFFE_UTIL_UPGRADE_PROTO_H_ diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 060d0000e51..a80abc6c029 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -27,7 +27,7 @@ Net::Net(const NetParameter& param) { template Net::Net(const string& param_file) { NetParameter param; - ReadParamsFromTextFile(param_file, ¶m); + ReadNetParamsFromTextFileOrDie(param_file, ¶m); Init(param); } @@ -302,58 +302,10 @@ void Net::CopyTrainedLayersFrom(const NetParameter& param) { template void Net::CopyTrainedLayersFrom(const string trained_filename) { NetParameter param; - ReadParamsFromBinaryFile(trained_filename, ¶m); + ReadNetParamsFromBinaryFileOrDie(trained_filename, ¶m); CopyTrainedLayersFrom(param); } -template -void Net::ReadParamsFromTextFile(const string& param_file, - NetParameter* param) { - CHECK(ReadProtoFromTextFile(param_file, param)) - << "Failed to parse NetParameter file: " << param_file; - if (NetNeedsUpgrade(*param)) { - // NetParameter was specified using the old style (V0LayerParameter); try to - // upgrade it. - LOG(ERROR) << "Attempting to upgrade input file specified using deprecated " - << "V0LayerParameter: " << param_file; - NetParameter original_param(*param); - if (!UpgradeV0Net(original_param, param)) { - LOG(ERROR) << "Warning: had one or more problems upgrading " - << "V0NetParameter to NetParameter (see above); continuing anyway."; - } else { - LOG(INFO) << "Successfully upgraded file specified using deprecated " - << "V0LayerParameter"; - } - LOG(ERROR) << "Note that future Caffe releases will not support " - << "V0NetParameter; use ./build/tools/upgrade_net_proto_text.bin to " - << "upgrade this and any other network proto files to the new format."; - } -} - -template -void Net::ReadParamsFromBinaryFile(const string& param_file, - NetParameter* param) { - CHECK(ReadProtoFromBinaryFile(param_file, param)) - << "Failed to parse NetParameter file: " << param_file; - if (NetNeedsUpgrade(*param)) { - // NetParameter was specified using the old style (V0LayerParameter); try to - // upgrade it. - LOG(ERROR) << "Attempting to upgrade input file specified using deprecated " - << "V0LayerParameter: " << param_file; - NetParameter original_param(*param); - if (!UpgradeV0Net(original_param, param)) { - LOG(ERROR) << "Warning: had one or more problems upgrading " - << "V0NetParameter to NetParameter (see above); continuing anyway."; - } else { - LOG(INFO) << "Successfully upgraded file specified using deprecated " - << "V0LayerParameter"; - } - LOG(ERROR) << "Note that future Caffe releases will not support " - << "V0NetParameter; use ./build/tools/upgrade_net_proto_binary.bin to " - << "upgrade this and any other network proto files to the new format."; - } -} - template void Net::ToProto(NetParameter* param, bool write_diff) { param->Clear(); diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index 306f960ff5f..e079b422dfb 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -8,6 +8,7 @@ #include #include "caffe/common.hpp" +#include "caffe/util/io.hpp" #include "caffe/util/upgrade_proto.hpp" #include "caffe/proto/caffe.pb.h" @@ -565,4 +566,50 @@ void NetParameterToPrettyPrint(const NetParameter& param, } } +void ReadNetParamsFromTextFileOrDie(const string& param_file, + NetParameter* param) { + CHECK(ReadProtoFromTextFile(param_file, param)) + << "Failed to parse NetParameter file: " << param_file; + if (NetNeedsUpgrade(*param)) { + // NetParameter was specified using the old style (V0LayerParameter); try to + // upgrade it. + LOG(ERROR) << "Attempting to upgrade input file specified using deprecated " + << "V0LayerParameter: " << param_file; + NetParameter original_param(*param); + if (!UpgradeV0Net(original_param, param)) { + LOG(ERROR) << "Warning: had one or more problems upgrading " + << "V0NetParameter to NetParameter (see above); continuing anyway."; + } else { + LOG(INFO) << "Successfully upgraded file specified using deprecated " + << "V0LayerParameter"; + } + LOG(ERROR) << "Note that future Caffe releases will not support " + << "V0NetParameter; use ./build/tools/upgrade_net_proto_text.bin to " + << "upgrade this and any other network proto files to the new format."; + } +} + +void ReadNetParamsFromBinaryFileOrDie(const string& param_file, + NetParameter* param) { + CHECK(ReadProtoFromBinaryFile(param_file, param)) + << "Failed to parse NetParameter file: " << param_file; + if (NetNeedsUpgrade(*param)) { + // NetParameter was specified using the old style (V0LayerParameter); try to + // upgrade it. + LOG(ERROR) << "Attempting to upgrade input file specified using deprecated " + << "V0LayerParameter: " << param_file; + NetParameter original_param(*param); + if (!UpgradeV0Net(original_param, param)) { + LOG(ERROR) << "Warning: had one or more problems upgrading " + << "V0NetParameter to NetParameter (see above); continuing anyway."; + } else { + LOG(INFO) << "Successfully upgraded file specified using deprecated " + << "V0LayerParameter"; + } + LOG(ERROR) << "Note that future Caffe releases will not support " + << "V0NetParameter; use ./build/tools/upgrade_net_proto_binary.bin to " + << "upgrade this and any other network proto files to the new format."; + } +} + } // namespace caffe From d821cafc593bbc82afb9e1ced1d71378a943fec6 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 28 Mar 2014 00:21:26 -0700 Subject: [PATCH 60/60] rename test_innerproduct_layer to test_inner_product_layer --- .../{test_innerproduct_layer.cpp => test_inner_product_layer.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/caffe/test/{test_innerproduct_layer.cpp => test_inner_product_layer.cpp} (100%) diff --git a/src/caffe/test/test_innerproduct_layer.cpp b/src/caffe/test/test_inner_product_layer.cpp similarity index 100% rename from src/caffe/test/test_innerproduct_layer.cpp rename to src/caffe/test/test_inner_product_layer.cpp