SINGA-253 Net converter for caffe model

Modification of pooling layer
  add a 'ceil' field in proto to be compatible with caffe
  train alexnet from caffe, achieving 82% accuracy.


Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/32b4368f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/32b4368f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/32b4368f

Branch: refs/heads/master
Commit: 32b4368f07b4c55880fa04bcc2d9f39ab9d75fc5
Parents: 77405ec
Author: Xiangrui <caix...@gmail.com>
Authored: Tue Oct 18 19:59:38 2016 +0800
Committer: Xiangrui <caix...@gmail.com>
Committed: Tue Oct 18 19:59:38 2016 +0800

----------------------------------------------------------------------
 examples/cifar10/caffe/caffe_net.py |  8 ++++----
 examples/cifar10/train.py           |  8 ++++----
 python/singa/layer.py               |  6 +++---
 src/model/layer/pooling.cc          | 21 ++++++++++++++++-----
 src/proto/model.proto               |  5 ++++-
 5 files changed, 31 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32b4368f/examples/cifar10/caffe/caffe_net.py
----------------------------------------------------------------------
diff --git a/examples/cifar10/caffe/caffe_net.py 
b/examples/cifar10/caffe/caffe_net.py
index 543e3a5..601fab2 100644
--- a/examples/cifar10/caffe/caffe_net.py
+++ b/examples/cifar10/caffe/caffe_net.py
@@ -23,10 +23,10 @@ def create_net(use_cpu):
     if use_cpu:
         layer.engine = 'singacpp'
 
-    #net_proto = os.path.abspath('./caffe/cifar10_full_train_test.prototxt')
-    #solver_proto = os.path.abspath('./caffe/cifar10_full_solver.prototxt')
-    net_proto = os.path.abspath('./caffe/cifar10_quick_train_test.prototxt')
-    solver_proto = os.path.abspath('./caffe/cifar10_quick_solver.prototxt')
+    net_proto = os.path.abspath('./caffe/cifar10_full_train_test.prototxt')
+    solver_proto = os.path.abspath('./caffe/cifar10_full_solver.prototxt')
+    #net_proto = os.path.abspath('./caffe/cifar10_quick_train_test.prototxt')
+    #solver_proto = os.path.abspath('./caffe/cifar10_quick_solver.prototxt')
     input_sample_shape = [3, 32, 32, ]
 
     cvt = converter.CaffeConverter(net_proto, solver_proto, input_sample_shape)

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32b4368f/examples/cifar10/train.py
----------------------------------------------------------------------
diff --git a/examples/cifar10/train.py b/examples/cifar10/train.py
index 6443cf4..d54d694 100644
--- a/examples/cifar10/train.py
+++ b/examples/cifar10/train.py
@@ -185,11 +185,11 @@ if __name__ == '__main__':
         train_x, test_x = normalize_for_alexnet(train_x, test_x)
         net = caffe_net.create_net(args.use_cpu)
         # for cifar10_full_train_test.prototxt
-        #train((train_x, train_y, test_x, test_y), net, 160, alexnet_lr, 0.004,
-        #      use_cpu=args.use_cpu)
-        # for cifar10_quick_train_test.prototxt
-        train((train_x, train_y, test_x, test_y), net, 18, caffe_lr, 0.004,
+        train((train_x, train_y, test_x, test_y), net, 160, alexnet_lr, 0.004,
               use_cpu=args.use_cpu)
+        # for cifar10_quick_train_test.prototxt
+        #train((train_x, train_y, test_x, test_y), net, 18, caffe_lr, 0.004,
+        #      use_cpu=args.use_cpu)
     elif args.model == 'alexnet':
         train_x, test_x = normalize_for_alexnet(train_x, test_x)
         net = alexnet.create_net(args.use_cpu)

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32b4368f/python/singa/layer.py
----------------------------------------------------------------------
diff --git a/python/singa/layer.py b/python/singa/layer.py
index e376bbf..c7f0ce8 100644
--- a/python/singa/layer.py
+++ b/python/singa/layer.py
@@ -102,8 +102,9 @@ class Layer(object):
                 self.param_specs.append(w)
                 self.param_specs.append(b)
                 #print 'conf:\n', conf
-            #if conf.type == 'Pooling':
-            #    print 'conf:\n', conf
+            if conf.type == 'Pooling':
+                conf.pooling_conf.ceil = True
+                #print 'conf:\n', conf
 
             elif (conf.type == 'ReLU' or conf.type == 18) or \
                 (conf.type == 'Sigmoid' or conf.type == 19) or \
@@ -888,7 +889,6 @@ def _create_layer(eng, layer):
         layers, use the specific activation mode, e.g. 'relu', 'tanh'.
     '''
     layer_type = eng + '_' + layer
-    print layer_type.lower()
     return singa_wrap.CreateLayer(layer_type.lower())
 
 

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32b4368f/src/model/layer/pooling.cc
----------------------------------------------------------------------
diff --git a/src/model/layer/pooling.cc b/src/model/layer/pooling.cc
index 2ddfc1c..75dc133 100644
--- a/src/model/layer/pooling.cc
+++ b/src/model/layer/pooling.cc
@@ -18,6 +18,8 @@
 
 #include "./pooling.h"
 #include "singa/model/layer.h"
+#include <cmath>
+
 namespace singa {
 
 RegisterLayerClass(singacpp_pooling, Pooling);
@@ -62,11 +64,20 @@ void Pooling::Setup(const Shape& in_sample, const 
LayerConf& conf) {
   height_ = in_sample.at(1);
   width_ = in_sample.at(2);
   pooled_height_ = 1;
-  if (stride_h_ > 0)
-    pooled_height_ =
-        static_cast<size_t>((height_ + 2 * pad_h_ - kernel_h_) / stride_h_) + 
1;
-  pooled_width_ =
-      static_cast<size_t>((width_ + 2 * pad_w_ - kernel_w_) / stride_w_) + 1;
+  if (pool_conf.ceil()) {
+    if (stride_h_ > 0)
+      pooled_height_ = static_cast<int>(ceil(static_cast<float>(
+              height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1;
+    pooled_width_ = static_cast<int>(ceil(static_cast<float>(
+        width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1;
+  }
+  else {
+    if (stride_h_ > 0)
+      pooled_height_ =
+          static_cast<size_t>((height_ + 2 * pad_h_ - kernel_h_) / stride_h_) 
+ 1;
+    pooled_width_ =
+        static_cast<size_t>((width_ + 2 * pad_w_ - kernel_w_) / stride_w_) + 1;
+  }
   out_sample_shape_ = vector<size_t>{channels_, pooled_height_, pooled_width_};
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32b4368f/src/proto/model.proto
----------------------------------------------------------------------
diff --git a/src/proto/model.proto b/src/proto/model.proto
index 641efb2..cfe946d 100644
--- a/src/proto/model.proto
+++ b/src/proto/model.proto
@@ -686,7 +686,10 @@ message PoolingConf {
   optional bool global_pooling = 12 [default = false];
   // whether to propagate nan
   optional bool nan_prop = 53 [default = false];
-}
+
+  // Added by xiangrui, 18 Oct, 2016
+  optional bool ceil = 60 [default = false];
+} 
 
 message PowerConf {
   // PowerLayer computes outputs y = (shift + scale * x) ^ power.

Reply via email to