{'fraction_non_alphanumeric': 0.04127058224410117, 'fraction_numerical': 0.014046851128840604, 'mean_word_length': 4.605923635066016, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Cost Sensitive Learning of Deep Feature Representations from Imbalanced Data

Class imbalance is a common problem in the case of real-world object detection and classification tasks. Data of some classes is abundant making them an over-represented majority, and data of other classes is scarce, making them an under-represented minority. This imbalance makes it challenging for a classifier to appropriately learn the discriminating boundaries of the majority and minority classes. In this work, we propose a cost sensitive deep neural network which can automatically learn robust feature representations for both the majority and minority classes. During training, our learning procedure jointly optimizes the class dependent costs and the neural network parameters. The proposed approach is applicable to both binary and multi-class problems without any modification. Moreover, as opposed to data level approaches, we do not alter the original data distribution which results in a lower computational cost during the training process. We report the results of our experiments on six major image classification datasets and show that the proposed approach significantly outperforms the baseline algorithms. Comparisons with popular data sampling techniques and cost sensitive classifiers demonstrate the superior performance of our proposed method.


I. INTRODUCTION
In most real-world classification problems, the collected data follows a long tail distribution i.e., data for few object classes is abundant while data for others is scarce. This behaviour is termed the 'class-imbalance problem' and it is inherently manifested in nearly all of the collected image classification databases (e.g., Fig. 1). A multi-class dataset is said to be 'imbalanced' or 'skewed' if some of its (minority) classes, in the training set, are heavily under-represented compared to other (majority) classes. This skewed distribution of class instances forces the classification algorithms to be biased towards the majority classes. As a result, the characteristics of the minority classes are not adequately learned.
The class imbalance problem is of particular interest in real-world scenarios, where it is essential to correctly classify examples from an infrequent but important minority class. For instance, a particular cancerous lesion (e.g., a melanoma) which appears rarely during dermoscopy should not be misclassified as benign (see Sec. IV). Similarly, for a continuous surveillance task, a dangerous activity which occurs occasionally should still be detected by the monitoring system. The same applies to many other application domains, e.g., object classification, where the correct classification of a minority class sample is equally important to the correct classification of a majority class sample. It is therefore required to enhance the overall accuracy of the system without unduly sacrificing the precision of any of the majority or minority classes. Most of the classification algorithms try to minimize the overall classification error during the training process. They, therefore, implicitly assign an identical misclassification cost to all types of errors assuming their equivalent importance. As a result the classifier tends to correctly classify and favour the more frequent classes.
Despite the pertinence of the class imbalance problem to practical computer vision, there have been very few research works on this topic in the recent years. Class imbalance is avoided in nearly all competitive datasets during the evaluation and training procedures (see Fig. 1). For instance, for the case of the popular image classification datasets (such as CIFAR−10/100, ImageNet, Caltech−101/256, and MIT−67), efforts have been made by the collectors to ensure that, either all of the classes have a minimum representation with sufficient data, or that the experimental protocols are reshaped to use an equal number of images for all classes during the training and testing processes [1,2,3]. This approach is reasonable in the case of datasets with only few classes, which have an equal probability to appear in practical scenarios (e.g., digits in MNIST). However, with the increasing number of classes in the collected object datasets, it is becoming impractical to provide equal representations for all classes in the training and testing subsets. For example, for a fine-grained coral categorization dataset, endangered coral species have a significantly lower representation compared to the more abundant ones [4].
In this work, we propose to jointly learn robust feature representations and classifier parameters, under a cost-sensitive setting. This enables us to learn not only an improved classifier that deals with the class imbalance problem, but also to extract suitably adapted intermediate feature representations from a arXiv:1508.03422v3 [cs.CV] 23 Mar 2017 deep Convolutional Neural Network (CNN). In this manner, we directly modify the learning procedure to incorporate classdependent costs during training. In contrast, previous works (such as [5,6,7,8]) only readjust the training data distribution to learn better classifiers. Moreover, unlike the methods in e.g., [4,9], we do not use a handcrafted cost matrix whose design is based on expert judgement and turns into a tedious task for a large number of classes. In our case, the classdependent costs are automatically set using data statistics (e.g., data distribution and separability measures) during the learning procedure. Another major difference with existing techniques is that our class specific costs are only used during the training process and once the optimal CNN parameters are learnt, predictions can be made without any modification to the trained network. From this perspective, our approach can be understood as a perturbation method, which forces the training algorithm to learn more discriminative features. Nonetheless, it is clearly different from the common perturbation mechanisms used during training e.g., data distortions [10], corrupted features [11], affine transformations [12] and activation dropout [13].
Our contribution consists of the following: 1-We introduce cost-sensitive versions of three widely used loss functions for joint cost-sensitive learning of features and classifier parameters in the CNN (Sec. III-C). We also show that the improved loss functions have desirable properties such as classification calibration and guess-aversion. 2-We analyse the effect of these modified loss functions on the backpropagation algorithm by deriving relations for propagated gradients (Sec. III-E). 3-We propose an algorithm for joint alternate optimization of the network parameters and the classsensitive costs (Sec. III-D). The proposed algorithm can automatically work for both binary and multi-class classification problems. We also show that the introduction of class-sensitive costs does not significantly affect the training and testing time of the original network (Sec. IV). 4-The proposed approach has been extensively tested on six major classification datasets and has shown to outperform baseline procedures and stateof-the-art approaches (Sec. IV-D).
The remainder of this paper is organized as follows. We briefly discuss the related work in the next section. In Sec. III-A and III-B, we introduce our proposed approach and analyse the modified loss functions in Sec. III-C. The learning algorithm is then described in Sec. III-D and the CNN implementation details are provided in Sec. IV-C. Experiments and results are summarized in Sec. IV and the paper concludes in Sec. V.
II. RELATED WORK Previous research on the class imbalance problem has concentrated mainly on two levels: the data level and the algorithmic level [14]. Below, we briefly discuss the different research efforts that tackle the class imbalance problem.
Data level approaches manipulate the class representations in the original dataset by either over-sampling the minority classes or under-sampling the majority classes to make the resulting data distribution balanced [14]. However, these techniques change the original distribution of the data and consequently introduce drawbacks. While under-sampling can potentially lose useful information about the majority class data, over-sampling makes the training computationally burdensome by artificially increasing the size of the training set. Furthermore, over-sampling is prone to cause over-fitting, when exact copies of the minority class are replicated randomly [5,14].
To address the over-fitting problem, Chawla et al. [5] introduced a method, called SMOTE, to generate new instances by linear interpolation between closely lying minority class samples. These synthetically generated minority class instances may lie inside the convex hull of the majority class instances, a phenomenon known as over-generalization. Over the years, several variants of the SMOTE algorithm have been proposed to solve this problem [15]. For example, Borderline SMOTE [16] only over-samples the minority class samples which lie close to the class boundaries. Safe-level SMOTE [17] carefully generates synthetic samples in the so called saferegions, where the majority and minority class regions are not overlapping. The local neighborhood SMOTE [18] considers the neighboring majority class samples when generating synthetic minority class samples and reports a better performance compared to the former variants of SMOTE. The combination of under and over sampling procedures (e.g., [8,19,20]) to balance the training data have also shown to perform well. However, a drawback of these approaches is the increased computational cost that is required for data pre-processing and for the learning of a classification model.
Algorithm level approaches directly modify the learning procedure to improve the sensitivity of the classifier towards minority classes. Zhang et al. [7] first divided the data into smaller balanced subsets, followed by intelligent sampling and a cost-sensitive SVM learning to deal with the imbalance problem. A neuro-fuzzy modeling procedure was introduced in [21] to perform leave-one-out cross-validation on imbalanced datasets. A scaling kernel along-with the standard SVM was used in [22] to improve the generalization ability of learned classifiers for skewed datasets. Li et al. [23] gave more importance to the minority class samples by setting weights with Adaboost during the training of an extreme learning machine (ELM). An ensemble of soft-margin SVMs was formed via boosting to perform well on both majority and minority classes [24]. These previous works hint towards the use of distinct costs for different training examples to improve the performance of the learning algorithm. However, they do not address the class imbalance learning of CNNs, which have recently emerged as the most popular tool for supervised classification, recognition and segmentation problems in computer vision [12,22,25,26,27]. Furthermore, they are mostly limited to the binary class problems [24,28], do not perform joint feature and classifier learning and do not explore computer vision tasks which inherently have imbalanced class distributions. In the context of neural networks, Kukar and Kononenko [29] showed that the incorporation of costs in the error function improves performance. However, their costs are randomly chosen in multiple runs of the network and remain fixed during the learning process in each run. In contrast, this paper presents the first attempt to incorporate automatic costsensitive learning in deep neural networks for imbalanced data.
After the submission of this work for review, we note that a number of new approaches have recently been proposed to incorporate class-specific costs in the deep networks [30,31,32]. Chung et al. [30] proposed a new cost-sensitive loss function which replaces traditional soft-max with a regression loss. In contrast, this work extends the traditionally used cost-functions in CNN for the cost-sensitive setting. Wang et al. [31] and Raj et al. [32] proposed a loss function which gives equal importance to mistakes in the minority and majority classes. Different to these works, our method is more flexible because it automatically learns the balanced error function depending on the end problem.

A. Problem Formulation for Cost-Sensitive Classification
Let the cost ξ p,q be used to denote the misclassification cost of classifying an instance belonging to a class p into a different class q. The diagonal of ξ (i.e., ξ p,p , ∀p) represents the benefit or utility for a correct prediction. Given an input instance x and the cost matrix ξ , the classifier seeks to minimize the expected risk R(p|x), where p is the class prediction made by the classifier. The expected risk can be expressed as: where, P (q|x) is the posterior probability over all possible classes given an instance x. According to the Bayes decision theory, an ideal classifier will give a decision in favour of the class (p * ) with the minimum expected risk: where, X and D define the input and output spaces respectively. Since, P (q|x) cannot be found trivially, we make use of empirical distribution derived from the training data. Given a training dataset consisting of tuples comprising of data and label, we can define the empirical risk as follows: where, M is the total number of images, o (i) ∈ R N is the neural network output for the i th sample and (·) is the misclassification error (0 − 1 loss) or a surrogate loss function which is typically used during the classifier training. For the case of cost-insensitive 0 − 1 loss, (ξ , d (i) , o (i) ) = I(d (i) = o (i) ) and ξ is an N × N matrix, where ξ p,p = 0, and ξ p,q = 1, ∀p = q. Next, we briefly describe the properties of traditional used cost matrix ξ , before introducing the proposed cost matrix. Properties of the Cost Matrix ξ : Lemmas III.1 and III.2 describe the main properties of the cost matrix ξ . Their proof can be found in Appendix A (supplementary material).
Lemma III.1. Offsetting the columns of the cost matrix ξ by any constant 'c' does not affect the associated classification risk R.
For convenience, the utility vector (i.e., the diagonal of the cost matrix) for correct classification is usually set to zero with the help of the property from Lemma III.1. We also show next that even when the utility is not zero, it must satisfy the following condition: Lemma III.2. The cost of the true class should be less than the mean cost of all misclassifications.
The entries of a traditional cost matrix (defined according to the properties above) usually have the form of: Such cost matrix can potentially increase the corresponding loss to a large value. During the CNN training, this network loss can make the training process unstable and can lead to the non-convergence of the error function. This requires the introduction of an alternative cost matrix.

B. Our Proposed Cost Matrix
We propose a new cost matrix ξ, which is suitable for CNN training. The cost matrix ξ is used to modify the output of the last layer of a CNN (before the softmax and the loss layer) (Fig. 2). The resulting activations are then squashed between [0, 1] before the computation of the classification loss.
For the case of a CNN, the classification decision is made in favour of the class with the maximum classification score. During the training process, the classifier weights are modified in order to reshape the classifier confidences (class probabilities) such that the desired class has the maximum score and the other classes have a considerably lower score. However, since the less frequent classes are under-represented in the training set, we introduce new 'score-level costs' to encourage the correct classification of infrequent classes. Therefore the CNN outputs (o) are modified using the cost matrix (ξ) according to a function (F) as follows: where, y denotes the modified output, p is the desired class and F : R → R represents a function whose exact definition depends on the type of loss layer. As an example, for the case of cost-sensitive MSE loss, where • denotes the hadamard product. In Sec. III-C, we will discuss in detail the definition of F for different surrogate losses. Note that the score-level costs perturb the classifier confidences. Such perturbation allows the classifier to give more importance to the less frequent and difficult-to-separate classes.
Properties of the Proposed Cost Matrix ξ: Next, we discuss few properties (lemmas A.3 -A.6) of the newly introduced cost matrix ξ and its similarities/differences with the traditionally used cost matrix ξ (Sec. III-A). The proofs of below mentioned properties can be found in Appendix A (supplementary material): Lemma III.3. The cost matrix ξ for a cost-insensitive loss function is an all-ones matrix, 1 p×p , rather than a 1−I matrix, as in the case of the traditionally used cost matrix ξ .
Lemma III.5. The cost matrix ξ is defined such that all of its elements in are within the range (0, 1], i.e., ξ p,q ∈ (0, 1]. Lemma III.6. Offsetting the columns of the cost matrix ξ can lead to an equally probable guess point. The cost matrix ξ configured according to the properties described above (Lemma A.3 -A.6) neither excessively increases the CNN outputs activations, nor does it reduce them to zero output values. This enables a smooth training process allowing the model parameters to be correctly updated. In the following section, we analyse the implications of the newly introduced cost matrix ξ on the loss layer ( Fig. 2).

C. Cost-Sensitive Surrogate Losses
Our approach addresses the class imbalance problem during the training of CNNs. For this purpose, we introduce a costsensitive error function which can be expressed as the mean loss over the training set: where, the predicted output (y) of the penultimate layer (before the loss layer) is parameterized by θ (network weights and biases) and ξ (class sensitive costs), M is the total number of training examples, d ∈ {0, 1} 1×N is the desired output (s.t. n d n := 1) and N denotes the total number of neurons in the output layer. For conciseness, we will not explicitly mention the dependence of y on the parameters (θ, ξ) and only consider a single data instance in the discussion below. Note that the error is larger when the model performs poorly on the training set. The objective of the learning algorithm is to find the optimal parameters (θ * , ξ * ) which give the minimum possible cost E * (Eq. (4)). Therefore, the optimization objective is given by: The loss function (·) in Eq. (4) can be any suitable surrogate loss such as the Mean Square Error (MSE), Support Vector Machine (SVM) hinge loss or a Cross Entropy (CE) loss (also called the 'soft-max log loss'). These popular loss functions are shown along-with other surrogate losses in Fig. 3. The cost-sensitive versions of these loss functions are discussed below: (a) Cost-Sensitive MSE loss: This loss minimizes the squared error of the predicted output with the desired ground-truth and can be expressed as follows: Neuron Output where, y n is related to the output of the previous layer o n via the logistic function, where, ξ is the class sensitive penalty which depends on the desired class of a particular training sample, i.e., p = argmax m d m . The effect of this cost on the back-propagation algorithm is discussed in Sec. III-E1.
(b) Cost-Sensitive SVM hinge loss: This loss maximizes the margin between each pair of classes and can be expressed as follows: where, y n can be represented in terms of the previous layer output o n and the cost ξ, as follows: The effect of the introduced cost on the gradient computation is discussed in Sec. III-E2.
(c) Cost-Sensitive CE loss: This loss maximizes the closeness of the prediction to the desired output and is given by: where y n incorporates the class-dependent cost (ξ) and is related to the output o n via the soft-max function, The effect of the modified CE loss on the back-propagation algorithm is discussed in Sec. III-E3. Classification Feasibility of Cost-Sensitive Losses: Next, we show (Lemmas III.7-III.9) that the cost-sensitive loss functions remain suitable for classification since they satisfy the following properties: 1) Classification Calibration [33] 2) Guess Aversion [34] Note that a classification calibrated (c-calibrated) loss is useful because the minimization of the empirical risk leads to classifiers which have risks that are closer to the Bayes-risk. Similarly, guess aversion implies that the loss function favours 'correct classification' instead of 'arbitrary guesses'. Since, CE loss usually performs best among the three loss functions we discussed above [3,35], Lemmas III.7-III. 9 show that the costsensitive CE loss is guess aversive and classification calibrated.
Lemma III.7. For a real valued ξ (ξ ∈ R C×C ∈ (0, 1]), given d (i) and the CNN output o (i) , the modified cost-sensitive CE loss will be guess-averse iff, where, g is the set of all guess points.
Proof: For real valued CNN activations, the guess point maps to an all zero output: which can be satisfied if, where, n is the true class. Since, ξ p,n ∈ (0, 1] and thus it is > 0. Also, if n is the true class then o n > o k , ∀k = n. Therefore, the above relation holds true. Lemma III.8. The cost matrix has diagonal entries greater than zero, i.e., diag(ξ) > 0.
Proof: According to Lemma III.1, if the CE loss is guess aversive, it must satisfy , We prove the Lemma by contradiction. Let us suppose that ξ p,n = 0, then the above relation does not hold true, since: and hence, diag(ξ) > 0.
Lemma III.9. The cost-sensitive CE loss function Proof: Given an input sample x which belongs to class p * (i.e., d p * = 1), then the CE loss can be expressed as: The classification risk can be expressed in terms of the expected value as follows: Next, we compute the derivative and set it to zero to find the ideal set of CNN outputs 'o', Similarly, By adding the above two derived expression and setting them to zero, we have : Which shows that there exists an inverse relationship between the optimal CNN output and the Bayes cost of the t th class, and hence, the cost-sensitive CE loss is classification calibrated.
Under the properties of Lemmas III.7-III.9, the modified loss functions are therefore suitable for classification. Having established the class-dependent costs (Sec. III-B) and their impact on the loss layer (Sec. III-C), we next describe the training algorithm to automatically learn all the parameters of our model (θ and ξ).

18:
end if 19: end for 20: return (θ * , ξ * ) the joint optimization, we alternatively solve for both types of parameters by keeping one fixed and minimizing the cost with respect to the other (Algorithm 1). Specifically, for the optimization of θ, we use the stochastic gradient descent (SGD) with the back-propagation of error (Eq. (4)). Next, to optimize ξ, we again use the gradient descent algorithm to calculate the direction of the step to update the parameters. The cost function is also dependent on the class-to-class separability, the current classification errors made by the network with current estimate of parameters and the overall classification error. The class-to-class (c2c) separability is measured by estimating the spread of the with-in class samples (intraclass) compared to the between-class (interclass) ones. In other words, it measures the relationship between the with-in class sample distances and the size of the separating boundary between the different classes. Note that the proposed cost function can be easily extended to include an externally defined cost matrix for applications where expert opinion is necessary. However, this paper mainly deals with class-imbalance in image classification datasets where externally specified costs are not required.
To calculate the c2c separability, we first compute a suitable distance measure between each point in a class c p and its nearest neighbour belonging to c p and the nearest neighbour in class c q . Note that these distances are calculated in the feature space where each point is a 4096 dimensional feature vector (f i : i ∈ [1, N ], N bieng the samples belonging to class c p ) obtained from the penultimate CNN layer (just before the output layer). Next, we find the average of intraclass distances to interclass distance for each point in a class and compute the ratio of the averages to find the c2c separability index.
Formally, the class separability between two classes, p and q is defined as: To avoid over-fitting and to keep this step computationally feasible, we measure the c2c separability on a small validation set. Also, the c2c separability was found to correlate well with the confusion matrix at each epoch. Therefore the measure was calculated after every 10 epochs to minimize the computational overhead. Note that by simply setting the parameters (ξ) based on the percentages of the classes in the data distribution results in a poor performance (Sec. IV-D). This suggests that the optimal parameter values for class-dependent costs (ξ * ) should not be the same as the frequency of the classes in the training data distribution. The following cost function is used for the gradient computation to update ξ: where E val is the validation error. The matrix T is defined as follows: where, µ, σ denote the parameters which are set using cross validation, R denotes the current classification errors as a confusion matrix, S denotes the class c2c separability matrix and H is a matrix defined using the histogram vector h which encodes the distribution of classes in the training set. The matrix H and vector h are linked as follows: where, c is the set of all classes in a given dataset. The resulting minimization objective to find the optimal ξ * can be expressed as: In order to optimize the cost function in Eq. (15), we use the gradient descent algorithm which computes the direction of the update step, as follows: where, v a = vec(T ), v b = vec(ξ) and J denotes the Jacobian matrix. Note that in order to incorporate the dependence of F (ξ) on the validation error E val , we take the update step only if it results in a decrease in E val (see Algorithm 1). Since, our approach involves the use of modified loss functions during the CNN parameter learning process, we will discuss their effect on the back-propagation algorithm in the next section.

E. Effect on Error Back-propagation
In this section, we discuss the impact of the modified loss functions on the gradient computation of the back-propagation algorithm.
1) Cost-Sensitive MSE: During the supervised training, the MSE loss minimizes the mean squared error between the predicted weighted outputs of the model y, and the groundtruth labels d, across the entire training set (Eq. (6)). The modification of the loss function changes the gradient computed during the back-propagation algorithm. Therefore, for the output layer, the mathematical expression of the gradient at each neuron is given by: The y n for the cost-sensitive MSE loss can be defined as: The partial derivative can be calculated as follows: The derivative of the loss function is therefore given by: 2) Cost-Sensitive SVM Hinge Loss: For the SVM hinge loss function given in Eq. (8), the directional derivative can be computed at each neuron as follows: ∂ (d, y) ∂o n = −(2d n − 1) ∂y n ∂o n I{1 > y n (2d n − 1)}.
The partial derivative of the output of the softmax w.r.t the output of the penultimate layer is given by: ∂y n /∂o n = ξ p,n . By combining the above two expressions, the derivative of the loss function can be represented as: ∂ (d, y) ∂o n = −(2d n − 1)ξ p,n I{1 > y n (2d n − 1)}.

3) Cost-Sensitive CE loss:
The cost-sensitive softmax log loss function is defined in Eq. (10). Next, we show that the introduction of a cost in the CE loss does not change the gradient formulas and the cost is rather incorporated implicitly in the softmax output y m . The effect of costs on the CE loss surface is illustrated in Fig. 4. Proposition 1. The introduction of a class imbalance cost ξ (·) in the softmax loss ( (·) in Eq. 10), does not affect the computation of the gradient during the back-propagation process.
Proof: We start with the calculation of the partial derivative of the softmax neuron with respect to its input: Now, two cases can arise here, either m = n or m = n. We first solve for the case when n = m: After simplification we get: The loss function can be differentiated as follows: Since, d is defined as a probability distribution over all output classes ( n d n = 1), therefore: This result is the same as in the case when CE does not contain any cost-sensitive parameters. Therefore the costs affect the softmax output y m but the gradient formulas remain unchanged.
In our experiments (Sec. IV), we will only report performances with the cost-sensitive CE loss function. This is because, it has been shown that the CE loss outperforms the other two loss functions in most cases [? ]. Moreover, it avoids the learning slowing down problem of the MSE loss [35].

IV. EXPERIMENTS AND RESULTS
The class imbalance problem is present in nearly all realworld object and image datasets. This is not because of any flawed data collection, but it is simply due to the natural frequency patterns of different object classes in real life. For example, a bed appears in nearly every bedroom scene, but a baby cot appears much less frequently. Consequently, from the perspective of class imbalance, the currently available image classification datasets can be divided into three categories: 1) Datasets with a significant class imbalance both in the training and the testing split (e.g., DIL, MLC), 2) Datasets with unbalanced class distributions but with experimental protocols that are designed in a way that an equal number of images from all classes are used during the training process (e.g., MIT-67, Caltech-101). The testing images can be equal or unequal for different classes. 3) Datasets with an equal representation of each class in the training and testing splits (e.g., MNIST, CIFAR-100). We perform extensive experiments on six challenging image classification datasets (two from each category) (see Sec. IV-B). For the case of imbalanced datasets (1 st category), we report results on the standard splits for two experiments. For the two datasets from the 2 nd category, we report our performances on the standard splits, deliberately deformed splits and the original data distributions. For the two datasets from the 3 rd category, we report results on the standard splits and on deliberately imbalanced splits. Since, our training procedure requires a small validation set (Algorithm 1), we use ∼ 5% of the training data in each experiment as a held-out validation set.

A. Multi-class Performance Metric
The main goal of this work is to enhance the overall classification accuracy without compromising the precision of minority and majority classes. Therefore, we report overall classification accuracy results in Tables I-VI, VIII and IX for comparisons with baseline and state-of-the art balanced and unbalanced data classification approaches. We report class recall rates in confusion matrices displayed in Fig. 6. We also show our results in terms of G-mean and F-measure scores on all the six datasets (see Table VII). Note that the F-measure and G-mean scores are primarily used for binary classification tasks. Here, we extend them to multi-class problem using the approach in [36], where these scores are calculated for each class in a one-vs-all setting and their weighted average is calculated using the class frequencies.
It is also important to note that neural networks give a single classification score and it is therefore not feasible to obtain ROC curves. As a result, we have not included AUC measurements in our experimental results.

B. Datasets and Experimental Settings
1) Imbalanced Datasets: Melanoma Detection : Edinburgh Dermofit Image Library (DIL) consists of 1300 high quality skin lesion images based on diagnosis from dermatologists and dermatopathologists. There are 10 types of lesions identified in this dataset including melanomas, seborrhoeic keratosis and basal cell carcinomas. The number of images in each category varies between 24 and 331 (mean 130, median 83). Similar to [37], we report results with 3-fold cross validation.
Coral Classification : Moorea Labelled Corals (MLC) contains 2055 images from three coral reef habitats during 2008-10. Each image is annotated with roughly 200 points belonging to the 9 classes (4 non-corals, 5 corals). Therefore in total, there are nearly 400,000 labelled points. The class representation varies approximately from 2622 to 196910 (mean 44387, median 30817). We perform two of the major standard experiments on this dataset similar to [4]. The first experiment involves training and testing on data from year 2008. In the second experiment, training is carried out on data from year 2008 and testing on data from year 2009.
2) Imbalanced Datasets-Balanced Protocols: Object Classification: Caltech-101 contains a total of 9,144 images, divided into 102 categories (101 objects + background). The number of images for each category varies between 31 and 800 images (mean: 90, median 59). The dataset is originally imbalanced but the standard protocol which is balanced uses 30 or 15 images for each category during training, and testing is performed on the remaining images (max. 50). We perform experiments using the standard 60%/40% and 30%/70% train/test splits.
Scene Classification: MIT-67 consists of 15,620 images belonging to 67 classes. The number of images varies between 101 and 738 (mean: 233, median: 157). The standard protocol uses a subset of 6700 images (100 per class) for training and evaluation to make the distribution uniform. We will, however, evaluate our approach both on the standard split (80 images for training, 20 for testing) and the complete dataset with imbalanced train/test splits of 60%/40% and 30%/70%.
3) Balanced Datasets-Balanced Protocols: Handwritten Digit Classification: MNIST consists of 70,000 images of digits (0-9). Out of the total, 60,000 images are used for training (∼600/class) and the remaining 10,000 for testing (∼100/class). We evaluate our approach on the standard split   as well as the deliberately imbalanced splits. To imbalance the training distribution, we reduce the representation of even and odd digit classes to only 25% and 10% of images, respectively.
Image Classification: CIFAR-100 contains 60,000 images belonging to 100 classes (600 images/class). The standard train/test split for each class is 500/100 images. We evaluate our approach on the standard split as well as on artificially imbalanced splits. To imbalance the training distribution, we reduce the representation of even-numbered and odd-numbered classes to only 25% and 10% of images, respectively.

C. Convolutional Neural Network
We use a deep CNN to learn robust feature representations for the task of image classification. The network architecture consists of a total of 18 weight layers (see Fig. 5 for details). Our architecture is similar to the state-of-the-art CNN (configuration D) proposed in [39], except that our architecture has two extra fully connected layers before the output layer and the proposed loss layer is cost-sensitive. Since there are a huge number of parameters (∼139 million) in the network, its not possible to learn all of them from scratch using a relatively smaller number of images. We, therefore, initialize the first 16 layers of our model with the pre-trained model of [39] and set random weights for the last two fully connected layers. We then train the full network with a relatively higher learning rate to allow a change in the network parameters. Note that the cost-sensitive (CoSen) CNN is trained with the modified cost functions introduced in Eqs. (6)(7)(8)(9)(10)(11). The CNN trained without cost-sensitive loss layer will be used as the baseline CNN in our experiments. Note that the baseline CNN architecture is exactly the same as the CoSen CNN, except that the final layer is a simple CE loss layer.

D. Results and Comparisons
For the two imbalanced datasets with imbalanced protocols, we summarize our experimental results and comparisons in Tables I, II. For each of the two datasets, we perform two standard experiments following the works of Beijbom et al. [4] and Ballerini et al. [38]. In the first experiment on the DIL   dataset, we perform 3-fold cross validation on the 5 classes (namely Actinic Keratosis, Basal Cell Carcinoma, Melanocytic Nevus, Squamous Cell Carcinoma and Seborrhoeic Keratosis) comprising of a total of 960 images. In the second experiment, we perform 3-fold cross validation on all of the 10 classes in the DIL dataset. We achieved a performance boost of ∼ 5.0% and ∼ 3.1% over the baseline CNN in the first and second experiments respectively (Table I).
For the MLC dataset, in the first experiment we train on two-thirds of the data from 2008 and test on the remaining one third. In the second experiment, data from year 2008 is used for training and tests are performed on data from year 2009. Note that in contrast to the 'multiple texton maps' (MTM) [4] approach which extracts features from multiple scales, we only extract features from the 224×224 dimensional patches. While we can achieve a larger gain by using multiple scales with our approach, we kept the setting similar to the one used with the other datasets for consistency. For similar reasons, we used the RGB color space instead of LAB, which was shown to perform better on the MLC dataset [4]. Compared to the baseline CNN, Methods (using stand. split) Performances Network in Network [1] 64.3% Probablistic Maxout Network [42] 61.9% Representation Learning [43] 60.8% Deeply Supervised Nets [3] 65.4% Generalized Pooling Func. [40] 67.6% Maxout NIN [41] 71.1%  we achieved a gain of 2.3% and 2.5% on the first and second experiments respectively. Although the gains in the overall accuracy may seem modest, it should be noted that the boost in the average class accuracy is more pronounced. For example, the confusion matrices for DIL and MLC datasets in Fig. 6 (corresponding to Exp. 1 and Exp. 2 respectively), show an improvement of 9.5% and 11.8% in the average class accuracy. The confusion matrices in Figs. 6a, 6b, 6c and 6d also show a very significant boost in performance for the least frequent classes e.g., Turf, Macro, Monti, AK and SCC.
Our results for the two balanced datasets, MNIST and CIFAR-100, are reported in Tables III, IV on the standard splits along-with the deliberately imbalanced splits. To imbalance the training distributions, we used the available/normal training data for the even classes and only 25% and 10% of data for the odd classes. Similarly, we experimented by keeping the normal representation of the odd classes and reducing the representation of the even classes to only 25% and 10%. Our   results show that the performance of our approach is equal to the performance of the baseline method when the distribution is balanced, but when the imbalance ratios increase, our approach produces significant improvements over the baseline CNN (which is trained without using the cost-sensitive loss layer). We also compare with the state-of-the-art techniques which report results on the standard split 1 and demonstrate that our performances are better or comparable. Note that for the MNIST digit dataset, nearly all the top performing approaches use distortions (affine and/or elastic) and data augmentation to achieve a significant boost in performance.
In contrast, our baseline and cost-sensitive CNNs do not use 1 Note that the standard split on the Caltech-101 and MIT-67 is different from the original data distribution (see Sec. IV-B for details).

Methods (using stand. split) Performances
Spatial Pooling Regions [52] 50.1% VC + VQ [53] 52.3% CNN-SVM [54] 58.4% Improved Fisher Vectors [55] 60.8% Mid Level Representation [56] 64.0% Multiscale Orderless Pooling [57] 68.9%    we further decrease the data of odd and even classes to just 10% respectively, and observe a better relative performance of our proposed approach compared to the baseline method. We report F-measure and G-mean scores on all the six datasets in Table VII. The metric calculation details are provided in Sec. IV-A. The most unbalanced splits (Fig. 7) are used for each dataset to clearly demonstrate the benefit of class-specific costs. We note that the cost-sensitive CNN model clearly out-performs the baseline model for all experiments.
The comparisons with the best approaches for classimbalance learning are shown in Table VIII. Note that we used a high degree of imbalance for the case of all six datasets to clearly show the impact of the class imbalance problem on the performance of the different approaches (Fig.7). For fairness and conclusive comparisons, our experimental procedure was kept as close as possible to the proposed CoSen CNN. For example, for the case of CoSen Support Vector Machine (SVM) and Random Forest (RF) classifiers, we used the 4096 dimensional features extracted from the pre-trained deep CNN (D) [39]. Similarly, for the cases of over and under-sampling, we used the same 4096 dimensional features, which have shown to perform well on other classification datasets. A twolayered neural network was used for classification with these sampling procedures. We also report comparisons with all types of data sampling techniques i.e., over-sampling (SMOTE [5]), under-sampling (Random Under Sampling -RUS [58]) and hybrid sampling (SMOTE-RSB * [8]). Note that despite the simplicity of the approaches in [5,58], they have been shown to perform very well on imbalanced datasets in data mining [14,61]. We also compare with the cost-sensitive versions of popular classifiers (weighted SVM [59] and weighted RF [60]  implementation of LIBSVM [62] and set the class-dependent costs based on the proportion of each class in the training set. Finally, we experiment with a recent cost-sensitive deep learning based technique of Chung et al. [30]. Unlike our approach, [30] does not automatically learn class-specific costs. To have a fair comparison, we incorporate their proposed smooth one-sided regression (SOSR) loss as the last layer of the baseline CNN model in our experiments. Similar to [30], we use the approach proposed in [63] to generate fixed cost matrices. Our proposed approach demonstrates a significant improvement over all of the cost-sensitive class imbalance methods.
Since our approach updates the costs with respect to the data statistics (i.e., data distribution, class separability and classification errors), an interesting aspect is to analyse the performance when the costs are fixed and set equal to these statistics instead of updating them adaptively. We experiment with fixed costs instead of adaptive costs in the case of CoSen-CNN. For this purpose, we used three versions of fixed costs, based on the class representation (H), data separability (S) and classification errors (M). Table IX shows the results for each dataset with four different types of costs. The results show that none of the fixed costs significantly improve the performance in comparison to the adaptive cost. This shows that the optimal costs are not the H, S and M themselves, rather an intermediate set of values give the best performance for cost-sensitive learning.
Lastly, we observed a smooth reduction in training and validation error for the case of cost-sensitive CNN. We show a comparison of classification errors between baseline and costsensitive CNNs at different training epochs in Fig. 8.

V. CONCLUSION
We proposed a cost-sensitive deep CNN to deal with the class-imbalance problem, which is commonly encountered when dealing with real-world datasets. Our approach is able to automatically set the class-dependent costs based on the data statistics of the training set. We analysed three commonly used cost functions and introduced class-dependent costs for each case. We show that the cost-sensitive CE loss function is c-calibrated and guess aversive. Furthermore, we proposed an alternating optimization procedure to efficiently learn the class-dependent costs as well as the network parameters. Our results on six popular classification datasets show that the modified cost functions perform very well on the majority as well as on the minority classes in the dataset. Proof: From Eq. 1, we have: q ξ p * ,q P (q|x) ≤ q ξ p,q P (q|x) ∀p = p * which gives the following relation: P (p * |x) ξ p * ,p * − ξ p,p * ≤ q =p * P (q|x) ξ p,q − ξ p * ,q , ∀p = p * As indicated in Sec. 3.1, the above expression holds for all p = p * . For a total number of N classes and an optimal prediction p * , there are N − 1 of the above relations. By adding up the left and the right hand sides of these N − 1 relations we get: This can be simplified to: where, Px = [P (1|x), . . . , P (N |x)]. Note that the posterior probabilities Px are positive ( i P (i|x) = 1 and P (i|x) > 0). It can be seen from the above equation that the addition of any constant c, does not affect the overall relation, i.e., for any column j, Therefore, the columns of the cost matrix can be shifted by a constant c without any effect on the associated risk.
Lemma A.2. The cost of the true class should be less than the mean cost of all misclassification.
Proof: Since, Px can take any distribution of values, we end up with the following constraint: For a correct prediction p * , P (p * |x) > P (p|x), ∀p = p * . Which implies that: It can be seen that the cost insensitive matrix (when diag(ξ ) = 0 and ξ i,j = 1, ∀j = i) satisfies this relation and provides the upper bound.
Lemma A.3. The cost matrix ξ for a cost-insensitive loss function is an all-ones matrix, 1 p×p , rather than a 1 − I matrix, as in the case of the traditionally used cost matrix ξ .
Proof: With all costs equal to the multiplicative identity i.e., ξp,q = 1, the CNN activations will remain unchanged. Therefore, all decisions have a uniform cost of 1 and the classifier is costinsensitive.
Proof: We adopt a proof by contradiction. Let us suppose that ξp,q = 0. During training in this case, the corresponding score for class q (sp,q) will always be zero for all samples belonging to class p. As a result, the output activation (yq) and the back-propagated error will be independent of the weight parameters of the network, which proves the Lemma.
Proof: Based on Lemmas A.3 and A.4, it is trivial that the costs are with-in the range (0, 1].
Lemma A.6. Offsetting the columns of the cost matrix ξ can lead to an equally probable guess point.
Proof: Let us consider the case of a cost-insensitive loss function. In this case, ξ = 1 (from Lemma A.3). Offsetting all of its columns by a constant c = 1 will lead to ξ = 0. For ξ = 0, the CNN outputs will be zero for any o (i) ∈ R N . Therefore, the classifier will make a random guess for classification.