# Neural Networks: Modifiying NN structures?

From this nice answer one can import pretrained NN models and structures for state of art models.

URLDownload[
"http://data.dmlc.ml/mxnet/models/imagenet/inception-v3.tar.gz",
FileNameJoin[{\$UserDocumentsDirectory, "inception-v3.tar.gz"}]
];

ExtractArchive["inception-v3.tar.gz"];
Needs["NeuralNetworks"]

net = NeuralNetworksImportMXNetModel[
"model//Inception-7-symbol.json",
"model//Inception-7-0001.params"
]


The returned result is NetGraph object. In this object, imported models uses NetPort for inputs, as a result one must enter tensor with given dimensions. However, Mathematica has some nice encoders:

enc = NetEncoder[{"Image", {150, 150}}]


My question:

Is it possible to change ports inputting tensors to these Mathematica defined encoders? If so, how?

In addition, I would like to know if it's possible in the inception model to change the number of output classes. Currently, I want to use the structure but not pretrained model with 1008 classes. Lets say I want 10 new classes for output. How can I modify DotPlusLayer("fc1"), SoftMaxLayer("softmax") and Port "Output" for this 10 classes?

net = NeuralNetworksImportMXNetModel[
"model//Inception-7-symbol.json",
"model//Inception-7-0001.params"
]


This network accepts images in resolution 299x299.

SeedRandom[1];
img = RandomImage[1, {299, 299}, ColorSpace -> "RGB"];

net2 = NetGraph[
{
net,
SoftmaxLayer[]
},
{1 -> 2},
"Input" -> NetEncoder[{"Image", {299, 299}, ColorSpace -> "RGB"}],
"Output" -> NetDecoder[{"Class", Range[1008]}]
]


net2[img]


920

UPDATE

If we want to keep neural architecture but change the number of classes (for example, add 10 new), we can trim original architecture using Take.

net3 = NetGraph[
{
Take[net, {NetPort["Input"], "flatten"}],
1008 + 10,
SoftmaxLayer[]
},
{1 -> 2 -> 3},
"Input" -> NetEncoder[{"Image", {299, 299}, ColorSpace -> "RGB"}],
"Output" -> NetDecoder[{"Class", Range[1008 + 10]}]
]


But this network should be trained again, because it doesn't keep information about previously learned classes.

NetExtract[net3, {2, "Weights"}]
`

Automatic

• Ohh, thank you! I thought I should overwrite the ports! Nice solution. – s.s.o Nov 22 '16 at 9:52
• @s.s.o I am glad to help you. Look at the image. This is a network inside the network. – Alexey Golyshev Nov 22 '16 at 10:50
• It was helpful thank you very much. I would like to know if it's possible in the inception model to change the number of output classes. Currently, I want to use the structure but not pretrained model with 1008 classes. Lets say I want 10 new classes for output. How can I modify DotPlusLayer("fc1"), SoftMaxLayer("softmax") and Port "Output" for this 10 classes. If it's possible I'll edit the question with new parts, too. – s.s.o Nov 22 '16 at 19:53
• @s.s.o See update. – Alexey Golyshev Nov 23 '16 at 6:20