shape.cpp 1.02 KB
Newer Older
limm's avatar
limm committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) OpenMMLab. All rights reserved.
#include "shape.h"

#include "../ncnn_ops_definer.h"

namespace mmdeploy {
using namespace ncnn;
DEFINE_LAYER_CREATOR(Shape)
DEFINE_NCNN_OPS(Shape, Shape)
Shape::Shape() {
  one_blob_only = true;
  support_inplace = false;
}

int Shape::forward(const Mat &bottom_blob, Mat &top_blob, const Option &opt) const {
  int dims = bottom_blob.dims;
  int w = bottom_blob.w;
  size_t elemsize = sizeof(float);
  top_blob.create(dims + 1, elemsize, opt.blob_allocator);
  if (top_blob.empty()) {
    return -100;
  }
  float *outptr = top_blob;

  if (dims == 1) {
    outptr[0] = 1.0f;
    outptr[1] = w;
  } else if (dims == 2) {
    int h = bottom_blob.h;
    outptr[0] = 1.0f;
    outptr[1] = h;
    outptr[2] = w;
  } else if (dims == 3) {
    int h = bottom_blob.h;
    int channels = bottom_blob.c;
    outptr[0] = 1.0f;
    outptr[1] = channels;
    outptr[2] = h;
    outptr[3] = w;
  } else {
    fprintf(stdout, "Unsupported dims=%d\n", dims);
  }

  return 0;
}

}  // namespace mmdeploy