Git Product home page Git Product logo

blurfacedetection's Introduction

header

This project was carried out by YAI 10th, in cooperation with Alchera


Gmail NOTION


Members


Β  πŸ‘‘ KIM MINSU, YAI 7th
Β  πŸš€ KIM HYUNJIN, YAI 8th
Β  πŸŽ“ PARK JUNYOUNG, YAI 9th
  🌡 LEE SUMIN, YAI 9th
  🐯 JIN HYUNBIN, YAI 9th
Β  πŸ˜€ CHOI SUNGBEOM, YAI 9th



Requirements


Conda virtual environment setup (recommend python>=3.7)

conda create -n "environment name" python=3.7
conda activate "environment name"

Install insightface(SCRFD)

pip install -U Cython cmake numpy
pip install onnxruntime-gpu
pip install -U insight-face

Environment setting

pip install torch>=1.8.1 
pip install torchvision>=0.9.1
pip install pytorch-lightning
pip install numpy
pip install scipy
pip install opencv-python
conda install scikit-image
pip install tqdm

Git clone repo

git clone https://github.com/minsu1206/BlurFaceDetection.git

You can just clone this repo into your own computer


And finally the directory hierarchy is configured as,

FaceBlurring
β”œβ”€β”€ config
β”‚      β”œβ”€β”€ resnet18_regression.yaml
β”‚      └── .....
β”œβ”€β”€ data
β”œβ”€β”€ data_samples
β”œβ”€β”€ dataset
β”‚      β”œβ”€β”€ blur.py
β”‚      β”œβ”€β”€ create_blurring.py
β”‚      β”œβ”€β”€ dataset.py
β”‚      β”œβ”€β”€ utils.py
β”‚      └── .....
β”œβ”€β”€ experiments
β”‚      β”œβ”€β”€ results
β”‚      β”œβ”€β”€ sample_code
β”‚      └── .....
β”œβ”€β”€ legacy
β”œβ”€β”€ models
β”‚      β”œβ”€β”€ utils # dir for yolov5n.py 
β”‚      β”œβ”€β”€ edgenext.py
β”‚      β”œβ”€β”€ mobilenetv2.py
β”‚      └── .....
β”œβ”€β”€ loss.py
β”œβ”€β”€ model_factory.py
β”œβ”€β”€ recorder.py
β”œβ”€β”€ sample.sh
β”œβ”€β”€ test.py
β”œβ”€β”€ train.py
└── utils.py

Dataset


Download data

  • FFHQ

drawing

- [https://github.com/NVlabs/ffhq-dataset](https://github.com/NVlabs/ffhq-dataset) - The FFHQ dataset consists of 70,000 high-quality PNG images at 1024Γ—1024 resolution and contains considerable variation in terms of age, ethnicity and image background. - Download 1024Γ—1024 images as png (89.1GB)
```
cd /data
wget https://raw.githubusercontent.com/NVlabs/ffhq-dataset/master/download_ffhq.py
python ./download_ffhq.py --images
cd ../
```

Create & save data

I made two methods to create blur images

You have two options to create blur images. The first option is to apply blur iteratively to an clean image. Second option is to apply blur method only once. As blur label, we use 1-cosine similarity.


How to make : Guide

I show an example command to create blurred images and save them with label information.

cd ./dataset
python create_blurimg_iterative.py --path ../data/FFHQ_1024/clean --n 4
python create_blur_label.py --path ../data/FFHQ_1024/clean 

Above command would generate set of blurred images which were applied blur method four times iterative.

cd ./dataset
python create_blurimg_iterative.py --path ../data/FFHQ_1024/clean --n 4
python create_blur_label.py --path ../data/FFHQ_1024/clean --wo --multi

Above command would generate set of blurred images using multiprocess to generate faster.

cd ./dataset
python create_blur_image.py --blur defocus --iter 1

It’s how to generate blurred images with Defocus method. One blur image is generated for one clean image.

cd ./dataset
python create_blur_image.py --blur deblurgan --iter 1

This command would use DeblurGAN blur method to generate blur images.

cd ./dataset
python create_blur_image.py --blur defocus --iter 1 --scrfd True

This command would generate blur images using defocus blur method and SCRFD inference. SCRFD module is used to detect face in an image.

All generated blur images are stored in the β€œdata” folder.

data
β”œβ”€β”€ FFHQ_1024
β”‚      β”œβ”€β”€ blur_deblurGAN
β”‚      β”‚      β”œβ”€β”€ 00000
β”‚      β”‚      β”‚      β”œβ”€β”€ 00000.png
β”‚      β”‚      β”‚      β”œβ”€β”€ 00001.png
β”‚      β”‚      β”‚      β”œβ”€β”€ .....
β”‚      β”‚      β”‚      └── 00999.png
β”‚      β”‚      β”œβ”€β”€ 01000
β”‚      β”‚      β”‚      β”œβ”€β”€ 01000.png
β”‚      β”‚      β”‚      β”œβ”€β”€ 01001.png
β”‚      β”‚      β”‚      β”œβ”€β”€ .....
β”‚      β”‚      β”‚      └── 01999.png
β”‚      β”‚      └── .....
β”‚      β”œβ”€β”€ blur_defocus
β”‚      β”‚      β”œβ”€β”€ 00000
β”‚      β”‚      β”‚      β”œβ”€β”€ 00000.png
β”‚      β”‚      β”‚      β”œβ”€β”€ .....
β”‚      β”‚      β”‚      └── 00999.png
β”‚      β”‚      └── .....
β”‚      └── blur_Random
β”‚             β”œβ”€β”€ 00000
β”‚             β”‚      β”œβ”€β”€ 00000.png
β”‚             β”‚      β”œβ”€β”€ .....
β”‚             β”‚      └── 00999.png
β”‚             └── .....
β”œβ”€β”€ label_deblurGAN
β”‚      └── label
β”‚             └── data_label.csv
β”œβ”€β”€ label_defocus
β”‚      └── label
β”‚             └── data_label.csv
β”œβ”€β”€label_random
β”‚      └── label
β”‚             └── data_label.csv
└──label_val.csv

Data distribution

The following code is used to plot the distribution of the generated blur images.(Below is an example using the deblurgan method)

python data_distribution.py --path ../data/label_deblurGAN/label/data_label.csv

The distribution of the data we provided is as follows. (The x-axis is the blur label, and the y-axis is the number of images. The graph is sequentially using DeblurGAN method, Defocus method, and both methods.)

DeblurGAN Defocus Both

drawing

drawing

drawing

About 210,000 image samples were generated with random kernel-based methods according to the DeblurGAN and Defocus methods. And we extracted 100,000 samples among them, so that the overall dataset samples were evenly distributed. Training and validation dataset were matched through random split applied with the same random seed in each experiment. The training/validation dataset distribution is as follows.

drawing



Train


Supported model

Basically, we provide a models such as resnet, and also provide light weight backbones which show a fast interference speed.

Model Model Size
(.pt file)
Inference speed :
Average
Config Pre-trained Weight
ResNet18 42.916 MB 143.502 (ms) resnet18_regression.yaml https://drive.google.com/file/d/17o8oqL-ZKcR87vIEDXwcvAIiqxrZZe2y/view?usp=sharing
ResNet34 81.542 MB 263.5752 (ms) - -
EdgeNext_xx_small 4.49 MB 155.0043 (ms) edgenext_regression.yaml https://drive.google.com/file/d/1Mo2wIPXJuj0pYFPyMDtC2C39bdH1VBxm/view?usp=sharing
YOLOv5n (custom backbone : x) 4.106 MB 132.2865 (ms) yolov5n_regression.yaml https://drive.google.com/file/d/1I-HfI5p_UC1Y39ipAjLgV1Gw9Sdqh594/view?usp=sharing
YOLOv5n (custom backbone : xx) 2.213 MB 129.8896 (ms) yolov5n_regression.yaml -
MobileNetV2_0.25 1.068 MB 111.6102 (ms) mobilenetv2_regression.yaml https://drive.google.com/file/d/1Nqb1mqy512Tpj2L-pQMmDVP4GC9h6VGP/view?usp=sharing
MobileNetV2_0.5 2.815 MB 123.4103 (ms) mobilenetv2_regression.yaml https://drive.google.com/file/d/1St2n0FX11_R9VrH032xACKXHoXOk3ibf/view?usp=sharing
EfficientNetLite0 13.137 MB 185.1595 (ms) - -
SqueezeNetV1.1 2.785 MB 57.3412 (ms) squeezenet_regression.yaml https://drive.google.com/file/d/1IjV-7Rj56jtiJ0o15rX1xfTzc2cdo7zm/view?usp=sharing

Train code

If you want to train the code, please refer to the training script below.

> python train.py --config config/{}.yaml --save {} --device {} --viz

optional arguments:
	--config								select yaml file to run (in config folder)
	--save									set a path to save checkpoint and graph
	--device								select a device (ex cuda:@)
	--viz										add if you want to visualize

EX)
> python train.py --config mobilenetv2_0.5_regression --save checkpoint/mobilenetv2_0.5 --device cuda:0 --viz


Evaluation

Performance : Baseline & Lightweight models

drawing

This figure shows that our designed model predicts motion blur well and their error is close to zero when compared to GT whether the blur angle is fixed or not. (Also whatever the backbone is!) Each model’s result is the mean of result about 30 people.


Ablation Study (1) : ResNet18 vs ResNet18 with complex regressor

drawing

This figure shows that ResNet with simple structure predicts better than one with complex structure. Furthermore, the stack of linear layers increases the inference speed and model size. Therefore, we don’t fix any regressor (fc layer) of all the models we used at this project.


Ablation Study (2) : How about solving this problem as Classification?

drawing

drawing

(Upper) : ResNet trained by classfication
(Bottom) : EdgeNext_xx_samll trained by classification

We divide 0 ~ 1 into $N(20, 40)$ classes. $i^{th}$ Class (i=0~N-1) means GT blur degree is between i/N ~ (i+1)/N, so regression label can be changed into classification label.

We train ResNet and EdgeNext_xx_small with cross entropy + MSE(CMSE) or crossentropy + probability based MSE (WeightMSE, WMSE). These figures show that solving this task as classification is also valid approach.



Qualitative results(Regression model)

ResNet18

drawing

EdgeNext

drawing


Qualitative results(Video test)

drawing

Qualitative model evaluation on video test samples. (a) is ResNet18, (b) is EdgeNext, (c) is Yolov5n, (d) is SqueezeNetV1.1, (e) is MobileNetv2(0.5) and (f) is MobileNetv2(0.25), respectively. Results of resizing the detected face image by applying the detection model, SCRFD, and then use it as an input to each model.

Quickstart examples with trained model

You can detect the blur of the video or image with trained model. First Download a video/image file to the "data" folder path that you want to detect a blur.

Below command detects a blur in the video and generates a result video.

python demo.py --device cpu --pretrained_path {pretrained_model.pt} --mode video --file_path ./data/sample.mp4 --save_path ./data/result_sample.mp4

Below command detects a blur in the image and generates a result image.

python demo.py --device cpu --pretrained_path {pretrained_model.pt} --mode image --file_path ./data/sample.png --save_path ./data/result_sample.png

Caution!!

Be careful you have to match following image specification(for facial cropped image) in test/inference with pre-trained model.

  • model.eval() and torch.no_grad()
  • Image spatial dimension should be $112 \times 112$
  • Image value should be normalized to $0 \sim 1$, not $0 \sim 255$

header

blurfacedetection's People

Contributors

hjhyunjinkim avatar hynbjn avatar junia3 avatar minsu1206 avatar smmin21 avatar sungbeomchoi avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.