| 184 | return result |
| 185 | |
| 186 | class EfficientNet(nn.Module): |
| 187 | def __init__(self, |
| 188 | width_coefficient: float, |
| 189 | depth_coefficient: float, |
| 190 | num_classes: int = 1000, |
| 191 | dropout_rate: float = 0.2, |
| 192 | drop_connect_rate: float = 0.2, |
| 193 | block: Optional[Callable[..., nn.Module]] = None, |
| 194 | norm_layer: Optional[Callable[..., nn.Module]] = None): |
| 195 | super(EfficientNet, self).__init__() |
| 196 | |
| 197 | # kernel_size, in_channel, out_channel, exp_ratio, strides, use_SE, drop_connect_rate, repeats |
| 198 | default_cnf = [[3, 32, 16, 1, 1, True, drop_connect_rate, 1], |
| 199 | [3, 16, 24, 6, 2, True, drop_connect_rate, 2], |
| 200 | [5, 24, 40, 6, 2, True, drop_connect_rate, 2], |
| 201 | [3, 40, 80, 6, 2, True, drop_connect_rate, 3], |
| 202 | [5, 80, 112, 6, 1, True, drop_connect_rate, 3], |
| 203 | [5, 112, 192, 6, 2, True, drop_connect_rate, 4], |
| 204 | [3, 192, 320, 6, 1, True, drop_connect_rate, 1]] |
| 205 | |
| 206 | def round_repeats(repeats): |
| 207 | """Round number of repeats based on depth multiplier.""" |
| 208 | return int(math.ceil(depth_coefficient * repeats)) |
| 209 | |
| 210 | if block is None: |
| 211 | block = InvertedResidual |
| 212 | |
| 213 | if norm_layer is None: |
| 214 | norm_layer = partial(nn.BatchNorm2d, eps=1e-3, momentum=0.1) |
| 215 | |
| 216 | adjust_channels = partial(InvertedResidualConfig.adjust_channels, width_coefficient=width_coefficient) |
| 217 | |
| 218 | # build inverted_rsidual_setting |
| 219 | bneck_conf = partial(InvertedResidualConfig, width_coefficient=width_coefficient) |
| 220 | |
| 221 | b = 0 |
| 222 | num_blocks = float(sum(round_repeats(i[-1]) for i in default_cnf)) |
| 223 | inverted_residual_setting = [] |
| 224 | for stage, args in enumerate(default_cnf): |
| 225 | cnf = copy.copy(args) |
| 226 | for i in range(round_repeats(cnf.pop(-1))): |
| 227 | if i > 0: |
| 228 | # strides equal 1 except first cnf |
| 229 | cnf[-3] = 1 # strides |
| 230 | cnf[1] = cnf[2] # input_channel equal output_channel |
| 231 | cnf[-1] = args[-2] * b / num_blocks # update dropout ratio |
| 232 | index = str(stage + 1) + chr(i + 97) |
| 233 | inverted_residual_setting.append(bneck_conf(*cnf, index)) |
| 234 | # create layers |
| 235 | layers = OrderedDict() |
| 236 | |
| 237 | # first conv |
| 238 | layers.update({"stem_conv": ConvBNActivation(in_planes=3, |
| 239 | out_planes=adjust_channels(32), |
| 240 | kernel_size=3, |
| 241 | stride=2, |
| 242 | norm_layer=norm_layer)}) |
| 243 | # building inverted residual blocks |
no outgoing calls
no test coverage detected