Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

TLEE

GitHub โ€” https://github.com/fangvv/TLEE

This is the source code for our paper: TLEE: Temporal-wise and Layer-wise Early Exiting Network for Efficient Video Recognition on Edge Devices. A brief introduction of this work is as follows:

With the explosive growth in video streaming comes a rising demand for efficient and scalable video understanding. State-of-the-art video recognition approaches based on Convolutional Neural Network (CNN) have shown promising performance by adopting 2D or 3D CNN architectures. However, the large data volumes, high resource demands, and strict latency requirements have hindered the wide application of these solutions on resource-constrained Internet-of-Things (IoT) and edge devices. To address this issue, we propose a novel framework called TLEE that enables the input samples the abilities of both Temporal-wise and Layer-wise Early Exiting on 2D CNN backbones for efficient video recognition. TLEE consists of three types of modules: gating module, branch module, and feature reuse module. The gating module determines for an input video from which frame of this video to exit the per-frame computation, while the branch module determines for an input frame from which layer of the CNN backbone to exit the per-layer computation. Besides, based on the accumulated features of frame sequences from exit branches, the feature reuse module generates effective video representations to enable more efficient predictions. Extensive experiments on benchmark datasets demonstrate that the proposed TLEE can significantly outperform the state-of-the-art approaches in terms of computational cost and inference latency, while maintaining competitive recognition accuracy. In addition, we verify the superiority of TLEE on the typical edge device NVIDIA Jetson Nano.

้š็€่ง†้ข‘ๆต็š„็ˆ†็‚ธๅผๅขž้•ฟ๏ผŒๅฏน้ซ˜ๆ•ˆๅฏๆ‰ฉๅฑ•่ง†้ข‘็†่งฃ็š„้œ€ๆฑ‚ๆ—ฅ็›Š่ฟซๅˆ‡ใ€‚ๅŸบไบŽๅท็งฏ็ฅž็ป็ฝ‘็ปœ๏ผˆCNN๏ผ‰็š„ๅ…ˆ่ฟ›่ง†้ข‘่ฏ†ๅˆซๆ–นๆณ•้€š่ฟ‡้‡‡็”จไบŒ็ปดๆˆ–ไธ‰็ปดCNNๆžถๆž„ๅฑ•็Žฐๅ‡บไผ˜ๅผ‚ๆ€ง่ƒฝใ€‚็„ถ่€Œ๏ผŒๆตท้‡ๆ•ฐๆฎใ€้ซ˜่ต„ๆบ้œ€ๆฑ‚ไธŽไธฅ่‹›ๅปถ่ฟŸ่ฆๆฑ‚้˜ป็ขไบ†่ฟ™ไบ›ๆ–นๆกˆๅœจ่ต„ๆบๅ—้™็š„็‰ฉ่”็ฝ‘ๅŠ่พน็ผ˜่ฎพๅค‡ไธŠ็š„ๅนฟๆณ›ๅบ”็”จใ€‚ไธบ่งฃๅ†ณๆญค้—ฎ้ข˜๏ผŒๆˆ‘ไปฌๆๅ‡บๆ–ฐๅž‹ๆก†ๆžถTLEE๏ผŒไฝฟ่พ“ๅ…ฅๆ ทๆœฌๅœจไบŒ็ปดCNNไธปๅนฒ็ฝ‘็ปœไธŠๅŒๆ—ถๅ…ทๅค‡ๆ—ถ้—ด็ปดๅบฆๅ’Œๅฑ‚็บง็ปดๅบฆ็š„ๆ—ฉ้€€่ƒฝๅŠ›๏ผŒไปฅๅฎž็Žฐ้ซ˜ๆ•ˆ่ง†้ข‘่ฏ†ๅˆซใ€‚TLEEๅŒ…ๅซไธ‰็ฑปๆจกๅ—๏ผš้—จๆŽงๆจกๅ—ใ€ๅˆ†ๆ”ฏๆจกๅ—ไธŽ็‰นๅพๅค็”จๆจกๅ—ใ€‚้—จๆŽงๆจกๅ—็กฎๅฎš่พ“ๅ…ฅ่ง†้ข‘ไปŽๅ“ชไธ€ๅธง็ปˆๆญข้€ๅธง่ฎก็ฎ—๏ผŒ่€Œๅˆ†ๆ”ฏๆจกๅ—็กฎๅฎš่พ“ๅ…ฅๅธงไปŽCNNไธปๅนฒ็ฝ‘็ปœ็š„ๅ“ชไธ€ๅฑ‚็ปˆๆญข้€ๅฑ‚่ฎก็ฎ—ใ€‚ๆญคๅค–๏ผŒๅŸบไบŽไปŽ้€€ๅ‡บๅˆ†ๆ”ฏ่Žทๅ–็š„ๅธงๅบๅˆ—็ดฏ็งฏ็‰นๅพ๏ผŒ็‰นๅพๅค็”จๆจกๅ—ๅฏ็”Ÿๆˆๆœ‰ๆ•ˆ็š„่ง†้ข‘่กจๅพไปฅๅฎž็Žฐๆ›ด้ซ˜ๆ•ˆ็š„้ข„ๆต‹ใ€‚ๅœจๅŸบๅ‡†ๆ•ฐๆฎ้›†ไธŠ็š„ๅคง้‡ๅฎž้ชŒ่กจๆ˜Ž๏ผŒๆ‰€ๆๅ‡บ็š„TLEEๅœจ่ฎก็ฎ—ๆˆๆœฌๅ’ŒๆŽจ็†ๅปถ่ฟŸๆ–น้ขๆ˜พ่‘—ไผ˜ไบŽ็Žฐๆœ‰ๅ…ˆ่ฟ›ๆ–นๆณ•๏ผŒๅŒๆ—ถไฟๆŒๅ…ทๆœ‰็ซžไบ‰ๅŠ›็š„่ฏ†ๅˆซๅ‡†็กฎ็އใ€‚ๆˆ‘ไปฌ่ฟ˜ๅœจๅ…ธๅž‹่พน็ผ˜่ฎพๅค‡NVIDIA Jetson NanoไธŠ้ชŒ่ฏไบ†TLEE็š„ไผ˜่ถŠๆ€งใ€‚

This work was published by IEEE IoTJ (IEEE Internet of Things Journal). Click here for our paper.

Required software

  • Python 3.6+
  • PyTorch (CUDA-enabled build recommended)
  • torchvision
  • NumPy
  • Pandas
  • Pillow
  • PyYAML
  • loguru

Project Structure

TLEE/
โ”œโ”€โ”€ arch/ # 2D CNN backbones used by TLEE
โ”‚ โ”œโ”€โ”€ VGG16.py # VGG-16 backbone with feature splits
โ”‚ โ”œโ”€โ”€ BN_Inception.py
โ”‚ โ”œโ”€โ”€ efficientnet.py
โ”‚ โ”œโ”€โ”€ mobilenetv2.py
โ”‚ โ””โ”€โ”€ mobilenetv2_tsm.py
โ”œโ”€โ”€ config/ # YAML configuration files
โ”‚ โ”œโ”€โ”€ ucf101.yml / ucf101_branch.yml / ucf101_train.yml
โ”‚ โ”œโ”€โ”€ cloud_hmdb51_*_branch.yml / cloud_ucf101_*_branch.yml
โ”‚ โ””โ”€โ”€ cloud_ssv2_branch.yml
โ”œโ”€โ”€ dataset/ # Video dataset loading & transforms
โ”‚ โ”œโ”€โ”€ dataset.py
โ”‚ โ””โ”€โ”€ transform.py
โ”œโ”€โ”€ model/ # Core TLEE components
โ”‚ โ”œโ”€โ”€ tlee.py # TLEEModel & BaseModel definitions
โ”‚ โ”œโ”€โ”€ branch.py # Conv_Branch / FC_Branch classifiers
โ”‚ โ”œโ”€โ”€ branch_model.py # Auxiliary branch for early-exit decision
โ”‚ โ””โ”€โ”€ model_splits.py # Backbone splitting at intermediate layers
โ”œโ”€โ”€ tools/ # Training / inference utilities
โ”‚ โ”œโ”€โ”€ FRM.py # Feature Reuse Module (Attention / EMA / Avg / Naive)
โ”‚ โ”œโ”€โ”€ random_exit.py # Random temporal / spatial exit baseline
โ”‚ โ””โ”€โ”€ lr_schedule.py # Step learning rate schedule
โ”œโ”€โ”€ utils/ # Miscellaneous helpers
โ”‚ โ”œโ”€โ”€ config.py # YAML config wrapper
โ”‚ โ”œโ”€โ”€ compute_ap.py
โ”‚ โ”œโ”€โ”€ metrics.py
โ”‚ โ”œโ”€โ”€ misc.py
โ”‚ โ””โ”€โ”€ utils.py
โ”œโ”€โ”€ log/
โ”‚ โ””โ”€โ”€ _log.py
โ”œโ”€โ”€ env_config/ # Conda environment spec
โ”œโ”€โ”€ main.py # Main entry: train/eval with branch heads
โ”œโ”€โ”€ train_basemodel.py # Train a plain backbone (no early exit)
โ”œโ”€โ”€ test.py # Quick CPU latency baseline
โ”œโ”€โ”€ time_test.py # End-to-end latency / MACs evaluation
โ”œโ”€โ”€ test.sh / cloud_*.sh # Job submission scripts
โ””โ”€โ”€ README.md

Core Modules

TLEEModel (model/tlee.py)

The full TLEE network that wraps a 2D CNN backbone with temporal gating and multiple layer-wise branch classifiers. It produces per-frame branch predictions and aggregates them through the Feature Reuse Module (FRM).

Attribute Description
backbone 2D CNN (VGG16 / BN-Inception / MobileNetV2 / EfficientNet)
splits Cut points of the backbone features (e.g. [14, 24, 34, -1] for VGG16)
branches Conv_Branch / FC_Branch classifiers attached to each cut
crm_num Number of channel-reduction convs inside each branch
branch_scale Channel scaling factor of the reduction block
useGateFunc Whether the temporal gating module is enabled
useFeatureFunc Whether the feature-reuse aggregation is enabled

Key methods:

  • forward(frames) โ€” Run a video clip through the backbone, executing early exit at the first branch whose confidence exceeds a threshold, and aggregate exit features for the final video-level prediction.

  • get_policies(...) โ€” Helper that returns a list of exit policies (per-frame, per-layer) used by main.py to decide when to stop computation.

  • BaseModel โ€” Plain 2D-CNN baseline with the same backbone but no early-exit branches, used as a reference in ablation studies.

Branch (model/branch.py, model/branch_model.py)

Auxiliary classifiers attached to intermediate backbone layers. A Branch first reduces channels with stacked 1ร—1 convs, then applies a spatial pooling followed by a fully-connected head that outputs class logits.

Class Description
Conv_Branch(size, inplanes, outputdim) Conv-based head with 1ร—1 / 3ร—3 / 1ร—1 feature extraction
FC_Branch(size, inplanes, outputdim) Lightweight FC head (Linear โ†’ BN โ†’ ReLU โ†’ Linear)
Branch (branch_model.py) Full branch module with Channel Reduction Module (CRM) and pool head

FeatureReuseModule / FRM (tools/FRM.py)

Aggregates the features coming from exit branches of consecutive frames so that the network can form a strong video-level representation even when each frame exits early.

Class Description
AttentionFRM Two-layer MLP with sigmoid that weighs current vs. accumulated features
EMAFRM Exponential moving average over previous features, learnable ฮฒ
AveragePooling Simple mean of the two feature tensors
NaiveAdd Sum of absolute values as a naive baseline

The class instantiated in model/tlee.py is configurable via config.model.useFeatureFunc.

Random Exit (tools/random_exit.py)

Baseline used in the ablation study: at each frame, randomly decide whether to exit spatially (layer-wise) and / or temporally. Returns two boolean flags (spatial_exit, temporal_exit).

ModelSplits (model/model_splits.py)

Cuts a torchvision-style backbone into a Sequential of feature blocks and exposes the (location, output map size, channel count) of each cut, so that branches can be attached to the right tensors.

Backbone split_location split_map_size split_channels
VGG16 [14, 24, 34, -1] [56, 28, 14, 7] [128, 256, 512, 512]
BN-Inception ['_block_1', '_block_3a', '_block_4a', '_block_5a'] [28, 14, 7, 1] [192, 576, 1056, 1024]
MobileNetV2 (see model_splits.py) last stage uses mean(2).mean(3) pooling โ€”

Data Pipeline (dataset/)

dataset/dataset.py provides get_dataloader(c_data, batch_size) for the supported datasets (UCF101, HMDB51, Something-Something V2, Mini-Kinetics, ActivityNet 1.3). Frame-level preprocessing is configured per YAML file under config/.

Utilities (utils/, tools/lr_schedule.py)

  • utils/config.py โ€” Config wrapper that turns a YAML file (or hierarchy) into a Python namespace accessible as config.<key>.<subkey>.
  • utils/utils.py โ€” AverageMeter, accuracy, accuracy_gate, save_checkpoint, compute_mac, print_exp_data, etc.
  • tools/lr_schedule.py โ€” Multi-step learning rate decay triggered at the epochs specified by config.model.schedule (each step multiplies the LR by 0.1).

Usage

# 1. Create a Python environment (example with conda)
conda env create -f env_config/fe.yaml
conda activate fe

# 2. Edit a config file to point to your dataset / checkpoint,
#    e.g. config/ucf101.yml:
#      data.path_frame:   /path/to/UCF101/jpg
#      data.path_split:   /path/to/ucf101_rgb_val_split_1.txt
#      data.path_label:   /path/to/ucf101_rgb_val_split_1.txt
#      data.path_classid: /path/to/UCF101/labels/classInd.txt
#      checkpoint.init:   /path/to/pretrained_backbone.pth

# 3. Train the TLEE model with branch heads
python -u main.py config/ucf101_branch.yml

# 4. Train a plain backbone baseline (no early exit) for ablation
python -u train_basemodel.py config/ucf101_train.yml

# 5. Test a trained TLEE checkpoint
python -u test.py        # uses config/test settings
python -u time_test.py   # reports latency and GMACs

# 6. (Cluster) submit the cloud_* scripts after editing the YAML inside
bash cloud_ucf101_branch.sh
bash cloud_test_branchmodel.sh
bash cloud_test_tse.sh
bash cloud_time_test.sh

YAML quick reference (config/ucf101.yml):

Section Key Meaning
data name Dataset name (ucf101, hmdb51, ssv2, โ€ฆ)
data num_frames Frames sampled per video clip (e.g. 10)
data.preprocessing resize, crop_center, normalize Standard ImageNet preprocessing
model backbone One of vgg16, BN_Inception, mobilenetv2, efficientnet*
model branch_num Number of layer-wise exit branches
model crm_num Channel-reduction blocks inside each branch
model branch_scale Channel scaling factor of the reduction block
model useFeatureFunc Enable / disable the Feature Reuse Module
model useGateFunc Enable / disable the temporal gating module
checkpoint init Path to pretrained backbone weights

Citation

If you find TLEE useful or relevant to your project and research, please kindly cite our paper:

@ARTICLE{10176276,
  author={Wang, Qingli and Fang, Weiwei and Xiong, Neal N.},
  journal={IEEE Internet of Things Journal},
  title={TLEE: Temporal-Wise and Layer-Wise Early Exiting Network for Efficient Video Recognition on Edge Devices},
  year={2024},
  volume={11},
  number={2},
  pages={2842-2854},
  doi={10.1109/JIOT.2023.3293506}
}

For more

The same authors have published a related work on UAV-assisted mobile edge computing using Deep Deterministic Policy Gradient, see UAV-DDPG for your reference. You can also refer to their other repositories: VN-MADDPG and MTACP (IMPALA).

Contact

Qingli Wang (20120418@bjtu.edu.cn)

Please note that the open source code in this repository was mainly completed by the graduate student author during his master's degree study. Since the author did not continue to engage in scientific research work after graduation, it is difficult to continue to maintain and update these codes. We sincerely apologize that these codes are for reference only.

About

Code for paper "TLEE: Temporal-wise and Layer-wise Early Exiting Network for Efficient Video Recognition on Edge Devices"

Topics

Resources

Stars

9 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages