4.2. Example 2: Customized Multilayer Perceptron (MLP) Model
This example adds layers to a simple Multilayer Perceptron (MLP) model as follows:
- A ReLU layer was added after each linear transformation in the previous layer.
- A Softmax layer was added at the end.
These additions are shown as an example only. The performance of this customized model has not been tested or optimized.
Model Information:
- Model: Multilayer Perception (MLP)
- Framework: PyTorch®/ONNX*
Figure 3. Original MLP Model Layers
import argparse
import torch
import numpy as np
from torch import nn, onnx
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.model = nn.Sequential(
nn.Linear(10, 128),
nn.Linear(128, 80),
nn.Linear(80, 10),
)
def forward(self, x):
return self.model(x)
Figure 4. Modified MLP Model Layers
import argparse
import torch
import numpy as np
from torch import nn, onnx
import os
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.model = nn.Sequential(
nn.Linear(10, 128),
nn.ReLU(),
nn.Linear(128, 80),
nn.ReLU(),
nn.Linear(80, 10),
nn.ReLU(),
)
def forward(self, x):
return self.model(x)
This model is created with the PyTorch® framework but must be converted to ONNX* to use the model with the OpenVINO™ Model Optimizer. The following Python code example illustrates how you can convert the PyTorch® model to ONNX*:
onnx.export(model, x, args.onnx_file, export_params=True)
For more information about converting PyTorch® to ONNX*, review the ONNX exporter documentation at the following URL:
https://pytorch.org/docs/stable/onnx.html#example-alexnet-from-pytorch-to-onnx
After the conversion is complete and the ONNX* model is saved, convert the model to OpenVINO™ IR with the following command:
mo --input_model <path to model>.onnx