def DarknetConv2D_BN_Leaky(*args, **kwargs): """Darknet Convolution2D followed by BatchNormalization and LeakyReLU.""" darknet_conv_kwargs = {'use_bias': False,'normalization':BatchNorm2d(momentum=0.03,eps=1e-4)} darknet_conv_kwargs['activation']=LeakyRelu(alpha=0.1) darknet_conv_kwargs['auto_pad'] = False if kwargs.get('strides')==(2,2) else True darknet_conv_kwargs.update(kwargs) return Conv2d_Block(*args, **darknet_conv_kwargs)
def BottleNeck_IR_SE(num_filters, strides, keep_filter=True): blocks = OrderedDict() blocks['res_layer'] = Sequential(BatchNorm2d(), Conv2d_Block( (3, 3), num_filters=num_filters, strides=1, auto_pad=True, use_bias=False, activation=PRelu(num_filters)), Conv2d_Block((3, 3), num_filters, strides=strides, use_bias=False, activation=None, normalization='batch'), SqueezeExcite(num_filters // 16, num_filters), name='res_layer') if keep_filter: blocks['shortcut_layer'] = MaxPool2d(1, strides=strides, name='shortcut_layer') else: blocks['shortcut_layer'] = Conv2d_Block((1, 1), num_filters, strides=strides, use_bias=False, activation=None, normalization='batch', name='shortcut_layer') return ShortCut2d(blocks, mode='add')
def DenseLayer(growth_rate, name=''): """ The basic normalization, convolution and activation combination for dense connection Args: growth_rate (int):The growth rate regulates how much new information each layer contributes to the global state name (str): None of this dense layer Returns: An instrance of dense layer. """ items = OrderedDict() items['norm'] = BatchNorm2d() items['relu'] = Relu() items['conv1'] = Conv2d_Block((1, 1), 4 * growth_rate, strides=1, activation='relu', auto_pad=True, padding_mode='zero', use_bias=False, normalization='batch') items['conv2'] = Conv2d((3, 3), growth_rate, strides=1, auto_pad=True, padding_mode='zero', use_bias=False) return Sequential(items)
def DenseNet(blocks, growth_rate=32, initial_filters=64, include_top=True, pretrained=True, input_shape=(3,224,224), num_classes=1000, name='', **kwargs): """' Instantiates the DenseNet architecture. Optionally loads weights pre-trained on ImageNet. Args blocks (tuple/ list of int ): numbers of building blocks for the dense layers. growth_rate (int):The growth rate regulates how much new information each layer contributes to the global state initial_filters (int): the channel of the first convolution layer pretrained (bool): If True, returns a model pre-trained on ImageNet. input_shape (tuple or list): the default input image size in CHW order (C, H, W) num_classes (int): number of classes name (string): anme of the model Returns A trident image classification model instance. """ densenet=Sequential() densenet.add_module('conv1/conv',Conv2d_Block((7,7),initial_filters,strides=2,use_bias=False,auto_pad=True,padding_mode='zero',activation='relu',normalization='batch', name='conv1/conv')) densenet.add_module('maxpool', (MaxPool2d((3, 3), strides=2, auto_pad=True, padding_mode='zero'))) densenet.add_module('denseblock1', DenseBlock(blocks[0],growth_rate=growth_rate)) densenet.add_module('transitiondown1', Transition(0.5)) densenet.add_module('denseblock2', DenseBlock(blocks[1], growth_rate=growth_rate)) densenet.add_module('transitiondown2', Transition(0.5)) densenet.add_module('denseblock3', DenseBlock(blocks[2], growth_rate=growth_rate)) densenet.add_module('transitiondown3', Transition(0.5)) densenet.add_module('denseblock4', DenseBlock(blocks[3], growth_rate=growth_rate)) densenet.add_module('classifier_norm',BatchNorm2d(name='classifier_norm')) densenet.add_module('classifier_relu', Relu(name='classifier_relu')) if include_top: densenet.add_module('avg_pool', GlobalAvgPool2d(name='avg_pool')) densenet.add_module('classifier', Dense(num_classes, activation=None, name='classifier')) densenet.add_module('softmax', SoftMax( name='softmax')) densenet.name = name model=ImageClassificationModel(input_shape=input_shape,output=densenet) #model.model.to(_device) with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'imagenet_labels1.txt'), 'r',encoding='utf-8-sig') as f: labels = [l.rstrip() for l in f] model.class_names = labels model.preprocess_flow = [resize((input_shape[2], input_shape[1]), keep_aspect=True), normalize(0, 255), normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])] # model.summary() return model
def Transition(reduction,name=''): """ The block for transition-down, down-sampling by average pooling Args: reduction (float): The depth_multiplier to transition-down the dense features name (str): Name of the transition-down process Returns: An instrance of transition-down . """ items=OrderedDict() items['norm']=BatchNorm2d() items['relu']=Relu() items['conv1']=Conv2d((1, 1),num_filters=None, depth_multiplier=reduction, strides=1, auto_pad=True,padding_mode='zero',use_bias=False) items['pool']=AvgPool2d(2,2,auto_pad=True) return Sequential(items,name=name)
def SEResNet_IR(include_top=True,num_layers=50,Bottleneck=BottleNeck_IR_SE,drop_ratio=0.4,feature_dim=128,input_shape=(3,112,112)): blocks=OrderedDict() blocks['input_layer']=Conv2d_Block((3,3),64,strides=1,auto_pad=True,use_bias=False,activation=PRelu(64),normalization='batch',name='input_layer') blocks['body']=Sequential( get_block(Bottleneck, out_channel=64, num_units=3,keep_filter=True)+ get_block(Bottleneck, out_channel=128, num_units=4,keep_filter=False)+ get_block(Bottleneck, out_channel=256, num_units=14,keep_filter=False)+ get_block(Bottleneck, out_channel=512, num_units=3,keep_filter=False) ) blocks['output_layer']=Sequential( BatchNorm2d(), Dropout(drop_ratio), Flatten(), Dense(feature_dim), BatchNorm(), name='output_layer' ) facenet=Sequential(blocks).to(_device) facenet.name=camel2snake('SEResNet_IR') model=FaceRecognitionModel(input_shape=input_shape,output=facenet) model.preprocess_flow=[Resize((input_shape[1],input_shape[2]),keep_aspect=True),Normalize(0,255),Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])] #model.summary() return model