Esempio n. 1
0
        /// <summary>
        /// Train the model.
        /// </summary>
        /// <param name="sender">Specifies the sender</param>
        /// <param name="e">specifies the arguments.</param>
        private void btnTrain_Click(object sender, EventArgs e)
        {
            m_evtCancel.Reset();
            InputData input = getInput(InputData.OPERATION.TRAIN);

            if (input != null)
            {
                m_bw.RunWorkerAsync(input);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// The constructor.
 /// </summary>
 /// <param name="param">Specifies the creation parameters.</param>
 /// <param name="log">Specifies the output log.</param>
 /// <param name="evtCancel">Specfies the cancel event used to cancel the process.</param>
 public VOCDataLoader(VOCDataParameters param, Log log, CancelEvent evtCancel)
 {
     m_param     = param;
     m_log       = log;
     m_evtCancel = evtCancel;
     m_evtCancel.Reset();
 }
        /// <summary>
        /// Starts running the internal thread function which then calls the DoWork event.
        /// </summary>
        /// <param name="cuda">Specifies the CudaDnn connection to Cuda placed in the ActionStartArgs passed along to DoWork.</param>
        /// <param name="log">Specifies the Log for output, placed in the ActionStartArgs passed along to DoWork.</param>
        /// <param name="nDeviceID">Optionally, specifies the DeviceID placed in the ActionStartArgs passed along to DoWork.</param>
        /// <param name="arg">Optionally, specifies an argument defined by the caller.</param>
        /// <param name="nInitialDelay">Optionally, specifies an initial delay in ms (default = 0).</param>
        public void StartInternalThread(CudaDnn <T> cuda, Log log, int nDeviceID = 0, object arg = null, int nInitialDelay = 0)
        {
            m_evtAbort.Reset();
            m_evtCancel.Reset();

            if (OnPreStart != null)
            {
                OnPreStart(this, new EventArgs());
            }

            if (m_bUseThread)
            {
                if (m_thread == null)
                {
                    m_thread = new Thread(new ParameterizedThreadStart(InternalThreadEntry));
                    m_thread.Start(new ActionStateArgs <T>(cuda, log, m_evtCancel, nDeviceID, arg, nInitialDelay));
                }
            }
            else
            {
                if (m_task == null)
                {
                    Action <object> action = new Action <object>(InternalThreadEntry);
                    m_task = Task.Factory.StartNew(action, new ActionStateArgs <T>(cuda, log, m_evtCancel, nDeviceID, arg), TaskCreationOptions.LongRunning);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// The constructor.
        /// </summary>
        /// <param name="cuda">Specifies the instance of CudaDnn to use.</param>
        /// <param name="log">Specifies the output log.</param>
        /// <param name="evtCancel">Specifies the cancel event used to abort processing.</param>
        /// <param name="strModelType">Specifies the model type: 'vgg19', 'vgg16'</param>
        /// <param name="strModel">Specifies the network model to use.</param>
        /// <param name="rgWeights">Optionally, specifies the weights to use (or <i>null</i> to ignore).</param>
        /// <param name="bCaffeModel">Specifies whether or not the weights are in the caffe (<i>true</i>) or mycaffe (<i>false</i>) format.</param>
        /// <param name="solverType">Optionally, specifies the solver type to use (default = LBFGS).</param>
        /// <param name="dfLearningRate">Optionally, specifies the solver learning rate (default = 1.0).</param>
        public NeuralStyleTransfer(CudaDnn <T> cuda, Log log, CancelEvent evtCancel, string strModelType, string strModel, byte[] rgWeights, bool bCaffeModel, SolverParameter.SolverType solverType = SolverParameter.SolverType.LBFGS, double dfLearningRate = 1.0)
        {
            m_cuda           = cuda;
            m_log            = log;
            m_evtCancel      = evtCancel;
            m_rgWeights      = rgWeights;
            m_solverType     = solverType;
            m_dfLearningRate = dfLearningRate;

            if (m_evtCancel != null)
            {
                m_evtCancel.Reset();
            }

            RawProto proto = RawProto.Parse(strModel);

            m_param = NetParameter.FromProto(proto);

            add_input_layer(m_param);
            m_rgstrUsedLayers = load_layers(strModelType);
            prune(m_param, m_rgstrUsedLayers);
            add_gram_layers(m_param);

            m_transformationParam             = new TransformationParameter();
            m_transformationParam.color_order = (bCaffeModel) ? TransformationParameter.COLOR_ORDER.BGR : TransformationParameter.COLOR_ORDER.RGB;
            m_transformationParam.scale       = 1.0;
            m_transformationParam.mean_value  = m_rgMeanValues;

            m_persist = new PersistCaffe <T>(m_log, false);
        }
        /// <summary>
        /// The constructor.
        /// </summary>
        /// <param name="param">Specifies the creation parameters.</param>
        /// <param name="log">Specifies the output log used to show status updates.</param>
        /// <param name="evtCancel">Specifies the cancel event used to abort the creation process.</param>
        public MnistDataLoader(MnistDataParameters param, Log log, CancelEvent evtCancel)
        {
            m_extractor             = new MnistDataLoaderLite(Path.GetDirectoryName(param.TrainImagesFile));
            m_extractor.OnProgress += m_extractor_OnProgress;
            m_extractor.OnError    += m_extractor_OnError;

            m_param     = param;
            m_log       = log;
            m_evtCancel = evtCancel;
            m_evtCancel.Reset();
        }
Esempio n. 6
0
        private void createMyCaffeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_bCaffeCreated = true;
            createMyCaffeToolStripMenuItem.Enabled     = false;
            destroyMyCaffeToolStripMenuItem.Enabled    = false;
            trainMNISTToolStripMenuItem.Enabled        = false;
            testMNISTToolStripMenuItem.Enabled         = false;
            loadMNISTToolStripMenuItem.Enabled         = false;
            deviceInformationToolStripMenuItem.Enabled = false;
            abortToolStripMenuItem.Enabled             = true;
            m_evtCancel.Reset();
            m_evtCaffeCancel.Reset();

            if (!m_bwProcess.IsBusy)
            {
                m_bwProcess.RunWorkerAsync();
            }

            m_Cmd = COMMAND.CREATE;
            m_evtCommandRead.Set();
        }
Esempio n. 7
0
        /// <summary>
        /// The constructor.
        /// </summary>
        /// <param name="cuda">Specifies the instance of CudaDnn to use.</param>
        /// <param name="log">Specifies the output log.</param>
        /// <param name="evtCancel">Specifies the cancel event used to abort processing.</param>
        /// <param name="rgLayers">Specifies the layers along with their style and content weights.</param>
        /// <param name="strModelDesc">Specifies the network model descriptor to use.</param>
        /// <param name="rgWeights">Optionally, specifies the weights to use (or <i>null</i> to ignore).</param>
        /// <param name="bCaffeModel">Specifies whether or not the weights are in the caffe (<i>true</i>) or mycaffe (<i>false</i>) format.</param>
        /// <param name="solverType">Optionally, specifies the solver type to use (default = LBFGS).</param>
        /// <param name="dfLearningRate">Optionally, specifies the solver learning rate (default = 1.0).</param>
        /// <param name="nMaxImageSize">Optionally, specifies the default maximum image size (default = 840).</param>
        /// <param name="nLBFGSCorrections">Optionally, specifies the LBFGS Corrections (only used when using the LBFGS solver, default = 100).</param>
        /// <param name="netShare">Optionally, specifies a net to share.</param>
        public NeuralStyleTransfer(CudaDnn <T> cuda, Log log, CancelEvent evtCancel, Dictionary <string, Tuple <double, double> > rgLayers, string strModelDesc, byte[] rgWeights, bool bCaffeModel, SolverParameter.SolverType solverType = SolverParameter.SolverType.LBFGS, double dfLearningRate = 1.0, int nMaxImageSize = 840, int nLBFGSCorrections = 100, Net <T> netShare = null)
        {
            m_log                  = log;
            m_evtCancel            = evtCancel;
            m_rgWeights            = rgWeights;
            m_solverType           = solverType;
            m_dfLearningRate       = dfLearningRate;
            m_nDefaultMaxImageSize = nMaxImageSize;
            m_nLBFGSCorrections    = nLBFGSCorrections;

            setupNetShare(netShare, cuda);

            if (m_evtCancel != null)
            {
                m_evtCancel.Reset();
            }

            RawProto proto = RawProto.Parse(strModelDesc);

            m_param = NetParameter.FromProto(proto);

            Dictionary <string, double> rgStyle   = new Dictionary <string, double>();
            Dictionary <string, double> rgContent = new Dictionary <string, double>();

            foreach (KeyValuePair <string, Tuple <double, double> > kv in rgLayers)
            {
                if (kv.Value.Item1 != 0)
                {
                    rgStyle.Add(kv.Key, kv.Value.Item1);
                }

                if (kv.Value.Item2 != 0)
                {
                    rgContent.Add(kv.Key, kv.Value.Item2);
                }
            }

            add_input_layer(m_param);
            m_rgstrUsedLayers = load_layers(rgStyle, rgContent);
            prune(m_param, m_rgstrUsedLayers);
            add_gram_layers(m_param);

            m_transformationParam             = new TransformationParameter();
            m_transformationParam.color_order = (bCaffeModel) ? TransformationParameter.COLOR_ORDER.BGR : TransformationParameter.COLOR_ORDER.RGB;
            m_transformationParam.scale       = 1.0;
            m_transformationParam.mean_value  = m_rgMeanValues;

            m_persist = new PersistCaffe <T>(m_log, false);
        }
Esempio n. 8
0
        /// <summary>
        /// The constructor.
        /// </summary>
        /// <param name="nQueryCount">Specifies the size of each query.</param>
        /// <param name="dtStart">Specifies the state date used for data collection.</param>
        /// <param name="nTimeSpanInMs">Specifies the time increment used between each data item.</param>
        /// <param name="nSegmentSize">Specifies the amount of data to query on the back-end from each custom query.</param>
        /// <param name="nMaxCount">Specifies the maximum number of items to allow in memory.</param>
        /// <param name="strSchema">Specifies the database schema.</param>
        /// <param name="rgCustomQueries">Optionally, specifies any custom queries to add directly.</param>
        /// <remarks>
        /// The database schema defines the number of custom queries to use along with their names.  A simple key=value; list
        /// defines the streaming database schema using the following format:
        /// \code{.cpp}
        ///  "ConnectionCount=2;
        ///   Connection0_CustomQueryName=Test1;
        ///   Connection0_CustomQueryParam=param_string1
        ///   Connection1_CustomQueryName=Test2;
        ///   Connection1_CustomQueryParam=param_string2"
        /// \endcode
        /// Each param_string specifies the parameters of the custom query and may include the database connection string, database
        /// table, and database fields to query.
        /// </remarks>
        public MgrQueryTime(int nQueryCount, DateTime dtStart, int nTimeSpanInMs, int nSegmentSize, int nMaxCount, string strSchema, List <IXCustomQuery> rgCustomQueries)
        {
            m_colCustomQuery.Load();
            m_colData      = new DataItemCollection(nQueryCount);
            m_nQueryCount  = nQueryCount;
            m_nSegmentSize = nSegmentSize;

            m_schema = new PropertySet(strSchema);

            foreach (IXCustomQuery icustomquery in rgCustomQueries)
            {
                m_colCustomQuery.Add(icustomquery);
            }

            int nConnections = m_schema.GetPropertyAsInt("ConnectionCount");

            for (int i = 0; i < nConnections; i++)
            {
                string strConTag           = "Connection" + i.ToString();
                string strCustomQuery      = m_schema.GetProperty(strConTag + "_CustomQueryName");
                string strCustomQueryParam = m_schema.GetProperty(strConTag + "_CustomQueryParam");

                IXCustomQuery iqry = m_colCustomQuery.Find(strCustomQuery);
                if (iqry == null)
                {
                    throw new Exception("Could not find the custom query '" + strCustomQuery + "'!");
                }

                if (iqry.QueryType != CUSTOM_QUERY_TYPE.TIME)
                {
                    throw new Exception("The custom query '" + iqry.Name + "' does not support the 'CUSTOM_QUERY_TYPE.TIME'!");
                }

                DataQuery dq = new DataQuery(iqry.Clone(strCustomQueryParam), dtStart, TimeSpan.FromMilliseconds(nTimeSpanInMs), nSegmentSize, nMaxCount);
                m_colDataQuery.Add(dq);

                m_nFieldCount += (dq.FieldCount - 1);  // subtract each sync field.
            }

            m_nFieldCount += 1; // add the sync field
            m_colDataQuery.Start();

            m_evtCancel.Reset();
            m_taskConsolidate = Task.Factory.StartNew(new Action(consolidateThread));
            m_evtEnabled.Set();
            m_colData.WaitData(10000);
        }
Esempio n. 9
0
        public void Create(DatasetConfiguration config, IXDatasetCreatorProgress progress)
        {
            string strTrainingFile1 = Properties.Settings.Default.vocTrainingFile1;
            string strTrainingFile2 = Properties.Settings.Default.vocTrainingFile2;
            string strTestingFile1  = Properties.Settings.Default.vocTestingFile1;
            bool   bExtract         = Properties.Settings.Default.ExpandFiles;

            m_evtCancel.Reset();

            DataConfigSetting dsTrainingFile1 = config.Settings.Find("Training Data File 2007");

            strTrainingFile1 = dsTrainingFile1.Value.ToString();

            DataConfigSetting dsTrainingFile2 = config.Settings.Find("Training Data File 2012");

            strTrainingFile2 = dsTrainingFile2.Value.ToString();

            DataConfigSetting dsTestingFile1 = config.Settings.Find("Testing Data File 2007");

            strTestingFile1 = dsTestingFile1.Value.ToString();

            DataConfigSetting dsExtract     = config.Settings.Find("Extract Data Files");
            OptionItem        extractOption = dsExtract.Value as OptionItem;

            bExtract = (extractOption.Index == 0) ? false : true;

            DataConfigSetting dsName    = config.Settings.Find("Output Dataset Name");
            string            strDsName = dsName.Value.ToString();

            string strTrainingSrc = strDsName + ".training";
            string strTestingSrc  = strDsName + ".testing";

            m_iprogress = progress;

            m_factory.DeleteSources(strTrainingSrc, strTestingSrc);

            Log log = new Log("VOC0712 Dataset Creator");

            log.OnWriteLine += new EventHandler <LogArg>(log_OnWriteLine);

            try
            {
                VOCDataParameters param  = new VOCDataParameters(strTrainingFile1, strTrainingFile2, strTestingFile1, bExtract);
                VOCDataLoader     loader = new VOCDataLoader(param, log, m_evtCancel);

                loader.OnProgress  += Loader_OnProgress;
                loader.OnError     += Loader_OnError;
                loader.OnCompleted += Loader_OnCompleted;

                if (!loader.LoadDatabase(config.ID))
                {
                    return;
                }

                using (DNNEntities entities = EntitiesConnection.CreateEntities())
                {
                    List <Dataset> rgDs          = entities.Datasets.Where(p => p.Name == strDsName).ToList();
                    List <Source>  rgSrcTraining = entities.Sources.Where(p => p.Name == strTrainingSrc).ToList();
                    List <Source>  rgSrcTesting  = entities.Sources.Where(p => p.Name == strTestingSrc).ToList();

                    if (rgSrcTraining.Count == 0)
                    {
                        throw new Exception("Could not find the training source '" + strTrainingSrc + "'.");
                    }

                    if (rgSrcTesting.Count == 0)
                    {
                        throw new Exception("Could not find the tesing source '" + strTestingSrc + "'.");
                    }

                    int    nSrcTestingCount  = rgSrcTesting[0].ImageCount.GetValueOrDefault();
                    int    nSrcTrainingCount = rgSrcTraining[0].ImageCount.GetValueOrDefault();
                    int    nSrcTotalCount    = nSrcTestingCount + nSrcTrainingCount;
                    double dfTestingPct      = (nSrcTrainingCount == 0) ? 0.0 : nSrcTestingCount / (double)nSrcTotalCount;

                    Dataset ds = null;

                    if (rgDs.Count == 0)
                    {
                        ds      = new Dataset();
                        ds.Name = strDsName;
                    }
                    else
                    {
                        ds = rgDs[0];
                    }

                    ds.ImageEncoded     = rgSrcTesting[0].ImageEncoded;
                    ds.ImageChannels    = rgSrcTesting[0].ImageChannels;
                    ds.ImageHeight      = rgSrcTraining[0].ImageHeight;
                    ds.ImageWidth       = rgSrcTraining[0].ImageWidth;
                    ds.TestingPercent   = (decimal)dfTestingPct;
                    ds.TestingSourceID  = rgSrcTesting[0].ID;
                    ds.TestingTotal     = rgSrcTesting[0].ImageCount;
                    ds.TrainingSourceID = rgSrcTraining[0].ID;
                    ds.TrainingTotal    = rgSrcTraining[0].ImageCount;
                    ds.DatasetCreatorID = config.ID;
                    ds.DatasetGroupID   = 0;
                    ds.ModelGroupID     = 0;

                    if (rgDs.Count == 0)
                    {
                        entities.Datasets.Add(ds);
                    }

                    entities.SaveChanges();
                }
            }
            catch (Exception excpt)
            {
                log.WriteLine("ERROR: " + excpt.Message);
            }
            finally
            {
                if (m_evtCancel.WaitOne(0))
                {
                    log.WriteLine("ABORTED converting VOC0712 data files.");
                    m_iprogress.OnCompleted(new CreateProgressArgs(1, "ABORTED!", null, true));
                }
                else
                {
                    log.WriteLine("Done converting VOC0712 data files.");
                    m_iprogress.OnCompleted(new CreateProgressArgs(1, "COMPLETED."));
                }

                Properties.Settings.Default.vocTestingFile1  = strTestingFile1;
                Properties.Settings.Default.vocTrainingFile1 = strTrainingFile1;
                Properties.Settings.Default.vocTrainingFile2 = strTrainingFile2;
                Properties.Settings.Default.ExpandFiles      = bExtract;
                Properties.Settings.Default.Save();
            }
        }
Esempio n. 10
0
 /// <summary>
 /// The train event start training the model.
 /// </summary>
 /// <param name="sender">Specifies the event sender.</param>
 /// <param name="e">Specifies the event args.</param>
 private void btnTrain_Click(object sender, EventArgs e)
 {
     m_evtCancel.Reset();
     m_bw.RunWorkerAsync(OPERATION.TRAIN);
 }