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 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; }