简体   繁体   English

Tensorflow.js:张量应该有 4096 个值但有 12288

[英]Tensorflow.js: the tensor should have 4096 values but has 12288

I try to get a newb mnist tutorial to work in an Ionic Angular App.我尝试获取一个 newb mnist 教程以在 Ionic Angular 应用程序中工作。 I converted a trained mnist model with the tensorflow js converter.我使用 tensorflow js 转换器转换了训练有素的 mnist model。 The predict function throws this error:预测 function 抛出此错误:

ERROR Error: Uncaught (in promise): Error: Based on the provided shape, [1,28,28,1], the tensor should have 784 values but has 2352错误错误:未捕获(承诺):错误:基于提供的形状,[1,28,28,1],张量应该有 784 个值,但有 2352

I searched for the error at google and I found as an answer, that the.bin file should be corrupt.我在 google 上搜索了错误,我发现作为答案,.bin 文件应该已损坏。 Now I'm totally confused how can the.bin file be corrupt, when I generated it myself.现在,当我自己生成.bin 文件时,我完全感到困惑。

Code:代码:

  async loadModel() {
  
    this.tfModel = await tf.loadLayersModel('/assets/models/model.json');
    console.log(this.tfModel.summary());
 }

 async predict() {
  const pred = await tf.tidy(() => {

    
    let img = tf.browser.fromPixels(this.canvasEl);
    console.log(img);

    const smalImg = tf.image.resizeBilinear(img, [28, 28]);
    const resized = tf.cast(smalImg, 'float32');
    const t4d = tf.tensor4d(Array.from(resized.dataSync()),[1,28,28,1])

    const output = this.tfModel.predict(t4d) as any;

    this.predictions = Array.from(output.dataSync());

    for (let i = 0; i < this.predictions.length; i++) {
      if (this.predictions[i] == "1") {
        this.textPrediction = i.toString();
      }
    }
    if (this.textPrediction == "") {
      this.textPrediction = ":(";
    }
  });
}

and the Model Summary from the console:以及来自控制台的 Model 摘要:

=================================================================
⚡️  [log] - conv2d_input (InputLayer)    [null,28,28,1]            0         
⚡️  [log] - _________________________________________________________________
⚡️  [log] - conv2d (Conv2D)              [null,28,28,32]           320       
⚡️  [log] - _________________________________________________________________
⚡️  [log] - activation (Activation)      [null,28,28,32]           0         
⚡️  [log] - _________________________________________________________________
⚡️  [log] - conv2d_1 (Conv2D)            [null,28,28,32]           25632     
⚡️  [log] - _________________________________________________________________
⚡️  [log] - activation_1 (Activation)    [null,28,28,32]           0         
⚡️  [log] - _________________________________________________________________
⚡️  [log] - max_pooling2d (MaxPooling2D) [null,14,14,32]           0         
⚡️  [log] - _________________________________________________________________
⚡️  [log] - conv2d_2 (Conv2D)            [null,14,14,64]           51264     
⚡️  [log] - _________________________________________________________________
⚡️  [log] - activation_2 (Activation)    [null,14,14,64]           0         
⚡️  [log] - _________________________________________________________________
⚡️  [log] - conv2d_3 (Conv2D)            [null,14,14,64]           200768    
⚡️  [log] - _________________________________________________________________
⚡️  [log] - activation_3 (Activation)    [null,14,14,64]           0         
⚡️  [log] - _________________________________________________________________
⚡️  [log] - max_pooling2d_1 (MaxPooling2 [null,7,7,64]             0         
⚡️  [log] - _________________________________________________________________
⚡️  [log] - flatten (Flatten)            [null,3136]               0         
⚡️  [log] - _________________________________________________________________
⚡️  [log] - dense (Dense)                [null,128]                401536    
⚡️  [log] - _________________________________________________________________
⚡️  [log] - activation_4 (Activation)    [null,128]                0         
⚡️  [log] - _________________________________________________________________
⚡️  [log] - dense_1 (Dense)              [null,10]                 1290      
⚡️  [log] - _________________________________________________________________
⚡️  [log] - activation_5 (Activation)    [null,10]                 0         
⚡️  [log] - =================================================================
⚡️  [log] - Total params: 680810
⚡️  [log] - Trainable params: 680810
⚡️  [log] - Non-trainable params: 0

Thank you in advance先感谢您

I've found the error.我发现了错误。

let img = tf.browser.fromPixels(this.canvasEl);

Imports an RGB Image, but I needed greyscale.导入 RGB 图像,但我需要灰度。 So with this change it's working.所以有了这个改变,它就起作用了。

let img = tf.browser.fromPixels(this.canvasEl, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM