convolve.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ///////////////////////////////////////////////////////////////////////
  2. // File: convolve.h
  3. // Description: Convolutional layer that stacks the inputs over its rectangle
  4. // and pulls in random data to fill out-of-input inputs.
  5. // Output is therefore same size as its input, but deeper.
  6. // Author: Ray Smith
  7. //
  8. // (C) Copyright 2014, Google Inc.
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. // http://www.apache.org/licenses/LICENSE-2.0
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS,
  15. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. // See the License for the specific language governing permissions and
  17. // limitations under the License.
  18. ///////////////////////////////////////////////////////////////////////
  19. #ifndef TESSERACT_LSTM_CONVOLVE_H_
  20. #define TESSERACT_LSTM_CONVOLVE_H_
  21. #include "genericvector.h"
  22. #include "matrix.h"
  23. #include "network.h"
  24. namespace tesseract {
  25. // Makes each time-step deeper by stacking inputs over its rectangle. Does not
  26. // affect the size of its input. Achieves this by bringing in random values in
  27. // out-of-input areas.
  28. class Convolve : public Network {
  29. public:
  30. // The area of convolution is 2*half_x + 1 by 2*half_y + 1, forcing it to
  31. // always be odd, so the center is the current pixel.
  32. Convolve(const STRING& name, int ni, int half_x, int half_y);
  33. ~Convolve() override = default;
  34. STRING spec() const override {
  35. STRING spec;
  36. spec.add_str_int("C", half_x_ * 2 + 1);
  37. spec.add_str_int(",", half_y_ * 2 + 1);
  38. return spec;
  39. }
  40. // Writes to the given file. Returns false in case of error.
  41. bool Serialize(TFile* fp) const override;
  42. // Reads from the given file. Returns false in case of error.
  43. bool DeSerialize(TFile* fp) override;
  44. // Runs forward propagation of activations on the input line.
  45. // See Network for a detailed discussion of the arguments.
  46. void Forward(bool debug, const NetworkIO& input,
  47. const TransposedArray* input_transpose,
  48. NetworkScratch* scratch, NetworkIO* output) override;
  49. // Runs backward propagation of errors on the deltas line.
  50. // See Network for a detailed discussion of the arguments.
  51. bool Backward(bool debug, const NetworkIO& fwd_deltas,
  52. NetworkScratch* scratch,
  53. NetworkIO* back_deltas) override;
  54. private:
  55. void DebugWeights() override {
  56. tprintf("Must override Network::DebugWeights for type %d\n", type_);
  57. }
  58. protected:
  59. // Serialized data.
  60. int32_t half_x_;
  61. int32_t half_y_;
  62. };
  63. } // namespace tesseract.
  64. #endif // TESSERACT_LSTM_SUBSAMPLE_H_