gosha20777 / keras2cpp

it's a small library for running trained Keras 2 models from a native C++ code.
MIT License
183 stars 54 forks source link

Initialize a 4-dim Tensor for inference #29

Open shashi7679 opened 2 years ago

shashi7679 commented 2 years ago

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, ImageDim = (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}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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.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/cppmodel.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..................?

joeycurnutt commented 2 years ago

Same issue, struggling to create multi-dimensional tensors

joeycurnutt commented 2 years ago

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.

joeycurnutt commented 2 years ago

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] = imutheta; in.data[12] = imuaccelx; in.data[13] = imuaccely; in.data[14] = liftpos; in.data_[15] = hookstate; // 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.

joeycurnutt commented 2 years ago

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