Commit 52689626 for tesseract

commit 52689626219f10373f3617e07fdd659540e17c72
Author: Stefan Weil <sw@weilnetz.de>
Date:   Wed Nov 18 14:39:46 2026 +0100

    lstm: add a placeholder for a dropout layer (#4704)

    Allow loading and recognizing with models which were trained with
    dropout, while this version of Tesseract still cannot train with it.
    With inverted dropout the layer passes its input through unchanged
    during recognition.

    The layer stores the dropout rate and dimension in the model file;
    invalid values are rejected when a model is read. Training is
    rejected with a specific message if the network specification or a
    checkpoint contains a dropout layer. Models are now evaluated in
    inference mode so lstmeval and the evaluations during a training give
    the correct results for such models; the results for existing networks
    do not change.

    Also disable the alloc-dealloc-mismatch ASan check in the ubuntu-22.04
    clang unittest job, where the packaged libc++ misreports it for the
    new tests' exceptions (llvm/llvm-project#59432).

    Assisted-by: OpenCode / Claude Fable 5.2 (Anthropic)
    Assisted-by: OpenCode / qwen3.8-27b-thinking (Alibaba Cloud)
    Signed-off-by: Stefan Weil <sw@weilnetz.de>

diff --git a/.github/workflows/unittest.yml b/.github/workflows/unittest.yml
index c2972c2e..d7037e81 100644
--- a/.github/workflows/unittest.yml
+++ b/.github/workflows/unittest.yml
@@ -24,7 +24,12 @@ jobs:
       matrix:
         config:
           - { name: ubuntu-24.04-gcc-unittest, os: ubuntu-24.04, cxx: g++, cxxflags: '-g -O2 -fsanitize=address,undefined' }
-          - { name: ubuntu-22.04-clang-unittest, os: ubuntu-22.04, cxx: clang++, cxxflags: '-g -O2 -fsanitize=address,undefined -stdlib=libc++' }
+          # Ubuntu 22.04's packaged libc++ is built with -Bsymbolic-functions,
+          # so its std::logic_error allocation escapes AddressSanitizer's
+          # interposition and any thrown or caught exception misreports
+          # alloc-dealloc-mismatch (llvm/llvm-project#59432). Disable that one
+          # check for the clang job; it is a toolchain false positive.
+          - { name: ubuntu-22.04-clang-unittest, os: ubuntu-22.04, cxx: clang++, cxxflags: '-g -O2 -fsanitize=address,undefined -stdlib=libc++', asan_options: 'alloc_dealloc_mismatch=0' }
     steps:
     - uses: actions/checkout@v7
       with:
@@ -82,6 +87,8 @@ jobs:
            tesseract test/testing/devatest.png - -l hin+eng  --tessdata-dir ../tessdata

     - name: Make and run Unit Tests
+      env:
+        ASAN_OPTIONS: ${{ matrix.config.asan_options }}
       run: |
            make check

diff --git a/Makefile.am b/Makefile.am
index d4122a55..391d2e75 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -505,6 +505,7 @@ endif
 # Rules for src/lstm.

 noinst_HEADERS += src/lstm/convolve.h
+noinst_HEADERS += src/lstm/dropout.h
 noinst_HEADERS += src/lstm/fullyconnected.h
 noinst_HEADERS += src/lstm/functions.h
 noinst_HEADERS += src/lstm/input.h
@@ -525,6 +526,7 @@ noinst_HEADERS += src/lstm/stridemap.h
 noinst_HEADERS += src/lstm/weightmatrix.h

 libtesseract_la_SOURCES += src/lstm/convolve.cpp
+libtesseract_la_SOURCES += src/lstm/dropout.cpp
 libtesseract_la_SOURCES += src/lstm/fullyconnected.cpp
 libtesseract_la_SOURCES += src/lstm/functions.cpp
 libtesseract_la_SOURCES += src/lstm/input.cpp
@@ -1160,6 +1162,7 @@ check_PROGRAMS += colpartition_test
 if ENABLE_TRAINING
 check_PROGRAMS += commandlineflags_test
 check_PROGRAMS += dawg_test
+check_PROGRAMS += dropout_test
 endif # ENABLE_TRAINING
 check_PROGRAMS += denorm_test
 if !DISABLED_LEGACY_ENGINE
@@ -1322,6 +1325,10 @@ dawg_test_SOURCES = unittest/dawg_test.cc
 dawg_test_CPPFLAGS = $(unittest_CPPFLAGS)
 dawg_test_LDADD = $(TRAINING_LIBS)

+dropout_test_SOURCES = unittest/dropout_test.cc
+dropout_test_CPPFLAGS = $(unittest_CPPFLAGS)
+dropout_test_LDADD = $(TRAINING_LIBS)
+
 denorm_test_SOURCES = unittest/denorm_test.cc
 denorm_test_CPPFLAGS = $(unittest_CPPFLAGS)
 denorm_test_LDADD = $(TESS_LIBS)
diff --git a/cmake/SourceLists.cmake b/cmake/SourceLists.cmake
index 78fc1785..ba745924 100644
--- a/cmake/SourceLists.cmake
+++ b/cmake/SourceLists.cmake
@@ -191,6 +191,7 @@ set(TESSERACT_SRC_DICT
 # LSTM module sources
 set(TESSERACT_SRC_LSTM
     src/lstm/convolve.cpp
+    src/lstm/dropout.cpp
     src/lstm/fullyconnected.cpp
     src/lstm/functions.cpp
     src/lstm/input.cpp
diff --git a/src/lstm/dropout.cpp b/src/lstm/dropout.cpp
new file mode 100644
index 00000000..abe79cd9
--- /dev/null
+++ b/src/lstm/dropout.cpp
@@ -0,0 +1,95 @@
+///////////////////////////////////////////////////////////////////////
+// File:        dropout.cpp
+// Description: Placeholder for a dropout layer.
+// Author:      Stefan Weil
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+///////////////////////////////////////////////////////////////////////
+
+#ifdef HAVE_CONFIG_H
+#  include "config_auto.h"
+#endif
+
+#include "dropout.h"
+#include "networkio.h"
+#include "serialis.h"
+#include "tprintf.h"
+
+#include <cstdint>   // for uint32_t
+#include <stdexcept> // for std::invalid_argument
+
+namespace tesseract {
+
+Dropout::Dropout(const std::string &name, int ni, float dropout_rate, unsigned dropout_dim)
+    : Network(NT_DROPOUT, name, ni, ni), dropout_rate_(dropout_rate), dropout_dim_(dropout_dim) {
+  if (!IsValid(dropout_rate, dropout_dim)) {
+    throw std::invalid_argument(
+        "Invalid dropout layer. The rate must be in [0, 1), the dimension 0, 1 or 2.");
+  }
+}
+
+void Dropout::DebugWeights() {
+  tprintf("Dropout layer '%s': rate=%g, dimension=%u\n", name_.c_str(),
+          static_cast<double>(dropout_rate_), dropout_dim_);
+}
+
+// Writes to the given file. Returns false in case of error.
+bool Dropout::Serialize(TFile *fp) const {
+  uint32_t dropout_dim = dropout_dim_;
+  return Network::Serialize(fp) && fp->Serialize(&dropout_rate_) && fp->Serialize(&dropout_dim);
+}
+
+// Reads from the given file. Returns false in case of error.
+bool Dropout::DeSerialize(TFile *fp) {
+  float dropout_rate;
+  uint32_t dropout_dim;
+  if (!fp->DeSerialize(&dropout_rate) || !fp->DeSerialize(&dropout_dim)) {
+    return false;
+  }
+  if (!IsValid(dropout_rate, dropout_dim)) {
+    // Reject a corrupt model.
+    return false;
+  }
+  dropout_rate_ = dropout_rate;
+  dropout_dim_ = dropout_dim;
+  no_ = ni_;
+  return true;
+}
+
+// Runs forward propagation of activations on the input line.
+// See Network for a detailed discussion of the arguments.
+void Dropout::Forward([[maybe_unused]] bool debug, const NetworkIO &input,
+                      [[maybe_unused]] const TransposedArray *input_transpose,
+                      [[maybe_unused]] NetworkScratch *scratch, NetworkIO *output) {
+  // The output has the same shape as the input (ni_ == no_).
+  output->Resize(input, no_);
+  // Pass the input through unchanged. CopyTimeStepFrom is mode-aware, so
+  // this also works for a network which was converted to integer mode.
+  const int width = input.Width();
+  for (int t = 0; t < width; ++t) {
+    output->CopyTimeStepFrom(t, input, t);
+  }
+}
+
+// Runs backward propagation of errors on the deltas line.
+// See Network for a detailed discussion of the arguments.
+bool Dropout::Backward([[maybe_unused]] bool debug, const NetworkIO &fwd_deltas,
+                       [[maybe_unused]] NetworkScratch *scratch, NetworkIO *back_deltas) {
+  back_deltas->Resize(fwd_deltas, ni_);
+  // Pass the deltas through unchanged.
+  const int width = fwd_deltas.Width();
+  for (int t = 0; t < width; ++t) {
+    back_deltas->CopyTimeStepFrom(t, fwd_deltas, t);
+  }
+  return needs_to_backprop_;
+}
+
+} // namespace tesseract.
diff --git a/src/lstm/dropout.h b/src/lstm/dropout.h
new file mode 100644
index 00000000..194b7736
--- /dev/null
+++ b/src/lstm/dropout.h
@@ -0,0 +1,87 @@
+///////////////////////////////////////////////////////////////////////
+// File:        dropout.h
+// Description: Placeholder for a dropout layer.
+// Author:      Stefan Weil
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+///////////////////////////////////////////////////////////////////////
+
+#ifndef TESSERACT_LSTM_DROPOUT_H_
+#define TESSERACT_LSTM_DROPOUT_H_
+
+#include "network.h"
+
+#include <cstdio> // for std::snprintf
+#include <string>
+
+namespace tesseract {
+
+// Placeholder for a dropout layer.
+//
+// A dropout layer only changes its input while a network is trained. When a
+// model is used for recognition, the layer passes its input through
+// unchanged. This class implements only that pass-through, so models which
+// contain a dropout layer can be loaded and used for recognition. This
+// version of Tesseract cannot train with dropout.
+//
+// The layer stores two values in a model file: the dropout rate (float,
+// 0 <= rate < 1) and the dropout dimension (32 bit unsigned integer,
+// 0 = single elements, 1 = whole timesteps, 2 = whole features).
+class Dropout : public Network {
+public:
+  TESS_API
+  Dropout(const std::string &name, int ni, float dropout_rate, unsigned dropout_dim = 0);
+  ~Dropout() override = default;
+
+  // Accessors.
+  std::string spec() const override {
+    // %g gives a compact representation (eg "0.5") instead of the six-decimal
+    // padding of std::to_string (eg "0.500000").
+    char buf[32];
+    std::snprintf(buf, sizeof(buf), "Do%g", static_cast<double>(dropout_rate_));
+    std::string s = buf;
+    if (dropout_dim_ != 0) {
+      s += "," + std::to_string(dropout_dim_);
+    }
+    return s;
+  }
+
+  // Writes to the given file. Returns false in case of error.
+  bool Serialize(TFile *fp) const override;
+  // Reads from the given file. Returns false in case of error.
+  bool DeSerialize(TFile *fp) override;
+
+  // Runs forward propagation of activations on the input line.
+  // See Network for a detailed discussion of the arguments.
+  void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose,
+               NetworkScratch *scratch, NetworkIO *output) override;
+
+  // Runs backward propagation of errors on the deltas line.
+  // See Network for a detailed discussion of the arguments.
+  bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch,
+                NetworkIO *back_deltas) override;
+
+private:
+  void DebugWeights() override;
+
+  // Returns true if the rate and the dimension are valid.
+  static bool IsValid(float dropout_rate, unsigned dropout_dim) {
+    // The comparison is written so that it is false for NaN.
+    return dropout_rate >= 0 && dropout_rate < 1 && dropout_dim <= 2;
+  }
+
+  float dropout_rate_;
+  unsigned dropout_dim_; // 0=elementwise, 1=temporal, 2=feature/channel
+};
+
+} // namespace tesseract.
+
+#endif // TESSERACT_LSTM_DROPOUT_H_
diff --git a/src/lstm/network.cpp b/src/lstm/network.cpp
index 7a3b7973..2014cf0f 100644
--- a/src/lstm/network.cpp
+++ b/src/lstm/network.cpp
@@ -27,6 +27,7 @@
 // This base class needs to know about all its sub-classes because of the
 // factory deserializing method: CreateFromFile.
 #include "convolve.h"
+#include "dropout.h"
 #include "fullyconnected.h"
 #include "image.h"          // for Image
 #include "input.h"
@@ -71,7 +72,7 @@ static char const *const kTypeNames[NT_COUNT] = {
     "Relu",        "Linear",
     "Softmax",     "SoftmaxNoCTC",
     "LSTMSoftmax", "LSTMBinarySoftmax",
-    "TensorFlow",
+    "TensorFlow",  "Dropout",
 };

 Network::Network()
@@ -257,6 +258,9 @@ Network *Network::CreateFromFile(TFile *fp) {
     case NT_CONVOLVE:
       network = new Convolve(name, ni, 0, 0);
       break;
+    case NT_DROPOUT:
+      network = new Dropout(name, ni, 0.5f);
+      break;
     case NT_INPUT:
       network = new Input(name, ni, no);
       break;
diff --git a/src/lstm/network.h b/src/lstm/network.h
index 353b110c..cc8e1f92 100644
--- a/src/lstm/network.h
+++ b/src/lstm/network.h
@@ -73,6 +73,9 @@ enum NetworkType {
   NT_LSTM_SOFTMAX_ENCODED, // 1-d LSTM with built-in binary encoded softmax.
   // A TensorFlow graph encapsulated as a Tesseract network.
   NT_TENSORFLOW,
+  // A dropout layer (see dropout.h). New types must be added here at the end
+  // because old model files store the type as a number.
+  NT_DROPOUT,

   NT_COUNT // Array size.
 };
diff --git a/src/training/common/networkbuilder.cpp b/src/training/common/networkbuilder.cpp
index 5a0d9171..e502077a 100644
--- a/src/training/common/networkbuilder.cpp
+++ b/src/training/common/networkbuilder.cpp
@@ -112,6 +112,12 @@ Network *NetworkBuilder::BuildFromString(const StaticShape &input_shape, const c
       return ParseFullyConnected(input_shape, str);
     case 'O':
       return ParseOutput(input_shape, str);
+    case 'D':
+      // Models with a dropout layer can be used for recognition, but this
+      // version cannot train with dropout.
+      tprintf("Dropout (Do) is not supported for training by this version of Tesseract:%s\n",
+              *str);
+      break;
     default:
       tprintf("Invalid network spec:%s\n", *str);
   }
diff --git a/src/training/lstmtraining.cpp b/src/training/lstmtraining.cpp
index cd3bb2e3..c8964201 100644
--- a/src/training/lstmtraining.cpp
+++ b/src/training/lstmtraining.cpp
@@ -187,6 +187,18 @@ int main(int argc, char **argv) {
       trainer.set_perfect_delay(FLAGS_perfect_sample_delay);
     }
   }
+  // Models with a dropout layer can be used for recognition, but this version
+  // cannot train with dropout. Don't continue a training which was started
+  // with dropout by another version: it would silently train without it.
+  for (const auto &id : trainer.EnumerateLayers()) {
+    const tesseract::Network *layer = trainer.GetLayer(id);
+    if (layer != nullptr && layer->type() == tesseract::NT_DROPOUT) {
+      tprintf("Error: the network contains a dropout layer (%s), but this version of "
+              "Tesseract cannot train with dropout.\n",
+              id.c_str());
+      return EXIT_FAILURE;
+    }
+  }
   if (!trainer.LoadAllTrainingData(
           filenames,
           FLAGS_sequential_training ? tesseract::CS_SEQUENTIAL : tesseract::CS_ROUND_ROBIN,
diff --git a/src/training/unicharset/lstmtester.cpp b/src/training/unicharset/lstmtester.cpp
index 33a87a24..434e4140 100644
--- a/src/training/unicharset/lstmtester.cpp
+++ b/src/training/unicharset/lstmtester.cpp
@@ -86,6 +86,10 @@ std::string LSTMTester::RunEvalSync(int iteration,
   if (!model_mgr.GetComponent(TESSDATA_LSTM, &fp) || !trainer.DeSerialize(&model_mgr, &fp)) {
     return "Deserialize failed";
   }
+  // The evaluation must use the network like a recognizer does. Otherwise
+  // dropout layers would still drop activations and report errors which are
+  // too high.
+  trainer.DisableTrainingForEval();
   int eval_iteration = 0;
   double char_error = 0.0;
   double word_error = 0.0;
diff --git a/src/training/unicharset/lstmtrainer.h b/src/training/unicharset/lstmtrainer.h
index 25bf8990..eb4b9b37 100644
--- a/src/training/unicharset/lstmtrainer.h
+++ b/src/training/unicharset/lstmtrainer.h
@@ -221,6 +221,13 @@ public:
   // Reads from the given file. Returns false in case of error.
   bool DeSerialize(const TessdataManager *mgr, TFile *fp);

+  // Switches the network to inference mode for an evaluation.
+  // A trainer keeps all layers in training mode, but layers like dropout
+  // must not modify their input while a model is evaluated.
+  void DisableTrainingForEval() {
+    network_->SetEnableTraining(TS_TEMP_DISABLE);
+  }
+
   // De-serializes the saved best_trainer_ into sub_trainer_, and adjusts the
   // learning rates (by scaling reduction, or layer specific, according to
   // NF_LAYER_SPECIFIC_LR).
diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt
index f11cc0b8..d7ac0106 100644
--- a/unittest/CMakeLists.txt
+++ b/unittest/CMakeLists.txt
@@ -47,6 +47,7 @@ set(GMOCK_TESTS
 set(TRAINING_TESTS
     commandlineflags_test.cc
     dawg_test.cc
+    dropout_test.cc
     lstm_recode_test.cc
     lstm_squashed_test.cc
     lstm_test.cc
diff --git a/unittest/dropout_test.cc b/unittest/dropout_test.cc
new file mode 100644
index 00000000..ad9098b2
--- /dev/null
+++ b/unittest/dropout_test.cc
@@ -0,0 +1,255 @@
+///////////////////////////////////////////////////////////////////////
+// File:        dropout_test.cc
+// Description: Unit tests for the placeholder of the dropout layer:
+//              the pass-through in float and integer mode, the
+//              serialization, and the rejection of "Do" in a network
+//              specification.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+///////////////////////////////////////////////////////////////////////
+
+#include "include_gunit.h"
+
+#include "dropout.h"
+#include "network.h"
+#include "networkbuilder.h" // for NetworkBuilder (spec parsing)
+#include "networkio.h"
+#include "networkscratch.h"
+#include "serialis.h" // for TFile
+#include "stridemap.h"
+
+#include <algorithm> // for std::copy
+#include <cstdint>   // for uint32_t
+#include <stdexcept> // for std::invalid_argument
+#include <vector>
+
+namespace tesseract {
+namespace {
+
+// A NetworkIO with `width` timesteps and `ni` float features. Each element
+// gets a different value, so a wrong copy is detected.
+NetworkIO MakeFloatInput(int width, int ni) {
+  StrideMap stride_map;
+  stride_map.SetStride({{1, width}});
+  NetworkIO input;
+  input.ResizeToMap(false, stride_map, ni);
+  for (int t = 0; t < width; ++t) {
+    for (int i = 0; i < ni; ++i) {
+      input.f(t)[i] = 0.1f * (t + 1) - 0.05f * i;
+    }
+  }
+  return input;
+}
+
+// Serializes the layer and returns the data.
+std::vector<char> SerializeLayer(const Dropout &net) {
+  std::vector<char> data;
+  TFile fp;
+  fp.OpenWrite(&data);
+  EXPECT_TRUE(net.Serialize(&fp));
+  return data;
+}
+
+// Replaces the bytes at the given offset by the serialized value.
+template <typename T>
+void OverwriteValue(std::vector<char> &data, size_t offset, const T &value) {
+  std::vector<char> bytes;
+  TFile fp;
+  fp.OpenWrite(&bytes);
+  ASSERT_TRUE(fp.Serialize(&value));
+  ASSERT_LE(offset + bytes.size(), data.size());
+  std::copy(bytes.begin(), bytes.end(), data.begin() + offset);
+}
+
+// The rate must lie in [0, 1).
+TEST(DropoutLayerTest, RejectsOutOfRangeRate) {
+  EXPECT_THROW(Dropout("Dropout", 4, 1.0f), std::invalid_argument);
+  EXPECT_THROW(Dropout("Dropout", 4, 1.5f), std::invalid_argument);
+  EXPECT_THROW(Dropout("Dropout", 4, -0.1f), std::invalid_argument);
+  EXPECT_NO_THROW(Dropout("Dropout", 4, 0.0f));
+}
+
+// The dimension must be 0, 1 or 2.
+TEST(DropoutLayerTest, RejectsOutOfRangeDim) {
+  EXPECT_THROW(Dropout("Dropout", 4, 0.5f, 3), std::invalid_argument);
+  EXPECT_NO_THROW(Dropout("Dropout", 4, 0.5f, 0));
+  EXPECT_NO_THROW(Dropout("Dropout", 4, 0.5f, 1));
+  EXPECT_NO_THROW(Dropout("Dropout", 4, 0.5f, 2));
+}
+
+// A dropout layer has no weights, and it has as many outputs as inputs.
+TEST(DropoutLayerTest, ShapeAndWeights) {
+  Dropout net("Dropout", 7, 0.5f);
+  EXPECT_EQ(7, net.NumInputs());
+  EXPECT_EQ(7, net.NumOutputs());
+  EXPECT_EQ(NT_DROPOUT, net.type());
+  EXPECT_EQ(0, net.num_weights());
+}
+
+// spec() is used for logging and for the description of a network.
+TEST(DropoutLayerTest, Spec) {
+  EXPECT_EQ("Do0.5", Dropout("Dropout", 4, 0.5f).spec());
+  EXPECT_EQ("Do0.25", Dropout("Dropout", 4, 0.25f).spec());
+  EXPECT_EQ("Do0.5,1", Dropout("Dropout", 4, 0.5f, 1).spec());
+  EXPECT_EQ("Do0.5,2", Dropout("Dropout", 4, 0.5f, 2).spec());
+}
+
+// The layer passes its input through unchanged, whether training is enabled
+// for the layer or not.
+TEST(DropoutLayerTest, ForwardIdentity) {
+  const int width = 5;
+  const int ni = 3;
+  for (auto state : {TS_ENABLED, TS_DISABLED}) {
+    Dropout net("Dropout", ni, 0.5f);
+    net.SetEnableTraining(state);
+    NetworkIO input = MakeFloatInput(width, ni);
+    NetworkScratch scratch;
+    NetworkIO output;
+    net.Forward(false, input, nullptr, &scratch, &output);
+    ASSERT_EQ(width, output.Width());
+    ASSERT_EQ(ni, output.NumFeatures());
+    for (int t = 0; t < width; ++t) {
+      for (int i = 0; i < ni; ++i) {
+        EXPECT_FLOAT_EQ(input.f(t)[i], output.f(t)[i]) << "t=" << t << " i=" << i;
+      }
+    }
+  }
+}
+
+// A network which was converted to integer mode also gets its input passed
+// through unchanged.
+TEST(DropoutLayerTest, ForwardIntModeIdentity) {
+  const int width = 4;
+  const int ni = 2;
+  Dropout net("Dropout", ni, 0.5f);
+  net.SetEnableTraining(TS_DISABLED);
+
+  StrideMap stride_map;
+  stride_map.SetStride({{1, width}});
+  NetworkIO input;
+  input.ResizeToMap(true, stride_map, ni);
+  // WriteTimeStepPart expects values in [-1, 1] and quantizes them to int8.
+  const float kValues[2] = {0.5f, -0.5f};
+  TFloat value;
+  for (int t = 0; t < width; ++t) {
+    for (int i = 0; i < ni; ++i) {
+      value = kValues[i];
+      input.WriteTimeStepPart(t, i, 1, &value);
+    }
+  }
+  NetworkScratch scratch;
+  NetworkIO output;
+  net.Forward(false, input, nullptr, &scratch, &output);
+  ASSERT_TRUE(output.int_mode());
+  for (int t = 0; t < width; ++t) {
+    for (int i = 0; i < ni; ++i) {
+      EXPECT_EQ(input.i(t)[i], output.i(t)[i]) << "t=" << t << " i=" << i;
+    }
+  }
+}
+
+// The backward pass also passes the deltas through unchanged.
+TEST(DropoutLayerTest, BackwardIdentity) {
+  const int width = 4;
+  const int ni = 2;
+  Dropout net("Dropout", ni, 0.5f);
+  NetworkIO fwd = MakeFloatInput(width, ni);
+  NetworkScratch scratch;
+  NetworkIO back;
+  net.Backward(false, fwd, &scratch, &back);
+  ASSERT_EQ(width, back.Width());
+  for (int t = 0; t < width; ++t) {
+    for (int i = 0; i < ni; ++i) {
+      EXPECT_FLOAT_EQ(fwd.f(t)[i], back.f(t)[i]) << "t=" << t << " i=" << i;
+    }
+  }
+}
+
+// Serializing a layer and reading it back must reproduce the layer type, the
+// rate and the dimension.
+TEST(DropoutLayerTest, SerializeRoundTrip) {
+  Dropout net("Dropout", 6, 0.4f, 2);
+  std::vector<char> data = SerializeLayer(net);
+  TFile fp;
+  ASSERT_TRUE(fp.Open(data.data(), data.size()));
+  Network *net2 = Network::CreateFromFile(&fp);
+  ASSERT_NE(net2, nullptr);
+  ASSERT_EQ(NT_DROPOUT, net2->type());
+  Dropout *d2 = dynamic_cast<Dropout *>(net2);
+  ASSERT_NE(d2, nullptr);
+  EXPECT_EQ(6, d2->NumInputs());
+  EXPECT_EQ(6, d2->NumOutputs());
+  EXPECT_EQ("Do0.4,2", d2->spec());
+  delete net2;
+}
+
+// The serialized layer ends with the rate (float) and the dimension (32 bit
+// unsigned integer). This is the format which other versions write, so it
+// must not change.
+TEST(DropoutLayerTest, SerializedFormat) {
+  Dropout net("Dropout", 6, 0.25f, 1);
+  std::vector<char> data = SerializeLayer(net);
+  const size_t tail_size = sizeof(float) + sizeof(uint32_t);
+  ASSERT_GE(data.size(), tail_size);
+  float rate = 0;
+  uint32_t dim = 0;
+  TFile fp;
+  ASSERT_TRUE(fp.Open(&data[data.size() - tail_size], tail_size));
+  ASSERT_TRUE(fp.DeSerialize(&rate));
+  ASSERT_TRUE(fp.DeSerialize(&dim));
+  EXPECT_FLOAT_EQ(0.25f, rate);
+  EXPECT_EQ(1u, dim);
+}
+
+// A model file with an invalid rate or dimension is rejected.
+TEST(DropoutLayerTest, DeSerializeRejectsInvalidValues) {
+  Dropout net("Dropout", 6, 0.25f, 1);
+  const std::vector<char> good = SerializeLayer(net);
+  const size_t rate_offset = good.size() - sizeof(float) - sizeof(uint32_t);
+  const size_t dim_offset = good.size() - sizeof(uint32_t);
+  for (float bad_rate : {1.0f, 2.5f, -0.5f}) {
+    std::vector<char> data = good;
+    OverwriteValue(data, rate_offset, bad_rate);
+    TFile fp;
+    ASSERT_TRUE(fp.Open(data.data(), data.size()));
+    EXPECT_EQ(nullptr, Network::CreateFromFile(&fp)) << "rate=" << bad_rate;
+  }
+  {
+    std::vector<char> data = good;
+    const uint32_t bad_dim = 3;
+    OverwriteValue(data, dim_offset, bad_dim);
+    TFile fp;
+    ASSERT_TRUE(fp.Open(data.data(), data.size()));
+    EXPECT_EQ(nullptr, Network::CreateFromFile(&fp));
+  }
+  {
+    // Truncated data.
+    TFile fp;
+    ASSERT_TRUE(fp.Open(good.data(), good.size() - 1));
+    EXPECT_EQ(nullptr, Network::CreateFromFile(&fp));
+  }
+}
+
+// This version cannot train with dropout, so "Do" in a network specification
+// is rejected.
+TEST(DropoutSpecTest, RejectedInSpec) {
+  for (const char *spec : {"Do0.25 ", "Do0.5,1 ", "Do0.5,2 ", "Do "}) {
+    StaticShape shape;
+    shape.SetShape(1, 1, 1, 4);
+    const char *s = spec;
+    Network *net = NetworkBuilder(1).BuildFromString(shape, &s);
+    EXPECT_EQ(nullptr, net) << spec;
+    delete net;
+  }
+}
+
+} // namespace
+} // namespace tesseract