【问题标题】:Tensorflowjs in ionic离子中的Tensorflowjs
【发布时间】:2018-05-08 00:05:24
【问题描述】:

我尝试在 ionic 中使用 tensorflowjs。从 python 转换现有模型然后从 ionic 导入后,它仅在我在本地服务器上运行时才有效(http://localhost:8100/ionic-lab

但是,当我为 android 构建项目时 tf.loadModel 方法不起作用,它无法从本地文件夹(即资产/模型)加载模型

我已经检查了这个链接Tensorflow.js with react-native ,但它没有帮助。我想,许多混合移动应用程序框架几乎都是同一条线。任何意见和建议将不胜感激。

    import {Component} from '@angular/core';
    import {IonicPage, AlertController} from 'ionic-angular';
    import {HttpClient} from "@angular/common/http";
    import * as tf from "@tensorflow/tfjs";

    @IonicPage()
    @Component({
      selector: 'page-tfpretrainedversion',
      templateUrl: 'tfpretrainedversion.html',
    })
    export class TfpretrainedversionPage {

      kerasTraindedModel: tf.Model;
      KERAS_MODEL_JSON = 'assets/model/model.json';

      constructor(private httpClient: HttpClient,
                  private alertCtrl: AlertController) {
        this.loadPretrainedModel();
      }

      loadPretrainedModel() {

        tf.loadModel(this.KERAS_MODEL_JSON)
          .then((result) => {
            this.kerasTraindedModel = result;
          })
          .catch((error)=>{
            let prompt = this.alertCtrl.create({
              title: 'Error',
              subTitle: error,
              buttons: ['OK']
            });
            prompt.present();
          });
      }
    }

这是一条错误消息 Failed to fetch

这是一个项目结构 Project structure

【问题讨论】:

  • 问题可能出在 mode.json 文件路径上。您需要从本地服务器提供它。您不能为 tf.loadModel() 方法提供“file://”。您在控制台中看到任何错误吗?
  • @superUser 根据以下链接,tfjs 团队计划通过对 tf.io.httpRequest 的自定义 fetch 配置来支持此功能。

标签: ionic-framework hybrid-mobile-app tensorflow.js


【解决方案1】:

TensorflowJs 通过 fetch() 加载模型。 fetch() 不支持加载本地文件。 https://fetch.spec.whatwg.org/

我的解决方法:

使用 polyfill (https://github.com/github/fetch) 并替换 fetch。

window.fetch = fetchPolyfill;

现在,可以加载本地文件(file:///),例如:

const modelUrl = './model.json'

const model = await tf.loadGraphModel(modelUrl);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-02
    • 2019-09-12
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    相关资源
    最近更新 更多