export-boostdesc.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #!/usr/bin/python
  2. """
  3. /*********************************************************************
  4. * Software License Agreement (BSD License)
  5. *
  6. * Copyright (c) 2016
  7. *
  8. * Balint Cristian <cristian dot balint at gmail dot com>
  9. *
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials provided
  20. * with the distribution.
  21. * * Neither the name of the copyright holders nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *********************************************************************/
  38. /* export-boostdesc.py */
  39. /* Export C headers from binary data */
  40. // [http://infoscience.epfl.ch/record/186246/files/boostDesc_1.0.tar.gz]
  41. """
  42. import sys
  43. import struct
  44. def float_to_hex(f):
  45. return struct.unpack( '<I', struct.pack('<f', f) )[0]
  46. def main():
  47. # usage
  48. if ( len(sys.argv) < 3 ):
  49. print( "Usage: %s <binary-type (BGM, LBGM, BINBOOST)> <boostdesc-binary-filename>" % sys.argv[0] )
  50. sys.exit(0)
  51. if ( ( sys.argv[1] != "BGM" ) and
  52. ( sys.argv[1] != "LBGM" ) and
  53. ( sys.argv[1] != "BINBOOST" ) ):
  54. print( "Invalid type [%s]" % sys.argv[1] )
  55. sys.exit(0)
  56. # enum literals
  57. Assign = [ "ASSIGN_HARD",
  58. "ASSIGN_BILINEAR",
  59. "ASSIGN_SOFT",
  60. "ASSIGN_HARD_MAGN",
  61. "ASSIGN_SOFT_MAGN" ]
  62. # open binary data file
  63. f = open( sys.argv[2], 'rb' )
  64. # header
  65. print "/*"
  66. print " *"
  67. print " * Header exported from binary."
  68. print " * [%s %s %s]" % ( sys.argv[0], sys.argv[1], sys.argv[2] )
  69. print " *"
  70. print " */"
  71. # ini
  72. nDim = 1;
  73. nWLs = 0;
  74. # dimensionality (where is the case)
  75. if ( ( sys.argv[1] == "LBGM" ) or
  76. ( sys.argv[1] == "BINBOOST" ) ):
  77. nDim = struct.unpack( '<i', f.read(4) )[0]
  78. print
  79. print "// dimensionality of learner"
  80. print "static const int nDim = %i;" % nDim
  81. # week learners (where is the case)
  82. if ( sys.argv[1] != "BINBOOST" ):
  83. nWLs = struct.unpack( '<i', f.read(4) )[0]
  84. # common header
  85. orientQuant = struct.unpack( '<i', f.read(4) )[0]
  86. patchSize = struct.unpack( '<i', f.read(4) )[0]
  87. iGradAssignType = struct.unpack( '<i', f.read(4) )[0]
  88. print
  89. print "// orientations"
  90. print "static const int orientQuant = %i;" % orientQuant
  91. print
  92. print "// patch size"
  93. print "static const int patchSize = %i;" % patchSize
  94. print
  95. print "// gradient assignment type"
  96. print "static const int iGradAssignType = %s;" % Assign[iGradAssignType]
  97. arr_thresh = ""
  98. arr_orient = ""
  99. arr__y_min = ""
  100. arr__y_max = ""
  101. arr__x_min = ""
  102. arr__x_max = ""
  103. arr__alpha = ""
  104. arr___beta = ""
  105. dims = nDim
  106. if ( sys.argv[1] == "LBGM" ):
  107. dims = 1
  108. # iterate each dimension
  109. for d in range( 0, dims ):
  110. if ( sys.argv[1] == "BINBOOST" ):
  111. nWLs = struct.unpack( '<i', f.read(4) )[0]
  112. if ( d == 0 ):
  113. print
  114. print "// number of weak learners"
  115. print "static const int nWLs = %i;" % nWLs
  116. # iterate each members
  117. for i in range( 0, nWLs ):
  118. # unpack structure array
  119. thresh = struct.unpack( '<f', f.read(4) )[0]
  120. orient = struct.unpack( '<i', f.read(4) )[0]
  121. y_min = struct.unpack( '<i', f.read(4) )[0]
  122. y_max = struct.unpack( '<i', f.read(4) )[0]
  123. x_min = struct.unpack( '<i', f.read(4) )[0]
  124. x_max = struct.unpack( '<i', f.read(4) )[0]
  125. alpha = struct.unpack( '<f', f.read(4) )[0]
  126. beta = 0
  127. if ( sys.argv[1] == "BINBOOST" ):
  128. beta = struct.unpack( '<f', f.read(4) )[0]
  129. # first entry
  130. if ( d*dims + i == 0 ):
  131. arr_thresh += "\n"
  132. arr_thresh += "// threshold array (%s x %s)\n" % (dims,nWLs)
  133. arr_thresh += "static const unsigned int thresh[] =\n{\n"
  134. arr_orient += "\n"
  135. arr_orient += "// orientation array (%s x %s)\n" % (dims,nWLs)
  136. arr_orient += "static const int orient[] =\n{\n"
  137. arr__y_min += "\n"
  138. arr__y_min += "// Y min array (%s x %s)\n" % (dims,nWLs)
  139. arr__y_min += "static const int y_min[] =\n{\n"
  140. arr__y_max += "\n"
  141. arr__y_max += "// Y max array (%s x %s)\n" % (dims,nWLs)
  142. arr__y_max += "static const int y_max[] =\n{\n"
  143. arr__x_min += "\n"
  144. arr__x_min += "// X min array (%s x %s)\n" % (dims,nWLs)
  145. arr__x_min += "static const int x_min[] =\n{\n"
  146. arr__x_max += "\n"
  147. arr__x_max += "// X max array (%s x %s)\n" % (dims,nWLs)
  148. arr__x_max += "static const int x_max[] =\n{\n"
  149. arr__alpha += "\n"
  150. arr__alpha += "// alpha array (%s x %s)\n" % (dims,nWLs)
  151. arr__alpha += "static const unsigned int alpha[] =\n{\n"
  152. if ( sys.argv[1] == "BINBOOST" ):
  153. arr___beta += "\n"
  154. arr___beta += "// beta array (%s x %s)\n" % (dims,nWLs)
  155. arr___beta += "static const unsigned int beta[] =\n{\n"
  156. # last entry
  157. if ( i == nWLs - 1 ) and ( d == dims - 1):
  158. arr_thresh += " 0x%08x\n};" % float_to_hex(thresh)
  159. arr_orient += " 0x%02x\n};" % orient
  160. arr__y_min += " 0x%02x\n};" % y_min
  161. arr__y_max += " 0x%02x\n};" % y_max
  162. arr__x_min += " 0x%02x\n};" % x_min
  163. arr__x_max += " 0x%02x\n};" % x_max
  164. arr__alpha += " 0x%08x\n};" % float_to_hex(alpha)
  165. if ( sys.argv[1] == "BINBOOST" ):
  166. arr___beta += " 0x%08x\n};" % float_to_hex(beta)
  167. break
  168. # align entries
  169. if ( (d*dims + i + 1) % 8 ):
  170. arr_thresh += " 0x%08x," % float_to_hex(thresh)
  171. arr_orient += " 0x%02x," % orient
  172. arr__y_min += " 0x%02x," % y_min
  173. arr__y_max += " 0x%02x," % y_max
  174. arr__x_min += " 0x%02x," % x_min
  175. arr__x_max += " 0x%02x," % x_max
  176. arr__alpha += " 0x%08x," % float_to_hex(alpha)
  177. if ( sys.argv[1] == "BINBOOST" ):
  178. arr___beta += " 0x%08x," % float_to_hex(beta)
  179. else:
  180. arr_thresh += " 0x%08x,\n" % float_to_hex(thresh)
  181. arr_orient += " 0x%02x,\n" % orient
  182. arr__y_min += " 0x%02x,\n" % y_min
  183. arr__y_max += " 0x%02x,\n" % y_max
  184. arr__x_min += " 0x%02x,\n" % x_min
  185. arr__x_max += " 0x%02x,\n" % x_max
  186. arr__alpha += " 0x%08x,\n" % float_to_hex(alpha)
  187. if ( sys.argv[1] == "BINBOOST" ):
  188. arr___beta += " 0x%08x,\n" % float_to_hex(beta)
  189. # extra array (when LBGM)
  190. if ( sys.argv[1] == "LBGM" ):
  191. arr___beta += "\n"
  192. arr___beta += "// beta array (%s x %s)\n" % (nWLs,nDim)
  193. arr___beta += "static const unsigned int beta[] =\n{\n"
  194. for i in range( 0, nWLs ):
  195. for d in range( 0, nDim ):
  196. beta = struct.unpack( '<f', f.read(4) )[0]
  197. # last entry
  198. if ( i == nWLs-1 ) and ( d == nDim-1 ):
  199. arr___beta += " 0x%08x\n};" % float_to_hex(beta)
  200. break
  201. # align entries
  202. if ( (i*nDim + d + 1) % 8 ):
  203. arr___beta += " 0x%08x," % float_to_hex(beta)
  204. else:
  205. arr___beta += " 0x%08x,\n" % float_to_hex(beta)
  206. # release
  207. f.close()
  208. # dump on screen
  209. print arr_thresh
  210. print arr_orient
  211. print arr__y_min
  212. print arr__y_max
  213. print arr__x_min
  214. print arr__x_max
  215. print arr__alpha
  216. if ( ( sys.argv[1] == "LBGM" ) or
  217. ( sys.argv[1] == "BINBOOST" ) ):
  218. print arr___beta
  219. if __name__ == "__main__":
  220. main()