I'm trying to implement the below code in a for loop, to avoid needing to have every XOR term written out separately.
unsigned int check_0 = P0^en[2]^en[4]^en[6]^en[8]^en[10]^en[12]^en[14]^en[16]^en[18]^en[20]^en[22]^en[24]^en[26]^en[28]^en[30];
This is what I've written, but it doesn't work. Can someone please let me know what I'm doing wrong?
unsigned int check_0z = P0;
unsigned int check_0 = 0;
int i = 2;
for (i = 2; i > 30; i += 2){
check_0 = check_0z^en[i];
check_0z = check_0;
}
I am trying to write a small function that calculates difference for each pixel. However, seems like casting int to uint8_t ( (uint8_t)temp ) is causing problems on a microprocessor at the very first iteration and leads to a restart
abs_diff = (uint8_t*)malloc(320*240 * sizeof(uint8_t));
for (int i = 0; i < 320*240; i++){
int temp = abs((int)_jpg_buf[i] - (int)_jpg_buf_prev[i]);
Serial.printf("abs %d\n", temp);
abs_diff[i] = (uint8_t)temp; // <- fails
}
P.S. not a C expert here
I am trying to use the C API of TF Lite to execute the inference cyclically, e.g. I set up the interpreter and feed him inputs every second or so to get predictions.
For this purpose, I have built libtensorflowlite_c.so via bazel. As documented here, I try to do the inference like this, but inside a for loop to simulate the cyclic execution:
#include "tensorflow/lite/c/c_api.h"
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char* argv[]) {
for(int i = 0; i < 3; i++)
{
printf("Iteration: %d\n", i);
float input[49] = { 0.0 };
TfLiteModel* model = TfLiteModelCreateFromFile("model.tflite");
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
TfLiteInterpreterOptionsSetNumThreads(options, 2);
TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);
TfLiteInterpreterAllocateTensors(interpreter);
TfLiteTensor* input_tensor = TfLiteInterpreterGetInputTensor(interpreter, 0);
TfLiteTensorCopyFromBuffer(input_tensor, input, 49 * sizeof(float));
TfLiteInterpreterInvoke(interpreter);
const TfLiteTensor* output_tensor = TfLiteInterpreterGetOutputTensor(interpreter, 14);
float output[49];
TfLiteTensorCopyToBuffer(output_tensor, output, 49 * sizeof(float));
printf("Output: \n\n");
for (int j = 0; j < 49; j++) {
printf("%d: %f\n", j, output[j]);
}
TfLiteInterpreterDelete(interpreter);
TfLiteInterpreterOptionsDelete(options);
TfLiteModelDelete(model);
}
return 0;
}
The first iteration runs fine and returns something. But on the second iteration, I get a SegFault when calling TfLiteTensorCopyToBuffer(output_tensor, output, 49 * sizeof(float));. Reason for this is that the previous function TfLiteInterpreterGetOutputTensor returns a nullpointer.
I expected to run this multiple times without any problems, as I destroy all old instances of variables at the end of the for-loop and thus start a fresh interpreter everytime. Obviously, this is not the case.
Can somebody provide any guidance on this? Also, I know that I probably do not have to create an interpreter on every iteration, but I wanted to make sure that everything is created new when I start over again.
EDIT:
I tried rewriting the code to exclude unnecessary parts from the actual loop:
#include "tensorflow/lite/c/c_api.h"
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char* argv[]) {
float input[49] = {0.0};
float output[49] = {[0 ... 48] = 2.5};
TfLiteModel* model = TfLiteModelCreateFromFile("VariationalAutoencoder_440.tflite");
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
TfLiteInterpreterOptionsSetNumThreads(options, 2);
TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);
TfLiteInterpreterAllocateTensors(interpreter);
TfLiteTensor* input_tensor = TfLiteInterpreterGetInputTensor(interpreter, 0);
const TfLiteTensor* output_tensor = TfLiteInterpreterGetOutputTensor(interpreter, 14);
for(int i = 0; i < 3; i++)
{
printf("\nIteration: %d\n", i);
TfLiteTensorCopyFromBuffer(input_tensor, input, 49 * sizeof(float));
TfLiteInterpreterInvoke(interpreter);
TfLiteTensorCopyToBuffer(output_tensor, output, 49 * sizeof(float));
printf("Output: \n");
for (int j = 0; j < 49; j++)
{
printf("%02d: %f\n", j, output[j]);
}
}
TfLiteInterpreterDelete(interpreter);
TfLiteInterpreterOptionsDelete(options);
TfLiteModelDelete(model);
return 0;
}
Remove all variable declarations outside and prior to the for loop, eg:
int main (int argc, char* argv[]) {
float input[49] = { 0.0 };
float output[49] = {0.0};//also needs to be initialized
//and others...
for(int i = 0; i < 3; i++)
{
printf("Iteration: %d\n", i);
....
Do the same for any calls that are creating re-usable objects, or allocating memory. Re-declaring re-usable objects in a loop (without freeing them before re-declare) can have similar results to calling malloc in a loop rather than using realloc for subsequent calls.
Your code snippet shows that you have created and deleted the following inside the loop:
TfLiteInterpreterDelete(interpreter);
TfLiteInterpreterOptionsDelete(options);
TfLiteModelDelete(model);
Calling this in a loop may also be problematic.
TfLiteTensor* input_tensor = TfLiteInterpreterGetInputTensor(interpreter, 0);
input_tensor, I believe should be created once, then, in the loop, resized as needed.
From the link you provide:
// NOTE: After a resize, the client *must* explicitly allocate tensors before
// attempting to access the resized tensor data or invoke the interpreter.
// REQUIRES: 0 <= input_index < TfLiteInterpreterGetInputTensorCount(tensor)
TFL_CAPI_EXPORT extern TfLiteStatus TfLiteInterpreterResizeInputTensor(
TfLiteInterpreter* interpreter, int32_t input_index, const int* input_dims,
int32_t input_dims_size);
Edit: One other item that seems odd:
const TfLiteTensor* output_tensor = TfLiteInterpreterGetOutputTensor(interpreter, 14);
The modifier const seems an odd bedfellow to output_tensor. It would seem if this variable will change inside the loop, then it should not be modified to const.
Your code is running well if TfLiteInterpreterGetOutputTensor use index below TfLiteInterpreterGetOutputTensorCount.
Maybe the tensor index 14 should be 13, but this depends on your model.
Adding some check like :
int count = TfLiteInterpreterGetOutputTensorCount(interpreter);
printf("output tensor count:%d\n", count);
if (count > 14) {
const TfLiteTensor* output_tensor = TfLiteInterpreterGetOutputTensor(interpreter, 14);
float output[49];
TfLiteTensorCopyToBuffer(output_tensor, output, 49 * sizeof(float));
printf("Output: \n\n");
for (int j = 0; j < 49; j++) {
printf("%d: %f\n", j, output[j]);
}
}
I am using LibSVM to carry out some multi-class classifications. I trained the model using the MATLAB interface of LibSVM. I then saved this model in a format that would be recognized in C. I now want to classify using svm_predict in C. I am having trouble being able to reproduce the results that I saw in MATLAB. In fact I get the same class output irrespective of what test vector I feed in (even a vector of zeros) I think the issue is with the way I am loading the test vector x into the svm_node structure. Below is the code snippet. Do let me know if this is correct way or if I am missing something.
struct svm_model *libsvm_model = svm_load_model('mymodel.svm');
struct svm_node x[2001]; // this is for one feature vector of size 2000x1
int index = 1;
int i = 0;
for (i = 0; i < features.size(); i++) {
x[i].index = index;
x[i].value = features.at(i);
index = index + 1;
}
x[i+1].index = -1;
x[i+1].value = '?';
double result = svm_predict(libsvm_model, x);
This seems to be a problem:
x[i+1].index = -1;
x[i+1].value = '?';
libsvm requires svm_node to be an input vector, which should have positive indexes, and double values. You should not "leave" some weird empty dimension.
And by the way, you don't need index variable
for (i = 0; i < features.size(); i++) {
x[i].index = index;
x[i].value = features.at(i);
index = index + 1;
}
is equivalent to
for (i = 0; i < features.size(); i++) {
x[i].index = i + 1;
x[i].value = features.at(i);
}
I have a array short frame[4] and I want it as a function parameter as short frame[2][2]
How can I cast it? I tried different things (like *(short [2][2])&frame[0]*), but I still get error messages.
Also not working is if I declare the function with void function(short frame[2][2])
and call the function with function(&frame[0]) while frame is a short frame[4];
I don't think it is a good pratice, anyway:
f((short (*)[2])a);
This works here, albeit with a warning.
#include <stdio.h>
void function(short frame[2][2])
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
printf("%d ", frame[i][j]);
printf("\n");
}
int main()
{
short frame[4] = { 0, 1, 2, 3 };
function(&frame[0]);
return 0;
}
What error message do you get?
You can't cast between different dimensional arrays.
It wouldn't know which way round you wanted it.
You'll have to write a function.
In pseudocode:
function castArray(short[4] input){
short[2][2] output = new short[2][2];
output[0][0] = input[0];
output[0][1] = input[1];
output[1][0] = input[2];
output[1][1] = input[3];
return output;
}
You achieve it an easy way like this :
Converting 1-D array into 2-D array
short frame[MAX];
short dup_frame[ROW][COL];
int i,j,k;
for(i = 0 ; i < MAX ; i++)
{
j= i / ROW ; // you can do it by i / COL also
k= i % ROW ; // you can do it by i % COL also
dup_frame[j][k] = frame[i];
}