|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +from torch.autograd import Variable |
| 4 | +import torch.functional as F |
| 5 | +import numpy as np |
| 6 | +import torch.optim as optim |
| 7 | +import math |
| 8 | +import sys |
| 9 | + |
| 10 | +class fire(nn.Module): |
| 11 | + def __init__(self, inplanes, squeeze_planes, expand_planes): |
| 12 | + super(fire, self).__init__() |
| 13 | + self.conv1 = nn.Conv2d(inplanes, squeeze_planes, kernel_size=1, stride=1) |
| 14 | + self.bn1 = nn.BatchNorm2d(squeeze_planes) |
| 15 | + self.relu1 = nn.ReLU(inplace=True) |
| 16 | + self.conv2 = nn.Conv2d(squeeze_planes, expand_planes, kernel_size=1, stride=1) |
| 17 | + self.bn2 = nn.BatchNorm2d(expand_planes) |
| 18 | + self.conv3 = nn.Conv2d(squeeze_planes, expand_planes, kernel_size=3, stride=1, padding=1) |
| 19 | + self.bn3 = nn.BatchNorm2d(expand_planes) |
| 20 | + self.relu2 = nn.ReLU() |
| 21 | + |
| 22 | + # using MSR initilization |
| 23 | + for m in self.modules(): |
| 24 | + if isinstance(m, nn.Conv2d): |
| 25 | + n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels |
| 26 | + m.weight.data.normal_(0, math.sqrt(2./n)) |
| 27 | + |
| 28 | + def forward(self, x): |
| 29 | + x = self.conv1(x) |
| 30 | + x = self.bn1(x) |
| 31 | + x = self.relu1(x) |
| 32 | + out1 = self.conv2(x) |
| 33 | + out1 = self.bn2(out1) |
| 34 | + out2 = self.conv3(x) |
| 35 | + out2 = self.bn3(out2) |
| 36 | + out = torch.cat([out1, out2], 1) |
| 37 | + out = self.relu2(out) |
| 38 | + return out |
| 39 | + |
| 40 | +class additional_layer(nn.Module): |
| 41 | + def __init__(self, add_input_channels, add_output_channels): |
| 42 | + super(additional_layer, self).__init__() |
| 43 | + self.conv1 = nn.Conv2d(add_input_channels, add_output_channels, kernel_size=3, stride=1,padding=1) |
| 44 | + self.bn1 = nn.BatchNorm2d(add_output_channels) |
| 45 | + self.relu1 = nn.ReLU(inplace=True) |
| 46 | + |
| 47 | + # using MSR initilization |
| 48 | + for m in self.modules(): |
| 49 | + if isinstance(m, nn.Conv2d): |
| 50 | + n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels |
| 51 | + m.weight.data.normal_(0, math.sqrt(2./n)) |
| 52 | + |
| 53 | + def forward(self, x): |
| 54 | + out_add = self.conv1(x) |
| 55 | + out_add = self.bn1(x) |
| 56 | + out_add = self.relu1(x) |
| 57 | + return out_add |
| 58 | + |
| 59 | + |
| 60 | +class conv_layer(nn.Module): |
| 61 | + def __init__(self, add_input_channels, add_output_channels,kernel_size,stride,padding): |
| 62 | + super(conv_layer, self).__init__() |
| 63 | + self.conv1 = nn.Conv2d(add_input_channels, add_output_channels, kernel_size, stride,padding) |
| 64 | + self.bn1 = nn.BatchNorm2d(add_output_channels) |
| 65 | + self.relu1 = nn.ReLU(inplace=True) |
| 66 | + |
| 67 | + # using MSR initilization |
| 68 | + for m in self.modules(): |
| 69 | + if isinstance(m, nn.Conv2d): |
| 70 | + n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels |
| 71 | + m.weight.data.normal_(0, math.sqrt(2./n)) |
| 72 | + |
| 73 | + def forward(self, x): |
| 74 | + out_add = self.conv1(x) |
| 75 | + out_add = self.bn1(out_add) |
| 76 | + out_add = self.relu1(out_add) |
| 77 | + return out_add |
| 78 | + |
| 79 | + |
| 80 | +class SqueezeNet(nn.Module): |
| 81 | + def __init__(self): |
| 82 | + super(SqueezeNet, self).__init__() |
| 83 | + self.conv1 = nn.Conv2d(3, 96, kernel_size=3, stride=1, padding=1) # 32 |
| 84 | + self.bn1 = nn.BatchNorm2d(96) |
| 85 | + self.relu = nn.ReLU() |
| 86 | + self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2) # 16 |
| 87 | + self.conv3x3_add_link_1 = conv_layer(96,96,kernel_size=3, stride=2, padding=1) |
| 88 | + self.fire2 = fire(96, 16, 64) |
| 89 | + self.dropout_1 = nn.Dropout2d(p=0.5,inplace=False) |
| 90 | + self.fire3 = fire(128, 16, 64) |
| 91 | + self.fire4 = fire(128, 32, 128) |
| 92 | + self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2) # 8 |
| 93 | +# self.conv3x3_add_link_2 = conv_layer(256,256,kernel_size=3, stride=2, padding=1) |
| 94 | + self.conv1x1_concat_1 = conv_layer(352,256,kernel_size=1, stride=1, padding=0) |
| 95 | + self.fire5 = fire(256, 32, 128) |
| 96 | + self.fire6 = fire(256, 48, 192) |
| 97 | + self.fire7 = fire(384, 48, 192) |
| 98 | + self.dropout_2 = nn.Dropout2d(p=0.5,inplace=False) |
| 99 | + self.fire8 = fire(384, 64, 256) |
| 100 | + self.maxpool3 = nn.MaxPool2d(kernel_size=2, stride=2) # 4 |
| 101 | +# self.conv1x1_concat_2 = conv_layer(768,512,kernel_size=1, stride=1, padding=0) |
| 102 | + self.fire9 = fire(512, 64, 256) |
| 103 | + self.conv2 = nn.Conv2d(512, 10, kernel_size=1, stride=1) |
| 104 | + self.avg_pool = nn.AvgPool2d(kernel_size=4, stride=4) |
| 105 | + self.softmax = nn.LogSoftmax(dim=1) |
| 106 | + for m in self.modules(): |
| 107 | + if isinstance(m, nn.Conv2d): |
| 108 | + n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels |
| 109 | + m.weight.data.normal_(0, math.sqrt(2. / n)) |
| 110 | + elif isinstance(m, nn.BatchNorm2d): |
| 111 | + m.weight.data.fill_(1) |
| 112 | + m.bias.data.zero_() |
| 113 | + |
| 114 | + |
| 115 | + def forward(self, x): |
| 116 | +# print("Initital Value:",x.size()) |
| 117 | + x = self.conv1(x) |
| 118 | + x = self.bn1(x) |
| 119 | + x = self.relu(x) |
| 120 | + x = self.maxpool1(x) |
| 121 | + add_link_1 = x |
| 122 | + conved_add_link_1 = self.conv3x3_add_link_1(add_link_1) |
| 123 | +# print("add_link_1: ",add_link_1.size()) |
| 124 | +# print("add_link_convolved",conved_add_link_1.size()) |
| 125 | +# sys.exit() |
| 126 | + x = self.fire2(x) |
| 127 | + x = self.dropout_1(x) |
| 128 | + x = self.fire3(x) |
| 129 | + x = self.fire4(x) |
| 130 | + x = self.maxpool2(x) |
| 131 | + print("X output: ",x.size()) |
| 132 | +# add_link_2 = x |
| 133 | +# conved_add_link_2 = self.conv3x3_add_link_2(add_link_2) |
| 134 | + x = torch.cat([x,conved_add_link_1],1) |
| 135 | + print("X concat output: ",x.size()) |
| 136 | + sys.exit(); |
| 137 | + x = self.conv1x1_concat_1(x) |
| 138 | + x = self.fire5(x) |
| 139 | + x = self.fire6(x) |
| 140 | + x = self.fire7(x) |
| 141 | + x = self.dropout_2(x) |
| 142 | + x = self.fire8(x) |
| 143 | + x = self.maxpool3(x) |
| 144 | +# x = torch.cat([x,conved_add_link_2],1) |
| 145 | +# x = self.conv1x1_concat_2(x) |
| 146 | + x = self.fire9(x) |
| 147 | + x = self.conv2(x) |
| 148 | + x = self.avg_pool(x) |
| 149 | + x = self.softmax(x) |
| 150 | + return x |
| 151 | + |
| 152 | +def fire_layer(inp, s, e): |
| 153 | + f = fire(inp, s, e) |
| 154 | + return f |
| 155 | + |
| 156 | +def squeezenet(pretrained=False): |
| 157 | + net = SqueezeNet() |
| 158 | + # inp = Variable(torch.randn(64,3,32,32)) |
| 159 | + # out = net.forward(inp) |
| 160 | + # print(out.size()) |
| 161 | + return net |
| 162 | + |
| 163 | +# if __name__ == '__main__': |
| 164 | +# squeezenet() |
0 commit comments