Home

Awesome

gluon2pytorch

Build Status GitHub License Python Version Readthedocs

Gluon to PyTorch model convertor with script generation.

Installation

git clone https://github.com/gmalivenko/gluon2pytorch
cd gluon2pytorch
pip install -e . 

or you can use pip:

pip install gluon2pytorch

How to use

It's the convertor of Gluon graph to a Pytorch model file + weights.

Firstly, we need to load (or create) Gluon Hybrid model:


class ReLUTest(mx.gluon.nn.HybridSequential):
    def __init__(self):
        super(ReLUTest, self).__init__()
        from mxnet.gluon import nn
        with self.name_scope():
            self.conv1 = nn.Conv2D(3, 32)
            self.relu = nn.Activation('relu')

    def hybrid_forward(self, F, x):
        x = F.relu(self.relu(self.conv1(x)))
        return x


if __name__ == '__main__':
    net = ReLUTest()
    
    # Make sure it's hybrid and initialized
    net.hybridize()
    net.collect_params().initialize()

The next step - call the converter:

    pytorch_model = gluon2pytorch(net, [(1, 3, 224, 224)], dst_dir=None, pytorch_module_name='ReLUTest')

Finally, we can check the difference

    import torch
    input_np = np.random.uniform(-1, 1, (1, 3, 224, 224))

    gluon_output = net(mx.nd.array(input_np))
    pytorch_model.eval()
    pytorch_output = pytorch_model(torch.FloatTensor(input_np))
    check_error(gluon_output, pytorch_output)

Supported layers

Layers:

Reshape:

Activations:

Element-wise:

Misc:

Classification models converted with gluon2pytorch

ModelTop1Top5ParamsFLOPsSource weightsRemarks
ResNet-1037.0915.555,418,792892.62Mosmr's repoSuccess
ResNet-1235.8614.465,492,7761,124.23Mosmr's repoSuccess
ResNet-1432.8512.415,788,2001,355.64Mosmr's repoSuccess
ResNet-1630.6811.106,968,8721,586.95Mosmr's repoSuccess
ResNet-18 x0.2549.1624.45831,096136.64Mosmr's repoSuccess
ResNet-18 x0.536.5414.963,055,880485.22Mosmr's repoSuccess
ResNet-18 x0.7533.2512.546,675,3521,045.75Mosmr's repoSuccess
ResNet-1829.139.9411,689,5121,818.21Mosmr's repoSuccess
ResNet-3425.347.9221,797,6723,669.16Mosmr's repoSuccess
ResNet-5023.506.8725,557,0323,868.96Mosmr's repoSuccess
ResNet-50b22.926.4425,557,0324,100.70Mosmr's repoSuccess
ResNet-10121.665.9944,549,1607,586.30Mosmr's repoSuccess
ResNet-101b21.185.6044,549,1607,818.04Mosmr's repoSuccess
ResNet-15221.015.6160,192,80811,304.85Mosmr's repoSuccess
ResNet-152b20.545.3760,192,80811,536.58Mosmr's repoSuccess
PreResNet-1828.729.8811,687,8481,818.41Mosmr's repoSuccess
PreResNet-3425.888.1121,796,0083,669.36Mosmr's repoSuccess
PreResNet-5023.396.6825,549,4803,869.16Mosmr's repoSuccess
PreResNet-50b23.166.6425,549,4804,100.90Mosmr's repoSuccess
PreResNet-10121.455.7544,541,6087,586.50Mosmr's repoSuccess
PreResNet-101b21.735.8844,541,6087,818.24Mosmr's repoSuccess
PreResNet-15220.705.3260,185,25611,305.05Mosmr's repoSuccess
PreResNet-152b21.005.7560,185,25611,536.78MGluon Model ZooSuccess
PreResNet-200b21.105.6464,666,28015,040.27Mtornadomeet/ResNetSuccess
ResNeXt-101 (32x4d)21.325.7944,177,7047,991.62MCadene's repoSuccess
ResNeXt-101 (64x4d)20.605.4183,455,27215,491.88MCadene's repoSuccess
SE-ResNet-5022.516.4428,088,0243,877.01MCadene's repoSuccess
SE-ResNet-10121.925.8949,326,8727,600.01MCadene's repoSuccess
SE-ResNet-15221.485.7766,821,84811,324.62MCadene's repoSuccess
SE-ResNeXt-50 (32x4d)21.065.5827,559,8964,253.33MCadene's repoSuccess
SE-ResNeXt-101 (32x4d)19.995.0048,955,4168,005.33MCadene's repoSuccess
SENet-15418.844.65115,088,98420,742.40MCadene's repoSuccess
DenseNet-12125.117.807,978,8562,852.39MGluon Model ZooSuccess
DenseNet-16122.406.1828,681,0007,761.25MGluon Model ZooSuccess
DenseNet-16923.896.8914,149,4803,381.48MGluon Model ZooSuccess
DenseNet-20122.716.3620,013,9284,318.75MGluon Model ZooSuccess
DPN-6823.577.0012,611,6022,338.71MCadene's repoSuccess
DPN-9820.235.2861,570,72811,702.80MCadene's repoSuccess
DPN-13120.035.2279,254,50416,056.22MCadene's repoSuccess
DarkNet Tiny40.3117.461,042,104496.34Mosmr's repoSuccess
DarkNet Ref38.0016.687,319,416365.55Mosmr's repoSuccess
SqueezeNet v1.040.9718.961,248,424828.30Mosmr's repoSuccess
SqueezeNet v1.139.0917.391,235,496354.88Mosmr's repoSuccess
SqueezeResNet v1.139.8317.841,235,496354.88Mosmr's repoSuccess
ShuffleNetV2 x0.540.6118.301,366,79242.34Mosmr's repoSuccess
ShuffleNetV2c x0.539.8718.111,366,79242.37Mtensorpack/tensorpackSuccess
ShuffleNetV2 x1.033.7613.222,278,604147.92Mosmr's repoSuccess
ShuffleNetV2c x1.030.7411.382,279,760148.85Mtensorpack/tensorpackSuccess
ShuffleNetV2 x1.532.3812.374,406,098318.61Mosmr's repoSuccess
ShuffleNetV2 x2.032.0412.107,601,686593.66Mosmr's repoSuccess
108-MENet-8x1 (g=3)43.6220.30654,51640.64Mosmr's repoSuccess
128-MENet-8x1 (g=4)45.8021.93750,79643.58Mclavichord93/MENetSuccess
228-MENet-12x1 (g=3)35.0313.991,806,568148.93Mclavichord93/MENetSuccess
256-MENet-12x1 (g=4)34.4913.901,888,240146.11Mclavichord93/MENetSuccess
348-MENet-12x1 (g=3)31.1711.413,368,128306.31Mclavichord93/MENetSuccess
352-MENet-12x1 (g=8)34.7013.752,272,872151.03Mclavichord93/MENetSuccess
456-MENet-24x1 (g=3)29.5710.435,304,784560.72Mclavichord93/MENetSuccess
MobileNet x0.2545.7822.18470,07242.30Mosmr's repoSuccess
MobileNet x0.536.1214.811,331,592152.04Mosmr's repoSuccess
MobileNet x0.7532.7112.282,585,560329.22MGluon Model ZooSuccess
MobileNet x1.029.2510.034,231,976573.83MGluon Model ZooSuccess
FD-MobileNet x0.2556.1931.38383,16012.44Mosmr's repoSuccess
FD-MobileNet x0.542.6219.69993,92840.93Mosmr's repoSuccess
FD-MobileNet x1.035.9514.722,901,288146.08Mclavichord93/FD-MobileNetSuccess
MobileNetV2 x0.2548.8925.241,516,39232.22MGluon Model ZooSuccess
MobileNetV2 x0.535.5114.641,964,73695.62MGluon Model ZooSuccess
MobileNetV2 x0.7530.8211.262,627,592191.61MGluon Model ZooSuccess
MobileNetV2 x1.028.519.903,504,960320.19MGluon Model ZooSuccess
NASNet-A-Mobile25.377.955,289,978587.29MCadene's repoSuccess
InceptionV321.225.5923,834,5685,746.72MGluon Model ZooSuccess
AirNet50-1x64d (r=2)22.486.2127,425,8644,757.77Msoeaver/AirNet-PyTorchSuccess
AirNet50-1x64d (r=16)22.916.4625,714,9524,385.54Msoeaver/AirNet-PyTorchSuccess
AirNeXt50-32x4d (r=2)20.875.5127,604,2965,321.18Msoeaver/AirNet-PyTorchSuccess
DiracNetV2-1831.4711.7011,511,7841,798.43Mszagoruyko/diracnetsSuccess
DiracNetV2-3428.759.9321,616,2323,649.37Mszagoruyko/diracnetsSuccess
DARTS26.708.744,718,752537.64Mszagoruyko/diracnetsSuccess
PolyNet19.104.5295,366,60034,768.84MCadene's repoSuccess
ZfNet????osmr's repoSuccess
FishNet-15022.856.3824,959,4006,435.02Mosmr's repoSuccess

Segmentation models converted with gluon2pytorch

NameModelpixAccmIoUSource weightsRemarks
fcn_resnet101_cocoFCN92.266.2Gluon Model ZooSuccess
fcn_resnet101_vocFCNN/A83.6Gluon Model ZooSuccess

Code snippets

Look at the tests directory.

License

This software is covered by MIT License.