Spring Boot(Kotlin)中使用TensorFlow模型推理报错求助
初始问题
在Spring Boot中使用TensorFlow模型进行推理时,已成功加载模型并创建张量,但调用模型获取结果时出现报错:
Caused by: org.tensorflow.exceptions.TFInvalidArgumentException: Expects arg[0] to be float but uint8 is provided
模型签名信息如下:
Signature for "serving_default": Method: "tensorflow/serving/predict" Inputs: "input_1": dtype=DT_FLOAT, shape=(-1, 299, 299, 3) Outputs: "dense_3": dtype=DT_FLOAT, shape=(-1, 41) Signature for "__saved_model_init_op": Outputs: "__saved_model_init_op": dtype=DT_INVALID, shape=()
当前使用的张量信息为DT_UINT8 tensor with shape [299, 299, 3]。尝试将张量类型改为float时,编写了如下代码:
val imageShape = TFloat32.tensorOf(runner.fetch(decodeImage).run()[0].shape()) val reshape = tf.reshape( decodeImage, tf.array( -1.0f, imageShape[0].getFloat(), imageShape[1].getFloat(), imageShape[2].getFloat()) )
执行后又出现新报错:
org.tensorflow.exceptions.TFInvalidArgumentException: Value for attr 'Tshape' of float is not in the list of allowed values: int32, int64
相关代码
加载模型代码(TFServices类中)
fun model(): SavedModelBundle { return SavedModelBundle .loader("/home/***/src/main/resources/pd/") .withRunOptions(RunOptions.getDefaultInstance()) .load() }
构建张量并调用模型的代码
val graph = Graph() val session = Session(graph) val tf = Ops.create(graph) val fileName = tf.constant("/home/***/src/main/resources/keyframe_1294.jpg") val readFile = tf.io.readFile(fileName) val runner = session.runner() val decodingOptions = DecodeJpeg.channels(3) val decodeImage = tf.image.decodeJpeg(readFile.contents(), decodingOptions) val imageShape = runner.fetch(decodeImage).run()[0].shape() val reshape = tf.reshape( decodeImage, tf.array( -1, imageShape.asArray()[0], imageShape.asArray()[1], imageShape.asArray()[2]) ) val tensor = runner.fetch(reshape).run()[0] val inputMap = mutableMapOf("input_tensor" to tensor) println(tensor.shape()) println(tensor.dataType()) println(tensor.asRawTensor()) val result = tfService.model().function("serving_default").call(inputMap)
更新:更换Kotlin TensorFlow依赖后的新问题
更换为Kotlin专属TensorFlow依赖:
implementation("org.jetbrains.kotlinx:kotlin-deeplearning-api:0.5.2") implementation("org.jetbrains.kotlinx:kotlin-deeplearning-tensorflow:0.5.2")
新的加载模型代码
fun myModel(): SavedModel { return SavedModel.load("/home/***/src/main/resources/pd/") }
新的预测调用代码
val file = File("/home/***/src/main/resources/keyframe_1294.jpg") val byteArray = ImageIO.read(file) val floatArray = ImageConverter.toRawFloatArray(byteArray) val myResult = tfService.myModel().predictSoftly(floatArray, "dense_3") println(myResult)
执行后出现新报错:
Caused by: org.tensorflow.TensorFlowException: Op type not registered 'DisableCopyOnRead' in binary running on My Computer. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.)
tf.contrib.resamplershould be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.
内容的提问来源于stack exchange,提问作者Saher Al-Sous

