From 95e77200ba6ff983db3b8157ef546fccd9c83ae7 Mon Sep 17 00:00:00 2001
From: Hammouda Elbez <hammouda.elbez@univ-lille.fr>
Date: Fri, 2 Dec 2022 10:05:19 +0100
Subject: [PATCH] Added CSNN scripts

---
 scripts/Weight_extractor.cpp | 43 ++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 scripts/Weight_extractor.cpp

diff --git a/scripts/Weight_extractor.cpp b/scripts/Weight_extractor.cpp
new file mode 100644
index 0000000..3d23453
--- /dev/null
+++ b/scripts/Weight_extractor.cpp
@@ -0,0 +1,43 @@
+#include "Experiment.h"
+#include "Tensor.h"
+#include "layer/Convolution.h"
+#include "execution/OptimizedLayerByLayer.h"
+
+/**
+ * How to use: ./Weight_extractor arg1 arg2
+ * 
+ * @param arg1 : [binary file]
+ * @param arg2 : [layer_name] 
+ * @return [binary file] 
+ */
+
+int main(int argc, char** argv) {
+
+	Experiment<OptimizedLayerByLayer> experiment(argc, argv, "weights_extractor");
+	// Sim load
+	experiment.load(argv[1]);
+	std::string layer_name = argv[2];
+	experiment.initialize(Shape({28, 28, 1}));
+
+	std::vector<std::vector<std::vector<std::vector<float>>>> loaded_weights = dynamic_cast<layer::Convolution&>(experiment.layer(layer_name)).get_weights();
+
+	// Save to another binary
+	std::ofstream file("weights_"+experiment.layer(layer_name).name(), std::ios::out | std::ios::trunc | std::ios::binary);
+
+	file.imbue(std::locale("C.UTF-8"));
+	
+	for(size_t x = 0; x < loaded_weights.size(); x++ ){
+
+	   for(size_t y = 0; y < loaded_weights[x].size(); y++ ){
+
+		   for(size_t z = 0; z < loaded_weights[x][y].size(); z++ ){
+
+			const char* buffer = (char*)(&loaded_weights[x][y][z][0]);
+			file.write(buffer, loaded_weights[x][y][z].size() * sizeof(float));
+ 
+			}
+	   }
+	}
+	
+	file.close();
+}
\ No newline at end of file
-- 
GitLab