Imbalanced Malware Images Classification: a CNN based Approach
Songqing Yue Tianyang Wang








Imbalanced Malware Images Classification: a CNN based Approach
    Songqing Yue 
University of Wisconsin

yues@uwplatt.edu

Tianyang Wang 
Austin Peay State University

toseattle@siu.edu

    December 30, 2023
============================================================================================================================





Deep convolutional neural networks (CNNs) can be applied to malware binary detection via image classification. The performance, however, is degraded due to the imbalance of malware families (classes). To mitigate this issue, we propose a simple yet effective weighted softmax loss which can be employed as the final layer of deep CNNs. The original softmax loss is weighted, and the weight value can be determined according to class size. A scaling parameter is also included in computing the weight. Proper selection of this parameter is studied and an empirical option is suggested. The weighted loss aims at alleviating the impact of data imbalance in an end-to-end learning fashion. To validate the efficacy, we deploy the proposed weighted loss in a pre-trained deep CNN model and fine-tune it to achieve promising results on malware images classification. Extensive experiments also demonstrate that the new loss function can well fit other typical CNNs, yielding an improved classification performance.









§ INTRODUCTION



Malware binary, usually with a file name extension of “.exe" or “.bin",  is a malicious program that could harm computer operating systems. Sometimes, it may have many variations with highly reused basic patterns. This implies that malware binaries could be categorized into multiple families (classes), and each variation inherits the characteristics of its own family. Therefore, it is important to effectively detect malware binary and recognize possible variations <cit.>.



This is non-trivial but challenging. A malware binary file can be visualized to a digital gray image <cit.>. After visualization, the malware binary detection turns into a multi-class image classification problem, which has been well studied in deep learning. 
One can manually extract features from malware images and feed them into classifiers such as SVM (support vector machine) or KNN (k-nearest neighbors algorithm) to detect malware binaries via classification. To be more discriminative, one can utilize CNN to automatically extract features as Razavian et al. did in <cit.> and perform classification in an end-to-end fashion. However, most deep CNNs are trained by properly solicited balanced data <cit.>, while malware image datasets may be highly imbalanced: some malware has many variations while some other only has few variations. For instance, the dataset <cit.> used in our paper consists of 25 classes, and some class includes more than 2000 images while some other only has 80 images or so. As a result,
the reputed pre-trained CNN models <cit.> may still perform poorly in this scenario. Furthermore, pre-trained CNN models are originally designed for specific vision tasks, and they cannot be applied to malware binary detection directly. 

One may argue that data augmentation could be a possible approach to balance data, such as oversampling minority classes and/or down-sampling majority classes. It is, however, not suitable for our problem due to two reasons. First, down-sampling may result in a loss of many representative malware variations. Second, simply augmenting data cannot guarantee to generate images corresponding to real malware binaries. To address the challenges, we investigate how to train a CNN model with the imbalanced data on hand. 


Inspired by the work in <cit.> which designed new loss functions for improving CNN training, we propose a weighted softmax loss for deep CNNs on malware images classification. Based on the error rate given by the softmax loss, we weight misclassifications by different values corresponding to class size. Intuitively, misclassifications in minority classes should be amplified, and that in majority classes need to be suppressed. Our weighted loss can achieve this goal and guide the model weights updating in a proper direction. We adopt a pre-trained VGG-19 model <cit.>, and retrain it to achieve promising results on the malware image classification. Once the proposed loss has been demonstrated effective on the VGG model, it can be extended to other models, such as GoogleNet <cit.> and ResNet <cit.>. 

The contributions of this work can be summarized into two-fold. First, we propose a weighted softmax loss to help CNNs deal with imbalanced data. Second, we apply the proposed loss to address the challenges in malware image classification. 





§ RELATED WORK



Malware Images: Nataraj et al. <cit.> proposed a way of converting malware binaries into digital gray images. They created 25 classes of malware images that are highly imbalanced. Two sample malware images from the ‘Adialer.C’ class and ‘Skintrim.N’ class are shown in Fig. <ref>. The Microsoft Malware Classification Challenge[https://www.kaggle.com/c/malware-classification] also provides imbalanced data which includes 9 classes only. Therefore, it is not as challenging as the one in <cit.> in terms of malware diversity. 



Deep Learning: Deep learning recently gains a remarkable success in computer vision. Krizhevsky et al. in <cit.> opened a new era for deep CNNs and their applications on image classification. Many works have been inspired thereafter. Simonyan and Zisserman proposed a very deep CNN in <cit.>, and they significantly improved the performance by increasing the depth and using small convolutional filters with a size of 3×3. Szegedy et al. <cit.> designed a 22 layers GoogLeNet which increased the width of the network, and achieved the new state-of-the-art performance on image classification. He et al. <cit.> eased the training of a 152 layers deep CNN by presenting a residual learning scheme. 
Besides image classification, deep learning has also been applied on fundamental image processing tasks, such as denoising <cit.> and contour detection <cit.>.  
Saxe and Berlin <cit.> discussed the feasibility of using deep CNNs on malware detection. Hand-crafted features such as ‘PE import’ are extracted prior to the model training, which is not the typical manner of an end-to-end learning process. 
Huang et al. <cit.> proposed a quintuplet based triple header hinge loss for extracting discriminative features from imbalanced data. Nevertheless, their work requires extracting features in advance in a hand-crafted manner or using another pre-trained CNN model. Meanwhile, clustering algorithms such as k-means are needed prior to the training. These steps have not been integrated into an end-to-end fashion, employed by typical CNNs. 
Recently, Xu et al. <cit.> have applied deep learning in medical CT physics, and perform scatter correction via designing a new loss function.



§ WEIGHTED SOFTMAX LOSS



In this section, we introduce how to weight softmax loss according to class size. 



 §.§ Softmax Loss


Softmax loss is a combination of softmax regression and entropy loss, used in multi-class classification problems. Given a K-classes training set including m images: {x^(i), y^(i)}, where x^(i) refers to an image, and y^(i) denotes the ground truth label (y^(i)∈{1,2,…,K}). Let a_j^(i) (j=1,2,…,K) be the output unit from the last fully connected layer of a CNN, then the probability that the label of x^(i) is j can be given by


    P_j^(i)=exp(a_j^i)/∑_l=0^K exp(a_l^(i)).
 Typical deep CNNs aim to minimize the entropy loss function: 


    J_0=-1/m{∑_i=1^m ∑_j=0^K 1(y(i)=j) log p_j^(i)},

where m is the batchsize, and K is the total number of the classes in a dataset. 1(.) is an indicator function such that, 1(true) gives 1, and 1(false) gives 0. In typical CNNs, convolutional filters are updated by the stochastic gradient descent (SGD) algorithm. The traditional softmax loss equally treats the misclassifications in each class, which is reasonable for balanced data, but will lead to a poor performance on imbalanced data, such as malware images. Our weighted softmax loss is proposed to address this issue. 



 §.§ Weighted Softmax Loss



Our approach can be formulated as follows, 


    J_0=-1/m{∑_i=1^m ∑_j=0^K ω_k *1(y(i)=j) log p_j^(i)},

where ω_k is a weight coefficient determined by 


    ω_k=1+S_max-S_k/β*S_max,
  
where k is the ground truth label of the i^th image. S_max is the size of the largest class in the dataset, and S_k is the size of an arbitrary class k in the dataset, and β is a parameter that controls the scaling of the weighted loss. Our empirical preference of β is 20. Extensive experiments about this parameter will be shown in section <ref>. It is worth noting that the minority classes will be assigned a larger weight coefficient whereas the majority classes will be lightly weighted. The weight coefficient will not dramatically affect the loss, thus it can be regarded as a subtle fine-tuning, that boosts the classification performance as well as avoids overfitting. In practice, the training procedure only needs to choose a ω_k according to the ground truth label of the i^th sample in a mini-batch. 



§ CLASSIFICATION WITH CNN



In this section, we discuss how to fine-tune the VGG-19 model for malware image classification.   



 §.§ Network Architecture


Simonyan and Zisserman in <cit.> proved that classification accuracy can be improved via deepening the network. The VGG-F model <cit.> consists of 21 layers, whereas VGG-19 includes 43 layers. Convolutional filters with a size of 3×3 are used in VGG-19, and such smaller filters have been demonstrated being capable of extracting discriminative features. We fine-tune VGG-19 since it has been shown very competitive in the VGG family. 

Simonyan and Zisserman <cit.> argued that the local response normalization (LRN) <cit.> could be ignored in CNNs. In order to boost the classification performance, we add Batch Normalization (BN) layer <cit.> between convolutional layer and ReLU (rectified linear unit) activation. However, the very first convolutional layer is followed by a ReLU layer directly, and the fully connected layer (expect the last one) is directly followed by a ReLU layer. 

A big potential threat to deep learning is overfitting, especially when a small training set is used. Srivastava et al. <cit.> invented dropout, a simple yet powerful approach to avoid overfitting in CNNs. Units in layers are randomly dropped and the corresponding connections are also removed temporarily. It works as randomly training different networks and averaging the results as the final one, aiming to enhance the generalization. We place two dropout layers (with probability =0.5) between the three fully connected layers to prevent overfitting (by default, the pre-trained VGG-19 does not have dropout layers). In testing stage, the dropout layers will be removed. 



 §.§ Weighted Softmax Loss Layer


We append the proposed loss as the last layer of our model. The final structure consists of 60 layers including the added dropout layers and BN layers. For brevity, only the fully connected layers, the added dropout layers, and the weighted loss layer are illustrated in Fig. <ref>. 






§ EXPERIMENTS



In this section, we validate the proposed loss on malware image classification. The VGG-F,M,S models <cit.> are also employed to validate the general fit of the new loss function. We also analyze the selection of the scaling parameter and suggest an empirical option. The Top-1 validation error is adopted as the evaluation metric. 

We conduct the experiments with the MatConvNet[http://www.vlfeat.org/matconvnet/] framework <cit.>, which is an open source library for deep learning in matlab. One Nvidia TITAN X GPU is used to accelerate the mini-batch processing. 



 §.§ Dataset and Experimental Settings


The dataset <cit.> used in our work consists of 25 classes which are highly imbalanced. The name and the size of each class is listed in Table <ref>[http://old.vision.ece.ucsb.edu/spam/malimg.shtml]. We partition the data as follows: the first 60% images in each class are used for training, and the following 20% for validation, and the last 20% for testing. No data augmentation is applied, but we pre-process an input by subtracting its mean.    








 §.§ Effects of the Weighted Loss

To validate the general fit, we deploy the proposed loss in 3 pre-trained CNN models, namely VGG-F, VGG-M, and VGG-S, respectively. The default structures are entirely preserved but the size of the last fully connected layer is changed from 1000 to 25, corresponding to the 25 classes in the malware dataset. We retrain the three networks, and the top-1 validation errors with and without the weighted loss are illustrated in Fig. <ref>. It can be seen that our method effectively decreases the top-1 validation error, and keeps the curves stable. The test results are given in Table <ref>. 










 



 §.§ Fine-tuning VGG-19 with the Weighted Loss


To achieve a better performance, we fine-tune VGG-19 as discussed in section <ref>. We initialize the filter weights with MSRA <cit.>. The momentum is set to 0.9, and the training is regularized by a weight decay rate of 0.0005. The learning rate is set to 0.0001, which we find leads to the best performance. Dynamic adjusting is not considered since our data size does not require many epochs to make the training converge. Batch size is set to 80. We compare the top-1 validation error of the models with and without the weighted loss. As shown in Fig. <ref>, the weighted loss more effectively decreases the error compared to the original loss. During testing, we remove the two added dropout layers, and the results are presented in Table 2. In the dataset, the ‘Autorun.K’, ‘Malex.gen!J’, ‘Rbot!gen’, ‘VB.AT’, and ‘Yuner.A’ classes are from the same pack (UPX-Ultimate Packer for eXecutables). However, we treat them as different malware families. Otherwise, the test error can be further decreased. 



Moreover, CNNs can extract image features automatically. In our fine-tuned model, a 4096-dimensional feature vector for an arbitrary image can be captured at layer 41 (the second fully connected layer). We generate a feature map for each malware class by extracting the features for all the images in that class and combining all the feature vectors to form a matrix, which is finally visualized. Therefore, the dimension of any feature map is n×4096, where n is the class size. Such a feature map can reflect the characteristics of the corresponding malware class. We give the feature maps of five typical malware classes in Fig. <ref>. Since the classes are highly imbalanced, n is different for each class. To attain the best display effect, we use the `imagesc` command in matlab to visualize a feature map, automatically scaling the map. 




















  
















 













 §.§ Scaling Parameter




In Eq. (4), the parameter β is needed to compute the weighted loss value. Empirically, 20 is the best option for β. To show the influence of this parameter, we further investigate three β values on the fine-tuned VGG-19 and report the top-1 validation error in Fig. <ref>. As can be seen, the curve with β=20 is smoother than the others and it also converges at a lower error rate. It indicates that an appropriate selection of β is also critical to the network training. 
 
 


§ CONCLUSION



We propose a weighted softmax loss for convolutional neural networks on imbalanced malware image classification. By imposing a weight, the classification error for different classes can be treated unequally. The principle of weighting the loss has a clear intuition, and our experiments demonstrate its capacity of working with existing CNN models. We also fine-tune the pre-trained VGG-19 with the proposed loss to achieve a satisfactory classification performance. In addition, we have empirically suggested an option of the scaling parameter β in computing the weighted loss. It indicates that an appropriate selection of this parameter is indispensable for the training success. 
 




  
  unsrt
   

