gosha20777/keras2cpp

Initialize a 4-dim Tensor for inference

shashi7679 opened this issue · 4 comments

I am trying to infer a single sample of dimensions (1,28,28,1). Since my dumped model accepts in this format.

I did the following to initialize my data:-

Tensor in{1}{28}{28}{1}; // Creating a 4 dim. test Sample :- Batch_Size = 1, Image_Dim = (28,28), Channel = 1 (for greysacle image)
in.data_ = {{{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{0.0}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0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}}};

But I am getting a lot of errors. Which are like these,

  1. Tensor in{1}{28}{28}{1}; // Creating a 4 dim. test Sample :- Batch_Size = 1, Image_Dim = (28,28), Channel = 1 (for greysacle image)
    ^
    /home/ghosh8/project/Demo/keras2cpp/cpp_model.cc:12:25: error: expected ‘;’ before ‘}’ token
    Tensor in{1}{28}{28}{1}; // Creating a 4 dim. test Sample :- Batch_Size = 1, Image_Dim = (28,28), Channel = 1 (for greysacle image)

  2. /home/ghosh8/project/Demo/keras2cpp/cpp_model.cc:13:26: error: expected ‘;’ before ‘{’ token
    in.data_ = {{{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}

  3. /home/ghosh8/project/Demo/keras2cpp/cpp_model.cc:14:20: error: expected unqualified-id before ‘{’ token
    {{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}

Could anyone help what I am doing wrong..................?

Same issue, struggling to create multi-dimensional tensors

Try Tensor in({1, 28, 28, 1})
That seemed to work for me but I am lost on how to input data into that tensor once you have formed it. Hope that helps a little.

Hi again, after some more experimentation, I found a tedious but seemingly working solution for my problem. I'm using a 3d tensor of shape (1, 1, 16) for an LSTM, and formatted like this:
// Create a 3D Tensor on length 16 for input data.
Tensor in{1, 1, 16};
in.data_[0] = reflectivity;
in.data_[1] = echoDist;
in.data_[2] = redx;
in.data_[3] = redy;
in.data_[4] = redSize;
in.data_[5] = bluex;
in.data_[6] = bluey;
in.data_[7] = blueSize;
in.data_[8] = yellowx;
in.data_[9] = yellowy;
in.data_[10] = yellowSize;
in.data_[11] = imu_theta;
in.data_[12] = imu_accelx;
in.data_[13] = imu_accely;
in.data_[14] = liftpos;
in.data_[15] = hook_state;
// Run prediction.
Tensor out = model(in);
result = out.data_[0]
Note I get my result this way because my output is a single value. Your "output" code may be different. I know this is late but hope this helps you and any future users.

And last thing, forgot to mention I removed the "kassert" and all related lines on around line 66 of the tensor.cc file.