如何检查tfjs模型是否正确加载到浏览器

时间:2019-11-25 04:32:43

标签: tensorflow.js

我尝试进行文本分类,通过

将模型加载回浏览器
async function loadFile(){    const jsonUpload = document.getElementById('json-upload');    
model = await tf.loadLayersModel(tf.io.browserFiles([jsonUpload.files[0], weightsUpload.files[0]]));
model.summary();

在控制台中有完整的摘要

Layer (type)                 Output shape              Param #   
    tfjs@latest:2 =================================================================
    tfjs@latest:2 embedding_Embedding1 (Embedd [null,15,50]              1009200   
    tfjs@latest:2 _________________________________________________________________
    tfjs@latest:2 conv1d_Conv1D1 (Conv1D)      [null,15,100]             15100     
    tfjs@latest:2 _________________________________________________________________
    tfjs@latest:2 max_pooling1d_MaxPooling1D1  [null,7,100]              0         
    tfjs@latest:2 _________________________________________________________________
    tfjs@latest:2 conv1d_Conv1D2 (Conv1D)      [null,7,100]              40100     
    tfjs@latest:2 _________________________________________________________________
    tfjs@latest:2 max_pooling1d_MaxPooling1D2  [null,3,100]              0         
    tfjs@latest:2 _________________________________________________________________
....
...
..
dense_Dense26 (Dense)        [null,2]                  42        
tfjs@latest:2 =================================================================
tfjs@latest:2 Total params: 1702322
tfjs@latest:2 Trainable params: 1702322
tfjs@latest:2 Non-trainable params: 0
tfjs@latest:2 
  1. 还有其他方法可以检查模型是否正确加载吗?

2 个答案:

答案 0 :(得分:0)

tf.loadLayersModel仅在成功加载模型后才会返回模型。否则,将引发错误并需要捕获。 万一无法加载模型,使用if语句将失败。 这是检查模型是否成功加载的方法。

try {
    model = await tf.loadLayersModel(tf.io.browserFiles([jsonUpload.files[0], weightsUpload.files[0]]))
} catch(e) {
   console.log("the model could not be loaded")
}

答案 1 :(得分:0)

您可以使用官方的Tensor Flow Vis API来检查模型是否已加载。

将此添加到您的html代码中:

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis@1.0.2/dist/tfjs-vis.umd.min.js"></script>

并在您的js文件中:

const model = tf.sequential();
model.add(tf.layers.dense({ units: 1, inputShape: [1], useBias: true }));

tfvis.show.modelSummary({ name: 'Model Summary' }, model);

打开浏览器,您将看到:

enter image description here