maxpool.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. ///////////////////////////////////////////////////////////////////////
  2. // File: maxpool.h
  3. // Description: Standard Max-Pooling layer.
  4. // Author: Ray Smith
  5. // Created: Tue Mar 18 16:28:18 PST 2014
  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_MAXPOOL_H_
  19. #define TESSERACT_LSTM_MAXPOOL_H_
  20. #include "reconfig.h"
  21. namespace tesseract {
  22. // Maxpooling reduction. Independently for each input, selects the location
  23. // in the rectangle that contains the max value.
  24. // Backprop propagates only to the position that was the max.
  25. class Maxpool : public Reconfig {
  26. public:
  27. Maxpool(const STRING& name, int ni, int x_scale, int y_scale);
  28. ~Maxpool() override = default;
  29. // Accessors.
  30. STRING spec() const override {
  31. STRING spec;
  32. spec.add_str_int("Mp", y_scale_);
  33. spec.add_str_int(",", x_scale_);
  34. return spec;
  35. }
  36. // Reads from the given file. Returns false in case of error.
  37. bool DeSerialize(TFile* fp) override;
  38. // Runs forward propagation of activations on the input line.
  39. // See Network for a detailed discussion of the arguments.
  40. void Forward(bool debug, const NetworkIO& input,
  41. const TransposedArray* input_transpose,
  42. NetworkScratch* scratch, NetworkIO* output) override;
  43. // Runs backward propagation of errors on the deltas line.
  44. // See Network for a detailed discussion of the arguments.
  45. bool Backward(bool debug, const NetworkIO& fwd_deltas,
  46. NetworkScratch* scratch,
  47. NetworkIO* back_deltas) override;
  48. private:
  49. // Memory of which input was the max.
  50. GENERIC_2D_ARRAY<int> maxes_;
  51. };
  52. } // namespace tesseract.
  53. #endif // TESSERACT_LSTM_MAXPOOL_H_