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

问题描述

我正在与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,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。简而言之,将图像从RGB转换为BGR,然后相对于ImageNet数据集将每个颜色通道零居中,而无需缩放,用于每个通道零居中的平均值为[103.939,116.779,123.68 ]。

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

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

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