How do I export machine learning model as FMU with Julia and integrate it into Simulink model ?

15 views (last 30 days)
I'd like to create a AI model and export it as FMU file with Julia and integrate it into Simulink.
Have you ever tried this way?
If you don't mind, it'd be nice if you could give me how exactly you did it.
I tried to write Julia code to create a simple machine learning model and export it as FMU file with this Julia package.
Then I'm going to use this FMU import block
and integrate FMU of Julia model into Simulink like this example.
Best,

Answers (1)

Aravind
Aravind on 16 Oct 2024
Hello @翼,
I understand that you wish to use an FMU created from a Julia code in Simulink and that you are using the “FMIExport.jl” package to do so. I am assuming that you have used the “FMIExport.jl” Julia package to successfully export an FMU for the Julia code you have written. The “FMIExport.jl” package you mentioned is capable of exporting Julia code to Model Exchange FMUs with an FMI version 2.0. These Model Exchange FMUs do not contain the local solver utilized during export, and they can be brought into Simulink using the "FMU" block. This block accommodates FMI versions 1.0, 2.0, and 3.0 for both Model Exchange and Co-Simulation FMUs. You can find the official documentation for the "FMU" block at https://www.mathworks.com/help/simulink/ref_extras/fmu.html.
When you export an FMU using the “FMIExport.jl” package, a ".fmu" file is created. To import this file into Simulink, follow these steps:
  1. Open the library browser and insert the "FMU" block by navigating to Simulink Extras/FMU Import/FMU.
  2. Once inserted, double-click the block to access the block parameters
  3. Select the ".fmu" file that was exported using the “FMIExport.jl” package in the "FMU Name" parameter.
This process will insert the FMU, which can then be simulated. If the FMU requires any inputs or outputs, the necessary ports will be automatically generated. You can connect the FMU block as you would with any standard Simulink block and proceed with the simulation.
Hope this helps!
  2 Comments
翼
on 16 Oct 2024
Edited: on 16 Oct 2024
Much Appreciated.
I actually figured out the steps to import .fmu file into Simulink.
However, it seems like there is a glitch with FMIExport.jl. So, I asked the FMIExport.jl developer about this issue here on Github. I'm not able to even try the example code(BouncingBall) of this package....
What I want is to write Julia code to create a simple Machine Learning model, and then export it as FMU file with FMIExport.jl.
By the way, I'm trying to create a Machine Learning model that inputs 7 features and predicts 2 output parameters,but now I'm stuck in exporting the model to FMU file...
Do you have any ideas? How would you do it ?
I just want to know what exactly code I write in order to save Julia ML model as FMU.
I'd be happy if you could tell me about your simple code to convert a machine learing model by Julia to FMU file.
Best,
翼
on 16 Oct 2024
I've already created my model as BSON file as below. I just want to save this model as FMU file.
using Flux
using Statistics
using BSON: @save
# create random data here
input_size = 7
output_size = 2
num_samples = 1000
X = rand(Float32, input_size, num_samples)
Y = rand(Float32, output_size, num_samples)
# define model
model = Chain(
Dense(input_size, 32, relu),
Dense(32, 16, relu),
Dense(16, output_size)
)
# define Loss Functions and Optimizers
loss(x, y) = Flux.mse(model(x), y)
opt = ADAM(0.01)
# training
epochs = 100
for epoch in 1:epochs
Flux.train!(loss, Flux.params(model), [(X, Y)], opt)
if epoch % 10 == 0
@show epoch
@show loss(X, Y)
end
end
# save as BSON file
@save "vehicle_model.bson" model
All that is left is to save it as FMU file but it doesn't go well...
I'd like you to lend me your expertise. Any advice?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!