def __init__(self, blocks_args=None, global_params=None): super().__init__() assert isinstance(blocks_args, list), 'blocks_args should be a list' assert len(blocks_args) > 0, 'block args must be greater than 0' self._global_params = global_params self._blocks_args = blocks_args # 获得一种卷积方法,固定了image_size Conv2d = get_same_padding_conv2d(image_size=global_params.image_size) # 获得标准化的参数 bn_mom = 1 - self._global_params.batch_norm_momentum bn_eps = self._global_params.batch_norm_epsilon # 网络主干部分开始 # 设定输入进来的是RGB三通道图像 in_channels = 3 # 利用round_filters可以使得通道数在扩张的时候可以被8整除 out_channels = round_filters(32, self._global_params) # 卷积+标准化 self._conv_stem = Conv2d(in_channels, out_channels, kernel_size=3, stride=2, bias=False) self._bn0 = nn.BatchNorm2d(num_features=out_channels, momentum=bn_mom, eps=bn_eps) # 对每个block的参数进行修改 self._blocks = nn.ModuleList([]) for i in range(len(self._blocks_args)): # 对每个block的参数进行修改,根据所选的efficient版本进行修改 self._blocks_args[i] = self._blocks_args[i]._replace( input_filters=round_filters(self._blocks_args[i].input_filters, self._global_params), output_filters=round_filters(self._blocks_args[i].output_filters, self._global_params), num_repeat=round_repeats(self._blocks_args[i].num_repeat, self._global_params) ) # 第一次大的Block里面的卷积需要考虑步长和输入进来的通道数! self._blocks.append(MBConvBlock(self._blocks_args[i], self._global_params)) if self._blocks_args[i].num_repeat > 1: self._blocks_args[i] = self._blocks_args[i]._replace(input_filters=self._blocks_args[i].output_filters, stride=1) for _ in range(self._blocks_args[i].num_repeat - 1): self._blocks.append(MBConvBlock(self._blocks_args[i], self._global_params)) # 增加了head部分 in_channels = self._blocks_args[len(self._blocks_args) - 1].output_filters out_channels = round_filters(1280, self._global_params) # 32倍降的输出通道 # 卷积+标准化 self._conv_head = Conv2d(in_channels, out_channels, kernel_size=1, bias=False) self._bn1 = nn.BatchNorm2d(num_features=out_channels, momentum=bn_mom, eps=bn_eps) # 最后的全连接层 self._avg_pooling = nn.AdaptiveAvgPool2d(1) self._dropout = nn.Dropout(self._global_params.dropout_rate) self._fc = nn.Linear(out_channels, self._global_params.num_classes) # 进行swish激活函数 self._swish = MemoryEfficientSwish()
def from_pretrained(cls, model_name, load_weights=True, advprop=True, num_classes=1000, in_channels=3): model = cls.from_name(model_name, override_params={'num_classes': num_classes}) if load_weights: load_pretrained_weights(model, model_name, load_fc=(num_classes == 1000), advprop=advprop) if in_channels != 3: Conv2d = get_same_padding_conv2d(image_size = model._global_params.image_size) out_channels = round_filters(32, model._global_params) model._conv_stem = Conv2d(in_channels, out_channels, kernel_size=3, stride=2, bias=False) return model
def __init__(self, blocks_args=None, global_params=None): super().__init__() assert isinstance(blocks_args, list), 'blocks_args should be a list' assert len(blocks_args) > 0, 'block args must be greater than 0' self._global_params = global_params self._blocks_args = blocks_args # 获得一种卷积方法 Conv2d = get_same_padding_conv2d(image_size=global_params.image_size) # 获得标准化的参数 bn_mom = 1 - self._global_params.batch_norm_momentum bn_eps = self._global_params.batch_norm_epsilon #-------------------------------------------------# # 网络主干部分开始 # 设定输入进来的是RGB三通道图像 # 利用round_filters可以使得通道可以被8整除 #-------------------------------------------------# in_channels = 3 out_channels = round_filters(32, self._global_params) #-------------------------------------------------# # 创建stem部分 #-------------------------------------------------# self._conv_stem = Conv2d( in_channels, out_channels, kernel_size=3, stride=2, bias=False) self._bn0 = nn.BatchNorm2d( num_features=out_channels, momentum=bn_mom, eps=bn_eps) #-------------------------------------------------# # 在这个地方对大结构块进行循环 #-------------------------------------------------# self._blocks = nn.ModuleList([]) for i in range(len(self._blocks_args)): #-------------------------------------------------------------# # 对每个block的参数进行修改,根据所选的efficient版本进行修改 #-------------------------------------------------------------# self._blocks_args[i] = self._blocks_args[i]._replace( input_filters=round_filters(self._blocks_args[i].input_filters, self._global_params), output_filters=round_filters(self._blocks_args[i].output_filters, self._global_params), num_repeat=round_repeats(self._blocks_args[i].num_repeat, self._global_params) ) #-------------------------------------------------------------# # 每个大结构块里面的第一个EfficientBlock # 都需要考虑步长和输入通道数 #-------------------------------------------------------------# self._blocks.append(MBConvBlock(self._blocks_args[i], self._global_params)) if self._blocks_args[i].num_repeat > 1: self._blocks_args[i] = self._blocks_args[i]._replace(input_filters=self._blocks_args[i].output_filters, stride=1) #---------------------------------------------------------------# # 在利用第一个EfficientBlock进行通道数的调整或者高和宽的压缩后 # 进行EfficientBlock的堆叠 #---------------------------------------------------------------# for _ in range(self._blocks_args[i].num_repeat - 1): self._blocks.append(MBConvBlock(self._blocks_args[i], self._global_params)) #----------------------------------------------------------------# # 这是efficientnet的尾部部分,在进行effcientdet构建的时候没用到 # 只在利用efficientnet进行分类的时候用到。 #----------------------------------------------------------------# in_channels = self._blocks_args[len(self._blocks_args)-1].output_filters out_channels = round_filters(1280, self._global_params) self._conv_head = Conv2d(in_channels, out_channels, kernel_size=1, bias=False) self._bn1 = nn.BatchNorm2d(num_features=out_channels, momentum=bn_mom, eps=bn_eps) self._avg_pooling = nn.AdaptiveAvgPool2d(1) self._dropout = nn.Dropout(self._global_params.dropout_rate) self._fc = nn.Linear(out_channels, self._global_params.num_classes) self._swish = MemoryEfficientSwish()