-
Notifications
You must be signed in to change notification settings - Fork 1
/
fhe_onnx_convert.py
48 lines (37 loc) · 1.38 KB
/
fhe_onnx_convert.py
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
import torch
import torch.nn as nn
import torch.onnx as onnx
from plain_models import MLP_Bank
from tools import load_data
# convert credit and bank model to onnx for helayer
class MLP_Credit(nn.Module):
def __init__(self) -> None:
super().__init__()
self.fc1 = nn.Linear(23, 128)
self.fc2 = nn.Linear(128, 2)
def sigmoid(self, x):
return 0.5 + 0.197 * x - 0.004 * (x**3)
def forward(self, x):
x = self.fc1(x)
x = self.sigmoid(x)
x = self.fc2(x)
return x
def convert_model(data_name):
data_name = data_name.lower()
if data_name == "credit":
train_loader, test_loader, x_train = load_data(data_name, batch_size=1, example=True)
plain_model = MLP_Credit()
elif data_name == "bank":
train_loader, test_loader, x_train = load_data(data_name, batch_size=1, example=True)
plain_model = MLP_Bank()
else:
raise NotImplementedError(data_name)
plain_model.load_state_dict(torch.load(f'./pretrained/{data_name}_plain.pt'))
plain_model.eval()
input_shape = [1] + list(x_train[0].shape)
dummy_input = torch.rand(input_shape)
onnx.export(plain_model, dummy_input, f'./pretrained/{data_name}_plain.onnx',
input_names=['inputs'], output_names=['outputs'])
if __name__ == "__main__":
convert_model("credit")
convert_model("bank")