-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodels.py
More file actions
executable file
·84 lines (76 loc) · 3.15 KB
/
models.py
File metadata and controls
executable file
·84 lines (76 loc) · 3.15 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
"""
Different neural network architectures for detecting the line
"""
from __future__ import print_function, division, absolute_import
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
from .constants import INPUT_HEIGHT, INPUT_WIDTH
import math
class ConvolutionalNetwork(nn.Module):
def __init__(self, num_channel=6, drop_p=0.25, num_output=2):
super(ConvolutionalNetwork, self).__init__()
self.conv_layers = nn.Sequential(
nn.Conv2d(num_channel, 8, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(8),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
nn.Conv2d(8, 16, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding = 1),
)
self.fc1 = nn.Linear(15088, 128)
self.fc2 = nn.Linear(128, num_output)
self.dropout1 = nn.Dropout(p=drop_p)
def forward(self, x):
x = self.conv_layers(x)
x = x.view(x.size(0), -1)
x = self.dropout1(x)
x = F.relu(self.fc1(x))
x = self.dropout1(x)
x = self.fc2(x)
return x
class CNN_LSTM(nn.Module):
def __init__(self, num_channel=3, drop_p=0.25, input_size=1600, hidden_size=128, num_layers=2, num_output=2):
super().__init__()
self.cnn = nn.Sequential(
nn.Conv2d(num_channel, 8, kernel_size=5, stride=2, padding=2),
nn.BatchNorm2d(8),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=4, stride=2, padding=1),
nn.Conv2d(8, 16, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=4, stride=2, padding = 1),
nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=4, stride=2, padding = 1)
)
self.drop_p = drop_p
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc1 = nn.Linear(hidden_size, 64)
self.fc2 = nn.Linear(64, num_output)
self.dropout1 = nn.Dropout(p=0.25)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def forward(self, inputs):
features = self.cnn(inputs)
features = features.view(features.size(0), -1)
features = features[None, :, :]
outputs, _ = self.lstm(features)
outputs = self.dropout1(outputs)
outs = [] # save all predictions
for time_step in range(outputs.size(1)): # calculate output for each time step
outs.append(self.fc2(self.dropout1(self.fc1(outputs[:, time_step, :]))))
outs = torch.stack(outs, dim=1)
outs = outs[0]
return outs