问题
I'm trying to get a pre-trained model loaded onto react native.
When I try to run it, it gives me the error:
[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'x.mul')]
Here is my code:
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'
}
});
The error occurs on the line:
const prediction = (await model2.predict(nextImageTensor2))[0];
But the two lines before
console.log(nextImageTensor2)
console.log(model2)
both return seemingly normal things.
One thing to note is that my model should be a graph model (it isn't sequential), but my model.json says it is a layers-model.
Link to python model: https://www.dropbox.com/s/6ginejkhna1sic8/unet_70.h5?dl=0
Link to Tf.js model: https://www.dropbox.com/sh/p9ddj0vee4vrd9j/AAB5uU2EFIGr21A0_zzj3XcQa?dl=0
Model Summary: https://pastebin.com/iUrhuxJq
To verify that the model is a graph model, look to "block1a_se_excite"
Command line used to convert: tensorflowjs_converter --input_format=keras --quantization_bytes=1 --weight_shard_size_bytes=9999999999999 ~/PycharmProjects/TrueSky/models/unet_70.h5 ~/PycharmProjects/TrueSky/models/tfjs_model
来源:https://stackoverflow.com/questions/62111983/unhandled-promise-rejection-typeerror-undefined-is-not-an-object-evaluating