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
And here is the tested image:
python tensorflow keras deep-learning feature-extraction
|
show 7 more comments
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
And here is the tested image:
python tensorflow keras deep-learning feature-extraction
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
andtrainingFeatures
? 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
|
show 7 more comments
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
And here is the tested image:
python tensorflow keras deep-learning feature-extraction
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
And here is the tested image:
python tensorflow keras deep-learning feature-extraction
python tensorflow keras deep-learning feature-extraction
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
andtrainingFeatures
? 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
|
show 7 more comments
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
andtrainingFeatures
? 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
|
show 7 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
andtrainingFeatures
? 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