def setUp(self): self.model = C.ResNet50( pretrained_model=None, initialW=chainer.initializers.Uniform(1), arch='he') self.model.pool1 = lambda x: F.max_pooling_2d( x, ksize=3, stride=2, cover_all=False) self.x = np.zeros((1, 3, 224, 224), dtype=np.float32) self.fn = 'ResNet50.onnx'
def export_onnx(input_image_path, output_path, gpu, only_output=True): """Export ResNet50 model to ONNX graph 'model.onnx' file will be exported under ``output_path``. """ model = C.ResNet50(pretrained_model='imagenet', arch='fb') input_image = read_image(input_image_path) input_image = scale(input_image, 256) input_image = center_crop(input_image, (224, 224)) input_image -= model.mean input_image = input_image[None, :] if gpu >= 0: model.to_gpu() input_image = chainer.cuda.to_gpu(input_image) if only_output: os.makedirs(output_path, exist_ok=True) name = os.path.join(output_path, 'model.onnx') export(model, input_image, filename=name) else: # an input and output given by Chainer will be also emitted # for using as test dataset export_testcase(model, input_image, output_path)
def main(): model = C.VGG16(pretrained_model='imagenet') save_as_onnx_then_import_from_nnvm(model, 'vgg16.onnx') model = C.ResNet50(pretrained_model='imagenet', arch='he') # Change cover_all option to False to match the default behavior of MXNet's pooling model.pool1 = lambda x: F.max_pooling_2d( x, ksize=3, stride=2, cover_all=False) save_as_onnx_then_import_from_nnvm(model, 'resnet50.onnx')
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import numpy as np import cupy import chainer import chainercv.links as C import onnx_chainer model = C.ResNet50(pretrained_model='imagenet', arch='he') #model = C.VGG16(pretrained_model='imagenet') model.to_gpu() # Pseudo input x = chainer.Variable(cupy.zeros((1, 3, 224, 224), dtype=np.float32)) onnx_chainer.export(model, x, filename='resnet50.onnx')
#!/usr/bin/env python # -*- coding: utf-8 -*- import argparse import chainer import numpy as np import chainercv.links as C import onnx_chainer parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--size', type=int, default=224) args = parser.parse_args() model = C.ResNet50() if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() # Pseudo input x = model.xp.zeros((1, 3, args.size, args.size), dtype=np.float32) onnx_chainer.export_testcase(model, x, 'resnet50_%d' % args.size)