Aidge Model Explorer#
Aidge propose an integration with the plugin Google Model Explorer to visualize Aidge graphs.
This tutorial is a walkthrough on how to use this visualization tool.
Setup#
[ ]:
import aidge_core
import aidge_model_explorer
import model_explorer
aidge_core.Log.set_console_level(aidge_core.Level.Warn)
For this tutorial, we will use a LSTM operator created using the LSTM MetaOperator available in aidge_core.
[ ]:
model = aidge_core.get_connected_graph_view(aidge_core.LSTM(in_channels=4, hidden_channels=8, seq_length=5))
Wrapper method#
Aidge proposes a wrapper method that simplify the use of the visualization feature.
The following command can be used in your python script and will open in your browser
[ ]:
aidge_model_explorer.visualize(model, "LSTM")
If you want to visualize the graph inside of your notebook you can use the embed option, this option only work with Python notebook.
[ ]:
aidge_model_explorer.visualize(model, "LSTM", embed=True)
Visualize from config#
You can also use the model_explorer API by using a aidge_model_explorer.Config() which is an overridden class enabling full compatibility with the model_explorer API.
⚠️ The model explorer API does not run the app in a separated thread and the call to visualize_from_config is blocking. You need to stop manually the cell from running to execute the next cells.
[ ]:
# Config is an override of model_explorer.config() to be compatible with Aidge GraphView
config = aidge_model_explorer.config()
config.add_graphview(model, "lstm")
# Directly call model_explorer API
model_explorer.visualize_from_config(config)
Advanced uses#
Now that we have seen the basic usage of the visualize function we are going to go more in depth on how you can use this module to make graph exploration more intuitive and insightful.
If you see a ⭐ this means the feature is unique to the module aidge_model_explorer.
Synchronize graphs with node ids#
Model explorer support an option to synchronize two graphs using node id.
You can use this functionality to visualize a graph before and after graph modifications:
Note: Screen recording taken using v0.1.5 of model explorer.
You can experiment with it with the cell bellow (beware that again we use model_explorer function which is blocking so you will have to end the cell manually before proceeding):
[ ]:
config = aidge_model_explorer.config()
# Note: need to add graphview before doing any manipulation to keep the ids
config.add_graphview(model, "lstm")
expanded_model = model.clone()
aidge_core.expand_metaops(expanded_model)
config.add_graphview(expanded_model, "lstm_expanded")
model_explorer.visualize_from_config(config)
Add custom metadata#
In order to gain custom insight on your model you can add metadata that will be displayed in the visualization.
We do this by introducing a ConverterConfig object that will be used when parsing the graph.
[ ]:
converter_config = aidge_model_explorer.ConverterConfig()
This configuration can be used to add metadata on each nodes:
[ ]:
converter_config.add_attribute(
"isMatMul",
lambda node: "True" if (node.type() == "Matmul") else None
) # If lambda return None no attribute is added!
A usage example could be to set an attribute to know if the node input and output isn’t above a threshold, to quickly troubleshoot if a DNN need to be compressed.
You can also add information at tensor level, for example add the information of the mean of the tensor:
[ ]:
converter_config.add_output_metadata(
"mean",
lambda _, tensor: str(tensor.mean())
)
Once the converter configuration is ready, you can provide it with your model to the model explorer config and visualize it (beware that again we use model_explorer function which is blocking so you will have to end the cell manually before proceeding):
[ ]:
config = aidge_model_explorer.config()
config.add_graphview(model, "custom_attr_lst", converter_config)
model_explorer.visualize_from_config(config)
Update node style directly in Aidge nodes#
If you want to apply a specific style, you can add the following attributes to your nodes:
model_explorer:bg_color: Changes the background color of the node. The value must be a string representing a valid color in hexadecimal format (e.g., “#ff0800” for red). The string should follow the standard 6-digit RGB hex code format, beginning with a #.
model_explorer:border_color: Changes the border color of the node. The value must be a string representing a valid color in hexadecimal format (e.g., “#ff0800” for red). The string should follow the standard 6-digit RGB hex code format, beginning with a #.
model_explorer:h_border_color: Changes the border color on hover of the node. The value must be a string representing a valid color in hexadecimal format (e.g., “#ff0800” for red). The string should follow the standard 6-digit RGB hex code format, beginning with a #.
[ ]:
node = aidge_core.ReLU(name="Colorized Node!")
node.attributes().set_attr(name="model_explorer:bg_color", value="#ff0800")
node.attributes().set_attr(name="model_explorer:border_color", value="#00ff4c")
node.attributes().set_attr(name="model_explorer:h_border_color", value="#1100ff")
g = aidge_core.sequential([node])
aidge_model_explorer.visualize(g, "Colored_ReLU", embed=True)
Default visualizations#
Aidge model explorer propose some default visualization in order to let the user get started quickly.
Has an implementation#
This function add an attribute and color in red every node for which we cannot find an implementation to use (for example because input/output datatype does not match). This attribute is named fail_best_match and contains the description of the current node specs and the specs for which Aidge has a registered implementation.
Lets take as an example a Mul with an input that is an Integer and the other one that is a Float.
[ ]:
mul_node = aidge_core.Mul(name="mul")
p0 = aidge_core.Producer(aidge_core.Tensor(int(0)), name="p0")
p1 = aidge_core.Producer(aidge_core.Tensor(float(1)), name="p1")
p0.add_child(mul_node, 0, 0)
p1.add_child(mul_node, 0, 1)
ill_graph = aidge_core.GraphView()
ill_graph.add({mul_node, p0, p1})
We can now check if we can find an implementation with the CPU backend.
[ ]:
import aidge_backend_cpu # <- Need to import this module to dynamically load the implementations
aidge_model_explorer.has_best_match(ill_graph, "cpu")
Has expected Aidge doesn’t have an implementation to multiply an integer and a float and hte Mul node is marked as failing in the visualization.