Looking at the mxnet documentation.

It takes the pretrained squeenext1_1 weights, and sets imagenet_hotdog_index variable to 713.

    net = models.squeezenet1_1(pretrained=True, prefix='deep_dog_', ctx=contexts)
# hot dog happens to be a class in imagenet.
# we can reuse the weight for that class for better performance
# here's the index for that class for later use
imagenet_hotdog_index = 713

Then they set a 2 class output layer on top of it

    deep_dog_net = models.squeezenet1_1(prefix='deep_dog_', classes=2)
deep_dog_net.features = net.features

Where I get confused is in the classify_hotog function - it is applying softmax to the output layer, and then returning the highest result index. This would make perfect sense if we had somehow told the network to compare against index 713. But it is called for prediction before the index variable is being reused? How does the network know the class to compare against is index 713/hot dog? We've basically taken squeezenet and reduced it down to 2 class output. But how does the network know what class to compare it to? Why would it give a high probability to the second argument/class when showing a hotdog - seems to me it shouldn't know what class it is comparing it to?

out = mx.nd.SoftmaxActivation(net(image.as_in_context(contexts[0])))
print('Probabilities are: '+str(out[0].asnumpy()))
result = np.argmax(out.asnumpy())

I would have expected it to maybe use the full squeezenet output layer, and set something like

if np.argmax(out.asnumpy()) == 713:
     "Hot Dog!'
     "Not hot dog!"

Never understood this and would appreciate if anyone could help me get this detail.


You are on the right track. But you would have to realize that 713 is only, effectively, being used as an index lookup to get the weights just for that class and "seed" your new model. From there, model becomes a binary classification model where 0 is nothotdog and 1 is hotdog. Once the weights are picked up, 713 shouldn't be found anywhere else in your model.

So the output, initially, is going to be something [0.493, 0.507], each corresponding to the probability that the image belongs to one of the two classes. From there, the call with argmax() is looking for which probability is highest, according to the binary selection. So it would pick 0.507 and call it a hotdog since that's corresponds to the binary value of 1.

If you want to verify me on all this, go to your output code and do something like print(out.asnumpy()) and you'll see what I mean.

  • 1
    $\begingroup$ Aha so basically you are saying that it doesn't know what specifically to look for once it's reduced to two classes. This happens when you load the weights from the class. Alternatively (if the particular class you are looking for doesn't exist), you can just train it on a labelled dataset. So checking if the out-of-the-box model predicts index 713, is kind of functionally equivalent to reducing to 2 classes and loading the 713 index weight? Am I understanding this right? Thanks!!! $\endgroup$ – L Xandor Feb 20 '19 at 23:23

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.