This repository is a customized version of I3D Feature Extraction with ResNet. The changes were made to suit the needs of the Smart Surveillance project. It efficiently extracts video features using ResNet and provides modular scripts for debugging and flexibility.
The extract_features.py
script was customized to improve functionality and compatibility. Key changes include:
-
Skipping Insufficient Frames
- Added a check to skip directories with insufficient frames:
if frame_cnt <= chunk_size: print(f"Skipping {frames_dir}: Insufficient frames ({frame_cnt})") return None
- Added a check to skip directories with insufficient frames:
-
Enhanced Frame Clipping
- Adjusted frame clipping logic for compatibility with smaller datasets.
-
Efficient Batch Processing
- Used
torch.no_grad()
for efficient GPU memory usage during inference.
- Used
-
Refined Feature Output
- Ensured extracted features meet downstream requirements:
full_features = full_features[:,:,:,0,0,0] full_features = np.array(full_features).transpose([1, 0, 2])
- Ensured extracted features meet downstream requirements:
-
Improved Logging
- Added logs for runtime visibility:
print("batchsize", batch_size)
- Added logs for runtime visibility:
The main.py
script was enhanced to improve usability and user experience. Key updates include:
-
Summary Logging
- Logs run details in
feature_config.txt
:{ "run_date": "2024-12-02 12:00:00", "total_videos": 100, "videos_skipped": 5, "processed_videos": 95 }
- Logs run details in
-
Graceful Skipping of Videos
- Skips videos with insufficient frames and logs the issue.
-
Feature Metadata Collection
- Logs metadata for extracted features (video name, file path, dimensions).
-
Temporary Directory Management
- Cleans up temporary directories after processing.
-
Flexible Output Paths
- Automatically creates nested directories for outputs.
-
Enhanced Logs
- Added detailed logs for each video, including processing time.
The dataset is structured as follows, ensuring traceability between input videos and output features:
dataset/
├── videos/
│ ├── train_val/
│ │ ├── anomaly/
│ │ ├── anomaly_augmented/
│ │ ├── anomaly_cropped/
│ │ ├── non_anomaly/
│ │ ├── non_anomaly_augmented/
│ │ ├── non_anomaly_cropped/
│ └── test/
│ ├── anomaly/
│ ├── anomaly_augmented/
│ ├── anomaly_cropped/
│ ├── non_anomaly/
│ ├── non_anomaly_cropped/
└── features/ # Mirrors the videos/ structure
The job_scripts/
folder contains SLURM job scripts for modular feature extraction from each dataset subfolder. This approach allows for better debugging and granular processing.
extract_features_train_val_anomaly.sh
extract_features_train_val_anomaly_cropped.sh
extract_features_test_non_anomaly.sh
These scripts were designed to run on Compute Canada - Narval cluster
#!/bin/bash
#SBATCH --account=<ACCOUNT_NAME>
#SBATCH --gres=gpu:<GPU_TYPE>:1
#SBATCH --cpus-per-task=4
#SBATCH --mem=40G
#SBATCH --time=72:00:00
#SBATCH --output=train_val_anomaly_augmented.out
- Set up the virtual environment
- Install dependencies using
requirements.txt
Submit batch jobs for specific subfolders within the dataset root directory:
sbatch job_scripts/extract_features_train_val_anomaly.sh
Run the feature extraction directly:
python main.py --datasetpath="/home/$USER/scratch/dataset/videos/train_val/anomaly_augmented" \
--outputpath="/home/$USER/scratch/dataset/features/train_val/anomaly_augmented"
Logs and output files for each job are saved in .out files in the current directory.