MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
Layer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6using System.Drawing;
7using System.Diagnostics;
8using MyCaffe.basecode;
9using MyCaffe.db.image;
10using MyCaffe.common;
11using MyCaffe.param;
12using System.IO;
13using System.Reflection;
14
18namespace MyCaffe.layers
19{
30 public abstract class Layer<T> : IDisposable
31 {
39 protected CudaDnn<T> m_cuda;
43 protected Log m_log;
51 protected Phase m_phase;
63 protected DictionaryMap<bool> m_rgbParamPropagateDown;
68 protected DictionaryMap<double> m_rgLoss;
72 protected T m_tOne;
76 protected T m_tZero;
80 protected bool m_bEnablePassthrough = false;
84 protected bool m_bUseHalfSize = false;
88 protected bool m_bConvertTopOnFwd = false;
92 protected bool m_bConvertTopOnBwd = true;
96 protected bool m_bConvertBottom = true;
100 protected bool m_bReshapeOnForwardNeeded = true;
104 protected bool m_bNetReshapeRequest = false;
109
110 private List<List<int>> m_rgrgLastBottomShape = new List<List<int>>();
111 private List<List<int>> m_rgrgLastTopShape = new List<List<int>>();
112
113 private double m_dfForwardTiming = 0;
114 private double m_dfForwardAverageTiming = 0;
115 private double m_dfBackwardTiming = 0;
116 private double m_dfBackwardAverageTiming = 0;
117 private double m_dfAverageInterval = 20.0;
118 private Stopwatch m_swTiming = new Stopwatch();
119 private WorkspaceArgs m_argsWs = new WorkspaceArgs(0, 0);
120
124 public event EventHandler<WorkspaceArgs> OnGetWorkspace;
128 public event EventHandler<WorkspaceArgs> OnSetWorkspace;
132 public event EventHandler<GetIterationArgs> OnGetIteration;
140 public event EventHandler<GetWorkBlobArgs<T>> OnDebug;
141
142 enum CONVERT_TYPE
143 {
144 BASE_TO_HALF,
145 HALF_TO_BASE
146 }
147
158 public Layer(CudaDnn<T> cuda, Log log, LayerParameter p)
159 {
160 m_cuda = cuda;
161 m_log = log;
162 m_param = p.Clone(true);
163 m_phase = p.phase;
164 m_rgbParamPropagateDown = new DictionaryMap<bool>(false);
165 m_rgLoss = new DictionaryMap<double>(0.0);
167
168 for (int i = 0; i < p.blobs.Count; i++)
169 {
170 m_colBlobs.Add(new Blob<T>(cuda, log, p.blobs[i]));
171 }
172
173 m_tOne = (T)Convert.ChangeType(1, typeof(T));
174 m_tZero = (T)Convert.ChangeType(0, typeof(T));
175 }
176
180 public void Dispose()
181 {
182 dispose();
183 }
184
188 protected virtual void dispose()
189 {
190 }
191
196 protected void dispose(ref Layer<T> l)
197 {
198 if (l != null)
199 {
200 l.Dispose();
201 l = null;
202 }
203 }
204
209 protected void dispose(ref Blob<T> b)
210 {
211 if (b != null)
212 {
213 b.Dispose();
214 b = null;
215 }
216 }
217
223 protected void dispose(ref BlobCollection<T> rg, bool bSetToNull = true)
224 {
225 if (rg != null)
226 {
227 rg.Dispose();
228 if (bSetToNull)
229 rg = null;
230 }
231 }
232
240 public virtual void ConnectLoss(LossLayer<T> layer)
241 {
242 }
243
248 {
249 get { return m_parentLayerType; }
250 }
251
255 public virtual bool SupportsPreProcessing
256 {
257 get { return false; }
258 }
259
263 public virtual bool SupportsPostProcessing
264 {
265 get { return false; }
266 }
267
272 {
273 get { return false; }
274 }
275
280 {
281 get { return false; }
282 }
283
294 public virtual BlobCollection<T> PreProcessInput(PropertySet customInput, out int nSeqLen, BlobCollection<T> colBottom = null)
295 {
296 nSeqLen = 0;
297 return colBottom;
298 }
299
316 public virtual bool PreProcessInput(string strEncInput, int? nDecInput, BlobCollection<T> colBottom)
317 {
318 return false;
319 }
320
328 public virtual List<Tuple<string, int, double>> PostProcessOutput(Blob<T> blobSofmtax, int nK = 1)
329 {
330 return null;
331 }
342 public virtual List<Tuple<string, int, double>> PostProcessLogitsOutput(int nCurIdx, Blob<T> blobLogits, Layer<T> softmax, int nAxis, int nK = 1)
343 {
344 return null;
345 }
351 public virtual string PostProcessFullOutput(Blob<T> blobSoftmax)
352 {
353 return null;
354 }
355
361 public virtual string PostProcessOutput(int nIdx)
362 {
363 return null;
364 }
365
370 public virtual void SetOnDebug(EventHandler<GetWorkBlobArgs<T>> fn)
371 {
372 OnDebug += fn;
373 }
374
379 public virtual void ResetOnDebug(EventHandler<GetWorkBlobArgs<T>> fn)
380 {
381 OnDebug -= fn;
382 }
383
389 public virtual bool ReInitializeParameters(WEIGHT_TARGET target)
390 {
391 return true;
392 }
393
399 {
400 GetIterationArgs args = null;
401
402 if (OnGetIteration != null)
403 {
404 args = new GetIterationArgs();
405 OnGetIteration(this, args);
406 }
407
408 return args;
409 }
410
415 {
417 }
418
423 public void SetPhase(Phase phase)
424 {
425 m_phase = phase;
426 m_param.phase = phase;
427 }
428
439 public void Setup(BlobCollection<T> colBottom, BlobCollection<T> colTop)
440 {
441 try
442 {
443 m_rgrgLastBottomShape = new List<List<int>>();
444 m_rgrgLastTopShape = new List<List<int>>();
445
446 CheckBlobCounts(colBottom, colTop);
448 LayerSetUp(colBottom, colTop);
449 Reshape(colBottom, colTop);
450 setShapes(colBottom, colTop);
451 SetLossWeights(colTop);
452
453 if (colBottom != colTop)
454 {
455 for (int i = 0; i < colBottom.Count; i++)
456 {
457 if (i < colTop.Count && colBottom[i] != colTop[i])
458 {
459 colTop[i].CopyParameters(colBottom[i]);
460 }
461 }
462 }
463 }
464 catch (Exception excpt)
465 {
466 if (m_param != null)
467 throw new Exception("Layer: '" + m_param.name + "' (" + m_param.type.ToString() + ") Error: " + excpt.Message, excpt);
468 else
469 throw excpt;
470 }
471 }
472
486 public abstract void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop);
487
492 public virtual void SetNetParameterUsed(NetParameter np)
493 {
494 }
495
506 public abstract void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop);
507
514 protected long convert_to_full(int nCount, long hMem)
515 {
516 if (OnGetWorkspace == null || OnSetWorkspace == null)
517 throw new Exception("The OnGetWorkSpace and OnSetWorkspace events must be connected!");
518
519 ulong lSize = (ulong)nCount * CudaDnn<T>.basetype_size(false);
521 if (args.WorkspaceSizeInBytes < lSize)
522 {
523 setWorkspace(lSize);
524 args = getWorkspace();
525 }
526
527 m_cuda.copy(nCount, hMem, args.WorkspaceData, 0, 0, -1, null, false);
528 return args.WorkspaceData;
529 }
530
535 protected void convert(BlobCollection<T> col)
536 {
537 ulong lMaxSize = 0;
538 bool bConversionNeeded = false;
539
540 foreach (Blob<T> b in col)
541 {
542 if ((m_bUseHalfSize && !b.HalfSize) ||
543 (!m_bUseHalfSize && b.HalfSize))
544 {
545 ulong lSize = b.GetConversionWorkSize(m_bUseHalfSize);
546 if (lMaxSize < lSize)
547 lMaxSize = lSize;
548
549 bConversionNeeded = true;
550 }
551 }
552
553 if (!bConversionNeeded)
554 return;
555
556 if (OnGetWorkspace == null || OnSetWorkspace == null)
557 throw new Exception("The OnGetWorkSpace and OnSetWorkspace events must be connected!");
558
560 if (args.WorkspaceSizeInBytes < lMaxSize)
561 {
562 setWorkspace(lMaxSize);
563 args = getWorkspace();
564 }
565
566 foreach (Blob<T> b in col)
567 {
568 if (m_bUseHalfSize && !b.HalfSize)
569 b.ConvertToHalf(args.WorkspaceData, args.WorkspaceSizeInBytes, true, true);
570 else if (!m_bUseHalfSize && b.HalfSize)
571 b.ConvertToBase(args.WorkspaceData, args.WorkspaceSizeInBytes, true, true);
572 }
573 }
574
580 {
581 if (OnGetWorkspace == null || OnSetWorkspace == null)
582 return;
583
584 ulong lMaxSize = 0;
585 bool bConversionNeeded = false;
586
587 foreach (Blob<T> b in col)
588 {
589 if (b.HalfSize)
590 {
591 ulong lSize = b.GetConversionWorkSize(false);
592 if (lMaxSize < lSize)
593 lMaxSize = lSize;
594
595 bConversionNeeded = true;
596 }
597 }
598
599 if (!bConversionNeeded)
600 return;
601
603 if (args.WorkspaceSizeInBytes < lMaxSize)
604 {
605 setWorkspace(lMaxSize);
606 args = getWorkspace();
607 }
608
609 foreach (Blob<T> b in col)
610 {
611 b.ConvertToBase(args.WorkspaceData, args.WorkspaceSizeInBytes, true, true);
612 }
613 }
614
622 protected virtual bool reshapeNeeded(BlobCollection<T> colBottom, BlobCollection<T> colTop, bool bReset = true)
623 {
624 return true;
625
626 // Memory optimizations require reshaping now on each pass.
627 //if (!bReset)
628 // return m_bReshapeOnForwardNeeded;
629
630 //if (!compareShapes(colBottom, colTop))
631 //{
632 // m_bReshapeOnForwardNeeded = true;
633 // return true;
634 //}
635 //else
636 //{
637 // m_bReshapeOnForwardNeeded = false;
638 // return false;
639 //}
640 }
641
648 protected bool compareShapes(BlobCollection<T> colBottom, BlobCollection<T> colTop)
649 {
650 if (!compareShapes(colBottom, m_rgrgLastBottomShape))
651 return false;
652
653 if (!compareShapes(colTop, m_rgrgLastTopShape))
654 return false;
655
656 return true;
657 }
658
659 private bool compareShapes(BlobCollection<T> col, List<List<int>> rgrg)
660 {
661 if (rgrg.Count != col.Count)
662 return false;
663
664 for (int i = 0; i < col.Count; i++)
665 {
666 int nCount = col[i].shape().Count;
667 if (rgrg[i].Count != nCount)
668 return false;
669
670 for (int j = 0; j < nCount; j++)
671 {
672 if (col[i].shape()[j] != rgrg[i][j])
673 return false;
674 }
675 }
676
677 return true;
678 }
679
685 protected void setShapes(BlobCollection<T> colBottom, BlobCollection<T> colTop)
686 {
687 setShapes(colBottom, ref m_rgrgLastBottomShape);
688 setShapes(colTop, ref m_rgrgLastTopShape);
689 }
690
691 private void setShapes(BlobCollection<T> col, ref List<List<int>> rgrg)
692 {
693 if (rgrg.Count != col.Count)
694 rgrg = new List<List<int>>(col.Count);
695
696 for (int i = 0; i < col.Count; i++)
697 {
698 int nCount = col[i].shape().Count;
699 if (rgrg.Count < col.Count)
700 rgrg.Add(new List<int>());
701 else if (rgrg[i].Count != nCount)
702 rgrg[i] = new List<int>(nCount);
703
704 for (int j = 0; j < nCount; j++)
705 {
706 nCount = col[i].shape().Count;
707 if (rgrg[i].Count < nCount)
708 rgrg[i].Add(col[i].shape()[j]);
709 else
710 rgrg[i][j] = col[i].shape()[j];
711 }
712 }
713 }
714
728 public double Forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
729 {
730 try
731 {
732 m_swTiming.Restart();
733 double dfLoss = 0;
734
736 convert(colBottom);
737
738 Reshape(colBottom, colTop);
739
741 convert(colTop);
742
743 forward(colBottom, colTop);
744
745 for (int i = 0; i < colTop.Count; i++)
746 {
747 if (loss(i) == 0)
748 continue;
749
750 int nCount = colTop[i].count();
751 long hData = colTop[i].gpu_data;
752 long hDiff = colTop[i].gpu_diff;
753 double dfBlobLoss = m_cuda.dot_double(nCount, hData, hDiff);
754
755 dfLoss += dfBlobLoss;
756 }
757
758 m_swTiming.Stop();
759 m_dfForwardTiming = m_swTiming.Elapsed.TotalMilliseconds;
760 m_dfForwardAverageTiming = getAveTiming(m_dfAverageInterval, m_dfForwardTiming, m_dfForwardAverageTiming);
761
762 if (OnDebug != null)
763 {
765 OnDebug(this, args);
766
767 foreach (Blob<T> b in colTop)
768 {
769 Tuple<double, double, double, double> mm_data = b.minmax_data(args.Blob, true);
770 Tuple<double, double, double, double> mm_diff = b.minmax_diff(args.Blob, true);
771
772 if (mm_data.Item3 > 0 || mm_data.Item4 > 0)
773 throw new Exception("NAN or INF detected in the TOP '" + b.Name + "' Data for layer '" + m_param.name + "' on the forward pass.");
774
775 if (mm_diff.Item3 > 0 || mm_diff.Item4 > 0)
776 throw new Exception("NAN or INF detected in TOP '" + b.Name + "' Diff for layer '" + m_param.name + "' on the forward pass.");
777 }
778 }
779
780 return dfLoss;
781 }
782 catch (Exception excpt)
783 {
784 if (m_param != null)
785 throw new Exception("Layer: '" + m_param.name + "' (" + m_param.type.ToString() + ") Error: " + excpt.Message, excpt);
786 else
787 throw excpt;
788 }
789 }
790
799 protected abstract void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop);
800
815 public void Backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
816 {
817 try
818 {
819 m_swTiming.Restart();
820
822 convert(colTop);
823
824 convert(colBottom);
825
826 backward(colTop, rgbPropagateDown, colBottom);
827 m_swTiming.Stop();
828 m_dfBackwardTiming = m_swTiming.Elapsed.TotalMilliseconds;
829 m_dfBackwardAverageTiming = getAveTiming(m_dfAverageInterval, m_dfBackwardTiming, m_dfBackwardAverageTiming);
830
831 if (OnDebug != null)
832 {
834 OnDebug(this, args);
835
836 foreach (Blob<T> b in colBottom)
837 {
838 Tuple<double, double, double, double> mm_data = b.minmax_data(args.Blob, true);
839 Tuple<double, double, double, double> mm_diff = b.minmax_diff(args.Blob, true);
840
841 if (mm_data.Item3 > 0 || mm_data.Item4 > 0)
842 throw new Exception("NAN or INF detected in the BOTTOM '" + b.Name + "' Data for layer '" + m_param.name + "' on the backward pass.");
843
844 if (mm_diff.Item3 > 0 || mm_diff.Item4 > 0)
845 throw new Exception("NAN or INF detected in the BOTTOM '" + b.Name + "' Diff for layer '" + m_param.name + "' on the backward pass.");
846 }
847 }
848 }
849 catch (Exception excpt)
850 {
851 if (m_param != null)
852 throw new Exception("Layer: '" + m_param.name + "' (" + m_param.type.ToString() + ") Error: " + excpt.Message, excpt);
853 else
854 throw excpt;
855 }
856 }
857
869 protected abstract void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom);
870
875 {
876 get { return m_colBlobs; }
877 }
878
883 {
884 get { return m_colInternalBlobs; }
885 }
886
891 protected virtual void setup_internal_blobs(BlobCollection<T> col)
892 {
893 }
894
899 {
900 get { return m_param; }
901 }
902
908 public double loss(int nTopIdx)
909 {
910 return m_rgLoss[nTopIdx];
911 }
912
918 public void set_loss(int nTopIdx, double dfLoss)
919 {
920 m_rgLoss[nTopIdx] = dfLoss;
921 }
922
927 {
928 get { return m_type; }
929 }
930
939 public virtual int ExactNumBottomBlobs
940 {
941 get { return -1; }
942 }
943
952 public virtual int MinBottomBlobs
953 {
954 get { return -1; }
955 }
956
965 public virtual int MaxBottomBlobs
966 {
967 get { return -1; }
968 }
969
978 public virtual int ExactNumTopBlobs
979 {
980 get { return -1; }
981 }
982
991 public virtual int MinTopBlobs
992 {
993 get { return -1; }
994 }
995
1004 public virtual int MaxTopBlobs
1005 {
1006 get { return -1; }
1007 }
1008
1017 public virtual bool EqualNumBottomTopBlobs
1018 {
1019 get { return false; }
1020 }
1021
1030 public virtual bool AutoTopBlobs
1031 {
1032 get { return false; }
1033 }
1034
1046 public virtual bool AllowForceBackward(int nBottomIdx)
1047 {
1048 return true;
1049 }
1050
1057 public bool param_propagate_down(int nParamIdx)
1058 {
1059 return m_rgbParamPropagateDown[nParamIdx];
1060 }
1061
1068 public void set_param_propagate_down(int nParamIdx, bool bPropagate)
1069 {
1070 m_rgbParamPropagateDown[nParamIdx] = bPropagate;
1071 }
1072
1080 protected void CheckBlobCounts(BlobCollection<T> colBottom, BlobCollection<T> colTop)
1081 {
1082 if (ExactNumBottomBlobs >= 0)
1083 m_log.CHECK_EQ(ExactNumBottomBlobs, colBottom.Count, type.ToString() + " Layer takes " + ExactNumBottomBlobs.ToString() + " bottom blob(s) as input.");
1084
1085 if (MinBottomBlobs >= 0)
1086 m_log.CHECK_LE(MinBottomBlobs, colBottom.Count, type.ToString() + " Layer takes at least " + MinBottomBlobs.ToString() + " bottom blob(s) as input.");
1087
1088 if (MaxBottomBlobs >= 0)
1089 m_log.CHECK_GE(MaxBottomBlobs, colBottom.Count, type.ToString() + " Layer takes at most " + MaxBottomBlobs.ToString() + " bottom blob(s) as input.");
1090
1091 if (ExactNumTopBlobs >= 0)
1092 m_log.CHECK_EQ(ExactNumTopBlobs, colTop.Count, type.ToString() + " Layer takes " + ExactNumTopBlobs.ToString() + " top blob(s) as output.");
1093
1094 if (MinTopBlobs >= 0)
1095 m_log.CHECK_LE(MinTopBlobs, colTop.Count, type.ToString() + " Layer takes at least " + MinTopBlobs.ToString() + " top blob(s) as output.");
1096
1097 if (MaxTopBlobs >= 0)
1098 m_log.CHECK_GE(MaxTopBlobs, colTop.Count, type.ToString() + " Layer takes at most " + MaxTopBlobs.ToString() + " top blob(s) as output.");
1099
1101 m_log.CHECK_EQ(colBottom.Count, colTop.Count, type.ToString() + " Layer produces one top blob as output for each bottom blob input.");
1102 }
1103
1109 protected void SetLossWeights(BlobCollection<T> colTop)
1110 {
1111 if (m_param.loss_weight.Count > 0)
1112 {
1113 m_log.CHECK_EQ(colTop.Count, m_param.loss_weight.Count, "loss_weight must be unspecified or specified once per top blob.");
1114
1115 for (int i = 0; i < colTop.Count; i++)
1116 {
1117 double dfLossWeight = m_param.loss_weight[i];
1118
1119 if (dfLossWeight == 0)
1120 continue;
1121
1122 set_loss(i, dfLossWeight);
1123 colTop[i].SetDiff(dfLossWeight);
1124 }
1125 }
1126 }
1127
1135 {
1136 if (pParent is LayerParameterEx<T>)
1137 {
1139 return new LayerParameterEx<T>(pChild, pEx.SharedBlobs, pEx.SharedLayerBlobs, null);
1140 }
1141
1142 return pChild;
1143 }
1144
1152 protected bool shareParameter(Blob<T> b, List<int> rgMinShape, bool bAllowEndsWithComparison = false)
1153 {
1155 if (paramEx == null)
1156 return false;
1157
1158 if (paramEx.SharedBlobs == null)
1159 return false;
1160
1161 return paramEx.SharedBlobs.Share(b, rgMinShape, false, bAllowEndsWithComparison);
1162 }
1163
1170 protected bool shareLayerBlob(Blob<T> b, List<int> rgMinShape)
1171 {
1173 if (paramEx == null)
1174 return false;
1175
1176 if (paramEx.SharedLayerBlobs == null)
1177 return false;
1178
1179 return paramEx.SharedLayerBlobs.Share(b, rgMinShape, false);
1180 }
1181
1187 protected bool shareLayerBlobs(Layer<T> layer)
1188 {
1190 if (paramEx == null)
1191 return false;
1192
1193 if (paramEx.SharedLayer == null)
1194 return false;
1195
1196 if (paramEx.SharedLayer.blobs.Count != layer.blobs.Count)
1197 return false;
1198
1199 for (int i = 0; i < paramEx.SharedLayer.blobs.Count; i++)
1200 {
1201 Blob<T> bSrc = paramEx.SharedLayer.blobs[i];
1202 Blob<T> bDst = layer.blobs[i];
1203
1204 string strSrc = bSrc.shape_string;
1205 string strDst = bDst.shape_string;
1206 if (strSrc != strDst)
1207 {
1208 m_log.WriteLine("WARNING: Cannot share blob '" + bSrc.Name + "'(" + strSrc + ") with blob '" + bDst.Name + "'(" + strDst + ") because the sizes differ!");
1209 return false;
1210 }
1211
1212 bSrc.Share(bDst);
1213 }
1214
1215 if (paramEx.SharedLayer.internal_blobs.Count != layer.internal_blobs.Count)
1216 return false;
1217
1218 for (int i = 0; i < paramEx.SharedLayer.internal_blobs.Count; i++)
1219 {
1220 Blob<T> bSrc = paramEx.SharedLayer.internal_blobs[i];
1221 Blob<T> bDst = layer.internal_blobs[i];
1222
1223 string strSrc = bSrc.shape_string;
1224 string strDst = bDst.shape_string;
1225 if (strSrc != strDst)
1226 {
1227 m_log.WriteLine("WARNING: Cannot share internal blob '" + bSrc.Name + "'(" + strSrc + ") with internal blob '" + bDst.Name + "'(" + strDst + ") because the sizes differ!");
1228 return false;
1229 }
1230
1231 bSrc.Share(bDst);
1232 }
1233
1234 return true;
1235 }
1236
1240 public double forward_timing
1241 {
1242 get { return m_dfForwardTiming; }
1243 }
1244
1249 {
1250 get { return m_dfForwardAverageTiming; }
1251 }
1252
1256 public double backward_timing
1257 {
1258 get { return m_dfBackwardTiming; }
1259 }
1260
1265 {
1266 get { return m_dfBackwardAverageTiming; }
1267 }
1268
1276 public void SetEnablePassthrough(bool bEnable)
1277 {
1278 m_bEnablePassthrough = bEnable;
1279 }
1280
1285 protected virtual WorkspaceArgs getWorkspace()
1286 {
1287 if (OnGetWorkspace == null)
1288 return null;
1289
1290 OnGetWorkspace(this, m_argsWs);
1291
1292 return m_argsWs;
1293 }
1294
1300 protected virtual bool setWorkspace(ulong lSizeInBytes)
1301 {
1302 if (OnSetWorkspace == null)
1303 return false;
1304
1305 OnSetWorkspace(this, new WorkspaceArgs(0, lSizeInBytes));
1306 return true;
1307 }
1308
1313 protected void check_nan(Blob<T> b)
1314 {
1315 double[] rg = convertD(b.update_cpu_data());
1316
1317 for (int i = 0; i < rg.Length; i++)
1318 {
1319 if (double.IsNaN(rg[i]))
1320 throw new Exception("NAN FOUND!");
1321 }
1322 }
1323
1329 protected T convert(double df)
1330 {
1331 return (T)Convert.ChangeType(df, typeof(T));
1332 }
1333
1339 protected T convert(float f)
1340 {
1341 return (T)Convert.ChangeType(f, typeof(T));
1342 }
1343
1349 protected double convertD(T df)
1350 {
1351 return (double)Convert.ChangeType(df, typeof(double));
1352 }
1353
1359 protected float convertF(T df)
1360 {
1361 return (float)Convert.ChangeType(df, typeof(float));
1362 }
1363
1369 protected double[] convertD(T[] rg)
1370 {
1371 if (typeof(T) == typeof(double))
1372 return (double[])Convert.ChangeType(rg, typeof(double[]));
1373
1374 double[] rgdf = new double[rg.Length];
1375 Array.Copy(rg, rgdf, rg.Length);
1376
1377 return rgdf;
1378 }
1379
1385 protected T[] convert(double[] rg)
1386 {
1387 if (typeof(T) == typeof(double))
1388 return (T[])Convert.ChangeType(rg, typeof(T[]));
1389
1390 float[] rgf = new float[rg.Length];
1391 Array.Copy(Array.ConvertAll(rg, p => Convert.ToSingle(p)), rgf, rg.Length);
1392
1393 return (T[])Convert.ChangeType(rgf, typeof(T[]));
1394 }
1395
1401 protected float[] convertF(T[] rg)
1402 {
1403 if (typeof(T) == typeof(float))
1404 return (float[])Convert.ChangeType(rg, typeof(float[]));
1405
1406 float[] rgf = new float[rg.Length];
1407 Array.Copy(Array.ConvertAll(rg, p => Convert.ToSingle(p)), rgf, rg.Length);
1408
1409 return rgf;
1410 }
1411
1417 protected T[] convert(float[] rg)
1418 {
1419 if (typeof(T) == typeof(float))
1420 return (T[])Convert.ChangeType(rg, typeof(T[]));
1421
1422 T[] rgt = new T[rg.Length];
1423 Array.Copy(rg, rgt, rg.Length);
1424
1425 return rgt;
1426 }
1427
1434 protected int val_at(T[] rg, int nIdx)
1435 {
1436 return (int)Convert.ChangeType(rg[nIdx], typeof(int));
1437 }
1438
1444 protected Size size_at(Blob<T> b)
1445 {
1446 T[] rg = b.update_cpu_data();
1447 int nHeight = val_at(rg, 0);
1448 int nWidth = (rg.Length > 1) ? val_at(rg, 1) : nHeight;
1449 return new Size(nWidth, nHeight);
1450 }
1451
1452 private double getAveTiming(double dfInterval, double dfTiming, double dfAveTiming)
1453 {
1454 double dfRatio = 1.0 / m_dfAverageInterval;
1455 return (dfAveTiming * (1.0 - dfRatio)) + (dfTiming * dfRatio);
1456 }
1457
1468 public static Layer<T> Create(CudaDnn<T> cuda, Log log, LayerParameter p, CancelEvent evtCancel, IXDatabaseBase db = null, TransferInput trxinput = null)
1469 {
1470 switch (p.type)
1471 {
1472 case LayerParameter.LayerType.ABSVAL:
1473 return new AbsValLayer<T>(cuda, log, p);
1474
1475 case LayerParameter.LayerType.ACCURACY:
1476 return new AccuracyLayer<T>(cuda, log, p);
1477
1478 case LayerParameter.LayerType.ARGMAX:
1479 return new ArgMaxLayer<T>(cuda, log, p);
1480
1481 case LayerParameter.LayerType.BATCHNORM:
1482 return new BatchNormLayer<T>(cuda, log, p);
1483
1484 case LayerParameter.LayerType.BATCHREINDEX:
1485 return new BatchReindexLayer<T>(cuda, log, p);
1486
1487 case LayerParameter.LayerType.BNLL:
1488 return new BNLLLayer<T>(cuda, log, p);
1489
1490 case LayerParameter.LayerType.BIAS:
1491 return new BiasLayer<T>(cuda, log, p);
1492
1493 case LayerParameter.LayerType.CLIP:
1494 return new ClipLayer<T>(cuda, log, p);
1495
1496 case LayerParameter.LayerType.CONCAT:
1497 return new ConcatLayer<T>(cuda, log, p);
1498
1499 case LayerParameter.LayerType.CONSTANT:
1500 return new ConstantLayer<T>(cuda, log, p);
1501
1502 case LayerParameter.LayerType.CONTRASTIVE_LOSS:
1503 return new ContrastiveLossLayer<T>(cuda, log, p);
1504
1505 case LayerParameter.LayerType.CONVOLUTION:
1506 return new ConvolutionLayer<T>(cuda, log, p);
1507
1508 case LayerParameter.LayerType.CROP:
1509 return new CropLayer<T>(cuda, log, p);
1510
1511 case LayerParameter.LayerType.DECONVOLUTION:
1512 return new DeconvolutionLayer<T>(cuda, log, p);
1513
1514 case LayerParameter.LayerType.IM2COL:
1515 return new Im2colLayer<T>(cuda, log, p);
1516
1517 case LayerParameter.LayerType.DATA:
1518 return new DataLayer<T>(cuda, log, p, db, evtCancel);
1519
1520 case LayerParameter.LayerType.DATA_NORMALIZER:
1521 return new DataNormalizerLayer<T>(cuda, log, p);
1522
1523 case LayerParameter.LayerType.DEBUG:
1524 return new DebugLayer<T>(cuda, log, p);
1525
1526 case LayerParameter.LayerType.DROPOUT:
1527 return new DropoutLayer<T>(cuda, log, p);
1528
1529 case LayerParameter.LayerType.DUMMYDATA:
1530 return new DummyDataLayer<T>(cuda, log, p);
1531
1532 case LayerParameter.LayerType.ELTWISE:
1533 return new EltwiseLayer<T>(cuda, log, p);
1534
1535 case LayerParameter.LayerType.ELU:
1536 return new ELULayer<T>(cuda, log, p);
1537
1538 case LayerParameter.LayerType.EMBED:
1539 return new EmbedLayer<T>(cuda, log, p);
1540
1541 case LayerParameter.LayerType.EUCLIDEAN_LOSS:
1542 return new EuclideanLossLayer<T>(cuda, log, p);
1543
1544 case LayerParameter.LayerType.EXP:
1545 return new ExpLayer<T>(cuda, log, p);
1546
1547 case LayerParameter.LayerType.FILTER:
1548 return new FilterLayer<T>(cuda, log, p);
1549
1550 case LayerParameter.LayerType.FLATTEN:
1551 return new FlattenLayer<T>(cuda, log, p);
1552
1553 case LayerParameter.LayerType.GRADIENTSCALER:
1554 return new GradientScaleLayer<T>(cuda, log, p);
1555
1556 case LayerParameter.LayerType.HINGE_LOSS:
1557 return new HingeLossLayer<T>(cuda, log, p);
1558
1559 case LayerParameter.LayerType.IMAGE_DATA:
1560 return new ImageDataLayer<T>(cuda, log, p, evtCancel);
1561
1562 case LayerParameter.LayerType.INFOGAIN_LOSS:
1563 return new InfogainLossLayer<T>(cuda, log, p);
1564
1565 case LayerParameter.LayerType.INNERPRODUCT:
1566 return new InnerProductLayer<T>(cuda, log, p);
1567
1568 case LayerParameter.LayerType.INPUT:
1569 return new InputLayer<T>(cuda, log, p);
1570
1571 case LayerParameter.LayerType.LOG:
1572 return new LogLayer<T>(cuda, log, p);
1573
1574 case LayerParameter.LayerType.LABELMAPPING:
1575 return new LabelMappingLayer<T>(cuda, log, p, db);
1576
1577 case LayerParameter.LayerType.LRN:
1578 return new LRNLayer<T>(cuda, log, p);
1579
1580 case LayerParameter.LayerType.MATH:
1581 return new MathLayer<T>(cuda, log, p);
1582
1583 case LayerParameter.LayerType.MEMORYDATA:
1584 return new MemoryDataLayer<T>(cuda, log, p);
1585
1586 case LayerParameter.LayerType.MEMORY_LOSS:
1587 return new MemoryLossLayer<T>(cuda, log, p);
1588
1589 case LayerParameter.LayerType.MULTINOMIALLOGISTIC_LOSS:
1590 return new MultinomialLogisticLossLayer<T>(cuda, log, p);
1591
1592 case LayerParameter.LayerType.MVN:
1593 return new MVNLayer<T>(cuda, log, p);
1594
1595 case LayerParameter.LayerType.PARAMETER:
1596 return new ParameterLayer<T>(cuda, log, p);
1597
1598 case LayerParameter.LayerType.POOLING:
1599 return new PoolingLayer<T>(cuda, log, p);
1600
1601 case LayerParameter.LayerType.POWER:
1602 return new PowerLayer<T>(cuda, log, p);
1603
1604 case LayerParameter.LayerType.PRELU:
1605 return new PReLULayer<T>(cuda, log, p);
1606
1607 case LayerParameter.LayerType.REDUCTION:
1608 return new ReductionLayer<T>(cuda, log, p);
1609
1610 case LayerParameter.LayerType.RELU:
1611 return new ReLULayer<T>(cuda, log, p);
1612
1613 case LayerParameter.LayerType.RESHAPE:
1614 return new ReshapeLayer<T>(cuda, log, p);
1615
1616 case LayerParameter.LayerType.SCALE:
1617 return new ScaleLayer<T>(cuda, log, p);
1618
1619 case LayerParameter.LayerType.SIGMOID:
1620 return new SigmoidLayer<T>(cuda, log, p);
1621
1622 case LayerParameter.LayerType.SIGMOIDCROSSENTROPY_LOSS:
1623 return new SigmoidCrossEntropyLossLayer<T>(cuda, log, p);
1624
1626 case LayerParameter.LayerType.SOFTMAXCROSSENTROPY_LOSS:
1627 return new SoftmaxCrossEntropyLossLayer<T>(cuda, log, p);
1628
1629 case LayerParameter.LayerType.SOFTMAXCROSSENTROPY2_LOSS:
1630 return new SoftmaxCrossEntropy2LossLayer<T>(cuda, log, p);
1631
1632 case LayerParameter.LayerType.SILENCE:
1633 return new SilenceLayer<T>(cuda, log, p);
1634
1635 case LayerParameter.LayerType.SLICE:
1636 return new SliceLayer<T>(cuda, log, p);
1637
1638 case LayerParameter.LayerType.SOFTMAX:
1639 return new SoftmaxLayer<T>(cuda, log, p);
1640
1641 case LayerParameter.LayerType.SOFTMAXWITH_LOSS:
1642 return new SoftmaxLossLayer<T>(cuda, log, p);
1643
1644 case LayerParameter.LayerType.SPLIT:
1645 return new SplitLayer<T>(cuda, log, p);
1646
1647 case LayerParameter.LayerType.SPP:
1648 return new SPPLayer<T>(cuda, log, p);
1649
1650 case LayerParameter.LayerType.SWISH:
1651 return new SwishLayer<T>(cuda, log, p);
1652
1653 case LayerParameter.LayerType.TANH:
1654 return new TanhLayer<T>(cuda, log, p);
1655
1656 case LayerParameter.LayerType.THRESHOLD:
1657 return new ThresholdLayer<T>(cuda, log, p);
1658
1659 case LayerParameter.LayerType.TILE:
1660 return new TileLayer<T>(cuda, log, p);
1661
1662 // DEPRECIATED
1663 case LayerParameter.LayerType.LSTM_SIMPLE:
1664 return new LSTMSimpleLayer<T>(cuda, log, p);
1665
1666 case LayerParameter.LayerType.RNN:
1667 return new RNNLayer<T>(cuda, log, p, evtCancel);
1668
1669 case LayerParameter.LayerType.LSTM:
1670 return new LSTMLayer<T>(cuda, log, p, evtCancel);
1671
1672 case LayerParameter.LayerType.LSTM_UNIT:
1673 return new LSTMUnitLayer<T>(cuda, log, p);
1674
1675 default:
1676 Layer<T> layer = createDynamicLayer(cuda, log, p, db, evtCancel);
1677 if (layer != null)
1678 return layer;
1679
1680 log.FAIL("Unknown layer type: " + p.type.ToString());
1681 break;
1682 }
1683
1684 throw new NotImplementedException("The layer type: " + p.type.ToString() + " is not implemented yet.");
1685 }
1686
1687 private static Layer<T> createDynamicLayer(CudaDnn<T> cuda, Log log, LayerParameter p, IXDatabaseBase db, CancelEvent evtCancel)
1688 {
1689 string strDir = System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
1690 string[] rgstrFiles = Directory.GetFiles(strDir, "mycaffe.layers.*.dll");
1691
1692 foreach (string strFile in rgstrFiles)
1693 {
1694 FileInfo fi = new FileInfo(strFile);
1695 if (fi.Name.ToLower().IndexOf("mycaffe.layers.") == 0 && fi.Extension.ToLower() == ".dll")
1696 {
1697 ILayerCreator icreator = loadCreator(strFile);
1698 if (icreator != null)
1699 {
1700 Layer<T> layer;
1701
1702 if (typeof(T) == typeof(double))
1703 layer = icreator.CreateDouble(cuda as CudaDnn<double>, log, p, evtCancel, db) as Layer<T>;
1704 else
1705 layer = icreator.CreateSingle(cuda as CudaDnn<float>, log, p, evtCancel, db) as Layer<T>;
1706
1707 if (layer != null)
1708 return layer;
1709 }
1710 }
1711 }
1712
1713 return null;
1714 }
1715
1716 private static ILayerCreator loadCreator(string strPath)
1717 {
1718 try
1719 {
1720 Assembly a = Assembly.LoadFile(strPath);
1721
1722 foreach (Type t in a.GetTypes())
1723 {
1724 if (t.IsPublic)
1725 {
1726 Type iface = t.GetInterface("ILayerCreator");
1727 if (iface != null)
1728 {
1729 object obj = Activator.CreateInstance(t);
1730 return (ILayerCreator)obj;
1731 }
1732 }
1733 }
1734
1735 return null;
1736 }
1737 catch (Exception)
1738 {
1739 return null;
1740 }
1741 }
1742 }
1743
1750 {
1751 BlobCollection<T> m_colSharedBlobs = null;
1752 BlobCollection<T> m_colLayerBlobs = new BlobCollection<T>();
1753 Layer<T> m_layer;
1754
1762 public LayerParameterEx(LayerParameter p, BlobCollection<T> colBlobs, BlobCollection<T> colLayerBlobs, Layer<T> sharedLayer)
1763 : base(p)
1764 {
1765 m_colSharedBlobs = colBlobs;
1766 m_colLayerBlobs = colLayerBlobs;
1767 m_layer = sharedLayer;
1768 }
1769
1774 {
1775 get { return m_layer; }
1776 }
1777
1782 {
1783 get { return m_colSharedBlobs; }
1784 }
1785
1790 {
1791 get { return m_colLayerBlobs; }
1792 }
1793
1799 public override LayerParameter Clone(bool bCloneBlobs)
1800 {
1801 return new LayerParameterEx<T>(base.Clone(bCloneBlobs), m_colSharedBlobs, m_colLayerBlobs, m_layer);
1802 }
1803 }
1804}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
The Log class provides general output in text form.
Definition: Log.cs:13
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
Definition: Log.cs:394
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
void CHECK_LE(double df1, double df2, string str)
Test whether one number is less than or equal to another.
Definition: Log.cs:263
void CHECK_GE(double df1, double df2, string str)
Test whether one number is greater than or equal to another.
Definition: Log.cs:287
Specifies a key-value pair of properties.
Definition: PropertySet.cs:16
The BlobCollection contains a list of Blobs.
void SetDiff(double df)
Set all blob diff to the value specified.
int Count
Returns the number of items in the collection.
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
Tuple< double, double, double, double > minmax_data(Blob< T > work, bool bDetectNans=false, bool bUseChunks=false)
Returns the minimum and maximum values in the data of the Blob.
Definition: Blob.cs:2624
void Share(Blob< T > b)
Share another Blob with this one, by setting the data and diff to the same data and diff of the other...
Definition: Blob.cs:1850
bool HalfSize
Returns whether or not this blob is using half sizes.
Definition: Blob.cs:369
string shape_string
Returns a string describing the Blob's shape.
Definition: Blob.cs:657
void ConvertToBase(long hWorkMem, ulong lWorkSize, bool bData, bool bDiff)
Converts this blob from the half type to the base type.
Definition: Blob.cs:339
void ConvertToHalf(long hWorkMem, ulong lWorkSize, bool bData, bool bDiff)
Converts this blob from its base type to the half type.
Definition: Blob.cs:306
Tuple< double, double, double, double > minmax_diff(Blob< T > work, bool bDetectNans=false, bool bUseChunks=false)
Returns the minimum and maximum values in the diff of the Blob.
Definition: Blob.cs:2694
ulong GetConversionWorkSize(bool bUseHalfSize)
Returns the amount of memory (in bytes) required to convert from base to half and back.
Definition: Blob.cs:293
T[] update_cpu_data()
Update the CPU data by transferring the GPU data over to the Host.
Definition: Blob.cs:1470
string Name
Get/set the name of the Blob.
Definition: Blob.cs:2184
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
The GetIterationArgs is sent bubbled up to the solver when a layer needs to know the curret training ...
Definition: EventArgs.cs:748
The GetWorkBlobArgs are passed to the Layer::OnGetWorkBlob event which is supported for debugging onl...
Definition: EventArgs.cs:91
The TransferInput class is used to transfer get and set input data.
The WorkspaceArgs are passed to both the Layer::OnSetWorkspace and Layer::OnGetWorkspace events.
Definition: EventArgs.cs:17
long WorkspaceData
Get/set the handle to workspace data in GPU memory.
Definition: EventArgs.cs:36
ulong WorkspaceSizeInBytes
Get/set the workspace memory size in bytes.
Definition: EventArgs.cs:45
The AbsValLayer computes the absolute value of the input.
Definition: AbsValLayer.cs:21
The AccuracyLayer computes the classification accuracy for a one-of-many classification task....
The ArgMaxLayer computes the index of the K max values for each datum across all dimensions ....
Definition: ArgMaxLayer.cs:26
The Binomial Normal Log Liklihod Layer.
Definition: BNLLLayer.cs:23
The BatchNormLayer normalizes the input to have 0-mean and/or unit (1) variance across the batch....
The BatchReindexLayer provides an index into the input blob along its first axis.
The BiasLayer computes a sum of two input Blobs, with the shape of the latter Blob 'broadcast' to mat...
Definition: BiasLayer.cs:25
The ClipLayer provides a neuron layer that clips the data to fit within the [min,max] range....
Definition: ClipLayer.cs:21
The ConcatLayer takes at least two Blobs and concatentates them along either the num or channel dimen...
Definition: ConcatLayer.cs:24
The ConstantLayer provides a layer that just outputs a constant value. This layer is initialized with...
The ContrastiveLossLayer computes the contrastive loss where . This layer is initialized with the My...
The ConvolutionLayer convolves the input image with a bank of learned filters, and (optionally) adds ...
The CropLayer takes a Blob and crops it to the shape specified by the second input Blob,...
Definition: CropLayer.cs:22
The DataLayer loads data from the IXImageDatabase database. This layer is initialized with the MyCaff...
Definition: DataLayer.cs:24
The DataNormalizerLayer normalizes the input data (and optionally label) based on the normalization o...
The DebugLayer merely stores, up to max_stored_batches, batches of input which are then optionally us...
Definition: DebugLayer.cs:21
The DeconvolutionLayer convolves the input with a bank of learned filtered, and (optionally) add bias...
During training only, sets a random portion of to 0, adjusting the rest of the vector magnitude acco...
Definition: DropoutLayer.cs:22
The DummyDataLayer provides data to the Net generated by a Filler. This layer is initialized with the...
The ELULayer computes exponential linear unit non-linearity . This layer is initialized with the MyCa...
Definition: ELULayer.cs:27
The EltwiseLayer computes elementwise oeprations, such as product and sum, along multiple input blobs...
Definition: EltwiseLayer.cs:23
The EmbedLayer is a layer for learning 'embeddings' of one-hot vector input. This layer is initialize...
Definition: EmbedLayer.cs:23
The EuclideanLossLayer computes the Euclidean (L2) loss for real-valued regression tasks.
The ExpLayer which computes the exponential of the input. This layer is initialized with the MyCaffe....
Definition: ExpLayer.cs:22
The FilterLayer takes two+ Blobs, interprets last Blob as a selector and filters remaining Blobs acco...
Definition: FilterLayer.cs:19
The FlattenLayer reshapes the input Blob into flat vectors This layer is initialized with the MyCaffe...
Definition: FlattenLayer.cs:28
The GradientScaleLayer which scales the deltas during the backpropagation. This layer is initialized ...
The HingeLossLayer computes the hinge loss for a one-of-many classification task. This layer is initi...
The Im2ColLayer is a helper layer for image operations that rearranges image regions into column vect...
Definition: Im2colLayer.cs:24
The ImageDataLayer loads data from the image files located in the root directory specified....
The InforgainLossLayer is a generalization of SoftmaxWithLossLayer that takes an 'information gain' (...
The InnerProductLayer, also know as a 'fully-connected' layer, computes the inner product with a set ...
The InputLayer provides data to the Net by assigning top Blobs directly. This layer is initialized wi...
Definition: InputLayer.cs:22
The "Local Response Normalization" LRNLayer is used to normalize the input in a local region across o...
Definition: LRNLayer.cs:21
The LSTMLayer processes sequential inputs using a 'Long Short-Term Memory' (LSTM) [1] style recurrent...
Definition: LSTMLayer.cs:59
[DEPRECIATED - use LSTMAttentionLayer instead with enable_attention = false] The LSTMSimpleLayer is a...
The LSTMUnitLayer is a helper for LSTMLayer that computes a single timestep of the non-linearity of t...
/b DEPRECIATED (use DataLayer DataLabelMappingParameter instead) The LabelMappingLayer converts origi...
An interface for the units of computation which can be composed into a Net.
Definition: Layer.cs:31
Log m_log
Specifies the Log for output.
Definition: Layer.cs:43
double backward_timing
Returns the timing of the last backward pass in milliseconds.
Definition: Layer.cs:1257
float[] convertF(T[] rg)
Converts an array of float values into an array of generic values.
Definition: Layer.cs:1401
virtual WorkspaceArgs getWorkspace()
Returns the WorkspaceArgs used to share a workspace between Layers.
Definition: Layer.cs:1285
long convert_to_full(int nCount, long hMem)
Convert half memory to full memory.
Definition: Layer.cs:514
double backward_timing_average
Returns the average timing of the backward passes in milliseconds.
Definition: Layer.cs:1265
virtual void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
Definition: Layer.cs:891
GetIterationArgs getCurrentIteration()
Fires the OnGetIteration event to query the current iteration.
Definition: Layer.cs:398
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
Definition: Layer.cs:47
void SetNetReshapeRequest()
Called by the Net when requesting a reshape.
Definition: Layer.cs:414
EventHandler< WorkspaceArgs > OnGetWorkspace
Specifies the OnGetWorkspace event that fires when the getWorkspace() function is called by a layer t...
Definition: Layer.cs:124
void setShapes(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Set the internal shape sizes - used when determining if a Reshape is necessary.
Definition: Layer.cs:685
virtual bool SupportsPostProcessing
Should return true when pre PostProcessing methods are overriden.
Definition: Layer.cs:264
virtual int MinBottomBlobs
Returns the minimum number of bottom (input) Blobs required by the Layer, or -1 if no minimum number ...
Definition: Layer.cs:953
virtual void SetOnDebug(EventHandler< GetWorkBlobArgs< T > > fn)
Set the OnDebug event.
Definition: Layer.cs:370
void convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
Definition: Layer.cs:535
virtual bool AutoTopBlobs
Return whether "anonymous" top (output) Blobs are created automatically by the Layer.
Definition: Layer.cs:1031
virtual void dispose()
Releases all GPU and host resources used by the Layer.
Definition: Layer.cs:188
void dispose(ref Blob< T > b)
Helper method used to dispose internal blobs.
Definition: Layer.cs:209
int val_at(T[] rg, int nIdx)
Returns the integer value at a given index in a generic array.
Definition: Layer.cs:1434
virtual List< Tuple< string, int, double > > PostProcessOutput(Blob< T > blobSofmtax, int nK=1)
The PostProcessOutput allows derivative data layers to post-process the results, converting them back...
Definition: Layer.cs:328
void set_loss(int nTopIdx, double dfLoss)
Sets the loss associated with a top Blob at a given index.
Definition: Layer.cs:918
double loss(int nTopIdx)
Returns the scalar loss associated with the top Blob at a given index.
Definition: Layer.cs:908
abstract void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Performs Layer specific setup. Derived layers should override this function as well as the Reshape fu...
bool shareLayerBlob(Blob< T > b, List< int > rgMinShape)
Attempts to share a Layer Blob if another parameter Blob with the same name and acceptable size is fo...
Definition: Layer.cs:1170
T m_tZero
Specifies a generic type equal to 0.0.
Definition: Layer.cs:76
void SetEnablePassthrough(bool bEnable)
Enables/disables the pass-through mode.
Definition: Layer.cs:1276
void Backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Given the top Blob error gradients, compute the bottom Blob error gradients.
Definition: Layer.cs:815
virtual bool setWorkspace(ulong lSizeInBytes)
Sets the workspace size (in items) and returns true if set, false otherwise.
Definition: Layer.cs:1300
T m_tOne
Specifies a generic type equal to 1.0.
Definition: Layer.cs:72
void ConvertToBase(BlobCollection< T > col)
ConvertToBase converts any blobs in a collection that are in half size to the base size.
Definition: Layer.cs:579
abstract void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
This forward abstract function must be overriden by each derived Layer class to compute the top (outp...
virtual bool ReInitializeParameters(WEIGHT_TARGET target)
Re-initialize the parameters of the layer.
Definition: Layer.cs:389
bool shareParameter(Blob< T > b, List< int > rgMinShape, bool bAllowEndsWithComparison=false)
Attempts to share a parameter Blob if another parameter Blob with the same name and accpetable size i...
Definition: Layer.cs:1152
virtual List< Tuple< string, int, double > > PostProcessLogitsOutput(int nCurIdx, Blob< T > blobLogits, Layer< T > softmax, int nAxis, int nK=1)
The PostProcessLogitsOutput allows derivative data layers to post-process the results,...
Definition: Layer.cs:342
bool compareShapes(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Compare the shapes of the top and bottom and if the same, return true, otherwise false.
Definition: Layer.cs:648
EventHandler< GetIterationArgs > OnGetIteration
Specifies the OnGetIteration event that fires when a layer needs to get the current iteration from th...
Definition: Layer.cs:132
LayerParameter.? LayerType parent_layer_type
Optionally, specifies the parent layer type (e.g. LOSS, etc.)
Definition: Layer.cs:248
bool shareLayerBlobs(Layer< T > layer)
Attempts to share the Layer blobs and internal_blobs with matching names and sizes with those in anot...
Definition: Layer.cs:1187
void SetPhase(Phase phase)
Changes the layer's Phase to the one specified.
Definition: Layer.cs:423
double Forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Given the bottom (input) Blobs, this function computes the top (output) Blobs and the loss.
Definition: Layer.cs:728
void dispose(ref Layer< T > l)
Helper method used to dispose internal layers.
Definition: Layer.cs:196
T[] convert(double[] rg)
Converts an array of double values into an array of generic values.
Definition: Layer.cs:1385
double forward_timing_average
Returns the average timing of the forward passes in milliseconds.
Definition: Layer.cs:1249
LayerParameter.LayerType type
Returns the LayerType of this Layer.
Definition: Layer.cs:927
virtual string PostProcessOutput(int nIdx)
Convert the index to the word.
Definition: Layer.cs:361
virtual int ExactNumTopBlobs
Returns the exact number of top (output) Blobs required by the Layer, or -1 if no exact number is req...
Definition: Layer.cs:979
virtual bool AllowForceBackward(int nBottomIdx)
Return whether to allow
Definition: Layer.cs:1046
void set_param_propagate_down(int nParamIdx, bool bPropagate)
Sets whether or not the Layer should compute gradients w.r.t. a parameter at a particular index given...
Definition: Layer.cs:1068
double forward_timing
Returns the timing of the last forward pass in milliseconds.
Definition: Layer.cs:1241
float convertF(T df)
Converts a generic to a float value.
Definition: Layer.cs:1359
bool m_bConvertTopOnBwd
Specifies whether or not to convert the top on the backward pass when using half sized memory (typica...
Definition: Layer.cs:92
DictionaryMap< double > m_rgLoss
Specifies the loss values that indeicate whether each top (output) Blob has a non-zero weight in the ...
Definition: Layer.cs:68
virtual string PostProcessFullOutput(Blob< T > blobSoftmax)
The PostProcessFullOutput allows derivative data layers to post-process the results,...
Definition: Layer.cs:351
T convert(float f)
Converts a float to a generic.
Definition: Layer.cs:1339
abstract void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Adjust the shapes of top blobs and internal buffers to accomodate the shapes of the bottom blobs.
bool m_bUseHalfSize
Specifies that the half size of the top (if any) should be converted to the base size.
Definition: Layer.cs:84
BlobCollection< T > m_colInternalBlobs
Specifies internal blobs used by the layer.
Definition: Layer.cs:59
bool m_bEnablePassthrough
Enables/disables the pass-through mode for the layer. Default = false.
Definition: Layer.cs:80
double convertD(T df)
Converts a generic to a double value.
Definition: Layer.cs:1349
LayerParameter.? LayerType m_parentLayerType
Specifies the layer type of the parent.
Definition: Layer.cs:108
virtual bool reshapeNeeded(BlobCollection< T > colBottom, BlobCollection< T > colTop, bool bReset=true)
Tests the shapes of both the bottom and top blobs and if they are the same as the previous sizing,...
Definition: Layer.cs:622
Size size_at(Blob< T > b)
Returns the Size of a given two element Blob, such as one that stores Blob size information.
Definition: Layer.cs:1444
virtual int MaxTopBlobs
Returns the maximum number of top (output) Blobs required by the Layer, or -1 if no maximum number is...
Definition: Layer.cs:1005
T[] convert(float[] rg)
Converts an array of float values into an array of generic values.
Definition: Layer.cs:1417
void Dispose()
Releases all GPU and host resources used by the Layer.
Definition: Layer.cs:180
virtual bool SupportsPreProcessing
Should return true when PreProcessing methods are overriden.
Definition: Layer.cs:256
virtual bool EqualNumBottomTopBlobs
Returns true if the Layer requires and equal number of bottom (input) and top (output) Blobs.
Definition: Layer.cs:1018
virtual bool PreProcessInput(string strEncInput, int? nDecInput, BlobCollection< T > colBottom)
Preprocess the input data for the RUN phase.
Definition: Layer.cs:316
bool param_propagate_down(int nParamIdx)
Returns whether or not the Layer should compute gradients w.r.t. a parameter at a particular index gi...
Definition: Layer.cs:1057
virtual int ExactNumBottomBlobs
Returns the exact number of bottom (input) Blobs required by the Layer, or -1 if no exact number is r...
Definition: Layer.cs:940
EventHandler< GetWorkBlobArgs< T > > OnDebug
Specifies the OnGetWorkBlob event that is only supported when debugging to get a work blob from the p...
Definition: Layer.cs:140
T convert(double df)
Converts a double to a generic.
Definition: Layer.cs:1329
virtual int MinTopBlobs
Returns the minimum number of top (output) Blobs required by the Layer, or -1 if no minimum number is...
Definition: Layer.cs:992
bool m_bConvertBottom
Specifies whether or not the layer should convert the bottom when using half sized memory.
Definition: Layer.cs:96
Phase m_phase
Specifies the Phase under which the Layer is run.
Definition: Layer.cs:51
virtual void ResetOnDebug(EventHandler< GetWorkBlobArgs< T > > fn)
Reset the OnDebug event, disabling it.
Definition: Layer.cs:379
virtual void SetNetParameterUsed(NetParameter np)
This function allows other layers to gather needed information from the NetParameters if any,...
Definition: Layer.cs:492
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
Definition: Layer.cs:39
void Setup(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Implements common Layer setup functionality.
Definition: Layer.cs:439
void dispose(ref BlobCollection< T > rg, bool bSetToNull=true)
Dispose the blob collection.
Definition: Layer.cs:223
void check_nan(Blob< T > b)
Checks a Blob for NaNs and throws an exception if found.
Definition: Layer.cs:1313
bool m_bReshapeOnForwardNeeded
Specifies whether or not the reshape on forward is needed or not.
Definition: Layer.cs:100
EventHandler< WorkspaceArgs > OnSetWorkspace
Specifies the OnSetWorkspace event that fires when the setWorkspace() function is called by a layer t...
Definition: Layer.cs:128
Layer(CudaDnn< T > cuda, Log log, LayerParameter p)
The Layer constructor.
Definition: Layer.cs:158
void CheckBlobCounts(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Called by the Layer::Setup function to check the number of bottom (input) and top (output) Blobs prov...
Definition: Layer.cs:1080
static Layer< T > Create(CudaDnn< T > cuda, Log log, LayerParameter p, CancelEvent evtCancel, IXDatabaseBase db=null, TransferInput trxinput=null)
Create a new Layer based on the LayerParameter.
Definition: Layer.cs:1468
LayerParameter.LayerType m_type
Specifies the Layer type.
Definition: Layer.cs:35
void SetLossWeights(BlobCollection< T > colTop)
Called by Layer::Setup to initialize the weights associated with any top (output) Blobs in the loss f...
Definition: Layer.cs:1109
LayerParameter layer_param
Returns the LayerParameter for this Layer.
Definition: Layer.cs:899
virtual void ConnectLoss(LossLayer< T > layer)
Called to connect the loss OnLoss event to a specified layer (typically the data layer).
Definition: Layer.cs:240
virtual int MaxBottomBlobs
Returns the maximum number of bottom (input) Blobs required by the Layer, or -1 if no maximum number ...
Definition: Layer.cs:966
BlobCollection< T > blobs
Returns the collection of learnable parameter Blobs for the Layer.
Definition: Layer.cs:875
virtual bool SupportsPostProcessingLogits
Should return true when pre PostProcessingLogits methods are overriden.
Definition: Layer.cs:272
BlobCollection< T > internal_blobs
Returns the collection of internal Blobs used by the Layer.
Definition: Layer.cs:883
double[] convertD(T[] rg)
Converts an array of generic values into an array of double values.
Definition: Layer.cs:1369
virtual bool SupportsPostProcessingFullOutput
Should return true when PostProcessingFullOutput is supported.
Definition: Layer.cs:280
abstract void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
This backward abstract function must be overriden by each derived Layer class to compute the bottom (...
BlobCollection< T > m_colBlobs
Specifies the learnable parameter Blobs of the Layer.
Definition: Layer.cs:55
DictionaryMap< bool > m_rgbParamPropagateDown
Specifies whether or not to compute the learnable diff of each parameter Blob.
Definition: Layer.cs:63
LayerParameter convertLayerParam(LayerParameter pChild, LayerParameter pParent)
Called to convert a parent LayerParameterEx, used in blob sharing, with a child layer parameter.
Definition: Layer.cs:1134
bool m_bConvertTopOnFwd
Specifies whether or not the layer should convert the top on the forward pass when using half sized m...
Definition: Layer.cs:88
bool m_bNetReshapeRequest
Specifies whether the reshape is requested from a Net.Reshape call or not.
Definition: Layer.cs:104
virtual BlobCollection< T > PreProcessInput(PropertySet customInput, out int nSeqLen, BlobCollection< T > colBottom=null)
The PreprocessInput allows derivative data layers to convert a property set of input data into the bo...
Definition: Layer.cs:294
The LayerParameterEx class is used when sharing another Net to conserve GPU memory and extends the La...
Definition: Layer.cs:1750
BlobCollection< T > SharedBlobs
Returns the shared parameter Blobs.
Definition: Layer.cs:1782
Layer< T > SharedLayer
Returns the layer in the shared Net that matches this one.
Definition: Layer.cs:1774
BlobCollection< T > SharedLayerBlobs
Returns the shared Layer Blobs.
Definition: Layer.cs:1790
override LayerParameter Clone(bool bCloneBlobs)
Creates and returns a new copy of this instance.
Definition: Layer.cs:1799
LayerParameterEx(LayerParameter p, BlobCollection< T > colBlobs, BlobCollection< T > colLayerBlobs, Layer< T > sharedLayer)
The LayerParameterEx constructor.
Definition: Layer.cs:1762
The LogLayer computes the log of the input. This layer is initialized with the MyCaffe....
Definition: LogLayer.cs:22
The LossLayer provides an interface for Layer's that take two blobs as input – usually (1) prediction...
Definition: LossLayer.cs:23
The "Mean-Variance Normalization" MVNLayer normalizes the input to have 0-mean and/or unit (1) varian...
Definition: MVNLayer.cs:22
The MathLayer which computes various mathematical functions of the input. This layer is initialized w...
Definition: MathLayer.cs:17
The MemoryDataLayer provides data to the Net from memory. This layer is initialized with the MyCaffe....
The MemoryLossLayer provides a method of performing a custom loss functionality. Similar to the Memor...
The MultinomialLogicistLossLayer computes the multinomial logistc loss for a one-of-many classificati...
The PReLULayer computes the "Parameterized Rectified Linear Unit" non-linearity. This layer is initia...
Definition: PReLULayer.cs:31
The ParameterLayer passes its blob[0] data and diff to the top[0].
The PoolingLayer pools the input image by taking the max, average, etc. within regions....
Definition: PoolingLayer.cs:23
The PowerLayer computes the power of the input. This layer is initialized with the MyCaffe....
Definition: PowerLayer.cs:24
The RNNLayer processes time-varying inputs using a simple recurrent neural network (RNN)....
Definition: RNNLayer.cs:35
The ReLULayer computes the "Rectifier Linear Unit" ReLULayer non-linearity, a classic for neural netw...
Definition: ReLULayer.cs:27
The ReductionLayer computes the 'reductions' – operations that return a scalar output Blob for an inp...
The ReshapeLayer reshapes the input Blob into an arbitrary-sized output Blob. This layer is initializ...
Definition: ReshapeLayer.cs:21
The SPPLayer does spatial pyramid pooling on the input image by taking the max, average,...
Definition: SPPLayer.cs:25
The ScaleLayer computes the elementwise product of two input Blobs, with the shape of the latter Blob...
Definition: ScaleLayer.cs:27
The SigmoidCrossEntropyLayer computes the cross-entropy (logisitic) loss and is often used for predic...
The SigmoidLayer is a neuron layer that calculates the sigmoid function, a classc choice for neural n...
Definition: SigmoidLayer.cs:28
The SilenceLayer ignores bottom blobs while producing no top blobs. (This is useuful to suppress outp...
Definition: SilenceLayer.cs:17
The SliceLayer takes a blob and slices it along either the num or channel dimensions outputting multi...
Definition: SliceLayer.cs:18
The SoftmaxCrossEntropy2Layer computes the cross-entropy (logisitic) loss and is often used for predi...
The SoftmaxCrossEntropyLossLayer computes the cross-entropy (logisitic) loss and is often used for pr...
The SoftmaxLayer computes the softmax function. This layer is initialized with the MyCaffe....
Definition: SoftmaxLayer.cs:24
Computes the multinomial logistic loss for a one-of-many classification task, passing real-valued pre...
The SplitLayer creates a 'split' path in the network by copying the bottom blob into multiple top blo...
Definition: SplitLayer.cs:17
The SwishLayer provides a novel activation function that tends to work better than ReLU....
Definition: SwishLayer.cs:22
The TanhLayer is a neuron layer that calculates the tanh function, popular with auto-encoders....
Definition: TanhLayer.cs:28
The ThresholdLayer is a neuron layer that tests whether the input exceeds a threshold: outputs 1 for ...
The TileLayer copies a Blob along specified dimensions. This layer is initialized with the MyCaffe....
Definition: TileLayer.cs:17
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
List< double > loss_weight
Specifies the loss weight.
LayerType type
Specifies the type of this LayerParameter.
Phase phase
Specifies the Phase for which this LayerParameter is run.
LayerType
Specifies the layer type.
override string ToString()
Returns a string representation of the LayerParameter.
List< BlobProto > blobs
Specifies the blobs of the LayerParameter.
virtual LayerParameter Clone(bool bCloneBlobs)
Creates a new copy of this instance of the parameter.
Specifies the parameters use to create a Net
Definition: NetParameter.cs:18
The ILayerCreator interface is implemented by each MyCaffe.layers.x layer extension dll and is used t...
Definition: Interfaces.cs:19
Layer< double > CreateDouble(CudaDnn< double > cuda, Log log, LayerParameter p, CancelEvent evtCancel, IXDatabaseBase db)
Create the layers when using the double base type.
Layer< float > CreateSingle(CudaDnn< float > cuda, Log log, LayerParameter p, CancelEvent evtCancel, IXDatabaseBase db)
Create the layers when using the float base type.
The IXDatabaseBase interface defines the general interface to the in-memory database.
Definition: Interfaces.cs:444
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
Phase
Defines the Phase under which to run a Net.
Definition: Interfaces.cs:61
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
WEIGHT_TARGET
Defines the type of weight to target in re-initializations.
Definition: Interfaces.cs:38
The MyCaffe.db.image namespace contains all image database related classes.
Definition: Database.cs:18
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
Definition: LayerFactory.cs:15
The MyCaffe.param namespace contains parameters used to create models.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12