/// <summary> /// Setup the layer. /// </summary> /// <param name="colBottom">Specifies the collection of bottom (input) Blobs.</param> /// <param name="colTop">Specifies the collection of top (output) Blobs.</param> public override void LayerSetUp(BlobCollection <T> colBottom, BlobCollection <T> colTop) { base.LayerSetUp(colBottom, colTop); // Internal softmax layer. LayerParameter softmax_param = new LayerParameter(LayerParameter.LayerType.SOFTMAX); softmax_param.softmax_param.axis = m_param.infogain_loss_param.axis; softmax_param.loss_weight.Clear(); softmax_param.loss_weight.Add(1); m_softmaxLayer = new SoftmaxLayer <T>(m_cuda, m_log, softmax_param); m_colSoftmaxBottomVec.Clear(); m_colSoftmaxBottomVec.Add(colBottom[0]); m_colSoftmaxTopVec.Clear(); m_colSoftmaxTopVec.Add(m_blobProb); m_softmaxLayer.Setup(m_colSoftmaxBottomVec, m_colSoftmaxTopVec); // ignore label. m_nIgnoreLabel = m_param.loss_param.ignore_label; // normalization m_log.CHECK(!m_param.loss_param.normalize, "normalize is drepreciated, use 'normalization'."); m_normalization = m_param.loss_param.normalization; // matrix H if (colBottom.Count < 3) { m_log.CHECK(m_param.infogain_loss_param.source != null, "Infogain matrix source must be specified."); PersistCaffe <T> persist = new PersistCaffe <T>(m_log, true); BlobProto blobProto = persist.LoadBlobProto(m_param.infogain_loss_param.source, 1); m_blobInfoGain.FromProto(blobProto); } }
/// <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); }
private BlobProto loadProtoMean(string strFile) { try { if (!File.Exists(strFile)) { throw new Exception("Cannot find the file '" + strFile + "'!"); } byte[] rgBytes; using (FileStream fs = new FileStream(strFile, FileMode.Open, FileAccess.Read)) { using (BinaryReader br = new BinaryReader(fs)) { rgBytes = br.ReadBytes((int)fs.Length); } } PersistCaffe <T> persist = new PersistCaffe <T>(m_log, true); return(persist.LoadBlobProto(rgBytes, 1)); } catch (Exception excpt) { m_log.FAIL("Loading Proto Image Mean: " + excpt.Message); return(null); } }
/// <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); }