|
| 1 | +# Copyright (c) 2022-2023, InterDigital Communications, Inc |
| 2 | +# All rights reserved. |
| 3 | + |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted (subject to the limitations in the disclaimer |
| 6 | +# below) provided that the following conditions are met: |
| 7 | + |
| 8 | +# * Redistributions of source code must retain the above copyright notice, |
| 9 | +# this list of conditions and the following disclaimer. |
| 10 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# * Neither the name of InterDigital Communications, Inc nor the names of its |
| 14 | +# contributors may be used to endorse or promote products derived from this |
| 15 | +# software without specific prior written permission. |
| 16 | + |
| 17 | +# NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
| 18 | +# THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | +# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT |
| 20 | +# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 21 | +# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 25 | +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 27 | +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 28 | +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +import torch.nn as nn |
| 31 | +from jde.models import EmptyLayer, Upsample, YOLOLayer |
| 32 | + |
| 33 | +from .intconv_wrapper import IntConv2dWrapper |
| 34 | + |
| 35 | +try: |
| 36 | + from jde.utils.syncbn import SyncBN |
| 37 | + |
| 38 | + batch_norm = SyncBN # nn.BatchNorm2d |
| 39 | +except ImportError: |
| 40 | + batch_norm = nn.BatchNorm2d |
| 41 | + |
| 42 | + |
| 43 | +def create_modules(module_defs, device: str): |
| 44 | + """ |
| 45 | + Constructs module list of layer blocks from module configuration in module_defs |
| 46 | + """ |
| 47 | + hyperparams = module_defs.pop(0) |
| 48 | + output_filters = [int(hyperparams["channels"])] |
| 49 | + module_list = nn.ModuleList() |
| 50 | + yolo_layer_count = 0 |
| 51 | + for i, module_def in enumerate(module_defs): |
| 52 | + modules = nn.Sequential() |
| 53 | + |
| 54 | + if module_def["type"] == "convolutional": |
| 55 | + bn = int(module_def["batch_normalize"]) |
| 56 | + filters = int(module_def["filters"]) |
| 57 | + kernel_size = int(module_def["size"]) |
| 58 | + pad = (kernel_size - 1) // 2 if int(module_def["pad"]) else 0 |
| 59 | + modules.add_module( |
| 60 | + "conv_%d" % i, |
| 61 | + IntConv2dWrapper( |
| 62 | + in_channels=output_filters[-1], |
| 63 | + out_channels=filters, |
| 64 | + kernel_size=kernel_size, |
| 65 | + stride=int(module_def["stride"]), |
| 66 | + padding=pad, |
| 67 | + bias=not bn, |
| 68 | + ), |
| 69 | + ) |
| 70 | + if bn: |
| 71 | + after_bn = batch_norm(filters) |
| 72 | + modules.add_module("batch_norm_%d" % i, after_bn) |
| 73 | + # BN is uniformly initialized by default in pytorch 1.0.1. |
| 74 | + # In pytorch>1.2.0, BN weights are initialized with constant 1, |
| 75 | + # but we find with the uniform initialization the model converges faster. |
| 76 | + nn.init.uniform_(after_bn.weight) |
| 77 | + nn.init.zeros_(after_bn.bias) |
| 78 | + if module_def["activation"] == "leaky": |
| 79 | + modules.add_module("leaky_%d" % i, nn.LeakyReLU(0.1)) |
| 80 | + |
| 81 | + elif module_def["type"] == "maxpool": |
| 82 | + kernel_size = int(module_def["size"]) |
| 83 | + stride = int(module_def["stride"]) |
| 84 | + if kernel_size == 2 and stride == 1: |
| 85 | + modules.add_module("_debug_padding_%d" % i, nn.ZeroPad2d((0, 1, 0, 1))) |
| 86 | + maxpool = nn.MaxPool2d( |
| 87 | + kernel_size=kernel_size, |
| 88 | + stride=stride, |
| 89 | + padding=int((kernel_size - 1) // 2), |
| 90 | + ) |
| 91 | + modules.add_module("maxpool_%d" % i, maxpool) |
| 92 | + |
| 93 | + elif module_def["type"] == "upsample": |
| 94 | + upsample = Upsample(scale_factor=int(module_def["stride"])) |
| 95 | + modules.add_module("upsample_%d" % i, upsample) |
| 96 | + |
| 97 | + elif module_def["type"] == "route": |
| 98 | + layers = [int(x) for x in module_def["layers"].split(",")] |
| 99 | + filters = sum([output_filters[i + 1 if i > 0 else i] for i in layers]) |
| 100 | + modules.add_module("route_%d" % i, EmptyLayer()) |
| 101 | + |
| 102 | + elif module_def["type"] == "shortcut": |
| 103 | + filters = output_filters[int(module_def["from"])] |
| 104 | + modules.add_module("shortcut_%d" % i, EmptyLayer()) |
| 105 | + |
| 106 | + elif module_def["type"] == "yolo": |
| 107 | + anchor_idxs = [int(x) for x in module_def["mask"].split(",")] |
| 108 | + # Extract anchors |
| 109 | + anchors = [float(x) for x in module_def["anchors"].split(",")] |
| 110 | + anchors = [(anchors[i], anchors[i + 1]) for i in range(0, len(anchors), 2)] |
| 111 | + anchors = [anchors[i] for i in anchor_idxs] |
| 112 | + nC = int(module_def["classes"]) # number of classes |
| 113 | + img_size = (int(hyperparams["width"]), int(hyperparams["height"])) |
| 114 | + # Define detection layer |
| 115 | + yolo_layer = YOLOLayer( |
| 116 | + anchors, |
| 117 | + nC, |
| 118 | + int(hyperparams["nID"]), |
| 119 | + int(hyperparams["embedding_dim"]), |
| 120 | + img_size, |
| 121 | + yolo_layer_count, |
| 122 | + device, |
| 123 | + ) |
| 124 | + modules.add_module("yolo_%d" % i, yolo_layer) |
| 125 | + yolo_layer_count += 1 |
| 126 | + |
| 127 | + # Register module list and number of output filters |
| 128 | + module_list.append(modules) |
| 129 | + output_filters.append(filters) |
| 130 | + |
| 131 | + return hyperparams, module_list |
0 commit comments