10using System.Collections.Generic;
15using System.Threading.Tasks;
34 int m_nIterations = 1000;
35 int m_nDisplayEvery = 100;
36 double m_dfTVLossWeight = 0;
37 double m_dfStyleDataScale1 = 0.0001;
38 double m_dfStyleDataScale2 = 1;
39 double m_dfContentDataScale = 0.0001;
40 double m_dfContentLossScale = 0.0001;
46 byte[] m_rgWeights =
null;
47 Dictionary<string, Dictionary<string, double>> m_rgLayers =
new Dictionary<string, Dictionary<string, double>>();
48 List<string> m_rgstrUsedLayers =
new List<string>();
49 List<double> m_rgMeanValues =
new List<double>();
51 double m_dfLearningRate = 1.0;
52 int m_nDefaultMaxImageSize = 840;
53 string m_strDataBlobName =
"data";
55 int m_nIntermediateOutput = 0;
56 int m_nPartialIteration = 0;
57 int m_nPartialIterations1 = 0;
59 bool m_bUsingSharedNet =
false;
60 int m_nLBFGSCorrections = 100;
61 bool m_bAllowHalfSize =
false;
62 bool m_bAllowHalfSizeOnGram =
true;
63 bool m_bAllowHalfSizeOnEvent =
true;
64 bool m_bAllowHalfSizeOnLoss =
true;
65 bool m_bAllowHalfSizeOnScalar =
true;
66 long m_hWorkspaceData = 0;
67 ulong m_lWorkspaceSizeInBytes = 0;
89 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.5,
int nLBFGSCorrections = 100,
double dfDataScale = 1.0,
Net<T> netShare =
null)
94 m_evtCancel = evtCancel;
95 m_rgWeights = rgWeights;
96 m_solverType = solverType;
97 m_dfLearningRate = dfLearningRate;
98 m_nLBFGSCorrections = nLBFGSCorrections;
100 setupNetShare(netShare, cuda);
102 if (m_evtCancel !=
null)
108 add_input_layer(m_param);
109 m_rgstrUsedLayers = load_layers(strModelType);
110 prune(m_param, m_rgstrUsedLayers);
111 add_gram_layers(m_param);
115 m_transformationParam.
scale = dfDataScale;
116 m_transformationParam.
mean_value = m_rgMeanValues;
137 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,
double dfDataScale = 1.0,
Net<T> netShare =
null)
140 m_evtCancel = evtCancel;
141 m_rgWeights = rgWeights;
142 m_solverType = solverType;
143 m_dfLearningRate = dfLearningRate;
144 m_nDefaultMaxImageSize = nMaxImageSize;
145 m_nLBFGSCorrections = nLBFGSCorrections;
147 setupNetShare(netShare, cuda);
149 if (m_evtCancel !=
null)
155 Dictionary<string, double> rgStyle =
new Dictionary<string, double>();
156 Dictionary<string, double> rgContent =
new Dictionary<string, double>();
158 foreach (KeyValuePair<
string, Tuple<double, double>> kv
in rgLayers)
160 if (kv.Value.Item1 != 0)
161 rgStyle.Add(kv.Key, kv.Value.Item1);
163 if (kv.Value.Item2 != 0)
164 rgContent.Add(kv.Key, kv.Value.Item2);
167 add_input_layer(m_param);
168 m_rgstrUsedLayers = load_layers(rgStyle, rgContent);
169 prune(m_param, m_rgstrUsedLayers);
170 add_gram_layers(m_param);
174 m_transformationParam.
scale = dfDataScale;
175 m_transformationParam.
mean_value = m_rgMeanValues;
187 if (m_solver !=
null)
193 if (m_hWorkspaceData != 0)
195 m_cuda.FreeMemory(m_hWorkspaceData);
196 m_hWorkspaceData = 0;
197 m_lWorkspaceSizeInBytes = 0;
200 if (m_transformer !=
null)
203 m_transformer =
null;
215 public void SetupHalfSize(
bool bAllowHs,
bool bAllowOnGram,
bool bAllowOnEvent,
bool bAllowOnLoss,
bool bAllowOnScalar)
217 m_bAllowHalfSize = bAllowHs;
218 m_bAllowHalfSizeOnEvent = bAllowOnEvent;
219 m_bAllowHalfSizeOnGram = bAllowOnGram;
220 m_bAllowHalfSizeOnLoss = bAllowOnLoss;
221 m_bAllowHalfSizeOnScalar = bAllowOnScalar;
223 if (!bAllowHs || !m_bAllowHalfSizeOnGram)
225 List<string> rgstrHalfLayers =
new List<string>();
231 if (layer1.
name.Contains(
"gram"))
234 rgstrHalfLayers.Add(layer1.
name);
238 if (!bAllowHs && rgstrHalfLayers.Count > 0)
240 string strErr =
"Half-sized memory not supported! Disable half-size in the following layers: " +
Utility.ToString<
string>(rgstrHalfLayers);
254 int nNetDeviceId = net.
Cuda.GetDeviceID();
257 if (nNetDeviceId != nCudaDeviceId)
264 m_cuda = m_netShare.
Cuda;
265 m_bUsingSharedNet =
true;
272 List<int> rgDelIdx =
new List<int>();
276 for (
int i = 0; i < p.
layer.Count; i++)
280 if (data_param ==
null)
282 data_param = p.
layer[i];
283 m_strDataBlobName = data_param.
top[0];
291 m_strDataBlobName = input.
top[0];
295 for (
int i = rgDelIdx.Count - 1; i >= 0; i--)
297 p.
layer.RemoveAt(rgDelIdx[i]);
306 input.
name =
"input1";
307 input.
top.Add(m_strDataBlobName);
309 p.
layer.Insert(0, input);
313 input.
name =
"input1";
317 private List<string> load_layers(
string strName)
319 Dictionary<string, double> rgContent =
new Dictionary<string, double>();
320 Dictionary<string, double> rgStyle =
new Dictionary<string, double>();
326 rgContent.Add(
"conv4_2", 1);
327 rgStyle.Add(
"conv1_1", 0.2);
328 rgStyle.Add(
"conv2_1", 0.2);
329 rgStyle.Add(
"conv3_1", 0.2);
330 rgStyle.Add(
"conv4_1", 0.2);
331 rgStyle.Add(
"conv5_1", 0.2);
333 m_rgMeanValues =
new List<double>() { 103.939, 116.779, 123.68 };
337 rgContent.Add(
"conv2/3x3", 2e-4);
338 rgContent.Add(
"inception_3a/output", 1 - 2e-4);
339 rgStyle.Add(
"conv1/7x7_s2", 0.2);
340 rgStyle.Add(
"conv2/3x3", 0.2);
341 rgStyle.Add(
"inception_3a/output", 0.2);
342 rgStyle.Add(
"inception_4a/output", 0.2);
343 rgStyle.Add(
"inception_5a/output", 0.2);
344 m_rgMeanValues =
new List<double>() { 104, 117, 123 };
348 rgContent.Add(
"conv4", 1);
349 rgStyle.Add(
"conv1", 0.2);
350 rgStyle.Add(
"conv2", 0.2);
351 rgStyle.Add(
"conv3", 0.2);
352 rgStyle.Add(
"conv4", 0.2);
353 rgStyle.Add(
"conv5", 0.2);
357 throw new Exception(
"Model '" + strName +
"' is not supported.");
360 return load_layers(rgStyle, rgContent);
363 private List<string> load_layers(Dictionary<string, double> rgStyle, Dictionary<string, double> rgContent)
365 m_rgLayers =
new Dictionary<string, Dictionary<string, double>>();
366 m_rgLayers.Add(
"content", rgContent);
367 m_rgLayers.Add(
"style", rgStyle);
369 List<string> rgstrUsedLayers =
new List<string>();
371 foreach (KeyValuePair<string, double> kv
in rgContent)
373 rgstrUsedLayers.Add(kv.Key);
377 Dictionary<string, double> rgGram =
new Dictionary<string, double>();
378 foreach (KeyValuePair<string, double> kv
in rgStyle)
380 rgstrUsedLayers.Add(kv.Key);
381 rgGram.Add(
"gram_" + kv.Key, kv.Value);
384 m_rgLayers.Add(
"gram", rgGram);
387 Dictionary<string, double> rgInput =
new Dictionary<string, double>();
388 foreach (KeyValuePair<string, double> kv
in rgContent)
390 rgInput.Add(kv.Key, kv.Value);
393 foreach (KeyValuePair<string, double> kv
in rgGram)
395 rgInput.Add(kv.Key, kv.Value);
398 m_rgLayers.Add(
"input", rgInput);
400 rgstrUsedLayers.Sort();
402 return rgstrUsedLayers;
405 private void prune(
NetParameter p, List<string> rgUsedLayers)
410 for (
int i = 0; i < p.
layer.Count; i++)
412 for (
int j = 0; j < p.
layer[i].top.Count; j++)
414 bool bIsUsed = rgUsedLayers.Contains(p.
layer[i].top[j]);
416 if (nPruneFrom >= 0 && bIsUsed)
421 else if (nPruneFrom < 0 && !bIsUsed)
430 for (
int i = p.
layer.Count - 1; i >= nPruneFrom; i--)
440 List<KeyValuePair<string, double>> lstStyle = m_rgLayers[
"style"].ToList();
441 List<KeyValuePair<string, double>> lstGram = m_rgLayers[
"gram"].ToList();
443 for (
int i=0; i<lstStyle.Count; i++)
445 bool bUseHalfSize =
false;
447 string strStyle = lstStyle[i].Key;
448 string strGram = lstGram[i].Key;
454 if (layer1.
top.Contains(strStyle))
462 layer.
name = strGram;
464 layer.
bottom.Add(strStyle);
465 layer.
top.Add(strGram);
469 layer.
use_halfsize = (bUseHalfSize && m_bAllowHalfSizeOnGram);
475 private double get_style_scale(
Blob<T> b)
477 double df1 = b.
shape(0);
478 df1 = Math.Pow(df1, -2);
479 double df2 = b.
count(1);
480 df2 = Math.Pow(df2, -2);
482 double dfC = (df1 * df2);
487 private double get_content_scale(
Blob<T> b)
489 return m_dfContentLossScale;
492 private void prepare_data_blob(
Net<T> net, Bitmap bmp)
494 List<int> rgDataShape =
new List<int>() { 1, 3, bmp.Height, bmp.Width };
495 m_transformer =
new DataTransformer<T>(m_cuda, m_log, m_transformationParam,
Phase.TEST, 3, bmp.Height, bmp.Width);
502 private void prepare_input_param(
Net<T> net, Bitmap bmp)
504 List<int> rgDataShape =
new List<int>() { 1, 3, bmp.Height, bmp.Width };
505 m_transformer =
new DataTransformer<T>(m_cuda, m_log, m_transformationParam,
Phase.TEST, 3, bmp.Height, bmp.Width);
512 private Bitmap save(
Net<T> net)
530 public Bitmap
Process(Bitmap bmpStyle, Bitmap bmpContent,
int nIterations,
int nIntermediateOutput = -1,
double dfTvLoss = 0,
int nMaxSize = -1,
bool bEnablePartialSolution =
false)
541 m_dfTVLossWeight = dfTvLoss;
542 m_nIterations = nIterations;
545 nMaxSize = m_nDefaultMaxImageSize;
547 if (bmpContent.Width > nMaxSize ||
548 bmpContent.Height > nMaxSize)
550 double dfAspectRatio = (double)bmpContent.Height / (
double)bmpContent.Width;
551 int nWidth = nMaxSize;
552 int nHeight = (int)(nMaxSize * dfAspectRatio);
556 if (bmpStyle.Width != bmpContent.Width ||
557 bmpStyle.Height != bmpContent.Height)
560 m_log.
WriteLine(
"Creating input network...");
562 net =
new Net<T>(m_cuda, m_log, m_param, m_evtCancel,
null,
Phase.TEST,
null, m_netShare, net_OnGetWorkspace, net_OnSetWorkspace);
565 if (m_rgWeights !=
null && !m_bUsingSharedNet)
572 prepare_data_blob(net, bmpStyle);
575 foreach (KeyValuePair<string, double> kvGram
in m_rgLayers[
"gram"])
577 string strGram = kvGram.Key;
579 colGramActivations.
Add(blobGram.
Clone());
582 prepare_data_blob(net, bmpContent);
585 foreach (KeyValuePair<string, double> kvContent
in m_rgLayers[
"content"])
587 string strContent = kvContent.Key;
589 colContentActivations.
Add(blobContent.
Clone());
599 foreach (KeyValuePair<string, double> kvInput
in m_rgLayers[
"input"])
601 string strName = kvInput.Key;
603 p.
name =
"input_" + strName;
610 net_param.layer.Add(p);
613 foreach (KeyValuePair<string, double> kvContent
in m_rgLayers[
"content"])
615 string strName = kvContent.Key;
616 string strScale1 =
"input_" + strName;
617 string strScale2 = strName;
618 Blob<T> blobContent = colContentActivations[strName];
620 if (m_dfContentDataScale != 1.0)
628 ps1.
bottom.Add(
"input_" + strName);
629 ps1.
top.Add(strScale1);
631 net_param.layer.Add(ps1);
640 ps2.
top.Add(strScale2);
642 net_param.layer.Add(ps2);
646 event_param.
name =
"event_" + strName;
647 event_param.
bottom.Add(strScale2);
648 event_param.
bottom.Add(strScale1);
650 event_param.
top.Add(
"event_" + strName);
652 net_param.layer.Add(event_param);
655 p.
name =
"loss_" + strName;
657 double dfScale = get_content_scale(blobContent);
660 p.
bottom.Add(
"event_" + strName);
662 p.
top.Add(
"loss_" + strName);
664 net_param.layer.Add(p);
667 foreach (KeyValuePair<string, double> kvGram
in m_rgLayers[
"gram"].ToList())
669 string strGramName = kvGram.Key;
670 Blob<T> blobGram = colGramActivations[strGramName];
673 event_param.
name =
"event_" + strGramName;
675 event_param.
bottom.Add(strGramName);
676 event_param.
bottom.Add(
"input_" + strGramName);
677 event_param.
top.Add(
"event_" + strGramName);
679 net_param.layer.Add(event_param);
682 p.
name =
"loss_" + strGramName;
685 double dfScale = get_style_scale(blobGram);
688 p.
bottom.Add(
"input_" + strGramName);
689 p.
bottom.Add(
"event_" + strGramName);
690 p.
top.Add(
"loss_" + strGramName);
692 net_param.layer.Add(p);
696 if (m_dfTVLossWeight != 0)
701 double dfWeight = m_dfTVLossWeight;
704 p.
bottom.Add(m_strDataBlobName);
705 p.
top.Add(
"loss_tv");
707 net_param.layer.Add(p);
713 for (
int i=0; i<net_param.layer.Count; i++)
717 if (p.
name ==
"input1")
720 net_param.layer[i].parameter_param.shape =
new BlobShape(data.
shape());
740 if (rgTypes.Contains(layer.
type))
757 solver_param.
display = m_nDisplayEvery;
762 solver_param.
base_lr = m_dfLearningRate;
763 solver_param.
type = m_solverType;
766 m_log.
WriteLine(
"Creating " + m_solverType.ToString() +
" solver with learning rate = " + m_dfLearningRate.ToString() +
"...");
770 solver =
new LBFGSSolver<T>(m_cuda, m_log, solver_param, m_evtCancel,
null,
null,
null, m_persist, 1, 0, m_netShare, net_OnGetWorkspace, net_OnSetWorkspace);
772 solver =
Solver<T>.
Create(m_cuda, m_log, solver_param, m_evtCancel,
null,
null,
null, m_persist, 1, 0, m_netShare, net_OnGetWorkspace, net_OnSetWorkspace);
783 eventLayer.
OnBackward += EventLayer_OnBackward;
787 prepare_input_param(solver.
net, bmpContent);
789 foreach (KeyValuePair<string, double> kvContent
in m_rgLayers[
"content"])
791 string strName = kvContent.Key;
792 Blob<T> blobDst = solver.
net.blob_by_name(
"input_" + strName);
793 Blob<T> blobSrc = colContentActivations[strName];
797 foreach (KeyValuePair<string, double> kvGram
in m_rgLayers[
"gram"])
799 string strName = kvGram.Key;
800 Blob<T> blobDst = solver.
net.blob_by_name(
"input_" + strName);
801 Blob<T> blobSrc = colGramActivations[strName];
806 colGramActivations =
null;
808 colContentActivations.
Dispose();
809 colContentActivations =
null;
816 if (nIntermediateOutput <= 0 || nIntermediateOutput == m_nIterations)
818 bEnablePartialSolution =
false;
819 nIntermediateOutput = m_nIterations;
822 int nIterations1 = m_nIterations / nIntermediateOutput;
824 if (m_rgWeights !=
null && !m_bUsingSharedNet)
826 Blob<T> blobInput = solver.
net.learnable_parameters[0];
827 solver.
net.learnable_parameters.RemoveAt(0);
828 solver.
net.LoadWeights(m_rgWeights, m_persist);
829 solver.
net.learnable_parameters.Insert(0, blobInput);
832 if (bEnablePartialSolution)
835 m_nPartialIteration = 0;
836 m_nPartialIterations1 = nIterations1;
837 m_nIntermediateOutput = nIntermediateOutput;
842 for (
int i = 0; i < nIterations1; i++)
847 solver.
Step(nIntermediateOutput,
TRAIN_STEP.NONE,
true,
true,
true);
853 Bitmap bmpTemp = save(solver.
net);
854 double dfPct = (double)i / (
double)nIterations1;
861 return save(solver.
net);
863 catch (Exception excpt)
878 if (colGramActivations !=
null)
881 if (colContentActivations !=
null)
882 colContentActivations.
Dispose();
892 private void net_OnSetWorkspace(
object sender,
WorkspaceArgs e)
898 m_cuda.DisableGhostMemory();
900 if (m_hWorkspaceData != 0)
901 m_cuda.FreeMemory(m_hWorkspaceData);
903 m_hWorkspaceData = m_cuda.AllocMemory((
long)m_lWorkspaceSizeInBytes);
905 m_cuda.ResetGhostMemory();
908 private void net_OnGetWorkspace(
object sender,
WorkspaceArgs e)
920 public Bitmap
ProcessNext(out Bitmap bmpIntermediate, out
int nIntermediateIdx)
924 bmpIntermediate =
null;
925 nIntermediateIdx = m_nPartialIteration * m_nIntermediateOutput;
927 if (m_solver ==
null)
928 throw new Exception(
"To run the next in process, the solver cannot be null! You must call Process first.");
930 m_solver.
Step(m_nIntermediateOutput,
TRAIN_STEP.NONE,
true,
true,
true);
935 m_nPartialIteration++;
937 if (m_nIntermediateOutput > 0 && m_nPartialIteration < m_nPartialIterations1)
939 bmpIntermediate = save(m_solver.
net);
943 double dfPct = (double)m_nPartialIteration / (
double)m_nPartialIterations1;
948 if (m_nPartialIteration < m_nPartialIterations1)
951 return save(m_solver.
net);
953 catch (Exception excpt)
959 if (m_nPartialIteration == m_nPartialIterations1)
961 if (m_solver !=
null)
973 long hTopDiff0 = e.
TopVec[0].mutable_gpu_diff;
974 long hBottomData1 = e.
BottomVec[1].gpu_data;
975 long hBottomDiff1 = e.
BottomVec[1].mutable_gpu_diff;
976 long hBottomDiff = e.
BottomVec[0].mutable_gpu_diff;
978 m_cuda.sign(nCount, hBottomData1, hBottomDiff1);
979 m_cuda.abs(nCount, hBottomDiff1, hBottomDiff1);
980 m_cuda.mul(nCount, hBottomDiff1, hTopDiff0, hBottomDiff);
987 if (
double.IsNaN(e.
Loss))
990 m_log.
FAIL(
"Loss = NAN!");
994 if (
double.IsInfinity(e.
Loss))
997 m_log.
FAIL(
"Loss = Infinity!");
1002 private void Solver_OnSnapshot(
object sender,
SnapshotArgs e)
1024 public static string CreateConfigurationString(
string strSolver,
double dfLearningRate,
int nMaxImageSize,
int nIterations,
int nIntermediateIterations, Dictionary<
string, Tuple<double, double>> rgWts, List<int> rgGpuID,
int nLBFGSCorrections,
double dfDataScale,
bool bAllowHs,
bool bAllowHsGram,
bool bAllowHsEvent,
bool bAllowHsScalar,
bool bAllowHsLoss)
1028 rgChildren.
Add(
"solver", strSolver);
1029 rgChildren.
Add(
"learning_rate", dfLearningRate);
1030 rgChildren.
Add(
"max_image_size", nMaxImageSize);
1031 rgChildren.
Add(
"iterations", nIterations);
1032 rgChildren.
Add(
"intermediate_iterations", nIntermediateIterations);
1035 foreach (KeyValuePair<
string, Tuple<double, double>> kv
in rgWts)
1038 layer.
Add(
"name", kv.Key);
1039 layer.
Add(
"style_wt", kv.Value.Item1);
1040 layer.
Add(
"content_wt", kv.Value.Item2);
1042 rgLayerWt.Add(
new RawProto(
"layer",
"", layer));
1045 rgChildren.Add(rgLayerWt);
1048 foreach (
int nGpuID
in rgGpuID)
1050 gpus.Add(
"gpuid", nGpuID.ToString());
1053 rgChildren.Add(gpus);
1054 rgChildren.Add(
"lbfgs_corrections", nLBFGSCorrections);
1055 rgChildren.Add(
"data_scale", dfDataScale);
1056 rgChildren.Add(
"allow_hs", bAllowHs);
1057 rgChildren.Add(
"allow_hs_gram", bAllowHsGram);
1058 rgChildren.Add(
"allow_hs_event", bAllowHsEvent);
1059 rgChildren.Add(
"allow_hs_scalar", bAllowHsScalar);
1060 rgChildren.Add(
"allow_hs_loss", bAllowHsLoss);
1085 public static Dictionary<string, Tuple<double, double>>
ParseConfigurationString(
string strConfig, out
string strSolver, out
double dfLearningRate, out
int nMaxImageSize, out
int nIterations, out
int nIntermediateIterations, out List<int> rgGpuID, out
int nLBFGSCorrections, out
double dfDataScale, out
bool bAllowHs, out
bool bAllowHsGram, out
bool bAllowHsEvent, out
bool bAllowHsScalar, out
bool bAllowHsLoss)
1091 if ((strVal = proto.
FindValue(
"solver")) !=
null)
1095 if ((strVal = proto.
FindValue(
"learning_rate")) !=
null)
1099 if ((strVal = proto.
FindValue(
"max_image_size")) !=
null)
1100 nMaxImageSize =
int.Parse(strVal);
1103 if ((strVal = proto.
FindValue(
"iterations")) !=
null)
1104 nIterations =
int.Parse(strVal);
1106 nIntermediateIterations = 0;
1107 if ((strVal = proto.
FindValue(
"intermediate_iterations")) !=
null)
1108 nIntermediateIterations =
int.Parse(strVal);
1110 Dictionary<string, Tuple<double, double>> rgLayers =
new Dictionary<string, Tuple<double, double>>();
1112 foreach (
RawProto styleProto
in style)
1114 string strLayer =
null;
1115 if ((strVal = styleProto.
FindValue(
"name")) !=
null)
1119 if ((strVal = styleProto.
FindValue(
"style_wt")) !=
null)
1123 if ((strVal = styleProto.
FindValue(
"content_wt")) !=
null)
1126 rgLayers.Add(strLayer,
new Tuple<double, double>(dfSWt, dfCWt));
1129 rgGpuID =
new List<int>();
1131 foreach (
RawProto gpuProto
in gpus)
1133 rgGpuID.
Add(
int.Parse(gpuProto.
Value));
1136 nLBFGSCorrections = 100;
1137 if ((strVal = proto.FindValue(
"lbfgs_corrections")) !=
null)
1138 nLBFGSCorrections =
int.Parse(strVal);
1141 if ((strVal = proto.FindValue(
"data_scale")) !=
null)
1145 if ((strVal = proto.FindValue(
"allow_hs")) !=
null)
1146 bAllowHs =
bool.Parse(strVal);
1148 bAllowHsGram =
true;
1149 if ((strVal = proto.FindValue(
"allow_hs_gram")) !=
null)
1150 bAllowHsGram =
bool.Parse(strVal);
1152 bAllowHsEvent =
true;
1153 if ((strVal = proto.FindValue(
"allow_hs_event")) !=
null)
1154 bAllowHsEvent =
bool.Parse(strVal);
1156 bAllowHsScalar =
true;
1157 if ((strVal = proto.FindValue(
"allow_hs_scalar")) !=
null)
1158 bAllowHsScalar =
bool.Parse(strVal);
1160 bAllowHsLoss =
true;
1161 if ((strVal = proto.FindValue(
"allow_hs_loss")) !=
null)
1162 bAllowHsLoss =
bool.Parse(strVal);
1185 m_nIteration = nIteration;
1187 m_dfPercent = dfPct;
1195 get {
return m_nIteration; }
1203 get {
return m_img; }
1211 get {
return m_dfPercent; }
The BaseParameter class is the base class for all other parameter classes.
static double ParseDouble(string strVal)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
void Reset()
Resets the event clearing any signaled state.
bool WaitOne(int nMs=int.MaxValue)
Waits for the signal state to occur.
void Set()
Sets the event to the signaled state.
The Datum class is a simple wrapper to the SimpleDatum class to ensure compatibility with the origina...
The ImageData class is a helper class used to convert between Datum, other raw data,...
static Bitmap GetImage(SimpleDatum d, ColorMapper clrMap=null, List< int > rgClrOrder=null)
Converts a SimplDatum (or Datum) into an image, optionally using a ColorMapper.
static Datum GetImageDataD(Bitmap bmp, int nChannels, bool bDataIsReal, int nLabel, bool bUseLockBitmap=true, int[] rgFocusMap=null)
The GetImageDataD function converts a Bitmap into a Datum using the double type for real data.
The Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
bool Enable
Enables/disables the Log. When disabled, the Log does not output any data.
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
The RawProtoCollection class is a list of RawProto objects.
void Add(RawProto p)
Adds a RawProto to the collection.
The RawProto class is used to parse and output Google prototxt file data.
string Value
Get/set the value of the node.
override string ToString()
Returns the RawProto as its full prototxt string.
static RawProto Parse(string str)
Parses a prototxt and places it in a new RawProto.
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
RawProtoCollection FindChildren(params string[] rgstrName)
Searches for all children with a given name in this node's children.
The Utility class provides general utility funtions.
The BackwardArgs are passed to the OnBackward event of the EventLayer.
The BlobCollection contains a list of Blobs.
void Dispose()
Release all resource used by the collection and its Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
The Blob is the main holder of data that moves through the Layers of the Net.
bool HalfSize
Returns whether or not this blob is using half sizes.
T[] mutable_cpu_data
Get data from the GPU and bring it over to the host, or Set data from the Host and send it over to th...
Blob< T > Clone()
Copies the Blob, including its data and diff.
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
void CopyFrom(Blob< T > src, int nSrcOffset, int nDstOffset, int nCount, bool bCopyData, bool bCopyDiff)
Copy from a source Blob.
List< int > shape()
Returns an array where each element contains the shape of an axis of the Blob.
int count()
Returns the total number of items in the Blob.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
int GetDeviceID()
Returns the current device id set within Cuda.
BlobCollection< T > BottomVec
Returns the bottom blobs.
BlobCollection< T > TopVec
Returns the top blobs.
Connects Layer's together into a direct acrylic graph (DAG) specified by a NetParameter
BlobCollection< T > Forward()
Run forward with the input Blob's already fed separately.
virtual void Dispose(bool bDisposing)
Releases all resources (GPU and Host) used by the Net.
void LoadWeights(byte[] rgWeights, IXPersist< T > persist, List< string > inputWtInfo=null, List< string > targetWtInfo=null, string strSkipBlobType=null)
Loads new weights into the Net.
Blob< T > param_by_name(string strName, bool bThrowExceptionOnError=true)
Returns a parameter given its name.
Blob< T > blob_by_name(string strName, bool bThrowExceptionOnError=true)
Returns a blob given its name.
CudaDnn< T > Cuda
Returns the instance of CudaDnn used by this network.
The PersistCaffe class is used to load and save weight files in the .caffemodel format.
The SnapshotArgs is sent to the Solver::OnSnapshot event which fires each time the Solver::Snapshot m...
int Iteration
Return the iteration of the test cycle.
The TrainingIterationArgs is sent to the Solver::OnTrainingIteration event that fires at the end of a...
double Loss
Returns the loss of the training cycle.
double SmoothedLoss
Retunrs the average loss after the training cycle.
The WorkspaceArgs are passed to both the Layer::OnSetWorkspace and Layer::OnGetWorkspace events.
long WorkspaceData
Get/set the handle to workspace data in GPU memory.
ulong WorkspaceSizeInBytes
Get/set the workspace memory size in bytes.
An interface for the units of computation which can be composed into a Net.
LayerParameter.LayerType type
Returns the LayerType of this Layer.
The EventLayer provides an event that fires on the forward pass and another that fires on the backwar...
EventHandler< BackwardArgs< T > > OnBackward
Defines the event that fires from within the backward pass.
Specifies the shape of a Blob.
Specifies the base parameter for all layers.
List< ParamSpec > parameters
Specifies the ParamSpec parameters of the LayerParameter.
string name
Specifies the name of this LayerParameter.
List< double > loss_weight
Specifies the loss weight.
LayerType type
Specifies the type of this LayerParameter.
bool use_halfsize
Specifies whether or not to use half sized memory or not.
GramParameter gram_param
Returns the parameter set when initialized with LayerType.GRAM
InputParameter input_param
Returns the parameter set when initialized with LayerType.INPUT
List< string > top
Specifies the active top connections (in the bottom, out the top)
ScalarParameter scalar_param
Returns the parameter set when initialized with LayerType.SCALAR
List< string > bottom
Specifies the active bottom connections (in the bottom, out the top).
LayerType
Specifies the layer type.
Specifies the parameters use to create a Net
static NetParameter FromProto(RawProto rp)
Parse a RawProto into a new instance of the parameter.
List< LayerParameter > layer
The layers that make up the net. Each of their configurations, including connectivity and behavior,...
Specifies training parameters (multipliers on global learning constants, and the name of other settin...
The SolverParameter is a parameter for the solver, specifying the train and test networks.
int lbgfs_corrections
Specifies the number of lbgfs corrections used with the L-BGFS solver.
List< int > test_iter
The number of iterations for each test.
NetParameter train_net_param
Inline train net param, possibly combined with one or more test nets.
SolverType
Defines the type of solver.
int test_interval
The number of iterations between two testing phases.
bool test_initialization
If true, run an initial test pass before the first iteration, ensuring memory availability and printi...
int display
The number of iterations between displaying info. If display = 0, no info will be displayed.
double base_lr
The base learning rate (default = 0.01).
SolverType type
Specifies the solver type.
double beta
Specifies the scaling factor applied after the gram operation.
bool disable_scaling_on_gradient
Specifies whether or not to apply the un-scaling of the alpha and beta values during the during the b...
double alpha
Specifies the scaling factor applied before the gram operation.
Specifies the parameters for the ScalarLayer
double value
Specifies the scalar value to apply.
ScalarOp operation
Specifies the scalar operation to apply (mul, add, etc).
bool passthrough_gradient
Specifies whether or not to pass-through the gradient without performing the back-prop calculation (d...
ScalarOp
Defines the scalar operations that may be performed.
Optimizes the parameters of a Net using L-BFGS. This implementation is based on minFunc,...
An interface for classes that perform optimization on Nets - this class serves as the base class for ...
void Dispose()
Discards the resources (GPU and Host) used by this Solver.
EventHandler< TrainingIterationArgs< T > > OnTrainingIteration
The OnTrainingIteration event fires at the end of each training iteration.
static SGDSolver< T > Create(CudaDnn< T > cuda, Log log, ProjectEx p, CancelEvent evtCancel, AutoResetEvent evtForceSnapshot, AutoResetEvent evtForceTest, IXDatabaseBase db, IXPersist< T > persist, int nSolverCount=1, int nSolverRank=0, Net< T > shareNet=null, onGetWorkspace getws=null, onSetWorkspace setws=null)
Create a new Solver based on the project containing the SolverParameter.
EventHandler< SnapshotArgs > OnSnapshot
The OnSnapshot event fires when the Solver detects that a snapshot is needed.
Net< T > net
Returns the main training Net.
bool Step(int nIters, TRAIN_STEP step=TRAIN_STEP.NONE, bool bZeroDiffs=true, bool bApplyUpdates=true, bool bDisableOutput=false, bool bDisableProgress=false, double? dfLossOverride=null, bool? bAllowSnapshot=null)
Steps a set of iterations through a training cycle.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Phase
Defines the Phase under which to run a Net.
The MyCaffe.common namespace contains common MyCaffe classes.
TRAIN_STEP
Defines the training stepping method (if any).
The MyCaffe.data namespace contains dataset creators used to create common testing datasets such as M...
The MyCaffe.layers.nt namespace contains all Neural Transfer related layers.
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
The MyCaffe.param.nt namespace defines the parameters used by the Nerual Style Transfer layers.
The MyCaffe.param namespace contains parameters used to create models.
The MyCaffe.solvers namespace contains all solver classes, including the base Solver.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...