Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Cannot copy to a TensorFlowLite tensor #55341

Closed
AndryCU opened this issue Mar 22, 2022 · 9 comments
Closed

Error Cannot copy to a TensorFlowLite tensor #55341

AndryCU opened this issue Mar 22, 2022 · 9 comments
Assignees
Labels
comp:lite TF Lite related issues stat:awaiting response Status - Awaiting response from author type:support Support issues

Comments

@AndryCU
Copy link

AndryCU commented Mar 22, 2022

When i traid to process my model got this error:
java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (serving_default_conv2d_input:0) with 90000 bytes from a Java Buffer with 22500 bytes.

Here are the libraries i'm using:
implementation 'org.tensorflow:tensorflow-lite:2.8.0'
implementation 'org.tensorflow:tensorflow-lite-support:0.3.1'
implementation 'org.tensorflow:tensorflow-lite-metadata:0.3.1'

Here you can see my model properties:

I'm traing to config the output of this way, i'm not sure if is of this way..
val p = photoFile.data!!.extras!!.get("data") as Bitmap
val tensorImage = TensorImage.fromBitmap(p)
val model = Modelo.newInstance(applicationContext)1
val imageProcessor: ImageProcessor = ImageProcessor.Builder()
.add(ResizeOp(150, 150, ResizeOp.ResizeMethod.NEAREST_NEIGHBOR))
.add(TransformToGrayscaleOp())
.add(NormalizeOp(0f,255f))
.build()
imageProcessor.process(tensorImage)

@AndryCU AndryCU added the type:others issues not falling in bug, perfromance, support, build and install or feature label Mar 22, 2022
@mohantym mohantym added comp:lite TF Lite related issues type:support Support issues and removed type:others issues not falling in bug, perfromance, support, build and install or feature labels Mar 23, 2022
@mohantym
Copy link
Contributor

mohantym commented Mar 23, 2022

Hi @AndryCU ! Can you let us know the results after changing the resize factor to (300,300)(90000 bytes) instead of (150,150)(22500 bytes) in this line ?

.add(ResizeOp(150, 150, ResizeOp.ResizeMethod.NEAREST_NEIGHBOR))

Attaching relevant thread for reference. R 1 ,2

@mohantym mohantym added the stat:awaiting response Status - Awaiting response from author label Mar 23, 2022
@AndryCU
Copy link
Author

AndryCU commented Mar 23, 2022

@mohantym it's works but, why change that value if my model was trained with images of 150x150 in greyscale with pixels values between 0 and 1???? Can you explain me??? Also who can i set each pixel value from 0 to1?? I will really apreciet it

@mohantym mohantym removed the stat:awaiting response Status - Awaiting response from author label Mar 23, 2022
@mohantym
Copy link
Contributor

@AndryCU ! The tensor buffer size is determined by datasize (float32: 4bytes) * flat size of the tensor shape (1 * height * width * 1). (4x22500 = 90000 bytes) . Can you allocate a buffer size of 90000 bytes in above method and let us know?

val imgData = ByteBuffer.allocateDirect(4 * 150 * 150 * 1 * 1);

@AndryCU
Copy link
Author

AndryCU commented Mar 23, 2022

@mohantym sorry, can you explain better, i don't know what to do sorry, if you preffer you can text me on telegram @andryssd, of course i will post evething here, but i want more interchange only if you can

@mohantym
Copy link
Contributor

mohantym commented Mar 24, 2022

@AndryCU ! Sorry ,I was proposing these changes to have a buffer size of 90000 bytes automatically. Attaching relevant thread for reference. Thanks!

import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.image.ImageProcessor;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.support.image.ops.ResizeOp;
import org.tensorflow.lite.support.common.ops.NormalizeOp;


#val p = photoFile.data!!.extras!!.get("data") as Bitmap
#val tensorImage = TensorImage.fromBitmap(p)

ImageProcessor imageProcessor =
    new ImageProcessor.Builder()
       .add(new ResizeOp(150, 150, ResizeOp.ResizeMethod.NEAREST_NEIGHBOR)))
       .add(TransformToGrayscaleOp())
       .add(NormalizeOp(0,255))
       .build();

// Create a TensorImage object. This creates the tensor of the corresponding
// tensor type (Float32 in this case) that the TensorFlow Lite interpreter needs.
TensorImage tensorImage = new TensorImage(DataType.FLOAT32); // setting Datatype float32 to allocate buffer size 90000 automatically

// Analysis code for every frame
// Preprocess the image
tensorImage.load(bitmap);
tensorImage = imageProcessor.process(tensorImage);

@mohantym mohantym added the stat:awaiting response Status - Awaiting response from author label Mar 24, 2022
@AndryCU
Copy link
Author

AndryCU commented Mar 25, 2022

It's work perfectly. Thanks, i only have to said that you wrote some lines in java, but, android studio detects it and convert to kotlin automatically.

@AndryCU AndryCU closed this as completed Mar 25, 2022
@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@YasmineeBa
Copy link

@AndryCU hi, i'm getting the same error that you got when i try to run ly model (tflite) in android studio for detecting object,
i got this problem : java.lang.IllegalArgumentException: Cannot copy from a TensorFlowLite tensor (StatefulPartitionedCall:0) with 7616000 bytes to a Java Buffer with 1411200 bytes.

Can you please tell me which file i need to modified to solve this issue please?

@mouathayed
Copy link

mouathayed commented Apr 23, 2024

hey, today I fixed the same issue. In my case, I stored the result of the preprocessing function in a Uint8List object, while my model expects a Float32 object instead. This size difference (4 bytes for float32 vs 1 byte for Uint8List) is causing the problem. I believe you are facing a similar issue because your model is expecting 90000 bytes but receiving only 22500, which is exactly a quarter of what it should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:lite TF Lite related issues stat:awaiting response Status - Awaiting response from author type:support Support issues
Projects
None yet
Development

No branches or pull requests

4 participants