1

I've trained my model in python and then converted the model so I could use it on my JS website. I have a brain tumor dataset, it has 4 classes:

  1. Glioma
  2. Menengioma
  3. No tumor
  4. Pituitary

So far I've done this:

<script>
    async function LoadModels(){  
               model = undefined;
               model = await tf.loadLayersModel("http://127.0.0.1:5500/modelsBrain/modelBrain.json");
               const pr = document.getElementById('photo');
               const image = tf.browser.fromPixels(pr);
               const image2 = tf.reshape(image, [1,200,200,3]);
               const prediction = model.predict(image2);
               alert(prediction.dataSync());
           }
           LoadModels();
</script>

// just predicting one photo

Which gives me an alert like [x, y, z, k]. And also I've created a .js file with classes target:

TARGET_CLASSES_BRAIN = {
    0: "Glioma",
    1: "Menengioma",
    2: "No Tumor",
    3: "Pituitary"
} 

So, the thing I want to do is to have an output like:

Glioma: 0.001

Menengioma: 0.0004

No tumor: 0.99

Pituitary: 0.0086

I want to include both class names and confidence values. I'm new to the TensorflowJS, could you please direct me? Thank you so much in advance.

UPDATE: I also used a softmax function:

Dense(4, activation='softmax')
Elizabeth Grant
  • 149
  • 1
  • 1
  • 12

1 Answers1

0

Here is a snippet that can help to get the label of a prediction

  const label = ['Glioma', 'Menengioma', 'No Tumor', 'Pituitary',]  
  // the code for prediction
  const prediction = model.predict(image2);
  const listIndexes = prediction.argMax(1).dataSync(); // get the class of highest probability
  labelsPred = Array.from(listIndexes ).map(index => label[index])
  console.log(labelsPred)
edkeveked
  • 17,989
  • 10
  • 55
  • 93