简体   繁体   中英

Display output of vgg19 layer as image

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: 从vgg图层进行图像重建 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)

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. Eg (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM