1. Pytorch分类器网络
# 定义一个简单的分类网络
class SimpleNet(nn.Module):
def __init__(self):
super(SimpleNet, self).__init__()
# 三个卷积层用于提取特征
# 1 input channel image 90x90, 8 output channel image 44x44
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=8, kernel_size=3, stride=1, padding=0),
nn.ReLU(),
nn.MaxPool2d(2)
)
# 8 input channel image 44x44, 16 output channel image 22x22
self.conv2 = nn.Sequential(
nn.Conv2d(in_channels=8, out_channels=8, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2)
)
# 16 input channel image 22x22, 32 output channel image 10x10
self.conv3 = nn.Sequential(
nn.Conv2d(in_channels=8, out_channels=8, kernel_size=3, stride=1, padding=0),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.conv4 = nn.Sequential(
nn.Conv2d(in_channels=8, out_channels=8, kernel_size=3, stride=1, padding=0),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.conv5 = nn.Sequential(
nn.Conv2d(in_channels=8, out_channels=8, kernel_size=3, stride=1, padding=0),
nn.ReLU(),
nn.MaxPool2d(2)
)
# 分类
self.classifier = nn.Sequential(
nn.Linear(400, 3)
)
def forward(self, x):
#print(x.size())
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
#print(x.size())
x = x.view(-1, 400)
x = self.classifier(x)
return x
2. pth转ONNX
# 转换pytorch训练的pth模型到ONNX模型
def convert_model_to_ONNX(input_pth_model, output_ONNX):
dummy_input = torch.randn(1, 1, 192, 108)
model = SimpleNet()
state_dict = torch.load(input_pth_model, map_location='cpu')
new_state_dict = collections.OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
input_names = ["input_image"]
output_names = ["output_classification"]
torch.onnx.export(model, dummy_input, output_ONNX, verbose=True, input_names=input_names,
output_names=output_names)
3. ONNX转MNN
MNN提供了转换ONNX到MNN模型的工具,执行如下脚本即可,关于MNN转换工具编译可以参考Model Conversion。下面是转换脚本:
./MNNConvert -f ONNX --modelFile classify.onnx --MNNModel classify.mnn --bizCode MNN
4. 在线部署
在线部署流程在这里,为使用MNN加载解析好的mnn模型参数进行inference等一系列业务操作。