[英]How to normalize image in tensorflow.js?
我在 pytorch 的訓練階段應用了轉換,然后我將 model 轉換為在 tensorflow.js 中運行。 它工作正常,但由於我沒有應用相同的轉換而得到錯誤的預測。
test_transform = torchvision.transforms.Compose([
torchvision.transforms.Resize(size=(224, 224)),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
我可以調整圖像大小但無法正常化。 我怎樣才能做到這一點?
更新:-
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js" type="text/javascript"></script>
<script>
{% load static %}
async function load_model(){
const model = await tf.loadGraphModel("{% static 'disease_detection/tfjs_model_2/model.json' %}");
console.log(model);
return model;
}
function loadImage(src){
return new Promise((resolve, reject) => {
const img = new Image();
img.src = src;
img.onload = () => resolve(tf.browser.fromPixels(img, 3));
img.onerror = (err) => reject(err);
});
}
function resizeImage(image) {
return tf.image.resizeBilinear(image, [224, 224]).sub([0.485, 0.456, 0.406]).div([0.229, 0.224, 0.225]);
}
function batchImage(image) {
const batchedImage = image.expandDims(0);
//const batchedImage = image;
return batchedImage.toFloat();
}
function loadAndProcessImage(image) {
//const croppedImage = cropImage(image);
const resizedImage = resizeImage(image);
const batchedImage = batchImage(resizedImage);
return batchedImage;
}
let model = load_model();
model.then(function (model_param){
loadImage('{% static 'disease_detection/COVID-19 (97).png' %}').then(img=>{
let imge = loadAndProcessImage(img);
const t4d = tf.tensor4d(Array.from(imge.dataSync()),[1,3,224,224])
console.log(t4d.dataSync());
let prediction = model_param.predict(t4d);
let v = prediction.argMax().dataSync()[0]
console.log(v)
})
})
我嘗試了這段代碼,但它沒有正確規范化圖像。
img = tf.image.resizeBilinear(img, [224, 224]).div(tf.scalar(255))
img = tf.cast(img, dtype = 'float32');
完整的function如下——
function imgTransform(img){
img = tf.image.resizeBilinear(img, [224, 224]).div(tf.scalar(255))
img = tf.cast(img, dtype = 'float32');
/*mean of natural image*/
let meanRgb = { red : 0.485, green: 0.456, blue: 0.406 }
/* standard deviation of natural image*/
let stdRgb = { red: 0.229, green: 0.224, blue: 0.225 }
let indices = [
tf.tensor1d([0], "int32"),
tf.tensor1d([1], "int32"),
tf.tensor1d([2], "int32")
];
/* sperating tensor channelwise and applyin normalization to each chanel seperately */
let centeredRgb = {
red: tf.gather(img,indices[0],2)
.sub(tf.scalar(meanRgb.red))
.div(tf.scalar(stdRgb.red))
.reshape([224,224]),
green: tf.gather(img,indices[1],2)
.sub(tf.scalar(meanRgb.green))
.div(tf.scalar(stdRgb.green))
.reshape([224,224]),
blue: tf.gather(img,indices[2],2)
.sub(tf.scalar(meanRgb.blue))
.div(tf.scalar(stdRgb.blue))
.reshape([224,224]),
}
/* combining seperate normalized channels*/
let processedImg = tf.stack([
centeredRgb.red, centeredRgb.green, centeredRgb.blue
]).expandDims();
return processedImg;
}
盡管我對 pytorch 文檔不太熟悉,但快速瀏覽一下它就會發現Normalize
的第一個參數是數據集的平均值,第二個參數是標准差。
要在 tensorflow.js 中使用這兩個參數進行規范化,可以使用以下內容
tensor.sub([0.485, 0.456, 0.406]).div([0.229, 0.224, 0.225])
但是張量值應該在 0 到 1 的范圍內,在調整大小操作后將其除為 255。 整個撰寫操作將如下所示
tf.image.resizeBilinear(image, [224, 224]).div(255)
.sub([0.485, 0.456, 0.406])
.div([0.229, 0.224, 0.225]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.