MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DummyDataLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8using MyCaffe.fillers;
9
10namespace MyCaffe.layers
11{
17 public class DummyDataLayer<T> : Layer<T>
18 {
19 List<Filler<T>> m_rgFillers = new List<Filler<T>>();
20 List<bool> m_rgbRefill = new List<bool>();
21
34 : base(cuda, log, p)
35 {
37 }
38
42 public override int ExactNumBottomBlobs
43 {
44 get { return 0; }
45 }
46
50 public override int MinTopBlobs
51 {
52 get { return 1; }
53 }
54
60 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
61 {
62 int num_top = colTop.Count;
64 int num_data_filler = param.data_filler.Count;
65
66 m_log.CHECK(num_data_filler == 0 || num_data_filler == 1 || num_data_filler == num_top, "Number of data fillers must be 0, 1 or equal to the number of tops: " + num_top.ToString() + "; you specified " + num_data_filler.ToString() + " data fillers.");
67
68 bool legacy_dims = (param.num.Count > 0 || param.channels.Count > 0 || param.height.Count > 0 || param.width.Count > 0) ? true : false;
69
70 if (legacy_dims)
71 {
72 m_log.CHECK_EQ(0, param.shape.Count, "Both shape and legacy fields were specified.");
73 // Using depreciated 4D output dim specifiers.
74 m_log.CHECK(param.num.Count == 1 || param.num.Count == num_top, "Must specify 'num' once, or once per top blob (" + num_top.ToString() + "); specified " + param.num.Count.ToString() + ".");
75 m_log.CHECK(param.channels.Count == 1 || param.channels.Count == num_top, "Must specify 'channels' once, or once per top blob (" + num_top.ToString() + "); specified " + param.channels.Count.ToString() + ".");
76 m_log.CHECK(param.height.Count == 1 || param.height.Count == num_top, "Must specify 'height' once, or once per top blob (" + num_top.ToString() + "); specified " + param.height.Count.ToString() + ".");
77 m_log.CHECK(param.width.Count == 1 || param.width.Count == num_top, "Must specify 'width' once, or once per top blob (" + num_top.ToString() + "); specified " + param.width.Count.ToString() + ".");
78 }
79
80 // refill_[i] tells Forward i whether or not to actually refill top Blob i.
81 // if refill_[i] is false, Forward does nothing for Blob i. We use this to
82 // avoid wastefully refilling 'constant' Blobs in every forward pass.
83 // We first fill refill_ in with the INVERSE of its final values.
84 // The first time we run Forward from the LayerSetup method, we'll fill only
85 // Blobs for which refill_ is normally false. These blobs will never be
86 // filled again.
87 m_rgbRefill = new List<bool>();
88 m_rgFillers = new List<Filler<T>>();
89
90 if (num_data_filler <= 1)
91 {
92 FillerParameter filler_param;
93
94 if (num_data_filler == 0)
95 {
96 filler_param = new FillerParameter("constant");
97 filler_param.value = 0;
98 }
99 else
100 {
101 filler_param = param.data_filler[0].Clone();
102 }
103
104 // Refill on each iteration iff not using a constant filler,
105 // but use the inverse of this rule for the first run.
106 m_rgbRefill.Add((filler_param.type == "constant") ? true : false);
107 m_rgFillers.Add(Filler<T>.Create(m_cuda, m_log, filler_param));
108 }
109 else
110 {
111 for (int i=0; i<num_top; i++)
112 {
113 m_rgFillers.Add(Filler<T>.Create(m_cuda, m_log, param.data_filler[i]));
114 // Refill on each iteration iff not using a constant filler,
115 // but use the inverse of this rule for the first run.
116 m_rgbRefill.Add((param.data_filler[i].type == "constant") ? true : false);
117 }
118 }
119
120 for (int i=0; i<num_top; i++)
121 {
122 if (legacy_dims)
123 {
124 int num = (int)((param.num.Count == 1) ? param.num[0] : param.num[i]);
125 int channels = (int)((param.channels.Count == 1) ? param.channels[0] : param.channels[i]);
126 int height = (int)((param.height.Count == 1) ? param.height[0] : param.height[i]);
127 int width = (int)((param.width.Count == 1) ? param.width[0] : param.width[i]);
128 colTop[i].Reshape(num, channels, height, width);
129 }
130 else
131 {
132 int shape_index = (param.shape.Count == 1) ? 0 : i;
133 colTop[i].Reshape(param.shape[shape_index]);
134 }
135 }
136
137 // Run Forward once, with refill_ inverted, to fill the constant blobs.
138 Forward(colBottom, colTop);
139
140 // Invert the inverted refill_ values to refill the desired (non-constant)
141 // Blobs in every usual forward pass.
142 for (int i = 0; i < m_rgbRefill.Count; i++)
143 {
145 m_rgbRefill[i] = true;
146 else
147 m_rgbRefill[i] = !m_rgbRefill[i];
148 }
149 }
150
156 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
157 {
158 }
159
160
169 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
170 {
171 for (int i = 0; i < colTop.Count; i++)
172 {
173 int filler_id = (m_rgFillers.Count > 1) ? i : 0;
174
175 if (m_rgbRefill[filler_id])
176 m_rgFillers[filler_id].Fill(colTop[i]);
177 }
178 }
179
181 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
182 {
183 }
184 }
185}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK(bool b, string str)
Test a flag for true.
Definition: Log.cs:227
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
The BlobCollection contains a list of Blobs.
int Count
Returns the number of items in the collection.
void Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
Abstract Filler class used to fill blobs with values.
Definition: Filler.cs:19
The DummyDataLayer provides data to the Net generated by a Filler. This layer is initialized with the...
DummyDataLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The DummyDataLayer constructor.
override int ExactNumBottomBlobs
Returns 0 for data layers have no bottom (input) Blobs.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Not implemented - data Layers do not perform backward..
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Data layers have no bottoms, so reshaping is trivial.
override int MinTopBlobs
Returns the minimum number of required top (output) Blobs: data
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Run the Forward computation, which fills the data into the top (output) Blobs.
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
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
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
Definition: Layer.cs:39
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
This layer produces N >= 1 top blobs. DummyDataParameter must specify 1 or shape fields,...
List< uint > width
DEPRECIATED - 4D dimensions, use 'shape' instead.
List< FillerParameter > data_filler
If 0 data fillers are specified, ConstantFiller
List< BlobShape > shape
Specifies the shape of the dummy data where: shape(0) = num shape(1) = channels shape(2) = height sha...
List< uint > num
DEPRECIATED - 4D dimensions, use 'shape' instead.
bool force_refill
(optional, default = true) Specifies whether to force refill the data (even constant data) as opposed...
List< uint > channels
DEPRECIATED - 4D dimensions, use 'shape' instead.
List< uint > height
>DEPRECIATED - 4D dimensions, use 'shape' instead.
Specifies the filler parameters used to create each Filler.
double value
Specifies the value used by 'constant' filler.
string type
Specifies the type of filler to use.
Specifies the base parameter for all layers.
DummyDataParameter dummy_data_param
Returns the parameter set when initialized with LayerType.DUMMYDATA
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.fillers namespace contains all fillers including the Filler class.
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