🌑

Stephen Cheng

Convert Pre-trained Model from MXNet to PyTorch or TensorFlow

 

Stephen Cheng

Intro

Currently there are many available deep learning frameworks for researchers and engineers to implement their desired deep models. Sometimes, when you find a fantastic GitHub repository which share a pre-trained model on a framework which you are not familiar with. For example, you are an expert PyTorch deep learning code developer, meanwhile you find a great code with its pre-trained model on MXNet; and you want to modify this model according to your needs. Thus, deep learning model conversion tools are extremely needed. As each framework has its own structure, converting a model between two different frameworks requires a great knowledge of both of them. However, There are many fantastic model conversion tools such as ONNX, MMdnn, and etc. for converting and visualizing deep models between a wide collection of frameworks.

Model Convertors

  • ONNX

ONNX is an effort to unify converters for neural networks in order to bring some sanity to the NN world. Released by Facebook and Microsoft.

  • MMdnn

MMdnn (Model Management Deep Neural Network) is supported by Microsoft, By using MMdnn, one can convert each model from the origin framework to a standard Intermediate Representation (IR), and then convert the IR format to the target framework structure. It can convert models between CaffeEmit, CNTK, CoreML, Keras, MXNet, ONNX, PyTorch and TensorFlow.

  • PyTorch convertor

PyTorch convertor can convert models to PyTorch model.

  • TensorFlow convertor

TensorFlow convertor can convert models to TensorFlow model.

  • Keras convertor

Keras convertor can convert models to Keras model.

  • MXNet convertor

MXNet convertor can convert models to MXNet model.

  • Caffe convertor

Caffe convertor can convert models to Caffe model.

  • Caffe2 convertor

Caffe2 convertor can convert models to Caffe2 model.

  • CNTK convertor

CNTK convertor can convert models to CNTK model.

  • Theano/Lasagne convertor

Theano/Lasagne convertor can convert models to Theano/Lasagne model.

  • Darknet convertor

Darknet convertor can convert models to Darknet model.

  • Torch convertor

Torch convertor can convert models to Torch model.

  • Neon convertor

Neon convertor can convert models to Neon model.

  • CoreML convertor

CoreML convertor can convert models to coreML model.

  • Paddle convertor

Paddle convertor can convert models to Paddle model.

  • Chainer convertor

Chainer convertor can convert models to Chainer model.

A Demo of Model Convertion from MXNet to PyTorch

Here is an appropriate example to convert the Full ImageNet pre-trained model from MXNet to PyTorch via MMdnn convertor. ImageNet is an image database organized according to the WordNet hierarchy, in which each node of the hierarchy is depicted by hundreds and thousands of images. Since 2010, the annual ImageNet Large Scale Visual Recognition Challenge (ILSVRC) is a competition where research teams evaluate their algorithms on the given data set, and compete to achieve higher accuracy on several visual recognition tasks. A common reason to train a network on ImageNet data is to use it for transfer learning (including feature extraction or fine-tuning other models). Having a pre-trained model which is trained on such a huge training data set (i.e., full ImageNet), would be a really valuable network. It can speed up the convergence early in the training phase, and also improves the target task accuracy in some scenarios.

  • Prerequisites:
1
sudo pip3 install --upgrade mmdnn
1
sudo pip3 install --upgrade torch torchvision
  • Download pre-trained models:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import errno


_base_model_url = 'http://data.mxnet.io/models/'
_default_model_info = {
'imagenet11k-resnet-152': {'symbol':_base_model_url+'imagenet-11k/resnet-152/resnet-152-symbol.json',
'params':_base_model_url+'imagenet-11k/resnet-152/resnet-152-0000.params'},
}


def download_file(url, local_fname=None, force_write=False):
# requests is not default installed
import requests
if local_fname is None:
local_fname = url.split('/')[-1]
if not force_write and os.path.exists(local_fname):
return local_fname

dir_name = os.path.dirname(local_fname)

if dir_name != "":
if not os.path.exists(dir_name):
try: # try to create the directory if it doesn't exists
os.makedirs(dir_name)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise

r = requests.get(url, stream=True)
assert r.status_code == 200, "failed to open %s" % url
with open(local_fname, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
return local_fname


def download_model(model_name, dst_dir='./', meta_info=None):
if meta_info is None:
meta_info = _default_model_info
meta_info = dict(meta_info)
if model_name not in meta_info:
return (None, 0)
if not os.path.isdir(dst_dir):
os.mkdir(dst_dir)
meta = dict(meta_info[model_name])
assert 'symbol' in meta, "missing symbol url"
model_name = os.path.join(dst_dir, model_name)
download_file(meta['symbol'], model_name+'-symbol.json')
assert 'params' in meta, "mssing parameter file url"
download_file(meta['params'], model_name+'-0000.params')
return (model_name, 0)


if __name__ == "__main__":
# ***** Download synset (i.e., Synonym Set):
synset_url = 'http://data.mxnet.io.s3-website-us-west-1.amazonaws.com/models/imagenet-11k/synset.txt'
download_file(synset_url, 'synset.txt')

# ***** Download Model:
download_model('imagenet11k-resnet-152', dst_dir='./')
  • Converting Full ImageNet Pre-trained Model from MXNet to PyTorch:
1
python3 -m mmdnn.conversion._script.convertToIR -f mxnet -n imagenet11k-resnet-152-symbol.json -w imagenet11k-resnet-152-0000.params -d resnet152 --inputShape 3,224,224
1
python3 -m mmdnn.conversion._script.IRToCode -f pytorch --IRModelPath resnet152.pb --dstModelPath kit_imagenet.py --IRWeightPath resnet152.npy -dw kit_pytorch.npy
1
python3 -m mmdnn.conversion.examples.pytorch.imagenet_test --dump resnet152Full.pth -n kit_imagenet.py -w kit_pytorch.npy
  • Testing the Converted Model:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import torch
import numpy as np
from tensorflow.contrib.keras.api.keras.preprocessing import image


# ************** Parameters:
num_predictions = 5 # Top-k Results
model_address = 'resnet152Full.pth' # for loading models
lexicon_address = 'synset.txt'
test_image_address = 'seagull.jpg'
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")


# Load Converted Model:
model = torch.load(model_address).to(device)
model.eval()


# Read Input Image and Apply Pre-process:
img = image.load_img(test_image_address, target_size=(224, 224))
x = image.img_to_array(img)
x = x[..., ::-1] # transform image from RGB to BGR
x = np.transpose(x, (2, 0, 1))
x = np.expand_dims(x, 0).copy()
x = torch.from_numpy(x)
x = x.to(device)


# Load Full-ImageNet Dictionary (i.e., lexicon):
with open(lexicon_address, 'r') as f:
labels = [l.rstrip() for l in f]


# Make prediction (forward pass):
with torch.no_grad():
output = model(x)
max, argmax = output.data.squeeze().max(0)
class_id = argmax.item()
class_name = labels[class_id]


# Print the top-5 Results:
h_x = output.data.squeeze()
probs, idx = h_x.sort(0, True)
print('Top-5 Results: ')
for i in range(0, num_predictions):
print('{:.2f}% -> {}'.format(probs[i] * 100.0, labels[idx[i]]))
str_final_label = 'The Image is a ' + class_name[10:] + '.'
print(str_final_label)

, , — Apr 20, 2020

Search

    Made with ❤️ and ☀️ on Earth.