【问题标题】:How To Save Model image classifier in TensorflowJS如何在 TensorflowJS 中保存模型图像分类器
【发布时间】:2020-10-05 06:32:09
【问题描述】:

我用TensorFlow.js在浏览器中创建了一个图像分类器,代码运行良好,但是当页面重新加载时,代码需要再次训练数据(需要时间),所以不用再次训练数据,我想保存模型并加载它们。我正在尝试使用此代码保存模型

  await model.save('anime/saved-model');

但它不起作用,顺便说一句,这是我的代码

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jotaro atau Giorno</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/knn-classifier"></script>
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
</head>
<body>
<h1>Test Image Giorno and Jotaro</h1>
<input type='file' onchange="readURL(this);" />
<img id="image_baru"  src="#" alt="your image" />
<button id="tombolPrediksi">Prediksi</button>
<h3>output in the console</h3>
</body>
</html>

Javascript

 let mobileneModule;
 let ClassifierKNN;

 function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function (e) {
      $('#image_baru')
      .attr('src', e.target.result)
      .width(300)
      .height(300);};
    reader.readAsDataURL(input.files[0]);}
    }

 const initScript = async function(){
  ClassifierKNN = knnClassifier.create();
  mobileneModule = await mobilenet.load();
    
    const jotaroExample = ()=>{
      for(let i=1; i<=3;i++){
      const im = new Image(300,300);
      im.src = 'anime/jotaro/'+i+'.jpg';
      im.onload = ()=>{
      let trainingImageJotaro = tf.browser.fromPixels(im);
      let predJotaro = mobileneModule.infer(trainingImageJotaro,'conv_preds');
      ClassifierKNN.addExample(predJotaro,"Jotaro Kujo");
      console.log("Giorno ok")
      }
      im.onload();
    }}

    const giornoExample = ()=>{
      for(let i=1; i<=3;i++){
      const im2 = new Image(300,300);
      im2.src = 'anime/giorno/'+i+'.jpg';
      im2.onload = ()=>{
      let trainingImageGiorno = tf.browser.fromPixels(im2);
      let predGiorno = mobileneModule.infer(trainingImageGiorno,'conv_preds');
      ClassifierKNN.addExample(predGiorno,"Giorno Giovanna");
      console.log("Giorno Oke")
    }
    im2.onload();
    }}

    await jotaroExample();
    await giornoExample();
    //save model
    await model.save('anime/saved-model');
    }

    initScript();

    const prediksiGambar = async function(){
      let imgX = document.getElementById('image_baru');
      const tensorX = tf.browser.fromPixels(imgX);
      const logitsX = mobileneModule.infer(tensorX,'conv_preds');
      let result = await ClassifierKNN.predictClass(logitsX);
      console.log('hasil prediksi:');
      console.log(result);
    }
     document.getElementById("tombolPrediksi").addEventListener("click",prediksiGambar);

我希望有人可以帮助我:)

【问题讨论】:

  • 您要将它们保存到本地存储还是您的设备?
  • 我想保存在本地存储中,这样我可以加载这些模型,而不是在重新加载页面时总是再次训练,我相信这种方式更有效
  • 我添加了答案
  • 谢谢我的朋友,我会试试的:D

标签: javascript tensorflow machine-learning tensorflow.js


【解决方案1】:
  1. 用于保存到本地设备并生成 2 个文件,model.json 文件和带有权重的二进制文件。
await model.save('downloads://my-model');
  1. 用于将模型保存在浏览器本地存储中,即使页面刷新也会保留。但请注意,每个浏览器的存储量都有限制。
// saves under the my-model in the browsers storage 
await model.save('localstorage://my-model');

More info from the documentation

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 2019-12-23
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2016-07-28
    相关资源
    最近更新 更多