-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
72 lines (52 loc) · 1.93 KB
/
model.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Model architecture for PyTorch
Date: 2020
Author: M. Sam Ribeiro
"""
import numpy as np
np.random.seed(42)
import random
random.seed(42)
import torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self, num_channels, audio_dim, num_classes):
super(Model, self).__init__()
# Encoder size is the flattened features from the ultrasound encoder.
# we normally estimate this based on input dimensions, number of
# channels, or kernel size. Since this is a pre-trained model with
# fixed-sized inputs, we hard-code it here for simplicity.
self.encoder_size = 16896
# Audio Encoder
self.audio_fc1 = nn.Linear(audio_dim, 256, bias=True)
# Ultrasound Encoder
self.conv1 = nn.Conv2d(in_channels=num_channels, out_channels=32, kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5)
self.batch_norm = nn.BatchNorm1d(self.encoder_size+256)
# phone classifier
self.fc1 = nn.Linear(self.encoder_size+256, 256, bias=True)
self.fc2 = nn.Linear(256, 128, bias=True)
self.fc3 = nn.Linear(128, num_classes, bias=True)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, ultra, audio):
''' forward pass '''
u = ultra
a = audio
# encode audio
a = F.relu( self.audio_fc1(a) )
# encode ultrasound
u = F.max_pool2d(F.relu(self.conv1(u)), kernel_size=(2, 2))
u = F.max_pool2d(F.relu(self.conv2(u)), kernel_size=(2, 2))
u = u.view(-1, self.encoder_size) # flatten
# join features and normalise
x = torch.cat([u, a], dim=1)
x = self.batch_norm(x)
# phone classifier
x = F.relu( self.fc1(x) )
x = F.relu( self.fc2(x) )
x = self.fc3(x)
x = self.softmax(x)
return x