MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
BaseDataLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
7using MyCaffe.common;
8using MyCaffe.param;
9using MyCaffe.data;
10using MyCaffe.db.image;
11
12namespace MyCaffe.layers
13{
18 public abstract class BaseDataLayer<T> : Layer<T>
19 {
27 protected bool m_bOutputLabels;
39 protected SimpleDatum m_imgMean = null;
40
49 : base(cuda, log, p)
50 {
51 if (db != null)
52 {
53 if (db.GetVersion() != DB_VERSION.IMG_V1 && db.GetVersion() != DB_VERSION.IMG_V2)
54 throw new Exception("Currently only image databases are supported by the BaseDataLayer.");
55
57
58 if (p.type == LayerParameter.LayerType.DATA ||
59 p.type == LayerParameter.LayerType.ANNOTATED_DATA)
61
63 {
64 if (db != null)
66 else
67 m_log.WriteLine("WARNING: The image database is NULL, and therefore no mean image can not be acquired.");
68 }
69 }
70 }
71
73 protected override void dispose()
74 {
75 if (m_transformer != null)
76 {
77 m_transformer.Dispose();
78 m_transformer = null;
79 }
80 }
81
86 protected virtual bool setupSourceDescriptor()
87 {
88 return false;
89 }
90
95 {
96 get { return m_imgMean; }
97 set { m_imgMean = value; }
98 }
99
109 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
110 {
112
113 if (colTop.Count == 1)
114 m_bOutputLabels = false;
115 else
116 m_bOutputLabels = true;
117
118 int nC = 0;
119 int nH = 0;
120 int nW = 0;
121
122 if (m_src != null)
123 {
124 nC = m_src.Channels;
125 nH = m_src.Height;
126 nW = m_src.Width;
127 }
128 else if (m_imgMean != null)
129 {
130 nC = m_imgMean.Channels;
131 nH = m_imgMean.Height;
132 nW = m_imgMean.Width;
133 }
134 else if (m_param.type == LayerParameter.LayerType.MEMORYDATA)
135 {
139 }
140 else if (m_param.type == LayerParameter.LayerType.DUMMYDATA)
141 {
142 nC = (int)m_param.dummy_data_param.channels[0];
143 nH = (int)m_param.dummy_data_param.height[0];
144 nW = (int)m_param.dummy_data_param.height[0];
145 }
146 else if (m_param.type == LayerParameter.LayerType.INPUT)
147 {
148 if (m_param.input_param.shape[0].dim.Count > 1)
149 nC = (int)m_param.input_param.shape[0].dim[1];
150
151 if (m_param.input_param.shape[0].dim.Count > 2)
152 nH = (int)m_param.input_param.shape[0].dim[2];
153
154 if (m_param.input_param.shape[0].dim.Count > 3)
155 nW = (int)m_param.input_param.shape[0].dim[3];
156 }
157 else if (m_param.type == LayerParameter.LayerType.VIDEO_DATA)
158 {
159 nC = 3;
160 nH = (int)m_param.video_data_param.video_height;
161 nW = (int)m_param.video_data_param.video_width;
162 }
163
164 if (nC == 0 && nH == 0 && nW == 0)
165 throw new Exception("The sizing of C, H, W cannot be zero for all three!");
166
167 if (nC == 0)
168 nC = 1;
169
170 if (nH == 0)
171 nH = 1;
172
173 if (nW == 0)
174 nW = 1;
175
177 m_transformer.InitRand();
178
179 // The subclasses should setup the size of bottom and top.
180 DataLayerSetUp(colBottom, colTop);
181 }
182
188 protected abstract void DataLayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop);
189
195 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
196 {
197 }
198
200 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
201 {
202 }
203
208 {
209 get { return m_transformer; }
210 }
211 }
212}
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
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:161
int Channels
Return the number of channels of the data.
int Width
Return the width of the data.
int Height
Return the height of the data.
int ID
Get/set the database ID of the item.
The SourceDescriptor class contains all information describing a data source.
int Height
Returns the height of each data item in the data source.
int Width
Returns the width of each data item in the data source.
int Channels
Returns the item colors - 1 channel = black/white, 3 channels = RGB color.
The BlobCollection contains a list of Blobs.
int Count
Returns the number of items in the collection.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
Applies common transformations to the input data, such as scaling, mirroring, subtracting the image m...
The BaseDataLayer is the base class for data Layers that feed Blobs of data into the Net.
SimpleDatum m_imgMean
Specifies the SimpleDatum that optionally contains the image Mean for data centering.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Implements common data layer setup functionality, and calls DataLayerSetUp to do special data layer s...
bool m_bOutputLabels
Specifies whether or not the Layer should output labels.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Not implemented - data Layers do not perform backward.
SourceDescriptor m_src
Specifies the SourceDescriptor of the data source.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Data layers have no bottoms, so reshaping is trivial.
virtual bool setupSourceDescriptor()
Allows any derivative classes to pre-initialize the m_src which is used in LayerSetup before the Data...
IXImageDatabaseBase m_imgdb
Specifies the MyCaffeImageDatabase.
override void dispose()
Releases all GPU and host resources used by the Layer.
DataTransformer< T > m_transformer
Specifies the DataTransformer used to transform each data item as it loaded.
DataTransformer< T > Transformer
Returns the data transformer used.
SimpleDatum ImageMean
Get/set the image mean.
abstract void DataLayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Override this method to perform the actual data loading.
BaseDataLayer(CudaDnn< T > cuda, Log log, LayerParameter p, IXDatabaseBase db)
The BaseDataLayer constructor.
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
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
Definition: Layer.cs:39
string source
When used with the DATA parameter, specifies the data 'source' within the database....
List< uint > channels
DEPRECIATED - 4D dimensions, use 'shape' instead.
List< uint > height
>DEPRECIATED - 4D dimensions, use 'shape' instead.
List< BlobShape > shape
Define N shapes to set a shape for each top. Define 1 shape to set the same shape for every top....
Specifies the base parameter for all layers.
LayerType type
Specifies the type of this LayerParameter.
DummyDataParameter dummy_data_param
Returns the parameter set when initialized with LayerType.DUMMYDATA
MemoryDataParameter memory_data_param
Returns the parameter set when initialized with LayerType.MEMORY_DATA
InputParameter input_param
Returns the parameter set when initialized with LayerType.INPUT
TransformationParameter transform_param
Returns the parameter set when initialized with LayerType.TRANSFORM
DataParameter data_param
Returns the parameter set when initialized with LayerType.DATA
VideoDataParameter video_data_param
Returns the parameter set when initialized with LayerType.VIDEO_DATA
Phase phase
Specifies the Phase for which this LayerParameter is run.
LayerType
Specifies the layer type.
uint width
The width of the data.
uint channels
The number of channels in the data.
uint height
The height of the data.
bool use_imagedb_mean
Specifies whether to subtract the mean image from the image database, subtract the mean values,...
The IXDatabaseBase interface defines the general interface to the in-memory database.
Definition: Interfaces.cs:444
SimpleDatum GetItemMean(int nSrcId)
Returns the item (e.g., image or temporal item) mean for a data source.
SourceDescriptor GetSourceByName(string strSrc)
Returns the SourceDescriptor for a given data source name.
DB_VERSION GetVersion()
Returns the version of the MyCaffe Image Database being used.
The IXImageDatabaseBase interface defines the general interface to the in-memory image database.
Definition: Interfaces.cs:878
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.
Definition: Annotation.cs:12
DB_VERSION
Defines the image database version to use.
Definition: Interfaces.cs:397
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
The MyCaffe.data namespace contains dataset creators used to create common testing datasets such as M...
Definition: BinaryFile.cs:16
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