# Intelligent Classification of Japonica Rice Growth Duration (GD) Based on CapsNets
## 2 Materials and Methods
### Evaluation Indices of CapsNets Model  
The goal of this paper was to investigate and design a CapsNets model for classifying GD of japonica rice as accurately as possible. The realization of the CapsNets model is divided into three layers: initial layer (Conv1), primary capsules layer, and final CapsLayer (Figure 3).  
As shown in Figure 3, Conv1 is a normal convolutional layer, which has 256 output channels and 9 \(\times\) 9 convolution kernels with a stride of 1 and ReLU activation. This layer extracts low-level features that are then used as inputs to the primary capsules, a convolutional capsule layer. This convolutional layer has 256 output channels and 9 \(\times\) 9 convolution kernels with a stride of 2. The outputs are segmented into [32 \(\times\) 8] vectors (primary capsules). Therefore, each primary capsule output sees the outputs of all 256 \(\times\) 9 \(\times\) 9 Conv1 units, which is a group 8D vectors in the 6 \(\times\) 6 grid. The primary capsules contain advanced features. The length of the output vector of a capsule represents the probability that the entity represented by the capsule is present in the current input. The output capsule is computed using a nonlinear squashing function (Equation (2)) to ensure that the length of the vector is between 0 and 1. The third layer (final CapsLayer) has one capsule per class, and each of these capsules receives input from all the capsules in the layer below.  
\[v_{j}=\frac{\left\|s_{j}\right\|^{2}}{1+\left\|s_{j}\right\|^{2}}\frac{s_{j}} {\left\|s_{j}\right\|} \tag{2}\]  
Note: \(v_{j}\) is the vector output of capsule \(j\), and \(s_{j}\) (Equation (3)) is the input vector of capsule \(j\).  
\[s_{j}=\sum_{i}c_{ij}\hat{a}_{\langle j|i\rangle} \tag{3}\]  
Note: \(c_{ij}\) (Equation (6)) are coupling coefficients. \(\hat{a}_{\langle j|i\rangle}\) (Equation (5)) is the prediction vector of the output of capsule \(j\) at a higher level computed by capsule \(i\) in the layer below.  
Agreement routing is used between primary capsules and final CapsLayer. Agreement \(a_{ij}\) Equation (4) for updating log probabilities and coupling coefficients is calculated (Equation (6)).  
\[a_{ij}=v_{j}\hat{a}_{\langle j|i\rangle} \tag{4}\]  
Note: \(v_{j}\) is the vector output of capsule \(j\). \(\hat{a}_{\langle j|i\rangle}\) is calculated.  
\[\hat{a}_{\langle j|i\rangle}=W_{ij}u_{i} \tag{5}\]  
Note: \(u_{i}\) is the output of capsule \(i\) in the layer below. \(W_{ij}\) is a weight matrix between each \(u_{i}\) in primary capsules and \(v_{j}\), \(j\in(1,3)\), which needs to be learned in the back pass. Coupling coefficients are calculated as follows:  
\[c_{ij}=\frac{\exp(b_{ij})}{\sum_{k}\exp(b_{ij})} \tag{6}\]  
Figure 3: Schematic of the CapsLayer model.  
Note: \(c_{ij}\) are coupling coefficients. \(b_{ij}\) is the log probability that capsule \(i\) will be coupled with capsule \(j\) and it is initially set to 0 at the beginning of the routing by agreement process. \(c_{ij}\) is calculated from \(b_{ij}\) using the softmax function.  
The final CapsLayer work can be simply summarized into four steps: I, matrix transformation; II, input weighting (Equation (5)); III, weighted sum (Equations (3)-(6)); IV, nonlinear transformation (Equation (2)). We use the Python-based pytorch (a deep learning algorithm framework) to build the CapsNets to complete the experiment.