You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@singa.apache.org by wa...@apache.org on 2017/08/04 08:32:47 UTC

[03/15] incubator-singa git commit: SINGA-290 Upgrade to Python 3

SINGA-290 Upgrade to Python 3


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

Branch: refs/heads/master
Commit: 14f0d8cdf6b85928681e434fb1984615b773c3c0
Parents: e5c438c
Author: Moaz Reyad <mo...@gmail.com>
Authored: Sat May 27 14:32:31 2017 +0800
Committer: Wei Wang <wa...@comp.nus.edu.sg>
Committed: Thu Aug 3 18:15:59 2017 +0800

----------------------------------------------------------------------
 CMakeLists.txt               | 38 +++++++++++++++++++++++++++-----------
 cmake/Dependencies.cmake     | 11 ++++++++---
 python/setup.py.in           |  6 ++++--
 python/singa/command.py      | 24 ++++++++++++++----------
 python/singa/converter.py    | 12 ++++++++----
 python/singa/data.py         | 14 +++++++++-----
 python/singa/device.py       |  1 +
 python/singa/image_tool.py   | 24 ++++++++++++++----------
 python/singa/initializer.py  |  8 +++++---
 python/singa/layer.py        |  9 +++++++--
 python/singa/loss.py         |  8 ++++++--
 python/singa/metric.py       | 11 ++++++++---
 python/singa/net.py          | 27 +++++++++++++++++----------
 python/singa/optimizer.py    |  8 ++++++--
 python/singa/snapshot.py     |  5 ++++-
 python/singa/tensor.py       | 15 ++++++++++-----
 tool/debian-python2/postinst | 21 +++++++++++++++++++++
 17 files changed, 169 insertions(+), 73 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f71ce69..0522f61 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -122,13 +122,13 @@ IF (USE_MODULES)
     #ENDIF()
     ExternalProject_Add(protobuf
         DOWNLOAD_COMMAND "wget"
-        "http://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz"
-        "-O" "protobuf-2.6.1.tar.gz"
+        "https://github.com/google/protobuf/archive/v3.3.0.tar.gz"
+        "-O" "protobuf-3.3.0.tar.gz"
         UPDATE_COMMAND "tar" "zxvf"
-        "${CMAKE_BINARY_DIR}/protobuf-prefix/src/protobuf-2.6.1.tar.gz" "-C" ".."
-        SOURCE_DIR "protobuf-2.6.1/"
+        "${CMAKE_BINARY_DIR}/protobuf-prefix/src/protobuf-3.3.0.tar.gz" "-C" ".."
+        SOURCE_DIR "protobuf-3.3.0/"
         BUILD_IN_SOURCE 1
-        CONFIGURE_COMMAND "./configure" "--disable-shared"
+        CONFIGURE_COMMAND "./autogen.sh" COMMAND "./configure" "--disable-shared"
         "--prefix=${CMAKE_BINARY_DIR}/" "CXXFLAGS=-fPIC"
         INSTALL_COMMAND "make" "install"
         )
@@ -192,7 +192,11 @@ IF(PACKAGE)
 		SET(CORE_DEPENDENCIES "libgoogle-glog-dev, libprotobuf-dev, libopenblas-dev, libstdc++6, libc6")
 	ENDIF()
 
-	SET(PYTHON_DEPENDENCIES "${CORE_DEPENDENCIES}, python-dev, libpython2.7, python-pip, python-numpy, python-pillow")
+	IF(PYTHON2)
+		SET(PYTHON_DEPENDENCIES "${CORE_DEPENDENCIES}, python-dev, libpython2.7, python-pip, python-numpy, python-pillow")
+	ELSE()
+		SET(PYTHON_DEPENDENCIES "${CORE_DEPENDENCIES}, python3, python3-dev, python3-pip, python3-numpy, python3-pillow")
+	ENDIF()
 
 	SET(CPACK_GENERATOR "DEB")
 	SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "Apache Incubator <de...@singa.incubator.apache.org>")
@@ -206,12 +210,24 @@ IF(PACKAGE)
 		SET(CPACK_DEBIAN_PACKAGE_DEPENDS ${PYTHON_DEPENDENCIES})
 		SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${PROJECT_SOURCE_DIR}/tool/debian/postinst" )
 		SET(CPACK_DEBIAN_PACKAGE_PREDEPENDS "ca-certificates")
-		IF (USE_CUDA)
-			SET(CPACK_DEBIAN_PACKAGE_NAME "python-singa-cuda")
-			SET(CPACK_PACKAGE_FILE_NAME "python-singa-cuda-${PACKAGE_VERSION}")
+		IF(PYTHON2)
+			SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${PROJECT_SOURCE_DIR}/tool/debian-python2/postinst" )
+			IF (USE_CUDA)
+				SET(CPACK_DEBIAN_PACKAGE_NAME "python-singa-cuda")
+				SET(CPACK_PACKAGE_FILE_NAME "python-singa-cuda-${PACKAGE_VERSION}")
+			ELSE()
+				SET(CPACK_DEBIAN_PACKAGE_NAME "python-singa")
+				SET(CPACK_PACKAGE_FILE_NAME "python-singa-${PACKAGE_VERSION}")
+			ENDIF()
 		ELSE()
-			SET(CPACK_DEBIAN_PACKAGE_NAME "python-singa")
-			SET(CPACK_PACKAGE_FILE_NAME "python-singa-${PACKAGE_VERSION}")
+			SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${PROJECT_SOURCE_DIR}/tool/debian/postinst" )
+			IF (USE_CUDA)
+				SET(CPACK_DEBIAN_PACKAGE_NAME "python3-singa-cuda")
+				SET(CPACK_PACKAGE_FILE_NAME "python3-singa-cuda-${PACKAGE_VERSION}")
+			ELSE()
+				SET(CPACK_DEBIAN_PACKAGE_NAME "python3-singa")
+				SET(CPACK_PACKAGE_FILE_NAME "python3-singa-${PACKAGE_VERSION}")
+			ENDIF()
 		ENDIF()
 	ELSE()
 		SET(CPACK_DEBIAN_PACKAGE_NAME "singa")

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/cmake/Dependencies.cmake
----------------------------------------------------------------------
diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake
index 83c5187..aa3c090 100644
--- a/cmake/Dependencies.cmake
+++ b/cmake/Dependencies.cmake
@@ -51,7 +51,7 @@ IF(USE_MODULES)
     #ENDIF()
     #ENDIF()
 ELSE()
-    FIND_PACKAGE( Protobuf REQUIRED )
+    FIND_PACKAGE( Protobuf 3.0 REQUIRED )
     #MESSAGE(STATUS "proto libs " ${PROTOBUF_LIBRARY})
     LIST(APPEND SINGA_LINKER_LIBS ${PROTOBUF_LIBRARY})
     #IF(USE_CBLAS)
@@ -124,8 +124,13 @@ ENDIF()
 #MESSAGE(STATUS "link lib : " ${SINGA_LINKER_LIBS})
 
 IF(USE_PYTHON)
-    FIND_PACKAGE(PythonLibs 2.7 REQUIRED)
-    FIND_PACKAGE(PythonInterp 2.7 REQUIRED)
+    IF(PYTHON2)
+        FIND_PACKAGE(PythonLibs 2.7 REQUIRED)
+        FIND_PACKAGE(PythonInterp 2.7 REQUIRED)
+    ELSE()
+        FIND_PACKAGE(PythonLibs 3.0 REQUIRED)
+        FIND_PACKAGE(PythonInterp 3.0 REQUIRED)
+    ENDIF()
     FIND_PACKAGE(SWIG 3.0 REQUIRED)
 ENDIF()
 

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/setup.py.in
----------------------------------------------------------------------
diff --git a/python/setup.py.in b/python/setup.py.in
index 6b3e5b5..044710f 100644
--- a/python/setup.py.in
+++ b/python/setup.py.in
@@ -50,6 +50,7 @@ setup(
         'Programming Language :: Python :: 2',
         'Programming Language :: Python :: 2.6',
         'Programming Language :: Python :: 2.7',
+        'Programming Language :: Python :: 3',
         ],
 
     keywords='deep learning singa apache',
@@ -60,11 +61,12 @@ setup(
 
     install_requires=[
         'numpy>=1.11.0',
-        'protobuf>=2.5.0',
+        'protobuf>=3',
         'unittest-xml-reporting',
         'flask>=0.10.1',
         'flask_cors>=3.0.2',
-        'pillow>=2.3.0'
+        'pillow>=2.3.0',
+        'future'
         ],
 
     #List additional groups of dependencies here (e.g. development

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/command.py
----------------------------------------------------------------------
diff --git a/python/singa/command.py b/python/singa/command.py
index f14c8c5..90c51cc 100644
--- a/python/singa/command.py
+++ b/python/singa/command.py
@@ -21,12 +21,16 @@ This script is the main entrance for user to run singa inside a model workspace
 
 To use this script, user sudo install these dependencies: flask pillow and protobuf
 '''
+from __future__ import print_function
 
+from future import standard_library
+standard_library.install_aliases()
+from builtins import str
 import sys, glob, os, random, shutil, time
 from flask import Flask, request, redirect, url_for
 import numpy as np
-import ConfigParser
-import urllib, traceback
+import configparser
+import urllib.request, urllib.parse, urllib.error, traceback
 
 
 from argparse import ArgumentParser
@@ -42,7 +46,7 @@ welcome to singa
 '''
 
 app = Flask(__name__)
-config = ConfigParser.RawConfigParser()
+config = configparser.RawConfigParser()
 service = {}
 data_path = "data_"
 parameter_path = "parameter_"
@@ -123,19 +127,19 @@ USAGE
         parameter_file=get_parameter(parameter_file)
 
         if parameter_file:
-            print "load parameter file: %s" % parameter_file
+            print("load parameter file: %s" % parameter_file)
             model.load(parameter_file)
 
         if use_cpu:
             raise CLIError("Currently cpu is not support!")
         else:
-            print "runing with gpu"
+            print("runing with gpu")
             d = device.create_cuda_gpu()
 
         model.to_device(d)
 
         if mode == "serve":
-            print "runing singa in serve mode, listen to  port: %s " % port
+            print("runing singa in serve mode, listen to  port: %s " % port)
             global service
             from serve import Service
             service =Service(model,d)
@@ -143,7 +147,7 @@ USAGE
             app.debug = debug
             app.run(host='0.0.0.0', port= port)
         elif mode == "train":
-            print "runing singa in train mode"
+            print("runing singa in train mode")
             global trainer
             from train import Trainer
             trainer= Trainer(model,d)
@@ -156,7 +160,7 @@ USAGE
     except KeyboardInterrupt:
         ### handle keyboard interrupt ###
         return 0
-    except Exception, e:
+    except Exception as e:
         if debug:
             traceback.print_exc()
             raise(e)
@@ -172,7 +176,7 @@ def file_prepare(reload_data=False):
     if not reload_data and os.path.exists("data_.py"):
         return
 
-    print "download file"
+    print("download file")
     #clean data
     shutil.rmtree("data_.py",ignore_errors=True)
     shutil.rmtree("data_",ignore_errors=True)
@@ -203,7 +207,7 @@ def download_file(name,path,dest):
     if (path.startswith('http')):
         file_name = path.split('/')[-1]
         target = os.path.join(dest,file_name)
-        urllib.urlretrieve(path,target)
+        urllib.request.urlretrieve(path,target)
     return name,target
 
 

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/converter.py
----------------------------------------------------------------------
diff --git a/python/singa/converter.py b/python/singa/converter.py
index cc42ef0..dc195f2 100644
--- a/python/singa/converter.py
+++ b/python/singa/converter.py
@@ -15,6 +15,10 @@
 # limitations under the License.
 # =============================================================================
 
+from __future__ import print_function
+from builtins import str
+from builtins import range
+from builtins import object
 from google.protobuf import text_format
 from singa import layer
 from singa import metric
@@ -25,7 +29,7 @@ from .proto import caffe_pb2
 import numpy as np
 
 
-class CaffeConverter:
+class CaffeConverter(object):
 
     def __init__(self, net_proto, solver_proto = None, input_sample_shape =
             None, param_path = None):
@@ -158,9 +162,9 @@ class CaffeConverter:
                     layer.engine = self.convert_engine(layer_confs[i], 0)
                 lyr = layer.Layer(conf.name, conf)
                 if len(net.layers) == 0:
-                    print 'input sample shape: ', self.input_sample_shape
+                    print('input sample shape: ', self.input_sample_shape)
                     lyr.setup(self.input_sample_shape)
-                    print lyr.name, lyr.get_output_sample_shape()
+                    print(lyr.name, lyr.get_output_sample_shape())
                 if layer_confs[i].type == 'InnerProduct' or layer_confs[i].type == 14:
                     net.add(layer.Flatten('flat' + str(flatten_id)))
                     flatten_id += 1
@@ -226,4 +230,4 @@ class CaffeConverter:
                 i += 1
                 params[i].copy_from_numpy(bias)
                 i += 1
-                print 'converting layer {0}, wmat shape = {1}, bias shape = {2}'.format(layer.name, w.shape, bias.shape)
+                print('converting layer {0}, wmat shape = {1}, bias shape = {2}'.format(layer.name, w.shape, bias.shape))

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/data.py
----------------------------------------------------------------------
diff --git a/python/singa/data.py b/python/singa/data.py
index 492d218..753edbf 100644
--- a/python/singa/data.py
+++ b/python/singa/data.py
@@ -48,7 +48,11 @@ Example usage::
         img.save('img%d.png' % idx)
     data.end()
 '''
+from __future__ import print_function
+from __future__ import absolute_import
 
+from builtins import range
+from builtins import object
 import os
 import random
 import time
@@ -56,7 +60,7 @@ from multiprocessing import Process, Queue
 import numpy as np
 
 
-class ImageBatchIter:
+class ImageBatchIter(object):
     '''Utility for iterating over an image dataset to get mini-batches.
 
     Args:
@@ -97,7 +101,7 @@ class ImageBatchIter:
         self.p.start()
         return
 
-    def next(self):
+    def __next__(self):
         assert self.p is not None, 'call start before next'
         while self.queue.empty():
             time.sleep(0.1)
@@ -164,7 +168,7 @@ class ImageBatchIter:
 
 
 if __name__ == '__main__':
-    import image_tool
+    from . import image_tool
     from PIL import Image
     tool = image_tool.ImageTool()
 
@@ -179,8 +183,8 @@ if __name__ == '__main__':
                           image_folder='images/',
                           capacity=10)
     data.start()
-    imgs, labels = data.next()
-    print labels
+    imgs, labels = next(data)
+    print(labels)
     for idx in range(imgs.shape[0]):
         img = Image.fromarray(imgs[idx].astype(np.uint8).transpose(1, 2, 0),
                               'RGB')

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/device.py
----------------------------------------------------------------------
diff --git a/python/singa/device.py b/python/singa/device.py
index fdd2a92..a1625e2 100644
--- a/python/singa/device.py
+++ b/python/singa/device.py
@@ -22,6 +22,7 @@ to call singa::Device and its methods.
 TODO(wangwei) implement py CudaGPU class.
 '''
 
+from builtins import object
 from . import singa_wrap as singa
 
 

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/image_tool.py
----------------------------------------------------------------------
diff --git a/python/singa/image_tool.py b/python/singa/image_tool.py
index ed865f1..529f73b 100644
--- a/python/singa/image_tool.py
+++ b/python/singa/image_tool.py
@@ -28,7 +28,11 @@ Example usage::
         img.save('%d.png' % idx)
 
 '''
+from __future__ import division
 
+from builtins import range
+from builtins import object
+from past.utils import old_div
 import random
 import numpy as np
 from PIL import Image, ImageEnhance
@@ -69,7 +73,7 @@ def crop(img, patch, position):
     elif position == 'right_bottom':
         left, upper = img.size[0]-patch[0], img.size[1]-patch[1]
     elif position == 'center':
-        left, upper = (img.size[0]-patch[0])/2, (img.size[1]-patch[1])/2
+        left, upper = old_div((img.size[0]-patch[0]),2), old_div((img.size[1]-patch[1]),2)
     else:
         raise Exception('position is wrong')
 
@@ -92,8 +96,8 @@ def crop_and_resize(img, patch, position):
         left, upper = 0, 0
         right, bottom = size[1], size[1]
     elif position == 'center':
-        left, upper = (size[0]-size[1])/2, 0
-        right, bottom = (size[0]+size[1])/2, size[1]
+        left, upper = old_div((size[0]-size[1]),2), 0
+        right, bottom = old_div((size[0]+size[1]),2), size[1]
     elif position == 'right':
         left, upper = size[0]-size[1], 0
         right, bottom = size[0], size[1]
@@ -101,8 +105,8 @@ def crop_and_resize(img, patch, position):
         left, upper = 0, 0
         right, bottom = size[0], size[0]
     elif position == 'middle':
-        left, upper = 0, (size[1]-size[0])/2
-        right, bottom = size[0], (size[1]+size[0])/2
+        left, upper = 0, old_div((size[1]-size[0]),2)
+        right, bottom = size[0], old_div((size[1]+size[0]),2)
     elif position == 'bottom':
         left, upper = 0, size[1]-size[0]
         right, bottom = size[0], size[1]
@@ -192,10 +196,10 @@ def flip_down(img):
 
 
 def get_list_sample(l, sample_size):
-    return [l[i] for i in sorted(random.sample(xrange(len(l)), sample_size))]
+    return [l[i] for i in sorted(random.sample(range(len(l)), sample_size))]
 
 
-class ImageTool():
+class ImageTool(object):
     '''A tool for image augmentation.
 
     For operations with inplace=True, the returned value is the ImageTool
@@ -238,7 +242,7 @@ class ImageTool():
             rng: a tuple (begin,end), include begin, exclude end
             inplace: inplace imgs or not ( return new_imgs)
         '''
-        size_list = range(rng[0], rng[1])
+        size_list = list(range(rng[0], rng[1]))
         return self.resize_by_list(size_list, 1, inplace)
 
     def resize_by_list(self, size_list, num_case=1, inplace=True):
@@ -273,7 +277,7 @@ class ImageTool():
             rng: a tuple (begin,end) in degree, include begin, exclude end
             inplace: inplace imgs or not ( return new_imgs)
         '''
-        angle_list = range(rng[0], rng[1])
+        angle_list = list(range(rng[0], rng[1]))
         return self.rotate_by_list(angle_list, 1, inplace)
 
     def rotate_by_list(self, angle_list, num_case=1, inplace=True):
@@ -391,7 +395,7 @@ class ImageTool():
         if num_case == patch5 + patch3:
             count = patch5
         else:
-            sample_list = range(0, patch5 + patch3)
+            sample_list = list(range(0, patch5 + patch3))
             samples = get_list_sample(sample_list, num_case)
             count = 0
             for s in samples:

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/initializer.py
----------------------------------------------------------------------
diff --git a/python/singa/initializer.py b/python/singa/initializer.py
index fb99663..5d5f31e 100644
--- a/python/singa/initializer.py
+++ b/python/singa/initializer.py
@@ -26,7 +26,9 @@ Example usages::
     initializer.uniform(x, 3, 5) # use both fan_in and fan_out
     initializer.uniform(x, 3, 0)  # use only fan_in
 '''
+from __future__ import division
 
+from past.utils import old_div
 import math
 
 
@@ -89,7 +91,7 @@ def xavier(t):
         t (Tensor): the parater tensor
     '''
 
-    scale = math.sqrt(6.0 / (t.shape[0] + t.shape[1]))
+    scale = math.sqrt(old_div(6.0, (t.shape[0] + t.shape[1])))
     t.uniform(-scale, scale)
 
 
@@ -102,7 +104,7 @@ def glorot(t):
     Args:
         t (Tensor): the parater tensor
     '''
-    scale = math.sqrt(2.0 / (t.shape[0] + t.shape[1]))
+    scale = math.sqrt(old_div(2.0, (t.shape[0] + t.shape[1])))
     t.gaussian(0, 1)
     t *= scale
 
@@ -119,4 +121,4 @@ def msra(t):
     Args:
         t (Tensor): the parater tensor
     '''
-    t.gaussian(0, math.sqrt(2.0 / t.shape[0]))
+    t.gaussian(0, math.sqrt(old_div(2.0, t.shape[0])))

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/layer.py
----------------------------------------------------------------------
diff --git a/python/singa/layer.py b/python/singa/layer.py
index 026a766..2cdfe69 100644
--- a/python/singa/layer.py
+++ b/python/singa/layer.py
@@ -44,11 +44,16 @@ Example usages::
     # dp is a list of tensors for parameter gradients
     dx, dp = conv.backward(kTrain, dy)
 """
+from __future__ import division
+from __future__ import absolute_import
 
+from builtins import str
+from builtins import range
+from builtins import object
 from sets import Set
 from . import singa_wrap
 from .proto import model_pb2
-import tensor
+from . import tensor
 
 
 engine = 'cudnn'
@@ -1217,7 +1222,7 @@ def _set_kernel_stride_pad(conf, kernel, stride, border_mode, pad, in_shape):
                      0)
             assert ph % 2 == 0 and pw % 2 == 0, 'ph=%d and pw=%d are not even' \
                 % (ph, pw)
-            pad = (ph / 2, pw / 2)
+            pad = (ph // 2, pw // 2)
         elif mode == 'valid':
             pad = (0, 0)
         else:

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/loss.py
----------------------------------------------------------------------
diff --git a/python/singa/loss.py b/python/singa/loss.py
index 7fd3d77..d643218 100644
--- a/python/singa/loss.py
+++ b/python/singa/loss.py
@@ -34,11 +34,15 @@ Example usage::
     l = f.forward(True, x, y)  # l is tensor with 3 loss values
     g = f.backward()  # g is a tensor containing all gradients of x w.r.t l
 '''
+from __future__ import division
+from __future__ import absolute_import
 
 
+from past.utils import old_div
+from builtins import object
 from . import singa_wrap as singa
 from proto import model_pb2
-import tensor
+from . import tensor
 import numpy as np
 
 
@@ -210,4 +214,4 @@ class SquaredError(Loss):
         Returns:
             a float value as the averaged error
         '''
-        return tensor.sum(tensor.square(x - y) * 0.5) / x.size()
+        return old_div(tensor.sum(tensor.square(x - y) * 0.5), x.size())

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/metric.py
----------------------------------------------------------------------
diff --git a/python/singa/metric.py b/python/singa/metric.py
index 64a1b72..8495c0c 100644
--- a/python/singa/metric.py
+++ b/python/singa/metric.py
@@ -34,10 +34,15 @@ Example usage::
     acc = f.evaluate(x, y)  # averaged accuracy over all 3 samples in x
 
 '''
+from __future__ import division
+from __future__ import absolute_import
 
 
+from builtins import range
+from past.utils import old_div
+from builtins import object
 from . import singa_wrap as singa
-import tensor
+from . import tensor
 import numpy as np
 
 
@@ -129,7 +134,7 @@ class Precision(Metric):
 
             #Num of common labels among prediction and groundtruth
             num_intersect = np.intersect1d(pred_np[i], label_np).size
-            prcs_np[i] = num_intersect / float(self.top_k)
+            prcs_np[i] = old_div(num_intersect, float(self.top_k))
 
         precision = tensor.from_numpy(prcs_np)
 
@@ -192,7 +197,7 @@ class Recall(Metric):
 
             #Num of common labels among prediction and groundtruth
             num_intersect = np.intersect1d(pred_np[i], label_np).size
-            recall_np[i] = float(num_intersect) / label_np.size
+            recall_np[i] = old_div(float(num_intersect), label_np.size)
 
         recall = tensor.from_numpy(recall_np)
 

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/net.py
----------------------------------------------------------------------
diff --git a/python/singa/net.py b/python/singa/net.py
index faaef2b..c49b9fa 100644
--- a/python/singa/net.py
+++ b/python/singa/net.py
@@ -53,13 +53,20 @@ Example usages::
     y = net.predict(x)
     print tensor.to_numpy(y)
 """
-
+from __future__ import print_function
+from __future__ import absolute_import
+
+from future import standard_library
+standard_library.install_aliases()
+from builtins import zip
+from builtins import str
+from builtins import object
 from .proto.model_pb2 import kTrain, kEval
-from __init__ import __version__
-import tensor
-import layer
-import snapshot
-import cPickle as pickle
+from .__init__ import __version__
+from . import tensor
+from . import layer
+from . import snapshot
+import pickle as pickle
 
 import os
 
@@ -137,7 +144,7 @@ class FeedForwardNet(object):
         else:
             self.out_sample_shape_of_layer[lyr.name] = [out_shape]
         self.layers.append(lyr)
-        print(lyr.name, out_shape)
+        print((lyr.name, out_shape))
         return lyr
 
     def param_values(self):
@@ -334,7 +341,7 @@ class FeedForwardNet(object):
         # print output_of_layer
         ret.update(output_of_layer)
         if len(ret) == 1:
-            return ret.values()[0]
+            return list(ret.values())[0]
         else:
             return ret
 
@@ -494,6 +501,6 @@ class FeedForwardNet(object):
                     val.copy_from_numpy(params[name])
             except AssertionError as err:
                 print('Error from copying values for param: %s' % name)
-                print('shape of param vs checkpoint',
-                      val.shape, params[name].shape)
+                print(('shape of param vs checkpoint',
+                      val.shape, params[name].shape))
                 raise err

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/optimizer.py
----------------------------------------------------------------------
diff --git a/python/singa/optimizer.py b/python/singa/optimizer.py
index 614fe6d..8f1fe8e 100644
--- a/python/singa/optimizer.py
+++ b/python/singa/optimizer.py
@@ -31,10 +31,14 @@ Example usage::
   sgd.apply(1, g, p, 'param')  # use the global lr=0.1 for epoch 1
   sgd.apply_with_lr(2, 0.03, g, p, 'param')  # use lr=0.03 for epoch 2
 '''
+from __future__ import division
+from __future__ import absolute_import
 
+from past.utils import old_div
+from builtins import object
 import math
 from . import singa_wrap as singa
-import tensor
+from . import tensor
 from proto import model_pb2
 
 
@@ -434,5 +438,5 @@ class L2Constraint(Constraint):
 
     def apply(self, epoch, value, grad, step=-1):
         nrm = grad.l2()
-        grad *= self.threshold / nrm
+        grad *= old_div(self.threshold, nrm)
         return grad

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/snapshot.py
----------------------------------------------------------------------
diff --git a/python/singa/snapshot.py b/python/singa/snapshot.py
index 3e1298f..4c359fc 100644
--- a/python/singa/snapshot.py
+++ b/python/singa/snapshot.py
@@ -29,9 +29,12 @@ Example usages::
     for k, v in params.iteritems():
         sn2.write(k, v)
 '''
+from __future__ import absolute_import
 
+from builtins import str
+from builtins import object
 from . import singa_wrap as singa
-import tensor
+from . import tensor
 
 
 class Snapshot(object):

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/python/singa/tensor.py
----------------------------------------------------------------------
diff --git a/python/singa/tensor.py b/python/singa/tensor.py
index d36abdc..fabd84a 100644
--- a/python/singa/tensor.py
+++ b/python/singa/tensor.py
@@ -53,12 +53,17 @@ Tensor module functions
 
 Every Tesor instance must be initialized before reading data from it.
 """
+from __future__ import division
+from __future__ import print_function
+from __future__ import absolute_import
 
+from past.utils import old_div
+from builtins import object
 import numpy as np
 from functools import reduce
 from .proto import core_pb2
 from . import singa_wrap as singa
-import device as pydevice
+from . import device as pydevice
 
 int32 = core_pb2.kInt
 float32 = core_pb2.kFloat32
@@ -214,7 +219,7 @@ class Tensor(object):
         elif dt == np.int or dt == np.int32:
             self.singa_tensor.CopyIntDataFromHostPtr(np_array)
         else:
-            print 'Not implemented yet for ', dt
+            print('Not implemented yet for ', dt)
 
     def copy_data(self, t):
         '''Copy data from other Tensor instance.
@@ -584,7 +589,7 @@ def to_numpy(t):
     elif th.dtype == core_pb2.kInt:
         np_array = th.singa_tensor.GetIntValue(int(th.size()))
     else:
-        print 'Not implemented yet for ', th.dtype
+        print('Not implemented yet for ', th.dtype)
     return np_array.reshape(th.shape)
 
 
@@ -746,7 +751,7 @@ def average(t, axis=None):
     if t.ndim() > 1:
         return _call_singa_func(singa.Average, t.singa_tensor, axis)
     else:
-        return singa.SumAsFloat(t.singa_tensor) / t.size()
+        return old_div(singa.SumAsFloat(t.singa_tensor), t.size())
 
 
 def softmax(t, out=None):
@@ -933,7 +938,7 @@ def div(lhs, rhs, ret=None):
     '''
     if ret is None:
         # call Tensor.__div__()
-        return lhs / rhs
+        return old_div(lhs, rhs)
     else:
         if isinstance(rhs, Tensor):
             singa.Div(lhs.singa_tensor, rhs.singa_tensor, ret.singa_tensor)

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/14f0d8cd/tool/debian-python2/postinst
----------------------------------------------------------------------
diff --git a/tool/debian-python2/postinst b/tool/debian-python2/postinst
new file mode 100644
index 0000000..2d63734
--- /dev/null
+++ b/tool/debian-python2/postinst
@@ -0,0 +1,21 @@
+#!/bin/bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+pip install /usr/local/lib/singa/python
+rm -r /usr/local/lib/singa
+