forked from elizamanelli/custom_net_on_movidius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytorch_to_movidius.py
67 lines (53 loc) · 2 KB
/
pytorch_to_movidius.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import torch.optim as optim
import torch.nn as nn
import torch
import torch.nn.functional as F
# installed: scikit-learn, pytorch, onnx, pandas
# Prepare the dataset
############################################################################################
# Load the iris dataset
iris = load_iris()
# Create X and y data
X = iris.data
y = iris.target
# Train Test Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Create the model
#############################################################################################
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.lin1 = nn.Linear(4, 50)
self.lin2 = nn.Linear(50, 50)
self.out = nn.Linear(50, 3)
def forward(self, x):
x = F.relu(self.lin1(x))
x = F.relu(self.lin2(x))
x = self.out(x)
return x
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters())
# Train the network
#############################################################################################
for epoch in range(50):
inputs = torch.autograd.Variable(torch.Tensor(X_train).float())
targets = torch.autograd.Variable(torch.Tensor(y_train).long())
optimizer.zero_grad()
out = net(inputs)
loss = criterion(out, targets)
loss.backward()
optimizer.step()
inputs = torch.autograd.Variable(torch.Tensor(X_test).float())
targets = torch.autograd.Variable(torch.Tensor(y_test).long())
optimizer.zero_grad()
out = net(inputs)
_, predicted = torch.max(out.data, 1)
error_count = y_test.size - np.count_nonzero((targets == predicted).numpy())
print('Errors: %d; Accuracy: %d%%' % (error_count, 100 * torch.sum(targets == predicted) / y_test.size))
# Save the model as ONNX
###############################################################################################
torch.onnx.export(net, inputs, "pytorch_iris.onnx")