當我有一個影像時,我可以按如下方式對影像通道進行標準化:
image[:, :, 0] = ((image[:, :, 0]-mean_1))/std_1
image[:, :, 1] = ((image[:, :, 1]-mean_2))/std_2
image[:, :, 2] = ((image[:, :, 2]-mean_3))/std_3
其中 mean_1 和 std_1 是第一個通道均值和標準差。mean_2、std_2、mean_3 和 std_3 相同。但現在影像是一個張量并具有以下資訊:
(460, 700, 3) <dtype: 'float32'>
<class 'tensorflow.python.framework.ops.Tensor'>
我是 tensorflow 的新手,我不知道如何將上述公式轉換為對張量執行相同任務的代碼image
?
編輯:平均值和標準是由我在所有資料集影像上計算的。所以我有他們的價值觀。
更新 1:我嘗試使用 tf.keras.layers.Normalization 來解決這個問題,阻礙了我的模型:
inputs = keras.Input(shape=(460,700,3))
norm_layer = Normalization(mean=[200.827,160.252,195.008],
variance=[np.square(33.154),
np.square(45.877),
np.square(29.523)])
inputs=norm_layer(inputs)
這提出了兩個新問題:
tf.keras.layers.Normalization 和上面的代碼是否根據我的需要對每個通道的輸入進行了標準化?
使用上面的代碼,tf.keras.layers.Normalization 是否僅適用于測驗和驗證資料或訓練資料?我需要它來處理所有資料集。
請幫幫我:(我很困惑。
uj5u.com熱心網友回復:
更新 1:修復以顯示如何與預處理層一起使用
import tensorflow as tf
import numpy as np
# Create random img
img = tf.Variable(np.random.randint(0, 255, (10, 224, 224 ,3)), dtype=tf.uint8)
# Create prerprocessing layer
# Note: Works with tensorflow 2.6 and up
norm_layer = tf.keras.layers.Normalization(mean=[0.485, 0.456, 0.406], variance=[np.square(33.154), np.square(45.877), np.square(29.523)])
# Apply norm_layer to your image
# You need not add it to your model
norm_img = norm_layer(img)
# or
# Use via numpy but the output is a tensor since your running a preprocesisng layer
# norm_img = norm_layer(img.numpy())
# Run model prediction
predictions = model.predict(norm_img)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/393780.html
上一篇:為什么我的模型在使用tf.data加載資料時精度很差?
下一篇:詳談單元測驗