def test_plot_graphviz_temp(self): dot = dedent(""" digraph D { A [shape=diamond] B [shape=box] C [shape=circle] A -> B [style=dashed, color=grey] A -> C [color="black:invis:black"] A -> D [penwidth=5, arrowhead=none] } """) import matplotlib.pyplot as plt temp = get_temp_folder(__file__, "temp_plot_graphviz_temp") img = os.path.join(temp, "dot.png") dotf = os.path.join(temp, "dot.dot") fig, ax = plt.subplots(1, 1) try: plot_graphviz(dot, ax=ax, temp_dot=dotf, temp_img=img) except FileNotFoundError as e: if "No such file or directory: 'dot'" in str(e): warnings.warn("graphviz not installed: %r." % e) return plt.close('all') self.assertExists(dotf) self.assertExists(img)
def plot_onnx(onx, ax=None, dpi=300, temp_dot=None, temp_img=None, show=False): """ Plots an ONNX graph into a :epkg:`matplotlib` graph. :param onx: ONNX object, @see cl OnnxInference :param ax: existing axes :param dpi: resolution :param temp_dot: temporary file, if None, a file is created and removed :param temp_img: temporary image, if None, a file is created and removed :param show: calls `plt.show()` :return: axes """ # delayed import from pyquickhelper.helpgen.graphviz_helper import plot_graphviz if ax is None: ax = plt.gca() # pragma: no cover elif isinstance(ax, str) and ax == 'new': _, ax = plt.subplots(1, 1) # pragma: no cover if not isinstance(onx, OnnxInference): oinf = OnnxInference(onx, skip_run=True) else: oinf = onx # pragma: no cover dot = oinf.to_dot() plot_graphviz(dot, dpi=dpi, ax=ax, temp_dot=temp_dot, temp_img=temp_img) if show: plt.show() # pragma: no cover return ax
def test_plot_graphviz(self): dot = dedent(""" digraph D { A [shape=diamond] B [shape=box] C [shape=circle] A -> B [style=dashed, color=grey] A -> C [color="black:invis:black"] A -> D [penwidth=5, arrowhead=none] } """) import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) try: plot_graphviz(dot, ax=ax) except FileNotFoundError as e: if "No such file or directory: 'dot'" in str(e): warnings.warn("graphviz not installed: %r." % e) return plt.close('all')
########################## # Predictions with onnxruntime. sess = rt.InferenceSession("pipeline_xgboost.onnx", providers=['CPUExecutionProvider']) pred_onx = sess.run(None, {"input": X[:5].astype(numpy.float32)}) print("predict", pred_onx[0]) print("predict_proba", pred_onx[1][:1]) ############################# # Final graph # +++++++++++ oinf = OnnxInference(model_onnx) ax = plot_graphviz(oinf.to_dot()) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ####################################### # Same example with XGBRegressor # ++++++++++++++++++++++++++++++ update_registered_converter(XGBRegressor, 'XGBoostXGBRegressor', calculate_linear_regressor_output_shapes, convert_xgboost) data = load_diabetes() x = data.data y = data.target X_train, X_test, y_train, _ = train_test_split(x, y, test_size=0.5)
# We can save the model into ONNX # format and compute the same predictions in many # platform using :epkg:`onnxruntime`. #################################### # Python runtime # ++++++++++++++ # # A python runtime can be used as well to compute # the prediction. It is not meant to be used into # production (it still relies on python), but it is # useful to investigate why the conversion went wrong. # It uses module :epkg:`mlprodict`. oinf = OnnxInference(onx, runtime="python_compiled") print(oinf) ########################################## # It works almost the same way. pred_pyrt = oinf.run({'X': X_test.astype(numpy.float32)})['variable'] print(diff(pred_skl, pred_pyrt)) ############################# # Final graph # +++++++++++ ax = plot_graphviz(oinf.to_dot(), dpi=100) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False)