Performance Discrepancy Between PyTorch and ONNX Model Conversion: Unexpected Increase in Latency

I have trained a binary image classifier using the Timm library and the ConvNeXt Base model as a pretrained model. When converting the model to ONNX format to optimize memory usage and response time, I observed a significant increase in latency. Specifically, for a batch of 100 images, the response time using the PyTorch .pth model was 7.6024 seconds, whereas the ONNX model took 74.3477 seconds under the same conditions. I am concerned about this discrepancy and would like to understand if this level of performance degradation is typical, as it was not anticipated.
This is the code I used to export the onnx model:

import time
import torch
import torch.onnx
import logging
import timm
import os

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def load_model(model_path, model_name='convnext_base', num_classes=2, pretrained=False):
    if not os.path.exists(model_path):
        logger.error(f"Model path {model_path} does not exist.")
        return None

    try:
        start_time = time.time()
        model = timm.create_model(model_name, pretrained=pretrained, num_classes=num_classes)
        model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
        model.eval()
        logger.info(f"Loading model and weights: {time.time() - start_time:.2f} seconds")
        return model
    except Exception as e:
        logger.error(f"Failed to load model: {e}")
        return None

def export_to_onnx(model, path, input_shape=(1, 3, 512, 512)):
    try:
        start_time = time.time()
        dummy_input = torch.randn(input_shape)
        torch.onnx.export(
            model, dummy_input, path,
            export_params=True,
            opset_version=18,
            do_constant_folding=True,
            input_names=['input'],
            output_names=['output'],
            dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}}
        )
        logger.info(f"Exporting to ONNX: {time.time() - start_time:.2f} seconds")
    except Exception as e:
        logger.error(f"Failed to export to ONNX: {e}")

if __name__ == "__main__":
    model_path = 'models/convnext_base_classifier-p12.pth'
    onnx_model_path = 'models/convnext_base_classifier-p12.onnx'
    num_classes = 2  # Set this to the number of output classes in your model

    # Load model
    model = load_model(model_path, model_name='convnext_base', num_classes=num_classes)
    
    if model:
        # Export to ONNX
        export_to_onnx(model, onnx_model_path)

        # Load and check the ONNX model
        import onnx
        onnx_model = onnx.load(onnx_model_path)
        onnx.checker.check_model(onnx_model)
        logger.info(f"ONNX model is well-formed and valid.")

I tried to convert a pytorch model (.pth) to .onnx model in order to get better latency but I get the inverse ‘dramatically’

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật