-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathYeNet.py
More file actions
126 lines (114 loc) · 4.17 KB
/
YeNet.py
File metadata and controls
126 lines (114 loc) · 4.17 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
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
import os
import sys
import numpy as np
import torch
import torch.nn as nn
from torch.nn.parameter import Parameter
import torch.nn.functional as F
SRM_npy = np.load('SRM_Kernels.npy')
class SRM_conv2d(nn.Module):
def __init__(self, stride=1, padding=0):
super(SRM_conv2d, self).__init__()
self.in_channels = 1
self.out_channels = 30
self.kernel_size = (5, 5)
if isinstance(stride, int):
self.stride = (stride, stride)
else:
self.stride = stride
if isinstance(padding, int):
self.padding = (padding, padding)
else:
self.padding = padding
self.dilation = (1,1)
self.transpose = False
self.output_padding = (0,)
self.groups = 1
self.weight = Parameter(torch.Tensor(30, 1, 5, 5), \
requires_grad=True)
self.bias = Parameter(torch.Tensor(30), \
requires_grad=True)
self.reset_parameters()
def reset_parameters(self):
self.weight.data.numpy()[:] = SRM_npy
self.bias.data.zero_()
def forward(self, input):
return F.conv2d(input, self.weight, self.bias, \
self.stride, self.padding, self.dilation, \
self.groups)
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, \
stride=1, with_bn=False):
super(ConvBlock, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, \
stride)
self.relu = nn.ReLU()
self.with_bn = with_bn
if with_bn:
self.norm = nn.BatchNorm2d(out_channels)
else:
self.norm = lambda x: x
self.reset_parameters()
def forward(self, x):
return self.norm(self.relu(self.conv(x)))
def reset_parameters(self):
nn.init.xavier_uniform(self.conv.weight)
self.conv.bias.data.fill_(0.2)
if self.with_bn:
self.norm.reset_parameters()
class YeNet(nn.Module):
def __init__(self, with_bn=False, threshold=3):
super(YeNet, self).__init__()
self.with_bn = with_bn
self.preprocessing = SRM_conv2d(1, 0)
self.TLU = nn.Hardtanh(-threshold, threshold, True)
if with_bn:
self.norm1 = nn.BatchNorm2d(30)
else:
self.norm1 = lambda x: x
self.block2 = ConvBlock(30, 30, 3, with_bn=self.with_bn)
self.block3 = ConvBlock(30, 30, 3, with_bn=self.with_bn)
self.block4 = ConvBlock(30, 30, 3, with_bn=self.with_bn)
self.pool1 = nn.AvgPool2d(2, 2)
self.block5 = ConvBlock(30, 32, 5, with_bn=self.with_bn)
self.pool2 = nn.AvgPool2d(3, 2)
self.block6 = ConvBlock(32, 32, 5, with_bn=self.with_bn)
self.pool3 = nn.AvgPool2d(3, 2)
self.block7 = ConvBlock(32, 32, 5, with_bn=self.with_bn)
self.pool4 = nn.AvgPool2d(3, 2)
self.block8 = ConvBlock(32, 16, 3, with_bn=self.with_bn)
self.block9 = ConvBlock(16, 16, 3, 3, with_bn=self.with_bn)
self.ip1 = nn.Linear(3 * 3 * 16, 2)
self.reset_parameters()
def forward(self, x):
x = x.float()
x = self.preprocessing(x)
x = self.TLU(x)
x = self.norm1(x)
x = self.block2(x)
x = self.block3(x)
x = self.block4(x)
x = self.pool1(x)
x = self.block5(x)
x = self.pool2(x)
x = self.block6(x)
x = self.pool3(x)
x = self.block7(x)
x = self.pool4(x)
x = self.block8(x)
x = self.block9(x)
x = x.view(x.size(0), -1)
x = self.ip1(x)
return x
def reset_parameters(self):
for mod in self.modules():
if isinstance(mod, SRM_conv2d) or \
isinstance(mod, nn.BatchNorm2d) or \
isinstance(mod, ConvBlock):
mod.reset_parameters()
elif isinstance(mod, nn.Linear):
nn.init.normal(mod.weight, 0. ,0.01)
mod.bias.data.zero_()
def accuracy(outputs, labels):
_, argmax = torch.max(outputs, 1)
return (labels == argmax.squeeze()).float().mean()