def computeMemorySize(_training, _model, _resultFolder, _discretization): csv = CSV(_training) lAtt = len(csv.findAttributes(0)) - 1 codeFile = "example_rf_sweet_spot.cpp" CodeGenerator().export(_training, _model, codeFile, _discretization) mem = [] platforms = [Arduino(), MSP430(), ESP32()] for platform in platforms: mem.append(platform.run(codeFile, "unsigned char", lAtt)) return mem
def computeMemorySize(_training, _model, _regression): csv = CSV(_training) lAtt = len(csv.findAttributes(0)) - 1 codeFile = "example_rf_sweet_spot.cpp" CodeGenerator().export(_training, _model, codeFile) if _regression == True: resultType = "float" else: resultType = "const char*" mem = [] platforms = [Arduino(), MSP430(), ESP32()] for platform in platforms: mem.append(platform.run(codeFile, resultType, lAtt)) return mem
def regressionRF(_training, _trees, _depth, _file, _resultFolder, _discretization): csv = CSV(training) attributes = csv.findAttributes(0) R = ResultMatrix() for numTrees in range(1, _trees + 1): for depth in range(1, _depth + 1): rf = RandomForest() rf.config.trees = numTrees rf.config.depth = depth # perform a cross validation to generate the training/test files e = Experiment(_training, "example_rf_sweet_spot_disc", verbose=False) e.regression([rf], 10) # r, c = CodeEvaluator().crossValidation(rf, _training, attributes, e.tmp(), _discretization) result = np.hstack([r.data.mean(0), r.data.std(0)]) header = r.header + [x + "_std" for x in r.header] mem = computeMemorySize(_training, rf, _resultFolder, _discretization) header += ["arduino", "msp", "esp"] result = np.hstack([result, mem]) print([ "#trees=" + str(numTrees) + "/" + str(_trees) + " depth=" + str(depth) + "/" + str(_depth) + ' mem=', mem ], flush=True) R.add(header, result) R.save(_file)
def export(self, _training, _model, _out, _discretize=False): FileHandler().createFolder("tmp") tmpId = "_" + str(uuid.uuid1()) tmpFolder = "tmp/" tmpTraining = "train" + tmpId + ".arff" csv = CSV(_training) csv.convertToARFF(tmpFolder + tmpTraining, False) d = None if _discretize: d = csv.discretizeData() attributes = csv.findAttributes(0) weka = WEKA() weka.folder = tmpFolder weka.train(_model, tmpFolder + tmpTraining, tmpId) data = "\n".join(FileHandler().read(tmpFolder + "raw" + tmpId + ".txt")) FileHandler().checkFolder(_out) weka.modelInterface.exportCode(data, csv, attributes, _out, _training, discretization=d) FileHandler().deleteFiles([tmpFolder + tmpTraining, tmpFolder + "raw" + tmpId + ".txt"])
def run(self, _training, _models, _platforms): R = ResultMatrix() M = []; for model in _models: # run the cross validation to compute the model performance M.append(model.toString()) e = Experiment(_training) header, result = e.regression([model], 10) R.add(header, result) # train with the global training data and export code training_arff = "tmp/recommend.arff" csv = CSV() csv.load(_training) csv.convertToARFF(training_arff, False) attributes = csv.findAttributes(0) lAtt = len(attributes)-1 WEKA().train(model, training_arff, "0") data = "\n".join(FileHandler().read("tmp/raw0.txt")) codeFile = "recommend.c" model.exportCode(data, csv, attributes, codeFile) # complile platform-specific code for platform in _platforms: "" #print(model.toString() + " : " + platform.toString()) print(R.header, R.data) print(M)
def generateCode(self, _file): csv = CSV(self.training) attributes = csv.findAttributes(0) normed = self.normalize(csv, attributes) resultType = "float" code = "#include <math.h>\n" if self.modelType == Type.CLASSIFICATION: code += "" classes = attributes[0].type.strip("{").strip("}").split(",") classes = ["\"" + key + "\"" for key in classes] code += CodeGenerator().generateArray("const char*", "classes", classes) + "\n\n" resultType = "const char*" else: code += "\n" # weight matrices if not self.useUnrolling: for i in range(0, len(self.layers)): W = self.layers[i][0] name = "w" + str(i) if i == len(self.layers) - 1: name = "w_out" code += "const " + CodeGenerator().generateMatrix( "float", name, W) + "\n" code += "\n" # threshold vectors for i in range(0, len(self.layers)): matrix = self.layers[i] T = self.layers[i][1] name = "th" + str(i) if i == len(self.layers) - 1: name = "th_out" code += "const " + CodeGenerator().generateArray("float", name, T) + "\n" code += "\n" # generate the required ann-specific methods code += self.sigmoid() + "\n\n" code += self.activate() + "\n\n" if not self.useUnrolling: code += self.mult() + "\n\n" if self.modelType == Type.CLASSIFICATION: code += CodeGenerator().findMax("float") + "\n\n" # generate the callable method header = ["_" + key for key in self.inputLayer] code += resultType + " predict(" + ", ".join( ["float " + x for x in header]) + ")\n{\n" # input layer for i in range(0, len(header)): header[i] = self.norm(header[i], normed[i + 1][0], normed[i + 1][1]) code += "\t" + CodeGenerator().generateArray("float", "in", header) + "\n\n" # activate the layers if self.useUnrolling: code += self.activateLayersWithUnrolling(normed) else: code += self.activateLayers(header, normed) code += "}\n" #code += CodeGenerator().generateDummyMain(len(attributes)-1) FileHandler().write(code, _file)
from models.m5.M5 import M5 from experiment.Experiment import Experiment from code.CodeGenerator import CodeGenerator from data.CSV import CSV from code.Arduino import Arduino # define the training data set and set up the model training = "../examples/mnoA.csv" model = M5() # perform a 10-fold cross validation e = Experiment(training, "example_arduino") e.regression([model], 10) # export the raw C++ code codeFile = e.path("arduino.cpp") CodeGenerator().export(training, model, codeFile) # create a dummy Arduino project which executes the model csv = CSV() csv.load(training) attributes = csv.findAttributes(0) mem = Arduino().run(codeFile, "float", len(attributes) - 1) print(mem) # all results are written to results/example_arduino/