Recognition of Hand Written Mathematical Expression using Scale Augmentation and Drop Attention

Abstract

This is a review paper. We start by explaining about how handwritten mathematical expressions have unstable scale.Then we show how augmented layers are used for scaling those mathematical expression.We continue by explaining how an attention based encoder-decoder is used for extracting features and generating predictions.The drop attention is used when the attention distribution of the decoder is not precise.This method achieves better performance than any other existing method.

1. Introduction

In this section we will discuss the problem we are addressing and how we are going to tackle the problem. So we are trying to convert Hand Written Mathematical Expressions to .tex format. One of the main problem that arises when converting Hand Written Mathematical Expression is scaling the image of the Mathematical Expression.
The main thing in this process of converting the Hand Written Mathematical Expression to .tex is the encoder-decoder network.We enhance this network by using Scale Augmentation at the beginning and Drop Attention at the end to enchance the accuracy which increases quality of this output.
Sketch of the Paper
  • Encoder-Decoder Network
  • Scale Augmentation
  • Drop Attention
  • Experiment and Implementation
  • Conclusion
  • References
Sketch of the process
  • Scale Augmentation
  • Encoder-Decoder network
  • Drop Attention
As we can see the Scale Augmentation is used at the begining before the Encoder-Decoder Network but it is used to enhance the Encoder-Decoder Network. So we need to understand what is the Encoder-Decoder Network then we can move on with Scale Augmentation.

2. Encoder-Decoder Network

The encoder is build by modifying the CNN architechture ResNet-18 and we set the stride of all convolutional layers in ResNet-18 to 1 in order to extract the more precise features and avoid the neglecting features.The other settings are same as in [1] .Max pooling layers are used for down-sampling and dropout layers are used to lessen the network over fitting.The detailed network configuration is shown in the table I.
Variables and their Meanings
  • c - Number of filters
  • k - Kernel number
  • s - Kernel Size
  • pa - Stride Padding
  • n - Block Number
  • p - Dropout Probability
Table I(detailed Network Configuration)
Layer/Block Setting Convolution c = 64 , k = 3 × 3 , s = 1 , p a = 1 Max pooling k = 2 × 2 , s = 2 Batch norm ReLU Building block c = 64 , n = 2 Max pooling k = 2 × 2 , s = 2 Dropout p = 0.1 Building block c = 128 , n = 2 Max pooling k = 2 × 2 , s = 2 Dropout p = 0.2 Building block c = 256 , n = 2 Max pooling k = 2 × 2 , s = 2 Dropout p = 0.3 Building block c = 512 , n = 2 Max pooling k = 2 × 2 , s = 2 Dropout p = 0.3  Layer/Block   Setting   Convolution  c = 64 , k = 3 × 3 , s = 1 , p a = 1  Max pooling  k = 2 × 2 , s = 2  Batch norm   ReLU   Building block  c = 64 , n = 2  Max pooling  k = 2 × 2 , s = 2  Dropout  p = 0.1  Building block  c = 128 , n = 2  Max pooling  k = 2 × 2 , s = 2  Dropout  p = 0.2  Building block  c = 256 , n = 2  Max pooling  k = 2 × 2 , s = 2  Dropout  p = 0.3  Building block  c = 512 , n = 2  Max pooling  k = 2 × 2 , s = 2  Dropout  p = 0.3 (()/([" Layer/Block "," Setting "],[" Convolution ",c=64","k=3xx3","s=1","pa=1],[" Max pooling ",k=2xx2","s=2],[" Batch norm ",-],[" ReLU ",-],[" Building block ",c=64","n=2],[" Max pooling ",k=2xx2","s=2],[" Dropout ",p=0.1],[" Building block ",c=128","n=2],[" Max pooling ",k=2xx2","s=2],[" Dropout ",p=0.2],[" Building block ",c=256","n=2],[" Max pooling ",k=2xx2","s=2],[" Dropout ",p=0.3],[" Building block ",c=512","n=2],[" Max pooling ",k=2xx2","s=2],[" Dropout ",p=0.3],[hline]))\begin{array}{c c} \hline \text { Layer/Block } & \text { Setting } \\ \hline \text { Convolution } & c=64, k=3 \times 3, s=1, p a=1 \\ \text { Max pooling } & k=2 \times 2, s=2 \\ \text { Batch norm } & - \\ \text { ReLU } & - \\ \text { Building block } & \mathrm{c}=64, \mathrm{n}=2 \\ \text { Max pooling } & k=2 \times 2, s=2 \\ \text { Dropout } & \mathrm{p}=0.1 \\ \text { Building block } & \mathrm{c}=128, \mathrm{n}=2 \\ \text { Max pooling } & k=2 \times 2, s=2 \\ \text { Dropout } & \mathrm{p}=0.2 \\ \text { Building block } & \mathrm{c}=256, \mathrm{n}=2 \\ \text { Max pooling } & k=2 \times 2, s=2 \\ \text { Dropout } & \mathrm{p}=0.3 \\ \text { Building block } & \mathrm{c}=512, \mathrm{n}=2 \\ \text { Max pooling } & k=2 \times 2, s=2 \\ \text { Dropout } & \mathrm{p}=0.3 \\ \hline \end{array}
The attention-based decoder is based on RNN and iteratively generates the target sequence Y from features F.
Definition(Softmax Function) :The softmax function is a function that turns a vector of K real values into a vector of K real values that sum to 1. The input values can be positive, negative, zero, or greater than one, but the softmax transforms them into values between 0 and 1, so that they can be interpreted as probabilities.
σ ( z ) i = e z i j = 1 K e z j σ ( z ) i = e z i j = 1 K e z j sigma( vec(z))_(i)=(e^(z_(i)))/(sum_(j=1)^(K)e^(z_(j)))\sigma(\vec{z})_{i}=\frac{e^{z_{i}}}{\sum_{j=1}^{K} e^{z_{j}}}
  • σ = softmax σ = softmax sigma=softmax\sigma=\operatorname{softmax}
  • z = z = vec(z)=\vec{z}= input vector
  • e z i = e z i = e^(z_(i))=e^{z_{i}}= standard exponential function for input vector
  • K = K = K=K= number of classes in the multi-class classifier
  • e z j = e z j = e^(z_(j))=e^{z_{j}}= standard exponential function for output vector
  • e z j = e z j = e^(z_(j))=e^{z_{j}}= standard exponential function for output vector
Variables and their Meanings
  • t - time step
  • y t y t y_(t)y_t - A member of the target sequence Y at time step t
  • c t c t c_(t)c_t - context at time step t
  • h t h t h_(t)h_t - hidden state at time step t
  • H H HH - Height of the feature F
  • W W WW - Width of the feature F
  • L L LL - the size of the features L = H W L = H W L=H*WL = H\cdot W
  • α t , l α t , l alpha_(t,l)\alpha _{t,l} - weight of the l t h l t h l^(th)l^{th} features of F at time step t
  • f l f l f_(l)f_l - the l t h l t h l^(th)l^{th} features of F
  • tanh tanh tanh\tanh - chosen activation function
  • e t , l e t , l e_(t,l)e_{t,l} - attention weight of the l t h l t h l^(th)l^{th} features of F at time step t
  • W e , W h , W f , W q , W s W e , W h , W f , W q , W s W^(e),W^(h),W^(f),W^(q),W^(s)W^e,W^h,W^f,W^q,W^s - trainable Parameters
  • q l q l q_(l)q_l - position embedding of the l t h l t h l^(th)l^{th} features of F
  • s l s l s_(l)s_l - coverage features of the l t h l t h l^(th)l^{th} features of F
  • E p , h E p , h E^(p,h)E^{p,h} - absolute position embedding in horizontal direction
  • E p , v E p , v E^(p,v)E^{p,v} - absolute position embedding in vertical direction
At t the probability of generating y t y t y_(t)y_t from the RNN output is shown in the equation 1 where g ( ) g ( ) g(*)g(\cdot) is a linear function followed by a softmax function.
(1) p ( y t ) = g ( c t , h t ) (1) p y t = g c t , h t {:(1)p(y_(t))=g(c_(t),h_(t)):}\begin{equation} p\left(y_{t}\right)=g\left(c_{t}, h_{t}\right) \end{equation}
Context c t c t c_(t)c_t is computed as a weighted sum of features in equation 2.
(2) c t = l = 0 L 1 α t , l f l (2) c t = l = 0 L 1 α t , l f l {:(2)c_(t)=sum_(l=0)^(L-1)alpha_(t,l)*f_(l):}\begin{equation} c_{t}=\sum_{l=0}^{L-1} \alpha_{t, l} \cdot f_{l} \end{equation}
Similar to human eyes the attention based decoder concentrates on only a subset of features at a time.Trained using back propagation algorithm using gradient descent,the decoder can be given the ability to determine which features are important for generating the word y t y t y_(t)y_t.We choose a linear function with the activation function tanh tanh tanh\tanh.We compute the attention weights and normalization using softmax function as shown in equation 3 and equation 4 respectively.
(3) e t , l = W e tanh ( W h h t + W f f l + W q q l + W s s l ) (3) e t , l = W e tanh W h h t + W f f l + W q q l + W s s l {:(3)e_(t,l)=W^(e)tanh(W^(h)h_(t)+W^(f)f_(l)+W^(q)q_(l)+W^(s)s_(l)):}\begin{equation} e_{t, l}=W^{e} \tanh \left(W^{h} h_{t}+W^{f} f_{l}+W^{q} q_{l}+W^{s} s_{l}\right) \end{equation}
(4) α t , l = exp ( e t , l ) i = 1 L exp ( e t , i ) (4) α t , l = exp e t , l i = 1 L exp e t , i {:(4)alpha_(t,l)=(exp(e_(t,l)))/(sum_(i=1)^(L)exp(e_(t,i))):}\begin{equation} \alpha_{t, l}=\frac{\exp \left(e_{t, l}\right)}{\sum_{i=1}^{L} \exp \left(e_{t, i}\right)} \end{equation}
Refer to [2] when calculating the attention weights, position embeddings make the decoder position sensetive.The calculation of position embedding is shown in equation 5.
(5) q l = E p , h ( l / H ) + E p , v ( l mod H ) (5) q l = E p , h ( l / H ) + E p , v ( l mod H ) {:(5)q_(l)=E^(p,h)(|__ l//H __|)+E^(p,v)(l mod H):}\begin{equation} q_{l}=E^{p, h}(\lfloor l / H\rfloor)+E^{p, v}(l \bmod H) \end{equation}
Referring to [3] the coverage features are used to lessen over-attention and under-attention. s l s l s_(l)s_lis given as the sum of past attention weights in equation 6.
(6) s l = τ = 0 t 1 α τ , l (6) s l = τ = 0 t 1 α τ , l {:(6)s_(l)=sum_(tau=0)^(t-1)alpha_(tau,l):}\begin{equation} s_{l}=\sum_{\tau=0}^{t-1} \alpha_{\tau, l} \end{equation}
The hidden state is calculated using a RNN with long short- term memory (LSTM) cells. At time step t, the RNN input is the word embedding at t−1 concatenated with context at t−1, which is a weighted sum of CNN features F with position embeddings at last time step is represented by equation 8.
(7) h t = R N N ( [ E d ( y t 1 ) , c t 1 ] , h t 1 ) (7) h t = R N N E d y t 1 , c t 1 , h t 1 {:(7)h_(t)=RNN([E^(d)(y_(t-1)),c_(t-1)^(')],h_(t-1)):}\begin{equation} h_{t}=R N N\left(\left[E^{d}\left(y_{t-1}\right), c_{t-1}^{\prime}\right], h_{t-1}\right) \end{equation}
(8) c t = l = 1 L α t , l ( f l + q l ) (8) c t = l = 1 L α t , l f l + q l {:(8)c_(t)^(')=sum_(l=1)^(L)alpha_(t,l)*(f_(l)+q_(l)):}\begin{equation} c_{t}^{\prime}=\sum_{l=1}^{L} \alpha_{t, l} \cdot\left(f_{l}+q_{l}\right) \end{equation}

3. Scale Augmentation

Since in a Mathematical Expression the expression consists of various symbols some smaller than the others which increases the recognition difficulty. So we apply augmentation to the Mathematical Expression according to equation 9 to improve the recognition.
(9) { x = k x y = k y (9) x = k x y = k y {:(9){[x^(')=kx],[y^(')=ky]:}:}\begin{equation} \left\{\begin{array}{l} x^{\prime}=k x \\ y^{\prime}=k y \end{array}\right. \end{equation}
k k kk is the scaling factor and we keep the aspect ratio unchanged.The encoder-decoder network is trained to adapt to Mathematical Expression of various scales in order to generate correct predictions.

4. Drop Attention

The normalised attention weight α α alpha\alpha has a great impact on recognition performance so when the attention neglects the key features, the model will generate an incorrect prediction which will result in worse performance.From [4] a different idea can be proposed that is the Drop Attention Module in the decoder.First, we randomly supress the features where the α α alpha\alpha is the highest.Then we randomly abandon spots according to equation 10.
Variables and their meanings
  • γ γ gamma\gamma - Supress Factor
  • r p , r s r p , r s r_(p),r_(s)r_p,r_s - Random Variables that obey Bernoulli Distribution
  • f l f l f_(l)^(')f_l^{\prime} - features (replaces f l f l f_(l)f_l in equation 2 and 8 in training phase to predict the correct symbol when the attention is imprecise)
(10) f l = { γ f l , if l = argmax ( α l ) and r p = 0 0 , if l argmax ( α l ) and r s = 0 f l , otherwise (10) f l = γ f l ,  if  l = argmax α l  and  r p = 0 0 ,  if  l argmax α l  and  r s = 0 f l ,  otherwise  {:(10)f_(l)^(')={[gamma*f_(l)","," if "l=argmax(alpha_(l))" and "r_(p)=0],[0","," if "l!=argmax(alpha_(l))" and "r_(s)=0],[f_(l)","," otherwise "]:}:}\begin{equation} f_{l}^{\prime}=\left\{\begin{array}{cc} \gamma \cdot f_{l}, & \text { if } l=\operatorname{argmax}\left(\alpha_{l}\right) \text { and } r_{p}=0 \\ 0, & \text { if } l \neq \operatorname{argmax}\left(\alpha_{l}\right) \text { and } r_{s}=0 \\ f_{l}, & \text { otherwise } \end{array}\right. \end{equation}

5. Experiment and Implementation

The proposed model was implemented using PyTorch and optimized on Nvidia TITAN X p X p X_(p)X_p Graphical Processing Unit.For simultaneous calculations to be carried out at the same time the batch size was set to 8.The Mathematical Expression images underwent scale augmentation in the training phase and were zero padded to a fixed size 256 × 1024 256 × 1024 256 xx1024256 \times 1024.Few of the mathematical expression were downsized because they were larger than the size before zero padding.The scaling factor(equation 9) k was set randomly within the bound [ 0.5 , 2 ] [ 0.5 , 2 ] [0.5,2][0.5,2] during each training iteration.Using CNN the features were extracted and fed to the attention based decoder with configurations according to Table I.The suppress factor γ γ gamma\gamma and the Bernoulli probabilities r p , r s r p , r s r_(p),r_(s)r_p,r_s(equation 10) during the training phase were set to 0.1 , 0.8 and 0.4 0.1 , 0.8  and  0.4 0.1,0.8" and "0.40.1,0.8\text{ and }0.4 respectively.The cross-entropy loss was calculated between the generated symbols and the ground truth.Adam optimizer was used to train the model, and the learning rate was 0.0001 0.0001 0.00010.0001.

5.1. Effect of Scale Augmentation

Comparative experiments for processing Mathematical Expression images were performed without applying the drop attention module.The three methods used are as follows :
  • Normalization to a fixed height of 256 without changing the aspect ratios.
  • Zero Padding to a fixed size.
  • Scale Augmentation
The expression recognition rate for the above mentioned methods is shown in Table II.
(Expression Recognition Rate % of different processing methods for Mathematical Expression Images)
Method CROHME2014 CROHME2016 Normalized to fixed height 48.78 49.26 Zero - padded to fixed size 50.00 47.95 Scale Augmentation 55.17 52.48  Method   CROHME2014   CROHME2016   Normalized to fixed height  48.78 49.26  Zero - padded to fixed size  50.00 47.95  Scale Augmentation  55.17 52.48 (()/([" Method "," CROHME2014 "," CROHME2016 "],[" Normalized to fixed height ",48.78,49.26],[" Zero - padded to fixed size ",50.00,47.95],[" Scale Augmentation ",55.17,52.48],[hline]))\begin{array}{ccc} \hline \text { Method } & \text { CROHME2014 } & \text { CROHME2016 } \\ \hline \text { Normalized to fixed height } & 48.78 & 49.26 \\ \text { Zero - padded to fixed size } & 50.00 & 47.95 \\ \text { Scale Augmentation } & 55.17 & 52.48 \\ \hline \end{array}
Table II[5]
It can be seen from Table II,the results with a fixed height normalization are slightly better than zero padding on the CHROME 2016 data set but worse on CHROME 2014.Whereas Scale Augmentation outperforms the other two methods which shows that Scale Augmentation is more effective than the other two methods.The model trained with Scale Augmentation could extract par subsection{Effect of Drop Attention} Comparative experiments were performed with and without the Drop Attention to verify its effectiveness.The results are shown in Table III.
(Expression Recognition Rate % of processing methods with or without Drop Attention)
Drop Attention CROHME 2014 CROHME 2016 without 55.17 52.48 with 56.59 54.58  Drop Attention   CROHME 2014   CROHME 2016  without 55.17 52.48 with 56.59 54.58 (()/([" Drop Attention "," CROHME 2014 "," CROHME 2016 "],["without",55.17,52.48],["with",56.59,54.58],[hline]))\begin{array}{ccc} \hline \text { Drop Attention } & \text { CROHME 2014 } & \text { CROHME 2016 } \\ \hline \text{without} & 55.17 & 52.48 \\ \text{with} & 56.59 & 54.58 \\ \hline \end{array}
Table III[5]
As shown above in Table III when the experiment was performed with the Drop Attention Module, the accuracy improves by 1.42% on CROHME 2016 and 2.1% on CROHME 2014.The drop attention module trains model generating better predictions when attention neglects the key features.The Drop Attention model was implemented during the training phase of subsequent experiments to improve the performance of the proposed model.

5.2. Comparison with proposed methods

The graphs below shows the expression recognition rate(ExpRate) taken out using recent offline attention based Hand Written Mathematical Expression models on CROHME 2014 and CROHME 2016 datasets. Among all the Hand Written Mathematical Expression models, the Scale Augmentation and Drop Attention model (SADA[5]) achieves the best performing results.

6. Conclusion

In this paper Scale Augmentation method is used to solve the problem of recognition of Mathematical Expression of various scales. The problem is caused due to its two dimensional structure.A new Drop Attention module is used to enhance the training of the model which helps to generate better results when the attention is imprecise.The experimental results indicated that these two technologies assist our method achieving state-of-the-art performance, compared with the previous methods without an ensemble.

7. References

  1. K. He, X. Zhang, S. Ren, and J. Sun, “Deep residual learning for image recognition,” in Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVRP), pp. 770–778, 2016
  2. J.-W. Wu, F. Yin, Y.-M. Zhang, X.-Y. Zhang, and C.-L. Liu, “Handwrit- ten mathematical expression recognition via paired adversarial learning,” International Journal of Computer Vision, pp. 1–16, 2020.
  3. J. Zhang, J. Du, S. Zhang, D. Liu, Y. Hu, J. Hu, S. Wei, and L. Dai, “Watch, attend and parse: An end-to-end neural network based approach to handwritten mathematical expression recognition,” Pattern Recognition, vol. 71, pp. 196–206, 2017.
  4. G. Sun, H. Cholakkal, S. Khan, F. S. Khan, and L. Shao, “Fine-grained recognition: Accounting for subtle differences between similar classes,” arXiv preprint arXiv:1912.06842, 2019.
  5. Z. Li, L. Jin, S. Lai and Y. Zhu, "Improving Attention-Based Handwritten Mathematical Expression Recognition with Scale Augmentation and Drop Attention," 2020 17th International Conference on Frontiers in Handwriting Recognition (ICFHR), Dortmund, Germany, 2020, pp. 175-180, doi: 10.1109/ICFHR2020.2020.00041.
  6. H. Mouchere, C. Viard-Gaudin, R. Zanibbi, and U. Garain, “Icfhr 2014 competition on recognition of on-line handwritten mathematical expressions (crohme 2014),” in 2014 14th International Conference on Frontiers in Handwriting Recognition (ICFHR), pp. 791–796, IEEE, 2014.
  7. A. D. Le and M. Nakagawa, “Training an end-to-end system for handwritten mathematical expression recognition by generated patterns,” in 2017 14th IAPR International Conference on Document Analysis and Recognition (ICDAR), vol. 1, pp. 1056–1061, IEEE, 2017.
  8. J.-W. Wu, F. Yin, Y.-M. Zhang, X.-Y. Zhang, and C.-L. Liu, “Image- to-markup generation via paired adversarial learning,” in Joint Euro- pean Conference on Machine Learning and Knowledge Discovery in Databases, pp. 18–34, Springer, 2018.
  9. A.D.Le,B.Indurkhya,andM.Nakagawa,“Patterngenerationstrategies for improving recognition of handwritten mathematical expressions,” Pattern Recognition Letters, vol. 128, pp. 255–262, 2019.
  10. Z. Xie, Z. Sun, L. Jin, H. Ni, and T. Lyons, “Learning spatial-semantic context with fully convolutional recurrent network for online handwritten chinese text recognition,” IEEE Transactions on Pattern Analysis and Machine Intelligence, vol. 40, no. 8, pp. 1903–1917, 2017.
  11. H. Mouche`re, C. Viard-Gaudin, R. Zanibbi, and U. Garain, “Icfhr2016 crohme: Competition on recognition of online handwritten mathematical expressions,” in 2016 15th International Conference on Frontiers in Handwriting Recognition (ICFHR), pp. 607–612, IEEE, 2016.
  12. Y.Deng,A.Kanervisto,andA.M.Rush,“Whatyougetiswhatyousee: A visual markup decompiler,” arXiv preprint arXiv:1609.04938, vol. 10, pp. 32–37, 2016.
  13. J. Zhang, J. Du, and L. Dai, “Multi-scale attention with dense encoder for handwritten mathematical expression recognition,” in 2018 24th International Conference on Pattern Recognition (ICPR), pp. 2245– 2250, IEEE, 2018.

Recommended for you

Emil Junker
Manipulative Attacks in Group Identification
Manipulative Attacks in Group Identification
This review provides an introduction to the group identification problem and gives an overview of the feasibility and computational complexity of manipulative attacks in group identification.
2 points
0 issues