MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
EventLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8
12namespace MyCaffe.layers.nt
13{
22 public class EventLayer<T> : Layer<T>
23 {
27 public event EventHandler<ForwardArgs<T>> OnLayerSetup;
31 public event EventHandler<ForwardArgs<T>> OnReshape;
35 public event EventHandler<ForwardArgs<T>> OnForward;
39 public event EventHandler<BackwardArgs<T>> OnBackward;
40
48 : base(cuda, log, p)
49 {
51 }
52
58 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
59 {
61
62 if (OnLayerSetup != null)
63 OnLayerSetup(this, new ForwardArgs<T>(colBottom, colTop));
64 }
65
71 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
72 {
73 if (OnReshape != null)
74 {
75 OnReshape(this, new ForwardArgs<T>(colBottom, colTop));
76 return;
77 }
78
79 colTop[0].ReshapeLike(colBottom[0], m_bUseHalfSize);
80 }
81
91 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
92 {
93 if (OnForward != null)
94 {
95 OnForward(this, new ForwardArgs<T>(colBottom, colTop));
96 return;
97 }
98
99 for (int i = 0; i < colBottom.Count && i < colTop.Count; i++)
100 {
101 int nCount = colTop[i].count();
102 int nCountB = colBottom[i].count();
103
104 m_log.CHECK_EQ(nCount, nCountB, "The top and bottom at " + i.ToString() + " must have the same number of items.");
105
106 long hBottomData = colBottom[i].gpu_data;
107 long hTopData = colTop[i].mutable_gpu_data;
108
109 m_cuda.copy(nCount, hBottomData, hTopData);
110 }
111 }
112
126 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
127 {
128 if (OnBackward != null)
129 {
130 OnBackward(this, new BackwardArgs<T>(colTop, rgbPropagateDown, colBottom));
131 return;
132 }
133
134 for (int i = 0; i < colBottom.Count && i < colTop.Count; i++)
135 {
136 if (rgbPropagateDown[i])
137 {
138 int nCount = colTop[i].count();
139 int nCountB = colBottom[i].count();
140
141 m_log.CHECK_EQ(nCount, nCountB, "The top and bottom at " + i.ToString() + " must have the same number of items.");
142
143 long hBottomDiff = colBottom[i].mutable_gpu_diff;
144 long hTopDiff = colTop[i].gpu_diff;
145
146 m_cuda.copy(nCount, hTopDiff, hBottomDiff);
147 }
148 }
149 }
150 }
151}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
The BackwardArgs are passed to the OnBackward event of the EventLayer.
Definition: EventArgs.cs:703
The BlobCollection contains a list of Blobs.
int Count
Returns the number of items in the collection.
void ReshapeLike(BlobCollection< T > src)
Reshapes all blobs in the collection to the sizes of the source.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
The ForwardArgs are passed to the OnForward event of the EventLayer.
Definition: EventArgs.cs:666
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
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
Definition: Layer.cs:47
bool m_bUseHalfSize
Specifies that the half size of the top (if any) should be converted to the base size.
Definition: Layer.cs:84
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
Definition: Layer.cs:39
LayerParameter.LayerType m_type
Specifies the Layer type.
Definition: Layer.cs:35
The EventLayer provides an event that fires on the forward pass and another that fires on the backwar...
Definition: EventLayer.cs:23
EventHandler< ForwardArgs< T > > OnLayerSetup
Defines the event that fires from within the LayerSetup function.
Definition: EventLayer.cs:27
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
Definition: EventLayer.cs:71
EventLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The EventLayer constructor.
Definition: EventLayer.cs:47
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the absolute value inputs.
Definition: EventLayer.cs:126
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: EventLayer.cs:58
EventHandler< ForwardArgs< T > > OnReshape
Defines the event that fires from within the Reshape function.
Definition: EventLayer.cs:31
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Either fires the OnForward event, or acts as a pass-through.
Definition: EventLayer.cs:91
EventHandler< BackwardArgs< T > > OnBackward
Defines the event that fires from within the backward pass.
Definition: EventLayer.cs:39
EventHandler< ForwardArgs< T > > OnForward
Defines the event that fires from within the forward pass.
Definition: EventLayer.cs:35
Specifies the base parameter for all layers.
bool use_halfsize
Specifies whether or not to use half sized memory or not.
LayerType
Specifies the layer type.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
The MyCaffe.layers.nt namespace contains all Neural Transfer related layers.
Definition: LayerFactory.cs:19
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