(module_defs, img_size, cfg)
| 7 | |
| 8 | |
| 9 | def create_modules(module_defs, img_size, cfg): |
| 10 | # Constructs module list of layer blocks from module configuration in module_defs |
| 11 | |
| 12 | img_size = [img_size] * 2 if isinstance(img_size, int) else img_size # expand if necessary |
| 13 | _ = module_defs.pop(0) # cfg training hyperparams (unused) |
| 14 | output_filters = [3] # input channels |
| 15 | module_list = nn.ModuleList() |
| 16 | routs = [] # list of layers which rout to deeper layers |
| 17 | yolo_index = -1 |
| 18 | |
| 19 | for i, mdef in enumerate(module_defs): |
| 20 | modules = nn.Sequential() |
| 21 | |
| 22 | if mdef['type'] == 'convolutional': |
| 23 | bn = mdef['batch_normalize'] |
| 24 | filters = mdef['filters'] |
| 25 | k = mdef['size'] # kernel size |
| 26 | stride = mdef['stride'] if 'stride' in mdef else (mdef['stride_y'], mdef['stride_x']) |
| 27 | if isinstance(k, int): # single-size conv |
| 28 | modules.add_module('Conv2d', nn.Conv2d(in_channels=output_filters[-1], |
| 29 | out_channels=filters, |
| 30 | kernel_size=k, |
| 31 | stride=stride, |
| 32 | padding=k // 2 if mdef['pad'] else 0, |
| 33 | groups=mdef['groups'] if 'groups' in mdef else 1, |
| 34 | bias=not bn)) |
| 35 | else: # multiple-size conv |
| 36 | modules.add_module('MixConv2d', MixConv2d(in_ch=output_filters[-1], |
| 37 | out_ch=filters, |
| 38 | k=k, |
| 39 | stride=stride, |
| 40 | bias=not bn)) |
| 41 | |
| 42 | if bn: |
| 43 | modules.add_module('BatchNorm2d', nn.BatchNorm2d(filters, momentum=0.03, eps=1E-4)) |
| 44 | else: |
| 45 | routs.append(i) # detection output (goes into yolo layer) |
| 46 | |
| 47 | if mdef['activation'] == 'leaky': # activation study https://github.com/ultralytics/yolov3/issues/441 |
| 48 | modules.add_module('activation', nn.LeakyReLU(0.1, inplace=True)) |
| 49 | elif mdef['activation'] == 'swish': |
| 50 | modules.add_module('activation', Swish()) |
| 51 | elif mdef['activation'] == 'mish': |
| 52 | modules.add_module('activation', Mish()) |
| 53 | elif mdef['activation'] == 'emb': |
| 54 | modules.add_module('activation', F.normalize()) |
| 55 | elif mdef['activation'] == 'logistic': |
| 56 | modules.add_module('activation', nn.Sigmoid()) |
| 57 | elif mdef['activation'] == 'silu': |
| 58 | modules.add_module('activation', nn.SiLU()) |
| 59 | |
| 60 | elif mdef['type'] == 'deformableconvolutional': |
| 61 | bn = mdef['batch_normalize'] |
| 62 | filters = mdef['filters'] |
| 63 | k = mdef['size'] # kernel size |
| 64 | stride = mdef['stride'] if 'stride' in mdef else (mdef['stride_y'], mdef['stride_x']) |
| 65 | if isinstance(k, int): # single-size conv |
| 66 | modules.add_module('DeformConv2d', DeformConv2d(output_filters[-1], |
no test coverage detected