2using System.Collections.Generic;
24 List<float> m_rgfMinSizes =
new List<float>();
25 List<float> m_rgfMaxSizes =
new List<float>();
26 List<float> m_rgfAspectRatios =
new List<float>();
30 List<float> m_rgfVariance =
new List<float>();
87 for (
int i = 0; i < p.
min_size.Count; i++)
90 m_log.
CHECK_GT(fMin, 0,
"min_size must be positive greater than zero.");
91 m_rgfMinSizes.Add(fMin);
96 m_nNumPriors = m_rgfAspectRatios.Count * m_rgfMinSizes.Count;
101 for (
int i = 0; i < p.
max_size.Count; i++)
104 m_log.
CHECK_GT(fMax, m_rgfMinSizes[i],
"The max_size must be greater than the min_size.");
105 m_rgfMaxSizes.Add(fMax);
117 for (
int i = 0; i < p.
variance.Count; i++)
120 m_log.
CHECK_GT(fVar, 0,
"The variance values must be greater than zero.");
121 m_rgfVariance.Add(fVar);
127 m_log.
CHECK_GT(fVar, 0,
"The variance value must be greater than zero.");
128 m_rgfVariance.Add(fVar);
133 m_rgfVariance.Add(0.1f);
138 m_log.
CHECK(!p.
img_size.HasValue,
"Either img_size or img_h/img_w should be specified; but not both.");
139 m_nImgH = (int)p.
img_h.Value;
140 m_log.
CHECK_GT(m_nImgH, 0,
"The img_h should be greater than 0.");
141 m_nImgW = (int)p.
img_w.Value;
142 m_log.
CHECK_GT(m_nImgW, 0,
"The img_w should be greater than 0.");
146 int nImgSize = (int)p.
img_size.Value;
147 m_log.
CHECK_GT(nImgSize, 0,
"The img_size should be greater than 0.");
159 m_log.
CHECK(!p.
step.HasValue,
"Either step_size or step_h/step_w should be specified; but not both.");
160 m_fStepH = p.
step_h.Value;
161 m_log.
CHECK_GT(m_nImgH, 0,
"The step_h should be greater than 0.");
162 m_fStepW = p.
step_w.Value;
163 m_log.
CHECK_GT(m_nImgW, 0,
"The step_w should be greater than 0.");
165 else if (p.
step.HasValue)
167 float fStep = p.
step.Value;
168 m_log.
CHECK_GT(fStep, 0,
"The step should be greater than 0.");
192 m_log.
CHECK_EQ(rgTopShape[0], 1,
"The topshape(0) should be 1.");
197 m_log.
CHECK_EQ(rgTopShape[1], 2,
"The topshape(1) should be 1.");
198 m_log.
CHECK_GT(rgTopShape[2], 0,
"The top shape at index 2 must be greater than zero.");
217 int nLayerW = colBottom[0].width;
218 int nLayerH = colBottom[0].height;
222 if (m_nImgW == 0 || m_nImgH == 0)
224 nImgW = colBottom[1].width;
225 nImgH = colBottom[1].height;
236 if (m_fStepW == 0 || m_fStepH == 0)
238 fStepW = (float)nImgW / (
float)nLayerW;
239 fStepH = (float)nImgH / (
float)nLayerH;
247 float[] rgfTopData =
Utility.ConvertVecF<T>(colTop[0].mutable_cpu_data);
248 int nDim = nLayerH * nLayerW * m_nNumPriors * 4;
251 for (
int h = 0; h < nLayerH; h++)
253 for (
int w = 0; w < nLayerW; w++)
255 float fCenterX = (w + m_fOffset) * fStepW;
256 float fCenterY = (h + m_fOffset) * fStepH;
260 for (
int s = 0; s < m_rgfMinSizes.Count; s++)
262 int nMinSize = (int)m_rgfMinSizes[s];
265 fBoxHeight = nMinSize;
266 fBoxWidth = nMinSize;
268 rgfTopData[nIdx] = (fCenterX - fBoxWidth / 2.0f) / nImgW;
271 rgfTopData[nIdx] = (fCenterY - fBoxHeight / 2.0f) / nImgH;
274 rgfTopData[nIdx] = (fCenterX + fBoxWidth / 2.0f) / nImgW;
277 rgfTopData[nIdx] = (fCenterY + fBoxHeight / 2.0f) / nImgH;
280 if (m_rgfMaxSizes.Count > 0)
282 m_log.
CHECK_EQ(m_rgfMinSizes.Count, m_rgfMaxSizes.Count,
"The max_sizes and min_sizes must have the same count.");
283 int nMaxSize = (int)m_rgfMaxSizes[s];
286 fBoxWidth = (float)Math.Sqrt(nMinSize * nMaxSize);
287 fBoxHeight = fBoxWidth;
289 rgfTopData[nIdx] = (fCenterX - fBoxWidth / 2.0f) / nImgW;
292 rgfTopData[nIdx] = (fCenterY - fBoxHeight / 2.0f) / nImgH;
295 rgfTopData[nIdx] = (fCenterX + fBoxWidth / 2.0f) / nImgW;
298 rgfTopData[nIdx] = (fCenterY + fBoxHeight / 2.0f) / nImgH;
303 for (
int r = 0; r < m_rgfAspectRatios.Count; r++)
305 float fAr = m_rgfAspectRatios[r];
307 if (Math.Abs(fAr - 1.0f) < 1e-6f)
310 fBoxWidth = (float)(nMinSize * Math.Sqrt(fAr));
311 fBoxHeight = (float)(nMinSize / Math.Sqrt(fAr));
313 rgfTopData[nIdx] = (fCenterX - fBoxWidth / 2.0f) / nImgW;
316 rgfTopData[nIdx] = (fCenterY - fBoxHeight / 2.0f) / nImgH;
319 rgfTopData[nIdx] = (fCenterX + fBoxWidth / 2.0f) / nImgW;
322 rgfTopData[nIdx] = (fCenterY + fBoxHeight / 2.0f) / nImgH;
332 for (
int d = 0; d < nDim; d++)
334 rgfTopData[d] = Math.Min(Math.Max(rgfTopData[d], 0.0f), 1.0f);
339 int nTopOffset = colTop[0].offset(0, 1);
341 if (m_rgfVariance.Count > 1)
344 for (
int h = 0; h < nLayerH; h++)
346 for (
int w = 0; w < nLayerW; w++)
348 for (
int i = 0; i < m_nNumPriors; i++)
350 for (
int j = 0; j < 4; j++)
352 rgfTopData[nTopOffset + nCount] = m_rgfVariance[j];
362 if (m_rgfVariance.Count == 1)
363 colTop[0].
SetData(m_rgfVariance[0], nTopOffset, nDim);
369 new NotImplementedException();
The Log class provides general output in text form.
void CHECK(bool b, string str)
Test a flag for true.
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
void CHECK_GT(double df1, double df2, string str)
Test whether one number is greater than another.
The Utility class provides general utility funtions.
static double[] ConvertVec(float[] rgf)
Convert an array of float to an array of generics.
The BlobCollection contains a list of Blobs.
void SetData(double df)
Set all blob data to the value specified.
void Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
An interface for the units of computation which can be composed into a Net.
Log m_log
Specifies the Log for output.
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
LayerParameter.LayerType m_type
Specifies the Layer type.
The PriorBoxLayer generates prior boxes of designated sizes and aspect ratios across all dimensions o...
override void dispose()
Releases all GPU and host resources used by the Layer.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: data
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: permute
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Not implemented.
PriorBoxLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The PriorBoxLayer constructor.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Generates prior boxes for a layer with specified parameters.
Specifies the base parameter for all layers.
PriorBoxParameter prior_box_param
Returns the parameter set when initialized with LayerType.PRIORBOX
LayerType
Specifies the layer type.
Specifies the parameters for the PriorBoxParameter.
float offset
Specifies the offset to the top left corner of each cell.
uint? img_size
Specifies the image size. By default we calculate the img_height, img_width, step_x and step_y based ...
uint? img_h
Specifies the image height. By default we calculate the img_height, img_width, step_x and step_y base...
static List< int > Reshape(PriorBoxParameter p, int nLayerWid, int nLayerHt, int? nNumPriors=null)
Calculate the reshape size based on the parameters.
List< float > variance
Specifies the variance for adjusting the prior boxes.
static List< float > GetAspectRatios(PriorBoxParameter p)
Return the list of aspect ratios to use based on the parameters.
List< float > max_size
Specifies the maximum box size (in pixels) and is required!
bool flip
Specifies whether or not to flip each aspect ratio. For example, if there is an aspect ratio 'r' we w...
float? step_w
Specifies the explicit step size to use along width.
uint? img_w
Specifies the image width. By default we calculate the img_height, img_width, step_x and step_y based...
float? step
Specifies the excplicit step size to use.
float? step_h
Specifies the explicit step size to use along height.
bool clip
Specifies whether or not to clip the prior so that it is within [0,1].
List< float > min_size
Specifies the minimum box size (in pixels) and is required!
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.common namespace contains common MyCaffe classes.
The MyCaffe.fillers namespace contains all fillers including the Filler class.
The MyCaffe.layers.ssd namespace contains all Single-Shot MultiBox (SSD) related layers.
The MyCaffe.param.ssd namespace contains all SSD related parameter objects that correspond to the nat...
The MyCaffe.param namespace contains parameters used to create models.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...