{'fraction_non_alphanumeric': 0.032937967516634296, 'fraction_numerical': 0.009463983138405588, 'mean_word_length': 4.701696512723846, 'pattern_counts': {'":': 0, '<': 1, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
On Class Imbalance and Background Filtering in Visual Relationship Detection

In this paper we investigate the problems of class imbalance and irrelevant relationships in Visual Relationship Detection (VRD). State-of-the-art deep VRD models still struggle to predict uncommon classes, limiting their applicability. Moreover, many methods are incapable of properly filtering out background relationships while predicting relevant ones. Although these problems are very apparent, they have both been overlooked so far. We analyse why this is the case and propose modifications to both model and training to alleviate the aforementioned issues, as well as suggesting new measures to complement existing ones and give a more holistic picture of the efficacy of a model.


I. INTRODUCTION
In recent years there have been many advances in the field of Computer Vision. Deep end-to-end architectures [1]- [3] have proven time and time again to be very effective in extracting visual features from an image that can be used to classify visible objects [4]- [7] and even predict their shape [8]. While object detection and segmentation is certainly an important step towards image understanding, it is not enough: even when performed flawlessly, it fails at capturing interactions between objects, which is a fundamental part in the process of scene understanding. This is why there has recently been an increasing focus on applying deep learning to what is commonly called Visual Relationship Detection (VRD) [9]- [12].
Performing both object and relationship detection on an image yields a set of subject, predicate, object 1 triplets, where subject and object are entities such as man or car and predicate describes the relationship that ties them together (such as behind or riding). These triplets can then be used to produce a graph where nodes represent objects and edges represent relationships. This is commonly referred to as a scene graph and it provides a structured and explicit image representation. Scene graphs have been successfully used in tasks such as Image Retrieval [13], Image Generation [14] and Visual Question Answering [15], [16].
The most common way in literature to tackle VRD is reducing it to a multi-class classification problem, using deep neural networks for feature extraction. A standard architecture [9]- [12], [17]- [23] includes a convolutional neural network (CNN) to obtain a visual feature map and detect objects, followed by another module that uses the information provided by the visual one in order to predict predicates between object pairs, picking the most likely out of many possible predicate classes. However, the model's capability to predict the appropriate predicate can be undermined if the data distribution is skewed towards some classes (i.e., there is class imbalance). This is an issue in many applications [24] and VRD is no exception: failing to detect infrequent relationships means that only a subset of classes can actually be predicted (see Table I). This leads to many details about interactions between objects being lost, as the model will prefer the common and generic on instead of a more fitting predicate such as riding or walking on. We believe the reason class imbalance has been overlooked in literature is that it does not seem to reflect in poor performance, which happens because the most commonly used evaluation metric (recall) is relatively insensitive to low accuracy on small classes.
We tackle class imbalance using cost-sensitive learning, which is a paradigm that has been shown to be effective in this kind of scenarios [24], [25]. This solution does not require significant alterations to the model, operating instead on the training loss. On top of ease of implementation and usage, our solution still allows the model to predict every relationship class present in the dataset on which the model is trained, in contrast to other methods that cluster classes together and can thus only predict more generic categories, hindering their expressive power [26]. We suggest a complementary metric to the standard recall that is agnostic to the number of examples belonging to each predicate, thus being equally affected by every class. Fig. 1: Every possible relationship whose subject is street, as predicted by NeuralMotifs (or NM) [12]. Objects have blue labels, relationship orange ones. The only meaningful predicate is highlighted in green, while the rest are irrelevant or absurd.
We also analyse model's capability to predict null relationships (also called background or negative), or equivalently abstaining from assigning any relationship for some subjectobject pairs. Inability to do so will result in fully connected scene graphs, i.e., graphs where each object node is connected to every other. They represent an issue because the number of possible edges (relationships) grows quadratically in the number of nodes, but the majority of them are most likely just noise. This is shown in Figure 1: in the depicted image, a human annotator would probably not consider relevant any relationship that has street as a subject except street, under, elephant , 2 but the method proposed in [12] predicts a predicate for every possible pair of objects. This example shows how the ability to predict the null relationship is important for a scene graph to constitute an appropriate description for the corresponding image. We propose an easyto-implement architectural change that can be applied to any model to filter out noisy relationships and use the well-known F 1 -score to measure its efficacy.

A. Visual Relationship Detection
Recently, scene graphs have been successfully used in tasks such as Image Retrieval [13], Image Generation [14] and Visual Question Answering [15], [16]. The process of getting a scene graph from an image is called Scene Graph Generation and VRD is a core part of it. Several deep learning methods have been proposed for this task [9]- [12], [17]- [23].
The most common architecture includes an object detector, usually Fast R-CNN [4] or Faster R-CNN [5] with VGG [2] or ResNet [3] as a backbone. This detector is used to both localise objects in the image and extract their visual features. Sometimes the same CNN is used to extract features for visual phrases [18], i.e., parts of the image that contain a pair of possibly related objects. A few approaches try to predict both relationships and objects directly [9], [17], [21], while some other approaches use all of these visual features to build some kind of preliminary graph which is refined through message passing between nodes [10], [18]- [20], with the goal of exchanging information across object and relationships. In [12], [22] they incorporate context in a different manner, by computing a global embedding vector that is jointly used with region-specific features at prediction time. Differently from most methods, in [11] they do not use any object detector. Instead, they compute a feature map and predict objects and relationships jointly for each mapped pixel, together with an embedding for each object. Objects are then assigned to each relationship based on embedding similarity with triplet components.
Some methods extend visual features with additional information. The most common way is to use pre-trained word embeddings [27], [28], which was first done in [9] to add a language prior that helps predicate detection by using the word embeddings of subject and object. Several methods have used the same idea since, including [12], [21]. Some models [12], [23] also exploit co-occurrence statistics found in the training set to obtain a prior predicate distribution over subject-object pairs that is then used to aid prediction by shifting probability away from unlikely pairs.
In our experiments we will use three of the most popular methods as baselines, namely [10], [11] and in particular [12], which represents the state of the art on VG-SGG at the time of writing.

B. Class imbalance
Class imbalance is an often overlooked issue in deep learning. A possible reason for this is that popular datasets like ImageNet [29] or CIFAR [30] exhibit little to no class imbalance [31], thus making handling it unnecessary. However, recently there has been an increase in awareness of this issue in real-world datasets and a growing number of papers mention techniques to deal with this problem [25], [31]- [35]. All of these methods intervene on the training procedure as opposed to the data, since techniques such as oversampling or undersampling incur the risk of respectively promoting overfitting or removing useful information [24]. Outside of this common denominator, the gamut of solutions is quite varied: in [32] a cost-sensitive pre-training strategy is proposed that allows to extract features in a fair (i.e., balanced) way; in [33] the authors use a cost-sensitive variation of the Mean Squared Error (MSE) loss function; [25] propose to modify standard loss functions (MSE, hinge and crossentropy) with a cost matrix that is learnt in alternation to the model on a separate validation set; in [31] a triplet-based training procedure is used, where each batch is balanced as it contains both positive and hard negative examples; [34] propose a transfer learning approach to facilitate the prediction of small classes by using what the model has learnt on large ones; and in [35] the training is performed over quintuplets with the aim of clustering examples belonging to the same class together, so that infrequent classes can be distinguished from frequent ones.
However, to the best of our knowledge, only one very recent method [26] has explicitly tackled the problem of class imbalance in VRD. The authors apply K-means clustering on object labels based on their relationship frequency distributions on the training set, with the goal of reducing the number of labels and increase the amount of examples for each label. They apply their method to the Person In Context 3 dataset, which focuses on human-centric relations (i.e., relationships where the subject is human) and accurate instance segmentation (as opposed to approximate bounding boxes). Our method differs in three main aspects: a) it is applied to the relationship prediction module directly, instead of trying to improve it indirectly by clustering object classes; b) it is easier to implementation and use, because it does not require significant changes to the model or training procedure; c) it still allows the model to predict every relationship class present in the dataset, as opposed to only clusters of classes.

A. Cost sensitive learning (CSL)
Our proposal to deal with class imbalance is cost-sensitive learning, which has proven effective in this kind of scenarios  Fig. 2: Comparison of quantized score distributions obtained with softmax and sigmoid activations using P2G [11].
[24], [25]. Specifically, we propose the following cost-sensitive loss: where N is the number of examples, C is the number of predicate classes, Z, T ∈ R N ×C are respectively the matrices of predicted scores and 1-hot encoded targets, u, v ∈ R C are two vectors of class weights. The j-th element of u (i.e., u j ) determines the importance given to positive examples of class j, while v j weighs negative examples for class j. Thus, setting u j > 1 tends to increase the recall for class j, while v j has an impact on the precision. The loss described in Equation 1 is a weighted version of the standard binary cross entropy loss, which is obtained when u j = v j = 1 ∀j. This loss derives from the maximum likelihood estimation of a sigmoid model, so we use sigmoid as activation function for the output layer. This comes with some additional benefits. First of all, predicates in VG-SGG semantically overlap with each other, as is the case, for instance, of on, sitting-on, on-back-of or of, belongingto. Using sigmoids has the advantage of modelling class predictions independently, which means that the output they produce preserves its meaning even with overlapping classes. Furthermore, it has been empirically shown [36] that softmax tends to produce inaccurate confidence scores. To verify that this applies in our case we compute the expected calibration error (eCE) as defined in [37], which measures the difference between predicted score and expected accuracy. The eCE quantifies the intuitive idea that in a set of predictions with average score x we expect 100x% of them to be correct. We report the results using the Pixels2Graph (P2G) model [11] in Table II. As it can be seen, sigmoid-produced scores are better calibrated than softmax ones by a large margin. This happens Fig. 3: Prediction pipeline with background filtering. In the picture R is the number of candidate relationships, D is the dimensionality of the feature space and C is the number of predicate classes. because the binary cross entropy loss used with sigmoids is more affected by negative samples, thus doing a better job at learning calibrated scores without the need of a larger amount of negative relationships to be provided. It can be indeed observed in Figure 2 that the histogram of scores computed using sigmoids is more widely distributed and shifted towards smaller values than the one obtained by a categorical cross entropy training.
We now describe how to set the two weight vectors u and v in the cost-sensitive binary cross entropy loss (Equation 1). Following a framework first described in [38], we first define the cost matrix R C×C W (w jk ), with where δ jk is the Kronecker delta (δ jk = 1 if j = k and 0 otherwise) and N j is the number of examples of class j. Entry w jk represent the costs of misclassifying an example of class j as belonging to class k. We want this cost to be proportional to N k because mistaking for a common class should be punished, as those classes are easier to predict due to the large number of examples. On the other hand, w jk should also be inversely proportional to N j , because the few examples of an infrequent class j should not be mistaken for other classes. However, taking the unmodified ratio affects training negatively because of the very large gap between the two extremes (most common and least common classes), which is why we use the log ratio instead. Also, since the aim of this modification is to aid uncommon classes and not to hinder the learning of common ones, we do not allow the weights to be less than one, which would have a direct consequence on the training by reducing the gradient norm. Finally, the diagonal of the matrix is identically 0 because it represents the cost of correct classification, which should be null. Given cost matrix W we can derive the weight vectors we need to compute the loss. Let us assume we are considering example i of class c(i). Note that i contributes as a negative example for every class k = c(i), and its loss contribution should be proportional to the cost of misclassifying it for class k, which is exactly w c(i),k . Therefore we set vector v as v = W c(i),· for an example of class c(i) where W k,· denotes the k-th row of matrix W. Coefficient u c(i) , on the other hand, defines the cost penalty for misclassifying example i so we set u as the expected misclassification costs vector: where P j represents the prior probability of class j, which we set as P j N j /N . Setting parameter vectors u and v this way increases the impact uncommon classes have on the training loss, thus making the classifier more likely to recognise them.

B. Noisy relationships filtering (NRF)
Another issue that we tackle regards background relationships. The problem is again twofold: a) some models are incapable of dealing with them and b) the standard evaluation pipeline does not capture this issue.
Models should be able to predict the null relationship. Not doing so leads to fully connected scene graphs, where each object node is predicted to have a relationship with every other. However, there is no guarantee that every object is related to all other ones: in fact, most of the possible edges in a scene graph do not convey any meaningful information, as shown in Figure 1 for all the relationships having street as a subject. This problem could be solved by adding a module that distinguishes between relevant (foreground) and irrelevant (background) relationships on top of the standard classification architecture. We propose to implement this module by repurposing the probability the network assigns to the background class to act as a filter for the output, as shown in Figure 3. Using 0 as an index for the background class and {1, . . . , C} for the foreground ones, the decision rule is not y arg max j∈{0,...,C} z j any more, but rather y 1(z 0 < θ) arg max where θ is a threshold to be fixed (we use 0.5 in our experiments). Note that the background relationship prediction module is trained in a cost-sensitive way as well.

C. Evaluation metrics
We believe that the main reason class imbalance has not been dealt with in VRD is that it does not appear to have an impact performance-wise. This happens because recall is the most commonly used evaluation metric and it is not affected much by low accuracy on small classes, as the total amount of examples it misses is very low compared to the correctly retrieved ones. We suggest the use of a metric called mean predicate classification recall or mPCR, defined as: where G(j) is the set of triplets i, s, o such that j is the predicate class associated to the subject-object pair s, o in image i,ŷ(i, s, o) is the model prediction for such pair and 1 is the indicator function that is 1 if its argument is true and 0 otherwise. The mPCR is essentially equivalent to the standard macro-averaged recall and it is insensitive to the number of examples of a particular class, which is why it is suited to measure the variety of predicates the model is actually able to predict. Moreover, as we mentioned, most object pairs should not be related. In fact, in VG-SGG on average only about 6% of the pairs is part of a relationship. While this percentage cannot be considered a gold standard due to the open-world assumption (i.e., the ground truth only contains a subset of the correct relationships), it is clear that the sparsity of relationship predictions is a desirable property of the model (see Figure 1). This is why we also use the well-known F 1 measure, since it depends both on the recall, which is widely used in the field of VRD, and on the precision, which penalises false positives. Models that cannot discriminate between relevant and irrelevant relationships will be penalised in their F 1 score.

A. Experimental settings
We applied our modifications on three models: Iterative Message Passing (IMP, [10]), Neural Motifs (NM, [12]) and Pixels2Graph by Associative Embedding (P2G, [11]). We tested them on VG-SGG [10], the subset of Visual Genome [39] most commonly used in the literature. It is a suitable dataset for cost-sensitive learning because its class distribution is very unbalanced (Figure 4). All of our measures are computed considering the top 100 results produced by the model, to be aligned with the standard metric of recall@100 [9].
We generally use the settings provided by the authors to train the models. There are two main exceptions, both regarding P2G. The first is about training. The default schedule trains the network for 2 million iterations, which we estimate takes around three weeks on our machine. We reduce this to 100 thousands iterations, as at this point the model already produces sensible results. Since the focus of our experiments is assessing whether the modifications we propose are effective or not, our comparison is still fair, because we follow the same training regime for the model that includes our additions as for the corresponding baseline. The second exception concerns P2G evaluation methodology. The authors allow multiple predictions for the same subject-object pair, but we disable this behaviour by only considering the best result for any single object pair at evaluation time. This way, the task remains the same (only one prediction per pair) across the different models we consider.  Figure 4) and the order is the same for both rows and columns and across methods. Recall    take into account the possible difference in variance across experiments, since we train the baseline ourselves with a reduced schedule. All of our results are statistically significant at the 95% confidence interval, unless otherwise specified.

B. Analysis
Our results are shown in Table III. As a general pattern, it can be observed that CSL makes the model significantly better at predicting uncommon classes and therefore reduces the effect of class imbalance during training, which is suggested by a higher mPCR. Table IV shows that the number of predicates never correctly predicted (i.e., they have 0 recall) decreases considerably. This can also be visually observed through confusion matrices (Figure 6), that show more details about the distribution of predictions. However, CSL generally causes lower recall. This happens because the model now tries to predict infrequent classes in cases when it used to prefer common ones in non-cost-sensitive training, for example (a) Image (b) Scene graph by NM [12] (c) Scene graph by NM-CSL-NRF favouring riding over a more generic on when describing the relationship between a person and a horse ( Figure 5). When we also plug in background filtering recall and mPCR decrease, which is expected because fewer relationships are overall predicted as foreground. The advantage is that the model is now capable of discerning the difference between a relevant relationship and an irrelevant one, as an increased precision and F 1 score indicate. This can be seen in Figure 7: the baseline model outputs many relationships that are irrelevant or even incorrect, such as street, under, pants , pants, on, shirt or helmet, on, skateboard . Our model, on the other hand, provides the relationships needed to describe the image and leaves out the noisy ones.

V. DISCUSSION AND CONCLUSION
In this paper we examine with a critical eye current popular deep models for VRD such as the ones proposed in [10]- [12]. These are, or have been in the past, state-of-the-art models and exhibit advanced architectures that are able to effectively deal with a complex task as VRD. However, some commonly overlooked issues -namely class imbalance and background filtering -hamper their performance and limit their usefulness. Recall, which is the most commonly used evaluation metric, fails to detect such problems.
Our contribution is twofold: we provide some simple adaptations that can be easily plugged into any training procedure or model to alleviate class imbalance and background filtering issues and we also suggest the usage of some additional metrics to complement recall in performance evaluation.
A summary of our main results can be found in Table III. They suggest that 1) cost-sensitive learning is effective at promoting uncommon class prediction, as shown by the increase in mPCR; 2) background-filtering modules are effective at reducing the number of noisy relationships, thus increasingsometimes by quite a large margin -the F 1 score.
Although our approach gives promising result, it can be improved. The definition of cost matrix we use (see Equation 2) captures the role that cost weights should fulfil, but it is somewhat heuristic. A few methods have been proposed in literature [25], [40], [41] to define cost-sensitive matrices. These can be integrated to further enhance performance and simplify design by not having to choose how to define the cost matrix. Another approach could be to complement our per-class cost-sensitive technique with per-example ones [42].
The background prediction module could also be enhanced. In training, a component could be added to the loss in order to put more emphasis on detective background examples from foreground ones, as proposed in [43]. From an architectural point of view, the module itself could be substituted by a more advanced one like that proposed in [44], although at the cost of a more complex model.