6
$\begingroup$

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 = NeuralNetworks`ImportMXNetModel[
 "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?

Upadate:

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?

$\endgroup$
5
$\begingroup$
net = NeuralNetworks`ImportMXNetModel[
  "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]}]
  ]

enter image description here

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]}]
  ]

enter image description here

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

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

Automatic

$\endgroup$
  • $\begingroup$ Ohh, thank you! I thought I should overwrite the ports! Nice solution. $\endgroup$ – s.s.o Nov 22 '16 at 9:52
  • $\begingroup$ @s.s.o I am glad to help you. Look at the image. This is a network inside the network. $\endgroup$ – Alexey Golyshev Nov 22 '16 at 10:50
  • $\begingroup$ 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. $\endgroup$ – s.s.o Nov 22 '16 at 19:53
  • $\begingroup$ @s.s.o See update. $\endgroup$ – Alexey Golyshev Nov 23 '16 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.