I was playing with the neural network in Mathematica and found that the same neural network gives different classification results to the same image, which is very strange.
Consider classifying this dog image using the "Wolfram ImageIdentify Net":
imgd =
ImageResize[
ImageCrop@Import["https://i.stack.imgur.com/8HXqN.jpg"], {224, 224}];
net = NetModel["Wolfram ImageIdentify Net for WL 11.1"];
net[imgd]
(* Entity["Concept", "GoldenRetriever::t59tg"] *)
We see that the image is correctly classified.
Now I construct another neural network using part of the first network
array = List@Normal[SparseArray[{2241 -> 1}, {4315}]];
net2 =
NetChain[{Take[net, {1, "softmax"}],
LinearLayer[1, "Weights" -> array, "Biases" -> None],
SummationLayer[]}];
then do something irrelevant to the first neural network
imtest = imgd;
Table[
gd = net2[imtest, NetPortGradient["Input"]];
gdimg = ImageAdjust@NetDecoder["Image"][gd];
imtest = ImageAdjust[imtest + 0.1*gdimg];
, {20}];
And now the classification of the dog image using the first neural network is not correct anymore
net[imgd]
(* Entity["Concept", "Colander::73kyp"] *)
However, both the image and the neural network are identical as before
imgd ==
ImageResize[
ImageCrop@Import["https://i.stack.imgur.com/8HXqN.jpg"], {224, 224}]
(* True *)
net == NetModel["Wolfram ImageIdentify Net for WL 11.1"]
(* True *)
So why does this happen? Is this a bug?
I'm using Mathematica version 11.1.1.0 on macOS 10.12.4.
It looks like the problem is in the BatchNormalizationLayer
. Although the two BatchNormalizationLayers have identical parameters, they produce different results:
net3 = NetModel["Wolfram ImageIdentify Net for WL 11.1"];
data = Take[net, {1, "conv_1"}][imgd];
InputForm[NetExtract[net, "bn_1"]] == InputForm[NetExtract[net3, "bn_1"]]
(* True *)
NetExtract[net, "bn_1"][data] == NetExtract[net3, "bn_1"][data]
(* False *)
I've also tested other models, it seems that only models contain the batch normalization layers have this problem. Networks don't have batch normalization such as VGG-16 behaves as what we expected.