how to do a PTQ with Fast R-CNN? #2547
-
I've been working on converting Torch models into OpenVINO models recently. I'm not sure how to solve this. Could you help me? import sys if sys.platform == "win32": def transformDTT_fn(dataItem): torch_device = torch.device("cuda" if torch.cuda.is_available() else "cpu") DATASET_FOLDER = - Path(OUTPUT_FOLDER).mkdir(exist_ok=True) torchModel = createModel(TASK_TYPE, BASE_MODEL, PRETRAINED_MODEL_PATH, NUM_CLASSES, IMAGE_SIZE[0], IMAGE_SIZE[1]) normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) trainTransform = transforms.Compose([transforms.Resize(IMAGE_SIZE), transforms.ToTensor(), normalize]) trainDataset = customDatasetDTT(xmlFile=XML_PATH, mode='TRAINING', imageFolder=DATASET_FOLDER, transform=trainTransform) trainLoader = torch.utils.data.DataLoader( valLoader = torch.utils.data.DataLoader( testLoader = torch.utils.data.DataLoader( calibrationDataset = nncf.Dataset(trainLoader, transformDTT_fn) ################################################ RuntimeError Traceback (most recent call last) File c:\Users\sa\anaconda3\envs\openVino\lib\site-packages\nncf\quantization\quantize_model.py:146, in quantize(model, calibration_dataset, mode, preset, target_device, subset_size, fast_bias_correction, model_type, ignored_scope, advanced_parameters) File c:\Users\sa\anaconda3\envs\openVino\lib\site-packages\nncf\torch\quantization\quantize_model.py:67, in quantize_impl(model, calibration_dataset, mode, preset, target_device, subset_size, fast_bias_correction, model_type, ignored_scope, advanced_parameters) File c:\Users\sa\anaconda3\envs\openVino\lib\site-packages\nncf\quantization\algorithms\post_training\algorithm.py:112, in PostTrainingQuantization.apply(self, model, graph, statistic_points, dataset) File c:\Users\sa\anaconda3\envs\openVino\lib\site-packages\nncf\quantization\algorithms\pipeline.py:164, in Pipeline.run_from_step(self, model, dataset, graph, start_step_index, step_index_to_statistics) File c:\Users\sa\anaconda3\envs\openVino\lib\site-packages\nncf\quantization\algorithms\pipeline.py:117, in Pipeline.run_step(self, step_index, step_statistics, model, graph) File c:\Users\sa\anaconda3\envs\openVino\lib\site-packages\nncf\quantization\algorithms\min_max\algorithm.py:834, in MinMaxQuantization.apply(self, model, graph, statistic_points, dataset) RuntimeError: Statistics were not collected for the node FasterRCNN/RoIHeads[roi_heads]/__sub___4 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @MinGiSa Thanks for the detailed report. Yes, we have a issue with quantization of FasterRCNN in PyTorch Backend. Shorter reproducer: import torch
import torchvision
from torchvision.models.detection.faster_rcnn import FasterRCNN_ResNet50_FPN_Weights
import nncf
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT)
image = torch.rand(1, 3, 600, 1200)
int8_model = nncf.quantize(model, nncf.Dataset([image])) To get quantized OpenVINO you can convert model to IR and use OpenVINO backend. from pathlib import Path
import openvino as ov
import torch
import torchvision
from torchvision.models.detection.faster_rcnn import FasterRCNN_ResNet50_FPN_Weights
import nncf
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT)
image = torch.rand(1, 3, 600, 1200)
output_dir = Path("faster_rcnn")
output_dir.mkdir(exist_ok=True)
# Convert to ONNX
# Note: FasterRCNN cannot be directly converted to an IR catalog by JIT tracing
onnx_fp32_path = output_dir / "faster_rcnn_fp32.onnx"
torch.onnx.export(model, image, onnx_fp32_path.as_posix(), opset_version=13)
# Convert to OpenVINO
ov_model = ov.convert_model(onnx_fp32_path.as_posix(), example_input=image, input=tuple(image.shape))
ov_fp32_path = output_dir / "faster_rcnn_fp32.xml"
ov.save_model(ov_model, ov_fp32_path, compress_to_fp16=False)
# Quantization
ov_int8_model = nncf.quantize(ov_model, nncf.Dataset([image]))
ov_int8_path = output_dir / "faster_rcnn_int8_ignored.xml"
ov.save_model(ov_int8_model, ov_int8_path, compress_to_fp16=False) |
Beta Was this translation helpful? Give feedback.
Hi @MinGiSa
Thanks for the detailed report.
Yes, we have a issue with quantization of FasterRCNN in PyTorch Backend.
Shorter reproducer:
To get quantized OpenVINO you can convert model to IR and use OpenVINO backend.