|
| 1 | +""" |
| 2 | + Load the vgg16 weight and save it to special file |
| 3 | +""" |
| 4 | + |
| 5 | +#from torchvision.models.vgg import vgg16 |
| 6 | +import torch.nn as nn |
| 7 | +import torch.nn.functional as F |
| 8 | +import torch |
| 9 | +from torch.autograd import Variable |
| 10 | +from collections import OrderedDict |
| 11 | + |
| 12 | +from torchvision.models.resnet import resnet18, resnet34, resnet50 |
| 13 | + |
| 14 | +def _ModifyConvStrideDilation(conv, stride=(1, 1), padding=None): |
| 15 | + conv.stride = stride |
| 16 | + |
| 17 | + if padding is not None: |
| 18 | + conv.padding = padding |
| 19 | + |
| 20 | +def _ModifyBlock(block, bottleneck=False, **kwargs): |
| 21 | + for m in list(block.children()): |
| 22 | + if bottleneck: |
| 23 | + _ModifyConvStrideDilation(m.conv2, **kwargs) |
| 24 | + else: |
| 25 | + _ModifyConvStrideDilation(m.conv1, **kwargs) |
| 26 | + |
| 27 | + if m.downsample is not None: |
| 28 | + # need to make sure no padding for the 1x1 residual connection |
| 29 | + _ModifyConvStrideDilation(list(m.downsample.children())[0], **kwargs) |
| 30 | + |
| 31 | +class ResNet18(nn.Module): |
| 32 | + def __init__(self): |
| 33 | + super().__init__() |
| 34 | + rn18 = resnet18(pretrained=True) |
| 35 | + |
| 36 | + |
| 37 | + # discard last Resnet block, avrpooling and classification FC |
| 38 | + # layer1 = up to and including conv3 block |
| 39 | + self.layer1 = nn.Sequential(*list(rn18.children())[:6]) |
| 40 | + # layer2 = conv4 block only |
| 41 | + self.layer2 = nn.Sequential(*list(rn18.children())[6:7]) |
| 42 | + |
| 43 | + # modify conv4 if necessary |
| 44 | + # Always deal with stride in first block |
| 45 | + modulelist = list(self.layer2.children()) |
| 46 | + _ModifyBlock(modulelist[0], stride=(1,1)) |
| 47 | + |
| 48 | + def forward(self, data): |
| 49 | + layer1_activation = self.layer1(data) |
| 50 | + x = layer1_activation |
| 51 | + layer2_activation = self.layer2(x) |
| 52 | + |
| 53 | + # Only need the output of conv4 |
| 54 | + return [layer2_activation] |
| 55 | + |
| 56 | +class ResNet34(nn.Module): |
| 57 | + def __init__(self): |
| 58 | + super().__init__() |
| 59 | + rn34 = resnet34(pretrained=True) |
| 60 | + |
| 61 | + # discard last Resnet block, avrpooling and classification FC |
| 62 | + self.layer1 = nn.Sequential(*list(rn34.children())[:6]) |
| 63 | + self.layer2 = nn.Sequential(*list(rn34.children())[6:7]) |
| 64 | + # modify conv4 if necessary |
| 65 | + # Always deal with stride in first block |
| 66 | + modulelist = list(self.layer2.children()) |
| 67 | + _ModifyBlock(modulelist[0], stride=(1,1)) |
| 68 | + |
| 69 | + |
| 70 | + def forward(self, data): |
| 71 | + layer1_activation = self.layer1(data) |
| 72 | + x = layer1_activation |
| 73 | + layer2_activation = self.layer2(x) |
| 74 | + |
| 75 | + return [layer2_activation] |
| 76 | + |
| 77 | +class L2Norm(nn.Module): |
| 78 | + """ |
| 79 | + Scale shall be learnable according to original paper |
| 80 | + scale: initial scale number |
| 81 | + chan_num: L2Norm channel number (norm over all channels) |
| 82 | + """ |
| 83 | + def __init__(self, scale=20, chan_num=512): |
| 84 | + super(L2Norm, self).__init__() |
| 85 | + # Scale across channels |
| 86 | + self.scale = \ |
| 87 | + nn.Parameter(torch.Tensor([scale]*chan_num).view(1, chan_num, 1, 1)) |
| 88 | + |
| 89 | + def forward(self, data): |
| 90 | + # normalize accross channel |
| 91 | + return self.scale*data*data.pow(2).sum(dim=1, keepdim=True).clamp(min=1e-12).rsqrt() |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +def tailor_module(src_model, src_dir, tgt_model, tgt_dir): |
| 96 | + state = torch.load(src_dir) |
| 97 | + src_model.load_state_dict(state) |
| 98 | + src_state = src_model.state_dict() |
| 99 | + # only need features |
| 100 | + keys1 = src_state.keys() |
| 101 | + keys1 = [k for k in src_state.keys() if k.startswith("features")] |
| 102 | + keys2 = tgt_model.state_dict().keys() |
| 103 | + |
| 104 | + assert len(keys1) == len(keys2) |
| 105 | + state = OrderedDict() |
| 106 | + |
| 107 | + for k1, k2 in zip(keys1, keys2): |
| 108 | + # print(k1, k2) |
| 109 | + state[k2] = src_state[k1] |
| 110 | + #diff_keys = state.keys() - target_model.state_dict().keys() |
| 111 | + #print("Different Keys:", diff_keys) |
| 112 | + # Remove unecessary keys |
| 113 | + #for k in diff_keys: |
| 114 | + # state.pop(k) |
| 115 | + tgt_model.load_state_dict(state) |
| 116 | + torch.save(tgt_model.state_dict(), tgt_dir) |
| 117 | + |
| 118 | +# Default vgg16 in pytorch seems different from ssd |
| 119 | +def make_layers(cfg, batch_norm=False): |
| 120 | + layers = [] |
| 121 | + in_channels = 3 |
| 122 | + for v in cfg: |
| 123 | + if v == 'M': |
| 124 | + layers += [nn.MaxPool2d(kernel_size=2, stride=2)] |
| 125 | + elif v == 'C': |
| 126 | + # Notice ceil_mode is true |
| 127 | + layers += [nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)] |
| 128 | + else: |
| 129 | + conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1) |
| 130 | + if batch_norm: |
| 131 | + layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)] |
| 132 | + else: |
| 133 | + layers += [conv2d, nn.ReLU(inplace=True)] |
| 134 | + in_channels = v |
| 135 | + return layers |
| 136 | + |
| 137 | +class Loss(nn.Module): |
| 138 | + """ |
| 139 | + Implements the loss as the sum of the followings: |
| 140 | + 1. Confidence Loss: All labels, with hard negative mining |
| 141 | + 2. Localization Loss: Only on positive labels |
| 142 | + Suppose input dboxes has the shape 8732x4 |
| 143 | + """ |
| 144 | + |
| 145 | + def __init__(self, dboxes): |
| 146 | + super(Loss, self).__init__() |
| 147 | + self.scale_xy = 1.0/dboxes.scale_xy |
| 148 | + self.scale_wh = 1.0/dboxes.scale_wh |
| 149 | + |
| 150 | + self.sl1_loss = nn.SmoothL1Loss(reduce=False) |
| 151 | + self.dboxes = nn.Parameter(dboxes(order="xywh").transpose(0, 1).unsqueeze(dim = 0), |
| 152 | + requires_grad=False) |
| 153 | + # Two factor are from following links |
| 154 | + # http://jany.st/post/2017-11-05-single-shot-detector-ssd-from-scratch-in-tensorflow.html |
| 155 | + self.con_loss = nn.CrossEntropyLoss(reduce=False) |
| 156 | + |
| 157 | + def _loc_vec(self, loc): |
| 158 | + """ |
| 159 | + Generate Location Vectors |
| 160 | + """ |
| 161 | + gxy = self.scale_xy*(loc[:, :2, :] - self.dboxes[:, :2, :])/self.dboxes[:, 2:, ] |
| 162 | + gwh = self.scale_wh*(loc[:, 2:, :]/self.dboxes[:, 2:, :]).log() |
| 163 | + |
| 164 | + return torch.cat((gxy, gwh), dim=1).contiguous() |
| 165 | + |
| 166 | + def forward(self, ploc, plabel, gloc, glabel): |
| 167 | + """ |
| 168 | + ploc, plabel: Nx4x8732, Nxlabel_numx8732 |
| 169 | + predicted location and labels |
| 170 | +
|
| 171 | + gloc, glabel: Nx4x8732, Nx8732 |
| 172 | + ground truth location and labels |
| 173 | + """ |
| 174 | + |
| 175 | + mask = glabel > 0 |
| 176 | + pos_num = mask.sum(dim=1) |
| 177 | + |
| 178 | + vec_gd = self._loc_vec(gloc) |
| 179 | + |
| 180 | + # sum on four coordinates, and mask |
| 181 | + sl1 = self.sl1_loss(ploc, vec_gd).sum(dim=1) |
| 182 | + sl1 = (mask.float()*sl1).sum(dim=1) |
| 183 | + |
| 184 | + # hard negative mining |
| 185 | + con = self.con_loss(plabel, glabel) |
| 186 | + |
| 187 | + # postive mask will never selected |
| 188 | + con_neg = con.clone() |
| 189 | + con_neg[mask] = 0 |
| 190 | + _, con_idx = con_neg.sort(dim=1, descending=True) |
| 191 | + _, con_rank = con_idx.sort(dim=1) |
| 192 | + |
| 193 | + # number of negative three times positive |
| 194 | + neg_num = torch.clamp(3*pos_num, max=mask.size(1)).unsqueeze(-1) |
| 195 | + neg_mask = con_rank < neg_num |
| 196 | + |
| 197 | + closs = (con*(mask.float() + neg_mask.float())).sum(dim=1) |
| 198 | + |
| 199 | + # avoid no object detected |
| 200 | + total_loss = sl1 + closs |
| 201 | + num_mask = (pos_num > 0).float() |
| 202 | + pos_num = pos_num.float().clamp(min=1e-6) |
| 203 | + |
| 204 | + ret = (total_loss*num_mask/pos_num).mean(dim=0) |
| 205 | + return ret |
| 206 | + |
0 commit comments