# EnCaps: Clothing Image Classification Based on Enhanced Capsule Network
## 2 Materials and Methods
### Spatial Structure Extraction  
Convolution is locally connected and parameter sharing, and as the layers of a convolutional network deepen, the network can learn more global contextual information and then use this information to make predictions. However, there is no available spatial information in the extracted features, which is one of the reasons for the prediction failure. First, the shape information of the extracted feature is important for object identification. The different types of clothing image have obvious structural features that can be used to classify. There are about seven common types of clothing profiles, such as 'A', 'H', 'X', 'T', 'Y', 'O', and 'V'. The feature of the clothing profile should enhance the classification accuracy. Thus, the spatial shape information is important for clothing classification. However, it is not enough to have profile information. The traditional convolutional neural network does not have the ability to analyze spatial information, which makes it difficult to distinguish between the two types of clothing with only slight differences. For example, skirt and dress, which are also in the shape of 'A', are extremely easy to be confused by the network due to no spatial information (localized spatial alignment information for clothing and body) being identified. To complement the capability of spatial feature extraction, the capsule network [21] is introduced to process the features further. The capsule network extracts the structural features of objectives based on the capsule unit. The traditional capsule network is shown in Figure 2. The fundamental structure of the capsule network includes convolution layer, initial capsule layer, convolution capsule layer, and fully connected layer. Different from the traditional CNN, the feature vector \(v_{i}\) of objective replaces the scalar feature. The \"prediction vectors\" \(\hat{u}_{j|i}\) from the capsules is obtained by Equation (1):  
\[\hat{u}_{j|i}=W_{ij}u_{i} \tag{1}\]  
where \(W_{ij}\) is the weight matrix of a certain capsule layer. \(i\) denotes the vector index of input capsules layer, and \(j|i\) denotes the capsules index of PrimaryCapsules \(j\) corresponding to vector \(i\). Figure 1: Overview of the proposed EnCaps. The network architecture consists of three main parts: (1) Parameter Optimization; (2) spatial structure Extraction; and (3) spatial structure Extraction. Then, the high level feature \(\hat{u}_{j|i}\) is processed by Equation (2) to realize the dynamic routing of features:  
\[s_{j}=\sum_{i}c_{ij}\hat{u}_{j|i} \tag{2}\]  
where the parameter \(c_{ij}\) indicates the routing probability from capsule \(i\) of \(L\) layer to capsule \(j\) of \(L+1\). The computational formula of routing probability is represented by Equation (3):  
\[c_{ij}=\frac{\exp(b_{ij})}{\sum_{j}\exp(b_{ij})} \tag{3}\]  
where \(b_{ij}\) is the prior probability from capsule \(i\) to capsule \(j\), which is iteratively updated in the model training, and the initial value is 0. Then, the parameter \(s_{j}\) is processed by the squashing function (Equation (4)) to obtain the \(L+1\) layer capsule:  
\[v_{j}=\frac{||s_{j}||^{2}}{1+||s_{j}||^{2}}\frac{s_{j}}{||s_{j}||} \tag{4}\]  
Since capsule network allows multiple classifications to co-exist, the traditional cross-entropy loss can not be used directly; an alternative is the margin loss commonly used in SVM. The capsule mechanism is used at the end of EnCaps, and the use of margin loss is also a way to maintain the performance of the model. It can be expressed as Equation (5):  
\[L_{c}=T_{c}max\big{(}0,m^{+}-||v_{c}||\big{)}^{2}+\lambda(1-T_{c})max\big{(} 0,||v_{c}||-m^{-}\big{)}^{2} \tag{5}\]  
where \(c\) is a certain classification, \(T_{c}\) is the indicator for the classification ('1' indicates the presence of class \(c\), '0' indicates the absence of class \(c\)), and \(m^{+}\) is the upper bound that is predicting the existence of class \(c\) but not its true existence.