2using System.Collections.Generic;
61 bool m_bShareLocation;
62 int m_nBackgroundLabelId;
63 bool m_bUseDifficultGt;
75 List<DictionaryMap<List<int>>> m_rgAllMatchIndices =
new List<DictionaryMap<List<int>>>();
76 List<List<int>> m_rgrgAllNegIndices =
new List<List<int>>();
93 m_blobLocPred =
new Blob<T>(cuda, log);
95 m_blobLocGt =
new Blob<T>(cuda, log);
97 m_blobLocLoss =
new Blob<T>(cuda, log);
100 m_blobConfPred =
new Blob<T>(cuda, log);
102 m_blobConfGt =
new Blob<T>(cuda, log);
104 m_blobConfLoss =
new Blob<T>(cuda, log);
142 if (m_locLossLayer !=
null)
144 m_locLossLayer.Dispose();
145 m_locLossLayer =
null;
148 if (m_confLossLayer !=
null)
151 m_confLossLayer =
null;
154 if (m_bboxUtil !=
null)
160 if (m_hostConf !=
null)
166 if (m_hostLoc !=
null)
172 if (m_hostGt !=
null)
178 if (m_hostPrio !=
null)
193 col.
Add(m_blobConfPred);
194 col.
Add(m_blobLocGt);
195 col.
Add(m_blobLocLoss);
196 col.
Add(m_blobConfPred);
197 col.
Add(m_blobConfGt);
198 col.
Add(m_blobConfLoss);
230 base.LayerSetUp(colBottom, colTop);
240 m_nNum = colBottom[0].num;
241 m_nNumPriors = colBottom[2].height / 4;
245 m_log.
CHECK_GE(m_nNumClasses, 1,
"The num_classes should not be less than 1.");
248 m_nLocClasses = (m_bShareLocation) ? 1 : m_nNumClasses;
256 m_log.
WriteLine(
"WARNING: do_neg_mining is depreciated, use mining_type instead.");
264 m_log.
CHECK(m_bShareLocation,
"Currently only support negative mining if share_location is true.");
272 m_blobLocPred.
Reshape(rgLocShape);
273 m_blobLocGt.
Reshape(rgLocShape);
274 m_colLocBottom.
Add(m_blobLocPred);
275 m_colLocBottom.
Add(m_blobLocGt);
278 m_blobLocLoss.
Reshape(rgLossShape);
279 m_colLocTop.
Add(m_blobLocLoss);
287 m_locLossLayer.Setup(m_colLocBottom, m_colLocTop);
292 p.
name +=
"_smooth_l1_loc";
295 m_locLossLayer.Setup(m_colLocBottom, m_colLocTop);
299 m_log.
FAIL(
"Unknown localization loss type.");
304 m_colConfBottom.
Add(m_blobConfPred);
305 m_colConfBottom.
Add(m_blobConfGt);
306 m_blobConfLoss.
Reshape(rgLossShape);
307 m_colConfTop.
Add(m_blobConfLoss);
313 m_log.
CHECK_GE(m_nBackgroundLabelId, 0,
"The background_label_id should be within [0, num_classes) for Softmax.");
314 m_log.
CHECK_LT(m_nBackgroundLabelId, m_nNumClasses,
"The background_label_id should be within [0, num_classes) for Softmax.");
316 p.
name +=
"_softmax_conf";
322 m_blobConfGt.
Reshape(rgConfShape);
323 rgConfShape.Add(m_nNumClasses);
324 m_blobConfPred.
Reshape(rgConfShape);
326 m_confLossLayer.
Setup(m_colConfBottom, m_colConfTop);
331 p.
name +=
"_logistic_conf";
335 rgConfShape.Add(m_nNumClasses);
336 m_blobConfGt.
Reshape(rgConfShape);
337 m_blobConfPred.
Reshape(rgConfShape);
339 m_confLossLayer.
Setup(m_colConfBottom, m_colConfTop);
343 m_log.
FAIL(
"Unknown confidence loss type.");
349 float? fNmsThreshold =
null;
350 int? nNmsTopK =
null;
351 float? fNmsEta =
null;
352 bool bNmsParam =
false;
362 m_hSsd =
m_cuda.CreateSSD(m_nNumClasses,
365 m_nBackgroundLabelId,
387 throw new Exception(
"Could not create the SSD!");
398 base.Reshape(colBottom, colTop);
400 m_nNum = colBottom[0].num;
401 m_nNumPriors = colBottom[2].height / 4;
402 m_nNumGt = colBottom[3].height;
405 m_cuda.SetupSSD(m_hSsd, m_nNum, m_nNumPriors, m_nNumGt);
407 m_log.
CHECK_EQ(colBottom[0].num, colBottom[1].num,
"The bottom[0] and bottom[1] num must be equal.");
408 m_log.
CHECK_EQ(m_nNumPriors * m_nLocClasses * 4, colBottom[0].channels,
"The number of priors must match the number of location predictions.");
409 m_log.
CHECK_EQ(m_nNumPriors * m_nNumClasses, colBottom[1].channels,
"The number of priors must match the number of confidence predictions.");
423 int nLocDataCount = colBottom[0].count();
424 long hLocGpuData = colBottom[0].gpu_data;
425 int nConfDataCount = colBottom[1].count();
426 long hConfGpuData = colBottom[1].gpu_data;
427 int nPriorDataCount = colBottom[2].count();
428 long hPriorGpuData = colBottom[2].gpu_data;
429 int nGtDataCount = colBottom[3].count();
430 long hGtGpuData = colBottom[3].gpu_data;
433 m_nNumMatches =
m_cuda.SsdMultiBoxLossForward(m_hSsd, nLocDataCount, hLocGpuData, nConfDataCount, hConfGpuData, nPriorDataCount, hPriorGpuData, nGtDataCount, hGtGpuData, out m_rgAllMatchIndices, out m_rgrgAllNegIndices, out nNumNegs);
460 if (m_nNumMatches >= 1)
463 List<int> rgLocShape =
new List<int>() { 1, m_nNumMatches * 4 };
464 m_blobLocPred.
Reshape(rgLocShape);
465 m_blobLocGt.
Reshape(rgLocShape);
471 m_locLossLayer.Reshape(m_colLocBottom, m_colLocTop);
472 m_locLossLayer.Forward(m_colLocBottom, m_colLocTop);
481 m_nNumConf = m_nNumMatches + nNumNegs;
483 m_nNumConf = m_nNum * m_nNumPriors;
488 List<int> rgConfShape =
new List<int>();
492 rgConfShape.Add(m_nNumConf);
493 m_blobConfGt.
Reshape(rgConfShape);
494 rgConfShape.Add(m_nNumClasses);
495 m_blobConfPred.
Reshape(rgConfShape);
500 rgConfShape.Add(m_nNumConf);
501 rgConfShape.Add(m_nNumClasses);
502 m_blobConfGt.
Reshape(rgConfShape);
503 m_blobConfPred.
Reshape(rgConfShape);
507 m_log.
FAIL(
"Unknown confidence loss type.");
514 m_log.
CHECK_EQ(m_blobConfPred.
count(), colBottom[1].count(),
"The conf pred and bottom[1] should have the same count.");
518 m_blobConfGt.
SetData(m_nBackgroundLabelId);
525 m_confLossLayer.
Reshape(m_colConfBottom, m_colConfTop);
526 m_confLossLayer.
Forward(m_colConfBottom, m_colConfTop);
538 double dfLocLoss =
Utility.ConvertVal<T>(m_blobLocLoss.
GetData(0));
539 dfLoss += m_fLocWeight * dfLocLoss / dfNormalizer;
544 double dfConfLoss =
Utility.ConvertVal<T>(m_blobConfLoss.
GetData(0));
545 dfLoss += dfConfLoss / dfNormalizer;
565 DictionaryMap<List<NormalizedBBox>> rgAllGtBboxes = m_bboxUtil.GetGroundTruth(rgfGtData, m_nNumGt, m_nBackgroundLabelId, m_bUseDifficultGt);
569 List<List<float>> rgrgPriorVariances;
572 List<NormalizedBBox> rgPriorBboxes = m_bboxUtil.GetPrior(rgfPriorData, m_nNumPriors, out rgrgPriorVariances);
577 List<LabelBBox> rgAllLocPreds = m_bboxUtil.GetLocPredictions(rgfLocData, m_nNum, m_nNumPriors, m_nLocClasses, m_bShareLocation);
580 List<DictionaryMap<List<float>>> rgAllMatchOverlaps;
581 m_bboxUtil.FindMatches(rgAllLocPreds, rgAllGtBboxes, rgPriorBboxes, rgrgPriorVariances,
m_param.
multiboxloss_param, out rgAllMatchOverlaps, out m_rgAllMatchIndices);
585 m_nNumMatches = m_bboxUtil.MineHardExamples(colBottom[1], rgAllLocPreds, rgAllGtBboxes, rgPriorBboxes, rgrgPriorVariances, rgAllMatchOverlaps,
m_param.
multiboxloss_param, m_rgAllMatchIndices, m_rgrgAllNegIndices, out nNumNegs);
587 if (m_nNumMatches >= 1)
590 List<int> rgLocShape =
new List<int>() { 1, m_nNumMatches * 4 };
591 m_blobLocPred.
Reshape(rgLocShape);
592 m_blobLocGt.
Reshape(rgLocShape);
594 m_bboxUtil.EncodeLocPrediction(rgAllLocPreds, rgAllGtBboxes, m_rgAllMatchIndices, rgPriorBboxes, rgrgPriorVariances,
m_param.
multiboxloss_param, m_blobLocPred, m_blobLocGt);
596 m_locLossLayer.Reshape(m_colLocBottom, m_colLocTop);
597 m_locLossLayer.Forward(m_colLocBottom, m_colLocTop);
606 m_nNumConf = m_nNumMatches + nNumNegs;
608 m_nNumConf = m_nNum * m_nNumPriors;
613 List<int> rgConfShape =
new List<int>();
617 rgConfShape.Add(m_nNumConf);
618 m_blobConfGt.
Reshape(rgConfShape);
619 rgConfShape.Add(m_nNumClasses);
620 m_blobConfPred.
Reshape(rgConfShape);
625 rgConfShape.Add(m_nNumConf);
626 rgConfShape.Add(m_nNumClasses);
627 m_blobConfGt.
Reshape(rgConfShape);
628 m_blobConfPred.
Reshape(rgConfShape);
632 m_log.
FAIL(
"Unknown confidence loss type.");
639 m_log.
CHECK_EQ(m_blobConfPred.
count(), colBottom[1].count(),
"The conf pred and bottom[1] should have the same count.");
643 m_blobConfGt.
SetData(m_nBackgroundLabelId);
647 m_bboxUtil.EncodeConfPrediction(rgfConfData, m_nNum, m_nNumPriors,
m_param.
multiboxloss_param, m_rgAllMatchIndices, m_rgrgAllNegIndices, rgAllGtBboxes, m_blobConfPred, m_blobConfGt);
648 m_confLossLayer.
Reshape(m_colConfBottom, m_colConfTop);
649 m_confLossLayer.
Forward(m_colConfBottom, m_colConfTop);
661 double dfLocLoss =
Utility.ConvertVal<T>(m_blobLocLoss.
GetData(0));
662 dfLoss += m_fLocWeight * dfLocLoss / dfNormalizer;
667 double dfConfLoss =
Utility.ConvertVal<T>(m_blobConfLoss.
GetData(0));
668 dfLoss += dfConfLoss / dfNormalizer;
706 if (rgbPropagateDown[2])
709 if (rgbPropagateDown[3])
713 if (rgbPropagateDown[0])
717 if (m_nNumMatches >= 1)
719 int nLocBottomDiffOffset = 0;
720 float[] rgfLocBottomDiff =
Utility.ConvertVecF<T>(colBottom[0].mutable_cpu_diff);
721 List<bool> rgLocPropagateDown =
new List<bool>();
724 rgLocPropagateDown.Add(
true);
725 rgLocPropagateDown.Add(
false);
726 m_locLossLayer.Backward(m_colLocTop, rgLocPropagateDown, m_colLocBottom);
730 double dfLossWeight =
Utility.ConvertVal<T>(colTop[0].GetDiff(0)) / dfNormalizer;
737 for (
int i = 0; i < m_nNum; i++)
739 DictionaryMap<List<int>> rgMap = m_rgAllMatchIndices[i];
741 foreach (KeyValuePair<
int, List<int>> kv
in rgMap.Map)
743 int nLabel = (m_bShareLocation) ? 0 : kv.Key;
744 List<int> rgMatchIndex = kv.Value;
746 for (
int j = 0; j < rgMatchIndex.Count; j++)
748 if (rgMatchIndex[j] <= -1)
752 int nStartIdx = m_nLocClasses * 4 * j + nLabel * 4;
753 Array.Copy(rgfLocPredDiff, nCount * 4, rgfLocBottomDiff, nLocBottomDiffOffset + nStartIdx, 4);
758 nLocBottomDiffOffset += colBottom[0].offset(1);
766 if (rgbPropagateDown[1])
772 int nConfBottomDiffOffset = 0;
773 float[] rgfConfBottomDiff =
Utility.ConvertVecF<T>(colBottom[1].mutable_cpu_diff);
774 List<bool> rgConfPropagateDown =
new List<bool>();
777 rgConfPropagateDown.Add(
true);
778 rgConfPropagateDown.Add(
false);
779 m_confLossLayer.
Backward(m_colConfTop, rgConfPropagateDown, m_colConfBottom);
783 double dfLossWeight =
Utility.ConvertVal<T>(colTop[0].GetDiff(0)) / dfNormalizer;
792 for (
int i = 0; i < m_nNum; i++)
795 Dictionary<int, List<int>> rgMap = m_rgAllMatchIndices[i].Map;
797 foreach (KeyValuePair<
int, List<int>> kv
in rgMap)
799 List<int> rgMatchIndex = kv.Value;
800 m_log.
CHECK_EQ(rgMatchIndex.Count, m_nNumPriors,
"The match index count should equal the num priors!");
802 for (
int j = 0; j < m_nNumPriors; j++)
804 if (rgMatchIndex[j] <= -1)
808 Array.Copy(rgfConfPredDiff, nCount * m_nNumClasses, rgfConfBottomDiff, nConfBottomDiffOffset + j * m_nNumClasses, m_nNumClasses);
814 for (
int n = 0; n < m_rgrgAllNegIndices[i].Count; n++)
816 int j = m_rgrgAllNegIndices[i][n];
817 m_log.
CHECK_LT(j, m_nNumPriors,
"The index must be less than the num priors!");
819 Array.Copy(rgfConfPredDiff, nCount * m_nNumClasses, rgfConfBottomDiff, nConfBottomDiffOffset + j * m_nNumClasses, m_nNumClasses);
823 nConfBottomDiffOffset += colBottom[1].offset(1);
831 m_cuda.copy(colBottom[1].count(), m_blobConfPred.
gpu_diff, colBottom[1].mutable_gpu_diff);
837 m_rgAllMatchIndices.Clear();
838 m_rgrgAllNegIndices.Clear();
The Log class provides general output in text form.
void CHECK(bool b, string str)
Test a flag for true.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
void CHECK_GE(double df1, double df2, string str)
Test whether one number is greater than or equal to another.
void CHECK_LT(double df1, double df2, string str)
Test whether one number is less than another.
The Utility class provides general utility funtions.
static List< int > Create(int nCount, int nStart, int nInc)
Create a new List and fill it with values starting with start and incrementing by inc.
static double[] ConvertVec(float[] rgf)
Convert an array of float to an array of generics.
The BBox class processes the NormalizedBBox data used with SSD.
The BlobCollection contains a list of Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
void SetData(double df)
Set all blob data to the value specified.
void SetDiff(double df)
Set all blob diff to the value specified.
int Count
Returns the number of items in the collection.
The Blob is the main holder of data that moves through the Layers of the Net.
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
void ShareData(Blob< T > b)
Set the data to point to the data of the other blob – useful in Layers which simply perform a copy in...
long mutable_gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
T[] mutable_cpu_diff
Get diff from the GPU and bring it over to the host, or Set diff from the Host and send it over to th...
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
T GetData(int nIdx)
Returns the data at a given flat index within the Blob.
int count()
Returns the total number of items in the Blob.
string Name
Get/set the name of the Blob.
long gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
The HostBuffer helps manage host memory, often used when implementing CPU versions of a function or l...
float[] GetHostDataAsFloat()
Returns the host buffer data as an array of floats.
void Dispose()
Release all resources used.
void CopyFrom(Blob< T > b, bool bFromDiff=false)
Copy the gpu data from the blob to the host buffer.
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.
void Backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Given the top Blob error gradients, compute the bottom Blob error gradients.
double Forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Given the bottom (input) Blobs, this function computes the top (output) Blobs and the loss.
abstract void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Adjust the shapes of top blobs and internal buffers to accomodate the shapes of the bottom blobs.
void Dispose()
Releases all GPU and host resources used by the Layer.
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
void Setup(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Implements common Layer setup functionality.
static Layer< T > Create(CudaDnn< T > cuda, Log log, LayerParameter p, CancelEvent evtCancel, IXDatabaseBase db=null, TransferInput trxinput=null)
Create a new Layer based on the LayerParameter.
LayerParameter.LayerType m_type
Specifies the Layer type.
The LossLayer provides an interface for Layer's that take two blobs as input – usually (1) prediction...
double GetNormalizer(LossParameter.NormalizationMode normalization_mode, int nOuterNum, int nInnerNum, int nValidCount)
Returns the normalizer used to normalize the loss.
The MultiBoxLossLayer performs multibox operations including the following:
override int ExactNumTopBlobs
Returns the exact number of top blobs required: argmax
void forwardGpu(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward GPU computation.
MultiBoxLossLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
Constructor.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the multibox loss error gradient w.r.t the predictions.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
void forwardCpu(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation.
override void dispose()
Release any resources used.
override int ExactNumBottomBlobs
Returns the exact number of bottom blobs required: input
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
List< double > loss_weight
Specifies the loss weight.
MultiBoxLossParameter multiboxloss_param
Returns the parameter set when initializing with LayerType.MULTIBOX_LOSS
SoftmaxParameter softmax_param
Returns the parameter set when initialized with LayerType.SOFTMAX
List< bool > propagate_down
Specifies whether or not the LayerParameter (or protions of) should be backpropagated.
LayerType
Specifies the layer type.
override string ToString()
Returns a string representation of the LayerParameter.
LossParameter loss_param
Returns the parameter set when initialized with LayerType.LOSS
Stores the parameters used by loss layers.
NormalizationMode
How to normalize the loss for loss layers that aggregate across batches, spatial dimensions,...
NormalizationMode? normalization
Specifies the normalization mode (default = VALID).
bool Active
When active, the parameter is used, otherwise it is ignored.
int axis
The axis along which to perform the softmax – may be negative to index from the end (e....
Specifies the parameters for the MultiBoxLossParameter.
float loc_weight
Get/set the weight for the localization loss (default = 1.0).
float overlap_threshold
Get/set the overlap threshold (default = 0.5).
bool use_difficult_gt
Get/set whether or not to consider the difficult ground truth (defalt = true).
bool use_gpu
Use the GPU version of the algorithm.
PriorBoxParameter.CodeType code_type
Get/set the coding method for the bounding box.
float neg_overlap
Get/set the negative overlap upperbound for the unmatched predictions (default = 0....
LocLossType
Defines the localization loss types.
int sample_size
Get/set the number of samples (default = 64).
float neg_pos_ratio
Get/set the negative/positive ratio (default = 3.0).
bool share_location
Get/sets whether or not the bounding box is shared among different classes (default = true).
MiningType
Defines the mining type used during training.
NonMaximumSuppressionParameter nms_param
Get/set the parameters used for the non maximum suppression during hard example training.
LocLossType loc_loss_type
Get/set the localization loss type (default = SMOOTH_L1).
uint background_label_id
Get/set the background label id.
ConfLossType
Defines the confidence loss types.
bool encode_variance_in_target
Get/set whether or not to encode the variance of the prior box in the loc loss target instead of in t...
bool ignore_cross_boundary_bbox
Get/set whether or not to ignore cross boundary bbox during matching (default = false)....
bool map_object_to_agnostic
Get/set whether or not to map all object classes to an agnostic class (default = false)....
bool? do_neg_mining
DEPRECIATED: Get/set whether or not to perform negative mining (default = false).
bool bp_inside
Get/set whether or not to only backpropagate on corners which are inside of the image region when enc...
ConfLossType conf_loss_type
Get/set the confidence loss type (default = SOFTMAX).
bool use_prior_for_matching
Get/set whether or not to use prior for matching.
uint num_classes
Get/set the number of classes to be predicted - required!
MiningType mining_type
Get/set the mining type used during training (default = MAX_NEGATIVE).
bool use_prior_for_nms
Get/set whether or not to use the prior bbox for nms.
MatchType match_type
Get/set the matching method used during training (default = PER_PREDICTION).
float eta
Get/set the parameter for adaptive nms.
int? top_k
Get/set the maximum number of results kept.
float nms_threshold
Get/set the threshold to be used in nms.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.common namespace contains common MyCaffe classes.
SSD_CONF_LOSS_TYPE
Defines the confidence loss types used during SSD cuda training.
SSD_MATCH_TYPE
Defines the matching method used during SSD cuda training.
SSD_CODE_TYPE
Defines the encode/decode type used during SSD cuda training.
SSD_LOC_LOSS_TYPE
Defines the location loss types used during SSD cuda training.
SSD_MINING_TYPE
Defines the mining type used during SSD cuda training.
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-...