示例#1
0
        private bool loadAnnotationFile(Log log, string strFile, SimpleDatum datum, SimpleDatum.ANNOTATION_TYPE type, Dictionary <string, int> rgNameToLabel)
        {
            if (type != SimpleDatum.ANNOTATION_TYPE.BBOX)
            {
                log.FAIL("Unknown annotation type '" + type.ToString() + "'!");
                return(false);
            }

            datum.annotation_group = new List <AnnotationGroup>();
            datum.annotation_type  = type;

            string strExt = Path.GetExtension(strFile).ToLower();

            switch (strExt)
            {
            case ".xml":
                return(loadXmlAnnotationFile(log, strFile, datum, rgNameToLabel));

            default:
                log.FAIL("Unknown annotation file type '" + strExt + "'!");
                break;
            }

            return(true);
        }
        /// <summary>
        /// Copy on parameter to another.
        /// </summary>
        /// <param name="src">Specifies the parameter to copy.</param>
        public override void Copy(LayerParameterBase src)
        {
            if (src is AnnotatedDataParameter)
            {
                AnnotatedDataParameter p = (AnnotatedDataParameter)src;
                m_type         = p.m_type;
                m_strLabelFile = p.m_strLabelFile;

                m_rgBatchSampler = new List <BatchSampler>();
                foreach (BatchSampler bs in p.m_rgBatchSampler)
                {
                    m_rgBatchSampler.Add(bs.Clone());
                }
            }
        }
示例#3
0
        private SimpleDatum loadDatum(Log log, string strImgFile, string strAnnotationFile, int nResizeHeight, int nResizeWidth, SimpleDatum.ANNOTATION_TYPE type, Dictionary <string, int> rgNameToLabel)
        {
            Bitmap bmp = new Bitmap(strImgFile);

            if (nResizeHeight == 0)
            {
                nResizeHeight = bmp.Height;
            }

            if (nResizeWidth == 0)
            {
                nResizeWidth = bmp.Width;
            }

            if (nResizeHeight != bmp.Height || nResizeWidth != bmp.Width)
            {
                Bitmap bmpNew = ImageTools.ResizeImage(bmp, nResizeWidth, nResizeHeight);
                bmp.Dispose();
                bmp = bmpNew;
            }

            SimpleDatum datum = ImageData.GetImageDataD(bmp, 3, false, 0, false);

            loadAnnotationFile(log, strAnnotationFile, datum, type, rgNameToLabel);

            return(datum);
        }
示例#4
0
        private bool loadFile(string strImagesFile, string strSourceName, int nExtractTotal, ref int nExtractIdx, int nTotal, ref int nIdx, Log log, bool bExtractFiles, Dictionary <string, int> rgNameToLabel)
        {
            Stopwatch sw = new Stopwatch();

            reportProgress(nIdx, nTotal, " Source: " + strSourceName);
            reportProgress(nIdx, nTotal, "  loading " + strImagesFile + "...");

            FileStream fs = null;

            try
            {
                int nSrcId = m_factory.AddSource(strSourceName, 3, -1, -1, false);
                addLabels(nSrcId, rgNameToLabel);
                m_factory.Open(nSrcId, 500, Database.FORCE_LOAD.NONE, log);

                int    nPos    = strImagesFile.ToLower().LastIndexOf(".tar");
                string strPath = strImagesFile.Substring(0, nPos);

                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }

                if (bExtractFiles)
                {
                    log.Progress = (double)nIdx / nExtractTotal;
                    log.WriteLine("Extracting files from '" + strImagesFile + "'...");

                    if ((nExtractIdx = TarFile.ExtractTar(strImagesFile, strPath, m_evtCancel, log, nExtractTotal, nExtractIdx)) == 0)
                    {
                        log.WriteLine("Aborted.");
                        return(false);
                    }
                }

                // Load the annotations.
                SimpleDatum.ANNOTATION_TYPE type = SimpleDatum.ANNOTATION_TYPE.BBOX;
                int nResizeHeight = 0;
                int nResizeWidth  = 0;

                // Create the training database images.
                // Create the master list file.
                List <Tuple <string, string> > rgFiles = createFileList(log, strPath);

                sw.Start();
                for (int i = 0; i < rgFiles.Count; i++)
                {
                    SimpleDatum datum = loadDatum(log, rgFiles[i].Item1, rgFiles[i].Item2, nResizeHeight, nResizeWidth, type, rgNameToLabel);
                    m_factory.PutRawImageCache(nIdx, datum);
                    nIdx++;

                    if (m_evtCancel.WaitOne(0))
                    {
                        log.WriteLine("Aborted.");
                        return(false);
                    }

                    if (sw.Elapsed.TotalMilliseconds > 1000)
                    {
                        log.Progress = (double)nIdx / nTotal;
                        log.WriteLine("Loading file " + i.ToString() + " of " + rgFiles.Count.ToString() + "...");
                        sw.Restart();
                    }
                }

                m_factory.ClearImageCashe(true);
                m_factory.Close();
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
            }

            return(true);
        }
        /// <summary>
        /// Setup the DataLayer by starting up the pre-fetching.
        /// </summary>
        /// <param name="colBottom">Not used.</param>
        /// <param name="colTop">Specifies the collection of top (output) Blobs.</param>
        protected override void DataLayerSetUp(BlobCollection <T> colBottom, BlobCollection <T> colTop)
        {
            int nBatchSize = (int)m_param.data_param.batch_size;

            m_cursor = m_db.NewCursor(m_transformer);

            foreach (BatchSampler sampler in m_param.annotated_data_param.batch_sampler)
            {
                m_rgBatchSamplers.Add(sampler);
            }

            m_strLabelMapFile = m_param.annotated_data_param.label_map_file;

            // Make sure dimension is consistent within batch.
            if (m_param.transform_param.resize_param != null && m_param.transform_param.resize_param.Active)
            {
                if (m_param.transform_param.resize_param.resize_mode == ResizeParameter.ResizeMode.FIT_SMALL_SIZE)
                {
                    m_log.CHECK_EQ(nBatchSize, 1, "The FIT_MSALL_SIZE resize mode only supports a batch size of 1.");
                }
            }

            // Read a data point, and use it to initialize the top blob.
            SimpleDatum anno_datum = m_cursor.GetValue(null, true);

            // Use data_transformer to infer the expected blob shape from anno_datum.
            List <int> rgTopShape = m_transformer.InferBlobShape(anno_datum);

            // Reshape top[0] and prefetch_data according to the batch_size.
            rgTopShape[0] = nBatchSize;
            colTop[0].Reshape(rgTopShape);

            for (int i = 0; i < m_rgPrefetch.Length; i++)
            {
                m_rgPrefetch[i].Data.Reshape(rgTopShape);
            }

            m_log.WriteLine("Output data size: " + colTop[0].ToSizeString());

            // Label
            if (m_bOutputLabels)
            {
                bool       bHasAnnoType = (anno_datum.annotation_type != SimpleDatum.ANNOTATION_TYPE.NONE) || (m_param.annotated_data_param.anno_type != SimpleDatum.ANNOTATION_TYPE.NONE);
                List <int> rgLabelShape = Utility.Create <int>(4, 1);

                if (bHasAnnoType)
                {
                    m_AnnoType = anno_datum.annotation_type;

                    // If anno_type is provided in AnnotatedDataParameter, replace the type stored
                    // in each individual AnnotatedDatum.
                    if (m_param.annotated_data_param.anno_type != SimpleDatum.ANNOTATION_TYPE.NONE)
                    {
                        m_log.WriteLine("WARNING: Annotation type stored in AnnotatedDatum is shadowed.");
                        m_AnnoType = m_param.annotated_data_param.anno_type;
                    }

                    // Infer the label shape from anno_dataum.AnnotationGroup().
                    int nNumBboxes = 0;

                    // Since the number of bboxes can be different for each image,
                    // we store the bbox information in a specific format. In specific:
                    // All bboxes are stored in one spatial plane (num and channels are 1)
                    // and each row contains one and only one box in the following format:
                    // [item_id, group_label, instance_id, xmin, ymin, xmax, ymax, diff]
                    // Note: Refer to caffe.proto for details about group_label and
                    // instance_id.
                    if (m_AnnoType == SimpleDatum.ANNOTATION_TYPE.BBOX)
                    {
                        if (anno_datum.annotation_group != null)
                        {
                            for (int g = 0; g < anno_datum.annotation_group.Count; g++)
                            {
                                nNumBboxes += anno_datum.annotation_group[g].annotations.Count;
                            }
                        }

                        rgLabelShape[0] = 1;
                        rgLabelShape[1] = 1;
                        // BasePrefetchingDataLayer.LayerSetup() requires to call
                        // cpu_data and gpu_data for consistent prefetch thread, thus
                        // we must make sure there is at least one bbox.
                        rgLabelShape[2] = Math.Max(nNumBboxes, 1);
                        rgLabelShape[3] = 8;
                    }
                    else
                    {
                        m_log.FAIL("Unknown annotation type.");
                    }
                }
                else
                {
                    rgLabelShape[0] = nBatchSize;
                }

                colTop[1].Reshape(rgLabelShape);

                for (int i = 0; i < m_rgPrefetch.Length; i++)
                {
                    m_rgPrefetch[i].Label.Reshape(rgLabelShape);
                }
            }
        }