MCPcopy Create free account
hub / github.com/apache/singa / crop

Function crop

src/io/image_transformer.cc:201–271  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

199#endif
200
201Tensor crop(Tensor& input, const size_t crop_height, const size_t crop_width,
202 const size_t crop_h_offset, const size_t crop_w_offset,
203 const string& image_dim_order) {
204 CHECK_LE(input.nDim(), 4u);
205 CHECK_GE(input.nDim(), 2u);
206
207 Tensor output;
208 const float* in = input.data<float>();
209 size_t out_idx = 0, in_idx = 0;
210 if (input.nDim() == 4u) {
211 /// TODO
212 LOG(FATAL) << "Not implemented";
213 } else if (input.nDim() == 3u) {
214 if (image_dim_order == "CHW") {
215 size_t height = input.shape(1), width = input.shape(2),
216 channel = input.shape(0);
217 CHECK_LE(crop_height + crop_h_offset, height);
218 CHECK_LE(crop_width + crop_w_offset, width);
219 float* out = new float[crop_height * crop_width * channel];
220 for (size_t c = 0; c < channel; c++) {
221 for (size_t h = 0; h < crop_height; h++) {
222 for (size_t w = 0; w < crop_width; w++) {
223 in_idx = (c * height + crop_h_offset + h) * width + crop_w_offset + w;
224 out_idx = (c * crop_height + h) * crop_width + w;
225 out[out_idx] = in[in_idx];
226 }
227 }
228 }
229 output.Resize(Shape{channel, crop_height, crop_width});
230 output.CopyDataFromHostPtr<float>(out, crop_height * crop_width * channel);
231 delete[] out;
232 } else if (image_dim_order == "HWC") {
233 size_t height = input.shape(0), width = input.shape(1),
234 channel = input.shape(2);
235 CHECK_LE(crop_height + crop_h_offset, height);
236 CHECK_LE(crop_width + crop_w_offset, width);
237 float* out = new float[crop_height * crop_width * channel];
238 for (size_t c = 0; c < channel; c++) {
239 for (size_t h = 0; h < crop_height; h++) {
240 for (size_t w = 0; w < crop_width; w++) {
241 in_idx = ((crop_h_offset + h) * width + crop_w_offset + w) * channel + c;
242 out_idx = (h * crop_width + w) * channel + c;
243 out[out_idx] = in[in_idx];
244 }
245 }
246 }
247 output.Resize(Shape{crop_height, crop_width, channel});
248 output.CopyDataFromHostPtr<float>(out, crop_height * crop_width * channel);
249 delete[] out;
250 } else {
251 LOG(FATAL) << "Unknow dimension order for images " << image_dim_order
252 << " Only support 'HWC' and 'CHW'";
253 }
254 } else { /// 2D gray image
255 size_t height = input.shape(0), width = input.shape(1);
256 CHECK_LE(crop_height + crop_h_offset, height);
257 CHECK_LE(crop_width + crop_w_offset, width);
258 float* out = new float[crop_height * crop_width];

Callers 1

ApplyMethod · 0.70

Calls 2

nDimMethod · 0.80
shapeMethod · 0.80

Tested by

no test coverage detected