tesseract  4.00.00dev
tesseract::Input Class Reference

#include <input.h>

Inheritance diagram for tesseract::Input:
tesseract::Network

Public Member Functions

 Input (const STRING &name, int ni, int no)
 
 Input (const STRING &name, const StaticShape &shape)
 
virtual ~Input ()
 
virtual STRING spec () const
 
virtual StaticShape InputShape () const
 
virtual StaticShape OutputShape (const StaticShape &input_shape) const
 
virtual bool Serialize (TFile *fp) const
 
virtual bool DeSerialize (TFile *fp)
 
virtual int XScaleFactor () const
 
virtual void CacheXScaleFactor (int factor)
 
virtual void Forward (bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)
 
virtual bool Backward (bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)
 
- Public Member Functions inherited from tesseract::Network
 Network ()
 
 Network (NetworkType type, const STRING &name, int ni, int no)
 
virtual ~Network ()
 
NetworkType type () const
 
bool IsTraining () const
 
bool needs_to_backprop () const
 
int num_weights () const
 
int NumInputs () const
 
int NumOutputs () const
 
const STRING & name () const
 
bool TestFlag (NetworkFlags flag) const
 
virtual bool IsPlumbingType () const
 
virtual void SetEnableTraining (TrainingState state)
 
virtual void SetNetworkFlags (uinT32 flags)
 
virtual int InitWeights (float range, TRand *randomizer)
 
virtual void ConvertToInt ()
 
virtual void SetRandomizer (TRand *randomizer)
 
virtual bool SetupNeedsBackprop (bool needs_backprop)
 
virtual void DebugWeights ()
 
virtual void Update (float learning_rate, float momentum, int num_samples)
 
virtual void CountAlternators (const Network &other, double *same, double *changed) const
 
void DisplayForward (const NetworkIO &matrix)
 
void DisplayBackward (const NetworkIO &matrix)
 

Static Public Member Functions

static Pix * PrepareLSTMInputs (const ImageData &image_data, const Network *network, int min_width, TRand *randomizer, float *image_scale)
 
static void PreparePixInput (const StaticShape &shape, const Pix *pix, TRand *randomizer, NetworkIO *input)
 
- Static Public Member Functions inherited from tesseract::Network
static Network * CreateFromFile (TFile *fp)
 
static void ClearWindow (bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
 
static int DisplayImage (Pix *pix, ScrollView *window)
 

Additional Inherited Members

- Protected Member Functions inherited from tesseract::Network
double Random (double range)
 
- Protected Attributes inherited from tesseract::Network
NetworkType type_
 
TrainingState training_
 
bool needs_to_backprop_
 
inT32 network_flags_
 
inT32 ni_
 
inT32 no_
 
inT32 num_weights_
 
STRING name_
 
ScrollView * forward_win_
 
ScrollView * backward_win_
 
TRand * randomizer_
 
- Static Protected Attributes inherited from tesseract::Network
static char const *const kTypeNames [NT_COUNT]
 

Detailed Description

Definition at line 28 of file input.h.

Constructor & Destructor Documentation

◆ Input() [1/2]

tesseract::Input::Input ( const STRING &  name,
int  ni,
int  no 
)

Definition at line 31 of file input.cpp.

32  : Network(NT_INPUT, name, ni, no), cached_x_scale_(1) {}

◆ Input() [2/2]

tesseract::Input::Input ( const STRING &  name,
const StaticShape &  shape 
)

Definition at line 33 of file input.cpp.

34  : Network(NT_INPUT, name, shape.height(), shape.depth()),
35  shape_(shape),
36  cached_x_scale_(1) {
37  if (shape.height() == 1) ni_ = shape.depth();
38 }

◆ ~Input()

tesseract::Input::~Input ( )
virtual

Definition at line 40 of file input.cpp.

40  {
41 }

Member Function Documentation

◆ Backward()

bool tesseract::Input::Backward ( bool  debug,
const NetworkIO &  fwd_deltas,
NetworkScratch *  scratch,
NetworkIO *  back_deltas 
)
virtual

Reimplemented from tesseract::Network.

Definition at line 78 of file input.cpp.

80  {
81  tprintf("Input::Backward should not be called!!\n");
82  return false;
83 }
#define tprintf(...)
Definition: tprintf.h:31

◆ CacheXScaleFactor()

void tesseract::Input::CacheXScaleFactor ( int  factor)
virtual

Reimplemented from tesseract::Network.

Definition at line 64 of file input.cpp.

64  {
65  cached_x_scale_ = factor;
66 }

◆ DeSerialize()

bool tesseract::Input::DeSerialize ( TFile *  fp)
virtual

Reimplemented from tesseract::Network.

Definition at line 51 of file input.cpp.

51  {
52  return fp->FReadEndian(&shape_, sizeof(shape_), 1) == 1;
53 }

◆ Forward()

void tesseract::Input::Forward ( bool  debug,
const NetworkIO &  input,
const TransposedArray *  input_transpose,
NetworkScratch *  scratch,
NetworkIO *  output 
)
virtual

Reimplemented from tesseract::Network.

Definition at line 70 of file input.cpp.

72  {
73  *output = input;
74 }

◆ InputShape()

virtual StaticShape tesseract::Input::InputShape ( ) const
inlinevirtual

Reimplemented from tesseract::Network.

Definition at line 44 of file input.h.

44 { return shape_; }

◆ OutputShape()

virtual StaticShape tesseract::Input::OutputShape ( const StaticShape &  input_shape) const
inlinevirtual

Reimplemented from tesseract::Network.

Definition at line 47 of file input.h.

47  {
48  return shape_;
49  }

◆ PrepareLSTMInputs()

Pix * tesseract::Input::PrepareLSTMInputs ( const ImageData &  image_data,
const Network *  network,
int  min_width,
TRand *  randomizer,
float *  image_scale 
)
static

Definition at line 89 of file input.cpp.

91  {
92  // Note that NumInputs() is defined as input image height.
93  int target_height = network->NumInputs();
94  int width, height;
95  Pix* pix = image_data.PreScale(target_height, kMaxInputHeight, image_scale,
96  &width, &height, nullptr);
97  if (pix == nullptr) {
98  tprintf("Bad pix from ImageData!\n");
99  return nullptr;
100  }
101  if (width <= min_width || height < min_width) {
102  tprintf("Image too small to scale!! (%dx%d vs min width of %d)\n", width,
103  height, min_width);
104  pixDestroy(&pix);
105  return nullptr;
106  }
107  return pix;
108 }
const int kMaxInputHeight
Definition: input.cpp:29
#define tprintf(...)
Definition: tprintf.h:31

◆ PreparePixInput()

void tesseract::Input::PreparePixInput ( const StaticShape &  shape,
const Pix *  pix,
TRand *  randomizer,
NetworkIO *  input 
)
static

Definition at line 117 of file input.cpp.

118  {
119  bool color = shape.depth() == 3;
120  Pix* var_pix = const_cast<Pix*>(pix);
121  int depth = pixGetDepth(var_pix);
122  Pix* normed_pix = nullptr;
123  // On input to BaseAPI, an image is forced to be 1, 8 or 24 bit, without
124  // colormap, so we just have to deal with depth conversion here.
125  if (color) {
126  // Force RGB.
127  if (depth == 32)
128  normed_pix = pixClone(var_pix);
129  else
130  normed_pix = pixConvertTo32(var_pix);
131  } else {
132  // Convert non-8-bit images to 8 bit.
133  if (depth == 8)
134  normed_pix = pixClone(var_pix);
135  else
136  normed_pix = pixConvertTo8(var_pix, false);
137  }
138  int height = pixGetHeight(normed_pix);
139  int target_height = shape.height();
140  if (target_height == 1) target_height = shape.depth();
141  if (target_height == 0) target_height = height;
142  float im_factor = static_cast<float>(target_height) / height;
143  if (im_factor != 1.0f) {
144  // Get the scaled image.
145  Pix* scaled_pix = pixScale(normed_pix, im_factor, im_factor);
146  pixDestroy(&normed_pix);
147  normed_pix = scaled_pix;
148  }
149  input->FromPix(shape, normed_pix, randomizer);
150  pixDestroy(&normed_pix);
151 }

◆ Serialize()

bool tesseract::Input::Serialize ( TFile *  fp) const
virtual

Reimplemented from tesseract::Network.

Definition at line 44 of file input.cpp.

44  {
45  if (!Network::Serialize(fp)) return false;
46  if (fp->FWrite(&shape_, sizeof(shape_), 1) != 1) return false;
47  return true;
48 }
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:153

◆ spec()

virtual STRING tesseract::Input::spec ( ) const
inlinevirtual

Reimplemented from tesseract::Network.

Definition at line 34 of file input.h.

34  {
35  STRING spec;
36  spec.add_str_int("", shape_.batch());
37  spec.add_str_int(",", shape_.height());
38  spec.add_str_int(",", shape_.width());
39  spec.add_str_int(",", shape_.depth());
40  return spec;
41  }
void add_str_int(const char *str, int number)
Definition: strngs.cpp:381
Definition: strngs.h:45
virtual STRING spec() const
Definition: input.h:34

◆ XScaleFactor()

int tesseract::Input::XScaleFactor ( ) const
virtual

Reimplemented from tesseract::Network.

Definition at line 58 of file input.cpp.

58  {
59  return 1;
60 }

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