Closed retrocold closed 2 years ago
What I've done so far
const nj = require("numjs");
const tf = require("@tensorflow/tfjs");
const format_y = (y) => y.map((x) => String.fromCharCode(parseInt(x))).join("");
const predict = async (path) => {
let im = nj.images.read(path);
im = nj.array(im).divide(255.0);
im = nj.array(im);
let y_pred = model.predict(nj.array([im]));
y_pred = tf.argMax(y_pred, -1);
console.log(`Prediction: ${format_y(y_pred[0])}`);
};
The Layers:
So far I managed to do this, but the result is unexpected
const predict = async (path) => {
let im = nj.images.read(path);
let arr = [...im.selection.data].map((el) =>
parseFloat((el / 255.0).toFixed(8))
);
let data = tf.tensor(arr, [1, 60, 160, 3]);
data.print();
let y_pred = model.predict(data);
y_pred = tf.argMax(y_pred, -1);
console.log(y_pred);
};
Got something like this:
Tensor {
kept: false,
isDisposedInternal: false,
shape: [ 1, 6 ],
dtype: 'int32',
size: 6,
strides: [ 6 ],
dataId: { id: 44 },
id: 51,
rankType: '2',
scopeId: 53
}
Nevermind, I finally solved it.
The solution:
const tf = require("@tensorflow/tfjs");
const nj = require("numjs");
let model;
const format_y = (y) => y.map((x) => String.fromCharCode(parseInt(x))).join("");
const predict = async (path) => {
let im = nj.images.read(path);
let arr = [...im.selection.data].map((el) => parseFloat((el / 255.0).toFixed(8)) );
let data = tf.tensor(arr, [1, 60, 160, 3]);
let y_pred = model.predict(data);
y_pred = tf.argMax(y_pred, -1);
let result = [...y_pred.dataSync()];
console.log(`Predictioni: ${format_y(result)}`);
};
const load = async () => {
model = await tf.loadLayersModel(
"url_to_model.json"
);
console.log(model.summary());
const result = await predict("./captcha/1.jpg");
};
load();
Im struggling to input the image in tensorflowjs, it always throw error
ValueError: Error when checking model : the Array of Tensors that you are passing to your model is not the size the the model expected model expected. Expected to see 1 Tensor(s), but instead got 0 Tensors(s).