2 // Copyright (c) 2016 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
19 #include "../C/crop.h"
20 #include "primitive.hpp"
24 /// @addtogroup cpp_api C++ API
26 /// @addtogroup cpp_topology Network Topology
28 /// @addtogroup cpp_primitives Primitives
32 /// @brief Marker type indicating that instead of reference input size left, top,
33 /// right and bottom borders (to cut out) should be specified.
35 /// @details Used to differentiate constructors.
36 struct crop_borders_t {};
38 /// @brief Marker indicating that instead of reference input size left, top,
39 /// right and bottom borders (to cut out) should be specified.
40 constexpr auto crop_borders = crop_borders_t{};
42 /// @brief Performs crop operation on input.
43 /// @details Crops the input to the shape of reference_input across all dimensions taking into account specified input offsets.
44 /// @n Borders variant calculated output shape from input shape minus the specified borders.
47 /// @n Crop without offset example:
48 /// \image html crop_no_offset.jpg
49 /// @n Crop with offset example:
50 /// \image html crop_w_offset.jpg
52 /// @n\b Requirements (reference size variant)
53 /// @n - Input size cannot be greater than reference size in any dimension
54 /// @n - All sizes have to have positive numbers
55 /// @n - Reference size plus offset cannot exceed input size
57 /// @n\b Requirements (borders variant)
58 /// @n - Borders support batch, feature and spatial dimensions (rest of dimensions ignored).
59 /// @n - Input size cannot be greater than reference size in any dimension
60 /// @n - All sizes specified in borders have to have non-negative values (positive or @c 0).
61 /// @n - Sum of sizes of opposite borders must be lower than input size (on all non-ignored dimensions).
63 /// @n Breaking any of this conditions will cause exception throw.
64 struct crop : public primitive_base<crop, CLDNN_PRIMITIVE_DESC(crop)>
66 CLDNN_DECLARE_PRIMITIVE(crop)
68 /// @brief Constructs crop primitive.
69 /// @param id This primitive id.
70 /// @param input Input primitive id.
71 /// @param reference_input Reference input tensor with the required dimensions.
72 /// @param offsets Input offsets.
74 const primitive_id& id,
75 const primitive_id& input,
76 const tensor& reference_input,
77 const tensor& offsets,
78 const padding& output_padding = padding()
80 :primitive_base(id, {input}, output_padding)
81 , reference_input(reference_input)
86 /// @brief Constructs crop primitive (borders variant).
88 /// @details Allows to specify borders from each side that should be cut out
90 /// @n NOTE: Borders variant supports only up to four dimensions.
92 /// @param id Identifier of newly created primitive.
93 /// @param input Identifier of input primitive which dimensions will be cropped.
94 /// @param lt_borders Border sizes (spatial dimensions define left (X) and top (Y)
95 /// borders, non-spatial dimensions - lower borders)
96 /// @param rb_borders Border sizes (spatial dimensions define right (X) and bottom (Y)
97 /// borders, non-spatial dimensions - upper borders)
99 const primitive_id& id,
100 const primitive_id& input,
101 const tensor& lt_borders,
102 const tensor& rb_borders,
103 const crop_borders_t,
104 const padding& output_padding = padding()
106 :primitive_base(id, {input}, output_padding)
107 , reference_input(rb_borders.negate())
108 , offsets(lt_borders)
112 /// @brief Constructs crop primitive (symmetric borders variant).
114 /// @details Allows to specify borders from each side that should be cut out
115 /// by the primitive.
116 /// @n NOTE: Borders variant supports only up to four dimensions.
118 /// @param id Identifier of newly created primitive.
119 /// @param input Identifier of input primitive which dimensions will be cropped.
120 /// @param xy_borders Border sizes (symmetric; spatial dimensions define left/right (X)
121 /// and top/bottom (Y) borders, non-spatial dimensions - lower/upper borders).
123 const primitive_id& id,
124 const primitive_id& input,
125 const tensor& xy_borders,
126 const crop_borders_t,
127 const padding& output_padding = padding()
129 :primitive_base(id, {input}, output_padding)
130 , reference_input(xy_borders.negate())
131 , offsets(xy_borders)
135 /// @brief Constructs a copy from C API @CLDNN_PRIMITIVE_DESC{crop}
138 , reference_input(dto->reference_input)
139 , offsets(dto->offsets)
143 /// @brief Reference input tensor with the required dimensions.
144 tensor reference_input;
145 /// @brief Input offsets.
149 void update_dto(dto& dto) const override
151 dto.reference_input = reference_input;
152 dto.offsets = offsets;