-
Notifications
You must be signed in to change notification settings - Fork 4
/
resnet12.py
70 lines (65 loc) · 2.9 KB
/
resnet12.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
from utils import *
from args import *
import torch
import torch.nn as nn
import torch.nn.functional as F
class BasicBlockRN12(nn.Module):
def __init__(self, in_planes, planes):
super(BasicBlockRN12, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes, kernel_size=3, padding=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes)
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, planes, kernel_size=1, bias=False),
nn.BatchNorm2d(planes)
)
def forward(self, x):
out = F.leaky_relu(self.bn1(self.conv1(x)), negative_slope = 0.1)
out = F.leaky_relu(self.bn2(self.conv2(out)), negative_slope = 0.1)
out = self.bn3(self.conv3(out))
out += self.shortcut(x)
if args.dropout > 0:
out = F.dropout(out, p=args.dropout, training=self.training, inplace=True)
return out
class ResNet12(nn.Module):
def __init__(self, feature_maps, input_shape, num_classes, few_shot, rotations):
super(ResNet12, self).__init__()
layers = []
layers.append(BasicBlockRN12(input_shape[0], feature_maps))
layers.append(BasicBlockRN12(feature_maps, int(2.5 * feature_maps)))
layers.append(BasicBlockRN12(int(2.5 * feature_maps), 5 * feature_maps))
layers.append(BasicBlockRN12(5 * feature_maps, 10 * feature_maps))
self.layers = nn.Sequential(*layers)
self.linear = linear(10 * feature_maps, num_classes)
self.rotations = rotations
self.linear_rot = linear(10 * feature_maps, 4)
self.mp = nn.MaxPool2d((2,2))
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='leaky_relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x, index_mixup = None, lam = -1):
if lam != -1:
mixup_layer = random.randint(0, 3)
else:
mixup_layer = -1
out = x
if mixup_layer == 0:
out = lam * out + (1 - lam) * out[index_mixup]
for i in range(len(self.layers)):
out = self.layers[i](out)
if mixup_layer == i + 1:
out = lam * out + (1 - lam) * out[index_mixup]
out = self.mp(F.leaky_relu(out, negative_slope = 0.1))
out = F.avg_pool2d(out, out.shape[2])
features = out.view(out.size(0), -1)
out = self.linear(features)
if self.rotations:
out_rot = self.linear_rot(features)
return (out, out_rot), features
return out, features