You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello @MaximProshin,
Recently, I'm doing post-training quantization using NNCF with yolov8_custom_model(Uncompressed OpenVINO model). I observed that It is generating similar inference results on test set, Albeit its quantized version is unable to do inference on the same dataset.
Creating and instantiating CustomDataset Class
from PIL import Image
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
import openvino as ov
class CustomDataset(Dataset):
def __init__(self, images_dir, labels_dir, transform=None):
self.images_dir = images_dir
self.labels_dir = labels_dir
self.transform = transform
self.image_files = [f for f in os.listdir(images_dir) if f.endswith(('.png', '.jpg', '.jpeg'))]
def __len__(self):
return len(self.image_files)
def __getitem__(self, idx):
img_name = self.image_files[idx]
image_path = os.path.join(self.images_dir, img_name)
label_path = os.path.join(self.labels_dir, img_name.replace('.jpg', '.txt').replace('.png', '.txt').replace('.jpeg', '.txt'))
# Load image
image = Image.open(image_path).convert('RGB')
# Load label
with open(label_path, 'r') as f:
label = f.read().strip() # Read label file, modify as needed
if self.transform:
image = self.transform(image)
return image, label
# Define the transform
transform = transforms.Compose([transforms.Resize((640, 640)), transforms.ToTensor()])
# Path to validation dataset
dataset_root_path = path_to_root_dir
images_dir = os.path.join(dataset_root_path, 'images')
labels_dir = os.path.join(dataset_root_path, 'labels')
# Instantiate uncompressed model
core = ov.Core()
model = core.read_model(model_path)
# Instantiate the custom dataset
val_dataset = CustomDataset(images_dir=images_dir, labels_dir=labels_dir, transform=transform)
dataset_loader = DataLoader(val_dataset, batch_size=1, shuffle=True)```
Optimize model using NNCF Post-training Quantization API
import nncf # Neural Network Compression Framework
# Step 1: Initialize transformation function
def transform_fn(data_item):
"""
Quantization transform function. Extracts and preprocess input data from dataloader item for quantization.
Parameters:
data_item: Tuple with data item produced by DataLoader during iteration
Returns:
input_tensor: Input data for quantization
"""
img = data_item[0].numpy()
input_tensor = image_to_tensor(img)
return input_tensor
# Step 2: Initialize NNCF Dataset
calibration_dataset = nncf.Dataset(dataset_loader, transform_fn)
ignored_scope = nncf.IgnoredScope(
names=[
"/model.22/cv3.0/cv3.0.0/conv/Conv", # in the post-processing subgraph
"/model.16/conv/Conv",
"/model.22/cv2.0/cv2.0.0/conv/Conv",
"/model.6/cv1/conv/Conv",
"/model.22/cv3.1/cv3.1.1/conv/Conv",
"/model.21/cv1/conv/Conv",
"/model.21/m.0/cv1/conv/Conv",
"/model.7/conv/Conv",
"/model.12/cv1/conv/Conv",
"/model.4/cv1/conv/Conv",
"/model.22/cv2.2/cv2.2.1/conv/Conv",
"/model.22/cv2.0/cv2.0.1/conv/Conv",
"/model.22/dfl/conv/Conv",
"/model.22/cv3.2/cv3.2.2/Conv",
"/model.22/cv3.0/cv3.0.2/Conv",
"/model.15/cv1/conv/Conv",
"/model.5/conv/Conv",
"/model.0/conv/Conv"
]
)
# Detection model
quantized_model = nncf.quantize(model, calibration_dataset, preset=nncf.QuantizationPreset.MIXED,
ignored_scope=ignored_scope)
quantized_model_save_path = path_to_save_quantized_model
ov.save_model(quantized_model, quantized_model_save_path)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello @MaximProshin,
Recently, I'm doing post-training quantization using NNCF with yolov8_custom_model(Uncompressed OpenVINO model). I observed that It is generating similar inference results on test set, Albeit its quantized version is unable to do inference on the same dataset.
Creating and instantiating CustomDataset Class
Optimize model using NNCF Post-training Quantization API
Installed packages
Even, I tried without ignored_scope arguments, but still getting the same results. Please help me out in this context.
Beta Was this translation helpful? Give feedback.
All reactions