-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RMN PR #32
RMN PR #32
Changes from 2 commits
d3c67e0
2822bf3
a647da4
15cbf35
a0adfa6
9772f43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright (c) 2019 - 2023 Advanced Micro Devices, Inc. All rights reserved. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
|
||
#pragma once | ||
#include "node.h" | ||
#include "parameter_factory.h" | ||
#include "parameter_vx.h" | ||
|
||
class ResizeMirrorNormalizeNode : public Node | ||
{ | ||
public: | ||
ResizeMirrorNormalizeNode(const std::vector<Image *> &inputs, const std::vector<Image *> &outputs); | ||
ResizeMirrorNormalizeNode() = delete; | ||
void init(std::vector<float>& mean, std::vector<float>& std_dev, IntParam *mirror); | ||
vx_array get_dst_width() { return _dst_roi_width; } | ||
vx_array get_dst_height() { return _dst_roi_height;} | ||
vx_array get_src_width() { return _src_roi_width; } | ||
vx_array get_src_height() { return _src_roi_height; } | ||
vx_array return_mirror(){ return _mirror.default_array(); } | ||
protected: | ||
void create_node() override; | ||
void update_node() override; | ||
private: | ||
vx_array _dst_roi_width, _dst_roi_height; | ||
std::vector<uint> _dest_width_val, _dest_height_val; | ||
vx_array _mean_array, _std_dev_array; | ||
std::vector<float> _mean; | ||
std::vector<float> _std_dev; | ||
ParameterVX<int> _mirror; | ||
constexpr static int MIRROR_RANGE [2] = {0, 1}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -567,6 +567,55 @@ rocalResize( | |
return output; | ||
} | ||
|
||
RocalImage ROCAL_API_CALL | ||
rocalResizeMirrorNormalize( | ||
RocalContext p_context, | ||
RocalImage p_input, | ||
unsigned dest_width, | ||
unsigned dest_height, | ||
std::vector<float> &mean, | ||
std::vector<float> &std_dev, | ||
bool is_output, | ||
RocalIntParam p_mirror) | ||
{ | ||
if(!p_context || !p_input || dest_width == 0 || dest_height == 0 ) | ||
THROW("Null values passed as input") | ||
Image* output = nullptr; | ||
auto context = static_cast<Context*>(p_context); | ||
auto input = static_cast<Image*>(p_input); | ||
auto mirror = static_cast<IntParam *>(p_mirror); | ||
for(unsigned i = 0; i < mean.size(); i++) { | ||
mean[i] = 0; | ||
std_dev[i] = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we hardcoding mean and std_dev value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the current image pipeline, the mean and std values were not being passed to the RMN calls and instead 0 and 1 is being passed. In case of CMN, we directly pass 0 and 1 to the CMN node init. But RMN Tensor needs mean and std as vectors so we are storing 0 and 1 in the vectors being passed to the RMN node |
||
} | ||
|
||
try | ||
{ | ||
// For the resize mirror normalize resize node, user can create an image with a different width and height | ||
ImageInfo output_info = input->info(); | ||
output_info.width(dest_width); | ||
output_info.height(dest_height); | ||
output = context->master_graph->create_image(output_info, is_output); | ||
// For the nodes that user provides the output size the dimension of all the images after this node will be fixed and equal to that size | ||
output->reset_image_roi(); | ||
|
||
std::shared_ptr<ResizeMirrorNormalizeNode> rmn_node = context->master_graph->add_node<ResizeMirrorNormalizeNode>({input}, {output}); | ||
// RPP doesn't support returning float buffers so passing 0 and 1 as mean and std and doing normalization in rocAL | ||
// TODO: To be removed with rocAL Tensor support | ||
// rmn_node->init(0, 1, mirror); | ||
rmn_node->init(mean, std_dev, mirror); | ||
// TODO: Uncomment the below lines once RMN meta node is added to ToT | ||
// if (context->master_graph->meta_data_graph()) | ||
// context->master_graph->meta_add_node<ResizeMirrorNormalizeMetaNode,ResizeMirrorNormalizeNode>(rmn_node); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was the meta node support not added? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR is just for adding the RMN augmentation support. As of the current status of this PR, there is no support for reading metadata for RMN |
||
} | ||
catch(const std::exception& e) | ||
{ | ||
context->capture_error(e.what()); | ||
ERR(e.what()) | ||
} | ||
return output; | ||
} | ||
|
||
RocalImage ROCAL_API_CALL | ||
rocalBrightness( | ||
RocalContext p_context, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
Copyright (c) 2019 - 2023 Advanced Micro Devices, Inc. All rights reserved. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
|
||
#include <vx_ext_rpp.h> | ||
#include <graph.h> | ||
#include <cmath> | ||
#include "node_resize_mirror_normalize.h" | ||
#include "exception.h" | ||
|
||
ResizeMirrorNormalizeNode::ResizeMirrorNormalizeNode(const std::vector<Image *> &inputs, const std::vector<Image *> &outputs) : | ||
Node(inputs, outputs), _mirror(MIRROR_RANGE[0], MIRROR_RANGE[1]) | ||
{ | ||
} | ||
|
||
void ResizeMirrorNormalizeNode::create_node() | ||
{ | ||
if(_node) | ||
return; | ||
|
||
std::vector<vx_float32> mean_vx, std_dev_vx; | ||
_dest_width_val.resize(_batch_size); | ||
_dest_height_val.resize(_batch_size); | ||
mean_vx.resize(_batch_size * 3); | ||
std_dev_vx.resize(_batch_size * 3); | ||
for (uint i = 0; i < _batch_size; i++) { | ||
mean_vx[3 * i] = _mean[0]; | ||
mean_vx[3 * i + 1] = _mean[1]; | ||
mean_vx[3 * i + 2] = _mean[2]; | ||
|
||
std_dev_vx[3 * i] = _std_dev[0]; | ||
std_dev_vx[3 * i + 1] = _std_dev[1]; | ||
std_dev_vx[3 * i + 2] = _std_dev[2]; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we change this to mem_copy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mean_vx and std_dev_vx are vectors of size BS*3 while _mean and _std_dev are vectors of size 3. If we opt for memcpy it still needs to done over a loop so this seems to be the clean option |
||
_mean_array = vxCreateArray(vxGetContext((vx_reference)_graph->get()), VX_TYPE_FLOAT32, _batch_size * 3); | ||
_std_dev_array = vxCreateArray(vxGetContext((vx_reference)_graph->get()), VX_TYPE_FLOAT32, _batch_size * 3); | ||
vx_status mean_status = VX_SUCCESS; | ||
mean_status |= vxAddArrayItems(_mean_array,_batch_size * 3, mean_vx.data(), sizeof(vx_float32)); | ||
mean_status |= vxAddArrayItems(_std_dev_array,_batch_size * 3, std_dev_vx.data(), sizeof(vx_float32)); | ||
_mirror.create_array(_graph ,VX_TYPE_UINT32, _batch_size); | ||
if(mean_status != 0) | ||
THROW(" vxAddArrayItems failed in the resize mirror normalize node (vxExtrppNode_ResizeMirrorNormalizeCropbatchPD ) node: "+ TOSTR(mean_status) + " "+ TOSTR(mean_status)) | ||
|
||
unsigned int chnShift = 0; | ||
vx_scalar chnToggle = vxCreateScalar(vxGetContext((vx_reference)_graph->get()), VX_TYPE_UINT32, &chnShift); | ||
|
||
std::vector<uint32_t> dst_roi_width(_batch_size,_outputs[0]->info().width()); | ||
std::vector<uint32_t> dst_roi_height(_batch_size, _outputs[0]->info().height_single()); | ||
|
||
_dst_roi_width = vxCreateArray(vxGetContext((vx_reference)_graph->get()), VX_TYPE_UINT32, _batch_size); | ||
_dst_roi_height = vxCreateArray(vxGetContext((vx_reference)_graph->get()), VX_TYPE_UINT32, _batch_size); | ||
vx_status width_status, height_status; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove extra lines inbetween |
||
width_status = vxAddArrayItems(_dst_roi_width, _batch_size, dst_roi_width.data(), sizeof(vx_uint32)); | ||
height_status = vxAddArrayItems(_dst_roi_height, _batch_size, dst_roi_height.data(), sizeof(vx_uint32)); | ||
if(width_status != 0 || height_status != 0) | ||
THROW(" vxAddArrayItems failed in the resize mirror normalize node (vxExtrppNode_ResizeMirrorNormalizeCropbatchPD ) node: " + TOSTR(width_status) + " " + TOSTR(height_status)) | ||
|
||
_node = vxExtrppNode_ResizeMirrorNormalizeTensor(_graph->get(), _inputs[0]->handle(), _src_roi_width, _src_roi_height, _outputs[0]->handle(), | ||
_dst_roi_width, _dst_roi_height, _mean_array, _std_dev_array, | ||
_mirror.default_array(), chnToggle, _batch_size); | ||
|
||
vx_status status; | ||
if((status = vxGetStatus((vx_reference)_node)) != VX_SUCCESS) | ||
THROW("Adding the resize mirror normalize node (vxExtrppNode_ResizeMirrorNormalizeCropbatchPD) node failed: "+ TOSTR(status)) | ||
} | ||
|
||
void ResizeMirrorNormalizeNode::update_node() | ||
{ | ||
std::vector<uint32_t> src_roi_width, src_roi_height; | ||
src_roi_width = _inputs[0]->info().get_roi_width_vec(); | ||
src_roi_height = _inputs[0]->info().get_roi_height_vec(); | ||
|
||
for(uint i = 0; i < _batch_size; i++) | ||
{ | ||
// Min size and max size used for MLPerf MaskRCNN resize augmentation | ||
// TODO: Get the min_size and max_size as user arguments from python | ||
int min_size = 800; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How did we arrive at this min size and max size? |
||
int max_size = 1333; | ||
int src_width = src_roi_width[i]; | ||
int src_height = src_roi_height[i]; | ||
int size = min_size; | ||
int output_width, output_height; | ||
|
||
float min_original_size = static_cast<float>(std::min(src_width, src_height)); | ||
float max_original_size = static_cast<float>(std::max(src_width, src_height)); | ||
if(max_original_size / min_original_size * size > max_size) | ||
size = static_cast<size_t>(round(max_size * min_original_size / max_original_size)); | ||
|
||
if (((src_width <= src_height) && (src_width == size)) || ((src_height <= src_width) && (src_height == size))) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For if and else condition syntax please follow google coding guidelines, in this file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this sorted out? |
||
_dest_height_val[i] = src_height; | ||
_dest_width_val[i] = src_width; | ||
continue; | ||
} | ||
|
||
if(src_width < src_height) { | ||
output_width = size; | ||
output_height = static_cast<size_t>(size * src_height / src_width); | ||
} else { | ||
output_height = size; | ||
output_width = static_cast<size_t>(size * src_width / src_height); | ||
} | ||
_dest_height_val[i] = output_height; | ||
_dest_width_val[i] = output_width; | ||
} | ||
vxCopyArrayRange((vx_array)_dst_roi_width, 0, _batch_size, sizeof(uint), _dest_width_val.data(), VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST); | ||
vxCopyArrayRange((vx_array)_dst_roi_height, 0, _batch_size, sizeof(uint), _dest_height_val.data(), VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST); | ||
|
||
_outputs[0]->update_image_roi(_dest_width_val, _dest_height_val); | ||
_mirror.update_array(); | ||
} | ||
|
||
void ResizeMirrorNormalizeNode::init(std::vector<float>& mean, std::vector<float>& std_dev, IntParam *mirror) | ||
{ | ||
_mean = mean; | ||
_std_dev = std_dev; | ||
_mirror.set_param(core(mirror)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please align with other params
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some are in tab and some in space. Convert everything to tab/space.