/// <summary> /// Return the list of aspect ratios to use based on the parameters. /// </summary> /// <param name="p">Specifies the prior box parameters.</param> /// <returns>The list of aspect ratios is returned.</returns> public static List <float> GetAspectRatios(PriorBoxParameter p) { List <float> rgAspectRatios = new List <float>(); rgAspectRatios.Add(1.0f); for (int i = 0; i < p.aspect_ratio.Count; i++) { float fAr = p.aspect_ratio[i]; bool bAlreadyExists = false; for (int j = 0; j < rgAspectRatios.Count; j++) { if (Math.Abs(fAr - rgAspectRatios[j]) < 1e-6f) { bAlreadyExists = true; break; } } if (!bAlreadyExists) { rgAspectRatios.Add(fAr); if (p.flip) { rgAspectRatios.Add(1.0f / fAr); } } } return(rgAspectRatios); }
/// <summary> /// Calculate the reshape size based on the parameters. /// </summary> /// <param name="p">Specifies the PriorBox parameter.</param> /// <param name="nLayerWid">Specifies the image width.</param> /// <param name="nLayerHt">Specifies the image height.</param> /// <param name="nNumPriors">Optionally, specifies a num priors override (default = null).</param> /// <returns>The new shape is returned.</returns> public static List <int> Reshape(PriorBoxParameter p, int nLayerWid, int nLayerHt, int?nNumPriors = null) { List <int> rgTopShape = Utility.Create <int>(3, 1); if (!nNumPriors.HasValue) { int nMinCount = (p.min_size == null) ? 0 : p.min_size.Count; List <float> rgAspectRatios = GetAspectRatios(p); int nAspectCount = rgAspectRatios.Count; nNumPriors = nMinCount * nAspectCount; if (p.max_size != null) { nNumPriors += p.max_size.Count; } } // Since all images in a batch have the same height and width, we only need to // generate one set of priors which can be shared across all images. rgTopShape[0] = 1; // 2 channels. // First channel stores the mean of each prior coordinate. // Second channel stores the variance of each prior coordiante. rgTopShape[1] = 2; rgTopShape[2] = nLayerWid * nLayerHt * nNumPriors.Value * 4; return(rgTopShape); }
/// <summary> /// Load the parameter from a binary reader. /// </summary> /// <param name="br">Specifies the binary reader.</param> /// <param name="bNewInstance">When <i>true</i> a new instance is created (the default), otherwise the existing instance is loaded from the binary reader.</param> /// <returns>Returns an instance of the parameter.</returns> public override object Load(BinaryReader br, bool bNewInstance = true) { RawProto proto = RawProto.Parse(br.ReadString()); PriorBoxParameter p = FromProto(proto); if (!bNewInstance) { Copy(p); } return(p); }
/// <summary> /// Copy the object. /// </summary> /// <param name="src">The copy is placed in this parameter.</param> public void Copy(PriorBoxParameter src) { m_rgMinSize = Utility.Clone <float>(src.min_size); m_rgMaxSize = Utility.Clone <float>(src.max_size); m_rgAspectRatio = Utility.Clone <float>(src.aspect_ratio); m_bFlip = src.flip; m_bClip = src.clip; m_rgVariance = Utility.Clone <float>(src.variance); m_nImgSize = src.img_size; m_nImgH = src.img_h; m_nImgW = src.img_w; m_fStep = src.step; m_fStepH = src.step_h; m_fStepW = src.step_w; m_fOffset = src.offset; }
/// <summary> /// Copy on parameter to another. /// </summary> /// <param name="src">Specifies the parameter to copy.</param> public override void Copy(LayerParameterBase src) { PriorBoxParameter p = src as PriorBoxParameter; m_rgMinSize = Utility.Clone <float>(p.min_size); m_rgMaxSize = Utility.Clone <float>(p.max_size); m_rgAspectRatio = Utility.Clone <float>(p.aspect_ratio); m_bFlip = p.flip; m_bClip = p.clip; m_rgVariance = Utility.Clone <float>(p.variance); m_nImgSize = p.img_size; m_nImgH = p.img_h; m_nImgW = p.img_w; m_fStep = p.step; m_fStepH = p.step_h; m_fStepW = p.step_w; m_fOffset = p.offset; }
/// <summary> /// Parses the parameter from a RawProto. /// </summary> /// <param name="rp">Specifies the RawProto to parse.</param> /// <returns>A new instance of the parameter is returned.</returns> public static PriorBoxParameter FromProto(RawProto rp) { PriorBoxParameter p = new PriorBoxParameter(); string strVal; p.min_size = rp.FindArray <float>("min_size"); p.max_size = rp.FindArray <float>("max_size"); p.aspect_ratio = rp.FindArray <float>("aspect_ratio"); if ((strVal = rp.FindValue("flip")) != null) { p.flip = bool.Parse(strVal); } if ((strVal = rp.FindValue("clip")) != null) { p.clip = bool.Parse(strVal); } p.variance = rp.FindArray <float>("variance"); if ((strVal = rp.FindValue("img_size")) != null) { p.img_size = uint.Parse(strVal); } else { if ((strVal = rp.FindValue("img_h")) != null) { p.img_h = uint.Parse(strVal); } if ((strVal = rp.FindValue("img_w")) != null) { p.img_w = uint.Parse(strVal); } } if ((strVal = rp.FindValue("step")) != null) { p.step = float.Parse(strVal); } else { if ((strVal = rp.FindValue("step_h")) != null) { p.step_h = float.Parse(strVal); } if ((strVal = rp.FindValue("step_w")) != null) { p.step_w = float.Parse(strVal); } } if ((strVal = rp.FindValue("offset")) != null) { p.offset = float.Parse(strVal); } return(p); }
/// <summary> /// Parses the parameter from a RawProto. /// </summary> /// <param name="rp">Specifies the RawProto to parse.</param> /// <returns>A new instance of the parameter is returned.</returns> public static MultiBoxLossParameter FromProto(RawProto rp) { MultiBoxLossParameter p = new MultiBoxLossParameter(); string strVal; if ((strVal = rp.FindValue("loc_loss_type")) != null) { p.loc_loss_type = LocLossTypeFromString(strVal); } if ((strVal = rp.FindValue("conf_loss_type")) != null) { p.conf_loss_type = ConfLossTypeFromString(strVal); } if ((strVal = rp.FindValue("loc_weight")) != null) { p.loc_weight = float.Parse(strVal); } if ((strVal = rp.FindValue("num_classes")) != null) { p.num_classes = uint.Parse(strVal); } if ((strVal = rp.FindValue("share_location")) != null) { p.share_location = bool.Parse(strVal); } if ((strVal = rp.FindValue("match_type")) != null) { p.match_type = MatchTypeFromString(strVal); } if ((strVal = rp.FindValue("overlap_threshold")) != null) { p.overlap_threshold = float.Parse(strVal); } if ((strVal = rp.FindValue("background_label_id")) != null) { p.background_label_id = uint.Parse(strVal); } if ((strVal = rp.FindValue("use_difficult_gt")) != null) { p.use_difficult_gt = bool.Parse(strVal); } if ((strVal = rp.FindValue("do_neg_mining")) != null) { p.do_neg_mining = bool.Parse(strVal); } if ((strVal = rp.FindValue("neg_pos_ratio")) != null) { p.neg_pos_ratio = float.Parse(strVal); } if ((strVal = rp.FindValue("neg_overlap")) != null) { p.neg_overlap = float.Parse(strVal); } if ((strVal = rp.FindValue("code_type")) != null) { p.code_type = PriorBoxParameter.CodeTypeFromString(strVal); } if ((strVal = rp.FindValue("encode_variance_in_target")) != null) { p.encode_variance_in_target = bool.Parse(strVal); } if ((strVal = rp.FindValue("map_object_to_agnostic")) != null) { p.map_object_to_agnostic = bool.Parse(strVal); } if ((strVal = rp.FindValue("ignore_corss_boundary_bbox")) != null) { p.ignore_cross_boundary_bbox = bool.Parse(strVal); } if ((strVal = rp.FindValue("bp_inside")) != null) { p.bp_inside = bool.Parse(strVal); } if ((strVal = rp.FindValue("mining_type")) != null) { p.mining_type = MiningTypeFromString(strVal); } RawProto rpNms = rp.FindChild("nms_param"); if (rpNms != null) { p.nms_param = NonMaximumSuppressionParameter.FromProto(rpNms); } if ((strVal = rp.FindValue("sample_size")) != null) { p.sample_size = int.Parse(strVal); } if ((strVal = rp.FindValue("use_prior_for_nms")) != null) { p.use_prior_for_nms = bool.Parse(strVal); } return(p); }