https://kotlinlang.org logo
#server
Title
# server
s

Saher Al-Sous

10/17/2023, 11:01 AM
Hello... there is no channel for Kotlin-DL, so if there anyone that can help with this in Spring boot, i will be really thankful... I'm trying to find a way to use `tensorflow`model in
Spring Boot
, I loaded the model successfully, and created the needed tensor, but I can't make a call to get the result from the model because of this error:
Caused by: org.tensorflow.exceptions.TFInvalidArgumentException: Expects arg[0] to be float but uint8 is provided
I checked the model signature and it was like this:
Copy code
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=()
my tensor details are
DT_UINT8 tensor with shape [299, 299, 3]
. When I changed my tensor data type into float like this:
Copy code
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())
            )
I got this error:
org.tensorflow.exceptions.TFInvalidArgumentException: Value for attr 'Tshape' of float is not in the list of allowed values: int32, int64
how can I fix this problem? if someone is curious how I loaded the model, created the tensor and called it, here is the code below Loading the model in `TFServices`:
Copy code
fun model(): SavedModelBundle  {
        return SavedModelBundle
            .loader("/home/saher/kotlin/Spring/machinelearning/src/main/resources/pd/")
            .withRunOptions(RunOptions.getDefaultInstance())
            .load()
    }
Building the Tensor and calling the model
Copy code
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)
regards
Update: I changed the whole code, and used the Kotlin Tensorflow dependencies
Copy code
implementation("org.jetbrains.kotlinx:kotlin-deeplearning-api:0.5.2")
implementation("org.jetbrains.kotlinx:kotlin-deeplearning-tensorflow:0.5.2")
I loaded the model:
Copy code
fun myModel(): SavedModel {
        return SavedModel.load("/home/***/src/main/resources/pd/")
    }
and used called for the prediction:
Copy code
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)
but i got this error:
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.resampler
should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.
f

Filipp Zhinkin

10/18/2023, 2:53 PM
@Saher Al-Sous there's #kotlindl channel, maybe someone there can help you
s

Saher Al-Sous

10/18/2023, 3:04 PM
didn't know about it... thank you
3 Views