2using System.Collections.Generic;
20 bool m_bExistTest =
false;
21 bool m_bExistTrain =
false;
22 bool m_bDatasetAdjusted =
false;
23 bool m_bDefaultSaveImagesToFile =
true;
25 int m_nOriginalProjectID = 0;
41 public ProjectEx(
string strName,
string strDsName =
null)
76 m_bExistTest = bExistTest;
77 m_bExistTrain = bExistTrain;
90 if (type.
Value.ToLower() ==
"annotateddata")
97 private string parse(
string str,
string strTarget,
string strDefault =
"UNKNOWN")
104 while (nPos1 < str.Length)
106 nPos1 = str.IndexOf(strTarget, nPos1);
110 if (nPos1 == 0 ||
char.IsWhiteSpace(str[nPos1 - 1]) || str[nPos1 - 1] ==
'\n' || str[nPos1 - 1] ==
'\r')
116 if (nPos1 >= str.Length)
119 nPos1 += strTarget.Length;
121 while (nPos1 < str.Length && (
char.IsWhiteSpace(str[nPos1]) || str[nPos1] ==
'\"'))
128 int nPos2 = str.IndexOfAny(
new char[] {
' ',
'\n',
'\r',
'\"',
'\t' }, nPos1);
130 strName = str.Substring(nPos1);
132 strName = str.Substring(nPos1, nPos2 - nPos1).Trim(
' ',
'\"');
137 private string getModelName(
string strDesc)
139 string strName = parse(strDesc,
"name:");
143 private string getSolverType(
string strDesc)
145 string strName = parse(strDesc,
"type:",
"SGD");
149 private void setDatasetFromProto(RawProto proto)
151 RawProtoCollection col = proto.FindChildren(
"layer");
152 string strSrcTest =
null;
153 string strSrcTrain =
null;
154 string strSrcTest2 =
null;
155 string strSrcTrain2 =
null;
157 foreach (RawProto rp
in col)
159 RawProto protoType = rp.FindChild(
"type");
160 if (protoType !=
null && protoType.Value ==
"Data")
162 RawProto protoParam = rp.FindChild(
"data_param");
163 if (protoParam !=
null)
165 bool bPrimary =
true;
167 RawProto protoPrimary = protoParam.FindChild(
"primary_data");
168 if (protoPrimary !=
null)
169 bPrimary =
bool.Parse(protoPrimary.Value);
171 RawProto protoSrc = protoParam.FindChild(
"source");
172 if (protoSrc !=
null)
174 RawProto protoInclude = rp.FindChild(
"include");
175 if (protoInclude !=
null)
177 RawProto protoPhase = protoInclude.FindChild(
"phase");
178 if (protoPhase !=
null)
182 if (protoPhase.Value ==
"TRAIN")
183 strSrcTrain = protoSrc.Value;
184 else if (protoPhase.Value ==
"TEST")
185 strSrcTest = protoSrc.Value;
189 if (protoPhase.Value ==
"TRAIN")
190 strSrcTrain2 = protoSrc.Value;
191 else if (protoPhase.Value ==
"TEST")
192 strSrcTest2 = protoSrc.Value;
201 if (strSrcTest !=
null)
207 if (strSrcTrain !=
null)
213 if (strSrcTest2 !=
null || strSrcTrain2 !=
null)
218 if (strSrcTest2 !=
null)
224 if (strSrcTrain2 !=
null)
232 private void setDatasetToProto(RawProto proto)
234 RawProtoCollection col = proto.FindChildren(
"layer");
240 foreach (RawProto rp
in col)
242 RawProto protoType = rp.FindChild(
"type");
243 if (protoType !=
null && (protoType.Value ==
"Data" || protoType.Value ==
"AnnotatedData"))
245 RawProto protoParam = rp.FindChild(
"data_param");
246 if (protoParam !=
null)
248 bool bPrimary =
true;
250 RawProto protoPrimary = protoParam.FindChild(
"primary_data");
251 if (protoPrimary !=
null)
252 bPrimary =
bool.Parse(protoPrimary.Value);
254 RawProto protoSrc = protoParam.FindChild(
"source");
255 if (protoSrc !=
null)
257 RawProto protoInclude = rp.FindChild(
"include");
258 if (protoInclude !=
null)
260 RawProto protoPhase = protoInclude.FindChild(
"phase");
261 if (protoPhase !=
null)
265 if (protoPhase.Value ==
"TRAIN")
267 if (strSrcTrain !=
null)
268 protoSrc.Value = strSrcTrain;
270 else if (protoPhase.Value ==
"TEST")
272 if (strSrcTest !=
null)
273 protoSrc.Value = strSrcTest;
278 if (protoPhase.Value ==
"TRAIN")
280 if (strSrcTrain2 !=
null)
281 protoSrc.Value = strSrcTrain2;
283 else if (protoPhase.Value ==
"TEST")
285 if (strSrcTest2 !=
null)
286 protoSrc.Value = strSrcTest2;
302 get {
return m_bDatasetAdjusted; }
303 set { m_bDatasetAdjusted = value; }
314 if (m_protoSolver ==
null)
328 strProperties = rprop.
Value;
343 string strPhase = rpPhase.
Value.ToUpper();
345 if (strPhase ==
Phase.TEST.ToString())
348 if (strPhase ==
Phase.TRAIN.ToString())
361 if (m_protoModel ==
null)
368 Phase p = getPhase(rp1);
370 if (p == phase || phase ==
Phase.NONE)
405 if (
string.IsNullOrEmpty(strVal))
420 if (m_protoModel ==
null)
427 Phase p = getPhase(rp1);
429 if (p == phase || phase ==
Phase.NONE)
455 if (m_protoSolver ==
null)
494 return (
int)dfVal.Value;
508 return bool.Parse(strVal);
525 get {
return m_project.
Name; }
526 set { m_project.
Name = value; }
534 get {
return m_project.
ID; }
544 if (m_nOriginalProjectID > 0)
545 return m_nOriginalProjectID;
551 m_nOriginalProjectID = value;
560 get {
return m_project.
Owner; }
561 set { m_project.
Owner = value; }
569 get {
return m_project.
Active; }
579 if (m_protoSolver ==
null)
583 if (
string.IsNullOrEmpty(strCustomTrainer))
586 if (strCustomTrainer ==
"RL.Trainer")
589 if (strCustomTrainer ==
"RNN.Trainer")
592 if (strCustomTrainer ==
"Dual.Trainer")
604 get {
return m_stage; }
605 set { m_stage = value; }
622 get {
return m_project.
Parameters.
Find(
"UseTrainingSourceForTesting",
false); }
727 get {
return (m_protoSolver ==
null) ? null : m_protoSolver.
ToString(); }
732 m_protoSolver =
null;
734 if (value !=
null && value.Length > 0)
741 setDatasetFromProto(m_protoSolver);
743 setDatasetToProto(m_protoSolver);
758 get {
return (m_protoModel ==
null) ? null : m_protoModel.
ToString(); }
761 m_project.
ModelName = getModelName(value);
765 if (value !=
null && value.Length > 0)
772 setDatasetFromProto(m_protoModel);
774 setDatasetToProto(m_protoModel);
789 get {
return m_project.
Group; }
856 get {
return m_state.
Error; }
857 set { m_state.
Error = value; }
865 get {
return m_state.
State; }
866 set { m_state.
State = value; }
874 get {
return m_state.
Weights; }
875 set { m_state.
Weights = value; }
897 get {
return m_project.
Dataset; }
923 if (!
int.TryParse(p.
Value, out nID))
935 p.
Value = value.ToString();
944 get {
return m_bExistTest; }
952 get {
return m_bExistTrain; }
987 if (m_protoSolver !=
null)
991 if (protoVar !=
null)
992 protoVar.
Value = strVal;
1010 using (StreamReader sr =
new StreamReader(strFile))
1022 using (StreamReader sr =
new StreamReader(strFile))
1057 string strLayers =
"layer";
1059 if (rgLayers.
Count == 0)
1062 strLayers =
"layers";
1065 bool bDirty =
false;
1069 int nTrainDataLayerIdx = -1;
1070 int nTestDataLayerIdx = -1;
1071 int nSoftmaxLossLayerIdx = -1;
1072 int nAccuracyLayerIdx = -1;
1075 foreach (
RawProto layer
in rgLayers)
1077 bool bRemove =
false;
1082 string strType = type.
Value.ToLower();
1084 if (strType ==
"softmax")
1085 protoSoftmax = layer;
1087 if (include !=
null)
1092 if (phase.
Value !=
"TEST" && phase.
Value !=
"TRAIN")
1096 if (strType ==
"data" || strType ==
"tokenizeddata")
1098 if (phase.
Value ==
"TRAIN")
1099 nTrainDataLayerIdx = nIdx;
1101 nTestDataLayerIdx = nIdx;
1103 else if (strType ==
"accuracy")
1105 nAccuracyLayerIdx = nIdx;
1107 else if (strType ==
"softmaxwithloss")
1109 nSoftmaxLossLayerIdx = nIdx;
1115 if (!bRemove && exclude !=
null)
1120 if (phase.
Value ==
"TEST" || phase.
Value ==
"TRAIN")
1127 rgRemove.Add(layer);
1133 if (nTestDataLayerIdx < 0)
1135 string strProto = getDataLayerProto(strLayers, strName, bCaffeFormat, 16,
"",
Phase.TEST);
1138 if (nTrainDataLayerIdx > 0)
1139 rgLayers.
Insert(nTrainDataLayerIdx + 1, protoData);
1141 rgLayers.Insert(0, protoData);
1146 if (nTrainDataLayerIdx < 0)
1148 string strProto = getDataLayerProto(strLayers, strName, bCaffeFormat, 16,
"",
Phase.TRAIN);
1150 rgLayers.
Insert(0, protoData);
1154 foreach (
RawProto layer
in rgRemove)
1156 proto.RemoveChild(layer);
1159 if (protoSoftmax !=
null)
1163 type.
Value =
"SoftmaxWithLoss";
1165 protoSoftmax.Children.Add(
new RawProto(
"bottom",
"label"));
1168 string strInclude =
"include { phase: TRAIN }";
1171 string strLoss =
"loss_param { normalization: VALID }";
1176 if (nAccuracyLayerIdx < 0)
1178 string strBottom =
null;
1179 if (rgLayers.Count > 0)
1181 RawProto last = rgLayers[rgLayers.Count - 1];
1184 if (colBtm.
Count > 0)
1185 strBottom = colBtm[0].Value;
1188 if (strBottom !=
null)
1190 string strProto = getAccuracyLayerProto(strLayers, strBottom);
1192 rgLayers.
Add(protoData);
1197 if (bDirty || proto.FindChildren(
"input_dim").Count > 0)
1199 rgLayers.Insert(0, protoName);
1200 proto =
new RawProto(
"root",
null, rgLayers);
1206 private static string getDataLayerProto(
string strLayer,
string strName,
bool bCaffeFormat,
int nBatchSize,
string strSrc,
Phase phase)
1208 string strRgb = (bCaffeFormat) ?
"BGR" :
"RGB";
1209 string strPhase = phase.ToString();
1210 return strLayer +
" { name: \"" + strName +
"\" type: \"Data\" top: \"data\" top: \"label\" include { phase: " + strPhase +
" } transform_param { scale: 1 mirror: True use_imagedb_mean: True color_order: " + strRgb +
" } data_param { source: \"" + strSrc +
"\" batch_size: " + nBatchSize.ToString() +
" backend: IMAGEDB enable_random_selection: True } }";
1213 private static string getAccuracyLayerProto(
string strLayer,
string strBottom)
1215 return strLayer +
" { name: \"accuracy\" type: \"Accuracy\" bottom: \"" + strBottom +
"\" bottom: \"label\" top: \"accuracy\" include { phase: TEST } accuracy_param { top_k: 1 } }";
1218 private static PhaseStageCollection getPhases(RawProto proto,
string strType)
1220 PhaseStageCollection psCol =
new PhaseStageCollection();
1222 RawProtoCollection type = proto.FindChildren(strType);
1223 if (type ==
null || type.Count == 0)
1226 return getPhases(type);
1229 private static PhaseStageCollection getPhases(RawProtoCollection col)
1231 PhaseStageCollection psCol =
new PhaseStageCollection();
1233 foreach (RawProto proto1
in col)
1235 RawProto protoPhase = proto1.FindChild(
"phase");
1236 if (protoPhase ==
null)
1240 RawProto protoStage = proto1.FindChild(
"stage");
1241 if (protoStage !=
null)
1243 if (protoStage.Value ==
Stage.RL.ToString())
1246 else if (protoStage.Value ==
Stage.RNN.ToString())
1251 if (protoPhase !=
null)
1253 if (protoPhase.Value ==
Phase.ALL.ToString())
1256 else if (protoPhase.Value ==
Phase.RUN.ToString())
1259 else if (protoPhase.Value ==
Phase.TEST.ToString())
1262 else if (protoPhase.Value ==
Phase.TRAIN.ToString())
1263 phase =
Phase.TRAIN;
1266 psCol.Add(phase, stage);
1272 private static bool includeLayer(RawProto layer,
Stage stage, out PhaseStageCollection psInclude, out PhaseStageCollection psExclude)
1274 psInclude = getPhases(layer,
"include");
1275 psExclude = getPhases(layer,
"exlcude").FindAllWith(stage);
1277 PhaseStageCollection psInclude1 = psInclude.FindAllWith(
Stage.NONE);
1278 PhaseStageCollection psInclude2 = psInclude.FindAllWith(stage);
1279 PhaseStageCollection psInclude3 = psInclude.FindAllWith(
Phase.NONE,
Phase.ALL,
Phase.RUN);
1280 psExclude = psExclude.FindAllWith(
Phase.RUN);
1282 if (psExclude.Count > 0)
1285 if (psInclude.Count > 0)
1287 if (psInclude3.Count == 0 || (psInclude1.Count == 0 && psInclude2.Count == 0))
1309 public static RawProto CreateModelForRunning(
string strModelDescription,
string strName,
int nNum,
int nChannels,
int nHeight,
int nWidth, out
RawProto protoTransform, out
bool bSkipTransformParam,
Stage stage =
Stage.NONE,
bool bSkipLossLayer =
false)
1311 PhaseStageCollection psInclude;
1312 PhaseStageCollection psExclude;
1315 int nInputInsertIdx = -1;
1316 int nInputShapeInsertIdx = -1;
1317 bool bNoInput =
false;
1318 bool bNameSet =
false;
1320 protoTransform =
null;
1321 bSkipTransformParam =
false;
1328 bool bUsesLstm =
false;
1330 foreach (
RawProto layer
in rgLayers)
1335 string strType = type.
Value.ToLower();
1336 if (strType ==
"lstm")
1341 else if (!bNameSet && (
1342 strType ==
"data" ||
1343 strType ==
"annotated_data" ||
1344 strType ==
"tokenizeddata" ||
1345 strType.StartsWith(
"tokenizeddatapairs")))
1348 if (tops !=
null && tops.
Count > 0)
1350 if (strType ==
"tokenizeddata" ||
1351 strType.StartsWith(
"tokenizeddatapairs"))
1354 strName = tops[0].Value;
1361 List<Tuple<string, int, int, int, int>> rgInputs =
new List<Tuple<string, int, int, int, int>>();
1362 rgInputs.
Add(
new Tuple<string, int, int, int, int>(strName, nNum, nChannels, nHeight, nWidth));
1364 bool bFoundInput =
false;
1365 bool bFoundMemoryData =
false;
1366 bool bSkipBottomRename =
false;
1368 foreach (
RawProto layer
in rgLayers)
1373 string strType = type.
Value.ToLower();
1374 if (strType ==
"input")
1378 if (includeLayer(layer, stage, out psInclude, out psExclude))
1384 if (input_param !=
null)
1390 for (
int i = 0; i < rgTop.
Count; i++)
1392 if (bUsesLstm && i < 2)
1395 if (rgDim.
Count > 1)
1397 rgDim[1].Value =
"1";
1401 if (rgTop[i].Value.ToLower() !=
"label")
1403 List<int> rgVal =
new List<int>();
1410 nNum = (rgVal.Count > 0) ? rgVal[0] : 1;
1411 nChannels = (rgVal.Count > 1) ? rgVal[1] : 1;
1412 nHeight = (rgVal.Count > 2) ? rgVal[2] : 1;
1413 nWidth = (rgVal.Count > 3) ? rgVal[3] : 1;
1415 rgInputs.Add(
new Tuple<string, int, int, int, int>(rgTop[i].Value, nNum, nChannels, nHeight, nWidth));
1422 else if (strType ==
"memorydata")
1424 bFoundMemoryData =
true;
1426 if (includeLayer(layer, stage, out psInclude, out psExclude))
1432 else if (strType ==
"data")
1434 if (rgInputs.Count > 0)
1437 if (colTop.
Count > 0)
1439 rgInputs[0] =
new Tuple<string, int, int, int, int>(colTop[0].Value, rgInputs[0].Item2, rgInputs[0].Item3, rgInputs[0].Item4, rgInputs[0].Item5);
1444 else if (strType ==
"tokenizeddata" || strType.StartsWith(
"tokenizeddatapairs"))
1446 if (rgInputs.Count > 0)
1449 if (colTop.
Count > 0)
1451 if (strType.StartsWith(
"tokenizeddatapairs"))
1453 rgInputs[0] =
new Tuple<string, int, int, int, int>(
"encin", rgInputs[0].Item2, rgInputs[0].Item3, rgInputs[0].Item4, rgInputs[0].Item5);
1454 rgInputs.Add(
new Tuple<string, int, int, int, int>(
"decin", rgInputs[0].Item2, rgInputs[0].Item3, rgInputs[0].Item4, rgInputs[0].Item5));
1455 layer.
Children.
Add<
string>(
"bottom",
new List<string>() {
"encin",
"decin" });
1459 rgInputs[0] =
new Tuple<string, int, int, int, int>(
"encin", rgInputs[0].Item2, rgInputs[0].Item3, rgInputs[0].Item4, rgInputs[0].Item5);
1460 layer.
Children.
Add<
string>(
"bottom",
new List<string>() {
"encin" });
1463 bSkipBottomRename =
true;
1469 if (bFoundInput && bFoundMemoryData)
1487 input.
Value = rgInputs[0].Item1;
1491 for (
int i = 0; i < rgInputs.Count; i++)
1495 nInputInsertIdx = nNameIdx;
1500 input_shape = proto.
FindChild(
"input_shape");
1501 if (input_shape !=
null)
1505 if (colDim.
Count > 0)
1506 colDim[0].Value = rgInputs[0].Item2.ToString();
1508 if (colDim.
Count > 1)
1509 colDim[1].Value = rgInputs[0].Item3.ToString();
1511 if (colDim.
Count > 2)
1512 colDim[2].Value = rgInputs[0].Item4.ToString();
1514 if (colDim.
Count > 3)
1515 colDim[3].Value = rgInputs[0].Item5.ToString();
1519 for (
int i = 0; i < rgInputs.Count; i++)
1521 input_shape =
new RawProto(
"input_shape",
"");
1523 nNum = rgInputs[i].Item2;
1524 nChannels = rgInputs[i].Item3;
1525 nHeight = rgInputs[i].Item4;
1526 nWidth = rgInputs[i].Item5;
1531 if (nHeight > 1 || nWidth > 1)
1537 rgInputShape.
Add(input_shape);
1538 nInputShapeInsertIdx = nNameIdx;
1544 if (net_name !=
null)
1545 net_name.
Value +=
"-Live";
1549 List<RawProto> rgProtoSoftMaxLoss =
new List<basecode.RawProto>();
1552 foreach (
RawProto layer
in rgLayers)
1557 string strType = type.
Value.ToLower();
1558 bool bKeepLayer =
false;
1560 bool bInclude = includeLayer(layer, stage, out psInclude, out psExclude);
1562 if (strType ==
"data" || strType ==
"annotateddata" || strType ==
"batchdata" || strType ==
"tokenizeddata" || strType.StartsWith(
"tokenizeddatapairs"))
1564 if (psInclude.Find(
Phase.TEST, stage) !=
null)
1565 protoTransform = layer.
FindChild(
"transform_param");
1567 if (strType ==
"tokenizeddata" || strType.StartsWith(
"tokenizeddatapairs"))
1569 bSkipTransformParam =
true;
1571 if (psInclude !=
null && psInclude.Count > 0 && psInclude[0].Phase ==
Phase.TRAIN)
1577 if (rpParam !=
null)
1580 if (rpBatch !=
null)
1581 rpBatch.
Value =
"1";
1586 else if (strType ==
"decode")
1588 List<RawProto> rgBtm =
new List<RawProto>();
1592 if (child.
Name ==
"bottom")
1596 if (rgBtm.Count > 0)
1607 rgRemove.Add(layer);
1609 else if (psExclude.Find(
Phase.RUN, stage) !=
null)
1611 rgRemove.Add(layer);
1613 else if (strType ==
"input")
1615 rgRemove.Add(layer);
1617 else if (strType ==
"softmaxwithloss" ||
1618 strType ==
"softmaxcrossentropy_loss" ||
1619 strType ==
"softmaxcrossentropyloss" ||
1620 strType ==
"softmaxcrossentropy2_loss" ||
1621 strType ==
"softmaxcrossentropy2loss")
1623 if (!bSkipLossLayer)
1625 rgProtoSoftMaxLoss.Add(layer);
1630 rgRemove.Add(layer);
1633 else if (strType ==
"memoryloss" ||
1634 strType ==
"contrastive_loss" ||
1635 strType ==
"contrastiveloss" ||
1636 strType ==
"euclidean_loss" ||
1637 strType ==
"euclideanloss" ||
1638 strType ==
"hinge_loss" ||
1639 strType ==
"hingeloss" ||
1640 strType ==
"infogain_loss" ||
1641 strType ==
"infogainloss" ||
1642 strType ==
"multinomiallogistic_loss" ||
1643 strType ==
"multinomiallogisticloss" ||
1644 strType ==
"sigmoidcrossentropy_loss" ||
1645 strType ==
"sigmoidcrossentropyloss" ||
1646 strType ==
"triplet_loss" ||
1647 strType ==
"tripletloss" ||
1648 strType ==
"triplet_loss_simple" ||
1649 strType ==
"tripletlosssimple")
1651 rgRemove.Add(layer);
1653 else if (strType ==
"softmax")
1655 protoSoftMax = layer;
1657 else if (strType ==
"labelmapping")
1659 rgRemove.Add(layer);
1661 else if (strType ==
"debug")
1663 rgRemove.Add(layer);
1665 else if (strType ==
"tokenizeddata" || strType.StartsWith(
"tokenizeddatapairs"))
1667 if (psInclude.Count > 0 && psInclude[0].Phase ==
Phase.TRAIN)
1680 if (!bKeepLayer && psInclude.FindAllWith(
Phase.TEST,
Phase.TRAIN).Count > 0 && psInclude.FindAllWith(
Phase.RUN).Count == 0)
1682 rgRemove.Add(layer);
1687 if (max_btm !=
null)
1692 if (phase1 !=
null && phase1.
Value ==
"RUN" && (stage1 ==
null || stage1.
Value == stage.ToString() || stage1.
Value ==
Stage.NONE.ToString()))
1699 List<int> rgRemoveIdx =
new List<int>();
1703 if (layer.
Children[i].Name !=
"bottom")
1710 for (
int i = nBtmEnd - 1; i >= nBtmIdx + nCount; i--)
1720 if (exclude !=
null)
1725 if (phase.
Value ==
"RUN")
1727 if (!rgRemove.Contains(layer))
1728 rgRemove.Add(layer);
1734 foreach (
RawProto protoSoftMaxLoss
in rgProtoSoftMaxLoss)
1736 if (protoSoftMax !=
null)
1738 rgRemove.Add(protoSoftMaxLoss);
1744 type.
Value =
"Softmax";
1748 for (
int i = 1; i < colBtm.
Count; i++)
1750 protoSoftMaxLoss.
RemoveChild(
"bottom", colBtm[i].Value,
true);
1755 foreach (
RawProto layer
in rgRemove)
1757 proto.RemoveChild(layer);
1764 if (btm !=
null && !bSkipBottomRename)
1765 btm.
Value = strName;
1768 if (input !=
null && input_shape !=
null)
1770 if (protoTransform !=
null)
1776 bool bActive = (bool)resize.
FindValue(
"active", typeof(
bool));
1779 int nNewHeight = (int)resize.
FindValue(
"height", typeof(
int));
1780 int nNewWidth = (int)resize.
FindValue(
"width", typeof(
int));
1782 if (rgInputShape[0].Children.Count < 1)
1783 rgInputShape[0].Children.Add(
new RawProto(
"dim",
"1"));
1785 if (rgInputShape[0].Children.Count < 2)
1786 rgInputShape[0].Children.Add(
new RawProto(
"dim",
"1"));
1788 if (rgInputShape[0].Children.Count < 3)
1789 rgInputShape[0].Children.Add(
new RawProto(
"dim", nNewHeight.ToString()));
1791 rgInputShape[0].Children[2] =
new RawProto(
"dim", nNewHeight.ToString());
1793 if (rgInputShape[0].Children.Count < 4)
1794 rgInputShape[0].Children.Add(
new RawProto(
"dim", nNewWidth.ToString()));
1796 rgInputShape[0].Children[3] =
new RawProto(
"dim", nNewWidth.ToString());
1801 for (
int i = rgInputShape.Count - 1; i >= 0; i--)
1803 proto.Children.Insert(0, rgInputShape[i]);
1806 for (
int i = rgInput.Count - 1; i >= 0; i--)
1808 proto.Children.Insert(0, rgInput[i]);
1824 if (dataset ==
null)
1831 bool bResized =
false;
1873 if (dataset ==
null)
1876 if (dataset.
Name ==
"MODEL")
1877 return strModelDesc;
1879 string strTypeLast =
null;
1882 List<RawProto> rgLastIp =
new List<RawProto>();
1884 RawProto protoDataTrainBatch =
null;
1885 RawProto protoDataTestBatch =
null;
1887 if (colLayers.
Count == 0)
1892 foreach (
RawProto protoChild
in colLayers)
1897 string strType = type.
Value.ToLower();
1899 if (strType ==
"data")
1904 if (data_param !=
null)
1909 if (include !=
null)
1915 if (phase.
Value ==
"TEST")
1917 protoDataTestBatch = batchProto;
1921 source.
Value = dataset.TestingSource.Name;
1922 nCropSize = dataset.TestingSource.Height;
1931 protoDataTrainBatch = batchProto;
1935 source.
Value = dataset.TrainingSource.Name;
1936 nCropSize = dataset.TrainingSource.Height;
1948 if (transform_param !=
null)
1951 if (crop_size !=
null)
1955 if (nCropSize != nSize)
1956 crop_size.
Value = nCropSize.ToString();
1960 else if (strType.Contains(
"loss"))
1962 if (colIP.Count > 0)
1964 rgLastIp.Add(colIP[0]);
1968 else if (strType ==
"inner_product" || strType ==
"innerproduct")
1970 colIP.Insert(0, protoChild);
1973 protoLast = protoChild;
1974 strTypeLast = strType;
1977 if (protoDataTestBatch !=
null && protoDataTrainBatch !=
null)
1979 int nTestSize =
int.
Parse(protoDataTestBatch.Value);
1980 int nTrainSize =
int.
Parse(protoDataTrainBatch.Value);
1982 if (nTrainSize < nTestSize)
1983 protoDataTrainBatch.
Value = nTestSize.ToString();
1988 foreach (
RawProto lastIp
in rgLastIp)
1991 if (protoParam !=
null)
1994 if (protoNumOut !=
null)
1996 int nNumOut = dataset.TrainingSource.Labels.Count;
2000 protoNumOut.
Value = nNumOut.ToString();
2008 return proto.ToString();
2024 public static string FindLayerParameter(
string strModelDescription,
string strLayerName,
string strLayerType,
string strParam,
string strField,
Phase phaseMatch =
Phase.NONE)
2031 foreach (
RawProto layer
in rgLayers)
2036 if (strLayerType == type.
Value.ToString() && (strLayerName ==
null || name.
Value.ToString() == strLayerName))
2038 if (phaseMatch !=
Phase.NONE)
2042 if (include !=
null)
2047 if (phase.
Value == phaseMatch.ToString())
2056 if (firstFound ==
null)
2062 if (firstFound ==
null)
2068 if (firstFound ==
null)
2073 if (strParam !=
null)
2089 if (m_protoSolver ==
null)
2100 if (protoTestInterval !=
null)
2102 if (protoTestInterval.
Value !=
"0")
2104 protoTestInterval.
Value =
"0";
2109 if (protoTestInit !=
null)
2111 if (protoTestInit.
Value !=
"False")
2113 protoTestInit.
Value =
"False";
2118 if (protoTestIter !=
null)
2136 string strName =
Name;
2138 if (strName ==
null || strName.Length == 0)
2142 if (strModelDesc !=
null && strModelDesc.Length > 0)
2144 int nPos = strModelDesc.IndexOf(
"name:");
2147 nPos = strModelDesc.IndexOf(
"Name:");
2152 int nPos2 = strModelDesc.IndexOfAny(
new char[] {
' ',
'\n',
'\r' }, nPos);
2155 strName = strModelDesc.Substring(nPos + 5, nPos2).Trim();
2159 if (strName.Length == 0)
2160 strName =
"(ID = " + m_project.
ID.ToString() +
")";
2163 return "Project: " + strName +
" -> Dataset: " + m_project.
Dataset.
Name;
2167 class PhaseStageCollection
2169 List<PhaseStage> m_rgItems =
new List<PhaseStage>();
2171 public PhaseStageCollection()
2177 get {
return m_rgItems.Count; }
2180 public PhaseStage
this[
int nIdx]
2182 get {
return m_rgItems[nIdx]; }
2187 PhaseStage ps = Find(p, s);
2190 m_rgItems.Add(
new PhaseStage(p, s));
2199 foreach (PhaseStage ps
in m_rgItems)
2201 if (ps.Phase == p && ps.Stage == s)
2208 public PhaseStageCollection FindAllWith(
Stage stage)
2210 PhaseStageCollection psCol =
new PhaseStageCollection();
2212 foreach (PhaseStage ps
in m_rgItems)
2214 if (ps.Stage == stage)
2215 psCol.Add(ps.Phase, ps.Stage);
2220 public PhaseStageCollection FindAllWith(params
Phase[] phase)
2222 PhaseStageCollection psCol =
new PhaseStageCollection();
2224 foreach (PhaseStage ps
in m_rgItems)
2226 if (phase.Contains(ps.Phase))
2227 psCol.Add(ps.Phase, ps.Stage);
2247 get {
return m_phase; }
2252 get {
return m_stage; }
The BaseParameter class is the base class for all other parameter classes.
static bool TryParse(string strVal, out double df)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
static double ParseDouble(string strVal)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
The OverrideProjectArgs is passed as an argument to the OnOverrideModel and OnOverrideSolver events f...
RawProto Proto
Get/set the RawProto used.
The ProjectEx class manages a project containing the solver description, model description,...
string GetLayerSettingEx(Phase phase, string strLayer, string strParam)
Returns the setting of a Layer (if found).
bool DisableTesting()
Disables the testing interval so that no test passes are run.
SNAPSHOT_WEIGHT_UPDATE_METHOD SnapshotWeightUpdateMethod
Returns the snapshot weight update favor. The snapshot can favor an improving accuracy,...
GroupDescriptor ModelGroup
Return the model group descriptor of the group that the Project participates in (if any).
string GpuOverride
Returns the list of comma separated GPU ID's that are to be used when training this Project.
int TargetDatasetID
Get/set the dataset ID of the target dataset (if exists), otherwise return 0.
bool HasResults
Return whether or not the project has results from a training session.
ProjectEx(ProjectDescriptor prj, StateDescriptor state=null, bool bExistTrain=false, bool bExistTest=false, bool bQueryModel=true, bool bQuerySolver=true)
The ProjectEx constructor.
string? SolverDescription
Get/set the solver description script used by the Project.
RawProto CreateModelForRunning(string strName, int nNum, int nChannels, int nHeight, int nWidth, out RawProto protoTransform, out bool bSkipTransformParam, Stage stage=Stage.NONE, bool bSkipLossLayer=false)
Create a model description as a RawProto for running the Project.
bool EnableRandomSelection
Returns whether or not random image selection is enabled. When enabled, images are randomly selected ...
string Name
Get/set the name of the Project.
ValueDescriptorCollection ProjectPerformanceItems
Return Project performance metrics.
int ID
Returns the ID of the Project in the database.
string DatasetName
Return the name of the dataset used.
SNAPSHOT_LOAD_METHOD SnapshotLoadMethod
Returns the snapshot load method. When loading the best error or accuracy, the snapshot loaded may no...
bool Active
Returns whether or not the Project is active.
int ImageLoadLimitRefreshPeriod
Returns the image load limit refresh period in milliseconds.
bool UseTrainingSourceForTesting
Returns whether or not the Project uses the training data source when testing (default = false).
int Iterations
Get/set the current number of iterations that the Project has been trained.
int TotalIterations
Get/set the total number of iterations that the Project has been trained.
bool ExistTrainResults
Return whether or not training results exist.
static RawProto CreateModelForRunning(string strModelDescription, string strName, int nNum, int nChannels, int nHeight, int nWidth, out RawProto protoTransform, out bool bSkipTransformParam, Stage stage=Stage.NONE, bool bSkipLossLayer=false)
Create a model description as a RawProto for running the Project.
SettingsCaffe Settings
Get/set the Caffe setting to use with the Project.
static string FindLayerParameter(string strModelDescription, string strLayerName, string strLayerType, string strParam, string strField, Phase phaseMatch=Phase.NONE)
This method searches for a given parameter within a given layer, optionally for a certain Phase.
string ModelName
Return the name of the model used by the Project.
bool RequiresDataCriteria()
Returns whether or not the data criteria is required by the current project model (e....
ProjectEx(string strName, string strDsName=null)
The ProjectEx constructor.
double ImageLoadLimitRefreshPercent
Returns the image load limit refresh percentage (to update).
double? GetSolverSettingAsNumeric(string strParam)
Get a setting from the solver descriptor as a double value.
ParameterDescriptorCollection Parameters
Returns any project parameters that may exist (if any).
void SetDataset(DatasetDescriptor dataset)
Sets the dataset used by the Project, overriding the current dataset used.
DatasetDescriptor Dataset
Return the descriptor of the dataset used.
TRAINING_CATEGORY TrainingCategory
Returns the training category of the project, or NONE if no custom trainer is used.
int? GetSolverSettingAsInt(string strParam)
Get a setting from the solver descriptor as an integer value.
string GetCustomTrainer(out string strProperties)
Returns the custom trainer and properties used by the project (if any).
void LoadSolverFile(string strFile)
Load the solver description from a file.
int ImageLoadLimit
Returns the image load limit.
bool ExistTestResults
Return whether or not testing results exist.
string SolverType
Return the type of the Solver used by the Project.
string? ModelDescription
Get/set the model description script used by the Project.
GroupDescriptor ProjectGroup
Return the project group descriptor of the group that the Project resides (if any).
EventHandler< OverrideProjectArgs > OnOverrideSolver
The OverrideSolver event fires each time the SetDataset function is called.
double? GetLayerSetting(Phase phase, string strLayer, string strParam)
Returns the setting of a Layer (if found).
byte[] WeightsState
Get/set the weight state.
DB_LOAD_METHOD ImageLoadMethod
Returns the method used to load the images into memory. Loading all images into memory has the highes...
override string ToString()
Returns a string representation of the Project.
int GetBatchSize(Phase phase)
Returns the batch size of the project used in a given Phase.
bool EnablePairSelection
Returns whether or not pair selection is enabled. When using pair selection, images are queried in pa...
string GetSolverSetting(string strParam)
Get a setting from the solver descriptor.
bool DatasetAdjusted
Get/set whether or not the dataset for the project has been changed.
int OriginalID
Get/set the original project ID.
DatasetDescriptor DatasetTarget
Returns the target dataset (if exists) or null if it does not.
string Owner
Get/set the ID of the Project owner.
double BestAccuracy
Get/set the best accuracy observed while testing the Project.
void LoadModelFile(string strFile)
Load the model description from a file.
double BestError
Get/set the best error observed while training the Project.
bool SetSolverVariable(string strVar, string strVal)
Set a given Solver variable in the solver description script.
bool EnableLabelBalancing
Returns whether or not label balancing is enabled. When enabled, first the label set is randomly sele...
GroupDescriptor DatasetGroup
Return the dataset group descriptor of the group that the Project participates in (if any).
bool EnableLabelBoosting
Returns whether or not label boosting is enabled. When using Label boosting, images are selected from...
static RawProto CreateModelForTraining(string strModelDescription, string strName, bool bCaffeFormat=false)
Create a model description as a RawProto for training the Project.
byte[] SolverState
Get/set the solver state.
double SuperBoostProbability
Get/set the super boost probability used by the Project.
Stage Stage
Return the stage under which the project was opened.
EventHandler< OverrideProjectArgs > OnOverrideModel
The OverrrideModel event fires each time the SetDataset function is called.
static string SetDataset(string strModelDesc, DatasetDescriptor dataset, out bool bResized, bool bUpdateOutputs=false)
Sets the dataset of a model, overriding the current dataset used.
bool? GetSolverSettingAsBool(string strParam)
Get a setting from the solver descriptor as a boolean value.
The RawProtoCollection class is a list of RawProto objects.
bool Remove(RawProto p)
Removes a RawProto from the collection.
void RemoveAt(int nIdx)
Removes the RawProto at a given index in the collection.
void Add(RawProto p)
Adds a RawProto to the collection.
void Insert(int nIdx, RawProto p)
Inserts a new RawProto into the collection at a given index.
int Count
Returns the number of items in the collection.
The RawProto class is used to parse and output Google prototxt file data.
TYPE
Defines the type of a RawProto node.
string Name
Returns the name of the node.
RawProtoCollection Children
Returns a collection of this nodes child nodes.
string Value
Get/set the value of the node.
RawProto FindChild(string strName)
Searches for a given 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.
int FindChildIndex(string strName)
Searches for the index to a given node's child.
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
bool RemoveChild(RawProto p)
Removes a given child from this node's children.
RawProtoCollection FindChildren(params string[] rgstrName)
Searches for all children with a given name in this node's children.
The SettingsCaffe defines the settings used by the MyCaffe CaffeControl.
SNAPSHOT_WEIGHT_UPDATE_METHOD SnapshotWeightUpdateMethod
Get/set the snapshot update method.
bool EnableRandomInputSelection
Get/set random image selection. When enabled, images are randomly selected from the entire set,...
SNAPSHOT_LOAD_METHOD SnapshotLoadMethod
Get/set the snapshot load method.
double DbAutoRefreshScheduledReplacementPercent
Get/set the automatic refresh scheduled update replacement percentage used on refresh (default = 0....
int DbLoadLimit
Get/set the image database load limit.
bool EnableLabelBoosting
DEPRECIATED: Get/set label boosting. When using Label boosting, images are selected from boosted labe...
double SuperBoostProbability
Get/set the superboost probability used when selecting boosted images.
DB_LOAD_METHOD DbLoadMethod
Get/set the image database loading method.
bool EnablePairInputSelection
Get/set pair image selection. When using pair selection, images are queried in pairs where the first ...
int DbAutoRefreshScheduledUpdateInMs
Get/set the automatic refresh scheduled udpate period (default = 10000, only applies when ImageDbLoad...
bool EnableLabelBalancing
Get/set label balancing. When enabled, first the label set is randomly selected and then the image is...
string Owner
Get/set the owner of the item.
int ID
Get/set the database ID of the item.
string Name
Get/set the name of the item.
The DatasetDescriptor class describes a dataset which contains both a training data source and testin...
GroupDescriptor DatasetGroup
Returns the dataset group.
SourceDescriptor TrainingSource
Get/set the training data source.
GroupDescriptor ModelGroup
Get/set the dataset model group.
string? TrainingSourceName
Returns the training source name, or null if not specifies.
SourceDescriptor TestingSource
Get/set the testing data source.
string? TestingSourceName
Returns the testing source name or null if not specified.
DatasetDescriptor(int nID, string strName, GroupDescriptor grpModel, GroupDescriptor grpDs, SourceDescriptor srcTrain, SourceDescriptor srcTest, string strCreatorName, string strDescription, string strOwner=null, GYM_TYPE gymType=GYM_TYPE.NONE)
The DatasetDescriptor constructor.
The GroupDescriptor class defines a group.
The ParameterDescriptorCollection class contains a list of ParameterDescriptor's.
void Add(ParameterDescriptor p)
Adds a ParameterDescriptor to the collection.
ParameterDescriptor Find(string strName)
Searches for a parameter by name in the collection.
The ParameterDescriptor class describes a parameter in the database.
string Value
Get/set the value of the item.
The ProjectDescriptor class contains all information describing a project, such as its: dataset,...
ValueDescriptorCollection AnalysisItems
Returns the collection of analysis ValueDescriptors of the Project.
string SolverDescription
Get/set the solver description script.
string SolverName
Get/set the solver name.
GroupDescriptor Group
Get/set the project group.
string ModelDescription
Get/set the model description script.
DatasetDescriptor DatasetTarget
Get/set the secondary 'target' dataset (if used).
string ModelName
Get/set the model name.
int TotalIterations
Get/set the total iterations.
virtual string GpuOverride
Get/set the GPU ID's to use as an override.
SettingsCaffe Settings
Get/set the settings of the Project.
bool Active
Returns whether or not the project is active.
ParameterDescriptorCollection Parameters
Returns the collection of parameters of the Project.
DatasetDescriptor Dataset
Get/set the dataset used.
The SourceDescriptor class contains all information describing a data source.
bool SaveImagesToFile
Gets whether or not the images are saved to the file system (true), or directly to the database (fals...
The StateDescriptor class contains the information related to the state of a project incuding the sol...
double Accuracy
Returns the accuracy observed while testing.
byte[] State
Get/set the state of a Solver in training.
bool? HasResults
Returns whether or not the state has results (e.g. it has been trained at least to some degree).
byte[] Weights
Get/set the weights of a trained Net.
double Error
Specifies the error observed whiel training.
int Iterations
Specifies the number of iterations run.
The ValueDescriptorCollection class contains a list of ValueDescriptor's.
The descriptors namespace contains all descriptor used to describe various items stored within the da...
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
DB_LOAD_METHOD
Defines how to laod the items into the in-memory database.
Phase
Defines the Phase under which to run a Net.
SNAPSHOT_LOAD_METHOD
Defines the snapshot load method.
SNAPSHOT_WEIGHT_UPDATE_METHOD
Defines the snapshot weight update method.
TRAINING_CATEGORY
Defines the category of training.
Stage
Specifies the stage underwhich to run a custom trainer.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...