forked from uwrealitylabs/universal-gestures-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_two_hands.py
138 lines (109 loc) · 4.12 KB
/
model_two_hands.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import json
from pprint import pprint
output_dim = 1 # binary classification for thumbs up or down
input_dim = 44 # 44 features
detect_threshold = 0.7 # threshold for classification as a thumbs up
SAVE_MODEL_PATH = "trained_model/"
SAVE_MODEL_FILENAME = "model_two_hands_weights.json"
# Model
class FeedforwardNeuralNetModel(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(FeedforwardNeuralNetModel, self).__init__()
# Linear function
self.fc1 = nn.Linear(input_dim, hidden_dim)
# Non-linearity
self.sigmoid = nn.Sigmoid()
# Linear function (readout)
self.fc2 = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
# Linear function
out = self.fc1(x)
# Non-linearity
out = self.sigmoid(out)
# Linear function (readout)
out = self.fc2(out)
return torch.sigmoid(out)
# Data set
def split_feature_label(data):
X = data[:, :-1]
Y = data[:, -1]
return X, Y
class CustomDataset(torch.utils.data.Dataset):
def __init__(self, data):
self.X, self.Y = split_feature_label(data)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.X[idx], self.Y[idx]
# Loader fn
def load_data(dataset, batch_size=64):
loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True)
return loader
def main():
train_path = "train_data/train_0.pt"
test_path = "test_data/test_0.pt"
train_data = torch.load(train_path)
test_data = torch.load(test_path)
batch_size = 64
n_iters = len(train_data) * 5 # 5 epochs
num_epochs = int(n_iters / (len(train_data) / batch_size))
X_train = torch.tensor(train_data[:, :-1])
y_train = torch.tensor(train_data[:, -1])
train_loader = torch.utils.data.DataLoader(
list(zip(X_train, y_train)), shuffle=True, batch_size=16
)
X_test = torch.tensor(test_data[:, :-1])
y_test = torch.tensor(test_data[:, -1])
test_loader = torch.utils.data.DataLoader(
list(zip(X_test, y_test)), shuffle=True, batch_size=16
)
model = FeedforwardNeuralNetModel(input_dim, 100, output_dim)
criterion = nn.BCELoss()
learning_rate = 0.0004
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
iter = 0
for epoch in range(num_epochs):
for i, (X, Y) in enumerate(train_loader):
Y = Y.view(-1, 1)
optimizer.zero_grad()
outputs = model(X.float())
loss = criterion(outputs, Y.float())
loss.backward()
optimizer.step()
iter += 1
if iter % 500 == 0:
correct = 0
total = 0
for X, Y in test_loader:
outputs = model(X.float())
predicted = (outputs > detect_threshold).float()
total += Y.size(0)
correct += (predicted == Y.view(-1, 1)).sum().item()
accuracy = 100 * correct / total
print(
"Iteration: {}. Loss: {}. Accuracy: {}".format(
iter, loss.item(), accuracy
)
)
# Extract the model's state dictionary, convert to JSON serializable format
state_dict = model.state_dict()
serializable_state_dict = {key: value.tolist() for key, value in state_dict.items()}
# Store state dictionary
with open(SAVE_MODEL_PATH + SAVE_MODEL_FILENAME, "w") as f:
json.dump(serializable_state_dict, f)
# Store as onnx for compatibility with Unity Barracuda
onnx_program = torch.onnx.dynamo_export(model, torch.randn(1, input_dim))
onnx_program.save(SAVE_MODEL_PATH + SAVE_MODEL_FILENAME.split(".")[0] + ".onnx")
print("\n--- Model Training Complete ---")
print("\nModel weights saved to ", SAVE_MODEL_PATH + SAVE_MODEL_FILENAME)
if __name__ == "__main__":
main()