tesseract  4.0.0-1-g2a2b
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 ()=default
 
STRING spec () const override
 
StaticShape InputShape () const override
 
StaticShape OutputShape (const StaticShape &input_shape) const override
 
bool Serialize (TFile *fp) const override
 
bool DeSerialize (TFile *fp) override
 
int XScaleFactor () const override
 
void CacheXScaleFactor (int factor) override
 
void Forward (bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
 
bool Backward (bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
 
- Public Member Functions inherited from tesseract::Network
 Network ()
 
 Network (NetworkType type, const STRING &name, int ni, int no)
 
virtual ~Network ()=default
 
NetworkType type () const
 
bool IsTraining () const
 
bool needs_to_backprop () const
 
int num_weights () const
 
int NumInputs () const
 
int NumOutputs () const
 
const STRINGname () const
 
bool TestFlag (NetworkFlags flag) const
 
virtual bool IsPlumbingType () const
 
virtual void SetEnableTraining (TrainingState state)
 
virtual void SetNetworkFlags (uint32_t flags)
 
virtual int InitWeights (float range, TRand *randomizer)
 
virtual int RemapOutputs (int old_no, const std::vector< int > &code_map)
 
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, float adam_beta, 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 NetworkCreateFromFile (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_t network_flags_
 
int32_t ni_
 
int32_t no_
 
int32_t num_weights_
 
STRING name_
 
ScrollViewforward_win_
 
ScrollViewbackward_win_
 
TRandrandomizer_
 
- 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) {}
const STRING & name() const
Definition: network.h:138

◆ 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 }
const STRING & name() const
Definition: network.h:138

◆ ~Input()

virtual tesseract::Input::~Input ( )
virtualdefault

Member Function Documentation

◆ Backward()

bool tesseract::Input::Backward ( bool  debug,
const NetworkIO fwd_deltas,
NetworkScratch scratch,
NetworkIO back_deltas 
)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 73 of file input.cpp.

75  {
76  tprintf("Input::Backward should not be called!!\n");
77  return false;
78 }
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37

◆ CacheXScaleFactor()

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

Reimplemented from tesseract::Network.

Definition at line 59 of file input.cpp.

59  {
60  cached_x_scale_ = factor;
61 }

◆ DeSerialize()

bool tesseract::Input::DeSerialize ( TFile fp)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 46 of file input.cpp.

46  {
47  return shape_.DeSerialize(fp);
48 }
bool DeSerialize(TFile *fp)
Definition: static_shape.h:64

◆ Forward()

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

Reimplemented from tesseract::Network.

Definition at line 65 of file input.cpp.

67  {
68  *output = input;
69 }

◆ InputShape()

StaticShape tesseract::Input::InputShape ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

Definition at line 44 of file input.h.

44 { return shape_; }

◆ OutputShape()

StaticShape tesseract::Input::OutputShape ( const StaticShape input_shape) const
inlineoverridevirtual

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 84 of file input.cpp.

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

◆ PreparePixInput()

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

Definition at line 112 of file input.cpp.

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

◆ Serialize()

bool tesseract::Input::Serialize ( TFile fp) const
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 41 of file input.cpp.

41  {
42  return Network::Serialize(fp) && shape_.Serialize(fp);
43 }
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:151
bool Serialize(TFile *fp) const
Definition: static_shape.h:76

◆ spec()

STRING tesseract::Input::spec ( ) const
inlineoverridevirtual

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  }
STRING spec() const override
Definition: input.h:34
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
Definition: strngs.h:45

◆ XScaleFactor()

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

Reimplemented from tesseract::Network.

Definition at line 53 of file input.cpp.

53  {
54  return 1;
55 }

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