Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

javascript - 如何在Node.js(tensorflow.js)中训练模型?(How to train a model in nodejs (tensorflow.js)?)

I want to make a image classifier, but I don't know python.

(我想做一个图像分类器,但是我不懂python。)

Tensorflow.js works with javascript, which I am familiar with.

(Tensorflow.js使用我熟悉的javascript。)

Can models be trained with it and what would be the steps to do so?

(可以用它训练模型吗?要这样做的步骤是什么?)

Frankly I have no clue where to start.

(坦白说,我不知道从哪里开始。)

The only thing I figured out is how to load "mobilenet", which apparently is a set of pre-trained models, and classify images with it:

(我唯一想到的是如何加载“移动网络”,该网络显然是一组经过预先训练的模型,并使用该模型对图像进行分类:)

const tf = require('@tensorflow/tfjs'),
      mobilenet = require('@tensorflow-models/mobilenet'),
      tfnode = require('@tensorflow/tfjs-node'),
      fs = require('fs-extra');

const imageBuffer = await fs.readFile(......),
      tfimage = tfnode.node.decodeImage(imageBuffer),
      mobilenetModel = await mobilenet.load();  

const results = await mobilenetModel.classify(tfimage);

which works, but it's no use to me because I want to train my own model using my images with labels that I create.

(可以,但是对我没有用,因为我想使用带有创建的标签的图像来训练自己的模型。)

=======================

(=======================)

Say I have a bunch of images and labels.

(说我有一堆图像和标签。)

How do I use them to train a model?

(如何使用它们训练模型?)

const myData = JSON.parse(await fs.readFile('files.json'));

for(const data of myData){
  const image = await fs.readFile(data.imagePath),
        labels = data.labels;

  // how to train, where to pass image and labels ?

}
  ask by Alex translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all, the images needs to be converted to tensors.

(首先,图像需要转换为张量。)

The first approach would be to create a tensor containing all the features (respectively a tensor containing all the labels).

(第一种方法是创建包含所有特征的张量(分别包含所有标签的张量)。)

This should the way to go only if the dataset contains few images.

(仅当数据集包含少量图像时,才应采用这种方式。)

  const imageBuffer = await fs.readFile(feature_file);
  tensorFeature = tfnode.node.decodeImage(imageBuffer) // create a tensor for the image

  // create an array of all the features
  // by iterating over all the images
  tensorFeatures = tf.stack([tensorFeature, tensorFeature2, tensorFeature3])

The labels would be an array indicating the type of each image

(标签将是一个数组,指示每个图像的类型)

 labelArray = [0, 1, 2] // maybe 0 for dog, 1 for cat and 2 for birds

One needs now to create a hot encoding of the labels

(现在需要创建标签的热编码)

 tensorLabels = tf.oneHot(tf.tensor1d(labelArray, 'int32'), 3);

Once there is the tensors, one would need to create the model for training.

(一旦有了张量,就需要创建训练模型。)

Here is a simple model.

(这是一个简单的模型。)

const model = tf.sequential();
model.add(tf.layers.conv2d({
  inputShape: [height, width, numberOfChannels], // numberOfChannels = 3 for colorful images and one otherwise
  filters: 32,
  kernelSize: 3,
  activation: 'relu',
}));
model.add(tf.layers.dense({units: 3, activation: 'softmax'}));

Then the model can be trained

(然后可以训练模型)

model.fit(tensorFeatures, tensorLabels)

If the dataset contains a lot of images, one would need to create a tfDataset instead.

(如果数据集包含很多图像,则需要创建一个tfDataset。)

This answer discusses why.

(这个答案讨论了为什么。)

const genFeatureTensor = image => {
      const imageBuffer = await fs.readFile(feature_file);
      return tfnode.node.decodeImage(imageBuffer)
}

const labelArray = indice => Array.from({length: numberOfClasses}, (_, k) => k === indice ? 1 : 0)

function* dataGenerator() {
  const numElements = numberOfImages;
  let index = 0;
  while (index < numFeatures) {
    const feature = genFeatureTensor ;
    const label = tf.tensor1d(labelArray(classImageIndex))
    index++;
    yield {xs: feature, ys: label};
  }
}

const ds = tf.data.generator(dataGenerator);

And use model.fitDataset(ds) to train the model

(并使用model.fitDataset(ds)训练模型)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...