2using System.Collections.Generic;
5using System.Threading.Tasks;
16 public List<float> mMask;
17 public List<float> mIoU;
18 public List<float> mStability;
19 public List<int> mBox;
21 public List<List<float>> mfinalMask;
24 this.mShape =
new int[4];
25 this.mMask =
new List<float>();
26 this.mIoU =
new List<float>();
27 this.mStability =
new List<float>();
28 this.mBox =
new List<int>();
30 this.mfinalMask =
new List<List<float>>();
33 public void Filter(
float pred_iou_thresh,
float stability_score_thresh)
35 List<float> m =
new List<float>();
36 List<float> i =
new List<float>();
37 List<float> s =
new List<float>();
39 for (
int j = 0; j < this.mShape[1]; j++)
41 if (this.mIoU[j] > pred_iou_thresh && this.mStability[j]> stability_score_thresh)
43 this.mfinalMask.Add(this.mMask.GetRange(j *
this.mShape[2] *
this.mShape[3],
this.mShape[2] *
this.mShape[3]));
46 s.Add(this.mStability[j]);
50 this.mShape[1] = batch;
51 this.mStability.Clear();
55 this.mIoU.AddRange(i);
56 this.mStability.AddRange(s);
59 public float[] CalculateStabilityScore(
float maskThreshold,
float thresholdOffset)
61 int batchSize = this.mShape[1];
62 int width = this.mShape[3];
63 int height = this.mShape[2];
65 float[] intersections =
new float[batchSize];
66 float[] unions =
new float[batchSize];
68 for (
int i = 0; i < batchSize; i++)
70 float intersectionSum = 0;
73 for (
int j = 0; j < width; j++)
75 for (
int k = 0; k < height; k++)
77 int index = i * width * height + k * width + j;
78 if (this.mMask[index] > maskThreshold + thresholdOffset)
82 if (this.mMask[index] > maskThreshold - thresholdOffset)
89 intersections[i] = intersectionSum;
93 float[] stabilityScores =
new float[batchSize];
94 for (
int i = 0; i < batchSize; i++)
96 stabilityScores[i] = intersections[i] / unions[i];
99 return stabilityScores;
104 this.mShape[0] = md.mShape[0];
105 this.mShape[1] += md.mShape[1];
106 this.mShape[2] = md.mShape[2];
107 this.mShape[3] = md.mShape[3];
108 this.mBox.AddRange(md.mBox);
109 this.mMask.AddRange(md.mMask);
110 this.mStability.AddRange(md.mStability);
111 this.mIoU.AddRange(md.mIoU);
112 this.mfinalMask.AddRange(md.mfinalMask);
115 public int[] batched_mask_to_box()
117 int C = this.mShape[1];
118 int width = this.mShape[3];
119 int height = this.mShape[2];
121 int[] boxes =
new int[C*4];
123 for (
int c = 0; c < C; c++)
125 bool emptyMask =
true;
131 for (
int i = 0; i < width; i++)
133 for (
int j = 0; j < height; j++)
137 int index =j * width + i;
138 if (this.mfinalMask[c][index] > 0)
141 top = Math.Min(top, j);
142 bottom = Math.Max(bottom, j);
143 left = Math.Min(left, i);
144 right = Math.Max(right, i);
159 boxes[c * 4 + 1] = top;
160 boxes[c * 4 + 2] = right;
161 boxes[c * 4 + 3] = bottom;
168 public void Dispose()
181 class MicroMaskData : IDisposable
184 public List<float> mMask;
185 public List<float> mIoU;
186 public List<List<float>> mfinalMask;
187 public MicroMaskData()
189 this.mMask = new List<float>();
190 this.mIoU = new List<float>();
193 public void Filter(float pred_iou_thresh, float stability_score_thresh)
195 List<float> m = new List<float>();
196 List<float> i = new List<float>();
197 List<float> s = new List<float>();
199 for (int j = 0; j < this.mShape[1]; j++)
201 if (this.mIoU[j] > pred_iou_thresh)
203 this.mfinalMask.Add(this.mMask.GetRange(j * this.mShape[2] * this.mShape[3], this.mShape[2] * this.mShape[3]));
204 //m.AddRange(this.mMask.GetRange(j * this.mShape[2] * this.mShape[3], this.mShape[2] * this.mShape[3]));
209 this.mShape[1] = batch;
212 //this.mMask.AddRange(m);
213 this.mIoU.AddRange(i);
217 public float[] CalculateStabilityScore(float maskThreshold, float thresholdOffset)
219 int batchSize = this.mShape[1];
220 int width = this.mShape[3];
221 int height = this.mShape[2];
223 float[] intersections = new float[batchSize];
224 float[] unions = new float[batchSize];
226 for (int i = 0; i < batchSize; i++)
228 float intersectionSum = 0;
231 for (int j = 0; j < width; j++)
233 for (int k = 0; k < height; k++)
235 int index = i * width * height + k * width + j;
236 if (this.mMask[index] > maskThreshold + thresholdOffset)
240 if (this.mMask[index] > maskThreshold - thresholdOffset)
247 intersections[i] = intersectionSum;
248 unions[i] = unionSum;
251 float[] stabilityScores = new float[batchSize];
252 for (int i = 0; i < batchSize; i++)
254 stabilityScores[i] = intersections[i] / unions[i];
257 return stabilityScores;
260 public void Cat(MaskData md)
262 this.mShape[0] = md.mShape[0];
263 this.mShape[1] += md.mShape[1];
264 this.mShape[2] = md.mShape[2];
265 this.mShape[3] = md.mShape[3];
266 this.mMask.AddRange(md.mMask);
267 this.mIoU.AddRange(md.mIoU);
268 this.mfinalMask.AddRange(md.mfinalMask);
271 public int[] batched_mask_to_box()
273 int C = this.mShape[1];
274 int width = this.mShape[3];
275 int height = this.mShape[2];
277 int[] boxes = new int[C * 4];
279 for (int c = 0; c < C; c++)
281 bool emptyMask = true;
287 for (int i = 0; i < width; i++)
289 for (int j = 0; j < height; j++)
291 //int index = c * width * height + j * width + i;
292 //if (this.mMask[index] > 0)
293 int index = j * width + i;
294 if (this.mfinalMask[c][index] > 0)
297 top = Math.Min(top, j);
298 bottom = Math.Max(bottom, j);
299 left = Math.Min(left, i);
300 right = Math.Max(right, i);
308 boxes[c * 4 + 1] = 0;
309 boxes[c * 4 + 2] = 0;
310 boxes[c * 4 + 3] = 0;
315 boxes[c * 4 + 1] = top;
316 boxes[c * 4 + 2] = right;
317 boxes[c * 4 + 3] = bottom;
324 public void Dispose()
A structure for storing masks and their related data in batched format. Implements basic filtering an...