ultralytics / ultralytics

NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
23.76k stars 4.74k forks source link

Error Implementing model training using YOLOv8 on Android #11245

Open vishalvky007 opened 2 weeks ago

vishalvky007 commented 2 weeks ago

Search before asking

Question

Screenshot from 2024-05-04 11-08-48 Getting this error java.lang.IllegalArgumentException: Cannot copy from a TensorFlowLite tensor (Identity) with shape [1, 8, 8400] to a Java object with shape [1, 10, 4].

Additional

No response

github-actions[bot] commented 2 weeks ago

👋 Hello @vishalvky007, thank you for your interest in Ultralytics YOLOv8 🚀! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 2 weeks ago

Hello! It seems like there's a mismatch between the expected output tensor shape from your TensorFlow Lite model and the shape defined in your Java code for handling the output.

Make sure the shape you're specifying in Java matches the model's final output. Based on the error, your model returns a tensor of shape [1, 8, 8400], so your Java object should reflect this shape to avoid shape incompatibility issues.

Here's how you might adjust your Java side:

// Instead, your output shape here should likely correlate with the shape [1, 8, 8400]
float[][][] output = new float[1][8][8400];

Adjust the dimensions accordingly to match your specific model outputs! If the error persists or there are further details you can provide, don't hesitate to reach out. Happy coding! 😊

vishalvky007 commented 1 week ago

package org.tensorflow.lite.examples.detection.tflite;

import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; import android.graphics.Bitmap; import android.graphics.RectF; import android.os.Trace; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Vector; import org.tensorflow.lite.Interpreter; import org.tensorflow.lite.examples.detection.env.Logger;

public class TFLiteObjectDetectionAPIModel implements Classifier { private static final Logger LOGGER = new Logger();

// Only return this many results. private static final int NUM_DETECTIONS = 10; // Float model private static final float IMAGE_MEAN = 128.0f; private static final float IMAGE_STD = 128.0f; // Number of threads in the java app private static final int NUM_THREADS = 4; private boolean isModelQuantized; // Config values. private int inputSize; // Pre-allocated buffers. private final Vector labels = new Vector(); private int[] intValues; // outputLocations: array of shape [Batchsize, NUM_DETECTIONS,4] // contains the location of detected boxes private float[][][] outputLocations; // outputClasses: array of shape [Batchsize, NUM_DETECTIONS] // contains the classes of detected boxes private float[][] outputClasses; // outputScores: array of shape [Batchsize, NUM_DETECTIONS] // contains the scores of detected boxes private float[][] outputScores; // numDetections: array of shape [Batchsize] // contains the number of detected boxes private float[] numDetections;

private ByteBuffer imgData;

private Interpreter tfLite;

private TFLiteObjectDetectionAPIModel() {}

/* Memory-map the model file in Assets. / private static MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename) throws IOException { AssetFileDescriptor fileDescriptor = assets.openFd(modelFilename); FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor()); FileChannel fileChannel = inputStream.getChannel(); long startOffset = fileDescriptor.getStartOffset(); long declaredLength = fileDescriptor.getDeclaredLength(); return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength); }

/**

Screenshot from 2024-05-11 00-04-01

I'm not getting the Java Object where it is declared?