CorTeX Core ABI 7
ToolTech Software · C++ library reference
Loading...
Searching...
No Matches
ctxInputOutputImpl.h
Go to the documentation of this file.
1//*****************************************************************************
2// File : ctxInputOutputImpl.h
3// Description : Declaration of generic input/output classes
4// Author : Anders Modén
5// Product : CorTeX 2.0.1
6//
7// Copyright © 2015- Anders Modén, Sweden
8//
9// NOTE: CorTeX is a software development kit (SDK)
10// for neural networks and genetic programming.
11// Licensed by Anders Modén.
12//
13//
14// Revision History...
15//
16// Who Date Description
17//
18// AMO 170509 Created file (1.0.15)
19// AMO 261012 Added optional interpolation entry point (2.0.2)
20//
21//******************************************************************************
22#ifndef __CTX_INPUT_OUTPUT_IMPL_H__
23#define __CTX_INPUT_OUTPUT_IMPL_H__
24
25#include "ctxInput.h"
26#include "ctxOutput.h"
27#include "ctxImageUtils.h"
28
29// -----------------------------------------------------------------------------------------
30
32 public ctxCategoryManager,
34{
35public:
36
38
39 // Input
40 CTX_EXPORT virtual gzFloat getValue_U(ctxContext *context,gzUInt32 index , gzUInt32 currentSample);
41 CTX_EXPORT virtual gzUInt32 getSampleSize();
42 CTX_EXPORT virtual gzUInt32 getSamples();
43
44 // Output
46 CTX_EXPORT virtual gzFloat getExpectedValue_U(ctxContext *context, gzUInt32 index, gzUInt32 currentSample);
47 CTX_EXPORT virtual gzUInt32 getResultSize();
48 CTX_EXPORT virtual gzUInt32 getResults();
49
50 // Results
51
52 CTX_EXPORT virtual gzFloat getResultValue(gzUInt32 id, gzUInt32 index , gzUInt32 sample_cx,gzUInt32 sample_cy,gzUInt32 sample_cz);
53 CTX_EXPORT virtual gzVoid onLoadImage(const gzString &name, const gzString &path, gzUInt32 id);
54 CTX_EXPORT virtual gzArray<gzFloat> getExpectedValueArray(const gzString &name, const gzString &path, gzUInt32 id);
55
56 // Division utilities
57
61
62 // Sampling
63
64 CTX_EXPORT gzFloat sampleImage(gzUInt32 index, gzFloat x, gzFloat y, gzInt32 depth);
65
70 CTX_EXPORT gzFloat sampleImageInterpolated(gzUInt32 index, gzDouble x, gzDouble y,
71 gzInt32 depth, ctxImageInterpolationType interpolation);
72
73 // Image Accessor
74
75 GZ_PROPERTY_EXPORT(ctxImageAccessInterfacePtr, Accessor, CTX_EXPORT);
76
77 // Sample WIndow
78
79 GZ_PROPERTY_EXPORT(gzUInt32, SampleWidth, CTX_EXPORT); // Size of the sampling window
80 GZ_PROPERTY_EXPORT(gzUInt32, SampleHeight, CTX_EXPORT);
81 GZ_PROPERTY_EXPORT(gzUInt32, SampleDepth, CTX_EXPORT);
82
83 GZ_PROPERTY_EXPORT(gzUInt32, SampleWidthStride, CTX_EXPORT); // Stride of each element in window
84 GZ_PROPERTY_EXPORT(gzUInt32, SampleHeightStride,CTX_EXPORT);
85 GZ_PROPERTY_EXPORT(gzUInt32, SampleDepthStride, CTX_EXPORT);
86
87 GZ_PROPERTY_EXPORT(gzUInt32, SampleWidthStep, CTX_EXPORT); // How large steps between each window
88 GZ_PROPERTY_EXPORT(gzUInt32, SampleHeightStep, CTX_EXPORT);
89 GZ_PROPERTY_EXPORT(gzUInt32, SampleDepthStep, CTX_EXPORT);
90
91 // Augment data with flipped X
92 GZ_PROPERTY_EXPORT(gzBool, FlipX, CTX_EXPORT);
93
94 // Augment data with flipped Y
95 GZ_PROPERTY_EXPORT(gzBool, FlipY, CTX_EXPORT);
96
97 // Augment data with flipped X with Y - requires squared image
98 GZ_PROPERTY_EXPORT(gzBool, FlipXY, CTX_EXPORT);
99
100 // Augment data with rotations
101 GZ_PROPERTY_EXPORT(gzUInt32, Rotations, CTX_EXPORT);
102
103 // Augment data with rotations, start angle in degrees
104 GZ_PROPERTY_EXPORT(gzFloat, StartAngle, CTX_EXPORT);
105
106 // Augment data with rotations, delta angle in degrees
107 GZ_PROPERTY_EXPORT(gzFloat, DeltaAngle, CTX_EXPORT);
108
109 // Augment data with scaling, One octave is divied Interval times
110 GZ_PROPERTY_EXPORT(gzUInt32, Intervall, CTX_EXPORT);
111
112 // Augment data with scaling
113 GZ_PROPERTY_EXPORT(gzUInt32, ScaleSteps, CTX_EXPORT);
114
115 // Number of resenved samples that will not be augmented
116 GZ_PROPERTY_EXPORT(gzUInt32, ReservedSamples, CTX_EXPORT);
117
118 GZ_PROPERTY_EXPORT(gzBool, UseResultArray, CTX_EXPORT);
119
120
121
122private:
123
124 // Utility functions for internal access
125
126
127 gzDynamicArray<gzArray<gzFloat>> m_results;
128};
129
130GZ_DECLARE_REFPTR(ctxIOImageSampler);
131
132// ----------------------------------------------------------------------------------------
133
135{
136public:
137
138 CTX_EXPORT ctxInputOutputViewer(gzUInt32 width, gzUInt32 height, gzUInt32 depth = 1);
139
140 CTX_EXPORT gzVoid showSample(gzUInt32 sample);
141
142 CTX_EXPORT gzVoid showAllSamples(gzBool waitForKey=TRUE,gzUInt32 maxCount = 0xffffffffU);
143
144 GZ_PROPERTY_EXPORT(ctxInputPtr, Input, CTX_EXPORT);
145
146 GZ_PROPERTY_EXPORT(ctxOutputPtr, Output, CTX_EXPORT);
147
148private:
149
150 gzUInt32 m_width;
151 gzUInt32 m_height;
152 gzUInt32 m_depth;
153};
154
155// ----------------------------------------------------------------------------------------
156
157template <class T> class ctxInputOutputAccelerator : public T
158{
159public:
160
161 // Bits in sample
162 virtual gzUInt32 getSampleSize()
163 {
164 return T::getSampleSize();
165 }
166
167 // Number of samples
168 virtual gzUInt32 getSamples()
169 {
170 return T::getSamples();
171 }
172
173 // Bits in result
174 virtual gzUInt32 getResultSize()
175 {
176 return T::getResultSize();
177 }
178
180 virtual gzUInt32 getResults()
181 {
182 return T::getResults();
183 }
184
186 {
187 gzUInt64 samples = getSamples();
188 gzUInt64 sampleSize = getSampleSize();
189
190 gzUInt64 size = samples * sampleSize;
191
192 if (size > LL(0x100000000)) // Approx 16 Gbyte
193 return FALSE;
194
195 gzUInt64 results = getResults();
196 gzUInt64 resultSize = getResultSize();
197
198 size = results * resultSize;
199
200 if (size > LL(0x100000000))
201 return FALSE;
202
203 if (samples != results)
204 return FALSE;
205
206 return TRUE;
207 }
208
209 gzBool preload(const gzString& name_prefix)
210 {
211 if (!hasValidSize())
212 return FALSE;
213
214 gzString fileName = gzString::formatString("%s_%d_%d_%d.dat", name_prefix, getSamples(), getSampleSize(), getResultSize());
215
216 gzSerializeAdapterPtr adapter = gzSerializeAdapter::getURLAdapter(fileName,GZ_SERIALIZE_INPUT,GZ_SERIALIZE_ADAPTER_FLAGS_DEFAULT|GZ_SERIALIZE_ADAPTER_NO_ERROR_MSG);
217
218 if (!adapter)
219 return FALSE;
220
221 m_input.setSize(getSamples());
222 m_output.setSize(getResults());
223
224 for (gzUInt32 i = 0; i < getSamples(); i++)
225 readAdapter(adapter, m_input[i],FALSE);
226
227 for (gzUInt32 i = 0; i < getResults(); i++)
228 readAdapter(adapter, m_output[i],FALSE);
229
230 return TRUE;
231 }
232
233 gzBool accelerate(const gzString &name_prefix, ctxContext *context=NULL)
234 {
235 if (m_input.getSize())
236 return TRUE;
237
238 if (!hasValidSize())
239 return FALSE;
240
241 if (!preload(name_prefix))
242 {
243 m_input.setSize(getSamples());
244
245 for (gzUInt32 i = 0; i < getSamples(); i++)
246 {
247 m_input[i].setSize(getSampleSize());
248
249 for (gzUInt32 j = 0; j < getSampleSize(); j++)
250 {
251 m_input[i][j] = T::getValue(context, j, i);
252 }
253 }
254
255 m_output.setSize(getResults());
256
257 for (gzUInt32 i = 0; i < getResults(); i++)
258 {
259 m_output[i].setSize(getResultSize());
260
261 for (gzUInt32 j = 0; j < getResultSize(); j++)
262 {
263 m_output[i][j] = T::getExpectedValue(context, j, i);
264 }
265 }
266
267 gzString fileName = gzString::formatString("%s_%d_%d_%d.dat", name_prefix, getSamples(), getSampleSize(), getResultSize());
268
269 gzSerializeAdapterPtr adapter = gzSerializeAdapter::getURLAdapter(fileName,GZ_SERIALIZE_OUTPUT);
270
271 if (adapter)
272 {
273 for (gzUInt32 i = 0; i < getSamples(); i++)
274 writeAdapter(adapter, m_input[i],FALSE);
275
276 for (gzUInt32 i = 0; i < getResults(); i++)
277 writeAdapter(adapter, m_output[i],FALSE);
278
279 return TRUE;
280 }
281
282 return FALSE;
283 }
284
285 return TRUE;
286 }
287
288 virtual gzFloat getValue(ctxContext* context, gzUInt32 index = 0, gzUInt32 sample = 0) override
289 {
290 if (m_input.getSize())
291 return m_input[sample][index];
292 else
293 return T::getValue(context, index, sample);
294 }
295
296 virtual gzFloat getExpectedValue(ctxContext* context, gzUInt32 index, gzUInt32 sample)
297 {
298 if (m_output.getSize())
299 return m_output[sample][index];
300 else
301 return T::getExpectedValue(context, index, sample);
302 }
303
304
305private:
306
307 gzArray<gzArray<gzFloat>> m_input;
308 gzArray<gzArray<gzFloat>> m_output;
309
310};
311
312#endif //__CTX_INPUT_OUTPUT_IMPL_H__
313
virtual gzArray< gzFloat > getExpectedValueArray(const gzString &name, const gzString &path, gzUInt32 id)
virtual gzFloat getExpectedValue_U(ctxContext *context, gzUInt32 index, gzUInt32 currentSample)
Bit expected value.
gzUInt32 getHeightDivisions()
virtual gzVoid onLoadImage(const gzString &name, const gzString &path, gzUInt32 id)
ctxIOImageSampler(ctxImageAccessInterfacePtr accessor=NULL)
gzFloat sampleImageInterpolated(gzUInt32 index, gzDouble x, gzDouble y, gzInt32 depth, ctxImageInterpolationType interpolation)
Explicit nearest/linear sampling through the existing image accessor.
virtual gzFloat getValue_U(ctxContext *context, gzUInt32 index, gzUInt32 currentSample)
gzUInt32 getWidthDivisions()
virtual gzUInt32 getResultSize()
gzUInt32 getDepthDivisions()
virtual gzUInt32 getResults()
virtual gzFloat getResultValue(gzUInt32 id, gzUInt32 index, gzUInt32 sample_cx, gzUInt32 sample_cy, gzUInt32 sample_cz)
virtual gzUInt32 getSampleSize()
gzFloat sampleImage(gzUInt32 index, gzFloat x, gzFloat y, gzInt32 depth)
virtual gzUInt32 getSamples()
virtual gzUInt32 getSampleSize()
gzBool preload(const gzString &name_prefix)
virtual gzUInt32 getResultSize()
virtual gzFloat getExpectedValue(ctxContext *context, gzUInt32 index, gzUInt32 sample)
virtual gzUInt32 getResults()
number of results
gzBool accelerate(const gzString &name_prefix, ctxContext *context=NULL)
virtual gzFloat getValue(ctxContext *context, gzUInt32 index=0, gzUInt32 sample=0) override
gzVoid showAllSamples(gzBool waitForKey=TRUE, gzUInt32 maxCount=0xffffffffU)
ctxInputOutputViewer(gzUInt32 width, gzUInt32 height, gzUInt32 depth=1)
gzVoid showSample(gzUInt32 sample)
#define CTX_EXPORT
Definition ctxBase.h:68
ctxLayer Input(gzUInt32 values)
ctxLayer Output(gzUInt32 values, ctxNeuronType type=CTX_NEURON_TYPE_DEFAULT)
ctxImageInterpolationType
Explicit interpolation for caller-owned image accessors.
ctxImageAccessInterface * ctxImageAccessInterfacePtr