tensorflow / models

Models and examples built with TensorFlow
Other
76.8k stars 45.84k forks source link

Detect Nothing on Android device #8680

Open Aspirinkb opened 4 years ago

Aspirinkb commented 4 years ago

I trained a mobilenet_v2_ssd and quantized to uint8. It runs ok on the official android demo, but detect nothing on my own app when input a same image.

The method as following, no detection result in outputMap.

    public List<Prediction> predict(Bitmap image) {
        outputLocations = new float[1][NUM_DETECTIONS][4];
        outputClasses = new float[1][NUM_DETECTIONS];
        outputScores = new float[1][NUM_DETECTIONS];
        numDetections = new float[1];
        int[] intValues = new int[300*300];
        image.getPixels(intValues, 0, 300, 0,0,300,300);
        this.imgData.rewind();
        for (int i = 0; i < 300; ++i) {
            for (int j = 0; j < 300; ++j) {
                int pixelValue = intValues[i * 300 + j];
                this.imgData.put((byte) ((pixelValue >> 16) & 0xFF));
                this.imgData.put((byte) ((pixelValue >> 8) & 0xFF));
                this.imgData.put((byte) (pixelValue & 0xFF));
            }
        }
        Object[] inputArray = {this.imgData};
        Map<Integer, Object> outputMap = new HashMap<>();
        outputMap.put(0, outputLocations);
        outputMap.put(1, outputClasses);
        outputMap.put(2, outputScores);
        outputMap.put(3, numDetections);
        this.interpreter.runForMultipleInputsOutputs(inputArray, outputMap);
        int numDetectionsOutput = Math.min(NUM_DETECTIONS, (int) numDetections[0]);
        final ArrayList<Prediction> predictions = new ArrayList<>(numDetectionsOutput);
        for (int i = 0; i < numDetectionsOutput; ++i) {
            final RectF bbox =
                    new RectF(
                            outputLocations[0][i][1] * inputSize,
                            outputLocations[0][i][0] * inputSize,
                            outputLocations[0][i][3] * inputSize,
                            outputLocations[0][i][2] * inputSize);
            int labelOffset = 1;
            predictions.add(
                    new Prediction(
                            "" + i,
                            labels.get((int) outputClasses[0][i] + labelOffset),
                            outputScores[0][i],
                            bbox));
            Log.i("InferenceEngine", predictions.get(i).toString());
        }
        return predictions;
    }
Simon-LLong commented 4 years ago

labeloffset should be zero. And your original bitmap is [300 * 300]?

Aspirinkb commented 4 years ago

@LL-Lee labeloff is 1 since the first label is "???", and the original bitmap is 300*300 RGB image.

Simon-LLong commented 4 years ago

The code seems right, maybe the problem is incorrect initialization of TFLiteObjectDetectionAPIModel

Aspirinkb commented 4 years ago

I don't use the TFLiteObjectDetectionAPIModel.