{'fraction_non_alphanumeric': 0.03152995186023528, 'fraction_numerical': 0.005834156916504607, 'mean_word_length': 4.609442060085837, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Interpretable UAV Collision Avoidance using Deep Reinforcement Learning

The significant components of any successful autonomous flight system are task completion and collision avoidance. Most deep learning algorithms successfully execute these aspects under the environment and conditions they are trained. However, they fail when subjected to novel environments. This paper presents an autonomous multi-rotor flight algorithm, using Deep Reinforcement Learning augmented with Self-Attention Models, that can effectively reason when subjected to varying inputs. In addition to their reasoning ability, they are also interpretable, enabling it to be used under real-world conditions. We have tested our algorithm under different weather conditions and environments and found it robust compared to conventional Deep Reinforcement Learning algorithms.


INTRODUCTION
The use of AI-powered drones for various tracking problems has become widely prevalent. They have been recently used for fighting forest fires, searching for a target during disasters and observing wildlife. In addition, recent work has focused on developing autonomous online algorithms that control the trajectory of the Unmanned Aerial Vehicle (UAV) under different mission scenarios. The ability of UAVs to adapt to diverse and novel constraints comes from the path planning capability of the underlying algorithm controlling it. [1]- [6]. Deep Reinforcement Learning (DRL) is usually preferred for this task over the conventional approaches such as A*, artifical potential fields, Floyd warshall, dynamic programming and similar methods, since it does not present the same disadvantages as other methods which make it infeasible to implement in real world situations, [7]- [13].
Reinforcement Learning (RL) is associated with an agent or a set of agents, placed within an environment, that take a series of actions to maximize their expected reward [14]. A Model Free RL algorithm, Q-Learning, was developed to obtain the action value for specific states [15]. Deep Q-Learning (DQN) (a class of DRL algorithms) combined Q-Learning with a Deep Neural Network (DNN) [16]. A DQN consists of multiple hidden layers, thereby allowing it to learn and function well in situations involving high dimensional spaces. Therefore, it can mitigate to some extent, the curse of dimensionality problem affecting Q-Learning [17]. Furthermore, the training of robots under real-world conditions is an expensive and time-consuming process. Therefore, it is beneficial to train the algorithm under realistic simulators and then transfer the parameters learned by the algorithm from the system into a real-world robot. Since simulated training cannot capture all the aspects and nuances of the real world, it is another reason why the training needs to be robust to novel environments and changes in weather conditions and lightning. A further motivation for the algorithm to be generalizable is that using all aspects of the real world is not possible in simulation. Therefore, we can only train it with a subset of such features. Our objective is to improve the learning and generalization capability of our multirotor using self-attention models. For collision avoidance tasks, a Convolutional Neural Network (CNN) is generally used as a function approximator. The CNN uses the robot camera as input and these inputs are then considered to be the 'State' in the DQN algorithm. The output of the CNN is a set of Q-values equaling the number of actions. Based on the Q value, the algorithm chooses an action that is then used to maximize the expected reward [18]. The disadvantage of using CNNs is that they are generally not invariant to deformations or rotations. Therefore, the learning algorithm may not be able to classify, similar objects within the environment under the same category if they have been deformed or modified in any way. To enable the DNN to generalize to modifications in the environment, it needs to have relational properties, which allows it to classify objects while relating them with other objects [19], [20]. Hence, we use a Self-Attention Model with Duelling Deep Q Network (D3QN-SAM) for collision avoidance, while comparing its performance with a Duelling Deep Q-Network that uses a CNN (D3QN-CNN).

RELATED WORK Attention Augmented Reinforcement Learning
[20] borrowed ideas from the Relational RL umbrella (RRL) [21], [22] that was introduced more than 20 years prior. RRL uses relational representations to represent states, policies and actions, and combines it with RL. In addition, they implemented a self attention model to compute non-local interactions. Their basis for choosing this model was that it wasn't computationally intensive and shared features with graph neural networks. Furthermore, this model enabled obtaining relations between different entities. Therefore they incorporated relational reasoning coupled with structured perception to develop RL algorithms capable of interpretable reasoning. It could be discerned that agents trained using such architectures tend to be more generalized and providing better performance compared with baseline models. Also, they found that the algorithm's internal computations could be visualized and the computations were in line with those architectures that obtained task-specific relations [19]. [23] developed a novel architecture consisting of various recurrent cells that had almost independent transition dynamics, known as Recurrent Independent Mechanism (RIM). Similar to [20], they used multiple attention heads that enabled communication between RIMs. In addition, these attention heads were used to select which RIMs were required for the input at a particular time step. They claimed that usually numerous processes consist of sub-processes working independently. However, ML models assume that all of these processes interact degrading the generalization of these algorithms. Therefore, to address this issue, they used RIMs that contended for attention during all time steps and were updated at only pertinent steps. The authors demonstrated that this technique improved the generalization capability across a wide range of tasks such as those having temporal patterns, objects and a combination of patterns and objects. [24] extended the idea of self-attention for visual RL tasks. They use neuro-evolution to train their DRL agent that used self-attention to focus on critical aspects within images in order to finish their tasks. It was observed that the agent's focusing ability enables it to generalize to new environments, since it concentrated on few (necessary) aspects of the visual stimuli and thereby was able to beat conventional algorithms with relatively lower parameters. However, the authors noted that their algorithm still couldn't compete with human abilities of generalization since despite the change in environment, the authors kept the properties of objects obtaining high attention the same. They claimed that modifying those properties could lead to performance degradation.
Collision Avoidance using Reinforcement Learning [25] used DRL coupled with Long Short Term Memory Networks to assist drone navigation around city environments. Their algorithm was used to assist pilots under safety critical scenarios. In addition, they implemented curriculum learning to augment their algorithm, wherein training starts on a single task whose complexity is increased as learning progresses [26]. However, the use of attention based mechanisms including transformers have been used to replace LSTMs and GRUs due to their inherent limitations [27]. Moreover, [18] developed an autonomous multi-rotor flight DRL algorithm that enabled collision avoidance under novel scenarios while encouraging exploration. The algorithm used depth images as input while flying across an enclosed space with the intention of maximizing the number of steps taken before a crash. Since the rewards obtained for this task was sparse, they developed a guidance based approach to motivate their agent to explore [28]. In addition to the existing standard D3QN networks (online and target), an additional CNN network was used to predict the future state and this prediction was then compared to the states in the replay memory. Thereby, they rewarded the agent if it moved to states that had least similarity with the ones already present in the replay memory. A Bayesian Gaussian Mixture Model was used to obtain a similarity value between the states. This technique enabled the agent to explore the environment and address the sparsity in rewards. The shortcoming of this algorithm is that the Gaussian Mixture Model used for assessing similarity is computationally costly and training would take a long time for large environments. Also, [29] used Deep Recurrent Q-Networks augmented using Temporal attention for the purpose of collision avoidance in multi-rotors. In addition, they use Conditional Generative Adversarial Neural Networks to estimate depth images from inputs obtained using a monocular depth camera. This algorithm was tested in a simulated room full of people and their algorithm was able to beat conventional baselines such as DQN and D3QN. A potential drawback of this algorithm is that [30] experimentally showed that the addition of recurrent layers did not help improve the performance of the algorithm relative to stacking observations while using a CNN. Therefore, stacking multiple observations together before passing it through a CNN could make the use of Recurrent Q-Networks redundant while reducing the computational overhead obtained from using RNNs.

Duelling Double Deep Q Networks
Our work deals with a single RL agent that has to make sequential decisions in discrete time steps while interacting with its environment, . The state space, S contains M images, s t = (x t−M +1 , ..., x t ), where t refers to the time step. Also, we take our action space, A to be discrete, a t = 1, ..., | A | and depending upon the action the agent obtains a reward r t . As previously described, the agent in a RL algorithm attempts to maximize its expected return. In addition, the return is subject to discounting, γ which is used to determine the value difference between future and current rewards.
The value of being in state s or the state-action (s, a), provided the agent follows the policy π is given by - Dynamic programming can be used to compute the following Q function - The optimal Q * (s, a) is given by, Q * (s, a) = max π Q π (s, a). Also, we get V * s = max a Q * (s, a) from a deterministic policy, a = arg max a ∈A Q * (s, a ). Therefore the optimal Q function can be obtained as follows - Moreover, based on the above equations, a new function can be defined that is used to associate the Q and value functions; A π (s, a) = Q π (s, a) − V π (s), where A is the advantage function From the preceding equations, it is easy to discern that the , E a π(s) [A π (s, a)] = 0. The relative significance of each action can be found using the advantage function, since it is the difference of the value of taking a specific action within a state and the value of being in that state. To approximate the value function, a deep Q-network can be employed: Q(s, a; θ) having parameter θ. Training the DNN involves minimizing the following loss function; , and i is the iteration at that time step. Here θ − constitutes the online and target network parameters. Having two networks, instead of one, has been shown to improve the stability of the DQN. As per this technique, the online network is updated every iteration, however the target network is allowed to update its parameters only at specific iterations [16]. The downside of both DQN and Q-learning is that the values used to choose and evaluate an action are the same, leading to value estimates that are overoptimistic. Double Deep Q-Networks addressed this issue by replacing the target y DDQN i with y DQN i as given below; [31] The motivation behind the Dueling Network Architecture was the observation that calculating the action values for various states did not help the agent. The authors demonstrated this while playing Enduro, the knowledge regarding left or right movements did not affect the game unless there was a chance of collision . The architecture consisted of a Qnetwork called the duelling network. The lower Convolutional layers of the dueling network were then split up into two fully connected layers estimating the advantage and value functions. Ultimately, the two layers were joined to output a Q function consisting of Q values for every action. [16] Furthermore, the design of the Dueling Network Architecture consisted of a few intricate details. We know that Q π (s, a) = V π (s) + A π (s, a) and Also, in the case of a deterministic policy, a * = arg max a ∈A Q(s, a ), we can infer that Q(s, a * ) = V (s) leading to A(s, a * ) = 0 The two fully connected layers in the dueling network yield a scalar V (s; θ, β) and the | A | − dimensional vector A(s, a; θ, α) respectively. Here α, β represent the parameters of the fully connected layers while θ denotes the convolution parameters. Moreover, the authors claimed that inferring the following equation from the above steps would be wrong; Q(s, a; θ, α, β) = V (s; θ, β) + A(s, a; θ, α) This is because V (s; θ, β) and A(s, a; θ, α) might not be good estimates of their functions. In addition, Q(s, a; θ, α, β) is not the Q-function but only an estimate of it. Also, the authors found that the above equation is unidentifiable, since one cannot find unique values of V and A if they have Q. This problem can be solved by coercing the estimator of the advantage function at the specified action to provide no advantage. Therefore, the authors ran forward mapping using the network's final module leading to the advantage function estimator obtaining a zero advantage for the specific action. Q(s, a; θ, α, β) = V (s; θ, β) + (A(s, a; θ, α) − max a ∈|A| A(s, a ; θ, α)) Also, from a * = arg max a ∈A Q(a, a ; θ, α, β) = arg max a ∈A A(s, a ; θ, α) leads to Q(a, a * ; θ, β) = V (s; θ, β). Therefore from the layers A(s, a; θ, α) we get an estimate of the advantage function whereas from the layers V (s; θ, β) we get the value function estimation. In addition, to increase the stability of optimization the authors replaced the max operator with average; Q(s, a; θ, α, β) = V (s, θ, β) + (A(s, a; θ, α) − 1/|A| a A(s, a ; θ, α)) Finally, it was noted that learning algorithms using Q networks in general could now be trained on the dueling architecture. [33] Self-Attention Models Self-Attention Models (SAM) can be used to obtain attention weights among all nodes within a set, N : R n×f .
Here n and f represent the number of nodes and the features associated with it respectively, The result of a SAM can be understood as an edge matrix with dimension n × n, E : R n×n . The edge matrix enables the observer to discern the relation between various nodes leading to a better understanding of how the DNN "reasons". Nodes that are highly related can communicate and therefore update their parameters. This entire process takes place within a single step and is referred to as the relational module. The output of a relational module, N : R n×f , is then sent to the next relational module. SAM have been shown to work with great success on language models in the past. However, in recent years it has also been used and demonstrated great promise on vision tasks [34], [35]. However, the visual data needs to be pre-processed before it can be used as input within a relational module. In essence, in order to represent images as nodes we need to convert pixel data into objects.To achieve this, a CNN is used to convert the input images into a collection of feature maps. The feature maps are responsible for learning fine and rough features that are present within the images. These features are then concatenated together along its channels thereby creating a cuboid. This cuboid can then be sliced along the horizontal and vertical dimensions to create objects that can be viewed as nodes of a graph which is used as input to the relational module. The relational module architecture described below was presented in the paper by [20]. The node matrix, N : R n×f is projected into key, query and value matrices. The matrices responsible for projecting the initial node matrix are set as learnable parameters during training and should converge to those than can generate optimal attention weights from the key, value and query matrices. To facilitate a better understanding, the SAM for two nodes, a : R 10 and b : R 10 has been described here. We get key, value and query matrices from these nodes through their projection matrices. a Q = a T Q, a K = a T K, a V = a T V To obtain the self similarity for the nodes, an inner product operation is performed between the key and query matrices of the respective nodes. Generally the similarity measure for a node with itself is expected to be high. However, depending upon its key and query that might not be the case. An un-normalized scalar attention weight is generated by the product of the key and query matrices of a particular node, w a,a =< a Q , a K >. This process can now be extended to both of the nodes a and b. This leads to four attention weights, (two for the interaction between the nodes and two for interaction with themselves). A softmax function is then used to normalize the attention weights so that each of them are between [0, 1] and they sum to 1. This normalization is crucial to ensuring that the attention weight focus only on the necessary nodes. The attention weights for the nodes a and b are stored in a 2 × 2 matrix. Subsequently, the value matrix gets updated by the product of the attention weights and the value matrix. Therefore, each node is modified depending upon its connection with the other. This process can now be generalized to n nodes. The process of generating an attention matrix by multiplying the key and query together and then using it to update the value matrix along with normalization can be combined and represented as a matrix operation -N = sof tmax(QK T )V Here n, f, Q, K and V represent the number of nodes, node feature vector dimension, query matrix, key matrix and value matrix respectively with the following dimensions; Q : R n×f , K T : R f ×n and V : R n×f . The attention weight matrix (QK T ) will have dimension n × n with the value at (x, y) demonstrating the strength of the interaction of node x with the node at y. The product of the normalized attention weight matrix, A with the value matrix updates the feature vector of all nodes corresponding to its interaction with others, leading to a node matrix with dimensions;N : R n×f . The last step to create a relational block is to run a linear layer over the resulting node matrix before implementing non-linearity for augmenting the learning process. Multiple relational blocks can be stacked together to increase the model complexity leading to better learning of higher order relations [19], [27].

Multi-Head Self Attention
The use of softmax in SAM models restricts the the attention weights that can be placed on individual objects, especially for cases where there are a large number of nodes. Moreover, this leads to cases wherein the attention is being placed on a limited number of nodes and others are being ignored. In order to mitigate the amount of information lost, we can use multiple independent attention heads and subsequently concatenate them together. Therefore, these attention heads could concentrate on different areas of the image, thereby increasing the amount of learning without affecting the relational modules. Another advantage of attention heads is that nodes corresponding to respective heads can concentrate on a smaller set of nodes, instead of providing a minute amount of weights by trying to cover all the nodes, leading to increased interpretability [19], [27].

Problem Statement
Our goal is to train a multi-rotor to avoid obstacles while flying around an area using DRL algorithms augmented with SAMs and CNNs. In addition, we test the performance of our trained algorithms on different weather conditions as well as new environments. Finally, we visualize the attention weights in order to interpret the decisions made by our algorithm.

Microsoft Airsim
The simulation environment was generated using the Microsoft Airsim Simulator, which provided the physics behind the control of the UAV Quadcopter used in the project. It can be implemented like a plugin for Unreal and can work with all Unreal environments. This is beneficial for robotic applications such as collision avoidance since evaluating such algorithms in the real world is expensive. Furthermore, Airsim can accommodate UAV movements such as pitch, roll and yaw through its API calls. In addition, the communication between the python RL program and the simulation occurs through the Airsim API. Figure 1, below shows the 3-D environments developed for training our agent using the Unreal Engine [36], [37].

MDP Formulation
State Space: The state space S for our algorithm is in the form of depth images taken by the depth camera during training in Airsim (Figure 2). Similar to [16], we stack 5 frames together to model the sequential movement of the drone.  = {a 0 , a 1 , a 2 , a 3 } The linear velocity (v) is controlled by two actions, a 0 and a 3 with a speed of 1m/s and 2m/s respectively. The actions a 1 and a 2 consist of a combination of angular and linear velocity. a 1 makes the multi-rotor take a 30°yaw-right and then move in the forward direction at 1m/s. Similarly, a 2 forces the agent to take a 30°yaw-left and then move forward at 1m/s. Reward Function: The reward function was designed so that the UAV learns to avoid collisions while staying away from walls and objects. Therefore it consists of two major components. The first component gives it a negative reward of -10 in case it collides against the wall. However, minor brushes against the wall are ignored. The next component rewards the UAV for staying away from objects. At each time step, it extracts the centre of the image feed using 20% of the width and height of depth frame. Next, it finds the minimum value within that region β and compares it against a minimum distance threshold, δ = 3. If the minimum value of the center of the depth image is greater than the threshold, we consider the UAV to be in a safe region and reward it proportionally based on how far it is from the wall. Therefore under this condition, the agent gets a reward of 3 + β/5. This gives the agent a reward of 3 for maintaining that threshold along with a proportionally increasing reward for finding the "most-open" area centered in front of it. For cases where β is less than δ, a negative reward proportional to how close it is to the walls is placed on the agent, equalling δ/(β + 0.5) * −1 . We add a 0.5 to the denominator to account for cases where β = 0, when the multi-rotor is extremely close to the wall, and prevent the expression from becoming − inf.
Collision Detected Model Dynamics: The reinforcement learning model largely follows work done by [20]. It takes in depth images as input and calculates the Q(s t , a t ) for all actions while it is at the state s t . After taking an action based on arg max a Q(s t , a t ) it reaches the next state s t+1 . The tuple containing the informa-tion (s t , a t , r t , s t+1 ) are stored in the replay memory M . In the event of a crash, the environment resets and the UAV returns to its initial position. The model uses a CNN to learn features obtained from the simulation depth camera. Next, it converts these features along the channel dimension to form individual nodes. This converts our two-dimensional image into a graphbased representation. Moreover, it projects these nodes into key, query and value matrices. This representation enables the attention algorithm to compute the relation between different nodes and generate an attention matrix. The last steps include running the attention matrix through linear layers and applying pooling operations (similar to that of CNN operations) to generate an output layer with nodes equaling the number of actions of the agent.

RESULTS AND DISCUSSIONS Training Results
In order to compare the performance of the SAM model with a conventional baseline, we trained two D3QN agents. One agent had a SAM (with 3 heads) as its function approximator whereas the other had a CNN. Each agent was trained for a total of 8000 epochs. While tuning the number of epochs, we found that using a large number of epochs led to catastrophic forgetting [38], wherein the agent's performance would suddenly drop and it seemed to 'forget' whatever it had learned during previous iterations. Furthermore, the training metrics are calculated based on episodes, which can be defined as the series of steps between each episode reset. The average episode length (Figure 3) and the average reward ( Figure  4) obtained by the agent was used to discern how well the learning process was going. The D3QN SAM and the D3QN CNN have quite similar plots. Also, the mean episode length and episode reward follow the same trend, which implies that that reward function is helping the agent with its objective. Moreover, during the epsilon-decay process, wherein the is reduced during the course of training, the final epsilon was kept at 0.05. This makes the agent take rare, random actions even after the epsilon has completely decayed. Furthermore, we found that it was important to view the training videos in simulation. Initially, we set our actions a 1 and a 2 to consist only of yawing actions minus the linear velocity. The agent increased its rewards and steps just by staying at one place and rotating indefinitely. While comparing our work with [18], we noted that the training times using the Guidance Training algorithm were a lot larger and that it took days to train before it attained a reasonable policy. In contrast, the SAM model was able to consistently increase its rewards and reduce its crash rate in a few hours. Furthermore, [29] used temporal attention with DRL that allocates weights to past observations(images) based on its importance and then predicts a trajectory while faced with moving obstacles. However, our SAM algorithm instead detects the relationship between different objects in a single image, which allows the algorithm to be robust in the presence of noise and ,to a certain extent, invariant to changes in the obstacles geometry [39].

Testing Results
To test the robustness of our algorithm, we added various obstacles as well tested the performance of the algorithm under different weather conditions including rain, snow and falling leaves for 1000 time steps. During this evaluation, the parameters were fixed and the D3QN algorithms were only allowed to exploit without doing any exploration. The algorithms were first tested on the environment in which they were trained (Basic Environment). Next, we developed a new environment consisting of cylindrical and rectangular beams as shown in Figure 5 and referred to it as the Complex Environment. Finally, we changed the weather conditions within the Basic environment using a combination of snow, rain and leaves and called it the Turbulent Environment ( Figure 6). The results of the testing process are shown in Table I.
It is evident from   D3QN-SAM had 0 failures in the Basic Environment, while the D3QN-CNN failed 9 times which is surprising as both algorithms had similar results during training. The architecture of the Complex Environment was drastically different from the Basic Environment for we wanted to check whether our algorithms actually learned a collision avoidance policy rather than just follow a fixed path that minimized contact with obstacles in the training environment. In addition, this environment was filled with beams and the agent would have to constantly make detours to avoid crashing. Based on the low number of crashes we can confidently claim that the algorithms learned the desired policy with the D3QN-SAM having just 1 failure in this environment. The Turbulent Environment was designed so as to distort the camera feed and therefore the input states provided to the DRL algorithm. The change in the depth image sent to the algorithm can be viewed at the lower left of Figure 6. The D3QN-SAM was robust to change in this feed for, based on its relational modules, it could identify the relation between objects and place attention on the pertinent aspects of the state. One way to explain this is that the former algorithm learned a better and robust policy while obtaining the same rewards and further investigation needs to be done regarding this. The rewards obtained by the Complex Environment is slightly lower than that of the Basic Environment. This can be attributed to the fact that it has many obstacles present in the form of vertical beams and one aspect of the reward depends on how far the agent is from any obstruction. Furthermore, the rewards present in the Turbulent Environment seem inconsistent with the other environments. The objects floating around in the Turbulent Environment, such as leaves, cause the depth sensor to obtain low values of depth even when the agent is far from walls or beams. These low values in turn generate negative reward values.

Interpreting the D3QN-SAM
A major use of attention models in DNN is the ability to visualize its weights, enabling a better understanding of how the network makes its decisions. The attention weights of the D3QN agent has been visualized, presented in green color,  (Figure 7). It can be seen that a relatively large amount of attention is placed on walls that are farthest from the UAV, which can be interpreted as the agent trying to focus on the optimal distance to increase it rewards. However, the choice to place the attention weights in Figure 7b is sub-par, for it would have been better to focus on the opening next to the walls. Perhaps increasing the rewards for staying away from walls and obstacles would lead to better placement of attention weights [19]. This paper investigates the integration of SAM with DRL for the purpose of UAV Collision Avoidance, while comparing it with a conventional CNN based model. While the training results were quite similar, the DRL-SAM outperformed the DRL-CNN during testing. Also, we show that the DRL-SAM algorithm is interpretable and therefore can be incorporated into safety-critical vision algorithms.
The next steps will involve getting the algorithm ready to be transferred onto a real drone. To further reduce the chances of collisions both in simulation and real-life we propose, that along with an penalty of coming close to objects, the episode should end with a large negative reward if the depth values reduce below a threshold. This would further encourage the UAV to keep a minimum distance from obstacles under all circumstances. Finally, based on the work by [29] and [30], we suggest a study on understanding the efficacy of stacking a larger number of observations as input to a CNN. While traversing difficult environments, the UAV would need to memorize larger trajectories for cases where they are moving obstacles present.