/// <summary> /// The Cursor constructor. /// </summary> /// <param name="db">Specifies the underlying database.</param> /// <param name="transformer">Specifies the data transformer used to transform the lables (when active).</param> /// <param name="strSrc">Specifies the name of the data source to use.</param> /// <param name="log">Optionally, specifies an output log used for diagnostic information if specified (default = null).</param> public Cursor(IXImageDatabaseBase db, DataTransformer <T> transformer, string strSrc, Log log = null) { m_log = log; m_db = db; m_transformer = transformer; SourceDescriptor src = m_db.GetSourceByName(strSrc); m_strSrc = src.Name; m_nSrcID = src.ID; m_nCount = src.ImageCount; }
public DataSequenceLayerTest(string strDs = null) : base("Data Sequence Layer Test") { m_settings = new SettingsCaffe(); m_settings.EnableLabelBalancing = false; m_settings.EnableLabelBoosting = false; m_settings.EnablePairInputSelection = false; m_settings.EnableRandomInputSelection = false; if (strDs != null && strDs.Length > 0) { m_db = createImageDb(null); m_db.InitializeWithDsName1(m_settings, strDs); DatasetDescriptor ds = m_db.GetDatasetByName(strDs); m_strSrc = ds.TrainingSourceName; } }
/// <summary> /// Initialize the gym with the specified properties. /// </summary> /// <param name="log">Specifies the output log to use.</param> /// <param name="properties">Specifies the properties containing Gym specific initialization parameters.</param> /// <remarks> /// The ModelGym uses the following initialization properties. /// /// 'GpuID' - the GPU to run on. /// 'ModelDescription' - the model description of the model to use. /// 'Dataset' - the name of the dataset to use. /// 'Weights' - the model trained weights. /// 'CudaPath' - the path of the CudaDnnDLL to use. /// 'BatchSize' - the batch size used when running images through the model (default = 16). /// 'RecreateData' - when 'True' the data is re-run through the model, otherwise if already run the data is loaded from file (faster). /// </remarks> public void Initialize(Log log, PropertySet properties) { m_nGpuID = properties.GetPropertyAsInt("GpuID"); m_strModelDesc = properties.GetProperty("ModelDescription"); m_strDataset = properties.GetProperty("Dataset"); m_rgWeights = properties.GetPropertyBlob("Weights"); m_nBatchSize = properties.GetPropertyAsInt("BatchSize", 16); m_bRecreateData = properties.GetPropertyAsBool("RecreateData", false); m_strProject = properties.GetProperty("ProjectName"); if (string.IsNullOrEmpty(m_strProject)) { m_strProject = "default"; } string strCudaPath = properties.GetProperty("CudaPath"); SettingsCaffe s = new SettingsCaffe(); s.GpuIds = m_nGpuID.ToString(); s.ImageDbLoadMethod = IMAGEDB_LOAD_METHOD.LOAD_ON_DEMAND_BACKGROUND; m_imgdb = new MyCaffeImageDatabase2(log); m_imgdb.InitializeWithDsName1(s, m_strDataset); m_ds = m_imgdb.GetDatasetByName(m_strDataset); SimpleDatum sd = m_imgdb.QueryImage(m_ds.TrainingSource.ID, 0, IMGDB_LABEL_SELECTION_METHOD.NONE, IMGDB_IMAGE_SELECTION_METHOD.NONE); BlobShape shape = new BlobShape(1, sd.Channels, sd.Height, sd.Width); if (m_evtCancel == null) { m_evtCancel = new CancelEvent(); } m_mycaffe = new MyCaffeControl <float>(s, log, m_evtCancel, null, null, null, null, strCudaPath); m_mycaffe.LoadToRun(m_strModelDesc, m_rgWeights, shape); m_log = log; }
/// <summary> /// The LBFGSSolver constructor. /// </summary> /// <param name="cuda">Specifies the instance of CudaDnn to use.</param> /// <param name="log">Specifies the Log for output.</param> /// <param name="p">Specifies teh SolverParameter.</param> /// <param name="evtCancel">Specifies a CancelEvent used to cancel the current operation (e.g. training, testing) for which the Solver is performing.</param> /// <param name="evtForceSnapshot">Specifies an automatic reset event that causes the Solver to perform a Snapshot when set.</param> /// <param name="evtForceTest">Specifies an automatic reset event that causes teh Solver to run a testing cycle when set.</param> /// <param name="imgDb">Specifies the MyCaffeImageDatabase.</param> /// <param name="persist">Specifies the peristence used for loading and saving weights.</param> /// <param name="nSolverCount">Specifies the number of Solvers participating in a multi-GPU session.</param> /// <param name="nSolverRank">Specifies the rank of this Solver in a multi-GPU session.</param> /// <param name="shareNet">Optionally, specifies the net to share when creating the training network (default = null, meaning no share net is used).</param> /// <param name="getws">Optionally, specifies the handler for getting the workspace.</param> /// <param name="setws">Optionally, specifies the handler for setting the workspace.</param> public LBFGSSolver(CudaDnn <T> cuda, Log log, SolverParameter p, CancelEvent evtCancel, AutoResetEvent evtForceSnapshot, AutoResetEvent evtForceTest, IXImageDatabaseBase imgDb, IXPersist <T> persist, int nSolverCount = 1, int nSolverRank = 0, Net <T> shareNet = null, onGetWorkspace getws = null, onSetWorkspace setws = null) : base(cuda, log, p, evtCancel, evtForceSnapshot, evtForceTest, imgDb, persist, nSolverCount, nSolverRank, shareNet, getws, setws) { m_tZero = (T)Convert.ChangeType(0, typeof(T)); m_tOne = (T)Convert.ChangeType(1, typeof(T)); m_tMinusOne = (T)Convert.ChangeType(-1, typeof(T)); PreSolve(); }
/// <summary> /// The worker thread used to either train or run the models. /// </summary> /// <remarks> /// When training, first the input hand-written image model is trained /// using the LeNet model. /// /// This input mode is then run in the onTrainingStart event to get the /// detected hand written character representation. The outputs of layer /// 'ip1' from the input model are then fed as input to the sequence /// model which is then trained to encode the 'ip1' input data with one /// lstm and then decoded with another which is then trained to detect /// a section of the Sin curve data. /// /// When running, the first input model is run to get its 'ip1' representation, /// which is then fed into the sequence model to detect the section of the /// Sin curve. /// </remarks> /// <param name="sender">Specifies the sender of the event (e.g. the BackgroundWorker)</param> /// <param name="args">Specifies the event args.</param> private void m_bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bw = sender as BackgroundWorker; OPERATION op = (OPERATION)e.Argument; SettingsCaffe s = new SettingsCaffe(); s.ImageDbLoadMethod = IMAGEDB_LOAD_METHOD.LOAD_ALL; m_operation = op; m_mycaffe = new MyCaffeControl <float>(s, m_log, m_evtCancel); m_mycaffeInput = new MyCaffeControl <float>(s, m_log, m_evtCancel); m_imgDb = new MyCaffeImageDatabase2(m_log); // Load the image database. m_imgDb.InitializeWithDsName1(s, "MNIST"); m_ds = m_imgDb.GetDatasetByName("MNIST"); // Create the MNIST image detection model NetParameter netParamMnist = m_model.CreateMnistModel(m_ds); SolverParameter solverParamMnist = m_model.CreateMnistSolver(); byte[] rgWts = loadWeights("input"); m_mycaffeInput.Load(Phase.TRAIN, solverParamMnist.ToProto("root").ToString(), netParamMnist.ToProto("root").ToString(), rgWts, null, null, false, m_imgDb); Net <float> netTrain = m_mycaffeInput.GetInternalNet(Phase.TRAIN); Blob <float> input_ip = netTrain.FindBlob(m_strInputOutputBlobName); // input model's second to last output (includes relu) // Run the train or run operation. if (op == OPERATION.TRAIN) { // Train the MNIST model first. m_mycaffeInput.OnTrainingIteration += m_mycaffeInput_OnTrainingIteration; m_plotsInputLoss = new PlotCollection("Input Loss"); m_mycaffeInput.Train(2000); saveWeights("input", m_mycaffeInput.GetWeights()); // Load the Seq2Seq training model. NetParameter netParam = m_model.CreateModel(input_ip.channels, 10); string strModel = netParam.ToProto("root").ToString(); SolverParameter solverParam = m_model.CreateSolver(); rgWts = loadWeights("sequence"); m_mycaffe.OnTrainingIteration += m_mycaffe_OnTrainingIteration; m_mycaffe.LoadLite(Phase.TRAIN, solverParam.ToProto("root").ToString(), netParam.ToProto("root").ToString(), rgWts, false, false); m_mycaffe.SetOnTrainingStartOverride(new EventHandler(onTrainingStart)); // Train the Seq2Seq model. m_plotsSequenceLoss = new PlotCollection("Sequence Loss"); m_mycaffe.Train(m_model.Iterations); saveWeights("sequence", m_mycaffe.GetWeights()); } else { NetParameter netParam = m_model.CreateModel(input_ip.channels, 10, 1, 1); string strModel = netParam.ToProto("root").ToString(); rgWts = loadWeights("sequence"); int nN = 1; m_mycaffe.LoadToRun(netParam.ToProto("root").ToString(), rgWts, new BlobShape(new List <int>() { nN, 1, 1, 1 }), null, null, false, false); runModel(m_mycaffe, bw); } // Cleanup. m_mycaffe.Dispose(); m_mycaffe = null; m_mycaffeInput.Dispose(); m_mycaffeInput = null; }
/// <summary> /// The DB Constructor. /// </summary> /// <param name="imgDb">The underlying database - the MyCaffeImageDatabase.</param> public DB(IXImageDatabaseBase imgDb) { m_db = imgDb; }