【问题标题】:Display output of vgg19 layer as image将 vgg19 层的输出显示为图像
【发布时间】:2019-04-25 11:48:24
【问题描述】:

我正在阅读这篇论文:Neural Style Transfer。在本文中,作者从 vgg19 层的输出重建图像。我正在使用 Keras。 block1_conv1 层的输出大小为(1, 400, 533, 64)。这里 1 是作为输入的图像数,400 是行数,533 列数和 64 通道数。当我尝试将其重建为图像时,由于图像大小为 13644800 不是 3 的倍数,因此出现错误,因此我无法在三个通道中显示图像。如何重建此图像?

我想从图层重建图像,如下所示: 以下是相同的代码:

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

我收到以下错误:

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

【问题讨论】:

  • 更多信息和代码,如果您需要帮助,请提供帮助
  • 添加代码和更多信息
  • 在这种情况下添加完整的原始错误消息是明智的。
  • @AbhayGupta 我也在研究 NST 并面临与您相同的错误。如果你找到了解决办法,能告诉我吗?

标签: python tensorflow keras conv-neural-network vgg-net


【解决方案1】:

您写道:

"block1_conv1层的输出大小为(1, 400, 533, 64).这里1 是作为输入的图像数量,400 是行数,533 是 列数和64个通道数" 但这是不正确的。 block1_conv1 输出对应 1 个通道维度(通道优先)、400 * 533 图像维度和 64 个过滤器。

当您尝试将具有 1 个通道 (400 * 533 * 64 = 13644800) 的图像输入的 VGG19 输出向量重塑为对应于3通道输出。

此外,您必须通过 3 通道 输入:

来自 VGG19 代码:

input_shape: 可选的形状元组,只需要指定 如果include_top 为 False(否则输入形状 必须是(224, 224, 3) (带channels_last数据格式) 或(3, 224, 224)(使用channels_first 数据格式)。 它应该正好有 3 个输入通道, 宽高不小于32。 例如。 (200, 200, 3) 将是一个有效值。

因此,您的输入图像必须是 3 个通道。如果您甚至想将 1 个通道(灰度)图像提供给 VGG19,您应该执行以下操作,如果 channels first:

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

或

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

如果channels last 没有批次维度或

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

批量维度。

如果您提供有关图像输入矩阵的实际尺寸及其类型(灰度、RGB)的更多信息,我可以在需要时为您提供更多帮助。

【讨论】:

  • 您误解了大小。 1 是批量大小,而不是通道数。通道数为 64。我想将其转换为 3 通道 RGB 图像。我猜这个问题不清楚。我也相应地更新了问题。
  • @AbhayGupta 不,64 是 过滤器 的数量。无论如何,我研究了你更新的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 2019-05-16
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多