MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DataGeneralGym.cs
1using MyCaffe.basecode;
3using MyCaffe.db.stream;
4using System;
5using System.Collections.Generic;
6using System.Diagnostics;
7using System.Drawing;
8using System.Drawing.Drawing2D;
9using System.Drawing.Imaging;
10using System.IO;
11using System.Linq;
12using System.Runtime.InteropServices;
13using System.Text;
14using System.Threading;
15using System.Threading.Tasks;
16
17namespace MyCaffe.gym
18{
22 public class DataGeneralGym : IXMyCaffeGymData, IDisposable
23 {
24 string m_strName = "DataGeneral";
25 Log m_log;
26 CryptoRandom m_random;
27 Dictionary<string, int> m_rgActions = new Dictionary<string, int>();
28 DATA_TYPE m_dt = DATA_TYPE.BLOB;
30 Phase m_phase = Phase.NONE;
31
36 {
37 }
38
42 public void Dispose()
43 {
44 }
45
57 public void Initialize(Log log, PropertySet properties)
58 {
59 m_log = log;
60 m_random = new CryptoRandom();
61 m_db = new MyCaffeStreamDatabase(m_log);
62 m_db.Initialize(QUERY_TYPE.GENERAL, properties.ToString());
63 m_db.Reset();
64 }
65
71 public IXMyCaffeGym Clone(PropertySet properties = null)
72 {
74
75 if (properties != null)
76 gym.Initialize(m_log, properties);
77
78 return gym;
79 }
80
85 {
86 get { return false; }
87 }
88
93 {
94 get { return m_dt; }
95 }
96
101 {
102 get { return new DATA_TYPE[] { DATA_TYPE.BLOB }; }
103 }
104
108 public string Name
109 {
110 get { return m_strName; }
111 }
112
116 public int UiDelay
117 {
118 get { return 0; }
119 }
120
124 public double TestingPercent
125 {
126 get { return 0.2; }
127 }
128
133 public Dictionary<string, int> GetActionSpace()
134 {
135 return m_rgActions;
136 }
137
141 public void Close()
142 {
143 m_db.Shutdown();
144 }
145
154 public Tuple<Bitmap, SimpleDatum> Render(bool bShowUi, int nWidth, int nHeight, bool bGetAction)
155 {
156 List<double> rgData = new List<double>();
157 return Render(bShowUi, nWidth, nHeight, rgData.ToArray(), bGetAction);
158 }
159
169 public Tuple<Bitmap, SimpleDatum> Render(bool bShowUi, int nWidth, int nHeight, double[] rgData, bool bGetAction)
170 {
171 return null;
172 }
173
180 public Tuple<State, double, bool> Reset(bool bGetLabel, PropertySet props = null)
181 {
182 m_db.Reset();
183 return Step(-1, bGetLabel);
184 }
185
193 public Tuple<State, double, bool> Step(int nAction, bool bGetLabel, PropertySet propExtra = null)
194 {
195 DataState data = new DataState();
196
197 SimpleDatum sd = m_db.Query(1000);
198 data.SetData(sd);
199
200 return new Tuple<State, double, bool>(data, 0, (sd == null) ? true : false);
201 }
202
210 {
211 if (dt == DATA_TYPE.DEFAULT)
212 dt = DATA_TYPE.BLOB;
213
214 if (dt != DATA_TYPE.BLOB)
215 {
216 if (log == null)
217 log = m_log;
218
219 if (log != null)
220 log.WriteLine("WARNING: This gym only supports the BLOB type, the datatype will be changed to BLOB.");
221 else
222 throw new Exception("This gym only supports the BLOB type.");
223
224 dt = DATA_TYPE.BLOB;
225 }
226
227 int nC = 1;
228 int nH = 1;
229 int nW = 0;
230
231 SourceDescriptor srcTrain = new SourceDescriptor((int)GYM_DS_ID.DATAGENERAL, Name + ".training", nW, nH, nC, false, false);
232 SourceDescriptor srcTest = new SourceDescriptor((int)GYM_SRC_TEST_ID.DATAGENERAL, Name + ".testing", nW, nH, nC, false, false);
233 DatasetDescriptor ds = new DatasetDescriptor((int)GYM_SRC_TRAIN_ID.DATAGENERAL, Name, null, null, srcTrain, srcTest, "DataGym", "Data Gym", null, GYM_TYPE.DATA);
234
235 m_dt = dt;
236
237 return ds;
238 }
239
249 public byte[] ConvertOutput(Stage stage, int nN, float[] rg, out string type)
250 {
251 return m_db.ConvertOutput(rg, out type);
252 }
253
258 {
259 get { return m_phase; }
260 set { m_phase = value; }
261 }
262 }
263
264 class DataState : State
265 {
266 SimpleDatum m_sd = null;
267
268 public DataState()
269 {
270 }
271
272 public DataState(DataState s)
273 {
274 m_sd = s.m_sd;
275 }
276
277 public override State Clone()
278 {
279 DataState data = new DataState(this);
280 data.SetData(m_sd);
281 return data;
282 }
283
284 public void SetData(SimpleDatum sd)
285 {
286 m_sd = sd;
287 }
288
289 public override SimpleDatum GetData(bool bNormalize, out int nDataLen)
290 {
291 nDataLen = (m_sd == null) ? 0 : m_sd.ItemCount;
292 return m_sd;
293 }
294 }
295}
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
Definition: CryptoRandom.cs:14
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
Specifies a key-value pair of properties.
Definition: PropertySet.cs:16
override string ToString()
Returns the string representation of the properties.
Definition: PropertySet.cs:325
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:161
int ItemCount
Returns the number of data items.
void SetData(SimpleDatum d)
Set the data of the current SimpleDatum by copying the data of another.
The DatasetDescriptor class describes a dataset which contains both a training data source and testin...
The SourceDescriptor class contains all information describing a data source.
The MyCaffeStreamDatabase provides a streaming data input source to MyCaffe gyms used as input for dy...
void Reset(int nStartOffset=0)
Reset the query to the start date used in Initialize, optionally with an offset from the start.
void Shutdown()
Shutdonw the streaming database.
void Initialize(QUERY_TYPE qt, string strSchema)
The Initialize method initializes the streaming database component, preparing it for data queries.
SimpleDatum Query(int nWait)
Query the next data in the streaming database.
byte[] ConvertOutput(float[] rg, out string type)
Converts the output values into the native type used by the first CustomQuery.
The DataGeneral Gym provides access to the MyCaffe Streaming Database with GENERAL query types.
void Close()
Shutdown and close the gym.
double TestingPercent
Returns the testinng percent of 0.2.
DatasetDescriptor GetDataset(DATA_TYPE dt, Log log=null)
Returns the dataset descriptor of the dynamic dataset produced by the Gym.
Tuple< State, double, bool > Step(int nAction, bool bGetLabel, PropertySet propExtra=null)
Step the gym one step in the data.
DATA_TYPE[] SupportedDataType
Returns the data types supported by this gym.
DataGeneralGym()
The constructor.
Tuple< Bitmap, SimpleDatum > Render(bool bShowUi, int nWidth, int nHeight, bool bGetAction)
Render the gym's current state on a bitmap and SimpleDatum.
Phase ActivePhase
Get/set the active phase under which the reset and next run.
void Initialize(Log log, PropertySet properties)
Initialize the gym with the specified properties.
Tuple< State, double, bool > Reset(bool bGetLabel, PropertySet props=null)
Reset the state of the gym.
bool RequiresDisplayImage
Returns true indicating that this Gym requires a display image.
Dictionary< string, int > GetActionSpace()
Returns the action space as a dictionary of name,actionid pairs.
Tuple< Bitmap, SimpleDatum > Render(bool bShowUi, int nWidth, int nHeight, double[] rgData, bool bGetAction)
Render the gyms specified data.
byte[] ConvertOutput(Stage stage, int nN, float[] rg, out string type)
Converts the output values into the native type used by the Gym during queries.
DATA_TYPE SelectedDataType
Returns the selected data type.
IXMyCaffeGym Clone(PropertySet properties=null)
Create a new copy of the gym.
string Name
Returns the gym's name.
void Dispose()
Release all resources used.
int UiDelay
Returns the delay to use (if any) when the user-display is visible.
State()
The constructor.
Definition: Interfaces.cs:346
The IXMyCaffeGym interface is used to interact with each Gym.
Definition: Interfaces.cs:192
The IXMyCaffeGym interface is used to interact with each Gym.
Definition: Interfaces.cs:99
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
Phase
Defines the Phase under which to run a Net.
Definition: Interfaces.cs:61
GYM_TYPE
Defines the gym type (if any).
Definition: Interfaces.cs:116
DATA_TYPE
Defines the gym data type.
Definition: Interfaces.cs:135
Stage
Specifies the stage underwhich to run a custom trainer.
Definition: Interfaces.cs:88
The MyCaffe.db.stream namespace contains all data streaming related classes.
QUERY_TYPE
Defines the query type to use.
Definition: Interfaces.cs:36
The MyCaffe.gym namespace contains all classes related to the Gym's supported by MyCaffe.
GYM_SRC_TRAIN_ID
Defines the Standard GYM Training Data Source ID's.
Definition: Interfaces.cs:45
GYM_DS_ID
Defines the Standard GYM Dataset ID's.
Definition: Interfaces.cs:18
GYM_SRC_TEST_ID
Defines the Standard GYM Testing Data Source ID's.
Definition: Interfaces.cs:72
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12