tesseract v5.3.3.20231005
tesseract::Input Class Reference

#include <input.h>

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

Public Member Functions

TESS_API Input (const std::string &name, int ni, int no)
 
TESS_API Input (const std::string &name, const StaticShape &shape)
 
 ~Input () override=default
 
std::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 std::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
 
virtual StaticShape InputShape () const
 
virtual StaticShape OutputShape (const StaticShape &input_shape) const
 
const std::string & name () const
 
virtual std::string spec () 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 int XScaleFactor () const
 
virtual void CacheXScaleFactor (int factor)
 
virtual void DebugWeights ()=0
 
virtual bool Serialize (TFile *fp) const
 
virtual bool DeSerialize (TFile *fp)=0
 
virtual void Update (float learning_rate, float momentum, float adam_beta, int num_samples)
 
virtual void CountAlternators (const Network &other, TFloat *same, TFloat *changed) const
 
virtual void Forward (bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)=0
 
virtual bool Backward (bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)=0
 
void DisplayForward (const NetworkIO &matrix)
 
void DisplayBackward (const NetworkIO &matrix)
 

Static Public Member Functions

static Image PrepareLSTMInputs (const ImageData &image_data, const Network *network, int min_width, TRand *randomizer, float *image_scale)
 
static void PreparePixInput (const StaticShape &shape, const Image 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 (Image pix, ScrollView *window)
 

Additional Inherited Members

- Protected Member Functions inherited from tesseract::Network
TFloat Random (TFloat 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_
 
std::string name_
 
ScrollViewforward_win_
 
ScrollViewbackward_win_
 
TRandrandomizer_
 

Detailed Description

Definition at line 27 of file input.h.

Constructor & Destructor Documentation

◆ Input() [1/2]

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

Definition at line 30 of file input.cpp.

31 : Network(NT_INPUT, name, ni, no), cached_x_scale_(1) {}
@ NT_INPUT
Definition: network.h:43
const std::string & name() const
Definition: network.h:140

◆ Input() [2/2]

tesseract::Input::Input ( const std::string &  name,
const StaticShape shape 
)

Definition at line 32 of file input.cpp.

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

◆ ~Input()

tesseract::Input::~Input ( )
overridedefault

Member Function Documentation

◆ Backward()

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

Implements tesseract::Network.

Definition at line 71 of file input.cpp.

72 {
73 tprintf("Input::Backward should not be called!!\n");
74 return false;
75}
void tprintf(const char *format,...)
Definition: tprintf.cpp:41

◆ CacheXScaleFactor()

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

Reimplemented from tesseract::Network.

Definition at line 58 of file input.cpp.

58 {
59 cached_x_scale_ = factor;
60}

◆ DeSerialize()

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

Implements tesseract::Network.

Definition at line 45 of file input.cpp.

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

◆ Forward()

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

Implements tesseract::Network.

Definition at line 64 of file input.cpp.

65 {
66 *output = input;
67}

◆ InputShape()

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

Reimplemented from tesseract::Network.

Definition at line 43 of file input.h.

43 {
44 return shape_;
45 }

◆ OutputShape()

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

Reimplemented from tesseract::Network.

Definition at line 48 of file input.h.

49 {
50 return shape_;
51 }

◆ PrepareLSTMInputs()

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

Definition at line 81 of file input.cpp.

82 {
83 // Note that NumInputs() is defined as input image height.
84 int target_height = network->NumInputs();
85 int width, height;
86 Image pix =
87 image_data.PreScale(target_height, kMaxInputHeight, image_scale, &width, &height, nullptr);
88 if (pix == nullptr) {
89 tprintf("Bad pix from ImageData!\n");
90 return nullptr;
91 }
92 if (width < min_width || height < min_width) {
93 tprintf("Image too small to scale!! (%dx%d vs min width of %d)\n", width, height, min_width);
94 pix.destroy();
95 return nullptr;
96 }
97 return pix;
98}
const int kMaxInputHeight
Definition: input.cpp:28

◆ PreparePixInput()

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

Definition at line 107 of file input.cpp.

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

◆ Serialize()

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

Reimplemented from tesseract::Network.

Definition at line 40 of file input.cpp.

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

◆ spec()

std::string tesseract::Input::spec ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

Definition at line 35 of file input.h.

35 {
36 return std::to_string(shape_.batch()) + "," +
37 std::to_string(shape_.height()) + "," +
38 std::to_string(shape_.width()) + "," +
39 std::to_string(shape_.depth());
40 }

◆ XScaleFactor()

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

Reimplemented from tesseract::Network.

Definition at line 52 of file input.cpp.

52 {
53 return 1;
54}

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