Skip to content
Snippets Groups Projects
Commit 1eb283dd authored by Gonzalo Martin Garcia's avatar Gonzalo Martin Garcia
Browse files

Add missing LHQ README samples

parent 0826b138
No related branches found
No related tags found
No related merge requests found
Showing
with 124 additions and 2 deletions
# Unconditional Diffusion model # Unconditional Diffusion model
This repo presents our paper implementation of the unconditional diffusion model ,with its popular optimizations, from *Denoising Diffusion Probabilistic Models* by Ho et al and *Improved Denoising Diffusion Probabilistic Models* by Nichol and Dhariwal. The pipeline contains training, sampling and evaluation functions meant to be run on the HPC. This repo presents our paper implementation of the unconditional diffusion model, with its popular optimizations, from [*Denoising Diffusion Probabilistic Models* ](https://proceedings.neurips.cc/paper/2020/hash/4c5bcfec8584af0d967f1ab10179ca4b-Abstract.html) by Ho et al and [*Improved Denoising Diffusion Probabilistic Models* ](https://proceedings.mlr.press/v139/nichol21a.html) by Nichol and Dhariwal. The pipeline contains training, sampling, and evaluation functions meant to be run on the HPC.
We show correctness of our pipeline by training unconditional diffusion models on the landscapes (LHQ) and celebrity A (CelebAHQ) datasets, generating realistic images with a resolution of 128x128px. We show the correctness of our pipeline by training unconditional diffusion models on the landscapes (LHQ) and celebrity A (CelebAHQ) datasets, generating realistic images with a resolution of 128x128px.
Diffusion models are a class of generative models that offer a unique approach to modeling complex data distributions by simulating a stochastic process, known as a diffusion process, that gradually transforms data from a simple initial distribution into a complex data distribution. More specifically, the simple distribution is given by Gaussian Noise which is iteratively denoised into coherent images through modeling the entire data distribution present in the training set. Diffusion models are a class of generative models that offer a unique approach to modeling complex data distributions by simulating a stochastic process, known as a diffusion process, that gradually transforms data from a simple initial distribution into a complex data distribution. More specifically, the simple distribution is given by Gaussian Noise which is iteratively denoised into coherent images through modeling the entire data distribution present in the training set.
## Sample Examples ## Sample Examples
### Unconditional Landscape Generation: ### Unconditional Landscape Generation:
<table>
<tr>
<td><img src="imgs/landscapes/sample_0_0.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_0_26.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_1_13.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_0_5.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_1_2.png" alt="land"></td>
</tr>
<tr>
<td><img src="imgs/landscapes/sample_1_4.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_0_13.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_0_6.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_0_35.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_0_36.png" alt="land"></td>
</tr>
<tr>
<td><img src="imgs/landscapes/sample_0_9.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_0_11.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_0_1.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_0_15.png" alt="land"></td>
<td><img src="imgs/landscapes/sample_0_18.png" alt="land"></td>
</tr>
</table>
### Unconditional Celebrity Face Generation: ### Unconditional Celebrity Face Generation:
<table> <table>
......
...@@ -10,6 +10,105 @@ from torchvision.models import resnet50 ...@@ -10,6 +10,105 @@ from torchvision.models import resnet50
from evaluation.helpers.kNN import * from evaluation.helpers.kNN import *
from evaluation.helpers.metrics import * from evaluation.helpers.metrics import *
def simple_evaluator(model,
device,
dataloader,
checkpoint,
experiment_path,
sample_idx=0,
**args,
):
'''
Takes a trained diffusion model from 'checkpoint' and evaluates its performance on the test
dataset 'dataloader' w.r.t. the three most important perfromance metrics; FID, IS, KID. We continue
the progress of our evaluation function for the LDM upscalaer and may update this function accordingly.
checkpoint: Name of the saved pth. file containing the trained weights and biases
experiment_path: Path to the experiment folder where the evaluation results will be stored
dataloader: Loads the test dataset for evaluation
sample_idx: Integer that denotes which sample directory sample_{sample_idx} from the checkpoint model shall be used for evaluation
'''
checkpoint_path = f'{experiment_path}trained_ddpm/{checkpoint}'
# create evaluation directory for the complete experiment (if first time sampling images)
output_dir = f'{experiment_path}evaluations/'
os.makedirs(output_dir, exist_ok=True)
# create evaluation directory for the current version of the trained model
model_name = os.path.basename(checkpoint_path)
epoch = re.findall(r'\d+', model_name)
if epoch:
e = int(epoch[0])
else:
raise ValueError(f"No digit found in the filename: {filename}")
model_dir = os.path.join(output_dir,f'epoch_{e}')
os.makedirs(model_dir, exist_ok=True)
# create the evaluation directory for this evaluation run for the current version of the model
eval_dir_list = [d for d in os.listdir(model_dir) if os.path.isdir(os.path.join(model_dir, d))]
indx_list = [int(d.split('_')[1]) for d in eval_dir_list if d.startswith('evaluation_')]
j = max(indx_list, default=-1) + 1
eval_dir = os.path.join(model_dir, f'evaluation_{j}')
os.makedirs(eval_dir, exist_ok=True)
# Compute Metrics
eval_path = os.path.join(eval_dir, 'eval.txt')
# get sampled images
transform = transforms.Compose([transforms.ToTensor(), transforms.Lambda(lambda x: (x * 255).type(torch.uint8))])
sample_path = os.path.join(f'{experiment_path}samples/',f'epoch_{e}',f'sample_{sample_idx}')
ignore_tensor = f'image_tensor{j}'
images = []
for samplename in os.listdir(sample_path):
if samplename == ignore_tensor:
continue
img = Image.open(os.path.join(sample_path, samplename))
img = transform(img)
images.append(img)
# split them into batches for GPU memory
generated = torch.stack(images).to(device)
generated_batches = torch.split(generated, dataloader.batch_size)
nr_generated_batches = len(generated_batches)
nr_real_batches = len(dataloader)
# Init FID, IS and KID scores
fid = FrechetInceptionDistance(normalize = False).to(device)
iscore = InceptionScore(normalize=False).to(device)
kid = KernelInceptionDistance(normalize=False, subset_size=32).to(device)
# Update scores for the full testing dataset w.r.t. the sampled batches
for idx,(data, _) in enumerate(dataloader):
data = data.to(device)
fid.update(data, real=True)
kid.update(data, real=True)
if idx < nr_generated_batches:
gen = generated_batches[idx].to(device)
fid.update(gen, real=False)
kid.update(gen, real=False)
iscore.update(gen)
# If there are sampled images left, add them too
for idx in range(nr_real_batches, nr_generated_batches):
gen = generated_batches[idx].to(device)
fid.update(gen, real=False)
kid.update(gen, real=False)
iscore.update(gen)
# compute total FID, IS and KID
fid_score = fid.compute()
i_score = iscore.compute()
kid_score = kid.compute()
# store results in txt file
with open(str(eval_path), 'a') as txt:
result = f'FID_epoch_{e}_sample_{sample_idx}:'
txt.write(result + str(fid_score.item()) + '\n')
result = f'KID_epoch_{e}_sample_{sample_idx}:'
txt.write(result + str(kid_score) + '\n')
result = f'IS_epoch_{e}_sample_{sample_idx}:'
txt.write(result + str(i_score) + '\n')
def ddpm_evaluator(experiment_path, realpath, genpath, data='lhq', size=128, arch='clip', mode='kNN', k=3, sample=10, name_appendix='', fid='no'): def ddpm_evaluator(experiment_path, realpath, genpath, data='lhq', size=128, arch='clip', mode='kNN', k=3, sample=10, name_appendix='', fid='no'):
......
imgs/landscapes/sample_0_0.png

27.6 KiB

imgs/landscapes/sample_0_1.png

34 KiB

imgs/landscapes/sample_0_11.png

25.7 KiB

imgs/landscapes/sample_0_13.png

35.8 KiB

imgs/landscapes/sample_0_15.png

26.6 KiB

imgs/landscapes/sample_0_18.png

31.4 KiB

imgs/landscapes/sample_0_26.png

26.2 KiB

imgs/landscapes/sample_0_35.png

26.6 KiB

imgs/landscapes/sample_0_36.png

24.2 KiB

imgs/landscapes/sample_0_5.png

29 KiB

imgs/landscapes/sample_0_6.png

32.7 KiB

imgs/landscapes/sample_0_9.png

24.7 KiB

imgs/landscapes/sample_1_13.png

24.5 KiB

imgs/landscapes/sample_1_2.png

28.8 KiB

imgs/landscapes/sample_1_4.png

27.1 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment