Beispiel #1
0
def apply_layers(model, settings, inputs, treeed_inputs):
    if 'networks' in model:
        all_layer_sequences = [
            layer_sequence(net) for net in model['networks']
        ]
        use_trees = [net.get('trees', False) for net in model['networks']]
    elif model['layers']:
        all_layer_sequences = [layer_sequence(model)]
        use_trees = [treeed_inputs is not None]
    else:
        all_layer_sequences = [[]]
        use_trees = [False]

    all_predictions = []
    outex = get_output_exposition(model)

    for lseq, tree_in in zip(all_layer_sequences, use_trees):
        if tree_in:
            preds = propagate(lseq, treeed_inputs)
        else:
            preds = propagate(lseq, inputs)

        if outex['type'] == NUMERIC and not settings.regression_normalize:
            preds = preds * outex['stdev'] + outex['mean']

        all_predictions.append(preds)

    if len(all_predictions) > 1:
        summed = tf.add_n(all_predictions)
        predictions = summed / len(all_predictions)
    else:
        predictions = all_predictions[0]

    return predictions
def extract_regression_parameters(reg_file):
    test_artifact = read_zipped_json(reg_file)
    all_options = []

    for i, test in enumerate(test_artifact):
        layers = extract_layers(test['model']['layers'])
        options = test['model']['training_options']

        drate = options.get('dropout_rate', 0.0)
        bn = options.get('batch_normalization', False)

        if drate and not bn:
            layers = add_dropouts(layers, drate)

        for key in [
                'randomize', 'activation_functions', 'layer_sizes',
                'activation_function', 'learn_residuals',
                'batch_normalizaiton', 'dropout_rate'
        ]:
            options.pop(key, None)

        make_model(layer_sequence({'layers': layers}), (6, ))

        options['layers'] = layers
        options['seed'] = i

        all_options.append(options)

    return all_options
Beispiel #3
0
def test_create_simple():
    network = {
        'layers': [
            {
                'type': 'dense',
                'number_of_nodes': 8,
                'seed': 42,
                'activation_function': 'leaky_relu',
                'weights': 'glorot_uniform',
                'offset': 'zeros'
            },
            {
                'type': 'dense',
                'number_of_nodes': 16,
                'seed': 43,
                'activation_function': 'softmax',
                'weights': 'glorot_normal',
                'offset': 'ones'
            }
        ]
    }

    lseq = layer_sequence(network)
    model = make_model(lseq, 4)

    assert len(lseq) == 2

    for sizes, layer in zip([(4, 8), (8, 16)], lseq):
        fan_in, fan_out = sizes
        weights = layer.get_weights()

        assert weights[0].shape == sizes
        assert weights[1].shape == (fan_out,), weights[1].shape

        assert len(set(weights[0].flatten())) == fan_in * fan_out
        assert np.all(weights[0] > -2.0)
        assert np.all(weights[0] < 2.0)

        if fan_out == 16:
            assert np.all(weights[1] == 1.0), weights[1]
        else:
            assert np.all(weights[1] == 0.0), weights[1]
Beispiel #4
0
def test_dropblock():
    image_shape = (None, 64, 64, 3)
    network = {
        'layers': [{'type': 'dropout',
                    'dropout_type': 'block',
                    'block_size': 7,
                    'rate': 0.1}]
    }

    settings = Settings({'image_path_prefix': 'tests/data/images'})
    reader = make_image_reader(settings, image_shape, False)
    lseq = layer_sequence(network)
    model = make_model(lseq, image_shape[1:])
    pizzas = ['pizza_people.jpg'] * 16

    # show_outputs(pizzas, reader, model)

    for image in pizzas:
        img = np.expand_dims(reader(image), axis=0)
        model(img, training=True)
Beispiel #5
0
    def __init__(self, image_network, settings):
        super(ImagePreprocessor, self).__init__()

        self._reader = ImageReader(image_network, settings)
        self._loader = ImageLoader(image_network)
        self._image_layers = layer_sequence(image_network)
Beispiel #6
0
def test_create_residual():
    network = {
        'layers': [
            {
                'type': 'dense',
                'number_of_nodes': 6,
                'seed': 42,
                'activation_function': 'leaky_relu',
                'weights': 'glorot_uniform',
                'offset': 'zeros'
            },
            {
                'type': 'dense_residual_block',
                'activation_function': 'softplus',
                'identity_path': [],
                'dense_path': [
                    {
                        'type': 'dense',
                        'number_of_nodes': 6,
                        'seed': 42,
                        'activation_function': None,
                        'weights': 'glorot_uniform',
                        'offset': 'zeros'
                    },
                    {
                        'type': 'batch_normalization',
                        'beta': 'ones',
                        'gamma': 'zeros',
                        'mean': 'ones',
                        'variance': 'zeros'
                    },
                    {
                        'type': 'activation',
                        'activation_function': 'relu',
                    },
                    {
                        'type': 'dense',
                        'number_of_nodes': 6,
                        'seed': 42,
                        'activation_function': None,
                        'weights': 'glorot_uniform',
                        'offset': 'zeros'
                    },
                    {
                        'type': 'batch_normalization',
                        'beta': 'ones',
                        'gamma': 'ones',
                        'mean': 'zeros',
                        'variance': 'zeros'
                    },
                    {
                        'type': 'activation',
                        'activation_function': 'softmax',
                    }
                ]
            },
            {
                'type': 'dense',
                'number_of_nodes': 4,
                'seed': 42,
                'activation_function': 'softmax',
                'weights': 'glorot_uniform',
                'offset': 'zeros'
            }
        ]
    }

    lseq = layer_sequence(network)
    model = make_model(lseq, 4)

    assert len(lseq) == 3

    assert [kl.Dense, BlockLayer, kl.Dense] == [type(layer) for layer in lseq]

    dense_path_types =[kl.Dense, kl.BatchNormalization, kl.Activation] * 2
    block_paths = lseq[1]._paths[0]

    assert dense_path_types == [type(layer) for layer in block_paths]

    for layer in [block_paths[0], block_paths[3]]:
        assert type(layer) == kl.Dense

        weights, offset = layer.get_weights()
        assert weights.shape == (6, 6)
        assert offset.shape == (6,)