-
Notifications
You must be signed in to change notification settings - Fork 7
Attempt porting to python 3. #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,39 +6,41 @@ cmake_minimum_required (VERSION 2.8) | |
| # | ||
| ###################################################################### | ||
|
|
||
| FIND_PACKAGE(PythonInterp 2 REQUIRED) | ||
|
|
||
| IF(PYTHONINTERP_FOUND) | ||
| # check that Python version 2.x is used | ||
| execute_process(COMMAND ${PYTHON_EXECUTABLE} -c | ||
| "import sys; print(sys.version[0])" | ||
| OUTPUT_VARIABLE PYTHON_MAJOR_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
| IF(NOT (${PYTHON_MAJOR_VERSION} EQUAL 2)) | ||
| MESSAGE(FATAL_ERROR "Python bindings require Python 2.x.") | ||
| ENDIF() | ||
| ENDIF() | ||
| FIND_PACKAGE(PythonInterp REQUIRED) | ||
|
|
||
| #IF(PYTHONINTERP_FOUND) | ||
| # # check that Python version 2.x is used | ||
| # execute_process(COMMAND ${PYTHON_EXECUTABLE} -c | ||
| # "import sys; print(sys.version[0])" | ||
| # OUTPUT_VARIABLE PYTHON_MAJOR_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
| # IF(NOT (${PYTHON_MAJOR_VERSION} EQUAL 2)) | ||
| # MESSAGE(FATAL_ERROR "Python bindings require Python 2.x.") | ||
| # ENDIF() | ||
| #ENDIF() | ||
|
|
||
| ###################################################################### | ||
| # | ||
| # find Python library | ||
| # | ||
| ###################################################################### | ||
|
|
||
| execute_process(COMMAND ${PYTHON_EXECUTABLE} -c | ||
| "import sys; print sys.exec_prefix" | ||
| OUTPUT_VARIABLE PYTHON_PREFIX OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
| find_package(PythonLibs REQUIRED) | ||
|
|
||
| IF(APPLE AND ${PYTHON_PREFIX} MATCHES ".*framework.*") | ||
| SET(PYTHON_LIBRARIES "${PYTHON_PREFIX}/Python" | ||
| CACHE FILEPATH "Python libraries" | ||
| FORCE) | ||
| ELSE() | ||
| execute_process(COMMAND ${PYTHON_EXECUTABLE} -c | ||
| "import sys; skip = 2 if sys.platform.startswith('win') else 1; print 'python' + sys.version[0:3:skip]" | ||
| OUTPUT_VARIABLE PYTHON_LIBRARY_NAME OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
| FIND_LIBRARY(PYTHON_LIBRARIES ${PYTHON_LIBRARY_NAME} HINTS "${PYTHON_PREFIX}" | ||
| PATH_SUFFIXES lib lib64 libs DOC "Python libraries") | ||
| ENDIF() | ||
| #execute_process(COMMAND ${PYTHON_EXECUTABLE} -c | ||
| # "import sys; print(sys.exec_prefix)" | ||
| # OUTPUT_VARIABLE PYTHON_PREFIX OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
|
|
||
| #IF(APPLE AND ${PYTHON_PREFIX} MATCHES ".*framework.*") | ||
| # SET(PYTHON_LIBRARIES "${PYTHON_PREFIX}/Python" | ||
| # CACHE FILEPATH "Python libraries" | ||
| # FORCE) | ||
| #ELSE() | ||
| # execute_process(COMMAND ${PYTHON_EXECUTABLE} -c | ||
| # "import sys; skip = 2 if sys.platform.startswith('win') else 1; print('python' + sys.version[0:3:skip])" | ||
| # OUTPUT_VARIABLE PYTHON_LIBRARY_NAME OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
| # FIND_LIBRARY(PYTHON_LIBRARIES ${PYTHON_LIBRARY_NAME} HINTS "${PYTHON_PREFIX}" | ||
| # PATH_SUFFIXES lib lib64 libs DOC "Python libraries") | ||
| #ENDIF() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same story regarding dropping. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add that while I haven't built this package before, I have had some rough experiences with CMake finding the right Python. If this works, great, but it might be worth checking with others who added this to verify it still works. |
||
|
|
||
| ###################################################################### | ||
| # | ||
|
|
@@ -47,7 +49,7 @@ ENDIF() | |
| ###################################################################### | ||
|
|
||
| execute_process(COMMAND ${PYTHON_EXECUTABLE} -c | ||
| "from distutils.sysconfig import *; print get_python_inc()" | ||
| "from distutils.sysconfig import *; print(get_python_inc())" | ||
| OUTPUT_VARIABLE PYTHON_INCLUDE OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
| SET(PYTHON_INCLUDE_PATH ${PYTHON_INCLUDE} | ||
| CACHE PATH "Path to Python include files" | ||
|
|
@@ -61,7 +63,7 @@ SET(PYTHON_INCLUDE_PATH ${PYTHON_INCLUDE} | |
| ###################################################################### | ||
| IF(NOT DEFINED IIBOOST_PYTHON_INSTALL_DIR OR IIBOOST_PYTHON_INSTALL_DIR MATCHES "^$") | ||
| execute_process(COMMAND ${PYTHON_EXECUTABLE} -c | ||
| "from distutils.sysconfig import *; print get_python_lib(1)" | ||
| "from distutils.sysconfig import *; print(get_python_lib(1))" | ||
| OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
| FILE(TO_CMAKE_PATH ${PYTHON_SITE_PACKAGES} IIBOOST_PYTHON_INSTALL_DIR) | ||
| ENDIF() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| from booster import Booster, EigenVectorsOfHessianImage, computeEigenVectorsOfHessianImage, computeIntegralImage, ROICoordinates | ||
| # for python 2.0 compatibility | ||
| from __future__ import absolute_import as _ai | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the aliasing? |
||
|
|
||
| from .booster import Booster, EigenVectorsOfHessianImage, computeEigenVectorsOfHessianImage, computeIntegralImage, ROICoordinates | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,8 @@ | |
| import numpy as np | ||
| import ctypes | ||
|
|
||
| from exceptions import RuntimeError | ||
| if sys.version_info[0] < 3: | ||
| from exceptions import RuntimeError | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this even need to be imported at all? FWICT this is already available on Python 2.7 and 2.6 without importing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also might be worth adding |
||
|
|
||
| # libiiboost_python.so must reside in the same directory as this module. | ||
| if sys.platform.startswith('win'): | ||
|
|
@@ -277,7 +278,7 @@ def trainWithChannels( self, imgStackList, eigVecOfHessianImgList, | |
|
|
||
| numStacks = len(chStackListList) | ||
| numChannels = len(chStackListList[0]) | ||
| print "Number of stacks: ",numStacks,". Each with ",numChannels," channels." | ||
| print("Number of stacks: ",numStacks,". Each with ",numChannels," channels.") | ||
|
|
||
| if debugOutput: | ||
| dbgOut = ctypes.c_int(1) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,19 +20,19 @@ def propToCArray( L, prop, cArrayElType ): | |
|
|
||
|
|
||
| # load data | ||
| print "--- Loading data ---" | ||
| print("--- Loading data ---") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth adding |
||
| gts = [joblib.load("../../testData/gt.jlb")] | ||
| imgs = [joblib.load("../../testData/img.jlb")] | ||
|
|
||
|
|
||
| print "--- Loading lib ---" | ||
| print("--- Loading lib ---") | ||
| boostLib = ctypes.CDLL("../../build/python/libiiboost_python.so") | ||
|
|
||
| # this returns a python string | ||
| boostLib.serializeModel.restype = ctypes.py_object | ||
|
|
||
|
|
||
| print "--- Calling train() ---" | ||
| print("--- Calling train() ---") | ||
|
|
||
| # we need to pass an array to the C call | ||
| widthList = propToCArray( imgs, "shape[2]", ctypes.c_int) | ||
|
|
@@ -53,13 +53,13 @@ def propToCArray( L, prop, cArrayElType ): | |
| ctypes.c_int(numStumps), | ||
| ctypes.c_int(debugOutput) ) ) | ||
|
|
||
| print "--- Serializing model ---" | ||
| print("--- Serializing model ---") | ||
| serStr = ctypes.py_object( boostLib.serializeModel( model ) ) | ||
|
|
||
| # pre-alloc prediction | ||
| pred = np.empty_like( imgs[0], dtype=np.dtype("float32") ) | ||
|
|
||
| print "--- Predicting ---" | ||
| print("--- Predicting ---") | ||
| boostLib.predict( model, | ||
| ctypes.c_void_p(imgs[0].ctypes.data), | ||
| widthList[0], heightList[0], depthList[0], | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this can just be dropped, right?