I am using a pre-trained neural network which I downloaded from an ONNX repository. I would like to study the output of a given internal layer in the network.
Here is a simple dummy example of what I would like to achieve. Lets create a simple network and save it in ONNX format
net = NetInitialize@NetGraph[{LinearLayer[2], Ramp, LinearLayer[3], Tanh,CatenateLayer[]}, {NetPort["Input1"] -> 1 -> 2, NetPort["Input2"] -> 3 -> 4, {2, 4} -> 5}, "Input1" -> 6, "Input2" -> 7];
Export["onnx_model.onnx", net];
Now I load the network just as if I downloaded it from some repository
ONNXnet = Import["onnx_model.onnx"]
The imported network from the ONNX file looks like this
I would now like to add an additional output to layer 2 to which I can connect a new network output port :
The only way I have found to do this is by taking the imported network apart and then rebuild it with the extra link to the output port
layerlist=Normal[ONNXnet]
NetGraph[layerlist,{NetPort["Input1"]->1->3,NetPort["Input2"]->2->4,{3,4}->5,3->NetPort["Extra_port"]}]
In case of a simple network like the above example or linear networks, this procedure is not too difficult. But when the original imported network has many layers and complicated links, rebuilding it by hand becomes tedious and is error prone.
I have seen examples of new netgraphs being build on top of existing netgraphs but I don't know how to access a given layer of the existing netgraph being used. I only know how to connect something to the input and output ports of the existing NetGraph, not the internal layers. Something like the following does not work :
NetGraph[<|"net"->ONNXnet|>,{NetPort["net",2]->NetPort["Extra_Port"]}]
Using NetReplacePart to replace layer 2 with an identical layer that has 2 outputs also does not work (this was a suggestion from mathematica.stackexchange)
rep=Netgraph[{ramp},{1->NetPort["Output"],1->NetPort["Extra_port"]}];NetReplacePart[ONNXnet,3->rep]
NetReplacePart::invnetrep: Replacement of net failed: replacement net had extra output port named "foo".
So my question is if it is at all possible to connect a new output port to an existing NetGraph and how to do this.