Home

Awesome

Self-supervised Video Representation Learning Using Inter-intra Contrastive Framework

Official code for paper, Self-supervised Video Representation Learning Using Inter-intra Contrastive Framework [ACMMM'20].

Arxiv paper Project page

Requirements

This is my experimental enviroment.

It seems that PyTorch 1.7.0 is not compatible with current codes, causing poor performance. #9

Inter-intra contrastive (IIC) framework

For samples, we have

Our work makes use of all usable parts (in this classification category) to form an inter-intra contrastive framework. The experiments here are mainly based on Contrastive Multiview Coding.

It is flexible to extend this framework to other contrastive learning methods which use negative samples, such as MoCo and SimCLR.

image

Highlights

Make the most of data for contrastive learning.

Except for inter-negative samples, all possible data are used to help train the network. This inter-intra learning framework can make the most use of data in contrastive learning.

Flexibility of the framework

The inter-intra learning framework can be extended to

Updates

Oct. 1, 2020 - Results using C3D and R(2+1)D are added; fix random seed more tightly. Aug. 26, 2020 - Add pretrained weights for R3D.

Usage of this repo

Notification: we have added codes to fix random seed more tightly for better reproducibility. However, results in our paper used previous random seed settings. Therefore, there should be tiny differences for the performance from that reported in our paper. To reproduce retrieval results same as our paper, please use the provided model weights.

Data preparation

You can download UCF101/HMDB51 dataset from official website: UCF101 and HMDB51. Then decoded videos to frames.
I highly recommend the pre-computed optical flow images and resized RGB frames in this repo.

If you use pre-computed frames, the folder architecture is like path/to/dataset/video_id/frames.jpg. If you decode frames on your own, the folder architecture may be like path/to/dataset/class_name/video_id/frames.jpg, in which way, you need pay more attention to the corresponding paths in dataset preparation.

For pre-computed frames, find rgb_folder, u_folder and v_folder in datasets/ucf101.py for UCF101 datasets and change them to meet your environment. Please note that all those modalities are prepared even though in some settings, optical flow data are not used train the model.

If you do not prepare optical flow data, simply set u_folder=rgb_folder and v_folder=rgb_folder should help to avoid errors.

Train self-supervised learning part

python train_ssl.py --dataset=ucf101

This equals to

python train_ssl.py --dataset=ucf101 --model=r3d --modality=res --neg=repeat

This default setting uses frame repeating as intra-negative samples for videos. R3D is used.

We use two views in our experiments. View #1 is a RGB video clip, View #2 can be RGB/Res/Optical flow video clip. Residual video clips are default modality for View #2. You can use --modality to try other modalities. Intra-negative samples are generated from View #1.

It may be wired to use only one optical flow channel u or v. We use only one channel to make it possible for only one model to handle inputs from different modalities. It is also an optional setting that using different models to handle each modality.

Retrieve video clips

python retrieve_clips.py --ckpt=/path/to/your/model --dataset=ucf101 --merge=True

One model is used to handle different views/modalities. You can set --modality to decide which modality to use. When setting --merge=True, RGB for View #1 and the specific modality for View #2 will be jointly used for joint retrieval.

By default training setting, it is very easy to get over 30%@top1 for video retrieval in ucf101 and around 13%@top1 in hmdb51 without joint retrieval.

Fine-tune model for video recognition

python ft_classify.py --ckpt=/path/to/your/model --dataset=ucf101

Testing will be automatically conducted at the end of training.

Or you can use

python ft_classify.py --ckpt=/path/to/your/model --dataset=ucf101 --mode=test

In this way, only testing is conducted using the given model.

Note: The accuracies using residual clips are not stable for validation set (this may also caused by limited validation samples), the final testing part will use the best model on validation set.

If everything is fine, you can achieve around 70% accuracy on UCF101. The results will vary from each other with different random seeds.

Results

Retrieval results

The table lists retrieval results on UCF101 split 1. We reimplemented CMC and report its results. Other results are from corresponding paper. VCOP, VCP, CMC, PRP, and ours are based on R3D network backbone.

Methodtop1top5top10top20top50
Jigsaw19.728.533.540.049.4
OPN19.928.734.040.651.6
R3D (random)9.918.926.035.551.9
VCOP14.130.340.451.166.5
VCP18.633.642.553.568.1
CMC26.437.745.153.266.3
Ours (repeat + res)36.554.162.972.483.4
Ours (repeat + u)41.860.469.578.487.7
Ours (shuffle + res)34.653.062.371.782.4
Ours (shuffle + v)42.460.969.277.186.5
PRP22.838.546.755.269.1
RTT26.148.559.169.682.8
MemDPC-RGB20.240.452.464.7-
MemDPC-Flow40.263.271.978.6-

Recognition results

We only use R3D as our network backbone. In this table, all reported results are pre-trained on UCF101.

Usually, 1. using Resnet-18-3D, R(2+1)D or deeper networks; 2.pre-training on larger datasets; 3. using larger input resolutions; 4. combining with audios or other features will also help.

MethodUCF101HMDB51
Jigsaw51.522.5
O3N (res)60.332.5
OPN56.322.1
OPN (res)71.836.7
CrossLearn58.727.2
CMC (3 views)59.126.7
R3D (random)54.523.4
ImageNet-inflated60.330.7
3D ST-puzzle65.833.7
VCOP (R3D)64.929.5
VCOP (R(2+1)D)72.430.9
VCP (R3D)66.031.5
Ours (repeat + res, R3D)72.835.3
Ours (repeat + u, R3D)72.736.8
Ours (shuffle + res, R3D)74.438.3
Ours (shuffle + v, R3D)67.034.0
PRP (R3D)66.529.7
PRP (R(2+1)D)72.135.0

Residual clips + 3D CNN The residual clips with 3D CNNs are effective, especially for scratch training. More information about this part can be found in Rethinking Motion Representation: Residual Frames with 3D ConvNets for Better Action Recognition (previous but more detailed version) and Motion Representation Using Residual Frames with 3D CNN (short version with better results).

The key code for this part is

shift_x = torch.roll(x,1,2)
x = ((shift_x -x) + 1)/2

which is slightly different from that in papers.

We also reimplement VCP in this repo. By simply using residual clips, significant improvements can be obtained for both video retrieval and video recognition.

Pretrained weights

Pertrained weights from self-supervised training step: R3D(google drive).

With this model, for video retrieval, you should achieve

Finetuned weights for action recognition: R3D(google drive).

With this model, for video recognition, you should achieve 72.7% @top1 with python ft_classify.py --model=r3d --modality=res --mode=test -ckpt=./path/to/model --dataset=ucf101 --split=1. This result is better than that reported in paper. Results may be further improved with strong data augmentations.

For any questions, please contact Li TAO (taoli@hal.t.u-tokyo.ac.jp).

Results for other network architectures

Results are averaged on 3 splits without using optical flow. R3D and R21D are the same as VCOP / VCP / PRP.

UCF101top1top5top10top20top50Recong
C3D (VCOP)12.529.039.050.666.965.6
C3D (VCP)17.331.542.052.667.768.5
C3D (PRP)23.238.146.055.768.469.1
C3D (ours, repeat)31.948.257.367.179.170.0
C3D (ours, shuffle)28.945.455.566.278.869.7
R21D (VCOP)10.725.935.447.363.972.4
R21D (VCP)19.933.742.050.564.466.3
R21D (PRP)20.334.041.951.764.272.1
R21D (ours, repeat)34.751.760.969.481.972.4
R21D (ours, shuffle)30.245.655.064.477.673.3
Res18-3D (ours, repeat)36.854.163.172.083.372.4
Res18-3D (ours, shuffle)33.049.259.169.180.673.1
HMDB51top1top5top10top20top50Recong
C3D (VCOP)7.422.634.448.570.128.4
C3D (VCP)7.823.835.349.371.632.5
C3D (PRP)10.527.240.456.275.934.5
C3D (ours, repeat)9.929.642.057.378.430.8
C3D (ours, shuffle)11.531.343.960.180.329.7
R21D (VCOP)5.719.530.745.667.030.9
R21D (VCP)6.721.332.749.273.332.2
R21D (PRP)8.225.336.251.073.035.0
R21D (ours, repeat)12.733.345.861.681.334.0
R21D (ours, shuffle)12.631.944.259.980.731.2
Res18-3D (ours, repeat)15.534.448.963.883.834.3
Res18-3D (ours, shuffle)12.433.646.963.283.534.3

Citation

If you find our work helpful for your research, please consider citing the paper

@article{tao2020selfsupervised,
    title={Self-supervised Video Representation Learning Using Inter-intra Contrastive Framework},
    author={Li Tao and Xueting Wang and Toshihiko Yamasaki},
    journal={arXiv preprint arXiv:2008.02531},
    year={2020},
    eprint={2008.02531},
}

If you find the residual input helpful for video-related tasks, please consider citing the paper

@article{tao2020rethinking,
  title={Rethinking Motion Representation: Residual Frames with 3D ConvNets for Better Action Recognition},
  author={Tao, Li and Wang, Xueting and Yamasaki, Toshihiko},
  journal={arXiv preprint arXiv:2001.05661},
  year={2020}
}

@article{tao2020motion,
  title={Motion Representation Using Residual Frames with 3D CNN},
  author={Tao, Li and Wang, Xueting and Yamasaki, Toshihiko},
  journal={arXiv preprint arXiv:2006.13017},
  year={2020}
}

Acknowledgements

Part of this code is inspired by CMC and VCOP.