def test_keras_model_train():
    model = Mock()
    layer = Mock
    criterion = Mock()
    optimizer = Mock()
    metrics = Mock()

    sizes = [(1, 24, 24), (24, 10)]

    l1 = layer()
    l1.get_input_shape_at.return_value = sizes[0]
    l2 = layer()
    l2.get_output_shape_at.return_value = sizes[1]
    model.layers = [l1, l2]

    batch = 32
    epoch = 2
    kdpm = DeepLearningModel(model, criterion, optimizer, batch, epoch, metrics)

    num_data = 30
    data = np.array([np.random.rand(24, 24) for i in range(num_data)])
    labels = np.array([np.zeros(10) for i in range(num_data)])
    for l in labels:
        l[np.random.randint(0, len(l))] = 1

    kdpm.train(data, labels)

    kdpm._model.fit.assert_called_once()
    params = kdpm._model.fit.call_args_list[0][1]

    assert np.array_equal(params['x'], data)
    assert np.array_equal(params['y'], labels)
    assert params['batch_size'] == batch
    assert params['epochs'] == epoch
def test_get_model_params():
    model = Mock()
    layer = Mock
    criterion = Mock()
    optimizer = Mock()
    metrics = Mock()

    num_data = 30
    sizes = [(1, 24, 24), (24, num_data)]

    l1 = layer()
    l1.get_input_shape_at.return_value = sizes[0]
    l2 = layer()
    l2.get_output_shape_at.return_value = sizes[1]
    model.layers = [l1, l2]

    batch = 32
    epoch = 2
    kdpm = DeepLearningModel(model, criterion, optimizer, batch, epoch, metrics)

    params = np.random.rand(30)
    kdpm._model.get_weights.return_value = params
    parm = kdpm.get_model_params()

    assert np.array_equal(params, parm)
def test_evaluate():
    model = Mock()
    layer = Mock
    criterion = Mock()
    optimizer = Mock()
    metrics = Mock()

    sizes = [(1, 24, 24), (24, 10)]

    l1 = layer()
    l1.get_input_shape_at.return_value = sizes[0]
    l2 = layer()
    l2.get_output_shape_at.return_value = sizes[1]
    model.layers = [l1, l2]

    batch = 32
    epoch = 2
    kdpm = DeepLearningModel(model, criterion, optimizer, batch, epoch, metrics)

    num_data = 30
    data = np.array([np.random.rand(24, 24) for i in range(num_data)])
    labels = np.array([np.zeros(10) for i in range(num_data)])
    for l in labels:
        l[np.random.randint(0, len(l))] = 1

    kdpm.evaluate(data, labels)

    kdpm._model.evaluate.assert_called_once()
def test_predict():
    model = Mock()
    layer = Mock
    criterion = Mock()
    optimizer = Mock()
    metrics = Mock()

    sizes = [(1, 24, 24), (24, 10)]

    l1 = layer()
    l1.get_input_shape_at.return_value = sizes[0]
    l2 = layer()
    l2.get_output_shape_at.return_value = sizes[1]
    model.layers = [l1, l2]

    batch = 32
    epoch = 2
    kdpm = DeepLearningModel(model, criterion, optimizer, batch, epoch, metrics)

    num_data = 30
    data = np.array([np.random.rand(24, 24) for i in range(num_data)])

    kdpm.predict(data)

    kdpm._model.predict.assert_called_once_with(data, batch_size=batch)
예제 #5
0
def test_performance():
    model = Mock()
    layer = Mock

    num_data = 30
    sizes = [(1, 24, 24), (24, num_data)]

    l1 = layer()
    l1.get_input_shape_at.return_value = sizes[0]
    l2 = layer()
    l2.get_output_shape_at.return_value = sizes[1]
    model.layers = [l1, l2]

    model.evaluate.return_value = [0, 0]

    batch = 32
    epoch = 2
    kdpm = DeepLearningModel(model, batch, epoch)

    kdpm._check_data = Mock()
    kdpm._check_labels = Mock()

    data = np.random.rand(25).reshape((5, 5))
    labels = np.random.randint(0, 2, 5)

    res = kdpm.performance(data, labels)

    kdpm._check_data.assert_called_once_with(data)
    kdpm._check_labels.assert_called_once_with(labels)

    kdpm._model.evaluate.assert_called_once_with(data, labels, verbose=0)

    assert res == 0
예제 #6
0
def test_set_weights():
    model = Mock()
    layer = Mock

    num_data = 30
    sizes = [(1, 24, 24), (24, num_data)]

    l1 = layer()
    l1.get_input_shape_at.return_value = sizes[0]
    l2 = layer()
    l2.get_output_shape_at.return_value = sizes[1]
    model.layers = [l1, l2]

    batch = 32
    epoch = 2
    kdpm = DeepLearningModel(model, batch, epoch)

    params = np.random.rand(30)
    kdpm.set_model_params(params)

    kdpm._model.set_weights.assert_called_once_with(params)
예제 #7
0
def test_wrong_predict():
    model = Mock()
    layer = Mock

    sizes = [(1, 24, 24), (24, 10)]

    l1 = layer()
    l1.get_input_shape_at.return_value = sizes[0]
    l2 = layer()
    l2.get_output_shape_at.return_value = sizes[1]
    model.layers = [l1, l2]

    batch = 32
    epoch = 2
    kdpm = DeepLearningModel(model, batch, epoch)

    num_data = 30
    data = np.array([np.random.rand(16, 16) for i in range(num_data)])

    with pytest.raises(AssertionError):
        kdpm.predict(data)
def test_train_wrong_data():
    model = Mock()
    layer = Mock
    criterion = Mock()
    optimizer = Mock()
    metrics = Mock()

    sizes = [(30, 24, 24), (24, 10)]

    l1 = layer()
    l1.get_input_shape_at.return_value = sizes[0]
    l2 = layer()
    l2.get_output_shape_at.return_value = sizes[1]
    model.layers = [l1, l2]

    batch = 32
    epoch = 2
    kdpl = DeepLearningModel(model, criterion, optimizer, batch, epoch, metrics)

    num_data = 30
    data = np.array([np.random.rand(16, 16) for i in range(num_data)])
    label = np.array([np.zeros(10) for i in range(num_data)])
    for l in label:
        l[np.random.randint(0, len(l))] = 1

    with pytest.raises(AssertionError):
        kdpl.train(data, label)

    data = np.array([np.random.rand(24, 24) for i in range(num_data)])
    label = np.array([np.zeros(8) for i in range(num_data)])
    for l in label:
        l[np.random.randint(0, len(l))] = 1
    with pytest.raises(AssertionError):
        kdpl.train(data, label)
    def model_builder():
        """
        Create a Tensorflow Model for image classification.

        # Returns:
            model: Instance of DeepLearningModel [DeepLearningModel](../model/#deeplearningmodel-class)).
        """
        model = tf.keras.models.Sequential()
        model.add(
            tf.keras.layers.Conv2D(32,
                                   kernel_size=(3, 3),
                                   padding='same',
                                   activation='relu',
                                   strides=1,
                                   input_shape=(28, 28, 1)))
        model.add(
            tf.keras.layers.MaxPooling2D(pool_size=2,
                                         strides=2,
                                         padding='valid'))
        model.add(tf.keras.layers.Dropout(0.4))
        model.add(
            tf.keras.layers.Conv2D(32,
                                   kernel_size=(3, 3),
                                   padding='same',
                                   activation='relu',
                                   strides=1))
        model.add(
            tf.keras.layers.MaxPooling2D(pool_size=2,
                                         strides=2,
                                         padding='valid'))
        model.add(tf.keras.layers.Dropout(0.3))
        model.add(tf.keras.layers.Flatten())
        model.add(tf.keras.layers.Dense(128, activation='relu'))
        model.add(tf.keras.layers.Dropout(0.1))
        model.add(tf.keras.layers.Dense(64, activation='relu'))
        model.add(tf.keras.layers.Dense(10, activation='softmax'))

        criterion = tf.keras.losses.CategoricalCrossentropy()
        optimizer = tf.keras.optimizers.RMSprop()
        metrics = [tf.keras.metrics.CategoricalAccuracy()]

        return DeepLearningModel(model=model,
                                 criterion=criterion,
                                 optimizer=optimizer,
                                 metrics=metrics)
    def model_builder():
        """
        Create a Tensorflow Model for image classification.

        # Returns:
            model: Instance of DeepLearningModel [DeepLearningModel](../model/#deeplearningmodel-class)).
        """
        model = tf.keras.models.Sequential()
        model.add(
            tf.keras.layers.Conv2D(32,
                                   kernel_size=(3, 3),
                                   padding='same',
                                   activation='relu',
                                   strides=1,
                                   input_shape=(28, 28, 1)))
        model.add(
            tf.keras.layers.MaxPooling2D(pool_size=2,
                                         strides=2,
                                         padding='valid'))
        model.add(tf.keras.layers.Dropout(0.4))
        model.add(
            tf.keras.layers.Conv2D(32,
                                   kernel_size=(3, 3),
                                   padding='same',
                                   activation='relu',
                                   strides=1))
        model.add(
            tf.keras.layers.MaxPooling2D(pool_size=2,
                                         strides=2,
                                         padding='valid'))
        model.add(tf.keras.layers.Dropout(0.3))
        model.add(tf.keras.layers.Flatten())
        model.add(tf.keras.layers.Dense(128, activation='relu'))
        model.add(tf.keras.layers.Dropout(0.1))
        model.add(tf.keras.layers.Dense(64, activation='relu'))
        model.add(tf.keras.layers.Dense(10, activation='softmax'))

        model.compile(optimizer="rmsprop",
                      loss="categorical_crossentropy",
                      metrics=["accuracy"])

        return DeepLearningModel(model)