2017-02-10 190 views
2

我正在尋找關於如何從輸入圖像中減去平均圖像的代碼片段。我已經將圖像意味着作爲numpy的數組:Tensorflow:如何減去平均圖像?

IMG_MEAN = np.array((104.00698793,116.66876762,122.67891434), dtype=np.float32) 

我猜減法部分會像

img = tf.subtract(img, mean_img) 

但如何創建mean_image與外形一樣IMG?

回答

2

假設IMG是所有一個矩陣,大小爲(100,100,3) RGB的意思是[1,2,3]

img = tf.ones([100, 100, 3], dtype=tf.float32) #(100, 100, 3) 
mean = tf.constant([1, 2, 3], dtype=tf.float32) # (3) 
mean = tf.reshape(mean, [1, 1, 3]) 

img_m = img - mean 

with tf.Session() as sess: 
    a = sess.run(img_m) # shape of a (100, 100, 3) 
    # a[:,:,0] = 0 
    # a[:,:,1] = -1 
    # a[:,:,1] = -2 
0

img = tf.subtract(img, mean_img)是正確只要img最後一維( tf.shape(img)[-1])是3.

您可以檢查Broadcasting operation in tensorflow Glossarynumpy Broadcasting瞭解更多信息。

如何創建與img相同形狀的mean_image?

你可以試試:

tf.reshape(tf.tile(m, [tf.reduce_prod(tf.shape(img)[:-1])]), 
      tf.shape(img)).eval()