简体   繁体   English

将vgg19层的输出显示为图像

[英]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. 在本文中,作者从vgg19层的输出中重建图像。 I am using Keras. 我正在使用Keras。 The size of output of block1_conv1 layer is (1, 400, 533, 64) . block1_conv1层的输出大小为( block1_conv1 (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. 这里1是输入的图像数,400是行数,533列数和64通道数。 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. 当我尝试将其重建为图像时,由于图像大小为13644800(不是3的倍数)而出现错误,因此无法在三个通道中显示图像。 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. “的输出的大小block1_conv1层是(1, 400, 533, 64 ),这里1是图像作为输入的数,400是行数,533列的数量和64号的信道”但是,这是不正确的。 The block1_conv1 output corresponds 1 channel dimension(channel first), 400 * 533 image dimension and 64 filters . block1_conv1输出对应于1个通道尺寸(通道在前),400 * 533图像尺寸和64个滤镜

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. 当您尝试将具有1通道(400 * 533 * 64 = 13644800)的图像输入的VGG19输出矢量重塑为对应于3通道输出的矢量时,会发生错误。

Furthermore you have to pass 3 channel input: 此外,您必须传递3通道输入:

From the VGG19 code: VGG19代码中:

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. input_shape:可选的形状元组,仅当include_top为False时才指定(否则,输入形状必须为(224, 224, 3) 224,224,3 (224, 224, 3) (使用channels_last数据格式)或(3, 224, 224) (使用channels_first数据格式)。它应该有3个输入通道,宽度和高度不小于32。例如(200, 200, 3)是一个有效值。

Thus your input images has to be 3 channels. 因此,您的输入图像必须是3个通道。 If you even want to feed 1 channel(grayscale) images to VGG19 you should make the following, if channels first : 如果你甚至想喂1个通道(灰度)图像以VGG19你应该做以下,如果channels first

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

or 要么

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

if channels last without batch dimension or 如果channels last 没有批次尺寸,或者

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. 如果您提供有关图像输入矩阵的实际尺寸及其类型(灰度,RGB)的更多信息,我会在需要时为您提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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