ConViViT -- A Deep Neural Network Combining Convolutions and Factorized
  Self-Attention for Human Activity Recognition
The Transformer architecture has gained significant popularity in computer
vision tasks due to its capacity to generalize and capture long-range
dependencies. This characteristic makes it well-suited for generating
spatiotemporal tokens from videos. On the other hand, convolutions serve as the
fundamental backbone for processing images and videos, as they efficiently
aggregate information within small local neighborhoods to create spatial tokens
that describe the spatial dimension of a video. While both CNN-based
architectures and pure transformer architectures are extensively studied and
utilized by researchers, the effective combination of these two backbones has
not received comparable attention in the field of activity recognition. In this
research, we propose a novel approach that leverages the strengths of both CNNs
and Transformers in an hybrid architecture for performing activity recognition
using RGB videos. Specifically, we suggest employing a CNN network to enhance
the video representation by generating a 128-channel video that effectively
separates the human performing the activity from the background. Subsequently,
the output of the CNN module is fed into a transformer to extract
spatiotemporal tokens, which are then used for classification purposes. Our
architecture has achieved new SOTA results with 90.05 \%, 99.6\%, and 95.09\%
on HMDB51, UCF101, and ETRI-Activity3D respectively.
Rachid Reda Dokkar, Faten Chaieb, Hassen Drira, Arezki Aberkane
ConViViT - A Deep Neural Network Combining Convolutions and Factorized Self-Attention for Human Activity Recognition

###### Abstract

The Transformer architecture has gained significant popularity in computer vision tasks due to its capacity to generalize and capture long-range dependencies. This characteristic makes it well-suited for generating spatiotemporal tokens from videos. On the other hand, convolutions serve as the fundamental backbone for processing images and videos, as they efficiently aggregate information within small local neighborhoods to create spatial tokens that describe the spatial dimension of a video. While both CNN-based architectures and pure transformer architectures are extensively studied and utilized by researchers, the effective combination of these two backbones has not received comparable attention in the field of activity recognition. In this research, we propose a novel approach that leverages the strengths of both CNNs and Transformers in an hybrid architecture for performing activity recognition using RGB videos. Specifically, we suggest employing a CNN network to enhance the video representation by generating a 128-channel video that effectively separates the human performing the activity from the background. Subsequently, the output of the CNN module is fed into a transformer to extract spatiotemporal tokens, which are then used for classification purposes. Our architecture has achieved new SOTA results with 90.05 %, 99.6%, and 95.09% on HMDB51, UCF101, and ETRI-Activity3D respectively.

 Activity Recognition, Transformer, CNN

## I Introduction

Activity recognition can be defined as allowing the machine to recognize/detect the activity based on information received from different sensors. These sensors can be cameras, wearable sensors, and sensors attached to objects of daily use or deployed in the environment. In this work, we are interested in activity Recognition using RGB videos. RGB videos are a complex type of data, that contain complex spatiotemporal dependency.

To extract spatial features, we utilized a CNN module inspired by [1]. The primary objective of this CNN module is to enhance the representation of the video by extracting spatial tokens.

The advantages of applying CNNs to video data have been extensively discussed in [1]. It has been affirmed that by applying small filters to localized neighborhoods of pixels, CNNs are capable of extracting fine-grained spatial tokens. Furthermore, it has been demonstrated that CNNs outperform self-attention mechanisms in terms of spatial token extraction. Moreover, by employing CNNs as a first step, the subsequent transformer module is able to perform self-attention on a reduced spatial set of tokens and extract temporal dependencies.

To extract temporal features, we have used a video transformer architecture inspired by [2]. Within this framework, we have investigated two distinct architectures, each utilizing a different type of self-attention: Factorised Dot-Product and Factorised Self-attention. Both of these attention mechanisms apply self-attention to both spatial and temporal axes. Factorized Self-attention first applies spatial attention, followed by temporal attention on the output of the spatial attention. In contrast, the Dot-Product attention applies spatial and temporal attention to the input and subsequently fuses the results. We have proved through our experiments that factorized self-attention yields better results compared to other approaches.

The proposed architecture has achieved state-of-the-art performance on three benchmark datasets. We conducted tests on HMDB51 [3], UCF101 [4] and ETRI-Activity3D [5] and obtained state-of-the-art results on HMDB51, UCF101, and ETRI-Activity3D with 90.05 %, 99.6%, and 95.09% respectively.

## II Related Works

The existing literature on activity recognition and computer vision can be broadly categorized into three main groups: (1) CNN-based approaches, (2) Transformer-based approaches and (3) Hybrid architectures combining CNNs and Transformers.

### _CNN-based approaches_

3D Convolutional Neural Networks (CNNs) have traditionally been the primary choice for visual data processing,
encompassing various types of visual data such as images and videos. Consequently, they have held a dominant position in the field of computer vision for a considerable time. However, with the adaptation of attention mechanisms and the transformer architecture from Natural Language Processing (NLP) to Computer Vision (CV), the landscape has witnessed significant changes [6].

Previous studies have attempted to address the challenges of activity recognition using purely 3D and 2D Convolutional Neural Networks [7, 8]. However, optimizing the results and achieving satisfactory performance with a pure CNN architecture for activity recognition has proven to be challenging, primarily due to the high computational demands associated with these architectures.

To overcome these challenges, the I3D approach [9] introduced the concept of inflating pre-trained 2D convolution kernels, which allowed for better optimization of the network. In addition, other prior works focused on factorizing 3D convolution kernels in various dimensions to reduce computational complexity [10, 11, 12].

More recent studies have proposed techniques to enhance the temporal modeling ability of 2D CNNs [13, 14]. However, due to the inherent nature of CNNs, which aggregate information within a small window of the neighborhood, these approaches did not achieve significant improvements in performance.

Taken together, these prior works have explored different strategies to address the challenges of activity recognition using CNN architectures. While attempts have been made to optimize and enhance the performance of pure CNNs, limitations related to computational requirements and the inherent nature of CNNs' spatial aggregation persist.

### _Transformer-based approaches_

Since the introduction of the vision transformer [6], numerous studies have embraced the transformer architecture for computer vision tasks. These works have consistently surpassed the results achieved by CNNs. This is due to the transformers' ability to capture long-range dependencies and effectively attend to important regions of the input through self-attention mechanisms.

Several notable works have contributed to the adoption and advancement of the transformer architecture in computer vision. These include works such as [15, 16, 17, 18], which propose various variants for spatiotemporal learning in video analysis. These variants aim to harness the power of transformers in capturing both spatial and temporal information for more comprehensive video understanding.

Video Vision Transformers (Timesformer [19] and ViViT [2]) are among the early Transformers approaches for action recognition. They introduce innovative embedding schemes and adaptations to ViT [17] and other related Transformers for modeling video clips.

In [19], the authors propose a tokenization scheme called uniform frame sampling based on a randomly selected frames from a video. Along similar lines, ViViT [2] introduced Tubelets Embedding to effectively preserve contextual time data within videos and handles 3D volumes instead of frames. Four different variants were proposed based on the attention technique: Spatiotemporal attention, Factorized Encoder, Factorized self-attention, and Factorized dot-product attention.

Simultaneously, other research efforts have focused on mitigating the computational cost associated with transformer architectures while still achieving impressive results. An example of such work is the Swin Transformer [20], which presents an innovative architecture designed to strike a balance between computational efficiency and powerful performance.

Collectively, these works have significantly propelled the adoption of transformer architectures in computer vision. By capitalizing on their ability to capture long-range dependencies and leverage self-attention mechanisms, these architectures have demonstrated remarkable capabilities in various visual tasks.

The self-attention mechanism is considered inefficient when it comes to encoding low-level features. To address this limitation, the Swin Transformer approach introduces a solution by applying attention within a local 3D window. This localized attention mechanism allows for more efficient encoding of low-level features.

### _Hybrid approaches_

In recent research, efforts have been made to incorporate convolutional neural networks into the transformer architecture for image recognition tasks. However, these approaches have not adequately addressed the spatiotemporal aspect of videos. Recognizing this limitation, Uniformer [1] presented an architecture specifically tailored for video understanding, utilizing a concise transformer format based on 3D convolutions that unifies convolutions and transformers. By integrating convolutional operations into the transformer architecture, Uniformer aims to combine the strengths of both convolutional neural networks and transformers, resulting in an improved framework for feature encoding.

In this work, we adopt 3D convolutions to address the inefficiency of self-attention in encoding low-level features. In fact, 3D convolutions enhance the capability of our model to capture both spatial and temporal information effectively.

## III Proposed Method

### _Architecture overview_

The proposed architecture consists of two main modules, as depicted in Figure 1: a CNN module to extract spatial features followed by a transformer module.

The CNN module plays a crucial role in capturing spatial information from the input data. It applies convolutional operations to extract relevant features that describe the spatial characteristics of the input images. The primary objective of this CNN module is to enhance the representation of the video by extracting spatial tokens. Its output is later fed to the transformer module.
The transformer module is the factorized self-attention transformer proposed in [2]. It takes advantage of its self-attention mechanism to capture long-range dependencies and model the interactions between spatial features. This module leverages the encoded spatial information from the CNN Module to extract spatiotemporal features that are vital for accurate action classification.

Inspired by the contributions of these two works, we propose an hybrid approach that combines the strengths of CNNs for extracting spatial cues and the transformer architecture for extracting spatiotemporal tokens. Our CNN module aims to enhance the video representation by transforming it from a three-channel video to a 128-channel video. Subsequently, the transformer takes the output of the CNN module and applies a Patch Embedding similarly to [1]. Then a factorized self-attention is applied generating a rich spatiotemporal representation that will be used by the classification head.

### _Patch Embedding_

The main purpose of Patch Embedding is to provide the order information to the transformer by slicing the input into \(16\times 16\) patches. Our proposed patch embedding block is inspired by the design and implementation of Uniformer's Dynamic Position Embedding (DPE) architecture [1] since the use of DPE improves the state-of-the-art results by \(0.5\%\) and \(1.7\%\) on ImageNet and Kinetics-400, respectively. This shows that by encoding the position information, DPE can maintain the spatiotemporal order, thus contributing to better learning of the spatiotemporal representation [1].

The main contribution here is the use of a CNN layer with \(0\) padding to create a more adequate representation. The fact that the transformer does not take video as input makes the application of a CNN layer more advantageous because this layer allows to manipulate and adjust how we introduce the order information to obtain better results.

### _CNN Block_

The proposed CNN block aims to extract spatial information and offers a compact spatial representation to be fed to the transformer block. It is based on 3D-CNNs and Depth-Wise CNNs architectures (see figure 1) as follows:

* **DW 3D-CNN \(3\times 3\times 3\)**: the depth-wise 3D-CNN aims to extract the spatial features of each 3D neighborhood (\(3\times 3\times 3\)). It consists in applying a single convolutional filter for each input channel. This allows to better extract spatial features.
* **3D-CNN \(1\times 1\times 1\)**: the 3D-CNN (\(1\times 1\times 1\)) aims to reduce the dimension of the input before applying the (\(5\times 5\times 5\)) filter to save computation time.
* **3D-CNN \(5\times 5\times 5\)**: The application of a 3D filter of size 5 (larger than 3) allows to have a more global representation of the spatial neighborhood.
* **3D-CNN \(1\times 1\times 1\)**: the 3D-CNN (\(1\times 1\times 1\)) is intended to increase the size of the input to give more information to the following steps.

### _Spatiotemporal Transformer Module_

The transformer block is the most important part of the proposed architecture. It takes the output of the CNN Module (spatial representation) in order to create a spatiotemporal representation. The application of attention allows any transformer to focus on the most important parts of the input as well as to understand the long sequences which allows extracting the temporal dependencies from a spatial sequence.

To extract temporal features, two types of self-attention could be chosen, the factorized dot product and the factorized self-attention proposed in [2]. Both apply attention to the spatial and temporal axes.

Factorised self-attention _;_ It consists in applying attention on the spatial axis of the input followed by temporal attention. The application of attention on the spatial axis first allows us to take into consideration the dependencies between

Fig. 1: Overall proposed architecture for Human Activity Recognition
the spatial tokens and to deduce the most important parts that characterize this axis, thus preparing the input for the next operation of self-attention. The result of the self-attention on the spatial axis will be the input for the self-attention on the time axis that has the goal of the extraction of the spatiotemporal features.

Factorized Dot-Product attentionFactorized self-attention is a way of applying self-attention. The idea is to apply attention on the spatial axis of the input X and then have an output Y that we will apply attention on its temporal axis.

The application of attention on the spatial axis first allows to take into consideration the dependencies between the spatial tokens and to deduce the most important parts that characterize this axis, thus preparing the input for the next operation of self-attention. The result of the self-attention on the spatial axis will be the input for the self-attention on the time axis that has for the goal of the extraction of the spatiotemporal features.

Although both variants are interesting, the factorized attention seems to be more suitable for our architecture (non-video inputs).

## IV Experiments

### _Datasets_

* **ETRI-3D**[5]: ETRI-3D is the first large-scale RGB-D dataset of the daily activity of the elderly (\(112\ 620\) samples). ETRI-3D is collected by Kinect v2 sensors and consists of three synchronized data modalities: RGB video, depth maps, and skeleton sequences. To film the visual data, 50 elderly subjects are recruited. The elderly subjects are in a wide age range from 64 to 88 years old, which leads to a realistic intra-class variation of actions. In addition, they acquired a dataset for 50 young people in their twenties in the same manner as the elderly.
* **UCF101**[4]: UCF101 is an action recognition dataset of realistic action videos collected from Youtube with 101 action categories. With 13320 videos of 101 action categories, UCF 101 offers a wide variety of actions with the presence of large variations in camera movement, object appearance and pose, object scale, viewpoint, cluttered background, lighting conditions, etc.
* **HMDB51**[3]: The HMDB51 dataset is a large collection of realistic videos from a variety of sources, including movies and web videos. The dataset consists of 6,849 video clips from 51 action categories (such as "jump" and "laugh"), with each category containing at least 101 clips. The original evaluation scheme uses three different training/test divisions. Within each division, each action class has 70 clips for training and 30 clips for testing.

### _Visualization of ConViViT shallow and Deep Layers outputs_

Figure 2 is a visualization of the effect of our spatial and spatiotemporal block in the shallow and deep layers.

We observe that the output of the first CNN block allows to localize the person (see figure 2(a)). Then the output of the next 3D-CNN block which is the output of the spatial module gives more importance to the person who makes the action (see figure 2(b)). Figure 2(c) shows five attention maps of ConViViT computed for a sequence of five images showing an action from HMDB51 dataset. Each attention map refers to the visualizations of the attention weights computed between each patch in the image. The last attention map which refers to the last frame of the action sequence shows that our model has succeeded to capture the entire trajectory of the action (red zones).

### _Ablation study_

In this section, we will investigate the influence of different modules of the proposed architecture on the overall performance. All experiments in this study are conducted on the HMDB51 dataset.

Mainly we focus on the usefulness of using a CNN block to extract spatial features as well as the usefulness of factorized attention.

As a reminder, the factorized attention block is inspired by the Vivit [2] architecture and the CNN block by Uniformer [1]. In order to study the impact of CNN and factorized attention, we compare the variants of the proposed architecture with Uniformer and Vivit. So, the Vivit architecture is based only on the transformer which applies the factorized attention directly to the input. The Uniformer consists of a hybrid architecture based on the general formula of attention introduced in the first vision transformer [6].

#### Iv-C1 Factorized Attention vs Factorized Dot Product Attention

We compare the two variants of self-attention: The factorized self-attention used in our architecture and the factorized dot product.

As illustrated in Table I (first two rows), the factorized attention outperforms the factorized dot-product architecture.

#### Iv-C2 Impact of the spatial block

To illustrate the importance of the proposed spatial block, we compare our results with those obtained by Vivit [2]. The table I (first and third rows) shows that the proposed architecture exceeds Vivit by \(9.87\%\). We opted for a hybrid architecture of CNN and transform it to prepare a spatial representation before applying any type of attention. The result of our test with dot-product attention supports our hypothesis regarding the use of CNN before transformers in computer vision.

In order to study the impact of the number of the required CNN blocks in the CNN Module, we compare the results obtained by our architecture based on a single CNN Block with two CNN Blocks. We notice that using two CNN Blocks yields to \(90.05\%\) accuracy versus \(64.47\%\) using one CNN block (see Table I - first and fourth rows).

#### Iv-C3 Spatio-temporal block

To prove the value of our spatio-temporal block we can compare our results with the results of Uniformer [1] since they opted for a hybrid architecture too but they used the general formula of attention introduced in the first vision transformer [6]. This experiment reveals an improvement of accuracy by \(4.69\%\) when comparing our
architecture (\(90.05\%\)) to first vision transformer [6] (\(85.36\%\)) (see Table I - First and fifth rows).

Actually, applying normal attention to the output of CNN gives good results but our choice to apply factorized attention is better because applying attention on an axis allows the model to see the dependencies between the elements on that axis. Thus, by applying attention to the spatial and then temporal axis we obtained results that exceeded that of Uniformer.

### _Comparison with state-of-the-art_

The table II represents a comparison between our architecture and the state-of-the-art architectures on HMDB51, we added the results of Uniformer and Vivit since they were not tested on HMDB51. Our architecture improved the previous state of the art by \(2.49\%\) and achieved a new SOTA result of \(90.05\%\).

The table III represents a comparison between our architecture and seven other state-of-the-art architectures tested on UCF101. The results of Uniformer and Vivit have not been included because they were not tested on UCF101. Our architecture improves the previous state of the art by 0.22% and achieved a new SOTA result of \(99.6\%\).

The table IV represents a comparison between our architecture and the FSA-CNN [5] architecture. FSA-CNN is a CNN architecture that takes as input videos (RGB) and/or skeleton data. It is based on a deep CNN network and an innovative approach to replace the activation functions. Our model outperforms the FSA-CNN [5] RGB model by more than \(4.9\%\) and FSA-CNN RGB+S by \(1.39\%\). Although the authors in [5] claim to include spatiotemporal variation in

Fig. 2: Visualization of spatial and spatiotemporal Transformer modules outputs on a clip from HMDB51 dataset
action data, a CNN network that takes RGB video and skeleton data as input is not able to extract a complete spatiotemporal representation due to the nature of CNN and the complexity of the data (2 modalities of very different types). Our architecture outperforms FSA-CNN thanks to the proposed transformer that supports the extraction of spatiotemporal dependencies.

## Conclusion and perspectives

In this paper, we propose a novel sequential combination of 3D CNN convolution and a spatiotemporal transformer. Experiments show that the proposed architecture achieves new SOTA results with \(90.05\%\), \(99.6\%\), and \(95.09\%\) on HMDB51, UCF101, and ETRI-Activity3D datasets respectively. In future work, we will investigate further schemes for combining CNN architectures with transformers.
