【问题标题】:TensorFlowJS model converted from GoogleColab has different probabilities values in prediction result (using angular with tfjs)从 Google Colab 转换的 TensorFlow JS 模型在预测结果中具有不同的概率值(使用 Angular 和 tfjs)
【发布时间】:2021-09-13 19:10:15
【问题描述】:

我是 TensorFlowJS 的新手。我使用 Google Colab 使用 TensorFlow Lite Model Maker 创建和训练模型,将其转换为 TFJS,然后在 Angular 应用程序中使用它。

在 colab 中,模型达到了很高的准确率。我已经使用以下方法保存了模型:

model.export(export_dir='.', export_format=ExportFormat.SAVED_MODEL);

然后我用

tfjs.converters.convert_tf_saved_model('saved_model','tfjsconvertido');

为了获得tfjs。在角度,我正在使用:

async fazerPrevisao(modelo: tf.GraphModel){    
const batched = tf.tidy(() => {      
    const im = new Image();
    im.src = this.foto.webviewPath;
    im.height = 224;
    im.width = 224;        
    const imagem = tf.cast(tf.browser.fromPixels(im).reshape([-1,224,224,3]),'float32');        
    
  this.prediction = modelo.predict(imagem);
  this.prediction.print();
 });
}

角度结果与 Colab 相差太大。在某些情况下,结果标签不正确,甚至得到正确的标签,概率的值也完全不同。会是什么?

提前致谢。

【问题讨论】:

    标签: angular prediction tensorflow.js


    【解决方案1】:
    im.height = 224;
    im.width = 224;  
    

    图像可能无法正确调整大小。 tf.image.resizeBilinear 可用于调整大小。

    const resized = tf.image.resizeBilinear(tf.browser.fromPixels(im), [224, 224]).expandDims()
    

    也有可能是图片在使用的时候没有完全加载

    function load(url){
      return new Promise((resolve, reject) => {
        const im = new Image()
            im.crossOrigin = 'anonymous'
            im.src = 'url'
            im.onload = () => {
              resolve(im)
            }
       })
    }
    
    const image = await load(url)
    let tensor = await tf.browser.fromPixels(image)
    
    const resized = tf.image.resizeBilinear (tensor, [224, 224]).expandDims()
    

    调整后的张量现在可以用于预测

    【讨论】:

    • 我试过用这个但是没用。实际上,如果我更改高度和宽度的值,则预测不起作用,这表明图像已加载。此外,当我尝试使用 tf.image.resizeBilinear 时,它返回一个 tf.Tensor3D 而 GraphModel.predict 需要一个 tf.Tensor。
    • 第一种情况,将高宽改为100,例如报错信息为:“The implicit shape can't be a fraction number. Got 30000 / 150528”。当我将 224 放入两者时,预测有效。当我使用 tf.image.resizeBilinear 时,消息错误是:“model.execute(dict) 中提供的 dict['input_1'] 的形状必须为 [-1,224,224,3],但为 [224,224,3]”
    • 您需要使用tf.expandDims 扩展第一个维度或使用tf.reshape 重塑张量。查看更新的答案
    • 我已经用 tf.expandDims 改过代码并且工作了,但是概率值还是错误的。我会继续调查。非常感谢您迄今为止的所有关注。
    • 因此,在搜索了其他替代方案后,我发现了 teachablemachine.withgoogle.com/train/image 以及我训练和导出的模型以角度工作。需要导入@teachablemachine/image 并修改代码,但现在更简单了,效果还不错。
    【解决方案2】:

    我找到了解决更改导出模型问题的替代方法。我找到了https://teachablemachine.withgoogle.com/train/image,在那里我训练了模型并将其导出到 TFJS。在角度,我已经安装了@teachablemachine/image,然后更改了代码以使用这种方法:

    ...
    
    import * as tmImage from '@teachablemachine/image';
    
    ...
    model: tmImage.CustomMobileNet;
    predictions: any;
    
    const modelURL = 'URL/model.json';
    const metadataURL = 'URL/metadata.json';
    
    this.model = await tmImage.load(modelURL, metadataURL);
    
    await this.fazerPrevisao(this.model);
    
    async fazerPrevisao(modelo: tmImage.CustomMobileNet){
    const im = new Image();
    im.src = PATHOFTHEPHOTO;
    im.height = 224;
    im.width = 224;              
    this.predictions = await this.model.predict(im,true);
    console.log('Previsão: ',this.predictions);
    }
    

    现在的概率非常好。感谢@edkeveked 的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 2019-11-07
      相关资源
      最近更新 更多