def predict_nn(trndata, epochs=300, test_error=0.0147, weight_decay=0.0001, momentum=0.15): """ FF neural net """ fnn = buildNetwork(trndata.indim, trndata.indim / 4, trndata.outdim, outclass=SoftmaxLayer) trainer = BackpropTrainer(fnn, trndata, momentum=momentum, weightdecay=weight_decay) epoch_delta = 1 stop = False totEpochs = np.array([]) trnResults = np.array([]) trnLogLoss = np.array([]) f, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 8)) while not stop: trainer.trainEpochs(epoch_delta) trnresult = percentError(trainer.testOnClassData(), trndata['class']) trnLogLoss = append_to_arr(trnLogLoss, nn_log_loss(fnn, trndata)) print("epoch: %4d" % trainer.totalepochs, \ " train error: %5.2f%%" % trnresult, \ " train logloss: %2.4f" % trnLogLoss[-1]) trnResults = append_to_arr(trnResults, trnresult) totEpochs = append_to_arr(totEpochs, trainer.totalepochs) plt.sca(ax1) plt.cla() ax1.plot(totEpochs, trnResults, label='Train') plt.sca(ax2) plt.cla() ax2.plot(totEpochs, trnLogLoss, label='Train') ax1.legend() ax2.legend() plt.draw() time.sleep(0.1) plt.pause(0.0001) stop = (trnLogLoss[-1] <= test_error or trainer.totalepochs >= epochs) return fnn
def get_1dlbp_features(neighborhood): tf = TrainFiles(inp_path, floor = neighborhood * 2 + 1) inputs = tf.get_training_inputs() start = timer() hist = np.array([]) outs = np.array([]) i = 0 writeBatch = 2000 prep_out_path(out_path) p = 1 << np.array(range(0, 2 * neighborhood), dtype='int32') d_powers = cuda.to_device(p) for inp in inputs: data_file = path.join(inp_path, inp) out_file = path.join(out_path, path.splitext(inp)[0] + ext) arr = np.fromfile(data_file, dtype = 'uint8') ##GPU## file_hist = extract_1dlbp_gpu(arr, neighborhood, d_powers) ##CPU## #file_hist = extract_1dlbp_cpu(arr, neighborhood, p) #file_hist = file_histogram(file_hist, neighborhood) i += 1 hist = append_to_arr(hist, file_hist) outs = append_to_arr(outs, out_file) if i == writeBatch: i = 0 first = True print "Writing....." for j in range(0, outs.shape[0]): hist[j].tofile(outs[j]) hist = np.array([]) outs = np.array([]) print "==============Done===================" print "Elapsed: ", timer() - start print "Writing......." for i in range(0, outs.shape[0]): hist[i].tofile(outs[i]) print "==============Done==================="
def predict_nn(trndata, epochs = 300, test_error = 0.0147, weight_decay = 0.0001, momentum = 0.15): """ FF neural net """ fnn = buildNetwork(trndata.indim, trndata.indim / 4, trndata.outdim, outclass = SoftmaxLayer) trainer = BackpropTrainer(fnn, trndata, momentum = momentum, weightdecay = weight_decay) epoch_delta = 1 stop = False totEpochs = np.array([]) trnResults = np.array([]) trnLogLoss = np.array([]) f, (ax1, ax2) = plt.subplots(1, 2, figsize=(15,8)) while not stop: trainer.trainEpochs(epoch_delta) trnresult = percentError( trainer.testOnClassData(), trndata['class'] ) trnLogLoss = append_to_arr(trnLogLoss, nn_log_loss(fnn, trndata)) print "epoch: %4d" % trainer.totalepochs, \ " train error: %5.2f%%" % trnresult, \ " train logloss: %2.4f" % trnLogLoss[-1] trnResults = append_to_arr(trnResults, trnresult) totEpochs = append_to_arr(totEpochs, trainer.totalepochs) plt.sca(ax1) plt.cla() ax1.plot(totEpochs, trnResults, label = 'Train') plt.sca(ax2) plt.cla() ax2.plot(totEpochs, trnLogLoss, label = 'Train') ax1.legend() ax2.legend() plt.draw() time.sleep(0.1) plt.pause(0.0001) stop = (trnLogLoss[-1] <= test_error or trainer.totalepochs >= epochs) return fnn
def _connect_labeled_data(self, inp_path, inputs, training): if self.labels == None: self.labels = self.get_labels_csv() X = np.array([]) Y = np.array([]) zip = False if self.isZip: zip = ZipFile(inp_path) for inp in inputs: inp_file = path.join(inp_path, inp) if not zip else inp x = np.fromfile(inp_file, dtype='int') if not zip else np.frombuffer( zip.read(inp_file), dtype='int') x = x.astype('float') X = append_to_arr(X, x) if training or self.validate: label_name = path.splitext(path.split(inp_file)[1])[0] label = filter(lambda x: x[0] == label_name, self.labels)[0][1] Y = np.append(Y, label) if self.debug: print "Processed: " + inp_file return X, Y
def connect_labeled_data(self, training, load_from_file): inputs, inp_path = self._get_inp_input_path(training) self.labels = self.get_labels_csv() X = np.array([]) # TODO: this is probably not the best way of doing it zip = False if self.isZip: zip = ZipFile(inp_path) for inp in inputs: inp_file = path.join(inp_path, inp) if not zip else inp x = load_from_file(inp_file) x = np.r_[[path.splitext(inp)[0]], x] X = append_to_arr(X, x) if self.debug: print "Processed: " + inp_file id = self.labels.columns[0] dfX = pd.DataFrame(X) dfX.columns=np.r_[[id], range(X.shape[1] - 1)] dfX = dfX.merge(self.labels, how='inner', on=id) return np.array(dfX[range(1, X.shape[1])]).astype(float), np.array(dfX[self.labels.columns[1]]).astype(int)
def connect_labeled_data(self, training, load_from_file): inputs, inp_path = self._get_inp_input_path(training) self.labels = self.get_labels_csv() X = np.array([]) # TODO: this is probably not the best way of doing it zip = False if self.isZip: zip = ZipFile(inp_path) for inp in inputs: inp_file = path.join(inp_path, inp) if not zip else inp x = load_from_file(inp_file) x = np.r_[[path.splitext(inp)[0]], x] X = append_to_arr(X, x) if self.debug: print "Processed: " + inp_file id = self.labels.columns[0] dfX = pd.DataFrame(X) dfX.columns = np.r_[[id], range(X.shape[1] - 1)] dfX = dfX.merge(self.labels, how='inner', on=id) return np.array(dfX[range(1, X.shape[1])]).astype(float), np.array( dfX[self.labels.columns[1]]).astype(int)
def _connect_labeled_data_csv(self, inp_path, inputs, training): if self.labels == None: self.labels = self.get_labels_csv() X = np.array([]) Y = np.array([]) for inp in inputs: inp_file = path.join(inp_path, inp) x = np.loadtxt(inp_file) X = append_to_arr(X, x) if training or self.validate: label_name = path.splitext(inp)[0] label = filter(lambda x: x[0] == label_name, self.labels)[0][1] Y = np.append(Y, label) if self.debug: print "Processed: " + inp_file return X, Y
def _connect_labeled_data(self, inp_path, inputs, training): if self.labels == None: self.labels = self.get_labels_csv() X = np.array([]) Y = np.array([]) zip = False if self.isZip: zip = ZipFile(inp_path) for inp in inputs: inp_file = path.join(inp_path, inp) if not zip else inp x = np.fromfile(inp_file, dtype='int') if not zip else np.frombuffer(zip.read(inp_file), dtype='int') x = x.astype('float') X = append_to_arr(X, x) if training or self.validate: label_name = path.splitext(path.split(inp_file)[1])[0] label = filter(lambda x: x[0] == label_name, self.labels)[0][1] Y = np.append(Y, label) if self.debug: print "Processed: " + inp_file return X, Y