简体   繁体   English

如何在React组件中使用deeplearn.js

[英]How to use deeplearn.js in a React component

I am currently working on creating a project with react and deeplearn.js, and have reached a roadblock when combining the two. 我目前正在使用react和deeplearn.js创建一个项目,并且在结合两者时遇到了障碍。 In my react application I am importing this deeplearnjs library model which I am using to do classification. 在我的react应用程序中,我正在导入用于进行分类的Deeplearnjs库模型。 Unfortunately, when I try to call the predict() method I get the following error: 不幸的是,当我尝试调用predict()方法时,出现以下错误:

TypeError: _this.variables is undefined

For the following part of code: 对于以下代码部分:

SqueezeNet.prototype.predictWithActivation = function (input, activationName) {
   var _this = this;
   var _a = this.math.scope(function () {
      var activation;
      var preprocessedInput = _this.math.subtract(input.asType('float32'), _this.preprocessOffset);

When I use the generated Javascript in a normal HTML it works perfectly, so I am unsure why I am getting this error within react. 当我在普通HTML中使用生成的Javascript时,它可以完美运行,因此我不确定为什么在响应中出现此错误。 I have a feeling it has to do with stricter React rules or Javascript versioning, but I am not sure. 我觉得这与更严格的React规则或Javascript版本控制有关,但我不确定。

Thanks! 谢谢!

UPDATE 更新

The simplest way to reproduce this is the following: 重现此内容的最简单方法如下:

  1. Create a new React app with create-react-app 使用create-react-app创建一个新的React create-react-app
  2. Run yarn add deeplearn and yarn add deeplearn-squeezenet 运行yarn add deeplearnyarn add deeplearn-squeezenet
  3. Modify App.js to the following: 将App.js修改为以下内容:

     import React, { Component } from 'react'; import {ENV, Array3D} from 'deeplearn'; import {SqueezeNet} from 'deeplearn-squeezenet'; class App extends Component { constructor() { super(); var net = new SqueezeNet(ENV.math); net.load(); var img = new Image(227, 227); img.src = 'boat.jpg'; img.onload = function () { var pixels = Array3D.fromPixels(img) var res = net.predict(pixels); }; } render() { return ( <div></div> ); } } export default App; 
  4. Download the following file into the public folder: https://raw.githubusercontent.com/PAIR-code/deeplearnjs/master/models/squeezenet/cat.jpg 将以下文件下载到公用文件夹中: https : //raw.githubusercontent.com/PAIR-code/deeplearnjs/master/models/squeezenet/cat.jpg

  5. Run yarn start 运行yarn start

For reference I am using react 16.2.0 供参考,我使用react 16.2.0

Your code is presumably failing because some of the method calls are asynchronous ( .load() for example). 您的代码可能失败,因为某些方法调用是异步的(例如.load() )。

Here is how you would make your example work with React: 这是使示例与React一起工作的方式:

  1. Create a new React app with create-react-app 使用create-react-app创建一个新的React create-react-app
  2. Run yarn add deeplearn and yarn add deeplearn-squeezenet 运行yarn add deeplearnyarn add deeplearn-squeezenet
  3. Add cat.jpg to the public folder cat.jpg添加到public folder
  4. Modify App.js as below 如下修改App.js

     import React, { Component } from 'react'; import { ENV, Array3D } from 'deeplearn'; import { SqueezeNet } from 'deeplearn-squeezenet'; const math = ENV.math; const squeezeNet = new SqueezeNet(math); class App extends Component { constructor() { super(); this.state = { statusText: 'Loading Squeezenet...' } } buildSuggestions(obj){ return Object.keys(obj).map( key => `${obj[key].toFixed(5)}: ${key}` ); } imageLoadHandler(e) { const img = e.target; squeezeNet.load() .then(() => { this.setState({ statusText: 'Predicting...' }); const pixels = Array3D.fromPixels(img); const result = squeezeNet.predict(pixels); this.setState({ statusText: '' }); squeezeNet.getTopKClasses(result, 5) .then((obj) => { this.setState({ statusText: this.buildSuggestions(obj) }); }); }); } render() { const text = Array.isArray(this.state.statusText)? this.state.statusText : [this.state.statusText]; return ( <div> <img src="cat.jpg" alt="cat" onLoad={this.imageLoadHandler.bind(this)} /> <div id="result"> { text.map(el => <div key={el}>{el}</div>) } </div> </div> ); } } export default App; 
  5. Then run yarn start 然后运行yarn start

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

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