簡體   English   中英

如何標准化 tensorflow.js 中的圖像?

[英]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)
        })
       })

我嘗試了這段代碼,但它沒有正確規范化圖像。

  • torchvision.transforms.ToTensor()將 0 到 255 范圍內的 PIL Image 或 numpy 數組轉換為 0.0 到 1.0 范圍內的浮點張量 os 形狀(通道 x 高度 x 寬度)。 要在 0.0 到 1.0 的范圍內轉換,它將張量的每個元素除以 255。因此,在 tensorflowJS 中執行相同的操作,我按如下方式執行 -
img = tf.image.resizeBilinear(img, [224, 224]).div(tf.scalar(255))
img = tf.cast(img, dtype = 'float32');
  • torchvision.transforms.Normalize()用均值和標准差對張量圖像進行歸一化。 給定 n 個通道的均值: (mean[1],...,mean[n]) 和 std: (std[1],..,std[n]) ,此變換將對輸入張量的每個通道進行歸一化,即, 輸出[通道] = (輸入[通道] - 平均[通道]) / 標准[通道]。 我在 tensorflowJS 中沒有找到任何這樣的 function。 所以,我分別對每個通道進行歸一化並再次組合它們。

完整的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM