Beispiel #1
0
        private BatchSampler createSampler(int nMaxTrials, int nMaxSample, float fMinScale = 1.0f, float fMaxScale = 1.0f, float fMinAspectRatio = 1.0f, float fMaxAspectRatio = 1.0f, float?fMinJaccardOverlap = null, float?fMaxJaccardOverlap = null)
        {
            BatchSampler sampler = new BatchSampler();

            sampler.max_trials               = (uint)nMaxTrials;
            sampler.max_sample               = (uint)nMaxSample;
            sampler.sampler.min_scale        = fMinScale;
            sampler.sampler.max_scale        = fMaxScale;
            sampler.sampler.min_aspect_ratio = fMinAspectRatio;
            sampler.sampler.max_aspect_ratio = fMaxAspectRatio;

            if (fMinJaccardOverlap.HasValue)
            {
                sampler.sample_constraint.min_jaccard_overlap = fMinJaccardOverlap.Value;
            }

            if (fMaxJaccardOverlap.HasValue)
            {
                sampler.sample_constraint.max_jaccard_overlap = fMaxJaccardOverlap.Value;
            }

            return(sampler);
        }
 public _RandomTransformMultiWorkerIter(Func <NDArray, NDArray>[] transform_fns, int interval, WorkerPool worker_pool, Func <NDArrayList, NDArrayList> batchify_fn, BatchSampler batch_sampler, bool pin_memory = false, int pin_device_id = 0, WorkerFn worker_fn = null, int prefetch = 0) : base(worker_pool, batchify_fn, batch_sampler, pin_memory, pin_device_id, worker_fn, prefetch)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Generate samples from the NormalizedBBox using the BatchSampler.
        /// </summary>
        /// <param name="sourceBBox">Specifies the source BBox.</param>
        /// <param name="rgObjectBboxes">Specifies the object normalized BBoxes.</param>
        /// <param name="batchSampler">Specifies the batch sampler.</param>
        /// <returns>The list of normalized BBoxes generated is returned.</returns>
        public List <NormalizedBBox> GenerateSamples(NormalizedBBox sourceBBox, List <NormalizedBBox> rgObjectBboxes, BatchSampler batchSampler)
        {
            List <NormalizedBBox> rgSampledBBoxes = new List <NormalizedBBox>();
            int nFound = 0;

            for (int i = 0; i < batchSampler.max_trials; i++)
            {
                if (batchSampler.max_sample > 0 && nFound >= batchSampler.max_sample)
                {
                    break;
                }

                // Generate sampleBbox in the normalized space [0,1]
                NormalizedBBox sampledBbox = sampleBBox(batchSampler.sampler);

                // Transform the sampledBbox w.r.t. the source BBox.
                sampledBbox = m_util.Locate(sourceBBox, sampledBbox);

                // Determine if the sampled BBox is positive or negative by the constraint.
                if (SatisfySampleConstraint(sampledBbox, rgObjectBboxes, batchSampler.sample_constraint))
                {
                    nFound++;
                    rgSampledBBoxes.Add(sampledBbox);
                }
            }

            return(rgSampledBBoxes);
        }
Beispiel #4
0
 public DetectionDataLoader(Dataset <NDArray> dataset, int?batch_size = null, bool shuffle = false, Sampler sampler = null, string last_batch = null, BatchSampler batch_sampler = null, Func <NDArrayList, NDArrayList> batchify_fn = null, int num_workers = 0) : base(dataset, batch_size, shuffle, sampler, last_batch, batch_sampler, batchify_fn, num_workers)
 {
     throw new NotImplementedException();
 }
 public RandomTransformDataLoader(Func <NDArray, NDArray>[] transform_fns, Dataset <NDArray> dataset, int interval = 1, int?batch_size = null, bool shuffle = false, Sampler sampler = null, string last_batch = null, BatchSampler batch_sampler = null, Func <NDArrayList, NDArrayList> batchify_fn = null, int num_workers = 0, bool pin_memory = false, int pin_device_id = 0, int?prefetch = null, bool thread_pool = false) : base(dataset, batch_size, shuffle, sampler, last_batch, batch_sampler, batchify_fn, num_workers, pin_memory, pin_device_id, prefetch, thread_pool)
 {
     throw new NotImplementedException();
 }
Beispiel #6
0
        /// <summary>
        /// The constructor.
        /// </summary>
        /// <param name="strBaseDirectory">Specifies the base directory that contains the data and models.</param>
        /// <param name="nBatchSize">Optionally, specifies the batch size (default = 32).</param>
        /// <param name="nAccumBatchSize">Optionally, specifies the accumulation batch size (default = 32).</param>
        /// <param name="rgGpuId">Optionally, specifies a set of GPU ID's to use (when null, GPU=0 is used).</param>
        /// <param name="bUseBatchNorm">Optionally, specifies to use batch normalization (default = false).</param>
        /// <param name="normMode">Optionally, specifies the normalization mode (default = VALID).</param>
        /// <param name="net">Specifies the 'base' net parameter that is to be altered.</param>
        public SsdPascalModelBuilder(string strBaseDirectory, int nBatchSize = 32, int nAccumBatchSize = 32, List <int> rgGpuId = null, bool bUseBatchNorm = false, LossParameter.NormalizationMode normMode = LossParameter.NormalizationMode.VALID, NetParameter net = null)
            : base(strBaseDirectory, net)
        {
            if (rgGpuId == null)
            {
                m_rgGpuID.Add(0);
            }
            else
            {
                m_rgGpuID = new List <int>(rgGpuId);
            }

            m_strJob = "SSD_" + m_nResizeWidth.ToString() + "x" + m_nResizeHeight.ToString();
            // The model name is used when initially creating the NetParameter.
            m_strModel = "VGG_VOC0712_" + m_strJob;

            m_bUseBatchNorm     = bUseBatchNorm;
            m_normalizationMode = normMode;

            m_nBatchSize      = nBatchSize;
            m_nAccumBatchSize = nAccumBatchSize;
            m_nIterSize       = m_nAccumBatchSize / m_nBatchSize;

            m_nBatchSizePerDevice = (m_rgGpuID.Count == 1) ? m_nBatchSize : m_nBatchSize / m_rgGpuID.Count;
            m_nIterSize           = (int)Math.Ceiling((float)m_nAccumBatchSize / (m_nBatchSizePerDevice * m_rgGpuID.Count));
            m_nGpuID = m_rgGpuID[0];

            // Set the base learning rate.
            m_dfLocWeight = (m_dfNegPosRatio + 1.0) / 4.0;
            m_dfBaseLr    = (m_bUseBatchNorm) ? 0.0004 : 0.00004;

            switch (m_normalizationMode)
            {
            case LossParameter.NormalizationMode.NONE:
                m_dfBaseLr /= m_nBatchSizePerDevice;
                break;

            case LossParameter.NormalizationMode.VALID:
                m_dfBaseLr *= 25.0 / m_dfLocWeight;
                break;

            case LossParameter.NormalizationMode.FULL:
                // Roughly there are 2000 prior bboxes per images (TODO: calculate and use exact number).
                m_dfBaseLr *= 2000;
                break;
            }

            // Ideally the test_batch_size should be divisible by the num_test_image,
            // otherwise mAP will be slightly off the true value.
            m_nTestIter = (int)Math.Ceiling((float)m_nNumTestImage / (float)m_nTestBatchSize);

            //-------------------------------------------------------
            // Create the transformer for Training.
            //-------------------------------------------------------
            m_transformTrain             = new TransformationParameter();
            m_transformTrain.mirror      = true;
            m_transformTrain.color_order = TransformationParameter.COLOR_ORDER.BGR; // to support caffe models.
            m_transformTrain.mean_value.Add(104);
            m_transformTrain.mean_value.Add(117);
            m_transformTrain.mean_value.Add(123);
            m_transformTrain.resize_param             = new ResizeParameter(true);
            m_transformTrain.resize_param.prob        = 1;
            m_transformTrain.resize_param.resize_mode = ResizeParameter.ResizeMode.WARP;
            m_transformTrain.resize_param.height      = (uint)m_nResizeHeight;
            m_transformTrain.resize_param.width       = (uint)m_nResizeWidth;
            m_transformTrain.resize_param.interp_mode.Add(ResizeParameter.InterpMode.LINEAR);
            m_transformTrain.resize_param.interp_mode.Add(ResizeParameter.InterpMode.AREA);
            m_transformTrain.resize_param.interp_mode.Add(ResizeParameter.InterpMode.NEAREST);
            m_transformTrain.resize_param.interp_mode.Add(ResizeParameter.InterpMode.CUBIC);
            m_transformTrain.resize_param.interp_mode.Add(ResizeParameter.InterpMode.LANCZOS4);
            m_transformTrain.distortion_param = new DistortionParameter(true);
            m_transformTrain.distortion_param.brightness_prob   = 0.5f;
            m_transformTrain.distortion_param.brightness_delta  = 32;
            m_transformTrain.distortion_param.contrast_prob     = 0.5f;
            m_transformTrain.distortion_param.contrast_lower    = 0.5f;
            m_transformTrain.distortion_param.contrast_upper    = 1.5f;
            m_transformTrain.distortion_param.saturation_prob   = 0.5f;
            m_transformTrain.distortion_param.saturation_lower  = 0.5f;
            m_transformTrain.distortion_param.saturation_upper  = 1.5f;
            m_transformTrain.distortion_param.random_order_prob = 0.0f;
            m_transformTrain.expansion_param                  = new ExpansionParameter(true);
            m_transformTrain.expansion_param.prob             = 0.5f;
            m_transformTrain.expansion_param.max_expand_ratio = 4.0f;
            m_transformTrain.emit_constraint                  = new EmitConstraint(true);
            m_transformTrain.emit_constraint.emit_type        = EmitConstraint.EmitType.CENTER;

            //-------------------------------------------------------
            // Create the transformer for Testing.
            //-------------------------------------------------------
            m_transformTest             = new TransformationParameter();
            m_transformTest.color_order = TransformationParameter.COLOR_ORDER.BGR; // to support caffe models.
            m_transformTest.mean_value.Add(104);
            m_transformTest.mean_value.Add(117);
            m_transformTest.mean_value.Add(123);
            m_transformTest.resize_param             = new ResizeParameter(true);
            m_transformTest.resize_param.prob        = 1;
            m_transformTest.resize_param.resize_mode = ResizeParameter.ResizeMode.WARP;
            m_transformTest.resize_param.height      = (uint)m_nResizeHeight;
            m_transformTest.resize_param.width       = (uint)m_nResizeWidth;
            m_transformTest.resize_param.interp_mode.Add(ResizeParameter.InterpMode.LINEAR);

            //-------------------------------------------------------
            // Create the batch samplers.
            //-------------------------------------------------------
            BatchSampler sampler = createSampler(1, 1);

            m_rgBatchSampler.Add(sampler);

            sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, 0.1f);
            m_rgBatchSampler.Add(sampler);

            sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, 0.3f);
            m_rgBatchSampler.Add(sampler);

            sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, 0.5f);
            m_rgBatchSampler.Add(sampler);

            sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, 0.7f);
            m_rgBatchSampler.Add(sampler);

            sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, 0.9f);
            m_rgBatchSampler.Add(sampler);

            sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, null, 1.0f);
            m_rgBatchSampler.Add(sampler);

            //-------------------------------------------------------
            // Create the Multi-box parameters.
            //-------------------------------------------------------
            m_multiBoxLossLayer = new LayerParameter(LayerParameter.LayerType.MULTIBOX_LOSS);
            m_multiBoxLossLayer.multiboxloss_param.loc_loss_type          = MultiBoxLossParameter.LocLossType.SMOOTH_L1;
            m_multiBoxLossLayer.multiboxloss_param.conf_loss_type         = MultiBoxLossParameter.ConfLossType.SOFTMAX;
            m_multiBoxLossLayer.multiboxloss_param.neg_pos_ratio          = (float)m_dfNegPosRatio;
            m_multiBoxLossLayer.multiboxloss_param.num_classes            = (uint)m_nNumClasses;
            m_multiBoxLossLayer.multiboxloss_param.loc_weight             = (float)m_dfLocWeight;
            m_multiBoxLossLayer.multiboxloss_param.share_location         = m_bShareLocation;
            m_multiBoxLossLayer.multiboxloss_param.match_type             = MultiBoxLossParameter.MatchType.PER_PREDICTION;
            m_multiBoxLossLayer.multiboxloss_param.overlap_threshold      = 0.5f;
            m_multiBoxLossLayer.multiboxloss_param.use_prior_for_matching = true;
            m_multiBoxLossLayer.multiboxloss_param.background_label_id    = (uint)m_nBackgroundLabelId;
            m_multiBoxLossLayer.multiboxloss_param.use_difficult_gt       = true;
            m_multiBoxLossLayer.multiboxloss_param.mining_type            = MultiBoxLossParameter.MiningType.MAX_NEGATIVE;
            m_multiBoxLossLayer.multiboxloss_param.neg_overlap            = 0.5f;
            m_multiBoxLossLayer.multiboxloss_param.code_type = PriorBoxParameter.CodeType.CENTER_SIZE;
            m_multiBoxLossLayer.multiboxloss_param.ignore_cross_boundary_bbox = false;
            m_multiBoxLossLayer.loss_param.normalization = m_normalizationMode;

            if (m_multiBoxLossLayer.multiboxloss_param.code_type == PriorBoxParameter.CodeType.CENTER_SIZE)
            {
                m_rgPriorVariance = new List <float>()
                {
                    0.1f, 0.1f, 0.2f, 0.2f
                }
            }
            ;
            else
            {
                m_rgPriorVariance = new List <float>()
                {
                    0.1f
                }
            };

            //-------------------------------------------------------
            // Create the Detection Output parameters.
            //-------------------------------------------------------
            m_detectionOut                                      = new DetectionOutputParameter();
            m_detectionOut.num_classes                          = (uint)m_nNumClasses;
            m_detectionOut.share_location                       = m_bShareLocation;
            m_detectionOut.background_label_id                  = m_nBackgroundLabelId;
            m_detectionOut.nms_param                            = new NonMaximumSuppressionParameter(true);
            m_detectionOut.nms_param.nms_threshold              = 0.45f;
            m_detectionOut.nms_param.top_k                      = 400;
            m_detectionOut.save_output_param                    = new SaveOutputParameter(true);
            m_detectionOut.save_output_param.output_directory   = m_strBaseDir + "\\results";
            m_detectionOut.save_output_param.output_name_prefix = "comp4_det_test_";
            m_detectionOut.save_output_param.label_map_file     = getFileName(m_strLabelMapFile, null);
            m_detectionOut.save_output_param.name_size_file     = getFileName(m_strNameSizeFile, null);
            m_detectionOut.save_output_param.num_test_image     = (uint)m_nNumTestImage;
            m_detectionOut.keep_top_k                           = 200;
            m_detectionOut.confidence_threshold                 = 0.01f;
            m_detectionOut.code_type                            = m_multiBoxLossLayer.multiboxloss_param.code_type;

            //-------------------------------------------------------
            // Create the Detection Evaluation parameters.
            //-------------------------------------------------------
            m_detectionEval                      = new DetectionEvaluateParameter();
            m_detectionEval.num_classes          = (uint)m_nNumClasses;
            m_detectionEval.background_label_id  = (uint)m_nBackgroundLabelId;
            m_detectionEval.overlap_threshold    = 0.5f;
            m_detectionEval.evaulte_difficult_gt = false;
            m_detectionEval.name_size_file       = getFileName(m_strNameSizeFile, null);

            //-------------------------------------------------------
            // Setup the MultiBox head layer info.
            //-------------------------------------------------------
            // conv4_3 ==> 38 x 38
            // fc7     ==> 19 x 19
            // conv6_2 ==> 10 x 10
            // conv7_2 ==>  5 x 5
            // conv8_2 ==>  3 x 3
            // conv9_2 ==>  1 x 1
            List <string> rgstrMboxSourceLayers = new List <string>()
            {
                "conv4_3", "fc7", "conv6_2", "conv7_2", "conv8_2", "conv9_2"
            };
            List <double> rgAspectWid = new List <double>()
            {
                2, 2, 2, 2, 2, 2
            };
            List <double> rgAspectHt = new List <double>()
            {
                2, 3, 3, 3, 2, 2
            };
            // L2 normalize conv4_3
            List <double> rgNormalization = new List <double>()
            {
                20, -1, -1, -1, -1, -1
            };
            List <double> rgStepsW = new List <double>()
            {
                8, 16, 32, 64, 100, 300
            };
            List <double> rgStepsH = new List <double>()
            {
                8, 16, 32, 64, 100, 300
            };
            int nMinDim = 300;
            // in percent %
            double        dfMinRatio  = 20;
            double        dfMaxRatio  = 90;
            double        dfRatioStep = (int)Math.Floor((dfMaxRatio - dfMinRatio) / (rgstrMboxSourceLayers.Count - 2));
            List <double> rgMinSizes  = new List <double>();
            List <double> rgMaxSizes  = new List <double>();

            for (double dfRatio = dfMinRatio; dfRatio < dfMaxRatio + 1; dfRatio += dfRatioStep)
            {
                rgMinSizes.Add(nMinDim * dfRatio / 100.0);
                rgMaxSizes.Add(nMinDim * (dfRatio + dfRatioStep) / 100.0);
            }

            rgMinSizes.Insert(0, nMinDim * 10 / 100.0);
            rgMaxSizes.Insert(0, nMinDim * 20 / 100.0);

            m_rgMultiBoxInfo = new List <MultiBoxHeadInfo>();

            for (int i = 0; i < rgstrMboxSourceLayers.Count; i++)
            {
                string strSrc    = rgstrMboxSourceLayers[i];
                double dfMinSize = rgMinSizes[i];
                double dfMaxSize = rgMaxSizes[i];
                double dfStepW   = rgStepsW[i];
                double dfStepH   = rgStepsH[i];
                double dfAspectW = rgAspectWid[i];
                double dfAspectH = rgAspectHt[i];
                double dfNorm    = rgNormalization[i];

                m_rgMultiBoxInfo.Add(new MultiBoxHeadInfo(strSrc, dfMinSize, dfMaxSize, dfStepW, dfStepH, dfAspectW, dfAspectH, dfNorm, null));
            }
        }