如何在tensorflow.js上加载/重新训练/保存ssd_inception_v2_coco?

时间:2019-02-27 03:01:34

标签: python tensorflow machine-learning tensorflow.js

ML / Tensorflow初学者。

是否可以将这些已经训练过的模型中的任何一个加载到tfjs上并在那里进行重新训练,然后导出到Downloads或Tensorflow python是唯一的方法?

我看到此tutorial中针对Tensorflow Python对该过程进行了很好的描述和记录,但是不幸的是,我找不到任何文档/教程来使用tfjs在浏览器上重新训练对象检测模型(图像分类是,则没有检测到对象。)

我知道如何使用npm加载coco-ssd模型,然后可能甚至触发将其保存为下载内容,但是怎么办:

  • 配置文件(需要修改它,因为我只想拥有一个类,而不是90)
  • 带注释的图像(.jpg,.xml和.csv)
  • labels.pbtxt
  • .record文件

是否有任何方法可以重新训练ssd模型(例如ssd_inception_v2_coco),而我没有找到正确的Google关键字,或者只是在当前框架状态下无法实现?

1 个答案:

答案 0 :(得分:0)

您可以通过将coco-ssd模型用作特征提取器来使用转移学习。可以在here中看到转移学习的示例。

这里是一个模型,该模型使用特征提取器作为新顺序模型的输入来提取特征。

const loadModel = async () => {
  const loadedModel = await tf.loadModel(MODEL_URL)
  console.log(loadedModel)
  // take whatever layer except last output
  loadedModel.layers.forEach(layer => console.log(layer.name))
  const layer = loadedModel.getLayer(LAYER_NAME)
  return tf.model({ inputs: loadedModel.inputs, outputs: layer.output });
}
loadModel().then(featureExtractor => {
  model = tf.sequential({
    layers: [
      // Flattens the input to a vector so we can use it in a dense layer. While
      // technically a layer, this only performs a reshape (and has no training
      // parameters).
      // slice so as not to take the batch size
      tf.layers.flatten(
        { inputShape: featureExtractor.outputs[0].shape.slice(1) }),
      // add all the layers of the model to train
      tf.layers.dense({
        units: UNITS,
        activation: 'relu',
        kernelInitializer: 'varianceScaling',
        useBias: true
      }),
      // Last Layer. The number of units of the last layer should correspond
      // to the number of classes to predict.
      tf.layers.dense({
        units: NUM_CLASSES,
        kernelInitializer: 'varianceScaling',
        useBias: false,
        activation: 'softmax'
      })
    ]
  });
})

要检测90种coco-ssd中的单个对象,只需在预测coco-ssd时使用条件测试即可。

const image = document.getElementById(id)

cocoSsd.load()
  .then(model => model.detect(image))
  .then(prediction => {
if (prediction.class === OBJECT_DETECTED) {
  // display it the bbox to the user}
})

如果该类在coco-ssd中不存在,则需要构建一个检测器。

相关问题