def moja(): bias, weight = load('net82.9_k2.pkl') net = Network([int(3136 / krok), 30, 40, 36], bias, weight) czas = time.localtime(time.time()) strCzas = str(czas.tm_hour) + ':' + str(czas.tm_min) + ':' + str( czas.tm_sec) print('Witam o ' + strCzas, end='') if czas.tm_hour < 5: print(' Późno już... Może kawy?') elif czas.tm_hour < 10: print('Jest dość wcześnie. Nie śpisz już, czy jeszcze? Może kawy?') else: print() czas = time.time() net.stochastic_gradient_descent(train, 100, 10, 6.0, test) czas2 = time.time() print('Nauczone. Czas na egzammin') evaluation = net.evaluate(test[1000:]) czas3 = time.time() print( str(evaluation) + ' out of ' + str(len(test[1000:])) + ', it\'s ' + str(100 * round(evaluation / len(test[1000:]), 2)) + '%' + '\n Training duration: ' + str(round(czas2 - czas, 2)) + '\n Eval duration: ' + str(round(czas3 - czas2, 2))) net.save()
def predict(test_data): """ Funkcja pobiera macierz przykladow zapisanych w macierzy X o wymiarach NxD i zwraca wektor y o wymiarach Nx1, gdzie kazdy element jest z zakresu {0, ..., 35} i oznacza znak rozpoznany na danym przykladzie. :param test_data: macierz o wymiarach NxD :return: wektor o wymiarach Nx1 """ krok = 1 bias, weight = load('net.pkl') net = Network([int(3136 / krok), 30, 40, 36], bias, weight) data_x = [] for main_data in test_data: data_x.append(main_data[::krok]) x = [] for dx in data_x: x.append(np.array(dx).reshape((len(dx), 1))) values = net.predict(x) print(values) values = np.reshape(values, (len(values), 1)) print(np.shape(values)) return values
def trigger_action_use_another_nw(self): """Позволяет выбрать другую полносвязную нейросеть""" filename, _ = QtWidgets.QFileDialog.getOpenFileName( self, "Открыть файл .json", "../data/networks/nw1.json", "Json Files (*.json)") if filename == "": return self.network1 = network.load(filename)
def get_model(load): """ Get the model. """ if load: logging.info("Loading model from %s", load) return network.load(load) else: logging.info("Building a new model") return network.build_model()
def _make_hyp_ref(RUN): "Make human sentences and machine sentences" JSON = '../var/30hidden-30epochs-10batch-0.5eta-5.0lambda-7window-100shape-{}run.net'.format(RUN) net = network.load(JSON) def classify(token_list): "Classify a list of token to compare with human's segmentation" result = [] sen_vec = make_vec(token_list) for x in sen_vec: result.append(_get_iob(net.feedforward(x))) return result test_list = get_files('test') test_sent = get_sentences(test_list, RUN) sents_ref = [_make_words(sent, tags) for sent, tags in test_sent] sents_hyp = [_make_words(sent, classify(sent)) for sent, _ in test_sent] return sents_ref, sents_hyp
def main(): args = sys.argv if len(args) <= 2 or not args[1].isdigit(): print("Usage: python validate.py EXAMPLE_ID NETWORK_FILE") exit() ex_id = args[1] directory = 'validation/' + ex_id + '/' dates = np.loadtxt(directory + 'dates') flux = np.load(directory + 'flux.npy') flux_err = np.load(directory + 'flux_err.npy') #event = LightCurve() #curve_of_interest = 4859 #event.load_curve(dates, flux[curve_of_interest], flux_err[curve_of_interest]) #return draw_plot(event) num_microlensing = 0 ml_events = [] network = load(args[2]) for i in range(len(flux)): lightcurve = LightCurve() lightcurve.load_curve(dates, flux[i], flux_err[i]) inputs = lightcurve.calculate_inputs() activations = network.activations(inputs) if np.argmax(activations[3]) == 0: num_microlensing += 1 ml_events.append((i, activations[3][0][0])) percent = round(100 * num_microlensing / (i + 1), 2) if i == len(flux) - 1: print("\r Validation complete, result: Error " + str(percent) + "% (" + str(num_microlensing) + " / " + str(i + 1) + ")") print("Index, Activation") for ind, act in sorted(ml_events, key=lambda x: x[1]): print(str(ind) + ", " + str(round(act, 2))) else: print(" " + str(num_microlensing) + " / " + str(i + 1) + " (Error " + str(percent) + "%)", end="\r") sys.stdout.flush()
def neuralNetwork(data): """ Calculates the output of NN for particular normalized BGR / YUV values VAlues can be changed : Threshold for Red, yellow and water Input : list of normalized BGR or YUV values output : """ net1 = network.load("nnet") a = net1.feedforward(data) a = a / sum(a) a = np.around(a, decimals=3) # print "Probabilities : ",a if a[0] > 0.72: a = (1, 0, 0) #Red elif a[1] > 0.72: a = (0, 1, 0) #yellow else: a = (0, 0, 1) #water return a
def neuralNetwork(data): """ Calculates the output of NN for particular normalized BGR / YUV values VAlues can be changed : Threshold for Red, yellow and water Input : list of normalized BGR or YUV values output : """ net1 = network.load("nnet") a = net1.feedforward(data) a = a/sum(a) a = np.around(a, decimals=3) # print "Probabilities : ",a if a[0] > 0.72 : a = (1,0,0) #Red elif a[1] > 0.72: a = (0,1,0) #yellow else : a = (0,0,1) #water return a
def __init__(self): QtWidgets.QMainWindow.__init__(self) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.buttonNetwork1.clicked.connect(self.handle_button_network1) self.ui.buttonNetwork2.clicked.connect(self.handle_button_network2) self.ui.actionOpen.triggered.connect(self.trigger_action_open) self.ui.actionAbout.triggered.connect(self.trigger_action_about) self.ui.actionUseAnotherNW.triggered.connect( self.trigger_action_use_another_nw) self.ui.actionRetrainNW.triggered.connect( self.trigger_action_retrain_nw1) self.ui.actionUse_network_1.triggered.connect(self.trigger_action_nw1) self.ui.actionUse_network_2.triggered.connect(self.trigger_action_nw2) self.ui.actionSaveImage.triggered.connect( self.trigger_action_save_image) self.ui.actionUseAnotherCNN.triggered.connect( self.trigger_action_use_another_cnn) self.network1 = network.load("../data/networks/network_30ep.json") self.network2 = cnn.ConvolutedNeuralNetwork( cnn.load("../data/networks/model_10epochs.h5")) self.image_filename = "" self.ui.tabWidget.setCurrentIndex(0) self.dialog = SecondaryWindow()
try: os.mkdir("usps/" + directory) except: pass save(htm, "usps/" + directory + "/") print "*** Summary **" print "Number of training sequences generated:" print " * Entry layer: ", seq_count[network.ENTRY] print " * Intermediate layer: ", seq_count[network.INTERMEDIATE] print " * Output layer: ", seq_count[network.OUTPUT] print "Training completed in ", time.time() - t0, "seconds" elif choice == 4: print "Enter the directory:" directory = raw_input() htm = load(directory) else: exit(0) print "*** HTM Testing ***" print "1. Test HTM on single input" print "2. Test HTM on USPS train100 training set" print "3. Test HTM on USPS train1000 training set" print "4. Test HTM on USPS full test set (over 2000 elements)" print "5. Quit" choice = int(raw_input()) t0 = time.time()
import flask from flask import Flask, render_template, request, url_for import base64 import numpy as np import cv2 import network # Initialize the useless part of the base64 encoded image. init_Base64 = 21 NET = network.load("trained.out") # Initializing new Flask instance. Find the html template in "templates". app = flask.Flask(__name__, template_folder='templates') # @app.route('/') # def hello_world(): # return 'Hello, World!' # First route : Render the initial drawing template @app.route('/') def home(): return render_template('draw.html') # Second route : Use our model to make prediction - render the results page. @app.route('/predict', methods=['POST']) def predict(): draw = request.form['url']
# sys.path.append("/home/pi/Documents/Github/Smart-Car/source/component") sys.path.append("../config") sys.path.append("../component") # import move import network import image_preprocess as imgprocess import common as common_config THRESHOLD = 220 ## Main function # major variables cap = cv2.VideoCapture(1) ret, frame = cap.read() # mo = move.Move() net = network.load("../config/result/0316-93%") ## Get Frame part #Continually updates the frame class GetFrameThread(Thread): def __init__(self): Thread.__init__(self) self.thread_stop = False def run(self): global ret, frame while not self.thread_stop: ret, frame = cap.read() def stop(self):
import data import network import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np import PIL.Image as pilimg import cv2 ## 맥북 실행법 -> 터미널에서 python3 predict.py 직접 실행 net = network.load("my-net-v1") capture = cv2.VideoCapture(0) capture.set(cv2.CAP_PROP_FRAME_WIDTH, 480) capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 640) def do_thr(t): if (t > 0.005): return 1.0 else: return 0.0 do_thr = np.vectorize(do_thr) while cv2.waitKey(33) < 0: ret, frame = capture.read() if frame is None: continue origen = np.copy(frame)
training_data, validation_data, test_data = mnist_loader.load_data_wrapper() training_data = list(training_data) import network # три слоя 784 - тк картинка 30-прост 10 - выход net = network.Network([784, 30, 10]) # 7 эпох net.Train(training_data, 30, 10, 3.0, 0.1, test_data=test_data, early_stopping_n=3) #net.save("net.dat") net = network.load("net.dat") import numpy as np count = 0 total = 0 for x, d in validation_data: total += 1 y = net.feedforward(x) if (d != np.argmax(y)): count += 1 #print("{} isn't {}".format(np.argmax(y), d)) print(count) print(total) print("Accurate : {}".format(1 - count / total))
import cv2 import cPickle import sys sys.path.append("../component") sys.path.append("../config") import network import common as common_config THRESHOLD = 220 DATA_DIR = "../data/expanded/" available_type = [".gif", ".jpg", ".jpeg", ".bmp", ".png"] INPUT_SIZE = common_config.NETWORK_INPUT_SIZE OUTPUT_SIZE = 3 net = network.load("../config/result/0317-95%") f = open(DATA_DIR + "data.pkl", 'rb') training_data, validation_data, te_d = cPickle.load(f) f.close() test_inputs = [np.reshape(x, (INPUT_SIZE, 1)) for x in te_d[0]] test_data = zip(test_inputs, te_d[1]) count = 0 for td in test_data: tr = np.argmax(net.feedforward(td[0])) # print tr, td[1] if tr == td[1]: count += 1 else: print tr, td[1]
import pdb import mnist_loader from network import load from plotter import plot_mnist_digit training_data, validation_data, test_data = mnist_loader.load_data_wrapper() #for data in training_data: # plot_mnist_digit(data[0]) # #pdb.set_trace() training_data_2 = [data for data in training_data if data[1][4] == 1] pdb.set_trace() print 'data loaded' net = load('network.json') net.generated_data = training_data[0][0] net.SGD(training_data_2, 30, 10, 0.5, evaluation_data=test_data, monitor_evaluation_accuracy=True)
import numpy as np import cv2 import cPickle import sys sys.path.append("../component") sys.path.append("../config") import network import common as common_config THRESHOLD = 220 DATA_DIR = "../data/expanded/" available_type = [".gif", ".jpg", ".jpeg", ".bmp", ".png"] INPUT_SIZE = common_config.NETWORK_INPUT_SIZE OUTPUT_SIZE = 3 net = network.load("../config/result/0317-95%") f = open(DATA_DIR+"data.pkl", 'rb') training_data, validation_data, te_d = cPickle.load(f) f.close() test_inputs = [np.reshape(x, (INPUT_SIZE, 1)) for x in te_d[0]] test_data = zip(test_inputs, te_d[1]) count = 0 for td in test_data: tr = np.argmax(net.feedforward(td[0])) # print tr, td[1] if tr == td[1]: count += 1 else: print tr, td[1]
from train_test_helper_funcs import get_train_test_split_2, get_test_data, get_prediction, compute_save_prediction_results_2, quantize_float import train_test_helper_funcs import time if __name__ == '__main__': train_pids_list, test_pids_list = get_train_test_split_2() fold = 1 avg_mape = 0 for test_pids in test_pids_list: print('{}-fold cross validation fold {}'.format(len(test_pids_list), fold)) nn_model_save_filename = train_test_helper_funcs.neural_network_model_save_filename.strip().split('.')[0] + '_fold_' + str(fold) + '_.json' nn = network.load(nn_model_save_filename) print('DEBUG - Computing and saving prediction results...') ini_time = time.time() mape = compute_save_prediction_results_2(nn, test_pids, fold) avg_mape += mape print('DEBUG - Computed and saved prediction results to ' + train_test_helper_funcs.prediction_results_filename) print('INFO - Execution time for computing and saving prediction results: ' + str(quantize_float(time.time() - ini_time)) + ' s') print('INFO - Mean Absolute percentage error (MAPE) = ' + str(mape)) if fold == len(test_pids_list): # print('DEBUG - Getting test data...')
''' TODO: - display where the user is drawing - average pixels to create greyscale values - increase "width" of user's cursors when drawing ''' root = Tk() K = 3 canvas = Canvas(root, width=28 * K, height=28 * K) arr = np.zeros((28, 28), dtype=np.float32) net = network.load('network.json') def click_move(event): #print(event.x, event.y) #print(canvas.winfo_width(), canvas.winfo_height() arr[event.y // K][event.x // K] = 1 def reset(event): global arr arr = np.zeros((28, 28), dtype=np.float32) def display(event): print(arr)
import sys from PIL import Image import numpy as np import network net = network.load('shape_detection_network.json') with Image.open(sys.argv[1]) as image: data = np.array(image.getdata(), dtype=np.float32).reshape((625, 1)) / 255. lookup = ['circle', 'square', 'star', 'triangle'] z = net.feedforward(data) print(z) print(lookup[np.argmax(z)])
previous_y = y root = tk.Tk("ANN") backboard = tk.LabelFrame(root, text="Blackboard") backboard.pack(fill="both", expand="yes") canvas = tk.Canvas(backboard, height=280, width=280, bg="black", bd=0, cursor="circle") canvas.bind("<Motion>", tell_me_where_you_are) canvas.bind("<B1-Motion>", draw_from_where_you_are) canvas.pack() net = NN.load("trainedNet") resultLabel = tk.StringVar() probaLabel = tk.StringVar() def guess(imgData): global net return net.feedforward(imgData) def getter(event): global root, canvas margin = 3 x = root.winfo_rootx() + canvas.winfo_x() + margin y = root.winfo_rooty() + canvas.winfo_y() + margin x1 = x + canvas.winfo_width() - margin * 2
x_0 = np.matrix([0, 0, np.pi, 0]).T # initial state x_f = np.matrix([0, 0, 0, 0]).T # final state mpc_N = 200 mpc_mode = False online_training = False # vectors to keep track of results x_vect = np.matrix(np.zeros([config.num_states, mpc_N + 1])) x_vect[:, 0] = x_0 x_pred = x_vect.copy() u_vect = np.matrix(np.zeros([config.num_inputs, mpc_N])) dx_vect = np.matrix(np.zeros([config.num_states, mpc_N])) cost_vect = np.zeros([mpc_N]) # load the neural network net = network.load(None) # MPC main loop for i in range(0, mpc_N): print('Iteration: {0}'.format(i)) (x_bar, u_bar, cost) = ddp.run(x_0, x_f) if (mpc_mode): # interact with system u = u_bar[:, 0] x = x_bar[:, 0] dx = dynamics.nonlinear(x, u) x_0 += dx * config.dt
############################################################################### # Model application ############################################################################### import sys import os import network from vec4net import make_vec import numpy as np # Replace this with your model's result JSON = '../var/30hidden-30epochs-10batch-0.5eta-5.0lambda-7window-100shape-3run.net' net = network.load(JSON) def _get_iob(arr): d = {0: 'i', 1: 'o', 2: 'b'} n = np.argmax(arr) return d[n] def _classify(token_list): "Classify a list of token" result = [] sen_vec = make_vec(token_list) for x in sen_vec: result.append(_get_iob(net.feedforward(x))) return result def _make_words(token_list, iob_list):
def ensemble(networks=None, data=None, test_names=None): """ Classifies the data using the provided networks. """ fileName = './Ensemble/testClassifications.pkl' nets = [] classification = [] if data is None: t = file("./Ensemble/correctTestLabels.pkl", 'rb') test_y, test_names = cPickle.load(t) t.close() else: test_y = data[1].eval() t = file("./Ensemble/correctTrainingLabels.pkl", 'wb') cPickle.dump((test_y, test_names), t) t.close() if networks is None: f = file(fileName, 'rb') classification = cPickle.load(f) f.close() else: for name in networks: net = network.load(name) #nets.append(net) mbs = 1 net.set_mini_batch_size(mbs) classification.append(net.classify(data)) acc = net.evaluate(data) print "loaded {0}, accuracy: {1}".format(name, acc) f = file(fileName, 'wb') cPickle.dump(classification, f) f.close() acc = np.mean(accuracy_max(classification, test_y)) print "accuracy_max: {0:.3%}".format(acc) acc = np.mean(accuracy_maxVote(classification, test_y)) print "accuracy_max: {0:.3%}".format(acc) acc = np.mean(accuracy_maxVote_ultmax(classification, test_y)) print "accuracy_max: {0:.3%}".format(acc) acc = np.mean(accuracy_maxVote_ultmax_probcast(classification, test_y)) print "accuracy_max: {0:.3%}".format(acc) acc = np.mean(accuracy_maxVote_ultmax_probcast_doubtfilter(classification, test_y)) print "accuracy_max: {0:.3%}".format(acc)
############################################################################### # Model application ############################################################################### import sys import os import network from vec4net import make_vec import numpy as np # Replace this with your model's result JSON = '../var/30hidden-30epochs-10batch-0.5eta-5.0lambda-7window-100shape-3run.net' net = network.load(JSON) def _get_iob(arr): d = {0: 'i', 1: 'o', 2: 'b'} n = np.argmax(arr) return d[n] def _classify(token_list): "Classify a list of token" result = [] sen_vec = make_vec(token_list) for x in sen_vec: result.append(_get_iob(net.feedforward(x))) return result def _make_words(token_list, iob_list): "Make segmented words from token list and corresponding iob list"
# this is not exactly the same because it returns an array of shape (n,1) def vectorize_word2(word): v = np.zeros((MAX_WORD_LENGTH * 26, 1), dtype=np.uint8) for i, c in enumerate(word.lower()): # print(i, c, i*26+(ord(c)-ord('a'))) v[i * 26 + (ord(c) - ord('a'))] = 1 return v net = network.load('language_detection_network.json') words = input('enter a string of words without special characters: ').split() with open('words.json') as f: lookup = json.load(f)['lookup'] count = np.zeros(11) for word in words: if len(word) > MAX_WORD_LENGTH: print(word, 'is too long.') continue z = net.feedforward(vectorize_word2(word))
import numpy as np import network from train_test_helper_funcs import get_train_test_split, get_test_data, get_prediction, compute_save_prediction_results, quantize_float import train_test_helper_funcs import time if __name__ == '__main__': train_pids, test_pids = get_train_test_split() nn = network.load(train_test_helper_funcs.neural_network_model_save_filename) print('DEBUG - Computing and saving prediction results...') ini_time = time.time() mape = compute_save_prediction_results(nn, test_pids) print('DEBUG - Computed and saved prediction results to ' + train_test_helper_funcs.prediction_results_filename) print('INFO - Execution time for computing and saving prediction results: ' + str(quantize_float(time.time() - ini_time)) + ' s') print('INFO - Mean Absolute percentage error (MAPE) = ' + str(mape)) # print('DEBUG - Getting test data...') test_input, test_result = get_test_data(test_pids) # print('DEBUG - Got test data...') print('Actual SBP = ' + str(test_result)) prediction = get_prediction(nn, test_input) print('Predicted SBP = ' + str(prediction)) print('INFO - Absolute Percentage error = ' + str(quantize_float(abs((prediction - test_result) / test_result * 100))))
return bestScore, bestMove return bestScore, bestMove def getComputerMove(board, computerTile): # Given a board and the computer's tile, determine where to # move and return that move as a [x, y] list. bestScore, bestMove = abNegaMax(board, computerTile, 0, 3, -INFINITY, INFINITY) if isOnCorner(bestMove[0], bestMove[1]): return bestMove #make another decision validList = getValidMoves(board, computerTile) for move in validList: if isOnCorner(move[0], move[1]): return move return bestMove def checkForQuit(): for event in pygame.event.get((QUIT, KEYUP)): # event handling loop if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE): pygame.quit() sys.exit() if __name__ == '__main__': Network = network.load("network.json") main()
def load(): """ load model """ return network.load()