Skip to content

Commit

Permalink
Use const char* for filename and network_spec parameters
Browse files Browse the repository at this point in the history
This replaces the proprietary STRING data type
(764 instead of 838 lines remaining).

It also removes STRING from osdetect.h and serialis.h.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed Nov 26, 2020
1 parent 4c35f51 commit 4613738
Show file tree
Hide file tree
Showing 26 changed files with 105 additions and 97 deletions.
2 changes: 1 addition & 1 deletion src/lstm/convolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace tesseract {

Convolve::Convolve(const STRING& name, int ni, int half_x, int half_y)
Convolve::Convolve(const std::string& name, int ni, int half_x, int half_y)
: Network(NT_CONVOLVE, name, ni, ni * (2*half_x + 1) * (2*half_y + 1)),
half_x_(half_x), half_y_(half_y) {
}
Expand Down
2 changes: 1 addition & 1 deletion src/lstm/convolve.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Convolve : public Network {
public:
// The area of convolution is 2*half_x + 1 by 2*half_y + 1, forcing it to
// always be odd, so the center is the current pixel.
Convolve(const STRING& name, int ni, int half_x, int half_y);
Convolve(const std::string& name, int ni, int half_x, int half_y);
~Convolve() override = default;

STRING spec() const override {
Expand Down
2 changes: 1 addition & 1 deletion src/lstm/fullyconnected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const int kNumThreads = 1;

namespace tesseract {

FullyConnected::FullyConnected(const STRING& name, int ni, int no,
FullyConnected::FullyConnected(const std::string& name, int ni, int no,
NetworkType type)
: Network(type, name, ni, no), external_source_(nullptr), int_mode_(false) {
}
Expand Down
3 changes: 1 addition & 2 deletions src/lstm/fullyconnected.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// File: fullyconnected.h
// Description: Simple feed-forward layer with various non-linearities.
// Author: Ray Smith
// Created: Wed Feb 26 14:46:06 PST 2014
//
// (C) Copyright 2014, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -27,7 +26,7 @@ namespace tesseract {
// C++ Implementation of the Softmax (output) class from lstm.py.
class FullyConnected : public Network {
public:
FullyConnected(const STRING& name, int ni, int no, NetworkType type);
FullyConnected(const std::string& name, int ni, int no, NetworkType type);
~FullyConnected() override = default;

// Returns the shape output from the network given an input shape (which may
Expand Down
4 changes: 2 additions & 2 deletions src/lstm/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ namespace tesseract {
// Max height for variable height inputs before scaling anyway.
const int kMaxInputHeight = 48;

Input::Input(const STRING& name, int ni, int no)
Input::Input(const std::string& name, int ni, int no)
: Network(NT_INPUT, name, ni, no), cached_x_scale_(1) {}
Input::Input(const STRING& name, const StaticShape& shape)
Input::Input(const std::string& name, const StaticShape& shape)
: Network(NT_INPUT, name, shape.height(), shape.depth()),
shape_(shape),
cached_x_scale_(1) {
Expand Down
4 changes: 2 additions & 2 deletions src/lstm/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace tesseract {

class Input : public Network {
public:
Input(const STRING& name, int ni, int no);
Input(const STRING& name, const StaticShape& shape);
Input(const std::string& name, int ni, int no);
Input(const std::string& name, const StaticShape& shape);
~Input() override = default;

STRING spec() const override {
Expand Down
9 changes: 5 additions & 4 deletions src/lstm/lstm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#endif
#include <cstdio>
#include <cstdlib>
#include <sstream> // for std::ostringstream

#if !defined(__GNUC__) && defined(_MSC_VER)
#include <intrin.h> // _BitScanReverse
Expand Down Expand Up @@ -99,7 +100,7 @@ static inline uint32_t ceil_log2(uint32_t n)
return (n == (1u << l2)) ? l2 : l2 + 1;
}

LSTM::LSTM(const STRING& name, int ni, int ns, int no, bool two_dimensional,
LSTM::LSTM(const std::string& name, int ni, int ns, int no, bool two_dimensional,
NetworkType type)
: Network(type, name, ni, no),
na_(ni + ns),
Expand Down Expand Up @@ -197,9 +198,9 @@ void LSTM::ConvertToInt() {
void LSTM::DebugWeights() {
for (int w = 0; w < WT_COUNT; ++w) {
if (w == GFS && !Is2D()) continue;
STRING msg = name_;
msg.add_str_int(" Gate weights ", w);
gate_weights_[w].Debug2D(msg.c_str());
std::ostringstream msg;
msg << name_ << " Gate weights " << w;
gate_weights_[w].Debug2D(msg.str().c_str());
}
if (softmax_ != nullptr) {
softmax_->DebugWeights();
Expand Down
3 changes: 1 addition & 2 deletions src/lstm/lstm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// File: lstm.h
// Description: Long-term-short-term-memory Recurrent neural network.
// Author: Ray Smith
// Created: Wed May 01 17:33:06 PST 2013
//
// (C) Copyright 2013, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -47,7 +46,7 @@ class LSTM : public Network {
// 2-d and bidi softmax LSTMs are not rejected, but are impossible to build
// in the conventional way because the output feedback both forwards and
// backwards in time does become impossible.
LSTM(const STRING& name, int num_inputs, int num_states, int num_outputs,
LSTM(const std::string& name, int num_inputs, int num_states, int num_outputs,
bool two_dimensional, NetworkType type);
~LSTM() override;

Expand Down
2 changes: 1 addition & 1 deletion src/lstm/maxpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace tesseract {

Maxpool::Maxpool(const STRING& name, int ni, int x_scale, int y_scale)
Maxpool::Maxpool(const char* name, int ni, int x_scale, int y_scale)
: Reconfig(name, ni, x_scale, y_scale) {
type_ = NT_MAXPOOL;
no_ = ni;
Expand Down
2 changes: 1 addition & 1 deletion src/lstm/maxpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace tesseract {
// Backprop propagates only to the position that was the max.
class Maxpool : public Reconfig {
public:
Maxpool(const STRING& name, int ni, int x_scale, int y_scale);
Maxpool(const char* name, int ni, int x_scale, int y_scale);
~Maxpool() override = default;

// Accessors.
Expand Down
24 changes: 12 additions & 12 deletions src/lstm/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Network::Network()
forward_win_(nullptr),
backward_win_(nullptr),
randomizer_(nullptr) {}
Network::Network(NetworkType type, const STRING& name, int ni, int no)
Network::Network(NetworkType type, const std::string& name, int ni, int no)
: type_(type),
training_(TS_ENABLED),
needs_to_backprop_(true),
Expand Down Expand Up @@ -161,7 +161,7 @@ bool Network::Serialize(TFile* fp) const {
if (!fp->Serialize(&ni_)) return false;
if (!fp->Serialize(&no_)) return false;
if (!fp->Serialize(&num_weights_)) return false;
if (!name_.Serialize(fp)) return false;
if (!fp->Serialize(name_.c_str(), name_.length())) return false;
return true;
}

Expand Down Expand Up @@ -208,40 +208,40 @@ Network* Network::CreateFromFile(TFile* fp) {

switch (type) {
case NT_CONVOLVE:
network = new Convolve(name, ni, 0, 0);
network = new Convolve(name.c_str(), ni, 0, 0);
break;
case NT_INPUT:
network = new Input(name, ni, no);
network = new Input(name.c_str(), ni, no);
break;
case NT_LSTM:
case NT_LSTM_SOFTMAX:
case NT_LSTM_SOFTMAX_ENCODED:
case NT_LSTM_SUMMARY:
network =
new LSTM(name, ni, no, no, false, type);
new LSTM(name.c_str(), ni, no, no, false, type);
break;
case NT_MAXPOOL:
network = new Maxpool(name, ni, 0, 0);
network = new Maxpool(name.c_str(), ni, 0, 0);
break;
// All variants of Parallel.
case NT_PARALLEL:
case NT_REPLICATED:
case NT_PAR_RL_LSTM:
case NT_PAR_UD_LSTM:
case NT_PAR_2D_LSTM:
network = new Parallel(name, type);
network = new Parallel(name.c_str(), type);
break;
case NT_RECONFIG:
network = new Reconfig(name, ni, 0, 0);
network = new Reconfig(name.c_str(), ni, 0, 0);
break;
// All variants of reversed.
case NT_XREVERSED:
case NT_YREVERSED:
case NT_XYTRANSPOSE:
network = new Reversed(name, type);
network = new Reversed(name.c_str(), type);
break;
case NT_SERIES:
network = new Series(name);
network = new Series(name.c_str());
break;
case NT_TENSORFLOW:
#ifdef INCLUDE_TENSORFLOW
Expand All @@ -259,7 +259,7 @@ Network* Network::CreateFromFile(TFile* fp) {
case NT_LOGISTIC:
case NT_POSCLIP:
case NT_SYMCLIP:
network = new FullyConnected(name, ni, no, type);
network = new FullyConnected(name.c_str(), ni, no, type);
break;
default:
break;
Expand Down Expand Up @@ -298,7 +298,7 @@ void Network::DisplayForward(const NetworkIO& matrix) {
// Displays the image of the matrix to the backward window.
void Network::DisplayBackward(const NetworkIO& matrix) {
Pix* image = matrix.ToPix();
STRING window_name = name_ + "-back";
std::string window_name = name_ + "-back";
ClearWindow(false, window_name.c_str(), pixGetWidth(image),
pixGetHeight(image), &backward_win_);
DisplayImage(image, backward_win_);
Expand Down
6 changes: 3 additions & 3 deletions src/lstm/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ enum TrainingState {
class Network {
public:
Network();
Network(NetworkType type, const STRING& name, int ni, int no);
Network(NetworkType type, const std::string& name, int ni, int no);
virtual ~Network() = default;

// Accessors.
Expand Down Expand Up @@ -135,7 +135,7 @@ class Network {
result.set_depth(no_);
return result;
}
const STRING& name() const {
const std::string& name() const {
return name_;
}
virtual STRING spec() const {
Expand Down Expand Up @@ -297,7 +297,7 @@ class Network {
int32_t ni_; // Number of input values.
int32_t no_; // Number of output values.
int32_t num_weights_; // Number of weights in this and sub-network.
STRING name_; // A unique name for this layer.
std::string name_; // A unique name for this layer.

// NOT-serialized debug data.
ScrollView* forward_win_; // Recognition debug display window.
Expand Down
2 changes: 1 addition & 1 deletion src/lstm/parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
namespace tesseract {

// ni_ and no_ will be set by AddToStack.
Parallel::Parallel(const STRING& name, NetworkType type) : Plumbing(name) {
Parallel::Parallel(const char* name, NetworkType type) : Plumbing(name) {
type_ = type;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lstm/parallel.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace tesseract {
class Parallel : public Plumbing {
public:
// ni_ and no_ will be set by AddToStack.
Parallel(const STRING& name, NetworkType type);
Parallel(const char* name, NetworkType type);
~Parallel() override = default;

// Returns the shape output from the network given an input shape (which may
Expand Down
2 changes: 1 addition & 1 deletion src/lstm/plumbing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
namespace tesseract {

// ni_ and no_ will be set by AddToStack.
Plumbing::Plumbing(const STRING& name)
Plumbing::Plumbing(const std::string& name)
: Network(NT_PARALLEL, name, 0, 0) {
}

Expand Down
3 changes: 1 addition & 2 deletions src/lstm/plumbing.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Description: Base class for networks that organize other networks
// eg series or parallel.
// Author: Ray Smith
// Created: Mon May 12 08:11:36 PST 2014
//
// (C) Copyright 2014, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -30,7 +29,7 @@ namespace tesseract {
class Plumbing : public Network {
public:
// ni_ and no_ will be set by AddToStack.
explicit Plumbing(const STRING& name);
explicit Plumbing(const std::string& name);
~Plumbing() override = default;

// Returns the required shape input to the network.
Expand Down
2 changes: 1 addition & 1 deletion src/lstm/reconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace tesseract {

Reconfig::Reconfig(const STRING& name, int ni, int x_scale, int y_scale)
Reconfig::Reconfig(const char* name, int ni, int x_scale, int y_scale)
: Network(NT_RECONFIG, name, ni, ni * x_scale * y_scale),
x_scale_(x_scale), y_scale_(y_scale) {
}
Expand Down
2 changes: 1 addition & 1 deletion src/lstm/reconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace tesseract {
// input stride is a multiple of the y_scale factor!
class Reconfig : public Network {
public:
Reconfig(const STRING& name, int ni, int x_scale, int y_scale);
Reconfig(const char* name, int ni, int x_scale, int y_scale);
~Reconfig() override = default;

// Returns the shape output from the network given an input shape (which may
Expand Down
3 changes: 1 addition & 2 deletions src/lstm/reversed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// File: reversed.cpp
// Description: Runs a single network on time-reversed input, reversing output.
// Author: Ray Smith
// Created: Thu May 02 08:42:06 PST 2013
//
// (C) Copyright 2013, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,7 +23,7 @@

namespace tesseract {

Reversed::Reversed(const STRING& name, NetworkType type) : Plumbing(name) {
Reversed::Reversed(const std::string& name, NetworkType type) : Plumbing(name) {
type_ = type;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lstm/reversed.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace tesseract {
// C++ Implementation of the Reversed class from lstm.py.
class Reversed : public Plumbing {
public:
explicit Reversed(const STRING& name, NetworkType type);
explicit Reversed(const std::string& name, NetworkType type);
~Reversed() override = default;

// Returns the shape output from the network given an input shape (which may
Expand Down
2 changes: 1 addition & 1 deletion src/lstm/series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
namespace tesseract {

// ni_ and no_ will be set by AddToStack.
Series::Series(const STRING& name) : Plumbing(name) {
Series::Series(const char* name) : Plumbing(name) {
type_ = NT_SERIES;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lstm/series.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace tesseract {
class Series : public Plumbing {
public:
// ni_ and no_ will be set by AddToStack.
explicit Series(const STRING& name);
explicit Series(const char* name);
~Series() override = default;

// Returns the shape output from the network given an input shape (which may
Expand Down
6 changes: 3 additions & 3 deletions src/training/lstmtrainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ bool LSTMTrainer::TryLoadingCheckpoint(const char* filename,
// are implemented.
// For other args see NetworkBuilder::InitNetwork.
// Note: Be sure to call InitCharSet before InitNetwork!
bool LSTMTrainer::InitNetwork(const STRING& network_spec, int append_index,
bool LSTMTrainer::InitNetwork(const char* network_spec, int append_index,
int net_flags, float weight_range,
float learning_rate, float momentum,
float adam_beta) {
mgr_.SetVersionString(mgr_.VersionString() + ":" + network_spec.c_str());
mgr_.SetVersionString(mgr_.VersionString() + ":" + network_spec);
adam_beta_ = adam_beta;
learning_rate_ = learning_rate;
momentum_ = momentum;
Expand All @@ -157,7 +157,7 @@ bool LSTMTrainer::InitNetwork(const STRING& network_spec, int append_index,
}
network_str_ += network_spec;
tprintf("Built network:%s from request %s\n",
network_->spec().c_str(), network_spec.c_str());
network_->spec().c_str(), network_spec);
tprintf(
"Training parameters:\n Debug interval = %d,"
" weights = %g, learning rate = %g, momentum=%g\n",
Expand Down
2 changes: 1 addition & 1 deletion src/training/lstmtrainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class LSTMTrainer : public LSTMRecognizer {
// are implemented.
// For other args see NetworkBuilder::InitNetwork.
// Note: Be sure to call InitCharSet before InitNetwork!
bool InitNetwork(const STRING& network_spec, int append_index, int net_flags,
bool InitNetwork(const char* network_spec, int append_index, int net_flags,
float weight_range, float learning_rate, float momentum,
float adam_beta);
// Initializes a trainer from a serialized TFNetworkModel proto.
Expand Down
Loading

0 comments on commit 4613738

Please sign in to comment.