Skip to content

Commit 7329e4f

Browse files
committed
[SW-184714] Add internal folder to fp8 quant
This is a folder used for experiments, not to be used by users Change-Id: I9e221ae582794e304e95392c0f37638f7bce69bc
1 parent da4bcd2 commit 7329e4f

File tree

9 files changed

+1498
-0
lines changed

9 files changed

+1498
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
How to calculate FID and clip score:
2+
3+
We will use the MS-COCO database. We use this for two things:
4+
- Generating a large amount of prompts which we can use to create diffusion images
5+
- Once we have diffusion images, we need a "ground truth" dataset to calculate the FID.
6+
7+
1) Run a python script which does the following things:
8+
- Takes a subset of MSCOCO
9+
- Create a CSV with prompts which can then be inserted into the diffusion model. These prompts are taken from captions of the images in the subset
10+
- Create a new folder with the images from the subset
11+
- The standard number of images for this evaluation is 30K or 10K
12+
13+
run the following:
14+
15+
python create_dataset.py /datasets/coco2014 <path to save CSV and dataset> <size of subset>
16+
17+
Now, create the generated images from the csv file
18+
19+
IMPORTANT!! - the script that does the actual evaluation (explained below) expects to get an image where the prompt is the title of the image. For example, if the prompt is "a monster playing the guitar" then the name of the file that is created using diffusion should be "<path>/a monster playing the guitar.png" (or jpg or whatever)
20+
21+
IMPORTANT!! #2 - from my experience, stable diffusion inference returns an error for prompts with the character '/' in them. There are very few, around one in a thousand. My recomendation, if you want to evaluate N images, create a subset of the size N+30 and delete prompts with '/' in them. After creating the CSV I just deleted these prompts manually (takes 10 seconds to do).
22+
(Perhaps automating this should be a future commit).
23+
24+
2) Now, run the evaluation script. This does the following:
25+
- Calculates the CLIP score – takes the CLIP embedding of each generated image and the embedding of the caption that created it (in this case each image and its file name). Then, calculates the cosine distance between them.
26+
- Calculates the FID - takes the real and generated images, and calculates according to the FID distance metric.
27+
- insert the number of images to evaluate with - could be the number of images in the subset created above or less
28+
29+
To do this, run:
30+
31+
python evaluator.py --device hpu --real_images_path /datasets/coco2014/val2014 --diff_images_path <generated images path> --num_of_images <Num of images to evaluate with>
32+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
How to calculate PSNR and SSIM for Super Resolution
2+
We will use the Imagenet validation dataset.
3+
4+
The evaluation is done by the following steps:
5+
1) We take the Imagenet validation set which has 50,000 images (We can also take a subset)
6+
2) Crop these Images to be 256*256 (center cropped), and save these images as the "ground truth" dataset. The name of
7+
the saved image is its label.
8+
3) Downsample the images to be 64*64 (using bicubic interpolation) and then restore them using Super Resolution.
9+
4) Calculate PSNR and SSIM between each ground truth image and restored image, and print the mean.
10+
11+
Steps 1,2 and 4 are inluded here, while step 3 (downsampling and restoring) should be done seperately, using the
12+
desired Super Resolution method. Keep in mind that this script assumes that the images are stored in a specific format,
13+
(detailed later). Later, the restored images path should be given as an input to step 4.
14+
15+
You can skip step 1+2 and use the images at /datasets/imagenet/val_cropped_labeled
16+
You can also run a python script which does the following to the imagenet validation dataset:
17+
- Crops images to 256*256 (this can also be changed using the argument --resize, 256*256 is the default)
18+
- Saves the images with the convention <path>/<label>_<ID>.png
19+
- a text file mapping imagenet class index to label is needed. It is given here as imagenet1000_clsidx_to_labels.txt, but
20+
can be given as an argument with --class_to_labels
21+
22+
to do this, run the following:
23+
24+
python create_SR_dataset.py --images <imagenet validation path> --out_dir <path to save ground truth images>
25+
26+
Now, create the generated images so they match the files created above (step 3)
27+
28+
IMPORTANT!! - the script that does the actual evaluation (explained below) expects to get an image where the prompt in the same format
29+
<generated images path>/<label>_<ID>.png. This means that the script expects the original and restored images to have the same name.
30+
31+
Find an example in /workdisk/ilamprecht/diffusion/stablediffusionv2/scripts/superres_gen_imgs.py
32+
33+
Now, run the evaluation script, which calculates PSNR and SSIM and prints the mean (step 4)
34+
35+
To do this, run:
36+
37+
python super_res_eval.py --num_images <desired number of images up to 50000> --real_images <real images path> --gen_images <generated images path>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import os
2+
import torch.nn.parallel
3+
import torch.optim
4+
import torch.utils.data
5+
import torch
6+
import torchvision.transforms as transforms
7+
import torchvision.datasets as datasets
8+
from torchvision.utils import save_image
9+
import argparse
10+
11+
from torchvision.transforms import functional as F
12+
13+
14+
class CenterCropAndResize(object):
15+
def __init__(self, size):
16+
self.size = size
17+
18+
def __call__(self, img):
19+
width, height = img.size
20+
crop_size = min(width, height)
21+
crop = F.center_crop(img, (crop_size, crop_size))
22+
resize = F.resize(crop, self.size)
23+
return resize
24+
25+
def get_data_loader(path, dataset="ImageNet",
26+
workers=4, shuffle=None, pin_memory=True, resize = 256):
27+
28+
#Data loader for ImageNet data.
29+
30+
31+
# defines desired resize amd creates dataset
32+
def get_dataset(path_to_data):
33+
transformations = [CenterCropAndResize(resize), transforms.ToTensor()]
34+
return datasets.ImageFolder(path_to_data, transforms.Compose(transformations))
35+
36+
# checks if given path is valid
37+
if isinstance(path, str):
38+
curr_path = path
39+
if not os.path.exists(curr_path):
40+
raise FileNotFoundError(f"Directory {curr_path} doesn't exist")
41+
data_dir = curr_path
42+
elif isinstance(path, list):
43+
for path_ in path:
44+
if os.path.exists(path_):
45+
curr_path = path_
46+
break
47+
else:
48+
raise FileNotFoundError(f"None of the default data directories exist in your env,"
49+
f" please manually specify one")
50+
data_dir = os.path.join(curr_path, 'val')
51+
else:
52+
raise ValueError("get_data_loader expects list of paths or single path")
53+
54+
# create dataloader from dataset
55+
dataset = get_dataset(data_dir)
56+
data_loader = torch.utils.data.DataLoader(
57+
dataset,
58+
batch_size=1, shuffle=shuffle,
59+
num_workers=workers, pin_memory=pin_memory)
60+
61+
return data_loader
62+
63+
parser = argparse.ArgumentParser('Create dataset of real images for SR evaluation', add_help=False)
64+
65+
parser.add_argument('--images', type = str, help = 'path to imagenet validation set')
66+
parser.add_argument('--out_dir', type = str, help = 'path to save images with correct format (cropped + modified file name)')
67+
parser.add_argument('--resize', type = int, default = 256, help = 'dimensions to resize image')
68+
parser.add_argument('--class_to_labels', type = str, default = 'imagenet1000_clsidx_to_labels.txt', help = 'path to text file containing'
69+
'mapping between class index and label')
70+
71+
args = parser.parse_args()
72+
images = args.images
73+
out_dir = args.out_dir
74+
resize = args.resize
75+
class_to_labels = args.class_to_labels
76+
77+
with torch.no_grad():
78+
# get dataloader
79+
dl = get_data_loader(images, resize = resize)
80+
81+
# open idx2label, which matches an integer signifying class with the correcs label
82+
idx2label = eval(open(class_to_labels).read())
83+
84+
# save images with correct filename
85+
for i,image in enumerate(dl):
86+
label = idx2label.get(image[1].item())
87+
save_image(image[0], f'{out_dir}/{label}_{i}.png')

0 commit comments

Comments
 (0)