Пример #1
0
    def test_network(self):
        '''
        Load in the neural network the 4 images of the previous stage of
        sequential way, giving the output 4 images with shape (1, 512, 512, 1)
        '''
        model = Network('./docs/aerial_model.h5')

        img_test_1 = cv2.imread('test/img/test_scale_image_1.png',
                                cv2.IMREAD_GRAYSCALE)
        img_test_2 = cv2.imread('test/img/test_scale_image_2.png',
                                cv2.IMREAD_GRAYSCALE)
        img_test_3 = cv2.imread('test/img/test_scale_image_3.png',
                                cv2.IMREAD_GRAYSCALE)
        img_test_4 = cv2.imread('test/img/test_scale_image_4.png',
                                cv2.IMREAD_GRAYSCALE)

        img_test_list = [img_test_1, img_test_2, img_test_3, img_test_4]

        test_output_network = []
        for idx, item in enumerate(img_test_list):
            X_test, Y_test = model.predict(img_test_list[idx])
            test_output_network.append(Y_test)

        self.assertEqual(np.shape(test_output_network[0]), (1, 512, 512, 1))
        self.assertEqual(np.shape(test_output_network[1]), (1, 512, 512, 1))
        self.assertEqual(np.shape(test_output_network[2]), (1, 512, 512, 1))
        self.assertEqual(np.shape(test_output_network[3]), (1, 512, 512, 1))
Пример #2
0
def test_networks():

    input_size = 20
    output_size = 3
    batch_size = 5
    eps_decimal = 4
    seed = 1
    layers = [(10, SigmoidActivation(), True),
              (output_size, SigmoidActivation(), True)]
    network = Network(input_size, layers, LogError(), seed)

    model = Sequential()
    model.add(
        Dense(10,
              use_bias=True,
              input_shape=(input_size, ),
              activation="sigmoid"))
    model.add(
        Dense(output_size,
              use_bias=True,
              input_shape=(input_size, ),
              activation="sigmoid"))
    model.compile(optimizer="sgd", loss="binary_crossentropy")

    model.layers[0].set_weights(
        [network.layers[0].w, network.layers[0].b.flatten()])
    model.layers[1].set_weights(
        [network.layers[1].w, network.layers[1].b.flatten()])

    x = np.random.rand(batch_size, input_size)
    y = np.random.rand(batch_size, output_size)

    loss = model.evaluate(x, y, verbose=2)

    output = model.predict(x)
    output2 = network.predict(x)
    loss2 = network.evaluate(x, y)

    # equal outputs
    np.testing.assert_almost_equal(output, output2, decimal=eps_decimal)

    # equal loss
    np.testing.assert_almost_equal(loss, loss2, decimal=eps_decimal)

    # equal weights and biases derivatives
    [dw0, db0, dw1, db1] = get_weight_grad(model, x, y)
    np.testing.assert_almost_equal(db1,
                                   network.layers[1].db,
                                   decimal=eps_decimal)
    np.testing.assert_almost_equal(dw1,
                                   network.layers[1].dw,
                                   decimal=eps_decimal)
    np.testing.assert_almost_equal(db0,
                                   network.layers[0].db,
                                   decimal=eps_decimal)
    np.testing.assert_almost_equal(dw0,
                                   network.layers[0].dw,
                                   decimal=eps_decimal)
Пример #3
0
def calculate_accuacy(network: Network,
                      df: pd.DataFrame,
                      multiclass: bool = False):

    good_counter = 0
    for i in range(len(df)):
        z = network.predict(np.array(df.iloc[i, :-1]))
        if multiclass:
            good_counter += 1 if np.argmax(z,
                                           axis=1) == df['cls'][i] - 1 else 0
        else:
            good_counter += 1 if (z.item() > 0.5 and df['cls'][i] == 2) or (
                z.item() < 0.5 and df['cls'][i] == 1) else 0

    return good_counter / len(df)
Пример #4
0
def plot_regression(network: Network, test_df: pd.DataFrame, num: float = 100):
    """
    Plot regression line of the network on top of the test data.
    num - number of points evaluated.
    """
    x_min, x_max = test_df['x'].min(), test_df['x'].max()

    xx = np.linspace(x_min, x_max, num)

    Z = np.zeros(xx.ravel().shape)
    for i, x in enumerate(xx.ravel()):
        Z[i] = network.predict(np.array([[x]]))

    plt.scatter(test_df['x'], test_df['y'], c='c', alpha=0.1)
    plt.plot(xx, Z, c='orange', linewidth=2, alpha=0.9)
    plt.show()
Пример #5
0
def plot_decision_boundary(network: Network,
                           test_df: pd.DataFrame,
                           h: float = .02):
    """
    Plots decision boundary of the classifier.
    h - spacing in a meshgrid.
    """

    x_min, x_max = test_df['x'].min(), test_df['x'].max()
    y_min, y_max = test_df['y'].min(), test_df['y'].max()

    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))

    Z = np.zeros(xx.ravel().shape)
    for i, (x, y) in enumerate(zip(xx.ravel(), yy.ravel())):
        z = network.predict(np.array([[x, y]]))
        if (z.shape[1] == 1):
            Z[i] = 0 if z < 0.5 else 1
        else:
            Z[i] = np.argmax(z, axis=1)
    Z = Z.reshape(xx.shape)
    if z.shape[1] == 1:
        plt.scatter(test_df['x'],
                    test_df['y'],
                    c=test_df['cls'],
                    cmap=plt.cm.viridis,
                    alpha=0.9)
    else:
        plt.scatter(test_df['x'],
                    test_df['y'],
                    c=test_df['cls'],
                    cmap=plt.cm.Dark2,
                    alpha=0.9)
    plt.contourf(xx, yy, Z, cmap=plt.cm.Dark2, alpha=0.3)
    plt.show()
Пример #6
0
def calculate_mse(network: Network, df: pd.DataFrame):

    P = network.predict(np.array(df.iloc[:, :-1]))
    P = P.flatten()
    return np.average((P - df.iloc[:, -1]) * (P - df.iloc[:, -1]))