JackonYang / captcha-tensorflow

Image Captcha Solving Using TensorFlow and CNN Model. Accuracy 90%+
MIT License
998 stars 272 forks source link

How do I test the model in tensorflow.js (node-js)? #42

Closed retrocold closed 2 years ago

retrocold commented 2 years ago
def format_y(y):
    return ''.join(map(lambda x: chr(int(x)), y))

def predict(image_path):
    im = Image.open(image_path)
    # im = im.resize((H, W))
    im = np.array(im) / 255.0
    im = np.array(im)

    y_pred = model.predict(np.array([im]))
    y_pred = tf.math.argmax(y_pred, axis=-1)

    print('predict: %s' % format_y(y_pred[0]))
    plt.imshow(im)

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).

retrocold commented 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])}`);
};
retrocold commented 2 years ago

The Layers:

image

retrocold commented 2 years ago

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
}
retrocold commented 2 years ago

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();