簡體   English   中英

始終使用 TensorflowJS 獲得相同的預測

[英]Always getting same prediction with TensorflowJS

我有一個問題,我總是得到與 TensorflowJS 相同的預測。

這是我的代碼:

HTML: <img id="img" src="normal_15.jpeg" style="width:300px;height:300px"></img>

JS:

async function run(){
        const MODEL_URL = 'http://localhost/pneumonia_detection/model.json';
        const model = await tf.loadLayersModel(MODEL_URL);
        console.log(model.summary());
        const image = document.getElementById('img');
        var img = tf.browser.fromPixels(image, 3).resizeNearestNeighbor([180,180]).toFloat();
        const offset = tf.scalar(255.0);
        var normalized  = img.div(offset);
        console.log(img[100,100,2]);
        const axis = 0;
        normalized = img.expandDims(axis);
        console.log(normalized.shape);
        prediction = model.predict(normalized);
        console.log(prediction.dataSync());
        var pIndex = tf.argMax(prediction, 1).dataSync();
        var classNames = ["Normal", "Pneumonia"];
        alert(classNames[pIndex]);
    }

如果重要的話,在 Python 中,在將圖像輸入 model 之前,model 進行預測,我將圖像除以 255.0。

我嘗試打印 img 的值並且我得到未定義,所以我似乎沒有正確地從照片中獲取像素。 如何成功獲取像素?

我在 JS 中做正確的處理嗎?

我解決了問題。

我不知道我怎么沒有注意到我寫了normalized = img.expandDims(axis); .

我不得不寫normalized.expandDims(axis);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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