【问题标题】:How to save loaded model in localstorage or IndexedDB如何在 localstorage 或 IndexedDB 中保存加载的模型
【发布时间】:2021-08-21 01:38:44
【问题描述】:

我正在使用TFJS's samle program。我想将加载的模型保存在 localstorage 或 IndexedDB 中。所以我写了这个程序。

<!-- Load TensorFlow.js. This is required to use coco-ssd model. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"> </script>
<!-- Load the coco-ssd model. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"> </script>

<!-- Replace this with your image. Make sure CORS settings allow reading the image! -->
<img id="img" src="cat.jpg"/>

<!-- Place your code in the script tag below. You can also use an external .js file -->
<script>
  // Notice there is no 'import' statement. 'cocoSsd' and 'tf' is
  // available on the index-page because of the script tag above.

  const img = document.getElementById('img');

  // Load the model.
  cocoSsd.load().then(model => {

    // save to localstorage. <-- My code
    model.save('localstorage://test') 

    // detect objects in the image.
    model.detect(img).then(predictions => {
      console.log('Predictions: ', predictions);
    });
  });
</script>

但是model.save('localstorage://test') 得到的错误是Uncaught (in promise) TypeError: model.save is not a function

如何将模型保存在 localstorage 或 IndexedDB 中?

【问题讨论】:

    标签: javascript tensorflow tensorflow.js


    【解决方案1】:

    让我们通过检查模型上存在的方法来检查你是否真的可以保存模型。

    function getMethods(o) {
      return Object.getOwnPropertyNames(Object.getPrototypeOf(o))
        .filter(m => 'function' === typeof o[m])
    }
    
    cocoSsd.load().then(model => {
      console.log(getMethods(model));
    })
    

    输出:

    ["constructor","getPrefix","load","infer","buildDetectedObjects","calculateMaxScores","detect","dispose"]
    

    查看方法,错误实际上是正确的,save 方法在 cocoSsd 模型上不存在。所以你不能直接将模型保存到本地存储或数据库中。

    不用担心,您可以从TensorFlow 2 Detection Model Zoo 的所有可用对象检测模型列表中下载原始模型。它们都在同一个 coco 数据集上进行了训练,因此您可以选择其中任何一个。

    例如我选择了centernet_mobilenetv2_fpn_od

    下载文件并解压将产生以下内容:

    |-- checkpoint
    |   |-- checkpoint
    |   |-- ckpt-301.data-00000-of-00001
    |   `-- ckpt-301.index
    |-- label_map.txt
    |-- model.tflite
    |-- pipeline.config
    `-- saved_model
        |-- assets
        |-- saved_model.pb
        `-- variables
            |-- variables.data-00000-of-00001
            `-- variables.index
    

    您需要的是 saved_model.pb,您需要将其转换为 Tensorflow.js 可用的格式。为此,您可以使用在终端中运行的tensorflowjs_wizard

    >> tensorflowjs_wizard                                                     
    Welcome to TensorFlow.js Converter.
    ? Please provide the path of model file or the directory that contains model files. 
    If you are converting TFHub module please provide the URL.  .
    ? What is your input model format? (auto-detected format is marked with *)  Tensorflow Saved Model *
    ? What is tags for the saved model?  serve
    ? What is signature name of the model?  signature name: serving_default
    ? Do you want to compress the model? (this will decrease the model precision.)  No compression (Higher accuracy)
    ? Please enter shard size (in bytes) of the weight files?  4194304
    ? Do you want to skip op validation? 
    This will allow conversion of unsupported ops, 
    you can implement them as custom ops in tfjs-converter.  No
    ? Do you want to strip debug ops? 
    This will improve model execution performance.  Yes
    ? Do you want to enable Control Flow V2 ops? 
    This will improve branch and loop execution performance.  Yes
    ? Do you want to provide metadata? 
    Provide your own metadata in the form: 
    metadata_key:path/metadata.json 
    Separate multiple metadata by comma.  
    ? Which directory do you want to save the converted model in?  /Users/yravindranath/Downloads/centernet_mobilenetv2_fpn_od/saved_model/model
    converter command generated:
    tensorflowjs_converter --control_flow_v2=True --input_format=tf_saved_model --metadata= --saved_model_tags=serve --signature_name=serving_default --strip_debug_ops=True --weight_shard_size_bytes=4194304 . /Users/yravindranath/Downloads/centernet_mobilenetv2_fpn_od/saved_model/model
    
    

    这将在指定的保存路径中产生以下内容:

    ./
    |-- group1-shard1of3.bin
    |-- group1-shard2of3.bin
    |-- group1-shard3of3.bin
    `-- model.json
    

    如果您想直接从本地存储中加载模型,您可以使用此函数将模型加载到您的 Javascript 应用程序中:

    const tf = require('@tensorflow/tfjs');
    const tfnode = require('@tensorflow/tfjs-node');
    
    async function loadModel(){
    
        const model = await loadGraphModel("path to model.json");
        // You can even save it to a github repo and load it in using a GET request like so:
        // const model = await loadGraphModel("https://raw.githubusercontent.com/hugozanini/TFJS-object-detection/master/models/web_model/model.json");
        console.log("Model loaded")
    }
    
    loadModel();
    

    【讨论】:

    • 抱歉回复晚了。我通过tensorflowjs_wiserd 创建了 tensorflow.js 模型。然后我通过 loadModel() 加载模型。 getMethods() 返回与 cocoSsd.loaded 模型的方法不同。我应该回到tensorflowjs_wiserd,不是吗?
    猜你喜欢
    • 2013-12-28
    • 2013-12-17
    • 2023-02-07
    • 2020-04-19
    • 2020-02-29
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多