# Machine Learning-Based Highway Truck Commodity Classification Using Logo Data
## 3 Methodology
### Image-Based Logo Detection and Recognition
#### 3.2.3 Feature-Matching Logo Recognizer  
In this approach, we treated logo recognition as an image retrieval problem with a few sample images for each class. We collected the gallery images from the Internet, which included roughly 30 images per logo class. The gallery images are used as the templates against which all logo predictions are matched. Two sets of features were extracted to represent logo images, namely deep metric features and the BoW (Bag of Visual Words) features. The deep metric features draw upon recent advances in deep metric learning and have the capability of extracting high-level discriminative semantic information for similarity measurements. The BoW features instead extract low-level image information such as textures, corners, and edges for logo matching. The BoW features are commonly used in image retrieval for research problems and industrial applications. After extracting these two types of image feature representations, we combined them to obtain the fused features. _Deep Metric Features_. Inspired by [14], we trained a DCNN (deep convolutional neural network) classifier on the QMUL-OpenLogo dataset and used the output features of the second-to-last layer as the feature representation for a given logo image. We did not train the classifier directly on the logo classes of interest, considering that new unseen logo classes might be added. Instead, we aim at projecting logo images into a feature space such that logos from different classes are separable. Therefore, we seek to increase the inter-class distances of logo features, bringing us to the popular deep metric learning (DML). The core concept of DML is to find a good representation of images with a good metric for similarity measurement. To measure the similarity of two feature vectors, we simply choose to use the common cosine similarity because it is bounded and invariant to feature magnitude; its formula is shown as follows:  
\[\cos(\textbf{x}_{i},\textbf{z}_{j})=\frac{\textbf{x}_{i}\cdot\textbf{x}_{j}}{ \||\textbf{x}_{i}||\cdot\||\textbf{x}_{j}||}, \tag{1}\]  
where \(\textbf{x}_{i}\) and \(\textbf{x}_{j}\) are the feature vectors extracted from image \(i\) and \(j\) using the DML model. If the score of cosine similarity is close to 1, it means these two feature vectors are likely to come from the same logo class, otherwise not. Formally, pairs are called positive pairs if they have the same label; otherwise, they are called negative pairs. To make logo features separable, we prefer a trained model that assigns higher similarity to positive pairs and lower similarity to negative pairs. We chose to use binomial deviance [37] as the loss function with the formula:  
\[L=\sum_{i,j}[\frac{1}{P_{i}}\sum_{\textbf{x}_{i}=\textbf{x}_{j}} \log[1+e^{a(\lambda-S_{ij})}]+\] \[\frac{1}{N_{i}}\sum_{\textbf{x}_{i}\
eq\textbf{x}_{j}}\log[1+e^{ \beta(S_{ij}-\lambda)}] \tag{2}\]  
where \(P_{i}\) and \(N_{i}\) denote the count of positive pairs and negative pairs related to \(\textbf{x}_{i}\), respectively. \(s_{ij}\) denotes the similarity of pair \((\textbf{x}_{i},\textbf{x}_{j})\). \(\alpha\), \(\beta\), and \(\lambda\) are hyperparameters, which are chosen based on the best heuristic setting of [14]. The hyperparameters \(\alpha\) and \(\beta\) were set to be 40 and 0, respectively. The hyperparameter \(\lambda\) was set to be 0.5. The loss function lays more emphasis on hard samples where positive pairs get low scores or negative pairs get high scores, which enforces the model to find more discriminative features. The deep metric learning model was implemented in PyTorch. We used the Inception network [38] as the backbone with a global pooling layer and a fully connected layer added on top of it. The network was trained in a pairwise way by gradient descent using the Adam optimizer [39]. Figure 4: Sample outputs from the developed approaches.