Display output of vgg19 layer as image












0














I was reading this paper: Neural Style Transfer. In this paper author reconstructs image from output of layers of vgg19. I am using Keras. The size of output of block1_conv1 layer is (1, 400, 533, 64). Here 1 is number of images as input, 400 is number of rows, 533 number of columns and 64 number of channels. When I try to reconstruct it as an image, I get an error as size of image is 13644800 which is not a multiple of 3, so I can't display the image in three channels. How can I reconstruct this image?



I want to reconstruct images from layers as shown below:
image reconstruction from vgg layers
Below is the code for the same:



from keras.preprocessing.image import load_img, img_to_array
from scipy.misc import imsave
import numpy as np
from keras.applications import vgg19
from keras import backend as K

CONTENT_IMAGE_FN = store image as input here

def preprocess_image(image_path):
img = load_img(image_path, target_size=(img_nrows, img_ncols))
img = img_to_array(img)
img = np.expand_dims(img, axis=0)
img = vgg19.preprocess_input(img)
return img

width, height = load_img(CONTENT_IMAGE_FN).size
img_nrows = 400
img_ncols = int(width * img_nrows / height)
base_image = K.variable(preprocess_image(CONTENT_IMAGE_FN))

RESULT_DIR = "generated/"
RESULT_PREFIX = RESULT_DIR + "gen"
if not os.path.exists(RESULT_DIR):
os.makedirs(RESULT_DIR)
result_prefix = RESULT_PREFIX

# this will contain our generated image
if K.image_data_format() == 'channels_first':
combination_image = K.placeholder((1, 3, img_nrows, img_ncols))
else:
combination_image = K.placeholder((1, img_nrows, img_ncols, 3))

x = preprocess_image(CONTENT_IMAGE_FN)

outputs_dict = dict([(layer.name, layer.output) for layer in model.layers])
feature_layers = ['block1_conv1', 'block2_conv1',
'block3_conv1', 'block4_conv1',
'block5_conv1']
outputs =
for layer_name in feature_layers:
outputs.append(outputs_dict[layer_name])
functor = K.function([combination_image], outputs ) # evaluation function

# Testing
test = x
layer_outs = functor([test])
print(layer_outs)

layer_outs[0].reshape(400, -1 , 3) //getting error here


I am getting following error:



ValueError: cannot reshape array of size 13644800 into shape (400,newaxis,3)









share|improve this question




















  • 1




    much more information and codes, please if you need help
    – Geeocode
    Nov 22 at 20:36










  • added code and more information
    – Abhay Gupta
    Nov 23 at 4:27










  • It is wise if you add the full original error message in this case.
    – Geeocode
    Nov 23 at 15:16
















0














I was reading this paper: Neural Style Transfer. In this paper author reconstructs image from output of layers of vgg19. I am using Keras. The size of output of block1_conv1 layer is (1, 400, 533, 64). Here 1 is number of images as input, 400 is number of rows, 533 number of columns and 64 number of channels. When I try to reconstruct it as an image, I get an error as size of image is 13644800 which is not a multiple of 3, so I can't display the image in three channels. How can I reconstruct this image?



I want to reconstruct images from layers as shown below:
image reconstruction from vgg layers
Below is the code for the same:



from keras.preprocessing.image import load_img, img_to_array
from scipy.misc import imsave
import numpy as np
from keras.applications import vgg19
from keras import backend as K

CONTENT_IMAGE_FN = store image as input here

def preprocess_image(image_path):
img = load_img(image_path, target_size=(img_nrows, img_ncols))
img = img_to_array(img)
img = np.expand_dims(img, axis=0)
img = vgg19.preprocess_input(img)
return img

width, height = load_img(CONTENT_IMAGE_FN).size
img_nrows = 400
img_ncols = int(width * img_nrows / height)
base_image = K.variable(preprocess_image(CONTENT_IMAGE_FN))

RESULT_DIR = "generated/"
RESULT_PREFIX = RESULT_DIR + "gen"
if not os.path.exists(RESULT_DIR):
os.makedirs(RESULT_DIR)
result_prefix = RESULT_PREFIX

# this will contain our generated image
if K.image_data_format() == 'channels_first':
combination_image = K.placeholder((1, 3, img_nrows, img_ncols))
else:
combination_image = K.placeholder((1, img_nrows, img_ncols, 3))

x = preprocess_image(CONTENT_IMAGE_FN)

outputs_dict = dict([(layer.name, layer.output) for layer in model.layers])
feature_layers = ['block1_conv1', 'block2_conv1',
'block3_conv1', 'block4_conv1',
'block5_conv1']
outputs =
for layer_name in feature_layers:
outputs.append(outputs_dict[layer_name])
functor = K.function([combination_image], outputs ) # evaluation function

# Testing
test = x
layer_outs = functor([test])
print(layer_outs)

layer_outs[0].reshape(400, -1 , 3) //getting error here


I am getting following error:



ValueError: cannot reshape array of size 13644800 into shape (400,newaxis,3)









share|improve this question




















  • 1




    much more information and codes, please if you need help
    – Geeocode
    Nov 22 at 20:36










  • added code and more information
    – Abhay Gupta
    Nov 23 at 4:27










  • It is wise if you add the full original error message in this case.
    – Geeocode
    Nov 23 at 15:16














0












0








0







I was reading this paper: Neural Style Transfer. In this paper author reconstructs image from output of layers of vgg19. I am using Keras. The size of output of block1_conv1 layer is (1, 400, 533, 64). Here 1 is number of images as input, 400 is number of rows, 533 number of columns and 64 number of channels. When I try to reconstruct it as an image, I get an error as size of image is 13644800 which is not a multiple of 3, so I can't display the image in three channels. How can I reconstruct this image?



I want to reconstruct images from layers as shown below:
image reconstruction from vgg layers
Below is the code for the same:



from keras.preprocessing.image import load_img, img_to_array
from scipy.misc import imsave
import numpy as np
from keras.applications import vgg19
from keras import backend as K

CONTENT_IMAGE_FN = store image as input here

def preprocess_image(image_path):
img = load_img(image_path, target_size=(img_nrows, img_ncols))
img = img_to_array(img)
img = np.expand_dims(img, axis=0)
img = vgg19.preprocess_input(img)
return img

width, height = load_img(CONTENT_IMAGE_FN).size
img_nrows = 400
img_ncols = int(width * img_nrows / height)
base_image = K.variable(preprocess_image(CONTENT_IMAGE_FN))

RESULT_DIR = "generated/"
RESULT_PREFIX = RESULT_DIR + "gen"
if not os.path.exists(RESULT_DIR):
os.makedirs(RESULT_DIR)
result_prefix = RESULT_PREFIX

# this will contain our generated image
if K.image_data_format() == 'channels_first':
combination_image = K.placeholder((1, 3, img_nrows, img_ncols))
else:
combination_image = K.placeholder((1, img_nrows, img_ncols, 3))

x = preprocess_image(CONTENT_IMAGE_FN)

outputs_dict = dict([(layer.name, layer.output) for layer in model.layers])
feature_layers = ['block1_conv1', 'block2_conv1',
'block3_conv1', 'block4_conv1',
'block5_conv1']
outputs =
for layer_name in feature_layers:
outputs.append(outputs_dict[layer_name])
functor = K.function([combination_image], outputs ) # evaluation function

# Testing
test = x
layer_outs = functor([test])
print(layer_outs)

layer_outs[0].reshape(400, -1 , 3) //getting error here


I am getting following error:



ValueError: cannot reshape array of size 13644800 into shape (400,newaxis,3)









share|improve this question















I was reading this paper: Neural Style Transfer. In this paper author reconstructs image from output of layers of vgg19. I am using Keras. The size of output of block1_conv1 layer is (1, 400, 533, 64). Here 1 is number of images as input, 400 is number of rows, 533 number of columns and 64 number of channels. When I try to reconstruct it as an image, I get an error as size of image is 13644800 which is not a multiple of 3, so I can't display the image in three channels. How can I reconstruct this image?



I want to reconstruct images from layers as shown below:
image reconstruction from vgg layers
Below is the code for the same:



from keras.preprocessing.image import load_img, img_to_array
from scipy.misc import imsave
import numpy as np
from keras.applications import vgg19
from keras import backend as K

CONTENT_IMAGE_FN = store image as input here

def preprocess_image(image_path):
img = load_img(image_path, target_size=(img_nrows, img_ncols))
img = img_to_array(img)
img = np.expand_dims(img, axis=0)
img = vgg19.preprocess_input(img)
return img

width, height = load_img(CONTENT_IMAGE_FN).size
img_nrows = 400
img_ncols = int(width * img_nrows / height)
base_image = K.variable(preprocess_image(CONTENT_IMAGE_FN))

RESULT_DIR = "generated/"
RESULT_PREFIX = RESULT_DIR + "gen"
if not os.path.exists(RESULT_DIR):
os.makedirs(RESULT_DIR)
result_prefix = RESULT_PREFIX

# this will contain our generated image
if K.image_data_format() == 'channels_first':
combination_image = K.placeholder((1, 3, img_nrows, img_ncols))
else:
combination_image = K.placeholder((1, img_nrows, img_ncols, 3))

x = preprocess_image(CONTENT_IMAGE_FN)

outputs_dict = dict([(layer.name, layer.output) for layer in model.layers])
feature_layers = ['block1_conv1', 'block2_conv1',
'block3_conv1', 'block4_conv1',
'block5_conv1']
outputs =
for layer_name in feature_layers:
outputs.append(outputs_dict[layer_name])
functor = K.function([combination_image], outputs ) # evaluation function

# Testing
test = x
layer_outs = functor([test])
print(layer_outs)

layer_outs[0].reshape(400, -1 , 3) //getting error here


I am getting following error:



ValueError: cannot reshape array of size 13644800 into shape (400,newaxis,3)






python tensorflow keras conv-neural-network vgg-net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 17:26

























asked Nov 22 at 19:16









Abhay Gupta

98213




98213








  • 1




    much more information and codes, please if you need help
    – Geeocode
    Nov 22 at 20:36










  • added code and more information
    – Abhay Gupta
    Nov 23 at 4:27










  • It is wise if you add the full original error message in this case.
    – Geeocode
    Nov 23 at 15:16














  • 1




    much more information and codes, please if you need help
    – Geeocode
    Nov 22 at 20:36










  • added code and more information
    – Abhay Gupta
    Nov 23 at 4:27










  • It is wise if you add the full original error message in this case.
    – Geeocode
    Nov 23 at 15:16








1




1




much more information and codes, please if you need help
– Geeocode
Nov 22 at 20:36




much more information and codes, please if you need help
– Geeocode
Nov 22 at 20:36












added code and more information
– Abhay Gupta
Nov 23 at 4:27




added code and more information
– Abhay Gupta
Nov 23 at 4:27












It is wise if you add the full original error message in this case.
– Geeocode
Nov 23 at 15:16




It is wise if you add the full original error message in this case.
– Geeocode
Nov 23 at 15:16












1 Answer
1






active

oldest

votes


















0














You wrote:




"The size of output of block1_conv1 layer is (1, 400, 533, 64). Here 1
is number of images as input, 400 is number of rows, 533 number of
columns and 64 number of channels"
But this is not correct. The block1_conv1 output corresponds 1 channel dimension(channel first), 400 * 533 image dimension and 64 filters.




The error occurs, as you try to reshape a vector of VGG19 output of an image input with a 1 channel (400 * 533 * 64 = 13644800) to a vector which correspond to a 3 channels output.



Furthermore you have to pass 3 channel input:



From the VGG19 code:




input_shape: optional shape tuple, only to be specified
if include_top is False (otherwise the input shape
has to be (224, 224, 3)
(with channels_last data format)
or (3, 224, 224) (with channels_first data format).
It should have exactly 3 inputs channels,
and width and height should be no smaller than 32.
E.g. (200, 200, 3) would be one valid value.




Thus your input images has to be 3 channels. If you even want to feed 1 channel(grayscale) images to VGG19 you should make the following, if channels first:



X = np.repeat(X, 3 , axis=0) 


or



X = np.repeat(X, 3 , axis=2) 


if channels last without batch dimension or



X = np.repeat(X, 3 , axis=3) 


with batch dimension.



If you provide more information regarding the actual dimensions of your input matrices of your images and type of it(grayscale,RGB), I can give you more help upon needing it.






share|improve this answer























  • You misunderstood the size. 1 is the batch size, not number of channels. Number of channels is 64. I want to convert it into a 3 channel RGB image. I guess the question was not clear. I have also updated the question accordingly.
    – Abhay Gupta
    Nov 23 at 17:29










  • @AbhayGupta No, 64 is the number of filters. Anyway I study your updated question.
    – Geeocode
    Nov 23 at 21:06











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',
autoActivateHeartbeat: false,
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%2f53436960%2fdisplay-output-of-vgg19-layer-as-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You wrote:




"The size of output of block1_conv1 layer is (1, 400, 533, 64). Here 1
is number of images as input, 400 is number of rows, 533 number of
columns and 64 number of channels"
But this is not correct. The block1_conv1 output corresponds 1 channel dimension(channel first), 400 * 533 image dimension and 64 filters.




The error occurs, as you try to reshape a vector of VGG19 output of an image input with a 1 channel (400 * 533 * 64 = 13644800) to a vector which correspond to a 3 channels output.



Furthermore you have to pass 3 channel input:



From the VGG19 code:




input_shape: optional shape tuple, only to be specified
if include_top is False (otherwise the input shape
has to be (224, 224, 3)
(with channels_last data format)
or (3, 224, 224) (with channels_first data format).
It should have exactly 3 inputs channels,
and width and height should be no smaller than 32.
E.g. (200, 200, 3) would be one valid value.




Thus your input images has to be 3 channels. If you even want to feed 1 channel(grayscale) images to VGG19 you should make the following, if channels first:



X = np.repeat(X, 3 , axis=0) 


or



X = np.repeat(X, 3 , axis=2) 


if channels last without batch dimension or



X = np.repeat(X, 3 , axis=3) 


with batch dimension.



If you provide more information regarding the actual dimensions of your input matrices of your images and type of it(grayscale,RGB), I can give you more help upon needing it.






share|improve this answer























  • You misunderstood the size. 1 is the batch size, not number of channels. Number of channels is 64. I want to convert it into a 3 channel RGB image. I guess the question was not clear. I have also updated the question accordingly.
    – Abhay Gupta
    Nov 23 at 17:29










  • @AbhayGupta No, 64 is the number of filters. Anyway I study your updated question.
    – Geeocode
    Nov 23 at 21:06
















0














You wrote:




"The size of output of block1_conv1 layer is (1, 400, 533, 64). Here 1
is number of images as input, 400 is number of rows, 533 number of
columns and 64 number of channels"
But this is not correct. The block1_conv1 output corresponds 1 channel dimension(channel first), 400 * 533 image dimension and 64 filters.




The error occurs, as you try to reshape a vector of VGG19 output of an image input with a 1 channel (400 * 533 * 64 = 13644800) to a vector which correspond to a 3 channels output.



Furthermore you have to pass 3 channel input:



From the VGG19 code:




input_shape: optional shape tuple, only to be specified
if include_top is False (otherwise the input shape
has to be (224, 224, 3)
(with channels_last data format)
or (3, 224, 224) (with channels_first data format).
It should have exactly 3 inputs channels,
and width and height should be no smaller than 32.
E.g. (200, 200, 3) would be one valid value.




Thus your input images has to be 3 channels. If you even want to feed 1 channel(grayscale) images to VGG19 you should make the following, if channels first:



X = np.repeat(X, 3 , axis=0) 


or



X = np.repeat(X, 3 , axis=2) 


if channels last without batch dimension or



X = np.repeat(X, 3 , axis=3) 


with batch dimension.



If you provide more information regarding the actual dimensions of your input matrices of your images and type of it(grayscale,RGB), I can give you more help upon needing it.






share|improve this answer























  • You misunderstood the size. 1 is the batch size, not number of channels. Number of channels is 64. I want to convert it into a 3 channel RGB image. I guess the question was not clear. I have also updated the question accordingly.
    – Abhay Gupta
    Nov 23 at 17:29










  • @AbhayGupta No, 64 is the number of filters. Anyway I study your updated question.
    – Geeocode
    Nov 23 at 21:06














0












0








0






You wrote:




"The size of output of block1_conv1 layer is (1, 400, 533, 64). Here 1
is number of images as input, 400 is number of rows, 533 number of
columns and 64 number of channels"
But this is not correct. The block1_conv1 output corresponds 1 channel dimension(channel first), 400 * 533 image dimension and 64 filters.




The error occurs, as you try to reshape a vector of VGG19 output of an image input with a 1 channel (400 * 533 * 64 = 13644800) to a vector which correspond to a 3 channels output.



Furthermore you have to pass 3 channel input:



From the VGG19 code:




input_shape: optional shape tuple, only to be specified
if include_top is False (otherwise the input shape
has to be (224, 224, 3)
(with channels_last data format)
or (3, 224, 224) (with channels_first data format).
It should have exactly 3 inputs channels,
and width and height should be no smaller than 32.
E.g. (200, 200, 3) would be one valid value.




Thus your input images has to be 3 channels. If you even want to feed 1 channel(grayscale) images to VGG19 you should make the following, if channels first:



X = np.repeat(X, 3 , axis=0) 


or



X = np.repeat(X, 3 , axis=2) 


if channels last without batch dimension or



X = np.repeat(X, 3 , axis=3) 


with batch dimension.



If you provide more information regarding the actual dimensions of your input matrices of your images and type of it(grayscale,RGB), I can give you more help upon needing it.






share|improve this answer














You wrote:




"The size of output of block1_conv1 layer is (1, 400, 533, 64). Here 1
is number of images as input, 400 is number of rows, 533 number of
columns and 64 number of channels"
But this is not correct. The block1_conv1 output corresponds 1 channel dimension(channel first), 400 * 533 image dimension and 64 filters.




The error occurs, as you try to reshape a vector of VGG19 output of an image input with a 1 channel (400 * 533 * 64 = 13644800) to a vector which correspond to a 3 channels output.



Furthermore you have to pass 3 channel input:



From the VGG19 code:




input_shape: optional shape tuple, only to be specified
if include_top is False (otherwise the input shape
has to be (224, 224, 3)
(with channels_last data format)
or (3, 224, 224) (with channels_first data format).
It should have exactly 3 inputs channels,
and width and height should be no smaller than 32.
E.g. (200, 200, 3) would be one valid value.




Thus your input images has to be 3 channels. If you even want to feed 1 channel(grayscale) images to VGG19 you should make the following, if channels first:



X = np.repeat(X, 3 , axis=0) 


or



X = np.repeat(X, 3 , axis=2) 


if channels last without batch dimension or



X = np.repeat(X, 3 , axis=3) 


with batch dimension.



If you provide more information regarding the actual dimensions of your input matrices of your images and type of it(grayscale,RGB), I can give you more help upon needing it.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 at 22:23

























answered Nov 23 at 14:55









Geeocode

2,1811819




2,1811819












  • You misunderstood the size. 1 is the batch size, not number of channels. Number of channels is 64. I want to convert it into a 3 channel RGB image. I guess the question was not clear. I have also updated the question accordingly.
    – Abhay Gupta
    Nov 23 at 17:29










  • @AbhayGupta No, 64 is the number of filters. Anyway I study your updated question.
    – Geeocode
    Nov 23 at 21:06


















  • You misunderstood the size. 1 is the batch size, not number of channels. Number of channels is 64. I want to convert it into a 3 channel RGB image. I guess the question was not clear. I have also updated the question accordingly.
    – Abhay Gupta
    Nov 23 at 17:29










  • @AbhayGupta No, 64 is the number of filters. Anyway I study your updated question.
    – Geeocode
    Nov 23 at 21:06
















You misunderstood the size. 1 is the batch size, not number of channels. Number of channels is 64. I want to convert it into a 3 channel RGB image. I guess the question was not clear. I have also updated the question accordingly.
– Abhay Gupta
Nov 23 at 17:29




You misunderstood the size. 1 is the batch size, not number of channels. Number of channels is 64. I want to convert it into a 3 channel RGB image. I guess the question was not clear. I have also updated the question accordingly.
– Abhay Gupta
Nov 23 at 17:29












@AbhayGupta No, 64 is the number of filters. Anyway I study your updated question.
– Geeocode
Nov 23 at 21:06




@AbhayGupta No, 64 is the number of filters. Anyway I study your updated question.
– Geeocode
Nov 23 at 21:06


















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%2f53436960%2fdisplay-output-of-vgg19-layer-as-image%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)