因此,我遵循了一些教程,将tensorflow模型(从tensorflow中心下载)转换为带有二进制和json文件的tfjs模型。当我尝试使用loadLayersModel()时,它会抛出一个错误。当我尝试使用loadGraphModel()时,它会加载并运行,但这些预测似乎没有效果,也没有任何意义。如何判断加载模型的方法?我需要一些关于如何排除故障的指导,或者一些推荐的工作流,特别是在tensorflowJS中。我正在使用这个反应本土与博览会项目。不确定这是否重要。
TensorflowHub:https://tfhub.dev/google/aiy/vision/classifier/food_V1/1
我的代码:
import * as tf from '@tensorflow/tfjs'
import { bundleResourceIO, decodeJpeg } from '@tensorflow/tfjs-react-native'
import * as FileSystem from 'expo-file-system';
const modelJSON = require('./TFJS/model.json')
const modelWeights = require('./TFJS/group1-shard1of1.bin')
export const loadModel = async () => {
const model = await tf.loadLayersModel(
bundleResourceIO(modelJSON, modelWeights)
).catch((e) => {
console.log("[LOADING ERROR] info:", e)
})
return model
}
export const transformImageToTensor = async (uri)=>{
//read the image as base64
const img64 = await FileSystem.readAsStringAsync(uri, {encoding:FileSystem.EncodingType.Base64})
const imgBuffer = tf.util.encodeString(img64, 'base64').buffer
const raw = new Uint8Array(imgBuffer)
let imgTensor = decodeJpeg(raw)
const scalar = tf.scalar(255)
//resize the image
imgTensor = tf.image.resizeNearestNeighbor(imgTensor, [192, 192])
//normalize; if a normalization layer is in the model, this step can be skipped
const tensorScaled = imgTensor.div(scalar)
//final shape of the rensor
const img = tf.reshape(tensorScaled, [1,192,192,3])
return img
}
export const getPredictions = async (image)=>{
await tf.ready()
const model = await loadModel()
const tensor_image = await transformImageToTensor(image)
// const predictions = await makePredictions(1, model, tensor_image)
const prediction = await model.predict(tensor_image)
console.log(prediction)
console.log(prediction.datasync()[0])
return prediction
}发布于 2022-11-04 20:25:39
如果它是一个层模型,那么您的loadLayersModel and if its a graph model, you use 'loadGraphModel -它们是不可交换的,并且您不能使用不同的方法加载模型。
因此,如果它使用loadGraphModel加载,那么它就是一个图形模型--就这样简单。
这些预测似乎没有效果,也没有意义。
这一点也没有帮助--你期望什么,你实际得到了什么?
https://stackoverflow.com/questions/74283328
复制相似问题