|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | + |
| 4 | +from model.fpn import * |
| 5 | +from model.backbone.shufflenetv2 import * |
| 6 | + |
| 7 | +class Detector(nn.Module): |
| 8 | + def __init__(self, classes, anchor_num, load_param): |
| 9 | + super(Detector, self).__init__() |
| 10 | + out_depth = 72 |
| 11 | + stage_out_channels = [-1, 24, 48, 96, 192] |
| 12 | + |
| 13 | + self.backbone = ShuffleNetV2(stage_out_channels, load_param) |
| 14 | + self.fpn = LightFPN(stage_out_channels[-2] + stage_out_channels[-1], stage_out_channels[-1], out_depth) |
| 15 | + |
| 16 | + self.output_reg_layers = nn.Conv2d(out_depth, 4 * anchor_num, 1, 1, 0, bias=True) |
| 17 | + self.output_obj_layers = nn.Conv2d(out_depth, anchor_num, 1, 1, 0, bias=True) |
| 18 | + self.output_cls_layers = nn.Conv2d(out_depth, classes, 1, 1, 0, bias=True) |
| 19 | + |
| 20 | + def forward(self, x): |
| 21 | + C2, C3 = self.backbone(x) |
| 22 | + cls_2, obj_2, reg_2, cls_3, obj_3, reg_3 = self.fpn(C2, C3) |
| 23 | + |
| 24 | + out_reg_2 = self.output_reg_layers(reg_2) |
| 25 | + out_obj_2 = self.output_obj_layers(obj_2) |
| 26 | + out_cls_2 = self.output_cls_layers(cls_2) |
| 27 | + |
| 28 | + out_reg_3 = self.output_reg_layers(reg_3) |
| 29 | + out_obj_3 = self.output_obj_layers(obj_3) |
| 30 | + out_cls_3 = self.output_cls_layers(cls_3) |
| 31 | + if not torch.onnx.is_in_onnx_export(): |
| 32 | + return out_reg_2, out_obj_2, out_cls_2, out_reg_3, out_obj_3, out_cls_3 |
| 33 | + else: |
| 34 | + # for out in (out_reg_2, out_obj_2, out_cls_2, out_reg_3, out_obj_3, out_cls_3): |
| 35 | + # print(out.shape) |
| 36 | + |
| 37 | + c = out_reg_2.shape[1] |
| 38 | + out_reg_2 = out_reg_2.permute(0, 2, 3, 1).view(-1, c) |
| 39 | + c = out_obj_2.shape[1] |
| 40 | + out_obj_2 = out_obj_2.permute(0, 2, 3, 1).view(-1, c) |
| 41 | + c = out_cls_2.shape[1] |
| 42 | + out_cls_2 = out_cls_2.permute(0, 2, 3, 1).view(-1, c) |
| 43 | + out_reg_2 = torch.sigmoid(out_reg_2) |
| 44 | + out_obj_2 = torch.sigmoid(out_obj_2) |
| 45 | + out_cls_2 = F.softmax(out_cls_2, dim=1) |
| 46 | + out2 = torch.cat((out_reg_2, out_obj_2, out_cls_2), dim=1) |
| 47 | + |
| 48 | + c = out_reg_3.shape[1] |
| 49 | + out_reg_3 = out_reg_3.permute(0, 2, 3, 1).view(-1, c) |
| 50 | + c = out_obj_3.shape[1] |
| 51 | + out_obj_3 = out_obj_3.permute(0, 2, 3, 1).view(-1, c) |
| 52 | + c = out_cls_3.shape[1] |
| 53 | + out_cls_3 = out_cls_3.permute(0, 2, 3, 1).view(-1, c) |
| 54 | + out_reg_3 = torch.sigmoid(out_reg_3) |
| 55 | + out_obj_3 = torch.sigmoid(out_obj_3) |
| 56 | + out_cls_3 = F.softmax(out_cls_3, dim=1) |
| 57 | + out3 = torch.cat((out_reg_3, out_obj_3, out_cls_3), dim=1) |
| 58 | + return torch.cat((out2, out3), dim=0) |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + model = Detector(80, 3, False) |
| 62 | + test_data = torch.rand(1, 3, 352, 352) |
| 63 | + torch.onnx.export(model, #model being run |
| 64 | + test_data, # model input (or a tuple for multiple inputs) |
| 65 | + "test.onnx", # where to save the model (can be a file or file-like object) |
| 66 | + export_params=True, # store the trained parameter weights inside the model file |
| 67 | + opset_version=11, # the ONNX version to export the model to |
| 68 | + do_constant_folding=True) # whether to execute constant folding for optimization |
| 69 | + |
| 70 | + |
| 71 | + |
0 commit comments