0

在閱讀Tensorflow implmentation of VGG model時,我注意到作者對輸入的RGB圖像執行了一些縮放操作,如下圖所示。我有兩個問題:VGG_MEAN 是什麼意思,以及如何獲得該設置?其次,爲什麼我們需要減去這些平均值獲得bgr關於運行vgg模型的圖像縮放操作

VGG_MEAN = [103.939, 116.779, 123.68] 

ef build(self, rgb): 
    """ 
    load variable from npy to build the VGG 
    :param rgb: rgb image [batch, height, width, 3] values scaled [0, 1] 
    """ 

    start_time = time.time() 
    print("build model started") 
    rgb_scaled = rgb * 255.0 

    # Convert RGB to BGR 
    red, green, blue = tf.split(3, 3, rgb_scaled) 
    assert red.get_shape().as_list()[1:] == [224, 224, 1] 
    assert green.get_shape().as_list()[1:] == [224, 224, 1] 
    assert blue.get_shape().as_list()[1:] == [224, 224, 1] 
    bgr = tf.concat(3, [ 
     blue - VGG_MEAN[0], 
     green - VGG_MEAN[1], 
     red - VGG_MEAN[2], 
    ]) 
    assert bgr.get_shape().as_list()[1:] == [224, 224, 3] 

回答

0
  1. 平均值是從訓練數據計算每一層的平均水平。
  2. rgb - > bgr是opencv的問題。
+0

嗨喬尼,對答覆表示感謝。但原始文章中包含的原始代碼不會導入opencv。 – user288609

0

該模型從Caffe移植而來,我相信它依賴於OpenCV功能並使用BGR通道的OpenCV約定。

0

第一關:你會使用到RGB轉換爲BGR OpenCV的代碼是:

from cv2 import cvtColor, COLOR_RGB2BGR 
img = cvtColor(img, COLOR_RGB2BGR) 

在你的代碼,這不這是代碼:

bgr = tf.concat(3, [ 
    blue - VGG_MEAN[0], 
    green - VGG_MEAN[1], 
    red - VGG_MEAN[2], 
]) 

圖像不[Height x Width]矩陣,它們是[H x W x C]立方體,其中C是顏色通道。在RGB到BGR中,您正在交換第一個和第三個通道。

第二:你不減去獲得BGR的平均值,你這樣做是爲了將顏色通道值歸一化到以均值爲中心 - 所以數值將在[-125,130]的範圍內,而不是[0,255]的範圍。

參見:Subtract mean from image

我寫了一個python腳本,以獲得BGR通道是指在目錄中的所有圖像,這可能是對你有用:https://github.com/ebigelow/save-deep/blob/master/get_mean.py