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?
Answer
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]}]
]
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
Comments
Post a Comment