3
$\begingroup$

I have come across this neat explanation on how to adjust a pre-trained Inception neural net to an arbitrary number of classes by truncating the top layers.

However, my input data does not come in $299 \times 299$ RGB images, but rather in $100 \times 100$ RGB images. I want to essentially get rid of the input layer, and replace it by a custom one ($3 \times 100 \times 100$ instead of $3 \times 299 \times 299$).

Here would be the loaded MXNet model:

net = NeuralNetworks`ImportMXNetModel[
   NotebookDirectory[] <> "model//Inception-7-symbol.json", 
   NotebookDirectory[] <> "model//Inception-7-0001.params"];

Which we then adapt to a custom number of classes (75):

net2 = NetGraph[{Take[net, {NetPort["Input"], "flatten"}],
 75, SoftmaxLayer[]},
 {1 -> 2 -> 3},
 "Input" -> NetEncoder[{"Image", {100, 100}, ColorSpace -> "RGB"}],
 "Output" -> NetDecoder[{"Class", Range[0, 74]}]]

However, as expected, this throws an error, because, from what I understand, I am simply adding a new Input layer, and not modifying the one from net.

How could I adapt the existing model (net) to handle inputs (i.e., images) with a lower number of dimensions (resolution)?

I know this can be done in Python's keras module by re-wiring the inputs to a custom Input layer, but I am not sure on how to proceed in Mathematica.

Any help and advice appreciated!

$\endgroup$
1
$\begingroup$

Netencoders of type image will automatically upsample or downsample images to the size specified in the netencoder function. You cannot change the size of the network input, as classifier at the end has a fully connected layer with 1000 units and 1024 weights per unit. Changing the upstream convolution layer size will lead to a change in the number of inputs to the fully connected layer. You could respecify the fully connected layer and retrain the network, or leave the 299x299 netencoder attached and allow it to upsample your images.

$\endgroup$
  • $\begingroup$ Thanks, I just saw the preprocess.py script included with the model, does exactly this. $\endgroup$ – FisherDisinformation Mar 4 '17 at 17:57
  • $\begingroup$ If my understanding is correct, I think this may not be true for an inception type neural network which is constructed from only convolutions (and some concatenate and softmax operations). In that case, the network works for any size image. $\endgroup$ – xslittlegrass Mar 23 '17 at 20:53
  • $\begingroup$ If your input size varied, the size of the final average pool output would vary, then the number of inputs to the linear layer would vary, which is disallowed. Spatial pyramid pooling was one technique that was used to address this issue, you can check the paper out by Kaiming He if you want to know more. It's now possible to implement in Mathematica using ResizeLayer. $\endgroup$ – Yss Mar 24 '17 at 1:14

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.