Skip to content

Make wrapper classes for making ML models compatible with tool #30

@KristofferSkare

Description

@KristofferSkare

Not every ML model / onnx file is compatible with the mlfmu tool and i can be a bit hard/confusing to make an incompatible model compatible.

To make this process easier for our users we can make example classes in tensorflow and pytorch that they can use and extend to make a compatibility wrapper for their trained model.

The class could have implemented some functions and give the user an idea of what they should do to make the wrapper.

class TensorflowWrapper(tensorflow.keras.Model):
   def __init__(self, trained_model):
        self.trained_model = trained_model
    def format_inputs(self, inputs, states, time):
        return 
    def format_outputs(self, inputs, states, time, model_outputs):
         return
    def call(self, x):
           inputs, states, time = x
           formatted_inputs = self.format_inputs(inputs, state, time)
           model_outputs = self.trained_model(formatted_inputs)
           return self.format_outputs(inputs, states, time, model_outputs)
class PytorchWrapper(torch.nn.Modulel):
   def __init__(self, trained_model):
        self.trained_model = trained_model
    def format_inputs(self, inputs, states, time):
        return 
    def format_outputs(self, inputs, states, time, model_outputs):
         return
    def forward(self, x):
           inputs, states, time = x
           formatted_inputs = self.format_inputs(inputs, state, time)
           model_outputs = self.trained_model(formatted_inputs)
           return self.format_outputs(inputs, states, time, model_outputs)

In easy cases just implementing the format_inputs and format_outputs classes could be sufficient to make the compatibility wrapper for their model. For more complex cases one would maybe also need to modify more, but this could be a good starting point for our users.

If we include this we should also include tools for loading onnx files into pytorch/tensorflow and converting pytorch/tensorflow to onnx files. That way a user could go the entire way from an incompatible onnx file to an compatible onnx to a compiled fmu. All within the this tool.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions