Integrate a machine learning model into Dynamo to be used for Generative Design purposes

Hello,
I am trying to integrate a machine learning model within Dynamo for analysis purposes. The idea would be to use this ML model and 10 numeric variables to predict 2 outputs which are also numeric values. I thought that code blocks in Dynamo would help me do the work, but they didn’t. I am trying to figure out how “python script” nodes in Dynamo can help me execute the attached codes (part 1 and part 2). I’ve screenshotted the output window for “code part 2” to show that the inputs are represented by one array that consists of 10 numeric values, and that the outputs are represented by one array of 2 values.
I also tried attaching “model_autokeras.zip” folder which is the ML model saved as h5 in this zipped folder, but the format isn’t supported here. I’ll try attaching it in a comment.
The purpose of this integration will be to use the ML model as the analysis part of the GD, and later the goals of the GD will be to minimize both outputs of the ML model (while varying the input values).

Thank you in advance!


You’ll need to load some external libraries by the looks of it. Don’t think pip will work.

This thread has some discussion on external library referencing:

Dynamo 2022 offers CPython but older builds are using IronPython. I believe numpy is in 2022 but not earlier builds. keras and tf aren’t in any by default as far as I’m aware.

Hello @ritaselias - have you tried this wiki page entry? Customizing Dynamo's Python 3 installation · DynamoDS/Dynamo Wiki · GitHub

We have plans to include some of the most common and popular Python 3 packages out-of-the-box, but haven’t had scope to do so yet.

2 Likes

Hello @ritaselias
I wrote an article recently, it might be able to help you (use Translate button)

it’s a good new :heart:
in addition to the most well-known packages, the implementation of some packages could be very useful
This would fill some feature gaps in PythonNet, although I am waiting for to test PythonNet 3 which brings new features and fixes.

1 Like

@GavinCrump , @solamour , and @c.poupin thank you all for your help! I’m sure I’ll get back to you with more questions once I start solving my problem! Thanks again!

3 Likes

I followed all your suggestions but I’m getting the attached error. I tried searching for a solution online but no luck!
Below is my code:


# Load the Python Standard and DesignScript Libraries
import sys
import os
import clr
import System



dirAppLoc = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) 
sys.path.append(dirAppLoc + r'\python-3.8.3-embed-amd64\Lib\site-packages')


# force to reload module
if "tensorflow" in sys.modules:
    del sys.modules["tensorflow"]
    

import autokeras as ak
import numpy as np
import tensorflow as tf
from tensorflow import keras

#Load Best Model
from tensorflow.keras.models import load_model

loaded_model = load_model("C:/Users/rita2/Downloads/model_autokeras", custom_objects=ak.CUSTOM_OBJECTS)

# The inputs to this node will be stored as a list in the IN variables.
X_test = np.array([IN[0], IN[1], IN[2], IN[3], IN[4], IN[5], IN[6], IN[7], IN[8], IN[9]])


OUT =[]
#Predictions
predicted_y = loaded_model.predict([X_test])

# Assign your output to the OUT variable.
OUT = predicted_y

Any idea? Thanks again for your help!Picture1

Not sure, but the error seems to come from your Keras model ML (maybe an incompatibility with tensorflow version )

simple test with an Export (from Sublime Text, Python 3.8) and Import (from Dynamo CPython 3.8) of a Keras Model

testExportImportModeltf

3 Likes

Thank you very much for your help! Your solutions are very thorough and helpful, I appreciate it! I’m still struggling with another error. I will try solving it before I give up and post it here!

I made sure that I have same tensorflow version 2.4.0 for both my code and Dynamo. But, I’m getting a new error:

Warning: RuntimeError : Unable to restore a layer of class Custom>MultiCategoryEncoding. Layers of class Custom>MultiCategoryEncoding require that the class be provided to the model loading code, either by registering the class using @keras.utils.register_keras_serializable on the class def and including that file in your program, or by passing the class in a keras.utils.CustomObjectScope that wraps this load call. [’ File “”, line 26, in \n’, ’ File “C:\Users\rita2\AppData\Local\python-3.8.3-embed-amd64\Lib\site-packages\tensorflow\python\keras\saving\save.py”, line 212, in load_model\n return saved_model_load.load(filepath, compile, options)\n’, ’ File “C:\Users\rita2\AppData\Local\python-3.8.3-embed-amd64\Lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py”, line 138, in load\n keras_loader.load_layers()\n’, ’ File “C:\Users\rita2\AppData\Local\python-3.8.3-embed-amd64\Lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py”, line 374, in load_layers\n self.loaded_nodes[node_metadata.node_id] = self._load_layer(\n’, ’ File "C:\Users\rita2\AppData\Local\python-3.8.3-embed-amd64\Lib\site-packages\tensorflow\python\keras\

new error

Thanks again for all your help!

look at this maybe it can help you

2 Likes

It didn’t but I really appreciate your help!