Exemple #1
0
def run_network(save=True):
    # THE MAIN FUNCTION. SET ALL HYPER-PARAMETERS HERE.

    # Set predict_ny to True if you want to produce predictions for one year.
    predict_ny = False
    # years_ago is the number of seasons ago you want to use to make future predictons for. years_ago = 1 = 2016/17
    years_ago = 2

    # These are the input categories. It's important to list actual categories or NaNs are added to data and network breaks.
    input_categories = ["GP","TOI","G","A1","A","P","iCF","iFF","iSF","iSCF","ixG","iFOW",
        "Avg.DIST","D?","Age","GP_prev","G_prev","P_prev","iCF_prev","ixG_prev",
                        "GP_2prev","G_2prev","P_2prev", "iCF_2prev","ixG_2prev",
                        "GP_3prev","G_3prev","P_3prev","iCF_3prev","ixG_3prev"]

    # Gets all the data.
    train_set_x_orig, train_set_y, test_set_x_orig, test_set_y,names = get_inputs(input_categories,years_ago,predict_ny )
    # x.shape = (dim,m)     y.shape = (1,m)

    layer1_dims = (train_set_x_orig.shape[0])

    # Set the dimensions of network here

    layers_dims = [layer1_dims,30,30, 1]


    # Set the learning rate and # of iterations here.
    # Learning rate seems to need to go down the more layers there are.
    # beta1 controls the how much momentum the gradient has. Closer to 1 =  more momentum. Set to 0 to turn off.
    # beta2 slightly more complicated but it leads to gradient not getting out of control. Leave as is. Set to 0 to turn off.
    # keep_prob controls dropout rate. Lower values lead to less overfitting. Set to 1 to turn off.
    # However if there are layers with few neurons do not allow keep_prob to be too low.
    # Don't want to lose all neurons in a layer
    parameters = L_layer_model(train_set_x_orig, train_set_y, layers_dims,
                               learning_rate=0.01, beta1=0.9, beta2=0.999, num_iterations=4002, keep_prob=0.94,
                               print_cost=True)


    Y_prediction_test = predict(parameters, test_set_x_orig)
    Y_prediction_train = predict(parameters, train_set_x_orig)

    # Print train/test Errors
    # The average absolute difference between predicted points and actual points
    # GOAL RIGHT NOW to get UNDER 9 for test set
    print("train accuracy: {}".format(np.mean(np.abs(Y_prediction_train - train_set_y))))
    print("test accuracy: {}".format(np.mean(np.abs(Y_prediction_test - test_set_y))))

    if save:
        save_predictions(test_set_y,Y_prediction_test,names)
Exemple #2
0
def test_image(model, image_path):
    classes = model['metadata']['classes']
    print (classes)
    img = Image.open(image_path)
    plt.imshow(img)
    new_shape = model['metadata']['input_size']
    img = img.resize(new_shape, Image.ANTIALIAS)
    img, _, _ = standardize(img, model['metadata']['train_mean'], model['metadata']['train_deviation'])
    img = np.array(img).reshape((new_shape[0]*new_shape[1]*3, 1))
    prediction = np.squeeze(NN.predict(model, img))
    if prediction:
        print ('The given model predicts this as a CAT image')
    else:
        print ('The given model predicts this as a NON-CAT image')
Exemple #3
0
def main():

    model = None
    while True:
        try:
            print("\n1.Process images ")
            print("2.Fit new model")
            print("3.Load existing model")
            print("4.Predict test data")
            select = int(input("Please enter a number: "))

            if select == 1:
                print("\n\tProccesing train images...")
                loadImages("images/TRAIN", True)
                print("\tDONE! Train images are processed.")

                print("\n\tProccesing test images...")
                loadImages("images/TEST", True)
                print("\tDONE! Test images are loaded.")

            elif select == 2:
                model = fitModel(epochs, batch_size)
            elif select == 3:
                try:
                    model = loadModel("network.h5")
                except OSError:
                    print(
                        "\n!!! Model doesn't exist. You need first to fit model."
                    )
            elif select == 4:
                if model is not None:
                    predict(model)
                else:
                    print("\n!!! You must first fit or load model")
        except ValueError:
            print("\n!!! That was no valid number.  Try again...")
Exemple #4
0
def conf_matrix(X_test,y_test, model):
    rows_X, cols_X = X_test.shape
    y_true = []
    y_pred = []
    for i in range(cols_X):
        test = X_test[:,i]
        p = nn.predict(test, model)
        y_pred.append(int(p))
        r = y_test[:,i]
        if r[0]==1:
            y_true.append(0)
        elif r[1]==1:
            y_true.append(1)
        elif r[2]==1:
            y_true.append(2)   
    r = confusion_matrix(y_true,y_pred)
    r = np.asmatrix(r).transpose()
    return r
def update_years_of_experience_input(quarter,
									 minute,
									 second,
									 down,
									 togo,
									 yardline,
									 formation,
									 yardlinedirection,
									 offenseteam,
									 defenseteam):
    
	



    if years_of_experience is not None and years_of_experience is not '':
        try:
            salary = nn.predict(model, float(years_of_experience))
            return salary
        except ValueError:
            return 'Unable to run'
Exemple #6
0
def hyperparameter_search():
    X, Y = neural_network.generate_dataset(8)
    layers_shape = (8, 3, 8)
    learning_performance = []
    for learning_rate in [0, 0.03, 0.03, 0.1, 0.3, 1, 3, 10, 30, 100]:
        for iterations in [0, 3, 10, 30, 100, 300, 1000, 3000, 10000]:
            print("learning rate", learning_rate, "iterations", iterations)
            # `parameters` are the weights and biases
            parameters = neural_network.train_model(
                X,
                Y,
                layers_shape,
                learning_rate=learning_rate,
                iterations=iterations,
                log_costs=False,
                save_graph=False)

            numCorrect = 0
            for x in X:
                x = np.array([x]).T
                h, predicted_label = neural_network.predict(x, parameters)
                if np.array_equiv(x, predicted_label):
                    numCorrect += 1
            learning_performance.append(
                [learning_rate, iterations, numCorrect / x.size])

    # create diagram

    frame = pd.DataFrame(learning_performance,
                         columns=["Learning rate", "#Iterations", "Accuracy"])
    wide_frame = pd.pivot_table(frame,
                                values="Accuracy",
                                index=["Learning rate"],
                                columns="#Iterations")
    sns.heatmap(wide_frame, cmap="viridis", annot=True)

    plt.savefig("learning_performance.png")
    plt.close()
Exemple #7
0
    y_min, y_max = X[:, 0].min() - .5, X[:, 0].max() + .5
    h = 0.01

    # Generate a grid of points with a distance h between them
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))

    # Predict the function value for the whole grid
    Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)

    # Plot the contour and training examples
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)


np.random.seed(0)
X, y = make_moons(200, noise=0.20)
plt.scatter(X[:, 0], X[:, 1], s=40, c=y, cmap=plt.cm.Spectral)

plt.figure(figsize=(16, 32))
hidden_layer_dimensions = [1, 2, 3, 4]
for i, nn_hdim in enumerate(hidden_layer_dimensions):
    plt.subplot(5, 2, i + 1)
    plt.title("HiddenLayerSize % d" % nn_hdim)
    model = build_model(X, y, nn_hdim, print_loss=False)
    plot_decision_boundary(lambda x: predict(model, x), X, y)
    poop = np.array([1.6, -0.4])
    print(predict(model, poop))
plt.show()
Exemple #8
0
    y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
    h = 0.01
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)


#X = np.array([[-2, 1], [1, 1], [1.5, -0.5], [-2, -1], [-1, -1.5], [2, -2]])
#Y = np.array([[0, 1], [0, 1], [0, 1], [1, 0], [1, 0], [1, 0]])
#b = nn.build_model(X, Y, 4, print_loss=True)

np.random.seed(0)
X, y = make_moons(200, noise=0.20)
#nn.build_model(X, y, 4, 200000,print_loss=True)
plt.scatter(X[:, 0], X[:, 1], s=40, c=y, cmap=plt.cm.Spectral)

plt.figure(figsize=(16, 32))
hidden_layer_dimensions = [1, 2, 3, 4]
for i, nn_hdim in enumerate(hidden_layer_dimensions):
    plt.subplot(5, 2, i + 1)
    plt.title('HiddenLayerSize%d' % nn_hdim)
    model = nn.build_model(X, y, nn_hdim, 200000, print_loss=True)
    #nn.plot_decision_boundary(lambda x: nn.predict(model, x), X, y)
    plot_decision_boundary(
        lambda X: np.array([nn.predict(model, x) for x in X]), X, y)

plt.savefig('foo.png')
Exemple #9
0
            blue_is_closer, dist = f.rectangle_to_closest_line_distance(
                BlueA, BlueB, GreenA, GreenB, newbox)

            if dist < limit:
                if blue_is_closer:
                    if f.above_line(BlueA, BlueB, newbox):
                        if f.rectangle_close_to_line(BlueA, BlueB, newbox,
                                                     rectangle_distance_limit):
                            # frame, newbox(~rect), kernel
                            image = frame.copy()
                            A, B, C, D = f.rect_to_points(newbox)
                            xmin, xmax, ymin, ymax = f.get_offset_values(
                                A, B, C, D)
                            output_shape, input_image = f.pre_detect(
                                frame, kernel, xmin, xmax, ymin, ymax)
                            number = nn.predict(input_image)
                            if number == last_blue_predicted_number:
                                if output_shape[0] == last_blue_roi_shape[
                                        0] and output_shape[
                                            1] == last_blue_roi_shape[1]:
                                    print()
                                else:
                                    suma = suma + number
                                    last_blue_roi_shape = output_shape
                                    last_blue_predicted_number = number
                            else:
                                suma = suma + number
                                last_roi_shape = output_shape
                                last_predicted_number = number
                            print(number)
Exemple #10
0
def read_doc(file, mode, read_up, read_low, read_digit):
    """
    read data from specified document
    :param
        file: the image to be read
    :param
        mode: type of the document (train/test)
    :param
        read_up: whether to work with uppercase classes
    :param
        read_low: whether to work with lowercase classes
    :param
        read_digit: whether to work with digit classes
    :return:
        dictionary of results for the server
    """

    # get cnn model and graph
    global model
    global graph

    # dictionary with result
    result = {}

    # convert file to numpy array
    file = np.fromstring(file.read(), np.uint8)

    # extract data to predict and original image to show
    if mode == 'train':
        data = pdf_read.read_train(file)
        org_images = data[1][1]
        inputs = data[1][0]

        # evaluate the model on the training data
        if BACKEND == 'tensorflow':  # workaround for tensorflow bug with django
            with graph.as_default():
                result.update(
                    train_evaluate(data, read_up, read_low, read_digit))
        else:
            result.update(train_evaluate(data, read_up, read_low, read_digit))

    else:
        data = pdf_read.read_test(file)
        org_images = data[1]
        inputs = data[0]

    # predict the results
    if BACKEND == 'tensorflow':  # workaround for tensorflow bug with django
        with graph.as_default():
            text = neural_network.predict(
                model, inputs,
                get_ignored_classes(read_up, read_low, read_digit))
    else:
        text = neural_network.predict(
            model, inputs, get_ignored_classes(read_up, read_low, read_digit))

    # add text and images into one list
    read_data = list(map(list, zip(text, org_images)))

    # split data into lines
    read_data = split_array(read_data, LINE_BREAK)

    # add reading results
    result.update({'read_data': read_data})
    return result
Exemple #11
0

def plot_decision_boundary(pred_func, X, y):
    x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
    y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
    h = .01
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)


X, y = make_moons(200, noise=0.20)

plt.scatter(X[:, 0], X[:, 1], s=40, c=y, cmap=plt.cm.Spectral)
plt.show()

plt.figure()
hidden_layer_dimensions = [1, 2, 4, 5]
# hidden_layer_dimensions = [10, 20, 50, 100]

for i, nn_hdim in enumerate(hidden_layer_dimensions):
    plt.subplot(2, 2, i + 1, aspect='equal')
    plt.title('Hidden Layer Size : ' + str(nn_hdim))
    model = nn.build_model(X, y, nn_hdim, 30000, True)
    plot_decision_boundary(lambda x: nn.predict(model, x), X, y)

plt.show()
Exemple #12
0
def find_numbers_clean(frame, lines_points, suma, blue_numbers, green_numbers):
    suma = suma

    frame_for_numbers = frame.copy()

    gray = cv2.cvtColor(frame_for_numbers, cv2.COLOR_BGR2GRAY)
    ret, th = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY)

    th_cont_img = th.copy()

    img, contours, hierarchy = cv2.findContours(th_cont_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    hierarchy = hierarchy[0]

    number_contours = []
    number_rect = []

    BlueA = lines_points[0][0]
    BlueB = lines_points[0][1]
    GreenA = lines_points[1][0]
    GreenB = lines_points[1][1]

    for contour in contours:
        center, size, angle = cv2.minAreaRect(contour)
        (x, y, w, h) = cv2.boundingRect(contour)
        width, height = size
        if width > 1 and width < 30 and height > 1 and height < 30:
            if width > 9 or height > 9:
                aboveBlue = above_line(BlueA, BlueB, (x, y, w, h))
                aboveGreen = above_line(GreenA, GreenB, (x, y, w, h))
                if aboveGreen or aboveBlue:
                    number_contours.append(contour) #not used
                    number_rect.append((x, y, w, h))

    img = frame.copy()
    #cv2.drawContours(img, boxes, -1, (0, 255, 255), 1)



    minDistBlue = 0
    minDistGreen = 0

    #print(len(number_rect))
    for rect in number_rect:
        A, B, C, D = rect_to_points(rect)

        limit = 2

        # Check if close to Blue line
        closeToBlueLine = rectangle_close_to_line(BlueA, BlueB, rect, limit)
        closeToGreenLine = rectangle_close_to_line(GreenA, GreenB, rect, limit)

        if closeToBlueLine or closeToGreenLine:
            # Rectangle is close to one line:
            # Udaljenost temena A(x,y) od linija
            dA_b = point_to_line_distance(BlueA, BlueB, A)
            dA_g = point_to_line_distance(GreenA, GreenB, A)

            # Udaljenost temena B(x+w, y) od linija
            dB_b = point_to_line_distance(BlueA, BlueB, B)
            dB_g = point_to_line_distance(GreenA, GreenB, B)

            # Udaljenost temana C(x, y+h) od linija
            dC_b = point_to_line_distance(BlueA, BlueB, C)
            dC_g = point_to_line_distance(GreenA, GreenB, C)

            # Udaljeost temena D(x+w, y+h) od linija
            dD_b = point_to_line_distance(BlueA, BlueB, D)
            dD_g = point_to_line_distance(GreenA, GreenB, D)

            minDistBlue = min(dA_b, dB_b, dC_b, dD_b)
            minDistGreen = min(dA_g, dB_g, dC_g, dD_g)

            xmin, xmax, ymin, ymax = get_offset_values(A, B, C, D)

            region = frame.copy()
            kernel = np.ones((3, 3), np.uint8)

            if minDistBlue < minDistGreen:
                # Paint rectangle in blue
                if above_line(BlueA, BlueB, rect):
                    # #cv2.drawContours(img, [box], 0, (255, 0, 0), 2)
                    # cv2.rectangle(img, A, D, (255, 0, 0), 2)
                    # # Add number
                    # roi = region[ymin: ymax, xmin: xmax]
                    #
                    # blank_image = np.zeros((28,28,3), np.uint8)
                    # x_offset = (28-roi.shape[0])/2
                    # y_offset = (28-roi.shape[1])/2
                    #
                    # x_offset = int(round(x_offset))
                    # y_offset = int(round(y_offset))
                    #
                    # blank_image[x_offset: x_offset+roi.shape[0], y_offset: y_offset+roi.shape[1]] = roi
                    #
                    # # ppp = blank_image.copy()
                    # # plt.imshow(ppp)
                    # # plt.show()
                    #
                    # gray = cv2.cvtColor(blank_image, cv2.COLOR_BGR2GRAY)
                    # ret, th = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY)
                    # th = cv2.morphologyEx(th, cv2.MORPH_OPEN, kernel)
                    # th = cv2.resize(th, (28, 28), interpolation=cv2.INTER_AREA)
                    # input = (np.expand_dims(th, 0))
                    output_shape, input = pre_detect(frame, kernel, xmin, xmax, ymin, ymax)

                    number = nn.predict(input)
                    number = number[0]
                    addNumber = False
                    # Check blue_numbers
                    for last_blue_predicted_number, last_blue_roi_shape in blue_numbers:
                        if number == last_blue_predicted_number:
                            if output_shape[0] == last_blue_roi_shape[0] and output_shape[1] == last_blue_roi_shape[1]:
                                print("Isti su")
                                addNumber = False
                                break
                            else:
                                # Dodaj u listu
                                addNumber = True
                        else:
                            addNumber = True
                    if addNumber:
                        suma = suma + number
                        value = [number, output_shape]
                        blue_numbers.append(value)
                    # if number == last_blue_predicted_number:
                    #     if output_shape[0] == last_blue_roi_shape[0] and output_shape[1] == last_blue_roi_shape[1]:
                    #         print()
                    #     else:
                    #         suma = suma + number
                    #         last_blue_roi_shape = output_shape
                    #         last_blue_predicted_number = number
                    # else:
                    #     suma = suma + number
                    #     last_blue_roi_shape = output_shape
                    #     last_blue_predicted_number = number
                    print(number)
            else:
                if above_line(GreenA, GreenB, rect):
                    # #cv2.drawContours(img, [box], 0, (0, 255, 0), 2)
                    # cv2.rectangle(img, A, D, (0, 255, 0), 2)
                    # # Substract number
                    # roi = region[ymin: ymax, xmin: xmax]
                    #
                    # blank_image = np.zeros((28, 28, 3), np.uint8)
                    # x_offset = (28 - roi.shape[0]) / 2
                    # y_offset = (28 - roi.shape[1]) / 2
                    #
                    # x_offset = int(round(x_offset))
                    # y_offset = int(round(y_offset))
                    #
                    # blank_image[x_offset: x_offset + roi.shape[0], y_offset: y_offset + roi.shape[1]] = roi
                    #
                    # # ppp = blank_image.copy()
                    # # plt.imshow(ppp)
                    # # plt.show()
                    #
                    # gray = cv2.cvtColor(blank_image, cv2.COLOR_BGR2GRAY)
                    # ret, th = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY)
                    # th = cv2.morphologyEx(th, cv2.MORPH_OPEN, kernel)
                    # th = cv2.resize(th, (28, 28), interpolation=cv2.INTER_AREA)
                    # input = (np.expand_dims(th, 0))
                    output_shape, input = pre_detect(frame, kernel, xmin, xmax, ymin, ymax)
                    number = nn.predict(input)
                    number = number[0]
                    minusNumber = False
                    # Check green_numbers
                    for last_green_predicted_number, last_green_roi_shape in green_numbers:
                        if number == last_green_predicted_number:
                            if output_shape[0] == last_green_roi_shape[0] and output_shape[1] == last_green_roi_shape[1]:
                                print("Isti su")
                                minusNumber = False
                                break
                            else:
                                minusNumber = True
                        else:
                            minusNumber = True
                    if minusNumber:
                        suma = suma - number
                        value = [number, output_shape]
                        green_numbers.append(value)

                    # if number == last_green_predicted_number:
                    #     if output_shape[0] == last_green_roi_shape[0] and output_shape[1] == last_green_roi_shape[1]:
                    #         print()
                    #     else:
                    #         suma = suma - number
                    #         last_green_roi_shape = output_shape
                    #         last_green_predicted_number = number
                    # else:
                    #     suma = suma - number
                    #     last_green_roi_shape = output_shape
                    #     last_green_predicted_number = number
                    print(number)

        else:
            # Rectangle is not close any line:
            #cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
            cv2.rectangle(img, A, D, (0, 0, 255), 2)

    retVal = []
    retVal.append(blue_numbers)
    retVal.append(green_numbers)

    return img, suma, retVal
Exemple #13
0
def find_numbers(frame, lines_points, suma):
    suma = suma

    frame_for_numbers = frame.copy()

    gray = cv2.cvtColor(frame_for_numbers, cv2.COLOR_BGR2GRAY)
    ret, th = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY)

    th_cont_img = th.copy()

    img, contours, hierarchy = cv2.findContours(th_cont_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    hierarchy = hierarchy[0]

    number_contours = []
    number_rect = []

    BlueA = lines_points[0][0]
    BlueB = lines_points[0][1]
    GreenA = lines_points[1][0]
    GreenB = lines_points[1][1]

    for contour in contours:
        center, size, angle = cv2.minAreaRect(contour)
        (x, y, w, h) = cv2.boundingRect(contour)
        width, height = size
        if width > 1 and width < 30 and height > 1 and height < 30:
            if width > 9 or height > 9:
                aboveBlue = above_line(BlueA, BlueB, (x, y, w, h))
                aboveGreen = above_line(GreenA, GreenB, (x, y, w, h))
                if aboveGreen or aboveBlue:
                    number_contours.append(contour) #not used
                    number_rect.append((x, y, w, h))

    img = frame.copy()
    #cv2.drawContours(img, boxes, -1, (0, 255, 255), 1)



    minDistBlue = 0
    minDistGreen = 0

    #print(len(number_rect))
    for rect in number_rect:
        #(A, B, C, D) = rect
        (x, y, w, h) = rect
        A = (x, y)
        B = (x + w, y)
        C = (x, y + h)
        D = (x + w, y + h)

        limit = 2

        # Check if close to Blue line
        closeToBlueLine = rectangle_close_to_line(BlueA, BlueB, rect, limit)
        closeToGreenLine = rectangle_close_to_line(GreenA, GreenB, rect, limit)

        if closeToBlueLine or closeToGreenLine:
            # Rectangle is close to one line:
            # Udaljenost temena A(x,y) od linija
            dA_b = point_to_line_distance(BlueA, BlueB, A)
            dA_g = point_to_line_distance(GreenA, GreenB, A)

            # Udaljenost temena B(x+w, y) od linija
            dB_b = point_to_line_distance(BlueA, BlueB, B)
            dB_g = point_to_line_distance(GreenA, GreenB, B)

            # Udaljenost temana C(x, y+h) od linija
            dC_b = point_to_line_distance(BlueA, BlueB, C)
            dC_g = point_to_line_distance(GreenA, GreenB, C)

            # Udaljeost temena D(x+w, y+h) od linija
            dD_b = point_to_line_distance(BlueA, BlueB, D)
            dD_g = point_to_line_distance(GreenA, GreenB, D)

            minDistBlue = min(dA_b, dB_b, dC_b, dD_b)
            minDistGreen = min(dA_g, dB_g, dC_g, dD_g)

            ys = []
            xs = []
            ys.append(A[1])
            ys.append(B[1])
            ys.append(C[1])
            ys.append(D[1])

            xs.append(A[0])
            xs.append(B[0])
            xs.append(C[0])
            xs.append(D[0])

            ymin = min(ys)
            ymax = max(ys)
            xmin = min(xs)
            xmax = max(xs)

            region = frame.copy()
            kernel = np.ones((3, 3), np.uint8)

            if minDistBlue < minDistGreen:
                # Paint rectangle in blue
                if above_line(BlueA, BlueB, rect):
                    #cv2.drawContours(img, [box], 0, (255, 0, 0), 2)
                    cv2.rectangle(img, A, D, (255, 0, 0), 2)
                    # Add number
                    roi = region[ymin: ymax, xmin: xmax]

                    blank_image = np.zeros((28,28,3), np.uint8)
                    x_offset = (28-roi.shape[0])/2
                    y_offset = (28-roi.shape[1])/2

                    x_offset = int(round(x_offset))
                    y_offset = int(round(y_offset))

                    blank_image[x_offset: x_offset+roi.shape[0], y_offset: y_offset+roi.shape[1]] = roi

                    # ppp = blank_image.copy()
                    # plt.imshow(ppp)
                    # plt.show()

                    gray = cv2.cvtColor(blank_image, cv2.COLOR_BGR2GRAY)
                    ret, th = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY)
                    th = cv2.morphologyEx(th, cv2.MORPH_OPEN, kernel)
                    th = cv2.resize(th, (28, 28), interpolation=cv2.INTER_AREA)
                    input = (np.expand_dims(th, 0))

                    number = nn.predict(input)
                    #print(number)

                    #print("Number:")
                    #print(number)
                    suma += number
            else:
                if above_line(GreenA, GreenB, rect):
                    #cv2.drawContours(img, [box], 0, (0, 255, 0), 2)
                    cv2.rectangle(img, A, D, (0, 255, 0), 2)
                    # Substract number
                    roi = region[ymin: ymax, xmin: xmax]

                    blank_image = np.zeros((28, 28, 3), np.uint8)
                    x_offset = (28 - roi.shape[0]) / 2
                    y_offset = (28 - roi.shape[1]) / 2

                    x_offset = int(round(x_offset))
                    y_offset = int(round(y_offset))

                    blank_image[x_offset: x_offset + roi.shape[0], y_offset: y_offset + roi.shape[1]] = roi

                    # ppp = blank_image.copy()
                    # plt.imshow(ppp)
                    # plt.show()

                    gray = cv2.cvtColor(blank_image, cv2.COLOR_BGR2GRAY)
                    ret, th = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY)
                    th = cv2.morphologyEx(th, cv2.MORPH_OPEN, kernel)
                    th = cv2.resize(th, (28, 28), interpolation=cv2.INTER_AREA)
                    input = (np.expand_dims(th, 0))

                    number = nn.predict(input)
                    #print(number)
                    suma -=  number

        else:
            # Rectangle is not close any line:
            #cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
            cv2.rectangle(img, A, D, (0, 0, 255), 2)


    return img, suma
print('\n\n')

print('Check gradients')
check_nn_gradients()

print('\nChecking Backpropagation (w/ Regularization) ... \n')

#  Check gradients by running checkNNGradients
_lambda = 3
check_nn_gradients(_lambda)

# Also output the costFunction debugging values

debug_J = compute_cost(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y_test, _lambda)

print('\n\nCost at (fixed) debugging parameters (w/ lambda = 3): ', debug_J,
      '\n(this value should be about 0.576051)')

print('\nPress enter to start training:')
input()
print('\nTraining Neural Network... \n')

theta1, theta2 = train_model(X, y_test, input_layer_size, hidden_layer_size, num_labels)

print('\nVisualizing Neural Network... \n')
#display_data(theta1[:, 1:])

# predict using trained parameters
pred = predict(theta1, theta2, X)
print('\nTraining Set Accuracy: ', np.mean(pred == y_test) * 100)
Exemple #15
0
import neural_network as nn

data_set = [[2.7810836, 2.550537003, 0],
            [1.465489372, 2.362125076, 0],
            [3.396561688, 4.400293529, 0],
            [1.38807019, 1.850220317, 0],
            [3.06407232, 3.005305973, 0],
            [7.627531214, 2.759262235, 1],
            [5.332441248, 2.088626775, 1],
            [6.922596716, 1.77106367, 1],
            [8.675418651, -0.242068655, 1],
            [7.673756466, 3.508563011, 1]]

train = data_set
test = train[4:6]

n_inputs = 2   # number of features
n_hidden_layers = 1  # how many hidden layers
n_hidden_neurons = 5  # how many neuron per hidden layer
n_outputs = 2  # number of classes
my_neural_network = nn.initialize_network(n_inputs, n_hidden_layers, n_hidden_neurons, n_outputs)
n_epoch = 40
l_rate = .3
nn.train_the_network(my_neural_network, train, l_rate, n_epoch, n_outputs)
for row in test:
    predict = nn.predict(my_neural_network, row)
    print('actual=>', row[-1], 'predicted=>', predict)
## Load training data
X = digits.data
y = digits.target[:, np.newaxis]

## Load theta weights into a list of thetas
thetas = nn.create_hidden_layer_thetas(nn_layers_info)

## Unroll parameters
nn_params = nn.unroll_parameters(thetas)

## Training neural network
# optimal_thetas = nn.fmincg(nn_params, X, y, nn_layers_info, lambda_reg=0)                             # Using advanced algorithm fmincg
optimal_thetas = nn.gradient_decsend(nn_params,
                                     X,
                                     y,
                                     nn_layers_info,
                                     lambda_reg,
                                     0.53,
                                     300,
                                     plot=True)  # Handwritten gradient descend

## Predict - Compare result to training labels
prediction = nn.predict(optimal_thetas, X)
accuracy = np.mean(prediction == y) * 100
print("# Training set accuracy: {}".format(accuracy))

## Save trained weights
if accuracy >= 90:
    np.save("8x8_weights.npy", nn.unroll_parameters(optimal_thetas))
Exemple #17
0
        a.array(temp[3], temp[2], temp[0], hunger)
        a.plot()
        e = Eliminator(user_info[0][3])
        e.elimination()
        e.plot()
        elimination_array = np.load("eliminated_array.npy")
        elim_time = np.array(elimination_array[minute], ndmin=2)
        bac_array.append(elimination_array[minute])

        # trains neural network every 5 inputs and lets it predict the drunkeness
        user_input_array.append(how_drunk)
        if len(bac_array) % 5 == 0:
            neural_network.learn(bac_array, user_input_array)
        if len(bac_array) == 1:
            neural_network.learn(bac_array, user_input_array)
        prediction = neural_network.predict(elim_time)
        print(prediction)
        np.save("drinking_session.npy", user_info)
    # informs the elimination rate adjuster on how the elimination rate will have to be adjusted
    elif decision == 2:
        sober_time = 0

        min = int(input("How many minutes ago did you sober up? \n"))
        sober_time = minute - min
        e.adjustment(sober_time)
    elif decision == 3:
        time = input("What time is it? (enter in hh:mm format) \n")
        hour = int((time[0] + time[1]))
        minute = int((time[3] + time[4]))
        minute += hour * 60
        temp.append(minute)
Exemple #18
0
 def on_button_release(self, event):
     self.matrix.print_matrix()
     str_prediction.set(predict(self.matrix.as_list()))
Exemple #19
0
clf.fit(X.T, Y.T);

# Plot the decision boundary for logistic regression
plot_decision_boundary(lambda x: clf.predict(x), X, Y)
plt.title("Logistic Regression")

# Print accuracy
LR_predictions = clf.predict(X.T)
print ('Accuracy of logistic regression: %d ' % float((np.dot(Y,LR_predictions) + np.dot(1-Y,1-LR_predictions))/float(Y.size)*100) +
       '% ' + "(percentage of correctly labelled datapoints)")

# Build a model with a n_h-dimensional hidden layer
parameters = nn_model(X, Y, n_h = 4, num_iterations = 10000, print_cost=True)

# Plot the decision boundary
plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
plt.title("Decision Boundary for hidden layer size " + str(4))

'''
    Accuracy is really high compared to Logistic Regression. The model has learnt
    the leaf patterns of the flower! Neural networks are able to learn even
    highly non-linear decision boundaries, unlike logistic regression.
'''

# Tuning hidden layer size
plt.figure(figsize=(16, 32))
hidden_layer_sizes = [1, 2, 3, 4, 5, 20, 50]
for i, n_h in enumerate(hidden_layer_sizes):
    plt.subplot(5, 2, i+1)
    plt.title('Hidden Layer of size %d' % n_h)
    parameters = nn_model(X, Y, n_h, num_iterations = 5000)