forked from wayfeng/ov_clip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantize_clip.py
98 lines (80 loc) · 3.16 KB
/
quantize_clip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import argparse
import openvino as ov
import logging
import cv2
import open_clip
import nncf
import numpy as np
import torch
import torch.utils.data as data
from torchvision.transforms.functional import to_pil_image
from zipfile import ZipFile
from pathlib import Path
def transform_fn(image_data):
"""
Quantization transform function. Extracts and preprocess input data from dataloader item for quantization.
Parameters:
image_data: image data produced by DataLoader during iteration
Returns:
input_tensor: input data in Dict format for model quantization
"""
return preprocess_image(to_pil_image(np.squeeze(image_data.numpy()))).unsqueeze(0)
class COCOLoader(data.Dataset):
def __init__(self, images_path):
self.images = list(Path(images_path).iterdir())
def __getitem__(self, index):
image_path = self.images[index]
image = cv2.imread(str(image_path))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image
def __len__(self):
return len(self.images)
def load_data(data_dir: Path):
# DATA_URL = "https://ultralytics.com/assets/coco128.zip"
# DATA_DIR = Path('./data')
zipfile = data_dir/'coco128.zip'
if not (data_dir / "coco128/images/train2017").exists():
with ZipFile(zipfile, "r") as zip_ref:
zip_ref.extractall(zipfile)
else:
print("File existed")
coco_dataset = COCOLoader(data_dir / 'coco128/images/train2017')
calibration_loader = torch.utils.data.DataLoader(coco_dataset)
return nncf.Dataset(calibration_loader, transform_fn)
# Model tags
# "ViT-B-32": "laion2b_s34b_b79k"
# "ViT-L-14": "laion2b_s32b_b82k"
clip_models = {
"vit-b-32": "./checkpoints/open_clip_vit_b_32.pth",
"vit-l-14": "./checkpoints/open_clip_vit_l_14.pth",
"vit-h-14": "./checkpoints/open_clip_vit_h_14.pth",
"vit-g-14": "./checkpoints/open_clip_vit_g_14.pth",
}
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('model_id', default='ViT-B-32',
help="Model id to convert")
parser.add_argument('-o', '--model_dir', default='/tmp',
help="Folder for OpenVINO IR")
parser.add_argument('-d', '--data_dir', default='./data',
help="Data folder to calibrate model")
args = parser.parse_args()
model_id = args.model_id
model, _, preprocess_image = open_clip.create_model_and_transforms(
model_id, pretrained=clip_models[model_id.lower()])
# tokenizer = open_clip.get_tokenizer(model_id)
core = ov.Core()
nncf.set_log_level(logging.ERROR)
fp16_model_path = Path(args.model_dir) / \
f"{model_id.lower().replace('-','_')}_visual.xml"
int8_model_path = Path(args.model_dir) / \
f"{model_id.lower().replace('-','_')}_visual_int8.xml"
# calibration_data = prepare_dataset()
ov_model = core.read_model(fp16_model_path)
calibration_dataset = load_data(Path(args.data_dir))
quantized_model = nncf.quantize(
model=ov_model,
calibration_dataset=calibration_dataset,
model_type=nncf.ModelType.TRANSFORMER,
)
ov.save_model(quantized_model, int8_model_path)