STN-OCR: A single Neural Network for Text Detection and Text Recognition
Christian Bartz Haojin Yang Christoph Meinel
cs.CV

	
	STN-OCR: A single Neural Network for Text Detection and Text Recognition
    C. Conti^4,5
    December 30, 2023
========================================================================



	
		OCROptical Character Recognition
		CNNConvolutional Neural Network
		RNNRecurrent Neural Network
		DNNDeep Neural Network
		BLSTMBidirectional Long-Short Term Memory
		CTCConnectionist Temporal Classification
		FSNSFrench Street Name Signs
		SGDStochastic Gradient Descent
	

	
		Detecting and recognizing text in natural scene images is a challenging, yet not completely solved task.
		In recent years several new systems that try to solve at least one of the two sub-tasks (text detection and text recognition) have been proposed.
		In this paper we present STN-OCR, a step towards semi-supervised neural networks for scene text recognition that can be optimized end-to-end.
		In contrast to most existing works that consist of multiple deep neural networks and several pre-processing steps we propose to use a single deep neural network that learns to detect and recognize text from natural images in a semi-supervised way.
		STN-OCR is a network that integrates and jointly learns a spatial transformer network <cit.>, that can learn to detect text regions in an image, and a text recognition network that takes the identified text regions and recognizes their textual content.
		We investigate how our model behaves on a range of different tasks (detection and recognition of characters, and lines of text).
		Experimental results on public benchmark datasets show the ability of our model to handle a variety of different tasks, without substantial changes in its overall network structure.
	

	

§ INTRODUCTION


Text is ubiquitous in our daily lifes.
Text can be found on documents, road signs, billboards, and other objects like cars or telephones.
Automatically detecting and reading text from natural scene images is an important part of systems that can be used for several challenging tasks such as image-based machine translation, autonomous cars or image/video indexing.
In recent years the task of detecting text and recognizing text in natural scenes has seen much interest from the computer vision and document analysis community.
Furthermore recent breakthroughs <cit.> in other areas of computer vision enabled the creation of even better scene text detection and recognition systems than before <cit.>.
Although the problem of OCR can be seen as solved for printed document texts it is still challenging to detect and recognize text in natural scene images.
Images containing natural scenes exhibit large variations of illumination, perspective distortions, image qualities, text fonts, diverse backgrounds, .

The majority of existing research works developed end-to-end scene text recognition systems that consist of complex two-step pipelines, where the first step is to detect regions of text in an image and the second step is to recognize the textual content of that identified region.
Most of the existing works only concentrate on one of these two steps.



In this paper, we present a solution that consists of a single DNN that can learn to detect and recognize text in a semi-supervised way.
This is contrary to existing works, where text detection and text recognition systems are trained separately in a fully-supervised way.
Recent work <cit.> showed that CNN are capable of learning how to solve complex multi-task problems, while being trained in an end-to-end manner.
Our motivation is to use these capabilities of CNN and create an end-to-end scene text recognition system that behaves more like a human by dividing the task at hand into smaller subtasks and solving these subtask independently from each other.
In order to achieve this behavior we learn a single DNN that is able to divide the input image into subtasks (single characters, words or even lines of text) and solve these subtasks independently of each other.
This is achieved by jointly learning a localization network that uses a recurrent spatial transformer <cit.> as attention mechanism and a text recognition network (see <ref> for a schematic overview of the system).
In this setting the network only receives the image and the labels for the text contained in that image as input.
The localization of the text is learned by the network itself, making this approach semi-supervised.


Our contributions are as follows:
[label=(*)]
	
  * We present a system that is a step towards solving end-to-end scene text recognition by integrating spatial transformer networks.
	
  * We train our proposed system end-to-end in a semi-supervised way.
	
  * We demonstrate that our approach is able to reach state-of-the-art/competitive performance on a range of standard scene text detection and recognition benchmarks.
	
  * We provide our code[<https://github.com/Bartzi/stn-ocr>] and trained models[<https://bartzi.de/research/stn-ocr>] to the research community.


This paper is structured in the following way:
In <ref> we outline work of other researchers related to ours.
Section <ref> describes our proposed system in detail and provides best practices on how to train such a system.
We show and discuss our results on standard benchmark datasets in <ref> and conclude our findings in <ref>.











































	

§ RELATED WORK



Over the course of years a rich environment of different approaches to scene text detection and recognition have been developed and published.
Nearly all systems use a two-step process for performing end-to-end recognition of scene text.
The first step is to detect regions of text and extract these regions from the input image.
The second step is to recognize the textual content and return the text strings of these extracted text regions.


It is further possible to divide these approaches into three broad categories:
[label=(*)]
	
  * Systems relying on hand crafted features and human knowledge for text detection and text recognition.
	
  * Systems using deep learning approaches together with hand crafted features, or two different deep networks for each of the two steps.
	
  * Systems that do not consist of a two step approach but rather perform text detection and recognition using a single deep neural network.

We will discuss some of these systems for each category below.



  
Hand Crafted Features
	In the beginning methods based on hand crafted features and human knowledge have been used to perform text detection.
	These systems used features like MSERs <cit.>, Stroke Width Transforms <cit.> or HOG-Features <cit.> to identify regions of text and provide them to the text recognition stage of the system.
	In the text recognition stage sliding window classifiers <cit.> and ensembles of SVMs <cit.> or k-Nearest Neighbor classifiers using HOG features <cit.> were used.
	All of these approaches use hand crafted features that have a large variety of hyper parameters that need expert knowledge to correctly tune them for achieving the best results.



  
Deep Learning Approaches
	More recent systems exchange approaches based on hand crafted features in one or both steps of end-to-end recognition systems by approaches using DNN.
	Gómez and Karatzas <cit.> propose a text-specific selective search algorithm that, together with a DNN, can be used to detect (distorted) text regions in natural scene images.
	Gupta <cit.> propose a text detection model based on the YOLO-Architecture <cit.> that uses a fully convolutional deep neural network to identify text regions.
	The text regions identified by these approaches can then be used as input for further systems based on DNN that perform text recognition.

	Bissacco <cit.> propose a complete end-to-end architecture that performs text detection using hand crafted features. The identified text regions are binarized and then used as input to a deep fully connected neural network that classifies each found character independently.
	Jaderberg <cit.> propose several systems that use deep neural networks for text detection and text recognition.
	In <cit.> Jaderberg propose a sliding window text detection approach that slides a convolutional text detection model across the image in multiple resolutions.
	The text recognition stage uses a single character CNN, which is slided across the identified text region.
	This CNN shares its weights with the CNN used for text detection.
	In <cit.> Jaderberg propose to use a region proposal network with an extra bounding box regression CNN for text detection and a CNN that takes the whole text region as input and performs classification across a pre-defined dictionary of words, making this approach only applicable to one given language.

	Goodfellow <cit.> propose a text recognition system for house numbers, that has been refined by Jaderberg <cit.> for unconstrained text recognition.
	This system uses a single CNN, which takes the complete extracted text region as input, and provides the text contained in that text region.
	This is achieved by having one independent classifier for each possible character in the given word.
	Based on this idea He <cit.> and Shi <cit.> propose text recognition systems that treat the recognition of characters from the extracted text region as a sequence recognition problem.
	He <cit.> use a naive sliding window approach that creates slices of the text region, which are used as input to their text recognition CNN.
	The features produced by the text recognition CNN are used as input to a RNN that predicts the sequence of characters.
	In our experiments on pure scene text recognition (see section <ref> for more information) we use a similar approach, but our system uses a more sophisticated sliding window approach, where the choice of the sliding windows is automatically learned by the network and not engineered by hand.
	Shi <cit.> utilize a CNN that uses the complete text region as input and produces a sequence of feature vectors, which are fed to a RNN that predicts the sequence of characters in the extracted text region.
	This approach generates a fixed number of feature vectors based on the width of the text region.
	That means for a text region that only contains a few characters, but has the same width as a text region with sufficently more characters, this approach will produce the same amount of feature vectors used as input to the RNN. In our pure text recognition experiments we utilized the strength of our approach to learn to attend to the most important information in the extracted text region, hence producing only as many feature vectors as necessary.
	Shi <cit.> improve their approach by firstly adding an extra step that utilizes the rectification capabilities of Spatial Transformer Networks <cit.> for rectifying the extracted text line.
	Secondly they added a soft-attention mechanism to their network that helps to produce the sequence of characters in the input image.
	In their work Shi make use of Spatial Transformers as an extra pre-processing step to make it easier for the recognition network to recognize the text in the image. In our system we use the Spatial Transformer as a core building block for detecting text in a semi-supervised way.



  
End-to-End trainable Approaches
	The presented systems always use a two-step approach for detecting and recognizing text from scene text images.
	Although recent approaches make use of deep neural networks they are still using a huge amount of hand crafted knowledge in either of the steps or at the point where the results of both steps are fused together.
	Smith <cit.> propose an end-to-end trainable system that is able to detect and recognize text on french street name signs, using a single DNN.
	In contrast to our system it is not possible for the system to provide the location of the text in the image, only the textual content can be extracted.
	Furthermore the attention mechanism used in our approach shows a more human-like behaviour because is sequentially localizes and recognizes text from the given image.




































































	

§ PROPOSED SYSTEM



A human trying to find and read text will do so in a sequential manner. The first action is to put attention on a line of text, read each character sequentially and then attend to the next line of text.
Most current end-to-end systems for scene text recognition do not behave in that way.
These systems rather try to solve the problem by extracting all information from the image at once.
Our system first tries to attend sequentially to different text regions in the image and then recognize the textual content of each text region.
In order to this we created a simple DNN consisting of two stages:
[label=(*)]
	
  * text detection
	
  * text recognition
.
In this section we will introduce the attention concept used by the text detection stage and the overall structure of the proposed system.
We also report best practices for successfully training such a system.



 §.§ Detecting Text with Spatial Transformers



A spatial transformer proposed by Jaderberg <cit.> is a differentiable module for DNN that takes an input feature map I and applies a spatial transformation to this feature map, producing an output feature map O.
Such a spatial transformer module is a combination of three parts.
The first part is a localisation network computing a function f_loc, that predicts the parameters θ of the spatial transformation to be applied.
These predicted parameters are used in the second part to create a sampling grid that defines which features of the input feature map should be mapped to the output feature map.
The third part is a differentiable interpolation method that takes the generated sampling grid and produces the spatially transformed output feature map O.
We will shortly describe each component in the following paragraphs.





  
Localization Network
	The localization network takes the input feature map I ∈ℝ^C × H × W, with C channels, height H and width W and outputs the parameters θ of the transformation that shall be applied.
	In our system we use the localization network (f_loc) to predict N two-dimensional affine transformation matrices A^n_θ, where n ∈{0, …, N - 1}:
	
    f_loc(I) = A^n_θ = 
    		[ θ^n_1 θ^n_2 θ^n_3; θ^n_4 θ^n_5 θ^n_6;       ]


	N is thereby the number of characters, words or textlines the localization network shall localize.
	The affine transformation matrices predicted in that way allow the network to apply translation, rotation, zoom and skew to the input image, hence the network learns to produce transformation parameters that can zoom on characters, words or text lines that are to be extracted from the image.

	In our system the N transformation matrices A^n_θ are produced by using a feed-forward CNN together with a RNN. Each of the N transformation matrices is computed using the hidden state h_n for each time-step of the RNN:
	
    c    = f^conv_loc(I) 
    
    		h_n    = f^rnn_loc(c, h_n-1) 
    
    		A^n_θ   = g_loc(h_n)

	where g_loc is another feed-forward network, and each transformation matrix A^n_θ is conditioned on the globally extracted convolutional features (f^conv_loc) together with the hidden state of the previously performed time-step.

	The CNN in the localization network used by us is a variant of the well known ResNet by He <cit.>.
	We use a variant of ResNet because we found that with this network structure our system learns faster and more successful, as compared to experiments with other network structures like the VGGNet <cit.>.
	We argue that this is due to the fact that the residual connections of the ResNet help with retaining a strong gradient down to the very first convolutional layers.
	In addition to the structure we also used Batch Normalization <cit.> for all our experiments.
	The RNN used in the localization network is a BLSTM <cit.> unit.
	This BLSTM is used to generate the hidden states h_n, which in turn are used to predict the affine transformation matrices.
	We used the same structure of the network for all our experiments we report in <ref>.
	<ref> provides a structural overview of this network.





  
Grid Generator
	The grid generator uses a regularly spaced grid G_o with coordinates y_h_o,x_w_o, of height H_o and width W_o, together with the affine transformation matrices A^n_θ to produce N regular grids G^n_i of coordinates u^n_i,v^n_j of the input feature map I, where i ∈ H_o and j ∈ W_o:
	
    [ u^n_i; v^n_j ]
    		= A^n_θ[ x_w_o; y_h_o;     1 ]
    		= [ θ^n_1 θ^n_2 θ^n_3; θ^n_4 θ^n_5 θ^n_6;       ][ x_w_o; y_h_o;     1 ]

	During inference we can extract the N resulting grids G^n_i which contain the bounding boxes of the text regions found by the localization network.
	Height H_o and width W_o can be chosen freely and if they are lower than height H or width W of the input feature map I the grid generator is producing a grid that performs a downsampling operation in the next step.
	


  
Image Sampling
	The N sampling grids G^n_i produced by the grid generator are now used to sample values of the feature map I at their corresponding coordinates u^n_i,v^n_j for each n ∈ N. Naturally these points will not always perfectly align with the discrete grid of values in the input feature map.
	Because of that we use bilinear sampling that extracts the value at a given coordinate by bilinear interpolating the values of the nearest neighbors. With that we define the values of the N output feature maps O^n at a given location i,j where i ∈ H_o and j ∈ W_o:
	
    O^n_ij = ∑^H_o_h ∑^W_o_w I_hw max(0, 1 - | u^n_i - h |) max(0, 1 - | v^n_j - w |)

	This bilinear sampling is (sub-)differentiable, hence it is possible to propagate error gradients to the localization network by using standard backpropagation.

The combination of localization network, grid generator and image sampler forms a spatial transformer and can in general be used in every part of a DNN.
In our system we use the spatial transformer as the first step of our network. The localization network receives the input image as input feature map and produces a set of affine transformation matrices that are used by the grid generator to calculate the position of the pixels that shall be sampled by the bilinear sampling operation.





 §.§ Text Recognition Stage


The image sampler of the text detection stage produces a set of N regions that are extracted from the original input image.
The text recognition stage (a structural overview of this stage can be found in <ref>) uses each of these N different regions and processes them independently of each other.
The processing of the N different regions is handled by a CNN.
This CNN is also based on the ResNet architecture as we found that we could only achieve good results if we use a variant of the ResNet architecture for our recognition network.
We argue that using a ResNet in the recognition stage is even more important than in the detection stage, because the detection stage needs to receive strong gradients from the recognition stage in order to successfully update the weights of the localization network.
The CNN of the recognition stage predicts a probability distribution ŷ over the label space L_ϵ, where L_ϵ = L ∪{ϵ}, with L = {0-9a-z} and ϵ representing the blank label.
Depending on the task this probability distribution is either generated by a fixed number of T softmax classifiers, where each softmax classifier is used to predict one character of the given word:

    x^n    = O^n 
    ŷ^n_t    = softmax(f_rec(x^n)) 
    ŷ^n    = ∑_t=1^
    	T ŷ^n_t

where f_rec(x) is the result of applying the convolutional feature extractor on the sampled input x.

Another possibility is to train the network using CTC <cit.> and retrieve the most probable labeling by setting ŷ to be the most probable labeling path π, that is given by:

    p(π|x^n)    = ∏^T_t=1ŷ^n_π_t, ∀π∈ L^T_ϵ
    ŷ^n_t    = argmax p(π|x^n) 
    ŷ^n    = B(∑^T_t=1ŷ^n_t)

with L^T_ϵ being the set of all labels that have the length T and p(π|x^n) being the probability that path π∈ L^T_ϵ is predicted by the DNN. B is a function that removes all predicted blank labels and all repeated labels (e.g. B(-IC-CC-V) = B(II–CCC-C–V-) = ICCV).



 §.§ Model Training


The training set X used for training the model consists of a set of input images I and a set of text labels L_I for each input image.
We do not use any labels for training the text detection stage.
This stage is learning to detect regions of text only by using the error gradients obtained by either calculating the cross-entropy loss or the CTC loss of the predictions and the textual labels.
During our experiments we found that, when trained from scratch, a network that shall detect and recognize more than two text lines does not converge.
The solution to this problem is to perform a series of pre-training steps where the difficulty is gradually increasing.
Furthermore we find that the optimization algorithm chosen to train the network has a great influence on the convergence of the network.
We found that it is beneficial to use SGD for pre-training the network on a simpler task and Adam <cit.> for finetuning the already pre-trained network on images with more text lines.
We argue that SGD performs better during pre-training because the learning rate η is kept constant during a longer period of time, which enables the text detection stage to explore the input images and better find text regions.
With decreasing learning rate the updates in the detection stage become smaller and the text detection stage (ideally) settles on already found text regions.
At the same time the text recognition network can start to use the extracted text regions and learn to recognize the text in that regions.
While training the network with SGD it is important to note that choosing a too high learning rate will result in divergence of the model early on.

We found that using initial learning rates between 1^-5 and 5^-7 tend to work in nearly all cases, except in cases where the network should only be fine-tuned.
Here we found that using Adam is the more reliable choice, as Adam chooses the learning rate for each parameter in an adaptive way and hence does not allow the detection network to explore as radically as it does when using SGD.

















































	

§ EXPERIMENTS



In this section we evaluate our presented network architecture on several standard scene text detection/recognition datasets.
We present the results of experiments for three different datasets, where the difficulty of the task at hand increases for each dataset.
We first begin with experiments on the SVHN dataset <cit.>, that we used to prove that our concept as such is feasible.
The second type of dataset we performed experiments on were datasets for focused scene text recognition, where we explored the performance of our model, when it comes to find and recognize single characters.
The third dataset we exerimented with was the FSNS dataset <cit.>, which is the most challenging dataset we used, as this dataset contains a vast amount of irregular, low resolution text lines that are more difficult to locate and recognize than text lines from the SVHN dataset.
We begin this section by introducing our experimental setup.
We will then present the results and characteristics of the experiments for each of the aforementioned datasets.



 §.§ Experimental Setup





  
Localization Network
The localization network used in every experiment is based on the ResNet architecture <cit.>.
The input to the network is the image where text shall be localized and later recognized.
Before the first residual block the network performs a 3 × 3 convolution followed by a 2 × 2 average pooling layer with stride 2.
After these layers three residual blocks with two 3 × 3 convolutions, each followed by batch normalization <cit.>, are used.
The number of convolutional filters is 32, 48 and 48 respectively and ReLU <cit.> is used as activation function for each convolutional layer.
A 2 × 2 max-pooling with stride 2 follows after the second residual block.
The last residual block is followed by a 5 × 5 average pooling layer and this layer is followed by a BLSTM with 256 hidden units.
For each time step of the BLSTM a fully connected layer with 6 hidden units follows.
This layer predicts the affine transformation matrix, that is used to generate the sampling grid for the bilinear interpolation.
As rectification of scene text is beyond the scope of this work we disabled skew and rotation in the affine transformation matrices by setting the according parameters to 0.
We will discuss the rectification capabilities of Spatial Transformers for scene text detection in our future work.



  
Recognition Network
The inputs to the recognition network are N crops from the original input image that represent the text regions found by the localization network.
The recognition network has the same structure as the localization network, but the number of convolutional filters is higher.
The number of convolutional filters is 32, 64 and 128 respectively.
Depending on the experiment we either used an ensemble of T independent softmax classifiers as used in <cit.> and <cit.>, where T is the maximum length that a word may have, or we used CTC with best path decoding as used in <cit.> and <cit.>.



  
Implementation
We implemented all our experiments using MXNet <cit.>. We conduted all our experiments on a work station which has an Intel(R) Core(TM) i7-6900K CPU, 64 GB RAM and 4 TITAN X (Pascal) GPUs.



 §.§ Experiments on the SVHN dataset



With our first experiments on the SVHN dataset <cit.> we wanted to prove that our concept works and can be used with real world data.
We therefore first conducted experiments similar to the experiments in <cit.> on SVHN image crops with a single house number in each image crop, that is centered around the number and also contains background noise.
<ref> shows that we are able to reach competitive recognition accuracies.



Based on this experiment we wanted to determine whether our model is able to detect different lines of text that are arranged in a regular grid or placed at random locations in the image.
In <ref> we show samples from two purpose build datasets[datasets are available here: <https://bartzi.de/research/stn-ocr>] that we used for our other experiments based on SVHN data.
We found that our network performs well on the task of finding and recognizing house numbers that are arranged in a regular grid.
An interesting observation we made during training on this data was that we were able to achieve our best results when we did two training steps.
The first step was to train the complete model from scratch (all weights initialized randomly) and then train the model with the same data again, but this time with the localization network pre-initialized with the weights obtained from the last training and the recognition net initialized with random weights.
This strategy leads to better localization results of the localization network and hence improved recognition results.

During our experiments on the second dataset, created by us, we found that it is not possible to train a model from scratch, that can find and recognize more than two textlines that are scattered across the whole image.
It is possible to train such a network by first training the model on easier tasks first (few textlines, textlines closer to the center of the image) and then increase the difficulty of the task gradually.
In the supplementary material we provide short video clips that show how the network is exploring the image while learning to detect text for a range of different experiments.






 §.§ Experiments on Robust Reading Datasets



In our next experiments we used datasets where text regions are aleady cropped from the input images.
We wanted to see whether our text localization network can be used as an intelligent sliding window generator that adopts to irregularities of the text in the cropped text region.
Therefore we trained our recognition model using CTC on a dataset of synthetic cropped word images, that we generated using our own data generator, that works similar to the data generator introduced by Jaderberg <cit.>.
In <ref> we report the recognition results of our model on the ICDAR 2013 robust reading <cit.>, the Street View Text (SVT) <cit.> and the IIIT5K <cit.> benchmark datasets.
For evaluation on the ICDAR 2013 and SVT datasets, we filtered all images that contain non-alphanumeric characters and discarded all images that have less than 3 characters as done in <cit.>.
We obtained our final results by post-processing the predictions using the standard hunspell english (en-US) dictionary.
Overall we find that our model achieves state-of-the-art performance for unconstrained recognition models on the ICDAR 2013 and IIIT5K dataset and competitive performance on the SVT dataset.
In <ref> we show that our model learns to follow the slope of the individual text regions, proving that our model produces sliding windows in an intelligent way.









 §.§ Preliminary Experiments on the FSNS dataset



Following our scheme of increasing the difficulty of the task that should be solved by the network, we chose the FSNS dataset by Smith <cit.> to be our third dataset to perform experiments on.
The results we report here are preliminary and are only meant to show that our network architecture is also applicable to this kind of data, although it does not yet reach state-of-the-art results.
The FSNS dataset contains images of french street name signs that have been extracted from Google Streetview.
This dataset is the most challenging dataset for our approach as it 
[label=(*)]
	
  * contains multiple lines of text with varying length embedded in natural scenes with distracting backgrounds and
	
  * contains a lot of images that do not include the full name of the streets.


During our first experiments with that dataset we found that our model is not able to converge, when trained on the supplied groundtruth. We argue that this is because the labels of the original dataset do not include any hint on which words can be found in which text line.
We therefore changed our approach and started with experiments where we tried to find individual words instead of textlines with more than one word.
We adapted the groundtruth accordingly and used all images that contain a maximum of three words for our experiments, which leaves us with approximately 80 of the original data from the dataset.
<ref> shows some examples from the FSNS dataset where our model correctly localized the individual words and also correctly recognized the words.
Using this approach we were able to achieve a reasonably good character recognition accuracy of 97 on the test set, but only a word accuracy of 71.8.
The discrepancy in character recognition rate and word recognition rate is caused by the fact that the model we trained for this task uses independent softmax classifiers for each character in a word.
Having a character recognition accuracy of 97 means that there is a high probability that at least one classifier makes a mistake and thus increases the sequence error.







































		
	

§ CONCLUSION



In this paper we presented a system that can be seen as a step towards solving end-to-end scene text recognition, using only a single multi-task deep neural network.
We trained the text detection component of our model in a semi-supervised way and are able to extract the localization results of the text detection component.
The network architecture of our system is simple, but it is not easy to train this system, as a successful training requires extensive pre-training on easier sub-tasks before the model can converge on the real task.
We also showed that the same network architecture can be used to reach competitive or state-of-the-art results on a range of different public benchmark datasets for scene text detection/recognition.

At the current state we note that our models are not fully capable of detecting text in arbitrary locations in the image, as we saw during our experiments with the FSNS dataset.
Right now our model is also constrained to a fixed number of maximum textlines/characters that can be detected at once, in our future work we want to redesign the network in a way that makes it possible for the network to determine the number of textlines in an image by itself.

















	ieee
		
	
