The official Pytorch implementation of our paper Where is My Spot? Few-shot Image Generation via Latent Subspace Optimization, CVPR 2023.
Where is My Spot? Few-shot Image Generation via Latent Subspace Optimization
Chenxi Zheng, Bangzhen Liu, Xuemiao Xu, Huaidong Zhang, and Shengfeng He
Prepare the environment using either conda or pip.
cd envs
conda env create -f environment.yaml
cd ..
cd envs
conda create -n LSO python=3.7
conda activate LSO
pip install -r requirements.txt
cd ..
If installation of pytorch fails or a custom Pytorch version is needed, please install pytorch following the official guidance manually.
Before training, prepare the pretrained ckpts optimized with seen categories.
Note that the latent codes only contain the subset for image generation $\mathbb{S}_{gen}^{c}$ in Sec. 4.2. For example, the shape of tensor in flowers_unseen17_0-10_step1300.npy
is $[17*10, 12, 512]$, which is corresponding to the slice $[85:102, 0:10, :, :, :]$ of images in flower_c8189_s128_data_rgb.npy
which has a tensor shape of $[102, 40, 128, 128, 3]$.
Unzip the files and set up the paths in configs/default_configs.py
.
DATA_PATH = <PATH_TO_DATASET_NPY>
CKPT_PATH = <PATH_TO_STYLEGAN2_CKPT>
WS_PATH = <PATH_TO_WS>
IDCKPT_PATH = <PATH_TO_IDWEIGHTS>
python train_unseen.py \
--outdir <output_dir> \
--k_shot <k> \
--dataset_name <dataset_name>
--classes=<start_idx>-<end_idx>
.<output_dir>/<run_dir>/few-shot_samples/magnitude=<magnitude-intensity>
. For each category, we randomly generate 128 images for quantitative evaluation.train_unseen.py
for command examples.We also provide single-task optimization for visualization and detailed optimization evaluation.
python train_unseen.py \
--outdir <output_dir> \
--k_shot <k> \
--single_task <cidx> <idx_1,...,idx_k> \
--dataset_name <dataset_name>
--single_task
with the category number and the image indices.--save_ckpt
as true.(optional) If the images are generated in the separated runs, use merger.py
to combine all the images.
python merger.py \
--path <output_dir> \
--idx <runidx_1>,...,<runidx_n>
merger.py
for command examples.Quantitative evaluation of the generated images.
python main_metric_calculate.py \
--real_dir <real_directory> \
--fake_dir <fake_directory> \
--dataset_name <dataset_name>
main_metric_calculate.py
for command examples.The calculation of LPIPS is significantly accelerated by first extracting the features of each image. Please refer to metrics/lpips_fs/lpips_fs.py
.
If you use this code for your research, please cite our paper.
@inproceedings{zheng2023my,
title={Where Is My Spot? Few-Shot Image Generation via Latent Subspace Optimization},
author={Zheng, Chenxi and Liu, Bangzhen and Zhang, Huaidong and Xu, Xuemiao and He, Shengfeng},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
pages={3272--3281},
year={2023}
}
This project builds upon and has been inspired by the following repositories:
Training and evaluation code are based on StyleGAN2-ADA and LoFGAN.
The code for calculating FID is based on pytorch-fid.
We would like to thank the entire open-source community for fostering an environment of collaboration and knowledge sharing.
This repository is under MIT license.