2

我正在使用 Keras 进行图像样式转移,但我陷入了通过平均像素移除零中心的部分

from __future__ import print_function
from keras.preprocessing.image import load_img, img_to_array
from scipy.misc import imsave
import numpy as np
from scipy.optimize import fmin_l_bfgs_b
import time
import argparse

from keras.applications import vgg19
from keras import backend as K

base_image_path = "images/input.jpg"
style_reference_image_path = "images/style.jpg"
result_prefix = "output"
iterations = 10

# Weights
content_weight = 0.025
style_weight = 1.0
# total variation weight
total_variation_weight = 1.0

# output 
width, height = load_img(base_image_path).size
img_nrows = 400
img_ncols = int(width * img_nrows / height)

# Fit into VGG19 format
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

# Turning feature vectors into image
def deprocess_image(x):
    if K.image_data_format() == 'channels_first':
        x = x.reshape((3, img_nrows, img_ncols))
        x = x.transpose((1, 2, 0))
    else:
        x = x.reshape((img_nrows, img_ncols, 3))
    # (Remove zero-center by mean pixel)
    x[:, :, 0] += 103.939
    x[:, :, 1] += 116.779
    x[:, :, 2] += 123.68
    # 'BGR'->'RGB'
    x = x[:, :, ::-1]
    x = np.clip(x, 0, 255).astype('uint8')
    return x

最后一部分(通过平均像素删除零中心),我在谷歌上搜索但找不到类似的方法。103.939、116.779 和 123.68 --> 我无法使用图像的平均值计算这些数字。

为什么会有“BGR”?他们不应该在一开始就处于“RGB”吗?

4

1 回答 1

4

1.Vgg-19模型 preprocessing_input函数文档:

def preprocess_input(x, data_format=None, mode='caffe', **kwargs):
"""Preprocesses a tensor or Numpy array encoding a batch of images.
# Arguments
    x: Input Numpy or symbolic tensor, 3D or 4D.
        The preprocessed data is written over the input data
        if the data types are compatible. To avoid this
        behaviour, `numpy.copy(x)` can be used.
    data_format: Data format of the image tensor/array.
    mode: One of "caffe", "tf" or "torch".
        - caffe: will convert the images from RGB to BGR,
            then will zero-center each color channel with
            respect to the ImageNet dataset,
            without scaling.
        - tf: will scale pixels between -1 and 1,
            sample-wise.
        - torch: will scale pixels between 0 and 1 and then
            will normalize each channel with respect to the
            ImageNet dataset.
# Returns
    Preprocessed tensor or Numpy array.

2.简而言之,图像从 RGB 转换为 BGR,然后每个颜色通道相对于 ImageNet 数据集以零为中心,没有缩放,用于每个通道的零中心的平均值为 [103.939, 116.779, 123.68]。

3. 在 deprocess_image() 函数中,相同的平均值 ([103.939, 116.779, 123.68]) 被添加到每个相应的通道,然后从 'BGR' -> 'RGB' 转换回原始形式,

注意:- 数据集的平均值是所有颜色通道(例如 RBG)上所有图像的像素的平均值。灰度图像只有一个平均值,而像 ImageNet 这样的彩色图像将有 3 个平均值。

通常在训练集上计算均值,并使用相同的均值对训练和测试图像进​​行归一化。

于 2020-10-09T08:11:33.490 回答