-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
96 lines (75 loc) · 3.57 KB
/
Copy pathmodels.py
File metadata and controls
96 lines (75 loc) · 3.57 KB
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
"""
Neural networks for motion prediction.
Copyright ETH Zurich, Manuel Kaufmann
"""
import torch
import torch.nn as nn
from data import AMASSBatch
from losses import mse
def create_model(config):
# This is a helper function that can be useful if you have several model definitions that you want to
# choose from via the command line. For now, we just return the Dummy model.
return DummyModel(config)
class BaseModel(nn.Module):
"""A base class for neural networks that defines an interface and implements a few common functions."""
def __init__(self, config):
super(BaseModel, self).__init__()
self.config = config
self.pose_size = config.pose_size
self.create_model()
# noinspection PyAttributeOutsideInit
def create_model(self):
"""Create the model, called automatically by the initializer."""
raise NotImplementedError("Must be implemented by subclass.")
def forward(self, batch: AMASSBatch):
"""The forward pass."""
raise NotImplementedError("Must be implemented by subclass.")
def backward(self, batch: AMASSBatch, model_out):
"""The backward pass."""
raise NotImplementedError("Must be implemented by subclass.")
def model_name(self):
"""A summary string of this model. Override this if desired."""
return '{}-lr{}'.format(self.__class__.__name__, self.config.lr)
class DummyModel(BaseModel):
"""
This is a dummy model. It provides basic implementations to demonstrate how more advanced models can be built.
"""
def __init__(self, config):
self.n_history = 10
super(DummyModel, self).__init__(config)
# noinspection PyAttributeOutsideInit
def create_model(self):
# In this model we simply feed the last time steps of the seed to a dense layer and
# predict the targets directly.
self.dense = nn.Linear(in_features=self.n_history * self.pose_size,
out_features=self.config.target_seq_len * self.pose_size)
def forward(self, batch: AMASSBatch):
"""
The forward pass.
:param batch: Current batch of data.
:return: Each forward pass must return a dictionary with keys {'seed', 'predictions'}.
"""
model_out = {'seed': batch.poses[:, :self.config.seed_seq_len],
'predictions': None}
batch_size = batch.batch_size
model_in = batch.poses[:, self.config.seed_seq_len-self.n_history:self.config.seed_seq_len]
pred = self.dense(model_in.reshape(batch_size, -1))
model_out['predictions'] = pred.reshape(batch_size, self.config.target_seq_len, -1)
return model_out
def backward(self, batch: AMASSBatch, model_out):
"""
The backward pass.
:param batch: The same batch of data that was passed into the forward pass.
:param model_out: Whatever the forward pass returned.
:return: The loss values for book-keeping, as well as the targets for convenience.
"""
predictions = model_out['predictions']
targets = batch.poses[:, self.config.seed_seq_len:]
total_loss = mse(predictions, targets)
# If you have more than just one loss, just add them to this dict and they will automatically be logged.
loss_vals = {'total_loss': total_loss.cpu().item()}
if self.training:
# We only want to do backpropagation in training mode, as this function might also be called when evaluating
# the model on the validation set.
total_loss.backward()
return loss_vals, targets