Exemple #1
0
def mean_weight_mask(model, name="dense0", th=0.05):
    layer = model.get_layer(name=name)
    layer_idx = get_layer_index(model, name=name)
    if layer_idx == 0:
        print("Not on the input layer")
        return
    layer_class = layer.__class__.__name__
    if not (layer_class == 'Dense'):
        print(" Please assign a dense or conv layer")
        return model
    weights = layer.get_weights()
    weight = weights[0]
    zeros_n = np.count_nonzero(weight)
    print("Before pruning: nonzeros in weights:", zeros_n)
    it  = np.nditer(weight, flags=['multi_index'])
    while not it.finished:
        if np.abs(it[0])<th:
            weight[it.multi_index] = 0.0
        it.iternext()
    zeros_n = np.count_nonzero(weight)
    print("After Pruning: nonzeros in weights:", zeros_n)
    weights[0] = weight
    layer.set_weights(weights)
    mask_layer = Masking(mask_value=0.0)
    model = insert_layer(model, model.layers[layer_idx-1],mask_layer)
    return model
def test_insert_layer():
    # Create all model layers
    input_1 = Input(shape=[7, 7, 1])
    dense_1 = Dense(3)
    dense_2 = Dense(3)
    dense_3 = Dense(3)
    dense_4 = Dense(1)
    # Create the model
    x = dense_1(input_1)
    x = dense_2(x)
    output_1 = dense_4(x)
    model_1 = utils.clean_copy(Model(input_1, output_1))
    # Create the expected modified model
    x = dense_1(input_1)
    x = dense_2(x)
    x = dense_3(x)
    output_2 = dense_4(x)
    model_2_exp = utils.clean_copy(Model(input_1, output_2))
    # Insert dense_3 before dense_4 in model_1
    model_2 = operations.insert_layer(model_1, model_1.get_layer(dense_4.name),
                                      dense_3)
    # Compare the modified model with the expected modified model
    assert compare_models(model_2, model_2_exp)