series.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ///////////////////////////////////////////////////////////////////////
  2. // File: series.h
  3. // Description: Runs networks in series on the same input.
  4. // Author: Ray Smith
  5. // Created: Thu May 02 08:20:06 PST 2013
  6. //
  7. // (C) Copyright 2013, 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_SERIES_H_
  19. #define TESSERACT_LSTM_SERIES_H_
  20. #include "plumbing.h"
  21. namespace tesseract {
  22. // Runs two or more networks in series (layers) on the same input.
  23. class Series : public Plumbing {
  24. public:
  25. // ni_ and no_ will be set by AddToStack.
  26. explicit Series(const STRING& name);
  27. ~Series() override = default;
  28. // Returns the shape output from the network given an input shape (which may
  29. // be partially unknown ie zero).
  30. StaticShape OutputShape(const StaticShape& input_shape) const override;
  31. STRING spec() const override {
  32. STRING spec("[");
  33. for (int i = 0; i < stack_.size(); ++i)
  34. spec += stack_[i]->spec();
  35. spec += "]";
  36. return spec;
  37. }
  38. // Sets up the network for training. Initializes weights using weights of
  39. // scale `range` picked according to the random number generator `randomizer`.
  40. // Returns the number of weights initialized.
  41. int InitWeights(float range, TRand* randomizer) override;
  42. // Recursively searches the network for softmaxes with old_no outputs,
  43. // and remaps their outputs according to code_map. See network.h for details.
  44. int RemapOutputs(int old_no, const std::vector<int>& code_map) override;
  45. // Sets needs_to_backprop_ to needs_backprop and returns true if
  46. // needs_backprop || any weights in this network so the next layer forward
  47. // can be told to produce backprop for this layer if needed.
  48. bool SetupNeedsBackprop(bool needs_backprop) override;
  49. // Returns an integer reduction factor that the network applies to the
  50. // time sequence. Assumes that any 2-d is already eliminated. Used for
  51. // scaling bounding boxes of truth data.
  52. // WARNING: if GlobalMinimax is used to vary the scale, this will return
  53. // the last used scale factor. Call it before any forward, and it will return
  54. // the minimum scale factor of the paths through the GlobalMinimax.
  55. int XScaleFactor() const override;
  56. // Provides the (minimum) x scale factor to the network (of interest only to
  57. // input units) so they can determine how to scale bounding boxes.
  58. void CacheXScaleFactor(int factor) override;
  59. // Runs forward propagation of activations on the input line.
  60. // See Network for a detailed discussion of the arguments.
  61. void Forward(bool debug, const NetworkIO& input,
  62. const TransposedArray* input_transpose, NetworkScratch* scratch,
  63. NetworkIO* output) override;
  64. // Runs backward propagation of errors on the deltas line.
  65. // See Network for a detailed discussion of the arguments.
  66. bool Backward(bool debug, const NetworkIO& fwd_deltas,
  67. NetworkScratch* scratch, NetworkIO* back_deltas) override;
  68. // Splits the series after the given index, returning the two parts and
  69. // deletes itself. The first part, up to network with index last_start, goes
  70. // into start, and the rest goes into end.
  71. void SplitAt(int last_start, Series** start, Series** end);
  72. // Appends the elements of the src series to this, removing from src and
  73. // deleting it.
  74. void AppendSeries(Network* src);
  75. };
  76. } // namespace tesseract.
  77. #endif // TESSERACT_LSTM_SERIES_H_