Keras Style Transfer 通过平均像素去除零中心

Keras Style Transfer remove zero-center by mean pixel

我正在使用 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

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

为什么会有“BGR”?开头不是应该是"RGB"吗?

1.Vgg-19 型号 preprocessing_input function 文档:

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.In 简而言之,图像从 RGB 转换为 BGR,然后每个颜色通道相对于 ImageNet 数据集是 zero-centered,没有缩放和平均值用于每个 zero-centering频道是 [103.939, 116.779, 123.68] 。

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

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

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