예제 #1
0
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),
                                  num_filters=4 * growth_rate,
                                  strides=1,
                                  activation='relu',
                                  auto_pad=True,
                                  padding_mode='zero',
                                  use_bias=False,
                                  normalization='batch')
    items['conv2'] = Conv2d((3, 3),
                            num_filters=growth_rate,
                            strides=1,
                            auto_pad=True,
                            padding_mode='zero',
                            use_bias=False)
    return Sequential(items)
예제 #2
0
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)
예제 #3
0
def DenseNet(blocks,
             growth_rate=32,
             initial_filters=64,
             include_top=True,
             pretrained=True,
             input_shape=(224, 224, 3),
             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),
                     num_filters=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'))
    densenet.add_module('avg_pool', GlobalAvgPool2d(name='avg_pool'))
    if include_top:
        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)

    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[0], 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