fastmachinelearning / hls4ml

Machine learning on FPGAs using HLS
https://fastmachinelearning.org/hls4ml
Apache License 2.0
1.19k stars 390 forks source link

Correctly expand dims of pointwise layer #715

Closed vloncar closed 1 year ago

vloncar commented 1 year ago

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

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.

thaarres commented 1 year ago

@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 commented 1 year ago

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?