tensorflow / tensorflow/tflite-support

Unknown image file format. One of JPEG, PNG, GIF, BMP required.

Open
#870 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
441
Forks
146
PR merge metrics
No merged PRs in 30d

Description

Hi , I have a tflite model trained with tensorflow 1.x
I have converted my model to tflite with below code:

         converter = tf.lite.TFLiteConverter.from_saved_model(
         saved_model_dir=SAVED_MODEL_DIR)
         converter.optimizations = {tf.lite.Optimize.DEFAULT} #optional sh
         converter.change_concat_input_ranges = True #optional sh
         converter.target_spec.supported_ops = [  #should be
         tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
         tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
          ] 
         tflite_model = converter.convert()

I have tested my tflite model with python interpreter and got desirable output by this code:

          input_details = interpreter.get_input_details()
          output_details = interpreter.get_output_details()
          image_filename='img.jpeg'
          input_data  = tf.compat.v1.gfile.FastGFile(image_filename, 'rb').read()
          input_data = np.array([input_data ])
          interpreter.set_tensor(input_details[0]['index'], input_data)
          interpreter.invoke()
          output_data = interpreter.get_tensor(output_details[0]['index'])
          print(output_data)

Now I want to write and inference in android studio 7.2.1 , the model is not loaded in ML folder and so I should load it with interpreter like this:

           tflitemodel = loadModelFile(this.assets, "tag.tflite")
           tflite = Interpreter(tflitemodel)
           val options = Interpreter.Options()
           val index = tflite.getInputIndex("input_values:0")
           tflite.resizeInput(
                index,
                intArrayOf(1, catBitmap!!.width, catBitmap!!.height, 3)
           )

until hear, everything is ok, but when I try to feed input image to my model with the below code:

     val catBitmap = getBitmapFromAsset("bwr.jpg")
     val width: Int = catBitmap.getWidth()
     val height: Int = catBitmap.getHeight()
     val imageProcessor = ImageProcessor.Builder()
            .add(
                ResizeOp(
                    height,
                    width,
                    ResizeOp.ResizeMethod.BILINEAR
                )
            )
            //.add(NormalizeOp(0.0, 255.0))
            .build()
      var tensorImage = TensorImage(DataType.UINT8)
      tensorImage.load(catBitmap);
      tensorImage = imageProcessor.process(tensorImage);
      val dd1=tensorImage.getBuffer()
      val ssw=dd1.order(ByteOrder.nativeOrder())
      val input1=arrayOf(ssw.toString())
      val probabilityBuffer =
            TensorBuffer.createFixedSize(intArrayOf(1, 5000), DataType.FLOAT32)
       
     tflite.run(input1, probabilityBuffer.getBuffer());

I face this error :

java.lang.IllegalArgumentException: Internal error: Failed to run on the given Interpreter: Unknown image file format. One of JPEG, PNG, GIF, BMP required.
(while executing 'DecodeBmp' via Eager)

while according to Netron, the input type of my model should be sting[1] as I provided in my code. would you please help me to fix it ? what is my mistake?

cPcyT

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Android inference code around Interpreter.run, TensorImage.getBuffer(), and the input tensor reported by Netron; compare the model's expected string[1] input with the value passed as input1. Done means the model runs without the DecodeBmp/unknown image format error and produces the expected output buffer.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
machine-learning, mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.