Skip to content

Commit

Permalink
add label_ids output in crop node (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingkuo authored and mkuczyns11 committed Dec 7, 2021
1 parent 2ef8fbd commit 96aa00b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/custom_nodes/model_zoo_intel_object_detection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ make BASE_OS=redhat
| Output name | Description | Shape | Precision |
| ------------- |:-------------:| -----:| -----:|
| images | Returns images representing detected text boxes. Boxes are filtered based on confidence_threshold param. Resolution is defined by the node parameters. All images are in a single batch. Batch size depend on the number of detected objects. | `N,1,C,H,W` | FP32 |
| coordinates | For every detected box `N` the following info is added: x coordinate for the box center, y coordinate for the box center, box original width, box original height | `N,1,4` | I32 |
| coordinates | For every detected box `N` the following info is added: x coordinate for the box center, y coordinate for the box center, box original width, box original height | `N,1,4` | FP32 |
| confidences | For every detected box `N` information about confidence level (N - number of detected boxes; more about demultiplexing [here](./../../../docs/demultiplexing.md)) | `N,1,1` | FP32 |
| label_ids | For every detected box `N` information about label id (N - number of detected boxes; more about demultiplexing [here](./../../../docs/demultiplexing.md)) | `N,1,1` | I32 |

# Custom node parameters
Parameters can be defined in pipeline definition in OVMS configuration file. [Read more](./../../../docs/custom_node_development.md) about node parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static constexpr const char* OUTPUT_TENSOR_INFO_NAME = "output_info";
static constexpr const char* OUTPUT_COORDINATES_INFO_DIMS_NAME = "coordinates_info_dims";
static constexpr const char* OUTPUT_IMAGES_INFO_DIMS_NAME = "images_info_dims";
static constexpr const char* OUTPUT_CONFIDENCES_INFO_DIMS_NAME = "confidences_info_dims";
static constexpr const char* OUTPUT_LABEL_IDS_TENSOR_NAME = "label_ids";

static constexpr const int QUEUE_SIZE = 1;

Expand Down Expand Up @@ -170,6 +171,26 @@ bool copy_confidences_into_output(struct CustomNodeTensor* output, const std::ve
return true;
}

bool copy_label_ids_into_output(struct CustomNodeTensor* output, const std::vector<int>& labelIds) {
const uint64_t outputBatch = labelIds.size();
uint64_t byteSize = sizeof(float) * outputBatch;

float* buffer = (float*)malloc(byteSize);
NODE_ASSERT(buffer != nullptr, "malloc has failed");
std::memcpy(buffer, labelIds.data(), byteSize);

output->data = reinterpret_cast<uint8_t*>(buffer);
output->dataBytes = byteSize;
output->dimsCount = 3;
output->dims = (uint64_t*)malloc(output->dimsCount * sizeof(uint64_t));
NODE_ASSERT(output->dims != nullptr, "malloc has failed");
output->dims[0] = outputBatch;
output->dims[1] = 1;
output->dims[2] = 1;
output->precision = I32;
return true;
}

int initializeInternalManager(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
// creating InternalManager instance
std::unique_ptr<CustomNodeLibraryInternalManager> internalManager = std::make_unique<CustomNodeLibraryInternalManager>();
Expand Down Expand Up @@ -348,6 +369,7 @@ int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct Custo
std::vector<cv::Rect> boxes;
std::vector<cv::Vec4f> detections;
std::vector<float> confidences;
std::vector<int> labelIds;

for (uint64_t i = 0; i < detectionsCount; i++) {
float* detection = (float*)(detectionTensor->data + (i * featuresCount * sizeof(float)));
Expand All @@ -369,6 +391,7 @@ int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct Custo
boxes.emplace_back(box);
detections.emplace_back(detection[3], detection[4], detection[5], detection[6]);
confidences.emplace_back(confidence);
labelIds.emplace_back(labelId);
if (debugMode) {
std::cout << "Detection:\nImageID: " << imageId << "; LabelID:" << labelId << "; Confidence:" << confidence << "; Box:" << box << std::endl;
}
Expand Down Expand Up @@ -413,6 +436,16 @@ int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct Custo
return 1;
}

CustomNodeTensor& labelIdsTensor = (*outputs)[3];
labelIdsTensor.name = OUTPUT_LABEL_IDS_TENSOR_NAME;
if (!copy_label_ids_into_output(&labelIdsTensor, labelIds)) {
cleanup(imagesTensor);
cleanup(coordinatesTensor);
cleanup(labelIdsTensor);
free(*outputs);
return 1;
}

return 0;
}

Expand Down Expand Up @@ -525,6 +558,15 @@ int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const str
(*info)[2].dims[1] = 1;
(*info)[2].dims[2] = 1;
(*info)[2].precision = FP32;

(*info)[3].name = OUTPUT_LABEL_IDS_TENSOR_NAME;
(*info)[3].dimsCount = 3;
(*info)[3].dims = (uint64_t*)malloc((*info)->dimsCount * sizeof(uint64_t));
NODE_ASSERT(((*info)[2].dims) != nullptr, "malloc has failed");
(*info)[3].dims[0] = 0;
(*info)[3].dims[1] = 1;
(*info)[3].dims[2] = 1;
(*info)[3].precision = I32;
return 0;
}

Expand Down

0 comments on commit 96aa00b

Please sign in to comment.