All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::NeuralNet Class Reference

#include <neural_net.h>

Classes

struct  Node
 
struct  WeightedNode
 

Public Member Functions

 NeuralNet ()
 
virtual ~NeuralNet ()
 
template<typename Type >
bool FeedForward (const Type *inputs, Type *outputs)
 
template<typename Type >
bool GetNetOutput (const Type *inputs, int output_id, Type *output)
 
int in_cnt () const
 
int out_cnt () const
 

Static Public Member Functions

static NeuralNetFromFile (const string file_name)
 
static NeuralNetFromInputBuffer (InputFileBuffer *ib)
 

Protected Member Functions

void Init ()
 
void Clear ()
 
template<class ReadBuffType >
bool ReadBinary (ReadBuffType *input_buff)
 
bool SetConnection (int from, int to)
 
bool CreateFastNet ()
 
float * AllocWgt (int wgt_cnt)
 
template<typename Type >
bool FastFeedForward (const Type *inputs, Type *outputs)
 
template<typename Type >
bool FastGetNetOutput (const Type *inputs, int output_id, Type *output)
 

Protected Attributes

bool read_only_
 
int in_cnt_
 
int out_cnt_
 
int neuron_cnt_
 
int wts_cnt_
 
Neuronneurons_
 
int alloc_wgt_cnt_
 
vector< vector< float > * > wts_vec_
 
bool auto_encoder_
 
vector< float > inputs_max_
 
vector< float > inputs_min_
 
vector< float > inputs_mean_
 
vector< float > inputs_std_dev_
 
vector< Nodefast_nodes_
 

Static Protected Attributes

static const int kWgtChunkSize = 0x10000
 
static const unsigned int kNetSignature = 0xFEFEABD0
 

Detailed Description

Definition at line 22 of file neural_net.h.

Constructor & Destructor Documentation

tesseract::NeuralNet::NeuralNet ( )

Definition at line 15 of file neural_net.cpp.

15  {
16  Init();
17 }
tesseract::NeuralNet::~NeuralNet ( )
virtual

Definition at line 19 of file neural_net.cpp.

19  {
20  // clean up the wts chunks vector
21  for (int vec = 0; vec < static_cast<int>(wts_vec_.size()); vec++) {
22  delete wts_vec_[vec];
23  }
24  // clean up neurons
25  delete []neurons_;
26  // clean up nodes
27  for (int node_idx = 0; node_idx < neuron_cnt_; node_idx++) {
28  delete []fast_nodes_[node_idx].inputs;
29  }
30 
31 }
vector< Node > fast_nodes_
Definition: neural_net.h:96
vector< vector< float > * > wts_vec_
Definition: neural_net.h:83

Member Function Documentation

float * tesseract::NeuralNet::AllocWgt ( int  wgt_cnt)
protected

Definition at line 189 of file neural_net.cpp.

189  {
190  // see if need to allocate a new chunk of wts
191  if (wts_vec_.size() == 0 || (alloc_wgt_cnt_ + wgt_cnt) > kWgtChunkSize) {
192  // add the new chunck to the wts_chunks vector
193  wts_vec_.push_back(new vector<float> (kWgtChunkSize));
194  alloc_wgt_cnt_ = 0;
195  }
196  float *ret_ptr = &((*wts_vec_.back())[alloc_wgt_cnt_]);
197  // incr usage counts
198  alloc_wgt_cnt_ += wgt_cnt;
199  wts_cnt_ += wgt_cnt;
200  return ret_ptr;
201 }
static const int kWgtChunkSize
Definition: neural_net.h:76
vector< vector< float > * > wts_vec_
Definition: neural_net.h:83
void tesseract::NeuralNet::Clear ( )
inlineprotected

Definition at line 100 of file neural_net.h.

100  {
101  for (int node = 0; node < neuron_cnt_; node++) {
102  neurons_[node].Clear();
103  }
104  }
void Clear()
Definition: neuron.h:37
bool tesseract::NeuralNet::CreateFastNet ( )
protected

Definition at line 124 of file neural_net.cpp.

124  {
125  fast_nodes_.resize(neuron_cnt_);
126  // build the node structures
127  int wts_cnt = 0;
128  for (int node_idx = 0; node_idx < neuron_cnt_; node_idx++) {
129  Node *node = &fast_nodes_[node_idx];
130  if (neurons_[node_idx].node_type() == Neuron::Input) {
131  // Input neurons have no fan-in
132  node->fan_in_cnt = 0;
133  node->inputs = NULL;
134  // Input bias is the normalization offset computed from
135  // training input stats
136  if (fabs(inputs_max_[node_idx] - inputs_min_[node_idx]) <
137  kMinInputRange) {
138  // if the range approaches zero, the stdev is not defined,
139  // this indicates that this input does not change.
140  // Set the bias to zero
141  node->bias = 0.0f;
142  } else {
143  node->bias = inputs_min_[node_idx] + (inputs_mean_[node_idx] *
144  (inputs_max_[node_idx] - inputs_min_[node_idx]));
145  }
146  } else {
147  node->bias = neurons_[node_idx].bias();
148  node->fan_in_cnt = neurons_[node_idx].fan_in_cnt();
149  // allocate memory for fan-in nodes
150  node->inputs = new WeightedNode[node->fan_in_cnt];
151  if (node->inputs == NULL) {
152  return false;
153  }
154  for (int fan_in = 0; fan_in < node->fan_in_cnt; fan_in++) {
155  // identify fan-in neuron
156  const int id = neurons_[node_idx].fan_in(fan_in)->id();
157  // Feedback connections are not allowed and should never happen
158  if (id >= node_idx) {
159  return false;
160  }
161  // add the the fan-in neuron and its wgt
162  node->inputs[fan_in].input_node = &fast_nodes_[id];
163  float wgt_val = neurons_[node_idx].fan_in_wts(fan_in);
164  // for input neurons normalize the wgt by the input scaling
165  // values to save time during feedforward
166  if (neurons_[node_idx].fan_in(fan_in)->node_type() == Neuron::Input) {
167  // if the range approaches zero, the stdev is not defined,
168  // this indicates that this input does not change.
169  // Set the weight to zero
170  if (fabs(inputs_max_[id] - inputs_min_[id]) < kMinInputRange) {
171  wgt_val = 0.0f;
172  } else {
173  wgt_val /= ((inputs_max_[id] - inputs_min_[id]) *
174  inputs_std_dev_[id]);
175  }
176  }
177  node->inputs[fan_in].input_weight = wgt_val;
178  }
179  // incr wgt count to validate against at the end
180  wts_cnt += node->fan_in_cnt;
181  }
182  }
183  // sanity check
184  return wts_cnt_ == wts_cnt;
185 }
vector< float > inputs_min_
Definition: neural_net.h:89
float fan_in_wts(int idx) const
Definition: neuron.h:108
int id() const
Definition: neuron.h:99
vector< float > inputs_mean_
Definition: neural_net.h:91
vector< float > inputs_std_dev_
Definition: neural_net.h:93
Neuron * fan_in(int idx) const
Definition: neuron.h:105
float bias() const
Definition: neuron.h:114
vector< Node > fast_nodes_
Definition: neural_net.h:96
vector< float > inputs_max_
Definition: neural_net.h:87
int fan_in_cnt() const
Definition: neuron.h:102
#define NULL
Definition: host.h:144
template<typename Type >
template bool tesseract::NeuralNet::FastFeedForward ( const Type *  inputs,
Type *  outputs 
)
protected

Definition at line 52 of file neural_net.cpp.

53  {
54  int node_idx = 0;
55  Node *node = &fast_nodes_[0];
56  // feed inputs in and offset them by the pre-computed bias
57  for (node_idx = 0; node_idx < in_cnt_; node_idx++, node++) {
58  node->out = inputs[node_idx] - node->bias;
59  }
60  // compute nodes activations and outputs
61  for (;node_idx < neuron_cnt_; node_idx++, node++) {
62  double activation = -node->bias;
63  for (int fan_in_idx = 0; fan_in_idx < node->fan_in_cnt; fan_in_idx++) {
64  activation += (node->inputs[fan_in_idx].input_weight *
65  node->inputs[fan_in_idx].input_node->out);
66  }
67  node->out = Neuron::Sigmoid(activation);
68  }
69  // copy the outputs to the output buffers
70  node = &fast_nodes_[neuron_cnt_ - out_cnt_];
71  for (node_idx = 0; node_idx < out_cnt_; node_idx++, node++) {
72  outputs[node_idx] = node->out;
73  }
74  return true;
75 }
vector< Node > fast_nodes_
Definition: neural_net.h:96
static float Sigmoid(float activation)
Definition: neuron.cpp:85
template<typename Type >
template bool tesseract::NeuralNet::FastGetNetOutput ( const Type *  inputs,
int  output_id,
Type *  output 
)
protected

Definition at line 231 of file neural_net.cpp.

233  {
234  // feed inputs in and offset them by the pre-computed bias
235  int node_idx = 0;
236  Node *node = &fast_nodes_[0];
237  for (node_idx = 0; node_idx < in_cnt_; node_idx++, node++) {
238  node->out = inputs[node_idx] - node->bias;
239  }
240 
241  // compute nodes' activations and outputs for hidden nodes if any
242  int hidden_node_cnt = neuron_cnt_ - out_cnt_;
243  for (;node_idx < hidden_node_cnt; node_idx++, node++) {
244  double activation = -node->bias;
245  for (int fan_in_idx = 0; fan_in_idx < node->fan_in_cnt; fan_in_idx++) {
246  activation += (node->inputs[fan_in_idx].input_weight *
247  node->inputs[fan_in_idx].input_node->out);
248  }
249  node->out = Neuron::Sigmoid(activation);
250  }
251 
252  // compute the output of the required output node
253  node += output_id;
254  double activation = -node->bias;
255  for (int fan_in_idx = 0; fan_in_idx < node->fan_in_cnt; fan_in_idx++) {
256  activation += (node->inputs[fan_in_idx].input_weight *
257  node->inputs[fan_in_idx].input_node->out);
258  }
259  (*output) = Neuron::Sigmoid(activation);
260  return true;
261 }
vector< Node > fast_nodes_
Definition: neural_net.h:96
static float Sigmoid(float activation)
Definition: neuron.cpp:85
template<typename Type >
template bool tesseract::NeuralNet::FeedForward ( const Type *  inputs,
Type *  outputs 
)

Definition at line 79 of file neural_net.cpp.

80  {
81  // call the fast version in case of readonly nets
82  if (read_only_) {
83  return FastFeedForward(inputs, outputs);
84  }
85  // clear all neurons
86  Clear();
87  // for auto encoders, apply no input normalization
88  if (auto_encoder_) {
89  for (int in = 0; in < in_cnt_; in++) {
90  neurons_[in].set_output(inputs[in]);
91  }
92  } else {
93  // Input normalization : subtract mean and divide by stddev
94  for (int in = 0; in < in_cnt_; in++) {
95  neurons_[in].set_output((inputs[in] - inputs_min_[in]) /
96  (inputs_max_[in] - inputs_min_[in]));
97  neurons_[in].set_output((neurons_[in].output() - inputs_mean_[in]) /
98  inputs_std_dev_[in]);
99  }
100  }
101  // compute the net outputs: follow a pull model each output pulls the
102  // outputs of its input nodes and so on
103  for (int out = neuron_cnt_ - out_cnt_; out < neuron_cnt_; out++) {
104  neurons_[out].FeedForward();
105  // copy the values to the output buffer
106  outputs[out] = neurons_[out].output();
107  }
108  return true;
109 }
vector< float > inputs_min_
Definition: neural_net.h:89
void set_output(float out_val)
Definition: neuron.h:96
bool FastFeedForward(const Type *inputs, Type *outputs)
Definition: neural_net.cpp:52
vector< float > inputs_mean_
Definition: neural_net.h:91
vector< float > inputs_std_dev_
Definition: neural_net.h:93
float output() const
Definition: neuron.h:93
vector< float > inputs_max_
Definition: neural_net.h:87
void FeedForward()
Definition: neuron.cpp:39
NeuralNet * tesseract::NeuralNet::FromFile ( const string  file_name)
static

Definition at line 204 of file neural_net.cpp.

204  {
205  // open the file
206  InputFileBuffer input_buff(file_name);
207  // create a new net object using input buffer
208  NeuralNet *net_obj = FromInputBuffer(&input_buff);
209  return net_obj;
210 }
static NeuralNet * FromInputBuffer(InputFileBuffer *ib)
Definition: neural_net.cpp:213
NeuralNet * tesseract::NeuralNet::FromInputBuffer ( InputFileBuffer ib)
static

Definition at line 213 of file neural_net.cpp.

213  {
214  // create a new net object
215  NeuralNet *net_obj = new NeuralNet();
216  if (net_obj == NULL) {
217  return NULL;
218  }
219  // load the net
220  if (!net_obj->ReadBinary(ib)) {
221  delete net_obj;
222  net_obj = NULL;
223  }
224  return net_obj;
225 }
#define NULL
Definition: host.h:144
template<typename Type >
template bool tesseract::NeuralNet::GetNetOutput ( const Type *  inputs,
int  output_id,
Type *  output 
)

Definition at line 265 of file neural_net.cpp.

267  {
268  // validate output id
269  if (output_id < 0 || output_id >= out_cnt_) {
270  return false;
271  }
272 
273  // call the fast version in case of readonly nets
274  if (read_only_) {
275  return FastGetNetOutput(inputs, output_id, output);
276  }
277 
278  // For the slow version, we'll just call FeedForward and return the
279  // appropriate output
280  vector<Type> outputs(out_cnt_);
281  if (!FeedForward(inputs, &outputs[0])) {
282  return false;
283  }
284  (*output) = outputs[output_id];
285 
286  return true;
287 }
bool FastGetNetOutput(const Type *inputs, int output_id, Type *output)
Definition: neural_net.cpp:231
bool FeedForward(const Type *inputs, Type *outputs)
Definition: neural_net.cpp:79
int tesseract::NeuralNet::in_cnt ( ) const
inline

Definition at line 40 of file neural_net.h.

40 { return in_cnt_; }
void tesseract::NeuralNet::Init ( )
protected

Definition at line 34 of file neural_net.cpp.

34  {
35  read_only_ = true;
36  auto_encoder_ = false;
37  alloc_wgt_cnt_ = 0;
38  wts_cnt_ = 0;
39  neuron_cnt_ = 0;
40  in_cnt_ = 0;
41  out_cnt_ = 0;
42  wts_vec_.clear();
43  neurons_ = NULL;
44  inputs_mean_.clear();
45  inputs_std_dev_.clear();
46  inputs_min_.clear();
47  inputs_max_.clear();
48 }
vector< float > inputs_min_
Definition: neural_net.h:89
vector< float > inputs_mean_
Definition: neural_net.h:91
vector< float > inputs_std_dev_
Definition: neural_net.h:93
vector< float > inputs_max_
Definition: neural_net.h:87
#define NULL
Definition: host.h:144
vector< vector< float > * > wts_vec_
Definition: neural_net.h:83
int tesseract::NeuralNet::out_cnt ( ) const
inline

Definition at line 41 of file neural_net.h.

41 { return out_cnt_; }
template<class ReadBuffType >
template bool tesseract::NeuralNet::ReadBinary ( ReadBuffType *  input_buff)
inlineprotected

Definition at line 106 of file neural_net.h.

106  {
107  // Init vars
108  Init();
109  // is this an autoencoder
110  unsigned int read_val;
111  unsigned int auto_encode;
112  // read and verify signature
113  if (input_buff->Read(&read_val, sizeof(read_val)) != sizeof(read_val)) {
114  return false;
115  }
116  if (read_val != kNetSignature) {
117  return false;
118  }
119  if (input_buff->Read(&auto_encode, sizeof(auto_encode)) !=
120  sizeof(auto_encode)) {
121  return false;
122  }
123  auto_encoder_ = auto_encode;
124  // read and validate total # of nodes
125  if (input_buff->Read(&read_val, sizeof(read_val)) != sizeof(read_val)) {
126  return false;
127  }
128  neuron_cnt_ = read_val;
129  if (neuron_cnt_ <= 0) {
130  return false;
131  }
132  // set the size of the neurons vector
133  neurons_ = new Neuron[neuron_cnt_];
134  if (neurons_ == NULL) {
135  return false;
136  }
137  // read & validate inputs
138  if (input_buff->Read(&read_val, sizeof(read_val)) != sizeof(read_val)) {
139  return false;
140  }
141  in_cnt_ = read_val;
142  if (in_cnt_ <= 0) {
143  return false;
144  }
145  // read outputs
146  if (input_buff->Read(&read_val, sizeof(read_val)) != sizeof(read_val)) {
147  return false;
148  }
149  out_cnt_ = read_val;
150  if (out_cnt_ <= 0) {
151  return false;
152  }
153  // set neuron ids and types
154  for (int idx = 0; idx < neuron_cnt_; idx++) {
155  neurons_[idx].set_id(idx);
156  // input type
157  if (idx < in_cnt_) {
159  } else if (idx >= (neuron_cnt_ - out_cnt_)) {
161  } else {
163  }
164  }
165  // read the connections
166  for (int node_idx = 0; node_idx < neuron_cnt_; node_idx++) {
167  // read fanout
168  if (input_buff->Read(&read_val, sizeof(read_val)) != sizeof(read_val)) {
169  return false;
170  }
171  // read the neuron's info
172  int fan_out_cnt = read_val;
173  for (int fan_out_idx = 0; fan_out_idx < fan_out_cnt; fan_out_idx++) {
174  // read the neuron id
175  if (input_buff->Read(&read_val, sizeof(read_val)) != sizeof(read_val)) {
176  return false;
177  }
178  // create the connection
179  if (!SetConnection(node_idx, read_val)) {
180  return false;
181  }
182  }
183  }
184  // read all the neurons' fan-in connections
185  for (int node_idx = 0; node_idx < neuron_cnt_; node_idx++) {
186  // read
187  if (!neurons_[node_idx].ReadBinary(input_buff)) {
188  return false;
189  }
190  }
191  // size input stats vector to expected input size
192  inputs_mean_.resize(in_cnt_);
193  inputs_std_dev_.resize(in_cnt_);
194  inputs_min_.resize(in_cnt_);
195  inputs_max_.resize(in_cnt_);
196  // read stats
197  if (input_buff->Read(&(inputs_mean_.front()),
198  sizeof(inputs_mean_[0]) * in_cnt_) !=
199  sizeof(inputs_mean_[0]) * in_cnt_) {
200  return false;
201  }
202  if (input_buff->Read(&(inputs_std_dev_.front()),
203  sizeof(inputs_std_dev_[0]) * in_cnt_) !=
204  sizeof(inputs_std_dev_[0]) * in_cnt_) {
205  return false;
206  }
207  if (input_buff->Read(&(inputs_min_.front()),
208  sizeof(inputs_min_[0]) * in_cnt_) !=
209  sizeof(inputs_min_[0]) * in_cnt_) {
210  return false;
211  }
212  if (input_buff->Read(&(inputs_max_.front()),
213  sizeof(inputs_max_[0]) * in_cnt_) !=
214  sizeof(inputs_max_[0]) * in_cnt_) {
215  return false;
216  }
217  // create a readonly version for fast feedforward
218  if (read_only_) {
219  return CreateFastNet();
220  }
221  return true;
222  }
vector< float > inputs_min_
Definition: neural_net.h:89
bool ReadBinary(ReadBuffType *input_buff)
Definition: neural_net.h:106
vector< float > inputs_mean_
Definition: neural_net.h:91
vector< float > inputs_std_dev_
Definition: neural_net.h:93
static const unsigned int kNetSignature
Definition: neural_net.h:79
vector< float > inputs_max_
Definition: neural_net.h:87
void set_id(int id)
Definition: neuron.h:111
void set_node_type(NeuronTypes type)
Definition: neuron.cpp:62
#define NULL
Definition: host.h:144
bool SetConnection(int from, int to)
Definition: neural_net.cpp:112
bool tesseract::NeuralNet::SetConnection ( int  from,
int  to 
)
protected

Definition at line 112 of file neural_net.cpp.

112  {
113  // allocate the wgt
114  float *wts = AllocWgt(1);
115  if (wts == NULL) {
116  return false;
117  }
118  // register the connection
119  neurons_[to].AddFromConnection(neurons_ + from, wts, 1);
120  return true;
121 }
void AddFromConnection(Neuron *neuron_vec, float *wts_offset, int from_cnt)
Definition: neuron.cpp:74
float * AllocWgt(int wgt_cnt)
Definition: neural_net.cpp:189
#define NULL
Definition: host.h:144

Member Data Documentation

int tesseract::NeuralNet::alloc_wgt_cnt_
protected

Definition at line 81 of file neural_net.h.

bool tesseract::NeuralNet::auto_encoder_
protected

Definition at line 85 of file neural_net.h.

vector<Node> tesseract::NeuralNet::fast_nodes_
protected

Definition at line 96 of file neural_net.h.

int tesseract::NeuralNet::in_cnt_
protected

Definition at line 63 of file neural_net.h.

vector<float> tesseract::NeuralNet::inputs_max_
protected

Definition at line 87 of file neural_net.h.

vector<float> tesseract::NeuralNet::inputs_mean_
protected

Definition at line 91 of file neural_net.h.

vector<float> tesseract::NeuralNet::inputs_min_
protected

Definition at line 89 of file neural_net.h.

vector<float> tesseract::NeuralNet::inputs_std_dev_
protected

Definition at line 93 of file neural_net.h.

const unsigned int tesseract::NeuralNet::kNetSignature = 0xFEFEABD0
staticprotected

Definition at line 79 of file neural_net.h.

const int tesseract::NeuralNet::kWgtChunkSize = 0x10000
staticprotected

Definition at line 76 of file neural_net.h.

int tesseract::NeuralNet::neuron_cnt_
protected

Definition at line 67 of file neural_net.h.

Neuron* tesseract::NeuralNet::neurons_
protected

Definition at line 71 of file neural_net.h.

int tesseract::NeuralNet::out_cnt_
protected

Definition at line 65 of file neural_net.h.

bool tesseract::NeuralNet::read_only_
protected

Definition at line 61 of file neural_net.h.

int tesseract::NeuralNet::wts_cnt_
protected

Definition at line 69 of file neural_net.h.

vector<vector<float> *> tesseract::NeuralNet::wts_vec_
protected

Definition at line 83 of file neural_net.h.


The documentation for this class was generated from the following files: