with open("cost_iris.pkl", "rb") as c: cost = pickle.load(c) with open("acc_iris.pkl", "rb") as c: accuracy = pickle.load(c) # Plot training cost and accuracy vs epochs plt.figure(1) plt.plot(list(range(0, 1000, 10)), cost) plt.ylabel("Training Error/Cost"), plt.xlabel("Epochs") plt.figure(2) plt.plot(list(range(0, 1000, 10)), accuracy) plt.ylabel("Training Accuracy"), plt.xlabel("Epochs") plt.show() op_nodes = 3 iris = load_iris() X = iris.data y = np.array([np.eye(op_nodes)[i] for i in iris.target]) # Shuffle random = np.random.permutation(len(X)) X = X[random] y = y[random] # Load the model nnet = NeuralNet(load=True, model_file="trained.pkl") op = nnet.predict(X, return_max=True) print("-" * 30 + "\nTEST SET ACCURACY\n" + "-" * 30) print(classification_report(y.argmax(axis=1), op))
X = X[idx] y = y[idx] # split the data Xtrain = X[:nTrain, :] ytrain = y[:nTrain] Xtest = X[nTrain:, :] ytest = y[nTrain:] # train the decision tree modelDT = DecisionTreeClassifier() modelDT.fit(Xtrain, ytrain) # train the naive Bayes layers = np.array(([25])) modelNN = NeuralNet(layers=layers, learningRate =2, numEpochs=500, epsilon=.62) modelNN.fit(Xtrain, ytrain) ypred_NNtrain = modelNN.predict(Xtrain) # output predictions on the remaining data ypred_NN = modelNN.predict(Xtest) # compute the training accuracy of the model accuracyNT = accuracy_score(ytrain, ypred_NNtrain) accuracyNN = accuracy_score(ytest, ypred_NN) print "Training = "+str(accuracyNT) print "Neural Net accuracy = "+str(accuracyNN) modelNN.visualizeHiddenNodes("visualizeHiddenNodes.bmp")
import numpy as np from sklearn import datasets from sklearn.metrics import accuracy_score from nn import NeuralNet filename_X = 'data/digitsX.dat' filename_y = 'data/digitsY.dat' X = np.loadtxt(filename_X, delimiter=',') y = np.loadtxt(filename_y, delimiter=',') # takes roughly 1s for each epoch clf_NN = NeuralNet(layers=np.array([25]), learningRate=2.0, numEpochs=450) clf_NN.fit(X, y) y_predict = clf_NN.predict(X) accuracy_NN = accuracy_score(y_predict, y) print "Accuracy: \t" + str(accuracy_NN)
from numpy import loadtxt, ones, zeros, where import numpy as np from sklearn import datasets from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score import sys, traceback from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from nn import NeuralNet # load the data set filename = 'data/digitsX.dat' data = loadtxt(filename, delimiter=',') X = data[:,:] filename = 'data/digitsY.dat' data1 = loadtxt(filename, delimiter=',') y = data1 layers = np.array([25]) clf = NeuralNet(layers = layers, learningRate = 2.0, numEpochs = 10) clf.fit(X,y) predicted = clf.predict(X) # print predicted print np.mean(predicted == y)
''' AUTHOR Wenqi Xian ''' from numpy import loadtxt import numpy as np from nn import NeuralNet X_train = loadtxt('data/digitsX.dat', delimiter=',') y_train = loadtxt('data/digitsY.dat', delimiter=',') layers = np.array([25]) NN = NeuralNet(layers = layers, learningRate = 1.8, numEpochs = 700) NN.fit(X_train,y_train) predicted = NN.predict(X_train) accuracy = 100.0 * (predicted == y_train).sum() / y_train.shape[0] print accuracy
batch_size=batch_size, print_details=True, epochs_bw_details=epochs_bw_details, dropout=dropout_percent, dropout_layers=d_layers) # Cost and Accuracy plotting plt.figure(1) plt.ylabel("Training Error/Cost"), plt.xlabel("Epochs") plt.plot(list(range(0, epochs, epochs_bw_details)), cost) plt.figure(2) plt.plot(list(range(0, epochs, epochs_bw_details)), accuracy) plt.ylabel("Training Accuracy"), plt.xlabel("Epochs") plt.show() op = nnet.predict(x_test, return_max=True) print("-" * 30 + "\nTEST SET ACCURACY\n" + "-" * 30) print(classification_report(y_test.argmax(axis=1), op), '\n') splits = 5 cumulative_acc = 0 # k-Fold Cross validation kf = KFold(n_splits=splits, shuffle=True) for i, (train_index, test_index) in enumerate(kf.split(x)): x_train, x_test = x[train_index], x[test_index] y_train, y_test = y[train_index], y[test_index] _cost, _accuracy = nnet.train(x_train, y_train,
# neural nets doesnt really make sense import math import numpy as np import sys from import_train import import_training_file from import_train import rmsle from sklearn.neural_network import BernoulliRBM from nn import NeuralNet if __name__ == '__main__': (X, y) = import_training_file(sys.argv[1], True) hidden_layers = [5] learningRate = 1.6 epsil = 0.12 eps = 1000 neural_network = NeuralNet(hidden_layers, learningRate, epsilon=epsil, numEpochs=eps) neural_network.fit(X, y) nn_predict = neural_network.predict(X)
X = digitsX[:] y = digitsY[:] n, d = X.shape nTrain = 0.2 * n #training on 50% of the data # shuffle the data # idx = np.arange(n) # np.random.seed(13) # np.random.shuffle(idx) # X = X[idx] # y = y[idx] # split the data Xtrain = X[:nTrain, :] ytrain = y[:nTrain] # Xtest = X[nTrain:,:] # ytest = y[nTrain:] model = NeuralNet( np.array([25]), .80, 0.12, 600) # 100 @ 2.5 = 0.885, 400 @ 1.6 = 0.88, 1000 @ 1 = 0.8542, model.fit(X, y) ypred = model.predict(Xtrain) accuracy = accuracy_score(ytrain, ypred) print "NeuralNet Accuracy = " + str(accuracy) # model.visualizeHiddenNodes('hiddenLayers.png')
# dataset = datasets.load_digits() X = digitsX[:] y = digitsY[:] n,d = X.shape nTrain = 0.2*n #training on 50% of the data # shuffle the data # idx = np.arange(n) # np.random.seed(13) # np.random.shuffle(idx) # X = X[idx] # y = y[idx] # split the data Xtrain = X[:nTrain,:] ytrain = y[:nTrain] # Xtest = X[nTrain:,:] # ytest = y[nTrain:] model = NeuralNet(np.array([25]), .80, 0.12, 600) # 100 @ 2.5 = 0.885, 400 @ 1.6 = 0.88, 1000 @ 1 = 0.8542, model.fit(X,y) ypred = model.predict(Xtrain) accuracy = accuracy_score(ytrain, ypred) print "NeuralNet Accuracy = "+str(accuracy) # model.visualizeHiddenNodes('hiddenLayers.png')
class AimAssistant: """ Wrapper around the neural net for data preprocessing / postprocessing """ sides_crop = 0.3 top_crop = 0.35 def __init__(self, crop: bool = False): self.to_crop = crop try: self.net = NeuralNet() except Exception as e: print(f"Failed while NN initialization. Error: {e}") raise e print("Aim assistant successfully initialized") def aim(self, frame: np.ndarray) -> dict: """ :param frame: :return: """ # Режем кадр, чтобы оставить только центр картинки где располагается опора. if self.to_crop: relative_coord, frame = AimAssistant.crop_frame( frame, self.sides_crop, self.top_crop) # Detect components on the frame components = self.net.predict(frame) # If components, calculate angles relatively to the frame centre output = dict() if components: # Пересчитаем координаты коробок относительно целого кадра. Сейчас они относительно # вырезанного np.ndarray если стоял флаг self.to_crop if self.to_crop: components = AimAssistant.recalculate_boxes( components, relative_coord) output = AimAssistant.calculate_angles(components=components, frame=frame) return output @staticmethod def recalculate_boxes(predictions: List[list], sliced_image_coord: List[int]) -> List[list]: """ Пересчитывает координаты коробки относительно оригинального кадра, а не вырезанной подкартинки :param predictions: :param sliced_image_coord: :return: """ output = list() for prediction in predictions: class_id = prediction[0] conf = prediction[1] left = prediction[2] + sliced_image_coord[0] top = prediction[3] + sliced_image_coord[1] right = prediction[4] + sliced_image_coord[0] bot = prediction[5] + sliced_image_coord[1] assert all(e > 0 for e in [left, top, right, bot ]), "Coordinates recalculated wrong" assert all( (left < right, top < bot)), "Coordinates recalculated wrong" output.append([class_id, conf, left, top, right, bot]) return output @staticmethod def crop_frame(frame: np.ndarray, sides: float = 0.3, top: float = 0.35) -> Tuple[list, np.ndarray]: """ Вырезает картинки из кадра где находится опора. Отрезает края и верхушку кадра. Они нам не интеренсты если снимаем с курсовой камеры, которая смотрит перед собой. :param frame: :param sides: :param top: :return: """ assert 0 < sides < 0.5, "Sides crop needs to be between 0 and 50%" assert 0 < top < 0.5, "Top crop needs to be between 0 and 50%" frame_height, frame_width = frame.shape[0], frame.shape[1] new_left = int(frame_width * sides) new_right = int(frame_width - (frame_width * sides)) new_top = int(frame_height * top) new_bot = frame_height assert all((new_top < new_bot, new_left < new_right)), "Wrong cropped coordinates" #cv2.rectangle(frame, (new_left, new_top), (new_right, new_bot), (0, 255, 255), 2) try: cropped_image = np.array(frame[new_top:new_bot, new_left:new_right, :]) except Exception as e: print("Failed while cropping the frame. Error: {e}") raise e return [new_left, new_top, new_right, new_bot], cropped_image @staticmethod def calculate_angles(components: List[list], frame: np.ndarray) -> Dict[int, dict]: """ :param components: detected components :param frame: frame :return: """ frame_centre = (frame.shape[1] // 2, frame.shape[0] // 2) # x,y output = dict() output_schema = { "obj_class": int, "bb_centre": tuple, "bb_coord": list, "aim_angle": tuple, "bb_size": float } for i, component in enumerate(components): obj_class, conf = component[0], component[1] # Skip concrete pillars, we're interested in only insulators and dumpers if obj_class == 2: continue left, top = component[2], component[3] right, bot = component[4], component[5] # Calculate element's BB centre relatively to the whole image element_x_centre = (left + right) // 2 element_y_centre = (top + bot) // 2 # Calculate delta (image centre vs element centre) delta_x = abs(frame_centre[0] - element_x_centre) delta_y = abs(frame_centre[1] - element_y_centre) # Calculate angles angle_1 = round(np.rad2deg(np.arctan2(delta_x, delta_y)), 2) angle_2 = round(90 - angle_1, 2) assert all((0 <= angle_1 <= 90, 0 <= angle_2 <= 90)), "Wrong angles calculated. Expected: [0, 90]" # Estimate object's relative diameter element_BB_diagonal = math.sqrt((top - bot)**2 + (right - left)**2) frame_diagonal = math.sqrt((frame.shape[0])**2 + (frame.shape[1])**2) diagonal = round(element_BB_diagonal / frame_diagonal, 3) schema_instance = output_schema.copy() try: schema_instance["obj_class"] = obj_class schema_instance["bb_centre"] = (int(element_x_centre), int(element_y_centre)) schema_instance["bb_coord"] = (left, top, right, bot) schema_instance["aim_angle"] = (angle_1, angle_2) schema_instance["bb_size"] = diagonal except Exception as e: print( f"Failed while filling the output schema instance. Error: {e}" ) raise e output[i] = schema_instance return output