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.

3 Likes

@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!

Hi @c.poupin,

I just read your article " [Dynamo+=Python] Deep Learning avec TensorFlow" ([Dynamo+=Python] Deep Learning avec TensorFlow ~ VoltaDynaBim). and I couldn’t install the “termcolor” which you have explained in your article pretty good. However, I couldn’t download that zip file to extract.

the only file available to download is “termcolor-1.1.0.tar.gz” file. Is it possible to send me the file or point me to a website so I can download that file?

Thank you,
Cezmi

Hi @Cezmi
You can open or extract directly the tar.gz file with 7zip software

1 Like

Hi again @c.poupin,

All good now! I was able to install the packages I need. Thank you so much and very appreciated!!

@c.poupin Thanks again for all your help. I paused my work for a while, this is why I didn’t get back to you earlier. But all your comments were super helpful and thoroughly answered my question. I am facing a new related issue now. My Python script node is working within Dynamo completely fine and serves as the objective function within my graph. It basically finds/calculates the outputs of my generative design study by calling/loading an Artificial Neural Network from an external file. The graph in Dynamo runs with no issue. However, when I create my GD study, the outputs turn out to have no value (*). I saw a comment made by @jacob.small on a different post:

Could this be the reason my GD study does not find values for my outputs? If yes, how can I solve it?

Thanks everyone for your help! Much appreciated!

2 Likes
1 Like

My Dynamo graph had 10 outputs so I could export it and created a Generative Design (GD) study. The GD Study was working completely fine until I integrated/added the Python script node that finds two additional outputs using a previously trained artificial neural network. Any idea anyone? Thanks again

Hi @ritaselias
I face the same problem. GD tools in Dynamo cannot work if you use pretrained Ml model. I add old post has this problem.

Yes but I noticed from your problem description that your GD study didn’t run at all. Mine is running (and completes the run), but outputs no value for most of the outputs. I don’t know but I’m still hoping there is a solution for that as it is running.
Did you find a workaround for this problem?

1 Like