Quellcode durchsuchen

add weight params

ika vor 7 Jahren
Commit
a11d161d96
6 geänderte Dateien mit 125 neuen und 0 gelöschten Zeilen
  1. 4 0
      .gitignore
  2. 0 0
      bias.h
  3. 13 0
      main.cc
  4. 11 0
      main.py
  5. 97 0
      util.py
  6. 0 0
      weight.h

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+.vscode
+
+data
+

Datei-Diff unterdrückt, da er zu groß ist
+ 0 - 0
bias.h


+ 13 - 0
main.cc

@@ -0,0 +1,13 @@
+#include <iostream>
+
+#include "bias.h"
+#include "weight.h"
+
+using std::cout; using std::endl;
+
+
+int main(int argc, char const *argv[])
+{
+  cout << weight6[128] << bia3[0] << endl;
+  return 0;
+}

+ 11 - 0
main.py

@@ -0,0 +1,11 @@
+# coding: utf-8
+
+import paddle.v2 as paddle
+
+
+def main():
+    pass
+
+
+if __name__ == '__main__':
+    main()

+ 97 - 0
util.py

@@ -0,0 +1,97 @@
+# coding: utf-8
+
+import gzip
+import os
+import shutil
+
+import numpy as np
+import paddle.v2 as paddle
+
+
+def extract_param(path):
+    """
+    Return the parameters of weight and bias
+    """
+    with gzip.open(path) as f:
+        p = paddle.parameters.Parameters.from_tar(f)
+    names = p.names()
+    weight, bias = [], []
+    for name in names:
+        if name.endswith('bias'):
+            bias.append(p.get(name))
+        else:
+            weight.append(p.get(name))
+    return weight, bias
+
+
+def param2header(param, path, dtype, var_name):
+    """
+    Trans the list of 2d np array into C++ header file
+    """
+    with open(path, 'w') as f:
+        postfix = 1
+        for matrix in param:
+            rows = len(matrix)
+            cols = len(matrix[0])
+            beg = 'static {0} {1}{2}[{3}]={{'.format(dtype, var_name, postfix, rows * cols)
+            end = '};\n'
+            f.write(beg)
+            for i in range(rows):
+                for j in range(cols):
+                    if i == 0 and j == 0:
+                        f.write('{:.6f}'.format(matrix[i][j]))
+                    else:
+                        f.write(',{:.6f}'.format(matrix[i][j]))
+                f.write('\n')
+            f.write(end)
+            postfix += 1
+        # layers = len(param)
+        # postfix = 1
+        # beg = 'static {0} **{1} = {0}*[{2}]{{{1}{3}'.format(dtype, var_name, layers, postfix)
+        # f.write(beg)
+        # while postfix < layers:
+        #     postfix += 1
+        #     f.write(', {0}{1}'.format(var_name, postfix))
+        # f.write(end)
+
+
+def cp_gtsrb_img(src, dst):
+    """
+    Copy all GTSRB images from src to dst, just like:
+    src/000xx/000yy_000zz.ppm -> dst/000xx_000yy_000zz.ppm
+    """
+    folders = os.listdir(src)
+    for folder in folders:
+        imgs = filter(lambda fname: fname.endswith('ppm'),
+                      os.listdir(src + '/' + folder))
+        for img in imgs:
+            shutil.copy(src + '/' + folder + '/' + img,
+                        dst + '/' + folder + '_' + img)
+
+
+def generate_gtsrb_dict(dir):
+    imgs = os.listdir(dir)
+    gtsrb_dict = {}
+    for img in imgs:
+        gtsrb_dict[img] = int(img.split('_')[0])
+    return gtsrb_dict
+
+
+if __name__ == '__main__':
+    gtsrb_img_path = 'data/GTSRB/Final_Training/Images'
+    gtsrb_cp_dst = 'data/GTSRB/Final_Training/All'
+
+    param_path = 'data/params_pass_1900.tar.gz'
+    weight, bias = extract_param(param_path)
+
+    for w in weight:
+        print w.shape
+
+    for b in bias:
+        print b.shape
+
+    param2header(weight, 'weight.h', 'float', 'weight')
+    param2header(bias, 'bias.h', 'float', 'bias')
+
+    # cp_gtsrb_img(gtsrb_img_path, gtsrb_cp_dst)
+    # generate_gtsrb_dict(gtsrb_cp_dst)

Datei-Diff unterdrückt, da er zu groß ist
+ 0 - 0
weight.h


Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.