Deep Learning with TensorFlow
1. Definition:
Deep learning is a type of artificial intelligence derived from machine learning where the machine is able to learn by itself, unlike programming with which it simply executes predetermined rules to the letter.
For this article, we will use the library developed by Google Brain " TensorFlow " (version 2.2.0) with the Keras model (which is now integrated into TensorFlow).
This article is not intended to teach you about TensorFlow (you will find many tutorials on this subject, including those from Google ). Instead, we will discover how we can use this library within Dynamo with Python and a very simple example.
2. The Data Set
For this example, the data set to train our neural network consists of 2 lists:
- a list of points (where only the X and Y coordinates are retrieved)
- a list of booleans that is associated with the list of points.
if the value is True → the point of the same index of the point list is in the circle
if the value is False → the point of the same index of the point list is outside the circle
Finally, we generate a last list which is made up of geometric spheres. It will be used to make predictions. The goal is to predict (from the trained neural network) for each sphere if it is located in the circle.
Note:
Obviously we could determine whether the spheres are in the circle from a simple mathematical operation, but that is not the goal here.
3. The Code
This article is not about performing a model, but about exploring the capabilities of TensorFlow in Dynamo
In the example below, we create a neural network with Keras (which makes operations much easier) that has the following characteristics:
- an input layer consisting of 2 nodes, each node will receive respectively the coordinates and an integer (0 or 1, depending on whether the point is in the circle or not)
- 2 hidden layers composed of 5 nodes
- an output layer with 2 nodes as well
Once the model is created, we train it with the fit() method , and finally we make predictions with predict()
The choice and characteristics of the activation functions, error function and optimizer are not discussed here.
import sys
import clr
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference('GeometryColor')
from Modifiers import GeometryColor
clr.AddReference('DSCoreNodes')
from DSCore import Color
clr.AddReference('System.Windows.Forms')
import System.Windows.Forms
from System.Windows.Forms import MessageBox, MessageBoxButtons
dirAppLoc = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)
sys.path.append(dirAppLoc + r'\python-3.8.3-embed-amd64\Lib\site-packages')
datacsv = dirAppLoc + "\\dataTensorFlow.csv"
# force to reload module
if "tensorflow" in sys.modules:
del sys.modules["tensorflow"]
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.python.client import device_lib
from io import StringIO
sys.stdout = StringIO()
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
print(device_lib.list_local_devices())
LEARNING_RATE = 0.01
EPOCH_COUNT = 2000
max_accuracy = 80
# get X and Y from randoms points
x = np.array([[pt.X, pt.Y] for pt in IN[0]], dtype=np.float32)
# convert bool to integer
y = np.array([int(x) for x in IN[1]], dtype=np.float32)
# get X and Y from Test points for prediction
lstSpheres = IN[2]
testPoints = np.array([[sphere.CenterPoint.X, sphere.CenterPoint.Y] for sphere in lstSpheres], dtype=np.float32)
outGeometry = []
### Different activation ###
# softmax , tanh , relu , sigmoid
#
### Different loss ###
# binary_crossentropy , mean_squared_error , squared_hinge , categorical_crossentropy , sparse_categorical_crossentropy
#
model = keras.Sequential()
model.add(keras.layers.Dense(units=5, input_shape=x.shape, activation='relu'))
model.add(keras.layers.Dense(units=5, activation='relu'))
model.add(keras.layers.Dense(units=2, activation='sigmoid'))
#optimizer = tf.keras.optimizers.SGD(learning_rate = LEARNING_RATE)
optimizer = tf.keras.optimizers.Adam(learning_rate = LEARNING_RATE)
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# train the model
i = 0
final_accuracy = 0
while i < 3 and final_accuracy <= max_accuracy * 0.01:
model.fit(x, y, epochs=EPOCH_COUNT)
# get the final accuracy
final_accuracy = round(model.history.history['accuracy'][-1], 2)
i += 1
model.summary()
print(f"final_accuracy : {final_accuracy}")
# test Prediction
result1 = model.predict(testPoints)
# color the result
for idx, (sph, (out1, out2)) in enumerate(zip(lstSpheres, result1)):
factor = 1 if out2 > 0.001 else 0
print("probability shpere at index {} is inside circle : {}%".format(idx, round((1 - out1) * factor, 2) * 100))
if out1 < 0.1 and out2 > 0.001:
geocolor = GeometryColor.ByGeometryColor(sph, Color.ByARGB(255,0,255,0))
outGeometry.append(geocolor)
else:
geocolor = GeometryColor.ByGeometryColor(sph, Color.ByARGB(255,255,0,0))
outGeometry.append(geocolor)
sys.stdout.seek(0)
readerstdout = sys.stdout.read()
OUT = readerstdout, result1, outGeometry
NOTE :
- This is a very simple model, in reality it will be necessary to implement:
- A bigger DataSet (more points)
- A validation dataset
- A possibly modified structure of the model with more layers
- An EarlyStopping callback
- The original version of the article and other works of the author can be found here: https://voltadynabim.blogspot.com/2021/07/dynamopython-deep-learning-avec.html
4. Video Demonstration
I am passionate about electrical engineering and IT and my job now allows me to combine these two worlds. As an eternal beginner and self-taught person I started with assembler programming, some Android development, some Lisp and now I stopped at Python programming and its BIM variants]. After years of learning from others, it was my turn to share some knowledge (Sharing increases knowledge). All this means that today I am recognised by Autodesk as an Expert Elite, sharing my knowledge through my blog ‘https://voltadynabim.blogspot.com/’ and taking part in the DynamoBim forum (Autodesk) as a moderator. |
Leave Your Comment