Koushik Dutta commited on
Commit ·
45d0854
1
Parent(s): 170246d
calibration
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +1 -0
- .gitignore +3 -1
- calibration.py +0 -41
- export.py +45 -2
- image_calibration.py +26 -0
- images/000000001999.jpg +3 -0
- images/000000003899.jpg +3 -0
- images/000000006712.jpg +3 -0
- images/000000009658.jpg +3 -0
- images/000000012999.jpg +3 -0
- images/000000013517.jpg +3 -0
- images/000000017153.jpg +3 -0
- images/000000020063.jpg +3 -0
- images/000000024307.jpg +3 -0
- images/000000024457.jpg +3 -0
- images/000000028127.jpg +3 -0
- images/000000029161.jpg +3 -0
- images/000000029716.jpg +3 -0
- images/000000030677.jpg +3 -0
- images/000000033300.jpg +3 -0
- images/000000033888.jpg +3 -0
- images/000000034973.jpg +3 -0
- images/000000036693.jpg +3 -0
- images/000000038226.jpg +3 -0
- images/000000038999.jpg +3 -0
- images/000000039764.jpg +3 -0
- images/000000040380.jpg +3 -0
- images/000000040768.jpg +3 -0
- images/000000040870.jpg +3 -0
- images/000000042404.jpg +3 -0
- images/000000044446.jpg +3 -0
- images/000000047942.jpg +3 -0
- images/000000048491.jpg +3 -0
- images/000000048595.jpg +3 -0
- images/000000053977.jpg +3 -0
- images/000000061834.jpg +3 -0
- images/000000061945.jpg +3 -0
- images/000000063409.jpg +3 -0
- images/000000063873.jpg +3 -0
- images/000000063879.jpg +3 -0
- images/000000065177.jpg +3 -0
- images/000000066503.jpg +3 -0
- images/000000069121.jpg +3 -0
- images/000000072207.jpg +3 -0
- images/000000075257.jpg +3 -0
- images/000000076428.jpg +3 -0
- images/000000077639.jpg +3 -0
- images/000000077864.jpg +3 -0
- images/000000081406.jpg +3 -0
- images/000000083755.jpg +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
.gitignore
CHANGED
|
@@ -2,4 +2,6 @@
|
|
| 2 |
vision.pt
|
| 3 |
text.pt
|
| 4 |
*pnnx*
|
| 5 |
-
*_ncnn.py
|
|
|
|
|
|
|
|
|
| 2 |
vision.pt
|
| 3 |
text.pt
|
| 4 |
*pnnx*
|
| 5 |
+
*_ncnn.py
|
| 6 |
+
__pycache__
|
| 7 |
+
|
calibration.py
DELETED
|
@@ -1,41 +0,0 @@
|
|
| 1 |
-
text_input_calibration_samples = [
|
| 2 |
-
"man with backpack", "person walking dog", "car parked in driveway",
|
| 3 |
-
"delivery man", "UPS truck", "FedEx truck", "Amazon delivery",
|
| 4 |
-
"woman with groceries", "child playing", "dog near gate",
|
| 5 |
-
"unknown person approaching", "motion detected", "vehicle on driveway",
|
| 6 |
-
"suspicious activity", "someone at the door", "person in backyard",
|
| 7 |
-
"car driving away", "truck on street", "package delivered",
|
| 8 |
-
"stranger near front door", "man wearing a hat", "person in black jacket",
|
| 9 |
-
"person with red bag", "bicycle near fence", "dog in yard",
|
| 10 |
-
"person running", "car entering driveway", "person in garden",
|
| 11 |
-
"no one in view", "snowy scene", "night vision footage",
|
| 12 |
-
"package left at door", "person holding something", "person climbing fence",
|
| 13 |
-
"cat in yard", "truck backing up", "bike leaning on wall",
|
| 14 |
-
"person walking", "car parked outside", "motion detected at night",
|
| 15 |
-
"woman in red dress", "man in blue shirt", "person in hoodie",
|
| 16 |
-
"person in sunglasses", "dog running", "car with unknown license plate",
|
| 17 |
-
"person with umbrella", "package pickup", "snow falling",
|
| 18 |
-
"person approaching house", "truck on driveway", "delivery vehicle parked",
|
| 19 |
-
"child riding bike", "person in white shirt", "man on phone",
|
| 20 |
-
"suspicious vehicle", "package delivered", "motion near garage",
|
| 21 |
-
"person wearing mask", "dog on porch", "truck driving by",
|
| 22 |
-
"person walking past house", "car idling", "person leaving house",
|
| 23 |
-
"woman on porch", "man in gray jacket", "person with package",
|
| 24 |
-
"unknown person at gate", "package on doorstep", "person walking dog at night",
|
| 25 |
-
"vehicle at front door", "person wearing cap", "suspicious delivery person",
|
| 26 |
-
"dog chasing car", "truck parked in front", "child walking alone",
|
| 27 |
-
"person wearing backpack", "car pulling into driveway", "motion near window",
|
| 28 |
-
"delivery package dropped", "person with camera", "man in uniform",
|
| 29 |
-
"woman in yellow dress", "person wearing gloves", "car driving fast",
|
| 30 |
-
"person entering gate", "truck leaving area", "package picked up",
|
| 31 |
-
"motion near mailbox", "suspicious man", "suspicious woman",
|
| 32 |
-
"person with box", "person with shopping bag", "dog near garage",
|
| 33 |
-
"person wearing jacket", "truck with logo", "suspicious backpack",
|
| 34 |
-
"child playing near car", "person in front yard", "motion detected at gate",
|
| 35 |
-
"package delivery", "person walking fast", "truck making delivery",
|
| 36 |
-
"man with camera", "person near fence", "suspicious car parked",
|
| 37 |
-
"person wearing gloves", "vehicle in backyard", "motion near porch",
|
| 38 |
-
"person with package in hand", "truck with delivery sign", "child on bike",
|
| 39 |
-
"person standing still", "car with lights on", "suspicious vehicle in front",
|
| 40 |
-
"woman with shopping bags", "person wearing jeans", "suspicious dog",
|
| 41 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export.py
CHANGED
|
@@ -3,7 +3,6 @@ from PIL import Image
|
|
| 3 |
import requests
|
| 4 |
from transformers import CLIPProcessor, CLIPModel
|
| 5 |
from transformers.models.clip.modeling_clip import _get_vector_norm
|
| 6 |
-
import coremltools as ct
|
| 7 |
import torch
|
| 8 |
import numpy as np
|
| 9 |
import platform
|
|
@@ -69,6 +68,7 @@ with torch.no_grad():
|
|
| 69 |
traced_text_model = torch.jit.trace(text, (inputs.data['input_ids'], inputs.data['attention_mask']))
|
| 70 |
|
| 71 |
def convert_coreml():
|
|
|
|
| 72 |
coreml_model = ct.convert(traced_vision_model, inputs=[ct.TensorType(shape=inputs.data['pixel_values'].shape)])
|
| 73 |
coreml_model.save('vision.mlpackage')
|
| 74 |
|
|
@@ -78,6 +78,7 @@ def convert_coreml():
|
|
| 78 |
# convert_coreml()
|
| 79 |
|
| 80 |
def infer_coreml():
|
|
|
|
| 81 |
coreml_vision_model = ct.models.MLModel('vision.mlpackage')
|
| 82 |
coreml_text_model = ct.models.MLModel('text.mlpackage')
|
| 83 |
|
|
@@ -164,6 +165,48 @@ def infer_openvino():
|
|
| 164 |
|
| 165 |
# infer_openvino()
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
def export_ncnn():
|
| 169 |
traced_vision_model.save(f"vision.pt")
|
|
@@ -175,7 +218,7 @@ def export_ncnn():
|
|
| 175 |
input_shape2_str = json.dumps(list(inputs.data['attention_mask'].shape)).replace(" ", "")
|
| 176 |
os.system(f"pnnx text.pt 'inputshape={input_shape_str}i64,{input_shape2_str}i64'")
|
| 177 |
|
| 178 |
-
export_ncnn()
|
| 179 |
|
| 180 |
def infer_ncnn():
|
| 181 |
import ncnn
|
|
|
|
| 3 |
import requests
|
| 4 |
from transformers import CLIPProcessor, CLIPModel
|
| 5 |
from transformers.models.clip.modeling_clip import _get_vector_norm
|
|
|
|
| 6 |
import torch
|
| 7 |
import numpy as np
|
| 8 |
import platform
|
|
|
|
| 68 |
traced_text_model = torch.jit.trace(text, (inputs.data['input_ids'], inputs.data['attention_mask']))
|
| 69 |
|
| 70 |
def convert_coreml():
|
| 71 |
+
import coremltools as ct
|
| 72 |
coreml_model = ct.convert(traced_vision_model, inputs=[ct.TensorType(shape=inputs.data['pixel_values'].shape)])
|
| 73 |
coreml_model.save('vision.mlpackage')
|
| 74 |
|
|
|
|
| 78 |
# convert_coreml()
|
| 79 |
|
| 80 |
def infer_coreml():
|
| 81 |
+
import coremltools as ct
|
| 82 |
coreml_vision_model = ct.models.MLModel('vision.mlpackage')
|
| 83 |
coreml_text_model = ct.models.MLModel('text.mlpackage')
|
| 84 |
|
|
|
|
| 165 |
|
| 166 |
# infer_openvino()
|
| 167 |
|
| 168 |
+
def export_openvino_int8():
|
| 169 |
+
import openvino as ov
|
| 170 |
+
import text_calibration
|
| 171 |
+
import image_calibration
|
| 172 |
+
import nncf
|
| 173 |
+
|
| 174 |
+
ov_vision_model = ov.Core().read_model("openvino/vision.xml")
|
| 175 |
+
ov_text_model = ov.Core().read_model("openvino/text.xml")
|
| 176 |
+
|
| 177 |
+
vision_calibration_dataset = image_calibration.get_image_calibration_data()
|
| 178 |
+
text_calibration_dataset = text_calibration.get_text_calibration_data()
|
| 179 |
+
|
| 180 |
+
vision_dataset = nncf.Dataset(vision_calibration_dataset)
|
| 181 |
+
text_dataset = nncf.Dataset(text_calibration_dataset)
|
| 182 |
+
|
| 183 |
+
quantized_vision_model = nncf.quantize(ov_vision_model, vision_dataset, preset=nncf.QuantizationPreset.MIXED)
|
| 184 |
+
quantized_text_model = nncf.quantize(ov_text_model, text_dataset, preset=nncf.QuantizationPreset.MIXED)
|
| 185 |
+
|
| 186 |
+
ov.save_model(quantized_vision_model, "openvino/vision_int8.xml")
|
| 187 |
+
ov.save_model(quantized_text_model, "openvino/text_int8.xml")
|
| 188 |
+
|
| 189 |
+
# export_openvino_int8()
|
| 190 |
+
|
| 191 |
+
def infer_openvino_int8():
|
| 192 |
+
import openvino as ov
|
| 193 |
+
ov_vision_model = ov.Core().read_model("openvino/vision_int8.xml")
|
| 194 |
+
ov_text_model = ov.Core().read_model("openvino/text_int8.xml")
|
| 195 |
+
|
| 196 |
+
compiled_vision_model = ov.Core().compile_model(ov_vision_model, "CPU")
|
| 197 |
+
compiled_text_model = ov.Core().compile_model(ov_text_model, "CPU")
|
| 198 |
+
|
| 199 |
+
vision_predictions = compiled_vision_model(inputs.data['pixel_values'])
|
| 200 |
+
text_predictions = compiled_text_model((inputs.data['input_ids'], inputs.data['attention_mask']))
|
| 201 |
+
|
| 202 |
+
image_embeds = vision_predictions[0]
|
| 203 |
+
text_embeds = text_predictions[0]
|
| 204 |
+
|
| 205 |
+
logits_per_text = text_embeds @ image_embeds.T
|
| 206 |
+
|
| 207 |
+
print("similarity:", logits_per_text.item())
|
| 208 |
+
|
| 209 |
+
infer_openvino_int8()
|
| 210 |
|
| 211 |
def export_ncnn():
|
| 212 |
traced_vision_model.save(f"vision.pt")
|
|
|
|
| 218 |
input_shape2_str = json.dumps(list(inputs.data['attention_mask'].shape)).replace(" ", "")
|
| 219 |
os.system(f"pnnx text.pt 'inputshape={input_shape_str}i64,{input_shape2_str}i64'")
|
| 220 |
|
| 221 |
+
# export_ncnn()
|
| 222 |
|
| 223 |
def infer_ncnn():
|
| 224 |
import ncnn
|
image_calibration.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import CLIPProcessor
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def get_image_list():
|
| 7 |
+
# Get list of all files in images directory
|
| 8 |
+
image_files = os.listdir('images/')
|
| 9 |
+
|
| 10 |
+
# Filter for .jpg files only
|
| 11 |
+
jpg_files = [f"images/{f}" for f in image_files if f.endswith('.jpg')]
|
| 12 |
+
|
| 13 |
+
return jpg_files
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_image_calibration_data():
|
| 17 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 18 |
+
|
| 19 |
+
calibration_data = []
|
| 20 |
+
for image_file in get_image_list():
|
| 21 |
+
image_path = os.path.join(image_file)
|
| 22 |
+
image = Image.open(image_path)
|
| 23 |
+
inputs = processor(images=image, return_tensors="pt", padding="max_length", truncation=True)
|
| 24 |
+
calibration_data.append(inputs.data["pixel_values"])
|
| 25 |
+
|
| 26 |
+
return calibration_data
|
images/000000001999.jpg
ADDED
|
Git LFS Details
|
images/000000003899.jpg
ADDED
|
Git LFS Details
|
images/000000006712.jpg
ADDED
|
Git LFS Details
|
images/000000009658.jpg
ADDED
|
Git LFS Details
|
images/000000012999.jpg
ADDED
|
Git LFS Details
|
images/000000013517.jpg
ADDED
|
Git LFS Details
|
images/000000017153.jpg
ADDED
|
Git LFS Details
|
images/000000020063.jpg
ADDED
|
Git LFS Details
|
images/000000024307.jpg
ADDED
|
Git LFS Details
|
images/000000024457.jpg
ADDED
|
Git LFS Details
|
images/000000028127.jpg
ADDED
|
Git LFS Details
|
images/000000029161.jpg
ADDED
|
Git LFS Details
|
images/000000029716.jpg
ADDED
|
Git LFS Details
|
images/000000030677.jpg
ADDED
|
Git LFS Details
|
images/000000033300.jpg
ADDED
|
Git LFS Details
|
images/000000033888.jpg
ADDED
|
Git LFS Details
|
images/000000034973.jpg
ADDED
|
Git LFS Details
|
images/000000036693.jpg
ADDED
|
Git LFS Details
|
images/000000038226.jpg
ADDED
|
Git LFS Details
|
images/000000038999.jpg
ADDED
|
Git LFS Details
|
images/000000039764.jpg
ADDED
|
Git LFS Details
|
images/000000040380.jpg
ADDED
|
Git LFS Details
|
images/000000040768.jpg
ADDED
|
Git LFS Details
|
images/000000040870.jpg
ADDED
|
Git LFS Details
|
images/000000042404.jpg
ADDED
|
Git LFS Details
|
images/000000044446.jpg
ADDED
|
Git LFS Details
|
images/000000047942.jpg
ADDED
|
Git LFS Details
|
images/000000048491.jpg
ADDED
|
Git LFS Details
|
images/000000048595.jpg
ADDED
|
Git LFS Details
|
images/000000053977.jpg
ADDED
|
Git LFS Details
|
images/000000061834.jpg
ADDED
|
Git LFS Details
|
images/000000061945.jpg
ADDED
|
Git LFS Details
|
images/000000063409.jpg
ADDED
|
Git LFS Details
|
images/000000063873.jpg
ADDED
|
Git LFS Details
|
images/000000063879.jpg
ADDED
|
Git LFS Details
|
images/000000065177.jpg
ADDED
|
Git LFS Details
|
images/000000066503.jpg
ADDED
|
Git LFS Details
|
images/000000069121.jpg
ADDED
|
Git LFS Details
|
images/000000072207.jpg
ADDED
|
Git LFS Details
|
images/000000075257.jpg
ADDED
|
Git LFS Details
|
images/000000076428.jpg
ADDED
|
Git LFS Details
|
images/000000077639.jpg
ADDED
|
Git LFS Details
|
images/000000077864.jpg
ADDED
|
Git LFS Details
|
images/000000081406.jpg
ADDED
|
Git LFS Details
|
images/000000083755.jpg
ADDED
|
Git LFS Details
|