# Converting Pre-trained Model from PyTorch to OneFlow¶

Since interfaces of OneFlow and PyTorch are compatible, we can convert a pre-trained model from PyTorch to OneFlow when it's needed to use a PyTorch pre-trained model.

## Example of Model Conversion¶

In the following code, we define and save a PyTorch model and then convert it to a OneFlow model.

import torch
import torch.nn as nn
save_file = 'model.pth'
model_torch = nn.Sequential(
nn.Linear(128, 2),
nn.Softmax()
)
torch.save(model_torch, save_file)


After running the above code, we get a model.pth file of PyTorch model. Then, the following two steps enable us to covert a PyTorch model to a OneFlow model：

• Defining a OneFlow model with the same structure
• Loading the model.pth file and initializing model parameters into OneFlow model

Code is shown below:

import oneflow as flow
import oneflow.nn as nn
import torch
model_flow = nn.Sequential(
nn.Linear(128, 2),
nn.Softmax()
)
for key, value in parameters.items():
val = value.detach().cpu().numpy()
parameters[key] = val


.state_dict() enables us to obtain model parameters defined by key-value. Then, we use .detach().cpu().numpy() to convert parameter whose gradients are blocked into Numpy. Lastly, .load_state_dict(parameters) allows to pass model parameters to OneFlow model.

With the simple example described above, we can find that the approach to convert PyTorch model into OneFlow is to use Numpy as a bridge. Therefore, provided the models defined by PyTorch and by OneFlow whose structures are consistent, even complicated models can still be smoothly converted.

Same as torchvision, flowvision also provides many pre-trained models, and the models in flowvision are compatible with those in torchvision. Taking AlexNet in flowvision for example, we will show how to convert complicate PyTorch pre-trained models into OneFlow by running the following code:

import torchvision.models as models_torch
import flowvision.models as models_flow
alexnet_torch = models_torch.alexnet(pretrained=True)
alexnet_flow = models_flow.alexnet()
parameters = alexnet_torch.state_dict()
for key, value in parameters.items():
val = value.detach().cpu().numpy()
parameters[key] = val

alexnet_flow = models_flow.alexnet(pretrained=True)