Skip to content

Commit 98b0228

Browse files
irexyclvhan028
authored andcommitted
[Feature] Support feature map output for mmsegmentation (#1625)
* add feature map output for mmseg * update api * update demo * fix return * update format_shape * fix lint * update csharp demo * update python demo && api * fix coreml build * fix lint * better sort * update * update cpp demo & add missing header * change to CHW * update csharp api * update isort version to 5.12.0 * fix python api * fix log * more detail api docs * isort support python3.7 * remove isort change * remove whitespace * axes check * remove FormatShapeImpl * minor * add permute tc * remove stride buffer (cherry picked from commit b85f341)
1 parent a22a38b commit 98b0228

File tree

30 files changed

+665
-329
lines changed

30 files changed

+665
-329
lines changed

csrc/mmdeploy/apis/c/mmdeploy/segmentor.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,17 @@ int mmdeploy_segmentor_get_result(mmdeploy_value_t output, mmdeploy_segmentation
119119
results_ptr->height = segmentor_output.height;
120120
results_ptr->width = segmentor_output.width;
121121
results_ptr->classes = segmentor_output.classes;
122-
auto mask_size = results_ptr->height * results_ptr->width;
123122
auto& mask = segmentor_output.mask;
124-
results_ptr->mask = mask.data<int>();
125-
buffers[i] = mask.buffer();
123+
auto& score = segmentor_output.score;
124+
results_ptr->mask = nullptr;
125+
results_ptr->score = nullptr;
126+
if (mask.shape().size()) {
127+
results_ptr->mask = mask.data<int>();
128+
buffers[i] = mask.buffer();
129+
} else {
130+
results_ptr->score = score.data<float>();
131+
buffers[i] = score.buffer();
132+
}
126133
}
127134

128135
*results = results_data;

csrc/mmdeploy/apis/c/mmdeploy/segmentor.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ extern "C" {
1717
#endif
1818

1919
typedef struct mmdeploy_segmentation_t {
20-
int height; ///< height of \p mask that equals to the input image's height
21-
int width; ///< width of \p mask that equals to the input image's width
22-
int classes; ///< the number of labels in \p mask
23-
int* mask; ///< segmentation mask of the input image, in which mask[i * width + j] indicates
24-
///< the label id of pixel at (i, j)
20+
int height; ///< height of \p mask that equals to the input image's height
21+
int width; ///< width of \p mask that equals to the input image's width
22+
int classes; ///< the number of labels in \p mask
23+
int* mask; ///< segmentation mask of the input image, in which mask[i * width + j] indicates
24+
///< the label id of pixel at (i, j), this field might be null
25+
float* score; ///< segmentation score map of the input image in CHW format, in which
26+
///< score[height * width * k + i * width + j] indicates the score
27+
///< of class k at pixel (i, j), this field might be null
2528
} mmdeploy_segmentation_t;
2629

2730
typedef struct mmdeploy_segmentor* mmdeploy_segmentor_t;

csrc/mmdeploy/apis/csharp/MMDeploy/APIs/Segmentor.cs

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ internal unsafe struct CSegment
1010
public int Width;
1111
public int Classes;
1212
public int* Mask;
13+
public float* Score;
1314
}
1415
#pragma warning restore 0649
1516

@@ -34,36 +35,83 @@ public struct SegmentorOutput
3435
public int Classes;
3536

3637
/// <summary>
37-
/// Mask data.
38+
/// Mask data, mask[i * width + j] indicates the label id of pixel at (i, j).
3839
/// </summary>
3940
public int[] Mask;
4041

42+
/// <summary>
43+
/// Score data, score[height * width * k + i * width + j] indicates the score
44+
/// of class k at pixel (i, j).
45+
/// </summary>
46+
public float[] Score;
47+
4148
/// <summary>
4249
/// Initializes a new instance of the <see cref="SegmentorOutput"/> struct.
4350
/// </summary>
4451
/// <param name="height">height.</param>
4552
/// <param name="width">width.</param>
4653
/// <param name="classes">classes.</param>
4754
/// <param name="mask">mask.</param>
48-
public SegmentorOutput(int height, int width, int classes, int[] mask)
55+
/// <param name="score">score.</param>
56+
public SegmentorOutput(int height, int width, int classes, int[] mask, float[] score)
4957
{
5058
Height = height;
5159
Width = width;
5260
Classes = classes;
53-
Mask = new int[Height * Width];
54-
Array.Copy(mask, this.Mask, mask.Length);
61+
if (mask.Length > 0)
62+
{
63+
Mask = new int[Height * Width];
64+
Array.Copy(mask, this.Mask, mask.Length);
65+
}
66+
else
67+
{
68+
Mask = new int[] { };
69+
}
70+
71+
if (score.Length > 0)
72+
{
73+
Score = new float[Height * Width * Classes];
74+
Array.Copy(score, this.Score, score.Length);
75+
}
76+
else
77+
{
78+
Score = new float[] { };
79+
}
5580
}
5681

5782
internal unsafe SegmentorOutput(CSegment* result)
5883
{
5984
Height = result->Height;
6085
Width = result->Width;
6186
Classes = result->Classes;
62-
Mask = new int[Height * Width];
63-
int nbytes = Height * Width * sizeof(int);
64-
fixed (int* data = this.Mask)
87+
if (result->Mask != null)
88+
{
89+
Mask = new int[Height * Width];
90+
91+
int nbytes = Height * Width * sizeof(int);
92+
fixed (int* data = this.Mask)
93+
{
94+
Buffer.MemoryCopy(result->Mask, data, nbytes, nbytes);
95+
}
96+
}
97+
else
98+
{
99+
Mask = new int[] { };
100+
}
101+
102+
if (result->Score != null)
103+
{
104+
Score = new float[Height * Width * Classes];
105+
106+
int nbytes = Height * Width * Classes * sizeof(float);
107+
fixed (float* data = this.Score)
108+
{
109+
Buffer.MemoryCopy(result->Score, data, nbytes, nbytes);
110+
}
111+
}
112+
else
65113
{
66-
Buffer.MemoryCopy(result->Mask, data, nbytes, nbytes);
114+
Score = new float[] { };
67115
}
68116
}
69117
}

csrc/mmdeploy/apis/python/segmentor.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,22 @@ class PySegmentor {
3737

3838
std::vector<py::array> rets(mats.size());
3939
for (size_t i = 0; i < mats.size(); ++i) {
40-
rets[i] = {
41-
{segm[i].height, segm[i].width}, // shape
42-
segm[i].mask, // data
43-
py::capsule(new Sptr(holder), // handle
44-
[](void* p) { delete reinterpret_cast<Sptr*>(p); }) //
45-
};
40+
if (segm[i].mask != nullptr) {
41+
rets[i] = {
42+
{segm[i].height, segm[i].width}, // shape
43+
segm[i].mask, // mask
44+
py::capsule(new Sptr(holder), // handle
45+
[](void* p) { delete reinterpret_cast<Sptr*>(p); }) //
46+
};
47+
}
48+
if (segm[i].score != nullptr) {
49+
rets[i] = {
50+
{segm[i].classes, segm[i].height, segm[i].width}, // shape
51+
segm[i].score, // score
52+
py::capsule(new Sptr(holder), // handle
53+
[](void* p) { delete reinterpret_cast<Sptr*>(p); }) //
54+
};
55+
}
4656
}
4757
return rets;
4858
}

csrc/mmdeploy/codebase/mmaction/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ project(mmdeploy_mmaction)
44

55
file(GLOB SRCS ${CMAKE_CURRENT_SOURCE_DIR} "*.cpp")
66
mmdeploy_add_module(${PROJECT_NAME} "${SRCS}")
7-
add_subdirectory(cpu)
8-
add_subdirectory(cuda)
7+
98
target_link_libraries(${PROJECT_NAME} PRIVATE
109
mmdeploy_operation
1110
mmdeploy_transform

csrc/mmdeploy/codebase/mmaction/cpu/CMakeLists.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

csrc/mmdeploy/codebase/mmaction/cpu/format_shape_impl.cpp

Lines changed: 0 additions & 67 deletions
This file was deleted.

csrc/mmdeploy/codebase/mmaction/cuda/CMakeLists.txt

Lines changed: 0 additions & 18 deletions
This file was deleted.

csrc/mmdeploy/codebase/mmaction/cuda/format_shape_impl.cpp

Lines changed: 0 additions & 66 deletions
This file was deleted.

csrc/mmdeploy/codebase/mmaction/cuda/transpose.cu

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)