Why feature vectors have a lot of zero values in Keras VGG16 model's output?











up vote
0
down vote

favorite












I am trying to extract the features from the last layer of VGG16 model in Keras using the following code:



from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np

model = VGG16(weights='imagenet', include_top=True )


img_path = 'E:projectKERAS DEEPpoodle.png'
img = image.load_img(img_path, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
model.summary()
model.layers.pop();
model.outputs = [model.layers[-1].output]
model.layers[-1].outbound_nodes =
feature = model.predict(img_data)[0]


feature variable supposes to be the feature vector but it has a lot of zeros, I think because of relu layer. In Matlab, for example, the extracted features vector seems to have both positive and negative values, how I can get the same with keras model?



The matlab code is:



im=imread('poodle.png');
im=imresize(im,[224,224]);
net=vgg16;
trainingFeatures = activations(net, im, 'fc7', ...
'OutputAs', 'rows');


the two output vectors feature and trainingFeatures as following (python output to the left and Matlab's to the right



Samples from the Keras outputenter image description here



And here is the tested image:



enter image description here










share|improve this question
























  • There is no way to answer this if you don't include the equivalent Matlab code and the results you are seeing.
    – Matias Valdenegro
    Nov 21 at 22:50










  • Edited! I think its more clear now :)
    – Ahmed Tarawneh
    Nov 22 at 12:05










  • Which variables are you showing in the two pictures? feature and trainingFeatures? Also where is ResNet coming into this, it looks like you've only used VGG? Is your python model using ReLu and your MATLAB model using tanh maybe?
    – Dan
    Nov 22 at 12:11










  • Yes, feature and trainingFeatures. I have edited the code and used vgg16 in both Python and Matlab for more clarification. Keras and Matlab model (vgg16) use the same ReLu layer
    – Ahmed Tarawneh
    Nov 22 at 12:17








  • 1




    Yes, it seems to be before ReLU, you can't extract the same in Keras as each layer has the ReLU embedded inside the layer. This explains all the differences.
    – Matias Valdenegro
    Nov 22 at 13:33















up vote
0
down vote

favorite












I am trying to extract the features from the last layer of VGG16 model in Keras using the following code:



from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np

model = VGG16(weights='imagenet', include_top=True )


img_path = 'E:projectKERAS DEEPpoodle.png'
img = image.load_img(img_path, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
model.summary()
model.layers.pop();
model.outputs = [model.layers[-1].output]
model.layers[-1].outbound_nodes =
feature = model.predict(img_data)[0]


feature variable supposes to be the feature vector but it has a lot of zeros, I think because of relu layer. In Matlab, for example, the extracted features vector seems to have both positive and negative values, how I can get the same with keras model?



The matlab code is:



im=imread('poodle.png');
im=imresize(im,[224,224]);
net=vgg16;
trainingFeatures = activations(net, im, 'fc7', ...
'OutputAs', 'rows');


the two output vectors feature and trainingFeatures as following (python output to the left and Matlab's to the right



Samples from the Keras outputenter image description here



And here is the tested image:



enter image description here










share|improve this question
























  • There is no way to answer this if you don't include the equivalent Matlab code and the results you are seeing.
    – Matias Valdenegro
    Nov 21 at 22:50










  • Edited! I think its more clear now :)
    – Ahmed Tarawneh
    Nov 22 at 12:05










  • Which variables are you showing in the two pictures? feature and trainingFeatures? Also where is ResNet coming into this, it looks like you've only used VGG? Is your python model using ReLu and your MATLAB model using tanh maybe?
    – Dan
    Nov 22 at 12:11










  • Yes, feature and trainingFeatures. I have edited the code and used vgg16 in both Python and Matlab for more clarification. Keras and Matlab model (vgg16) use the same ReLu layer
    – Ahmed Tarawneh
    Nov 22 at 12:17








  • 1




    Yes, it seems to be before ReLU, you can't extract the same in Keras as each layer has the ReLU embedded inside the layer. This explains all the differences.
    – Matias Valdenegro
    Nov 22 at 13:33













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to extract the features from the last layer of VGG16 model in Keras using the following code:



from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np

model = VGG16(weights='imagenet', include_top=True )


img_path = 'E:projectKERAS DEEPpoodle.png'
img = image.load_img(img_path, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
model.summary()
model.layers.pop();
model.outputs = [model.layers[-1].output]
model.layers[-1].outbound_nodes =
feature = model.predict(img_data)[0]


feature variable supposes to be the feature vector but it has a lot of zeros, I think because of relu layer. In Matlab, for example, the extracted features vector seems to have both positive and negative values, how I can get the same with keras model?



The matlab code is:



im=imread('poodle.png');
im=imresize(im,[224,224]);
net=vgg16;
trainingFeatures = activations(net, im, 'fc7', ...
'OutputAs', 'rows');


the two output vectors feature and trainingFeatures as following (python output to the left and Matlab's to the right



Samples from the Keras outputenter image description here



And here is the tested image:



enter image description here










share|improve this question















I am trying to extract the features from the last layer of VGG16 model in Keras using the following code:



from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np

model = VGG16(weights='imagenet', include_top=True )


img_path = 'E:projectKERAS DEEPpoodle.png'
img = image.load_img(img_path, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
model.summary()
model.layers.pop();
model.outputs = [model.layers[-1].output]
model.layers[-1].outbound_nodes =
feature = model.predict(img_data)[0]


feature variable supposes to be the feature vector but it has a lot of zeros, I think because of relu layer. In Matlab, for example, the extracted features vector seems to have both positive and negative values, how I can get the same with keras model?



The matlab code is:



im=imread('poodle.png');
im=imresize(im,[224,224]);
net=vgg16;
trainingFeatures = activations(net, im, 'fc7', ...
'OutputAs', 'rows');


the two output vectors feature and trainingFeatures as following (python output to the left and Matlab's to the right



Samples from the Keras outputenter image description here



And here is the tested image:



enter image description here







python tensorflow keras deep-learning feature-extraction






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 12:52

























asked Nov 21 at 20:54









Ahmed Tarawneh

167




167












  • There is no way to answer this if you don't include the equivalent Matlab code and the results you are seeing.
    – Matias Valdenegro
    Nov 21 at 22:50










  • Edited! I think its more clear now :)
    – Ahmed Tarawneh
    Nov 22 at 12:05










  • Which variables are you showing in the two pictures? feature and trainingFeatures? Also where is ResNet coming into this, it looks like you've only used VGG? Is your python model using ReLu and your MATLAB model using tanh maybe?
    – Dan
    Nov 22 at 12:11










  • Yes, feature and trainingFeatures. I have edited the code and used vgg16 in both Python and Matlab for more clarification. Keras and Matlab model (vgg16) use the same ReLu layer
    – Ahmed Tarawneh
    Nov 22 at 12:17








  • 1




    Yes, it seems to be before ReLU, you can't extract the same in Keras as each layer has the ReLU embedded inside the layer. This explains all the differences.
    – Matias Valdenegro
    Nov 22 at 13:33


















  • There is no way to answer this if you don't include the equivalent Matlab code and the results you are seeing.
    – Matias Valdenegro
    Nov 21 at 22:50










  • Edited! I think its more clear now :)
    – Ahmed Tarawneh
    Nov 22 at 12:05










  • Which variables are you showing in the two pictures? feature and trainingFeatures? Also where is ResNet coming into this, it looks like you've only used VGG? Is your python model using ReLu and your MATLAB model using tanh maybe?
    – Dan
    Nov 22 at 12:11










  • Yes, feature and trainingFeatures. I have edited the code and used vgg16 in both Python and Matlab for more clarification. Keras and Matlab model (vgg16) use the same ReLu layer
    – Ahmed Tarawneh
    Nov 22 at 12:17








  • 1




    Yes, it seems to be before ReLU, you can't extract the same in Keras as each layer has the ReLU embedded inside the layer. This explains all the differences.
    – Matias Valdenegro
    Nov 22 at 13:33
















There is no way to answer this if you don't include the equivalent Matlab code and the results you are seeing.
– Matias Valdenegro
Nov 21 at 22:50




There is no way to answer this if you don't include the equivalent Matlab code and the results you are seeing.
– Matias Valdenegro
Nov 21 at 22:50












Edited! I think its more clear now :)
– Ahmed Tarawneh
Nov 22 at 12:05




Edited! I think its more clear now :)
– Ahmed Tarawneh
Nov 22 at 12:05












Which variables are you showing in the two pictures? feature and trainingFeatures? Also where is ResNet coming into this, it looks like you've only used VGG? Is your python model using ReLu and your MATLAB model using tanh maybe?
– Dan
Nov 22 at 12:11




Which variables are you showing in the two pictures? feature and trainingFeatures? Also where is ResNet coming into this, it looks like you've only used VGG? Is your python model using ReLu and your MATLAB model using tanh maybe?
– Dan
Nov 22 at 12:11












Yes, feature and trainingFeatures. I have edited the code and used vgg16 in both Python and Matlab for more clarification. Keras and Matlab model (vgg16) use the same ReLu layer
– Ahmed Tarawneh
Nov 22 at 12:17






Yes, feature and trainingFeatures. I have edited the code and used vgg16 in both Python and Matlab for more clarification. Keras and Matlab model (vgg16) use the same ReLu layer
– Ahmed Tarawneh
Nov 22 at 12:17






1




1




Yes, it seems to be before ReLU, you can't extract the same in Keras as each layer has the ReLU embedded inside the layer. This explains all the differences.
– Matias Valdenegro
Nov 22 at 13:33




Yes, it seems to be before ReLU, you can't extract the same in Keras as each layer has the ReLU embedded inside the layer. This explains all the differences.
– Matias Valdenegro
Nov 22 at 13:33

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420338%2fwhy-feature-vectors-have-a-lot-of-zero-values-in-keras-vgg16-models-output%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420338%2fwhy-feature-vectors-have-a-lot-of-zero-values-in-keras-vgg16-models-output%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Fiat S.p.A.

Type 'String' is not a subtype of type 'int' of 'index'