if (len(sys.argv) != 5): print "Usage: " + sys.argv[0] + " <inputfile> <tags> <n> <outputfile>" exit(0) inputfile = sys.argv[1] if (sys.argv[2] == "all"): t = read_tags() tags = t.all_tags else: tags = sys.argv[2].split(",") print tags n = int(sys.argv[3]) outputfile = sys.argv[4] columnNames = load_column_names(inputfile) tagsIdx = map(lambda x: columnNames.index(x), tags) print tagsIdx print "Opening files" f = open(inputfile, "r") nOther = 0 nPositive = [] for i in range(len(tags)): nPositive.append(0) outf = open(outputfile, "w") print "Processing" lines = f.readlines() nLines = len(lines)-1
def main(): # Create dataset inputs = ["jrms", "rms", "mean_10", "mean_100", "max_10", "max_100", "stddev_10", "stddev_100"] # inputs = ["max_100", "rms"] # inputs = ["max_100", "mean_100"] # inputs = ["rms"] mode = "train" if (len(sys.argv) >= 2): if (sys.argv[1] == "test"): mode = "test" print "Create dataset" data = load_data("out.txt") columnNames = load_column_names("out.txt") ds = create_data_set(data, columnNames, inputs, "jumping") targets = ds['target'] #print "sum of targest: " + str(targets.sum()) # Create network if (mode == "train"): print "Build network" if (len(sys.argv) >= 3): nEpochs = int(sys.argv[2]) else: nEpochs = 1 net = buildNetwork(len(inputs), N_HIDDEN, 1, bias=True, hiddenclass=TanhLayer) trainer = BackpropTrainer(net, ds, verbose = True) #net = buildNetwork(len(inputs), N_HIDDEN, 1, bias=True, hiddenclass=TanhLayer) print "Train" print "Base error: " + str(trainer.testOnData()) results = testOnClassData(trainer) print "Percentage error: " + str(relativePercentError( results, targets )) + "%" print "Training started" trainer.trainEpochs(nEpochs) print "Training done: Saving model" NetworkWriter.writeToFile(net, "model.xml") else: net = NetworkReader.readFrom("model.xml") trainer = BackpropTrainer(net, ds, verbose = True) #print "Final error: " + str(trainer.testOnData()) results = testOnClassData(trainer) print "Percentage error (final): " + str(relativePercentError( results, targets )) + "%" # Plot # Data frames = data[:,1] nplots = 3 subplot(nplots, 1, 1) dataType = inputs[0] title("Data (" + dataType + ")") setp(plot(frames, ds['input'][:,0], color="black", marker=".", linestyle='None')) xlabel("frame") ylabel(dataType) # nplots = len(inputs) + 2 # for i in range(0, len(inputs)): # subplot(nplots, 1, i+1) # dataType = inputs[i] # title("Data (" + dataType + ")") # setp(plot(frames, ds['input'][:,i], color="black", marker=".", linestyle='None')) # xlabel("frame") # ylabel(inputs[i]) # Correct classification (target) subplot(nplots, 1, nplots-1) title("Target classification") targets = targets[:,0] setp(plot(frames, targets, color="blue")) # Classification (from NN) subplot(nplots, 1, nplots) title("NN classification and errors") setp(plot(frames, results), color="blue") errors = [[],[]] for i in range(len(results)): if (results[i] != targets[i]): errors[0].append(frames[i]) errors[1].append(0.5) setp(plot(errors[0], errors[1]), color="red", marker='.', linestyle='None', alpha=0.5) savefig("figure.png")
if (len(sys.argv) != 5): print "Usage: " + sys.argv[0] + " <inputfile> <tags> <n> <outputfile>" exit(0) inputfile = sys.argv[1] if (sys.argv[2] == "all"): t = read_tags() tags = t.all_tags else: tags = sys.argv[2].split(",") print tags n = int(sys.argv[3]) outputfile = sys.argv[4] columnNames = load_column_names(inputfile) tagsIdx = map(lambda x: columnNames.index(x), tags) print tagsIdx print "Opening files" f = open(inputfile, "r") nOther = 0 nPositive = [] for i in range(len(tags)): nPositive.append(0) outf = open(outputfile, "w") print "Processing" lines = f.readlines() nLines = len(lines) - 1