Reputation: 21
hello I'm new to this topic of ml. I trained a model in python which classifies the images in 2 classes as 'santa' and 'not santa' and it is predicting correctly. I converted this model to tensorflow.js as I need to use it in my website for classifying uploaded images but it is not classifying images correctly as in the python model. model.predict() returns 2 probabilities which of one is higher returns that class. I feel the problem lies in the preprocessing part in the javascript for testing from the model. I have attached the code below.
below is the code snippet for testing in python
image = cv2.imread(args["image"])
orig = image.copy()
image = cv2.resize(image, (28, 28))
image = image.astype("float") / 255.0
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
print("[INFO] loading network...")
model = load_model(args["model"])
x= model.predict(image)
print(x)
below is javascript code for testing
function preprocess(img)
{
let tensor = tf.browser.fromPixels(img)
const resized = tf.image.resizeBilinear(tensor, [28, 28]).toFloat()
const offset = tf.scalar(255.0);
const normalized = tf.scalar(1.0).sub(resized.div(offset));
const batched = normalized.expandDims(0)
return batched
}
function predict(imgData) {
var class_names = ['santa','not santa']
var pred = model.predict(preprocess(imgData)).dataSync()
console.log(pred)
const idx = tf.argMax(pred);
var indices = findIndicesOfMax(pred, 1)
console.log(indices)
var probs = findTopValues(pred, 1)
var names = getClassNames(indices)
document.getElementById("Result").innerHTML = names
console.log(names);
console.log(document.getElementById("Result"));
}
please help how can I solve this problem. for a sample images the python model returns the value as follows [[0.9940202 0.00597982]] [python output]1
and for the same image tensorflowjs model returns value as follows Float32Array [ 0.24975205957889557, 0.7502480149269104 ] [tensorflowjs]2
Upvotes: 1
Views: 1150
Reputation: 18401
There is a slight difference in the way the image is preprocessed in Python and in JavaScript. With this line
image = image.astype("float") / 255.0
You're only dividing the image pixel values by 255. A common processing is to substract 127 before the division operation:
image = (image.astype("float") -127) / 127
In js
const normalized = resized.sub(offset).div(offset));
Upvotes: 3