reconfig.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ///////////////////////////////////////////////////////////////////////
  2. // File: reconfig.h
  3. // Description: Network layer that reconfigures the scaling vs feature
  4. // depth.
  5. // Author: Ray Smith
  6. //
  7. // (C) Copyright 2014, Google Inc.
  8. // Licensed under the Apache License, Version 2.0 (the "License");
  9. // you may not use this file except in compliance with the License.
  10. // You may obtain a copy of the License at
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. ///////////////////////////////////////////////////////////////////////
  18. #ifndef TESSERACT_LSTM_RECONFIG_H_
  19. #define TESSERACT_LSTM_RECONFIG_H_
  20. #include "genericvector.h"
  21. #include "matrix.h"
  22. #include "network.h"
  23. namespace tesseract {
  24. // Reconfigures (Shrinks) the inputs by concatenating an x_scale by y_scale tile
  25. // of inputs together, producing a single, deeper output per tile.
  26. // Note that fractional parts are truncated for efficiency, so make sure the
  27. // input stride is a multiple of the y_scale factor!
  28. class Reconfig : public Network {
  29. public:
  30. Reconfig(const STRING& name, int ni, int x_scale, int y_scale);
  31. ~Reconfig() override = default;
  32. // Returns the shape output from the network given an input shape (which may
  33. // be partially unknown ie zero).
  34. StaticShape OutputShape(const StaticShape& input_shape) const override;
  35. STRING spec() const override {
  36. STRING spec;
  37. spec.add_str_int("S", y_scale_);
  38. spec.add_str_int(",", x_scale_);
  39. return spec;
  40. }
  41. // Returns an integer reduction factor that the network applies to the
  42. // time sequence. Assumes that any 2-d is already eliminated. Used for
  43. // scaling bounding boxes of truth data.
  44. // WARNING: if GlobalMinimax is used to vary the scale, this will return
  45. // the last used scale factor. Call it before any forward, and it will return
  46. // the minimum scale factor of the paths through the GlobalMinimax.
  47. int XScaleFactor() const override;
  48. // Writes to the given file. Returns false in case of error.
  49. bool Serialize(TFile* fp) const override;
  50. // Reads from the given file. Returns false in case of error.
  51. bool DeSerialize(TFile* fp) override;
  52. // Runs forward propagation of activations on the input line.
  53. // See Network for a detailed discussion of the arguments.
  54. void Forward(bool debug, const NetworkIO& input,
  55. const TransposedArray* input_transpose,
  56. NetworkScratch* scratch, NetworkIO* output) override;
  57. // Runs backward propagation of errors on the deltas line.
  58. // See Network for a detailed discussion of the arguments.
  59. bool Backward(bool debug, const NetworkIO& fwd_deltas,
  60. NetworkScratch* scratch,
  61. NetworkIO* back_deltas) override;
  62. private:
  63. void DebugWeights() override {
  64. tprintf("Must override Network::DebugWeights for type %d\n", type_);
  65. }
  66. protected:
  67. // Non-serialized data used to store parameters between forward and back.
  68. StrideMap back_map_;
  69. // Serialized data.
  70. int32_t x_scale_;
  71. int32_t y_scale_;
  72. };
  73. } // namespace tesseract.
  74. #endif // TESSERACT_LSTM_SUBSAMPLE_H_