[未处理的承诺拒绝:TypeError:未定义不是对象(正在评估'x.mul')]

时间:2020-05-31 05:33:20

标签: react-native tensorflow.js

我正在尝试将预训练的模型加载到react native上。

当我尝试运行它时,它给了我错误:

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'x.mul')]

这是我的代码:

const TensorCamera = cameraWithTensors(Camera);

const modelJson = require('../../assets/model/model-fixed.json');
const modelWeights = require('../../assets/model/group1-shard1of1.bin');

class CameraCompo extends Component {

  constructor(props) {
    super(props)
    this.state = { modelLoaded: false };
    this.isModelLoaded = this.isModelLoaded.bind(this);
    this.getModel = this.getModel.bind(this);
  }

  async componentDidMount() {
    await tf.ready()
    console.log("TF Ready")
    this.model = await tf.loadLayersModel(bundleResourceIO(modelJson, modelWeights));
    console.log("Model Loaded")
    this.setState({modelLoaded:true})
  }

  isModelLoaded(){
    return this.state.modelLoaded;
  }

  getModel(){
    return this.model
  }

  handleCameraStream(images, updatePreview, gl) {
    model2 = this.getModel()
    console.log(model2)
    const loop = async () => {
      const nextImageTensor = images.next().value
      const nextImageTensor2 = nextImageTensor.reshape([1,320,320,3])

      //console.log("Prediction:")
      if (this.isModelLoaded()) {
        if (model2 == undefined){
          model2 = this.getModel()
        }
        else{
        console.log(nextImageTensor2)
        console.log(model2)
        const prediction = (await model2.predict(nextImageTensor2))[0];
        console.log(prediction)
        }
      }


      //
      // do something with tensor here
      //

      // if autorender is false you need the following two lines.
      // updatePreview();
      // gl.endFrameEXP();

      requestAnimationFrame(loop);
    }
    loop();
  }

  render() {
    // Currently expo does not support automatically determining the
    // resolution of the camera texture used. So it must be determined
    // empirically for the supported devices and preview size.

    let textureDims;
    if (Platform.OS === 'ios') {
      textureDims = {
        height: 1920,
        width: 1080,
      };
    } else {
      textureDims = {
        height: 1200,
        width: 1600,
      };
    }

    return <View>
      <TensorCamera
        // Standard Camera props
        style={CameraStyle.preview}
        type={Camera.Constants.Type.front}
        isModelLoaded={this.isModelLoaded}
        getModel = {this.getModel}
        // Tensor related props
        cameraTextureHeight={textureDims.height}
        cameraTextureWidth={textureDims.width}
        resizeHeight={320}
        resizeWidth={320}
        resizeDepth={3}
        onReady={this.handleCameraStream}
        autorender={true}
      />
    </View>
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  }
});

该错误发生在行上:

const prediction = (await model2.predict(nextImageTensor2))[0];

但是前面两行

console.log(nextImageTensor2)
console.log(model2)

两者都看似正常。

要注意的一件事是,我的模型应该是图模型(它不是顺序模型),但是我的model.json表示它是一个图层模型。

链接到python模型:https://www.dropbox.com/s/6ginejkhna1sic8/unet_70.h5?dl=0

链接到Tf.js模型: https://www.dropbox.com/sh/p9ddj0vee4vrd9j/AAB5uU2EFIGr21A0_zzj3XcQa?dl=0

型号摘要: https://pastebin.com/iUrhuxJq

要验证模型是图模型,请查看“ block1a_se_excite”

用于转换的命令行:tensorflowjs_converter --input_format = keras --quantization_bytes = 1 --weight_shard_size_bytes = 9999999999999〜/ PycharmProjects / TrueSky / models / unet_70.h5〜/ PycharmProjects / TrueSky / models / tfjs_model

0 个答案:

没有答案