Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-hills committed Jan 27, 2021
1 parent a9386ef commit 6188272
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.vscode/
*.out
*.out
*.a
*.o
*.so
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This repository contains the source code to compile an image upscale dynamic lib
Make sure Tensorflow c is installed on your machine then link to that library when compiling (similar to below).

```console
gcc -I /usr/local/include/ -L /usr/local/lib/ -Wall -fPIC -C src/entry_functions.c -ltensorflow -o entry_functions.o
gcc -I /usr/local/include/ -L /usr/local/lib/ -Wall -fPIC -c src/entry_functions.c -ltensorflow -o entry_functions.o
gcc -shared -o image-upscale.so entry_functions.o
```

Expand Down
18 changes: 9 additions & 9 deletions src/entry_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Model* model = NULL;
ModelInfo* modelInfo = NULL;

void initialize(const char* errorMsg){
void initialize(char* errorMsg){
// create model
model = newModel();
// get NoiseCancel model info (currently only supported model)
Expand All @@ -15,27 +15,27 @@ void initialize(const char* errorMsg){
if(check.code)
{
freeModel(model); // free memory from model
errorMsg = TF_Message(check.status);
strcpy(errorMsg, TF_Message(check.status));
return;
}
check = findModelNodes(model, modelInfo);
if(check.code)
{
freeModel(model); // free memory from model
errorMsg = TF_Message(check.status);
strcpy(errorMsg, TF_Message(check.status));
return;
}
}
// on success call freeOutputData to avoid memory leak, returns null and errorMsg if errors
OutputData* runModel(const char* errorMsg, DataInfo dataInfo){
OutputData* runModel(char* errorMsg, DataInfo dataInfo){
// create input and output tensor on heap
TF_Tensor** inputTensor=(TF_Tensor**)malloc(sizeof(TF_Tensor*) * modelInfo->numInputNodes);
TF_Tensor* outputTensor = (TF_Tensor*)malloc(sizeof(TF_Tensor*));
TF_Tensor* outputTensor=malloc(sizeof(TF_Tensor*));
// create tensors for all the static data required for models
for(unsigned int i=0; i < modelInfo->numStaticInputData; ++i){
TFInfo check = dataInfoToTensor(&inputTensor, &modelInfo->staticInputData[i], model->status, i);
if(check.code){
errorMsg = TF_Message(check.status);
strcpy(errorMsg, TF_Message(check.status));
freeTensor(inputTensor, modelInfo->numInputNodes);
free(outputTensor);
return NULL;
Expand All @@ -44,15 +44,15 @@ OutputData* runModel(const char* errorMsg, DataInfo dataInfo){
// create tensors from the passed in data
TFInfo check = dataInfoToTensor(&inputTensor, &dataInfo, model->status, modelInfo->numStaticInputData);
if(check.code){
errorMsg = TF_Message(check.status);
strcpy(errorMsg, TF_Message(check.status));
freeTensor(inputTensor, modelInfo->numInputNodes);
free(outputTensor);
return NULL;
}
// run model on the tensors
check = run(model, modelInfo, inputTensor, &outputTensor);
if(check.code){
errorMsg = TF_Message(check.status);
strcpy(errorMsg, TF_Message(check.status));
freeTensor(inputTensor, modelInfo->numInputNodes);
freeTensor(&outputTensor, 1);
return NULL;
Expand All @@ -77,4 +77,4 @@ void freeOutputData(OutputData *outputData){

void cleanup(){
freeModel(model);
}
}
4 changes: 2 additions & 2 deletions src/entry_functions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "models.c"
#include "macros.c"

EXPORTED OutputData* runModel(const char* errorMsg, DataInfo dataInfo);
EXPORTED void initialize(const char* errorMsg);
EXPORTED OutputData* runModel(char* errorMsg, DataInfo dataInfo);
EXPORTED void initialize(char* errorMsg);
EXPORTED void freeOutputData(OutputData *outputData);
EXPORTED void cleanup();
2 changes: 1 addition & 1 deletion src/type_defines.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ typedef struct OutputData{
// output tensor here for free call
TF_Tensor* outputTensor;
// number of dimensions
unsigned numOfDimensions;
unsigned int numOfDimensions;
// shape of the data
int64_t* dimension;
}OutputData;
10 changes: 9 additions & 1 deletion src/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ TFInfo dataInfoToTensor(TF_Tensor*** inputTensor, DataInfo* dataInfo, TF_Status*
&NoOpDeallocator,
0);
if(inputTensor[0][index] == NULL){
return newTFInfo(status, TF_ABORTED, "Input tensor could not be created");
char buffer[256];
sprintf(buffer, "Input tensor could not be created. numDims: %i, dataSize: %i, dims:[%i,%i,%i,%i]",
(int)dataInfo->numberOfDimensions,
(int)dataInfo->dataSize,
(int)dataInfo->dimensions[0],
(int)dataInfo->dimensions[1],
(int)dataInfo->dimensions[2],
(int)dataInfo->dimensions[3]);
return newTFInfo(status, TF_ABORTED, buffer);
}
return newTFInfo(status, TF_OK, "");
}
Expand Down

0 comments on commit 6188272

Please sign in to comment.