def test_evaluate():
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()
    number_of_train_samples_to_use = 500
    number_of_test_samples_to_use = 200
    X_train = X_train[0:number_of_train_samples_to_use, :]
    y_train = y_train[0:number_of_train_samples_to_use]
    X_test = X_test[0:number_of_test_samples_to_use, :]
    y_test = y_test[0:number_of_test_samples_to_use]
    my_cnn = CNN()
    my_cnn.add_input_layer(shape=(32, 32, 3), name="input")
    my_cnn.append_conv2d_layer(num_of_filters=32,
                               kernel_size=(3, 3),
                               padding="same",
                               activation='relu',
                               name="conv1")
    my_cnn.append_maxpooling2d_layer(pool_size=(2, 2), name="pool1")
    my_cnn.append_conv2d_layer(num_of_filters=64,
                               kernel_size=3,
                               activation='relu',
                               name="conv2")
    my_cnn.append_maxpooling2d_layer(pool_size=(2, 2), name="pool2")
    my_cnn.append_conv2d_layer(num_of_filters=64,
                               kernel_size=3,
                               activation='relu',
                               name="conv3")
    my_cnn.append_flatten_layer(name="flat1")
    my_cnn.append_dense_layer(num_nodes=64, activation="relu", name="dense1")
    my_cnn.append_dense_layer(num_nodes=10,
                              activation="softmax",
                              name="dense2")
    np.random.seed(seed=1)
    weigh = my_cnn.get_weights_without_biases(layer_name="conv1")
    w_set = np.random.rand(*weigh.shape)
    my_cnn.set_weights_without_biases(w_set, layer_name="conv1")
    np.random.seed(seed=1)
    weigh = my_cnn.get_weights_without_biases(layer_name="conv2")
    w_set = np.random.rand(*weigh.shape)
    my_cnn.set_weights_without_biases(w_set, layer_name="conv2")
    np.random.seed(seed=1)
    weigh = my_cnn.get_weights_without_biases(layer_name="conv3")
    w_set = np.random.rand(*weigh.shape)
    my_cnn.set_weights_without_biases(w_set, layer_name="conv3")
    np.random.seed(seed=1)
    weigh = my_cnn.get_weights_without_biases(layer_name="dense1")
    w_set = np.random.rand(*weigh.shape)
    my_cnn.set_weights_without_biases(w_set, layer_name="dense1")
    np.random.seed(seed=1)
    weigh = my_cnn.get_weights_without_biases(layer_name="dense2")
    w_set = np.random.rand(*weigh.shape)
    my_cnn.set_weights_without_biases(w_set, layer_name="dense2")
    my_cnn.set_loss_function()
    my_cnn.set_optimizer(optimizer="SGD", learning_rate=0.01, momentum=0.0)
    my_cnn.set_metric(metric="accuracy")
    # los = np.array([2.30277, 2.30264, 2.30242, 2.30225, 2.30207, 2.30190, 2.30171, 2.30154, 2.30138])
    # los = np.around(los,decimals=2)
    my_cnn.train(X_train, y_train, 60, 10)
    acc = my_cnn.evaluate(X_test, y_test)
    de = np.float32(0.07)
    assert (acc == de)
Esempio n. 2
0
def test_set_weights_without_biases():
    my_cnn = CNN()
    image_size = (np.random.randint(32, 100), np.random.randint(20, 100),
                  np.random.randint(3, 10))
    number_of_conv_layers = np.random.randint(2, 10)
    my_cnn.add_input_layer(shape=image_size, name="input")
    previous_depth = image_size[2]
    for k in range(number_of_conv_layers):
        number_of_filters = np.random.randint(3, 100)
        kernel_size = np.random.randint(3, 9)
        my_cnn.append_conv2d_layer(num_of_filters=number_of_filters,
                                   kernel_size=(kernel_size, kernel_size),
                                   padding="same",
                                   activation='linear')

        w = my_cnn.get_weights_without_biases(layer_number=k + 1)
        w_set = np.full_like(w, 0.2)
        my_cnn.set_weights_without_biases(w_set, layer_number=k + 1)
        w_get = my_cnn.get_weights_without_biases(layer_number=k + 1)
        print("here 1", w_get.shape)
        print("here 2", w_set.shape)

        assert w_get.shape == w_set.shape
        previous_depth = number_of_filters
    pool_size = np.random.randint(2, 5)
    my_cnn.append_maxpooling2d_layer(pool_size=pool_size,
                                     padding="same",
                                     strides=2,
                                     name="pool1")
    my_cnn.append_flatten_layer(name="flat1")
    my_cnn.append_dense_layer(num_nodes=10)
    number_of_dense_layers = np.random.randint(2, 10)
    previous_nodes = 10
    for k in range(number_of_dense_layers):
        number_of_nodes = np.random.randint(3, 100)
        kernel_size = np.random.randint(3, 9)
        my_cnn.append_dense_layer(num_nodes=number_of_nodes)

        w = my_cnn.get_weights_without_biases(layer_number=k +
                                              number_of_conv_layers + 4)
        w_set = np.full_like(w, 0.8)
        my_cnn.set_weights_without_biases(w_set,
                                          layer_number=k +
                                          number_of_conv_layers + 4)
        w_get = my_cnn.get_weights_without_biases(layer_number=k +
                                                  number_of_conv_layers + 4)
        assert w_get.shape == w_set.shape
        previous_nodes = number_of_nodes
Esempio n. 3
0
def test_get_weights_without_biases_2():
    my_cnn = CNN()
    image_size=(np.random.randint(32,100),np.random.randint(20,100),np.random.randint(3,10))
    number_of_conv_layers=np.random.randint(2,10)
    my_cnn.add_input_layer(shape=image_size,name="input")
    previous_depth=image_size[2]
    for k in range(number_of_conv_layers):
        number_of_filters = np.random.randint(3, 100)
        kernel_size= np.random.randint(3,9)
        my_cnn.append_conv2d_layer(num_of_filters=number_of_filters,
                                   kernel_size=(kernel_size,kernel_size),
                                   padding="same", activation='linear')

        actual = my_cnn.get_weights_without_biases(layer_number=k+1)
        assert actual.shape == (kernel_size,kernel_size,previous_depth,number_of_filters)
        previous_depth=number_of_filters
    actual = my_cnn.get_weights_without_biases(layer_number=0)
    assert actual is None
Esempio n. 4
0
def test_load_and_save_model():
    # Note: This test may take a long time to load the data
    my_cnn = CNN()
    my_cnn.load_a_model(model_name="VGG19")
    w = my_cnn.get_weights_without_biases(layer_name="block5_conv4")
    assert w.shape == (3, 3, 512, 512)
    w = my_cnn.get_weights_without_biases(layer_number=-1)
    assert w.shape == (4096, 1000)
    my_cnn.append_dense_layer(num_nodes=10)
    path = os.getcwd()
    file_path = os.path.join(path, "my_model.h5")
    my_cnn.save_model(model_file_name=file_path)
    my_cnn.load_a_model(model_name="VGG16")
    w = my_cnn.get_weights_without_biases(layer_name="block4_conv1")
    assert w.shape == (3, 3, 256, 512)
    my_cnn.load_a_model(model_file_name=file_path)
    os.remove(file_path)
    w = my_cnn.get_weights_without_biases(layer_number=-1)
    assert w.shape == (1000, 10)
Esempio n. 5
0
def test_get_weights_without_biases_3():
    my_cnn = CNN()
    image_size = (np.random.randint(32, 100), np.random.randint(20, 100),
                  np.random.randint(3, 10))
    number_of_conv_layers = np.random.randint(2, 10)
    my_cnn.add_input_layer(shape=image_size, name="input")
    previous_depth = image_size[2]
    for k in range(number_of_conv_layers):
        number_of_filters = np.random.randint(3, 100)
        kernel_size = np.random.randint(3, 9)
        my_cnn.append_conv2d_layer(num_of_filters=number_of_filters,
                                   kernel_size=(kernel_size, kernel_size),
                                   padding="same",
                                   activation='linear')

        actual = my_cnn.get_weights_without_biases(layer_number=k + 1)
        assert actual.shape == (kernel_size, kernel_size, previous_depth,
                                number_of_filters)
        previous_depth = number_of_filters
    actual = my_cnn.get_weights_without_biases(layer_number=0)
    assert actual is None
    pool_size = np.random.randint(2, 5)
    my_cnn.append_maxpooling2d_layer(pool_size=pool_size,
                                     padding="same",
                                     strides=2,
                                     name="pool1")
    actual = my_cnn.get_weights_without_biases(layer_name="pool1")
    assert actual is None
    my_cnn.append_flatten_layer(name="flat1")
    actual = my_cnn.get_weights_without_biases(layer_name="flat1")
    assert actual is None
    my_cnn.append_dense_layer(num_nodes=10)
    number_of_dense_layers = np.random.randint(2, 10)
    previous_nodes = 10
    for k in range(number_of_dense_layers):
        number_of_nodes = np.random.randint(3, 100)
        kernel_size = np.random.randint(3, 9)
        my_cnn.append_dense_layer(num_nodes=number_of_nodes)
        actual = my_cnn.get_weights_without_biases(layer_number=k +
                                                   number_of_conv_layers + 4)
        # assert actual.shape == (previous_nodes, number_of_nodes)
        previous_nodes = number_of_nodes
Esempio n. 6
0
def test_get_weights_without_biases_1():
    my_cnn = CNN()
    input_size = np.random.randint(32, 100)
    number_of_dense_layers = np.random.randint(2, 10)
    my_cnn.add_input_layer(shape=input_size, name="input")
    previous_nodes = input_size
    for k in range(number_of_dense_layers):
        number_of_nodes = np.random.randint(3, 100)
        kernel_size = np.random.randint(3, 9)
        my_cnn.append_dense_layer(num_nodes=number_of_nodes)
        actual = my_cnn.get_weights_without_biases(layer_number=k + 1)
        assert actual.shape == (previous_nodes, number_of_nodes)
        previous_nodes = number_of_nodes
Esempio n. 7
0
def test_predict():
    X = np.float32([[0.1, 0.2, 0.3, 0.4, 0.5, -0.1, -0.2, -0.3, -0.4, -0.5]])
    X = np.float32([[0.1, 0.2, 0.3, 0.4, 0.5, 0, 0, 0, 0, 0]])
    X = np.float32([np.linspace(0, 10, num=10)])
    my_cnn = CNN()
    my_cnn.add_input_layer(shape=(10, ), name="input0")
    my_cnn.append_dense_layer(num_nodes=5, activation='linear', name="layer1")
    w = my_cnn.get_weights_without_biases(layer_name="layer1")
    w_set = np.full_like(w, 2)
    my_cnn.set_weights_without_biases(w_set, layer_name="layer1")
    b = my_cnn.get_biases(layer_name="layer1")
    b_set = np.full_like(b, 2)
    b_set[0] = b_set[0] * 2
    my_cnn.set_biases(b_set, layer_name="layer1")
    actual = my_cnn.predict(X)
    assert np.array_equal(actual, np.array([[104., 102., 102., 102., 102.]]))