Commit 4ec94acc authored by Jayaram Bobba's avatar Jayaram Bobba Committed by Scott Cyphers

Added accessor methods for layer op attributes (#2964)

* Added accessor methods for layer op attributes

* style fixes and addressed PR feedback
parent 9ad52bfa
......@@ -67,6 +67,7 @@ namespace ngraph
virtual std::shared_ptr<Node>
copy_with_new_args(const NodeVector& new_args) const override;
const DetectionOutputAttrs& get_attrs() const { return m_attrs; }
private:
DetectionOutputAttrs m_attrs;
};
......
......@@ -49,6 +49,7 @@ namespace ngraph
virtual std::shared_ptr<Node>
copy_with_new_args(const NodeVector& new_args) const override;
const InterpolateAttrs& get_attrs() const { return m_attrs; }
private:
InterpolateAttrs m_attrs;
};
......@@ -71,6 +72,7 @@ namespace ngraph
virtual std::shared_ptr<Node>
copy_with_new_args(const NodeVector& new_args) const override;
const InterpolateAttrs& get_attrs() const { return m_attrs; }
private:
InterpolateAttrs m_attrs;
};
......
......@@ -23,25 +23,9 @@ using namespace ngraph;
op::PriorBox::PriorBox(const shared_ptr<Node>& layer_shape,
const shared_ptr<Node>& image_shape,
const std::vector<float>& min_sizes,
const std::vector<float>& max_sizes,
const std::vector<float>& aspect_ratios,
const bool clip,
const bool flip,
const float step,
const float offset,
const std::vector<float>& variances,
const bool scale_all)
const PriorBoxAttrs& attrs)
: Op("PriorBox", check_single_output_args({layer_shape, image_shape}))
, m_min_sizes(min_sizes)
, m_max_sizes(max_sizes)
, m_aspect_ratios(aspect_ratios)
, m_clip(clip)
, m_flip(flip)
, m_step(step)
, m_offset(offset)
, m_variances(variances)
, m_scale_all(scale_all)
, m_attrs(attrs)
{
constructor_validate_and_infer_types();
}
......@@ -82,14 +66,16 @@ void op::PriorBox::validate_and_infer_types()
auto layer_shape = const_shape->get_shape_val();
size_t num_priors = 0;
// {Prior boxes, Variance-adjusted prior boxes}
if (m_scale_all)
if (m_attrs.scale_all)
{
num_priors = ((m_flip ? 2 : 1) * m_aspect_ratios.size() + 1) * m_min_sizes.size() +
m_max_sizes.size();
num_priors = ((m_attrs.flip ? 2 : 1) * m_attrs.aspect_ratios.size() + 1) *
m_attrs.min_sizes.size() +
m_attrs.max_sizes.size();
}
else
{
num_priors = (m_flip ? 2 : 1) * m_aspect_ratios.size() + m_min_sizes.size() - 1;
num_priors = (m_attrs.flip ? 2 : 1) * m_attrs.aspect_ratios.size() +
m_attrs.min_sizes.size() - 1;
}
set_output_type(
......@@ -104,15 +90,5 @@ void op::PriorBox::validate_and_infer_types()
shared_ptr<Node> op::PriorBox::copy_with_new_args(const NodeVector& new_args) const
{
check_new_args_count(this, new_args);
return make_shared<PriorBox>(new_args.at(0),
new_args.at(1),
m_min_sizes,
m_max_sizes,
m_aspect_ratios,
m_clip,
m_flip,
m_step,
m_offset,
m_variances,
m_scale_all);
return make_shared<PriorBox>(new_args.at(0), new_args.at(1), m_attrs);
}
......@@ -22,6 +22,28 @@ namespace ngraph
{
namespace op
{
struct PriorBoxAttrs
{
// min_sizes Desired min_sizes of prior boxes
// max_sizes Desired max_sizes of prior boxes
// aspect_ratios Aspect ratios of prior boxes
// clip Clip output to [0,1]
// flip Flip aspect ratios
// step Distance between prior box centers
// offset Box offset relative to top center of image
// variances Values to adjust prior boxes with
// scale_all Scale all sizes
std::vector<float> min_sizes;
std::vector<float> max_sizes;
std::vector<float> aspect_ratios;
bool clip = false;
bool flip = false;
float step = 1.0f;
float offset = 0.0f;
std::vector<float> variances;
bool scale_all = false;
};
/// \brief Layer which generates prior boxes of specified sizes
/// normalized to input image size
class PriorBox : public Op
......@@ -31,42 +53,19 @@ namespace ngraph
///
/// \param layer_shape Shape of layer for which prior boxes are computed
/// \param image_shape Shape of image to which prior boxes are scaled
/// \param min_sizes Desired min_sizess of prior boxes
/// \param max_sizes Desired max_sizess of prior boxes
/// \param aspect_ratios Aspect ratios of prior boxes
/// \param clip Clip output to [0,1]
/// \param flip Flip aspect ratios
/// \param step Distance between prior box centers
/// \param offset Box offset relative to top center of image
/// \param variances Values to adjust prior boxes with
/// \param scale_all Scale all sizes
/// \param attrs PriorBox attributes
PriorBox(const std::shared_ptr<Node>& layer_shape,
const std::shared_ptr<Node>& image_shape,
const std::vector<float>& min_sizes,
const std::vector<float>& max_sizes,
const std::vector<float>& aspect_ratios,
const bool clip,
const bool flip,
const float step,
const float offset,
const std::vector<float>& variances,
const bool scale_all);
const PriorBoxAttrs& attrs);
void validate_and_infer_types() override;
virtual std::shared_ptr<Node>
copy_with_new_args(const NodeVector& new_args) const override;
const PriorBoxAttrs& get_attrs() const { return m_attrs; }
private:
std::vector<float> m_min_sizes;
std::vector<float> m_max_sizes;
std::vector<float> m_aspect_ratios;
bool m_clip;
bool m_flip;
float m_step;
float m_offset;
std::vector<float> m_variances;
bool m_scale_all;
PriorBoxAttrs m_attrs;
};
}
}
......@@ -23,23 +23,9 @@ using namespace ngraph;
op::PriorBoxClustered::PriorBoxClustered(const shared_ptr<Node>& layer_shape,
const shared_ptr<Node>& image_shape,
const size_t num_priors,
const std::vector<float>& widths,
const std::vector<float>& heights,
const bool clip,
const float step_widths,
const float step_heights,
const float offset,
const std::vector<float>& variances)
const PriorBoxClusteredAttrs& attrs)
: Op("PriorBoxClustered", check_single_output_args({layer_shape, image_shape}))
, m_num_priors(num_priors)
, m_widths(widths)
, m_heights(heights)
, m_clip(clip)
, m_step_widths(step_widths)
, m_step_heights(step_heights)
, m_offset(offset)
, m_variances(variances)
, m_attrs(attrs)
{
constructor_validate_and_infer_types();
}
......@@ -69,18 +55,18 @@ void op::PriorBoxClustered::validate_and_infer_types()
image_shape_rank);
NODE_VALIDATION_CHECK(this,
m_widths.size() == m_num_priors,
m_attrs.widths.size() == m_attrs.num_priors,
"Num_priors ",
m_num_priors,
m_attrs.num_priors,
" doesn't match size of widths vector ",
m_widths.size());
m_attrs.widths.size());
NODE_VALIDATION_CHECK(this,
m_heights.size() == m_num_priors,
m_attrs.heights.size() == m_attrs.num_priors,
"Num_priors ",
m_num_priors,
m_attrs.num_priors,
" doesn't match size of heights vector ",
m_heights.size());
m_attrs.heights.size());
set_input_is_relevant_to_shape(0);
......@@ -94,7 +80,7 @@ void op::PriorBoxClustered::validate_and_infer_types()
auto layer_shape = const_shape->get_shape_val();
// {Prior boxes, variances-adjusted prior boxes}
set_output_type(
0, element::f32, Shape{2, 4 * layer_shape[0] * layer_shape[1] * m_num_priors});
0, element::f32, Shape{2, 4 * layer_shape[0] * layer_shape[1] * m_attrs.num_priors});
}
else
{
......@@ -105,14 +91,5 @@ void op::PriorBoxClustered::validate_and_infer_types()
shared_ptr<Node> op::PriorBoxClustered::copy_with_new_args(const NodeVector& new_args) const
{
check_new_args_count(this, new_args);
return make_shared<PriorBoxClustered>(new_args.at(0),
new_args.at(1),
m_num_priors,
m_widths,
m_heights,
m_clip,
m_step_widths,
m_step_heights,
m_offset,
m_variances);
return make_shared<PriorBoxClustered>(new_args.at(0), new_args.at(1), m_attrs);
}
......@@ -22,6 +22,26 @@ namespace ngraph
{
namespace op
{
struct PriorBoxClusteredAttrs
{
// num_priors Number of prior boxes
// widths Desired widths of prior boxes
// heights Desired heights of prior boxes
// clip Clip output to [0,1]
// step_widths Distance between prior box centers
// step_heights Distance between prior box centers
// offset Box offset relative to top center of image
// variances Values to adjust prior boxes with
size_t num_priors;
std::vector<float> widths;
std::vector<float> heights;
bool clip = false;
float step_widths = 1.0f;
float step_heights = 1.0f;
float offset = 0.0f;
std::vector<float> variances;
};
/// \brief Layer which generates prior boxes of specified sizes
/// normalized to input image size
class PriorBoxClustered : public Op
......@@ -31,39 +51,19 @@ namespace ngraph
///
/// \param layer_shape Shape of layer for which prior boxes are computed
/// \param image_shape Shape of image to which prior boxes are scaled
/// \param num_priors Number of prior boxes
/// \param widths Desired widths of prior boxes
/// \param heights Desired heights of prior boxes
/// \param clip Clip output to [0,1]
/// \param step_widths Distance between prior box centers
/// \param step_heights Distance between prior box centers
/// \param offset Box offset relative to top center of image
/// \param variances Values to adjust prior boxes with
/// \param attrs PriorBoxClustered attributes
PriorBoxClustered(const std::shared_ptr<Node>& layer_shape,
const std::shared_ptr<Node>& image_shape,
const size_t num_priors,
const std::vector<float>& widths,
const std::vector<float>& heights,
const bool clip,
const float step_widths,
const float step_heights,
const float offset,
const std::vector<float>& variances);
const PriorBoxClusteredAttrs& attrs);
void validate_and_infer_types() override;
virtual std::shared_ptr<Node>
copy_with_new_args(const NodeVector& new_args) const override;
const PriorBoxClusteredAttrs& get_attrs() const { return m_attrs; }
private:
size_t m_num_priors;
std::vector<float> m_widths;
std::vector<float> m_heights;
bool m_clip;
float m_step_widths;
float m_step_heights;
float m_offset;
std::vector<float> m_variances;
PriorBoxClusteredAttrs m_attrs;
};
}
}
......@@ -24,35 +24,9 @@ using namespace ngraph;
op::Proposal::Proposal(const std::shared_ptr<Node>& class_probs,
const std::shared_ptr<Node>& class_logits,
const std::shared_ptr<Node>& image_shape,
const size_t base_size,
const size_t pre_nms_topn,
const size_t post_nms_topn,
const float nms_threshold,
const size_t feature_stride,
const size_t min_size,
const std::vector<float>& anchor_ratios,
const std::vector<float>& anchor_scales,
const bool clip_before_nms,
const bool clip_after_nms,
const bool normalize,
const float box_size_scale,
const float box_coord_scale,
const std::string& algo)
const ProposalAttrs& attrs)
: Op("Proposal", check_single_output_args({class_probs, class_logits, image_shape}))
, m_base_size(base_size)
, m_pre_nms_topn(pre_nms_topn)
, m_post_nms_topn(post_nms_topn)
, m_nms_threshold(nms_threshold)
, m_feature_stride(feature_stride)
, m_min_size(min_size)
, m_anchor_ratios(anchor_ratios)
, m_anchor_scales(anchor_scales)
, m_clip_before_nms(clip_before_nms)
, m_clip_after_nms(clip_after_nms)
, m_normalize(normalize)
, m_box_size_scale(box_size_scale)
, m_box_coord_scale(box_coord_scale)
, m_algo(algo)
, m_attrs(attrs)
{
constructor_validate_and_infer_types();
}
......@@ -77,7 +51,7 @@ void op::Proposal::validate_and_infer_types()
auto image_shape = const_shape->get_shape_val();
set_output_type(0, element::f32, Shape{image_shape[0] * m_post_nms_topn, 5});
set_output_type(0, element::f32, Shape{image_shape[0] * m_attrs.post_nms_topn, 5});
}
else
{
......@@ -88,21 +62,5 @@ void op::Proposal::validate_and_infer_types()
shared_ptr<Node> op::Proposal::copy_with_new_args(const NodeVector& new_args) const
{
check_new_args_count(this, new_args);
return make_shared<Proposal>(new_args.at(0),
new_args.at(1),
new_args.at(2),
m_base_size,
m_pre_nms_topn,
m_post_nms_topn,
m_nms_threshold,
m_feature_stride,
m_min_size,
m_anchor_ratios,
m_anchor_scales,
m_clip_before_nms,
m_clip_after_nms,
m_normalize,
m_box_size_scale,
m_box_coord_scale,
m_algo);
return make_shared<Proposal>(new_args.at(0), new_args.at(1), new_args.at(2), m_attrs);
}
......@@ -22,6 +22,38 @@ namespace ngraph
{
namespace op
{
// base_size Anchor sizes
// pre_nms_topn Number of boxes before nms
// post_nms_topn Number of boxes after nms
// nms_threshold Threshold for nms
// feature_stride Feature stride
// min_size Minimum box size
// anchor_ratios Ratios for anchor generation
// anchor_scales Scales for anchor generation
// clip_before_nms Clip before NMs
// clip_after_nms Clip after NMs
// normalize Normalize boxes to [0,1]
// box_size_scale Scale factor for scaling box size logits
// box_coord_scale Scale factor for scaling box coordiate logits
// algo Calculation algorithm to use
struct ProposalAttrs
{
size_t base_size;
size_t pre_nms_topn;
size_t post_nms_topn;
float nms_threshold = 0.0f;
size_t feature_stride = 1;
size_t min_size = 1;
std::vector<float> anchor_ratios;
std::vector<float> anchor_scales;
bool clip_before_nms = false;
bool clip_after_nms = false;
bool normalize = false;
float box_size_scale = 1.0f;
float box_coord_scale = 1.0f;
std::string algo;
};
class Proposal : public Op
{
public:
......@@ -30,58 +62,20 @@ namespace ngraph
/// \param class_probs Class probability scores
/// \param class_logits Class prediction logits
/// \param image_shape Shape of image
/// \param base_size Anchor sizes
/// \param pre_nms_topn Number of boxes before nms
/// \param post_nms_topn Number of boxes after nms
/// \param nms_threshold Threshold for nms
/// \param feature_stride Feature stride
/// \param min_size Minimum box size
/// \param anchor_ratios Ratios for anchor generation
/// \param anchor_scales Scales for anchor generation
/// \param clip_before_nms Clip before NMs
/// \param clip_after_nms Clip after NMs
/// \param normalize Normalize boxes to [0,1]
/// \param box_size_scale Scale factor for scaling box size logits
/// \param box_coord_scale Scale factor for scaling box coordiate logits
/// \param algo Calculation algorithm to use
/// \param attrs Proposal op attributes
Proposal(const std::shared_ptr<Node>& class_probs,
const std::shared_ptr<Node>& class_logits,
const std::shared_ptr<Node>& image_shape,
const size_t base_size,
const size_t pre_nms_topn,
const size_t post_nms_topn,
const float nms_threshold,
const size_t feature_stride,
const size_t min_size,
const std::vector<float>& anchor_ratios,
const std::vector<float>& anchor_scales,
const bool clip_before_nms,
const bool clip_after_nms,
const bool normalize,
const float box_size_scale,
const float box_coord_scale,
const std::string& algo);
const ProposalAttrs& attrs);
void validate_and_infer_types() override;
virtual std::shared_ptr<Node>
copy_with_new_args(const NodeVector& new_args) const override;
const ProposalAttrs& get_attrs() const { return m_attrs; }
private:
size_t m_base_size;
size_t m_pre_nms_topn;
size_t m_post_nms_topn;
float m_nms_threshold;
size_t m_feature_stride;
size_t m_min_size;
std::vector<float> m_anchor_ratios;
std::vector<float> m_anchor_scales;
bool m_clip_before_nms;
bool m_clip_after_nms;
bool m_normalize;
float m_box_size_scale;
float m_box_coord_scale;
std::string m_algo;
ProposalAttrs m_attrs;
};
}
}
......@@ -47,6 +47,11 @@ namespace ngraph
virtual std::shared_ptr<Node>
copy_with_new_args(const NodeVector& new_args) const override;
size_t get_output_dim() const { return m_output_dim; }
size_t get_group_size() const { return m_group_size; }
float get_spatial_scale() const { return m_spatial_scale; }
const Shape& get_num_bins() const { return m_num_bins; }
const std::string& get_kind() const { return m_kind; }
private:
size_t m_output_dim;
size_t m_group_size;
......
......@@ -49,6 +49,13 @@ namespace ngraph
virtual std::shared_ptr<Node>
copy_with_new_args(const NodeVector& new_args) const override;
size_t get_num_coords() const { return m_num_coords; }
size_t get_num_classes() const { return m_num_classes; }
size_t get_num_regions() const { return m_num_regions; }
bool get_do_softmax() const { return m_do_softmax; }
const std::vector<int64_t>& get_mask() const { return m_mask; }
int get_axis() const { return m_axis; }
int get_end_axis() const { return m_end_axis; }
private:
size_t m_num_coords;
size_t m_num_classes;
......
......@@ -21,9 +21,9 @@
using namespace std;
using namespace ngraph;
op::ReorgYolo::ReorgYolo(const shared_ptr<Node>& input, const Strides& stride)
op::ReorgYolo::ReorgYolo(const shared_ptr<Node>& input, const Strides& strides)
: Op("ReorgYolo", check_single_output_args({input}))
, m_stride(stride)
, m_strides(strides)
{
constructor_validate_and_infer_types();
}
......@@ -38,8 +38,8 @@ void op::ReorgYolo::validate_and_infer_types()
for (size_t i = 2; i < input_shape.size(); i++)
{
output_shape.push_back(input_shape[i] / m_stride[0]);
output_shape[1] *= m_stride[0];
output_shape.push_back(input_shape[i] / m_strides[0]);
output_shape[1] *= m_strides[0];
}
set_output_type(0, input_et, output_shape);
}
......@@ -52,5 +52,5 @@ void op::ReorgYolo::validate_and_infer_types()
shared_ptr<Node> op::ReorgYolo::copy_with_new_args(const NodeVector& new_args) const
{
check_new_args_count(this, new_args);
return make_shared<ReorgYolo>(new_args.at(0), m_stride);
return make_shared<ReorgYolo>(new_args.at(0), m_strides);
}
......@@ -28,16 +28,17 @@ namespace ngraph
/// \brief Constructs a ReorgYolo operation
///
/// \param input Input
/// \param stride Stride to reorganize input by
ReorgYolo(const std::shared_ptr<Node>& input, const Strides& stride);
/// \param strides Stride to reorganize input by
ReorgYolo(const std::shared_ptr<Node>& input, const Strides& strides);
void validate_and_infer_types() override;
virtual std::shared_ptr<Node>
copy_with_new_args(const NodeVector& new_args) const override;
const Strides get_strides() const { return m_strides; }
private:
Strides m_stride;
Strides m_strides;
};
}
}
......@@ -43,6 +43,9 @@ namespace ngraph
virtual std::shared_ptr<Node>
copy_with_new_args(const NodeVector& new_args) const override;
const Shape& get_output_size() const { return m_output_size; }
float get_spatial_scale() const { return m_spatial_scale; }
const std::string& get_kind() const { return m_kind; }
private:
Shape m_output_size;
float m_spatial_scale;
......
......@@ -82,98 +82,69 @@ TEST(type_prop_layers, dyn_interpolate)
TEST(type_prop_layers, prior_box1)
{
op::PriorBoxAttrs attrs;
attrs.min_sizes = {2.0f, 3.0f};
attrs.aspect_ratios = {1.0f, 2.0f, 0.5f};
auto layer_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {32, 32});
auto image_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {300, 300});
auto pb = make_shared<op::PriorBox>(layer_shape,
image_shape,
std::vector<float>{2.0f, 3.0f},
std::vector<float>{},
std::vector<float>{1.0f, 2.0f, 0.5f},
false,
false,
1.0f,
0.5f,
std::vector<float>{1.0f, 0.0f, 0.0f, 2.0f},
false);
auto pb = make_shared<op::PriorBox>(layer_shape, image_shape, attrs);
ASSERT_EQ(pb->get_shape(), (Shape{2, 16384}));
}
TEST(type_prop_layers, prior_box2)
{
op::PriorBoxAttrs attrs;
attrs.min_sizes = {2.0f, 3.0f};
attrs.aspect_ratios = {1.0f, 2.0f, 0.5f};
attrs.flip = true;
auto layer_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {32, 32});
auto image_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {300, 300});
auto pb = make_shared<op::PriorBox>(layer_shape,
image_shape,
std::vector<float>{2.0f, 3.0f},
std::vector<float>{},
std::vector<float>{1.0f, 2.0f, 0.5f},
false,
true,
1.0f,
0.5f,
std::vector<float>{1.0f, 0.0f, 0.0f, 2.0f},
false);
auto pb = make_shared<op::PriorBox>(layer_shape, image_shape, attrs);
ASSERT_EQ(pb->get_shape(), (Shape{2, 28672}));
}
TEST(type_prop_layers, prior_box3)
{
op::PriorBoxAttrs attrs;
attrs.min_sizes = {256.0f};
attrs.max_sizes = {315.0f};
attrs.aspect_ratios = {2.0f};
attrs.flip = true;
attrs.scale_all = true;
auto layer_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {1, 1});
auto image_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {300, 300});
auto pb = make_shared<op::PriorBox>(layer_shape,
image_shape,
std::vector<float>{256.0f},
std::vector<float>{315.0f},
std::vector<float>{2.0f},
false,
true,
1.0f,
0.5f,
std::vector<float>{1.0f, 0.0f, 0.0f, 2.0f},
true);
auto pb = make_shared<op::PriorBox>(layer_shape, image_shape, attrs);
ASSERT_EQ(pb->get_shape(), (Shape{2, 16}));
}
TEST(type_prop_layers, prior_box_clustered)
{
op::PriorBoxClusteredAttrs attrs;
attrs.num_priors = 3;
attrs.widths = {4.0f, 2.0f, 3.2f};
attrs.heights = {1.0f, 2.0f, 1.1f};
auto layer_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {19, 19});
auto image_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {300, 300});
auto pbc = make_shared<op::PriorBoxClustered>(layer_shape,
image_shape,
3,
std::vector<float>{4.0f, 2.0f, 3.2f},
std::vector<float>{1.0f, 2.0f, 1.1f},
false,
1.0f,
2.0f,
0.0f,
std::vector<float>{1.0f, 0.0f, 0.0f, 2.0f});
auto pbc = make_shared<op::PriorBoxClustered>(layer_shape, image_shape, attrs);
// Output shape - 4 * 19 * 19 * 3 (num_priors)
ASSERT_EQ(pbc->get_shape(), (Shape{2, 4332}));
}
TEST(type_prop_layers, proposal)
{
op::ProposalAttrs attrs;
attrs.base_size = 1;
attrs.pre_nms_topn = 20;
attrs.post_nms_topn = 200;
auto class_probs = make_shared<op::Parameter>(element::f32, Shape{1, 12, 34, 62});
auto class_logits = make_shared<op::Parameter>(element::f32, Shape{1, 24, 34, 62});
auto image_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {1, 6});
auto op = make_shared<op::Proposal>(class_probs,
class_logits,
image_shape,
1,
20,
200,
0.0f,
1,
1,
std::vector<float>{},
std::vector<float>{},
false,
false,
false,
0.1f,
0.1f,
std::string{""});
auto op = make_shared<op::Proposal>(class_probs, class_logits, image_shape, attrs);
ASSERT_EQ(op->get_shape(), (Shape{200, 5}));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment