## Focal Frequency Loss - Official PyTorch Implementation

This repository provides the official PyTorch implementation for the following paper:
**Focal Frequency Loss for Image Reconstruction and Synthesis**<br>
[Liming Jiang](https://liming-jiang.com/), [Bo Dai](http://daibo.info/), [Wayne Wu](https://wywu.github.io/) and [Chen Change Loy](http://personal.ie.cuhk.edu.hk/~ccloy/)<br>
In ICCV 2021.<br>
[**Project Page**](https://www.mmlab-ntu.com/project/ffl/index.html) | [**Paper**](https://arxiv.org/abs/2012.12821) | [**Poster**](https://liming-jiang.com/projects/FFL/resources/poster.pdf) | [**Slides**](https://liming-jiang.com/projects/FFL/resources/slides.pdf) | [**YouTube Demo**](https://www.youtube.com/watch?v=RNTnDtKvcpc)
> **Abstract:** *Image reconstruction and synthesis have witnessed remarkable progress thanks to the development of generative models. Nonetheless, gaps could still exist between the real and generated images, especially in the frequency domain. In this study, we show that narrowing gaps in the frequency domain can ameliorate image reconstruction and synthesis quality further. We propose a novel focal frequency loss, which allows a model to adaptively focus on frequency components that are hard to synthesize by down-weighting the easy ones. This objective function is complementary to existing spatial losses, offering great impedance against the loss of important frequency information due to the inherent bias of neural networks. We demonstrate the versatility and effectiveness of focal frequency loss to improve popular models, such as VAE, pix2pix, and SPADE, in both perceptual quality and quantitative performance. We further show its potential on StyleGAN2.*
## Updates
- [09/2021] The **code** of Focal Frequency Loss is **released**.
- [07/2021] The [paper](https://arxiv.org/abs/2012.12821) of Focal Frequency Loss is accepted by **ICCV 2021**.
## Quick Start
Run `pip install focal-frequency-loss` for installation. Then, the following code is all you need.
```python
from focal_frequency_loss import FocalFrequencyLoss as FFL
ffl = FFL(loss_weight=1.0, alpha=1.0) # initialize nn.Module class
import torch
fake = torch.randn(4, 3, 64, 64) # replace it with the predicted tensor of shape (N, C, H, W)
real = torch.randn(4, 3, 64, 64) # replace it with the target tensor of shape (N, C, H, W)
loss = ffl(fake, real) # calculate focal frequency loss
```
**Tips:**
1. Current supported PyTorch version: `torch>=1.1.0`. Warnings can be ignored. Please note that experiments in the paper were conducted with `torch<=1.7.1,>=1.1.0`.
2. Arguments to initialize the `FocalFrequencyLoss` class:
- `loss_weight (float)`: weight for focal frequency loss. Default: 1.0
- `alpha (float)`: the scaling factor alpha of the spectrum weight matrix for flexibility. Default: 1.0
- `patch_factor (int)`: the factor to crop image patches for patch-based focal frequency loss. Default: 1
- `ave_spectrum (bool)`: whether to use minibatch average spectrum. Default: False
- `log_matrix (bool)`: whether to adjust the spectrum weight matrix by logarithm. Default: False
- `batch_matrix (bool)`: whether to calculate the spectrum weight matrix using batch-based statistics. Default: False
3. Experience shows that the main hyperparameters you need to adjust are `loss_weight` and `alpha`. The loss weight may always need to be adjusted first. Then, a larger alpha indicates that the model is more focused. We use `alpha=1.0` as default.
## Exmaple: Image Reconstruction (Vanilla AE)
As a guide, we provide an example of applying the proposed focal frequency loss (FFL) for Vanilla AE image reconstruction on CelebA. Applying FFL is pretty easy. The core details can be found [here](https://github.com/EndlessSora/focal-frequency-loss/blob/master/VanillaAE/models.py).
### Installation
After installing [Anaconda](https://www.anaconda.com/), we recommend you to create a new conda environment with python 3.8.3:
```bash
conda create -n ffl python=3.8.3 -y
conda activate ffl
```
Clone this repo, install PyTorch 1.4.0 (`torch>=1.1.0` may also work) and other dependencies:
```bash
git clone https://github.com/EndlessSora/focal-frequency-loss.git
cd focal-frequency-loss
pip install -r VanillaAE/requirements.txt
```
### Dataset Preparation
In this example, please download [img\_align\_celeba.zip](https://drive.google.com/file/d/0B7EVK8r0v71pZjFTYXZWM3FlRnM/view?usp=sharing&resourcekey=0-dYn9z10tMJOBAkviAcfdyQ) of the CelebA dataset from its [official website](https://mmlab.ie.cuhk.edu.hk/projects/CelebA.html). Then, we highly recommend you to unzip this file and symlink the `img_align_celeba` folder to `./datasets/celeba` by:
```bash
bash scripts/datasets/prepare_celeba.sh [PATH_TO_IMG_ALIGN_CELEBA]
```
Or you can simply move the `img_align_celeba` folder to `./datasets/celeba`. The resulting directory structure should be:
```
├── datasets
│ ├── celeba
│ │ ├── img_align_celeba
│ │ │ ├── 000001.jpg
│ │ │ ├── 000002.jpg
│ │ │ ├── 000003.jpg
│ │ │ ├── ...
```
### Test and Evaluation Metrics
Download the [pretrained models](https://drive.google.com/file/d/1YIH09eoDyP2JLmiYJpju4hOkVFO7M3b_/view?usp=sharing) and unzip them to `./VanillaAE/experiments`.
We have provided the example [test scripts](https://github.com/EndlessSora/focal-frequency-loss/tree/master/scripts/VanillaAE/test). If you only have a CPU environment, please specify `--no_cuda` in the script. Run:
```bash
bash scripts/VanillaAE/test/celeba_recon_wo_ffl.sh
bash scripts/VanillaAE/test/celeba_recon_w_ffl.sh
```
The Vanilla AE image reconstruction results will be saved at `./VanillaAE/results` by default.
After testing, you can further calculate the evaluation metrics for this example. We have implemented a series of [evaluation metrics](https://github.com/EndlessSora/focal-frequency-loss/tree/master/metrics) we used and provided the [metric scripts](https://github.com/EndlessSora/focal-frequency-loss/tree/master/scripts/VanillaAE/metrics). Run:
```bash
bash scripts/VanillaAE/metrics/celeba_recon_wo_ffl.sh
bash scripts/VanillaAE/metrics/celeba_recon_w_ffl.sh
```
You will see the scores of different metrics. The metric logs will be saved in the respective experiment folders at `./VanillaAE/results`.
### Training
We have provided the example [training scripts](https://github.com/EndlessSora/focal-frequency-loss/tree/master/scripts/VanillaAE/train). If you only have a CPU environment, please specify `--no_cuda` in the script. Run:
```bash
bash scripts/VanillaAE/train/celeba_recon_wo_ffl.sh
bash scripts/VanillaAE/train/celeba_recon_w_ffl.sh
```
After training, inference on the newly trained models is similar to [Test and Evaluation Metrics](#test-and-evaluation-metrics). The results could be better reproduced on NVIDIA Tesla V100 GPUs with `torch<=1.7.1,>=1.1.0`.
## More Results
Here, we show other examples of applying the proposed focal frequency loss (FFL) under diverse settings.
### Image Reconstruction (VAE)

### Image-to-Image Translation (pix2pix | SPADE)

### Unconditional Image Synthesis (StyleGAN2)
256x256 results (without truncation) and the mini-batch average spectra (adjusted to better contrast):

1024x1024 results (without truncation) synthesized by StyleGAN2 with FFL:

## Citation
If you find
没有合适的资源?快使用搜索试试~ 我知道了~
[ICCV_2021]_位置_频率_损耗_图像_重建_焦点-频率损耗

共30个文件
py:11个
sh:7个
jpg:5个

需积分: 5 0 下载量 58 浏览量
2024-08-26
15:57:00
上传
评论
收藏 4.28MB ZIP 举报
温馨提示
[ICCV_2021]_位置_频率_损耗_图像_重建_焦点-频率损耗_[ICCV_2021]_Focal_Frequency_Loss_for_Image_Reconst_focal-frequency-loss.zip_[ICCV_2021]_Focal_Frequency_Loss_for_Image_Reconst_focal-frequency-loss
资源推荐
资源详情
资源评论




























收起资源包目录













































共 30 条
- 1
资源评论


好家伙VCC
- 粉丝: 4495
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 专业服装管理软件经销协议书.docx
- 儿童品牌-红孩子网络营销方案课件.ppt
- 机械制造与自动化生产的实习报告.docx
- 利用微信 hook 拦截修改特定 call 并嵌入 Python 代码爬取微信公众号文章
- 电子商务技术概述.doc
- 年软件开发个人工作总结.docx
- 协同办公自动化的五大好处.pdf
- 基于PLC的加热炉温度控制系统本科毕业设计.doc
- 互联网大学生创新创业大赛项目计划书.doc
- 数字通信SDH教案.pptx
- 京东商城网络营销策略.pptx
- 2023年计算机考试题模拟优质.doc
- 复旦大学《软件工程》钱乐秋课件教案PPT04.ppt
- 算法设计与分析第四版剖析.pptx
- 基于JSP的动态WEB学习系统的研究与实现论文.doc
- 计算机辅助创新技术在产品设计过程中的运用.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
