Javascript Can';t添加tflite模型以响应本机项目
我已经使用Expo创建了一个React原生项目,并从keras获得了一个功能齐全的转换tflite模型。我正在尝试使用react原生tensorflow lite软件包API识别我的模型a JPEG图片 我在expo项目中使用了完全相同的模板,但由于某些原因,API无法识别model.tflite文件路径。(请参阅图像附件中的错误消息) 下面是我如何使用Expo资产库实现此API的副本:Javascript Can';t添加tflite模型以响应本机项目,javascript,android,react-native,tensorflow,tensorflow-lite,Javascript,Android,React Native,Tensorflow,Tensorflow Lite,我已经使用Expo创建了一个React原生项目,并从keras获得了一个功能齐全的转换tflite模型。我正在尝试使用react原生tensorflow lite软件包API识别我的模型a JPEG图片 我在expo项目中使用了完全相同的模板,但由于某些原因,API无法识别model.tflite文件路径。(请参阅图像附件中的错误消息) 下面是我如何使用Expo资产库实现此API的副本: import {TFLiteImageRecognition} from 'react-native-te
import {TFLiteImageRecognition} from 'react-native-tensorflow-lite';
import React, {Component} from "react";
import {Text, View, StyleSheet} from "react-native";
import {Asset} from "expo-asset";
//Assets
import Anna from '../../assets/images/anna.jpg';
import Model from '../../android/app/src/main/assets/converted_model.tflite';
import Labels from '../../android/app/src/main/assets/labels.txt';
class MyImageClassifier extends Component {
constructor() {
super();
this.state = {};
try {
// Initialize TensorFlow Lite Image Recognizer
this.tfLiteImageRecognition = new TFLiteImageRecognition({
labels: Asset.fromModule(Labels).downloadAsync(),// Your label file in assets folder
model: Asset.fromModule(Model).downloadAsync() // Your tflite model in assets folder.
})
} catch (err) {
alert(err)
}
}
/*init (callback) {
// do something async and call the callback:
callback.bind(this)();
}*/
componentWillMount() {
let imagePath = Asset.fromModule(require("../../assets/images/anna.jpg"));
console.warn(imagePath);
this.classifyImage().then(console.log("Image Classified!"));
}
async classifyImage() {
try {
const results = await this.tfLiteImageRecognition.recognize({
image: Asset.fromModule(Anna).downloadAsync(), // Your image path.
inputShape: (1, 100, 100, 1), // the input shape of your model. If none given, it will be default to 224.
});
const resultObj = {
name: "Name: " + results[0].name,
confidence: "Confidence: " + results[0].confidence,
inference: "Inference: " + results[0].inference + "ms"
};
this.setState(resultObj)
} catch (err) {
alert(err)
}
}
componentWillUnmount() {
this.tfLiteImageRecognition.close() // Must close the classifier when destroying or unmounting component to release object.
}
render() {
return (
<View style={styles.container}>
<View>
<Text style={styles.results}>
{this.state.name}
</Text>
<Text style={styles.results}>
{this.state.confidence}
</Text>
<Text style={styles.results}>
{this.state.inference}
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingTop: 15,
backgroundColor: '#fff',
},
results: {
fontSize: 22,
fontFamily: "serif",
textAlign: 'center'
}
});
export default MyImageClassifier;
从'react native tensorflow lite'导入{TFLiteImageRecognition};
从“React”导入React,{Component};
从“react native”导入{Text,View,StyleSheet};
从“expo资产”导入{Asset};
//资产
从“../../assets/images/Anna.jpg”导入Anna;
从“../../android/app/src/main/assets/converted_Model.tflite”导入模型;
从“../../android/app/src/main/assets/Labels.txt”导入标签;
类MyImageClassifier扩展组件{
构造函数(){
超级();
this.state={};
试一试{
//初始化TensorFlow Lite图像识别器
this.tfLiteImageRecognition=新的tfLiteImageRecognition({
标签:Asset.fromModule(labels).downloadAsync(),//资产文件夹中的标签文件
模型:Asset.fromModule(model).downloadAsync()//资产文件夹中的tflite模型。
})
}捕捉(错误){
警报(错误)
}
}
/*初始化(回调){
//执行异步操作并调用回调:
绑定(这个)();
}*/
组件willmount(){
让imagePath=Asset.fromModule(require(“../../assets/images/anna.jpg”);
console.warn(imagePath);
this.classifyImage().then(console.log(“Image Classified!”);
}
异步classifyImage(){
试一试{
const results=等待this.tfLiteImageRecognition.recognition({
image:Asset.fromModule(Anna).downloadsync(),//您的映像路径。
inputShape:(1100,100,1),//模型的输入形状。如果没有给定,则默认为224。
});
常数resultObj={
名称:“名称:”+结果[0]。名称,
信心:“信心:”+结果[0]。信心,
推断:“推断:+结果[0]。推断+“毫秒”
};
此.setState(resultObj)
}捕捉(错误){
警报(错误)
}
}
组件将卸载(){
this.tfliteMageRecognition.close()//在销毁或卸载组件以释放对象时必须关闭分类器。
}
render(){
返回(
{this.state.name}
{this.state.confidence}
{this.state.interference}
);
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
为内容辩护:“中心”,
对齐项目:“居中”,
paddingTop:15,
背景颜色:“#fff”,
},
结果:{
尺寸:22,
fontFamily:“衬线”,
textAlign:“中心”
}
});
导出默认MyImageClassifier;
我已经测试过文件路径是否准确地指向相关文件,因此这绝对不是问题所在。我还验证了当传入正确形状的输入数据时,我的TFLite模型可以进行预测
下面是我用来作为参考的视频,帮助我实现API
任何帮助都将不胜感激。我注意到的一件事是,您的输入形状是作为inputShape:(1100,100,1)而不是单个数字给出的。我想我记得在文档中它被传递为“inputShape:224”(其中224是您的形状,我认为它是高度、宽度和通道的乘积?),感谢insight@user402516。我现在使用另一种方法,在aws sagemaker中托管模型并在那里运行推断。这个问题可以结束了。