Skip to main content

Convert Caffe model to ONNX model for inference

Project description

caffe-onnx

This tool converts caffe model convert to onnx model
only use for inference

中文文档请参阅 README_CN.md

Introduction

This is the second version of converting caffe model to onnx model. In this version, all the parameters will be transformed to tensor and tensor value info when reading .caffemodel file and each operator node is constructed directly into the type of NodeProto in onnx.

Dependencies

  • protobuf
  • onnx==1.4.0
$ pip install -r requirements.txt

( caffe environment is not required! )

How to Use

usage: convert2onnx.py [-h] [caffe_graph_path] [caffe_params_path] [onnx_name] [save_dir]

positional arguments:
  caffe_graph_path          caffe's prototxt file path
  caffe_params_path         caffe's caffemodel file path
  onnx_name                 onnx model name
  save_dir                  onnx model file saved path

Take ResNet-50 as an example, you can follow the instructions.

  1. Download resnet50 .caffemodel file from BaiduDisk and put resnet-50-model.caffemodel to ./caffemodel/resnet-50/
    Link:https://pan.baidu.com/s/10YB42muAd0vGiNTCetvLsA
    Code:7az4

  2. Convert resnet50 caffe model to onnx model

    $ python convert2onnx.py \
              caffemodel/resnet-50/resnet-50-model.prototxt \
              caffemodel/resnet-50/resnet-50-model.caffemodel \
              resnet50 onnxmodel
    
  3. Visualize onnx model by netron

    $ netron onnxmodel/resnet50.onnx --host 0.0.0.0 --port 8008
    
  4. Run test scripts

    $ python onnxmodel/test_resnet.py \
              --input_shape 224 224 \
              --img_path onnxmodel/airplane.jpg \
              --onnx_path onnxmodel/resnet50.onnx
    
    # you will get result 404 which is the class id of airplane in IMAGENET.
    
  5. If you have custom layers in caffe which makes your caffe.proto is different than the one in the origin caffe code. The things you should do before convertion is:

    • First of all, compile your proto file with protoc

      # for example
      $ protoc /your/path/to/caffe_ssd.proto --python_out ./proto
      
    • Then specify the caffe proto file by replacing the line from proto import caffe_upsample_pb2 as caffe_pb2 with your module.

Current Support Operator

BatchNorm
Convolution
Deconvolution
Concat
Dropout
InnerProduct(Reshape+Gemm)
LRN
Pooling
Unpooling
ReLU
Softmax
Eltwise
Upsample
Scale

Test Caffe Model

  • Resnet50
  • AlexNet
  • Agenet
  • Yolo V3
  • vgg16

Visualization

netron is recommended: https://github.com/lutzroeder/netron
netron Browser

Development Guide

See Develop Guide

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

caffe_onnx-0.1.0.tar.gz (57.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

caffe_onnx-0.1.0-py3-none-any.whl (66.0 kB view details)

Uploaded Python 3

File details

Details for the file caffe_onnx-0.1.0.tar.gz.

File metadata

  • Download URL: caffe_onnx-0.1.0.tar.gz
  • Upload date:
  • Size: 57.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.2

File hashes

Hashes for caffe_onnx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b4d84fd2255ea7357d62909fbf1c32c2fecc9d9e1ea90cc981b35eab11103412
MD5 656fe72d9f0a0cbee49a0b236d583f4b
BLAKE2b-256 d50e8c90c006687555be1bbcf8dff04046bb1cc34e3099a39286868b796b60f7

See more details on using hashes here.

File details

Details for the file caffe_onnx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: caffe_onnx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 66.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.2

File hashes

Hashes for caffe_onnx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6451497b11614007e623a17553a32e78994c62d5af72ae6b409e3cab4fd7365f
MD5 cb883ca8f0f19176235dfc94538598d2
BLAKE2b-256 fc07f9eb8265200e06e69e953f27a5152c1182daecbcb70713ced2519b9e90dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page