Coder Social home page Coder Social logo

Comments (5)

tilakrayal avatar tilakrayal commented on May 1, 2024

@saad-koukous,
Could you please share colab link or simple standalone code with supporting files to reproduce the issue in our environment. It helps us in localizing and debugging the issue faster. Thank you!

from tensorflow.

saad-koukous avatar saad-koukous commented on May 1, 2024

@tilakrayal,
Thank you for your response. However, I can't share the .tflite model and data. As I mentioned before, the expected input dimensions are 1x3x40x180. When I use the model in Python, I provide input with these dimensions. But when using the C API, it indicates that the dimensions are 1x3x1x1. Below is a snippet of my C code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tensorflow/lite/c/c_api.h>
int main() {
    // Load the TFLite model
    const char* model_path = "model.tflite";
    TfLiteModel* model = TfLiteModelCreateFromFile(model_path);
    if (!model) {
        printf("Failed to load the model.\n");
        return 1;
    }
    // Create an interpreter
    TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
    TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);
    TfLiteInterpreterOptionsDelete(options);
    if (!interpreter) {
        printf("Failed to create the interpreter.\n");
        return 1;
    }
    // Allocate input tensors
    TfLiteInterpreterAllocateTensors(interpreter);
    // Get the input tensor
    TfLiteTensor* input_tensor = TfLiteInterpreterGetInputTensor(interpreter, 0);
    int num_dims = TfLiteTensorNumDims(input_tensor);
    int dim1 = TfLiteTensorDim(input_tensor, 0);
    int dim2 = TfLiteTensorDim(input_tensor, 1);
    int dim3 = TfLiteTensorDim(input_tensor, 2);
    int dim4 = TfLiteTensorDim(input_tensor, 4);
    printf("Input tensor:\n");
    printf("Number of dimensions: %i\n", num_dims);
    printf("Dimensions: %i x %i x %i x %i\n", dim1, dim2, dim3, dim4);
    if (TfLiteTensorType(input_tensor) != kTfLiteFloat32) {
    printf("Unexpected data type in the input tensor.\n");
    return 1;
}

    // Set the input data

    // the input data is a 3D array of shape (3, 40, 180)

    // Allocate memory for the input data

    printf("stacked_input_data[0]: %f\n", stacked_input_data[0]);

    TfLiteStatus copy_status = TfLiteTensorCopyFromBuffer(input_tensor, stacked_input_data,3 * 40 * 180 * sizeof(float) );

    if (copy_status != kTfLiteOk) {

    printf("Failed to copy input data to tensor.\n");

    return 1;

    }

    printf("data copied\n");

    if (interpreter == NULL) {

        printf("Interpreter is null.\n");

        return 1;

    }
    // Free the memory for the stacked input data

    free(stacked_input_data);

    TfLiteStatus invoke_status = TfLiteInterpreterInvoke(interpreter);

    if (invoke_status != kTfLiteOk) {

        printf("Failed to invoke interpreter.\n");

        return 1;

    }
    // Get the output tensor

    const TfLiteTensor* output_tensor = TfLiteInterpreterGetOutputTensor(interpreter, 0);

    // Process the output data as needed

    // Assuming the output is a float array called 'output_data'

    float* output_data = (float*)malloc(40 * 180 * sizeof(float));

    if (!output_data) {
        printf("Failed to allocate memory for the output data.\n");
        free(output_data);
        return 1;
    }
    TfLiteTensorCopyToBuffer(output_tensor, output_data,40 * 180 * sizeof(float));
    // Clean up
    free(output_data);
    TfLiteInterpreterDelete(interpreter);
    TfLiteModelDelete(model);
    return 0;
}

from tensorflow.

pkgoogle avatar pkgoogle commented on May 1, 2024

Hi @saad-koukous, can you please show us a colab/code for how the model is produced in Python? usually you see this when those dimensions are dynamic in Python but were not explicitly set in some way but C++ doesn't have the right information to infer the actual dimensions. Explicitly setting the dimensions in Python or Resizing them in C++: https://www.tensorflow.org/lite/guide/inference#run_inference_with_dynamic_shape_model will likely resolve your issue. Let us know if that works. Thanks!

from tensorflow.

saad-koukous avatar saad-koukous commented on May 1, 2024

@pkgoogle Thank you so much! I discovered that the model's input has dynamic dimensions, so I regenerated a TFLite model with fixed input dimensions.

from tensorflow.

google-ml-butler avatar google-ml-butler commented on May 1, 2024

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

from tensorflow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.