Skip to content

Correctly expand dims of pointwise layer #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2023

Conversation

vloncar
Copy link
Contributor

@vloncar vloncar commented Feb 15, 2023

Description

When creating PointwiseConv layers we always expand dimensions of weights to (1, 1, x, y), but in 1d we should only expand to (1, x, y). This breaks some model Thea is using.

Type of change

  • Bug fix (non-breaking change that fixes an issue)

Tests

From Thea:

import numpy as np
import tensorflow as tf
import hls4ml

np.random.seed(12)
tf.random.set_seed(12)

from tensorflow import keras
import tensorflow.keras.layers as KL

def get_deepsets_net(input_shape=(16, 16)):
    deepsets_input = keras.Input(shape=input_shape, name="input_layer")

    # Permutation Equivariant Layer
    x_max = KL.GlobalMaxPooling1D(keepdims=True)(deepsets_input)
    x_lambd = KL.Dense(32, use_bias=False)(x_max)
    x_gamma = KL.Dense(32)(deepsets_input)
    x = KL.Subtract()([x_gamma, x_lambd])
    x = KL.Activation('elu')(x)

    x_max = KL.GlobalMaxPooling1D()(x)
    x = KL.Dense(16)(x)
    x = KL.Activation('elu')(x)
    deepsets_output = KL.Dense(5)(x)
    deepsets_network = keras.Model(deepsets_input, deepsets_output, name="deepsets")
    return deepsets_network

input = np.random.rand(1,16,16)
input_shape = input.shape[1:]
model = get_deepsets_net(input_shape=input_shape)
model.summary()

config = hls4ml.utils.config_from_keras_model(model, granularity='model')
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config)

Note that there is a misparse due to a keepdims argument not being supported in GlobalPooling layer. I'll push a PR for that as well.

Checklist

I can't run pre-commit due to a strange bug so this will probably fail the test, but I'll fix that afterwards.

@jmitrevs jmitrevs added this to the v0.7.0 milestone Feb 16, 2023
@jmduarte jmduarte added bug please test Trigger testing by creating local PR branch labels Mar 4, 2023
@jmduarte jmduarte self-requested a review March 4, 2023 15:59
Copy link
Member

@jmduarte jmduarte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I'm wondering if we should merge this first, and then add a deep-sets-like model test to #716

@thaarres
Copy link
Contributor

thaarres commented Mar 4, 2023

@jmduarte @vloncar Feel free to use the model above as test if it's useful, but it might have other issues down the line :)

@vloncar
Copy link
Contributor Author

vloncar commented Mar 5, 2023

I would add the deep sets-like model in the example-models repository, this is something we don't have and it is a useful demonstration. Do we have a reasonable model at hand?

@jmduarte jmduarte merged commit f0bcd4f into fastmachinelearning:main Mar 6, 2023
calad0i pushed a commit to calad0i/hls4ml that referenced this pull request Jul 1, 2023
* Correctly expand dims of pointwise layer

* Style rules applied
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug please test Trigger testing by creating local PR branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants