def DenseNet201(include_top=True,
                pretrained=True,
                input_shape=(224, 224, 3),
                classes=1000,
                **kwargs):
    """
    Constructor the image classicication model with DenseNet201 as backbond

    Args:
        include_top ():
        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)
        classes (int): number of classes

    References
        Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf

    Returns:
        the image classicication model with DenseNet201

    Examples:
        >>> dense201 = DenseNet201(include_top=True,pretrained=True,input_shape=(224,224,3),classes=1000)
        >>> 'n02124075' in dense201.infer_single_image(get_image_from_google_drive('1SwablQsZO8mBuB84xnr1IoOisE3pm03l'),1).key_list[0]
        True

    """
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)

    densenet201 = DenseNet([6, 12, 48, 32],
                           32,
                           64,
                           include_top=include_top,
                           pretrained=True,
                           input_shape=input_shape,
                           num_classes=classes,
                           name='densenet201')
    if pretrained == True:
        download_model_from_google_drive('1dJfgus11jXVoCLWfZqTgZ6jKKtrB70om',
                                         dirname, 'densenet201_tf.pth')
        recovery_model = load(os.path.join(dirname, 'densenet201_tf.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model._name = 'densenet201'
        recovery_model.eval()
        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
        else:
            if classes != 1000:
                recovery_model.remove_at(-1)
                recovery_model.remove_at(-1)
                recovery_model.add_module(
                    'classifier',
                    Dense(classes, activation=None, name='classifier'))
                recovery_model.add_module('softmax', SoftMax(name='softmax'))
        recovery_model.signature = Signature(name='DenseNetFcn')
        if is_tensor(recovery_model._input_shape):
            recovery_model.signature.inputs['input'] = TensorSpec(
                shape=recovery_model._input_shape, name='input')
        if is_tensor(recovery_model._output_shape):
            recovery_model.signature.outputs['output'] = TensorSpec(
                shape=recovery_model._output_shape, name='output')

        densenet201.model = recovery_model
    return densenet201
示例#2
0
def DenseNet201(include_top=True,
                pretrained=True,
                input_shape=(224, 224, 3),
                freeze_features=False,
                classes=1000,
                **kwargs):
    """
    Constructor the image classicication model with DenseNet201 as backbond

    Args:
        freeze_features ():
        include_top ():
        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)
        classes (int): number of classes

    References
        Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf

    Returns:
        the image classicication model with DenseNet201

    Examples:
        >>> dense201 = DenseNet201(include_top=True,pretrained=True,input_shape=(224,224,3),classes=1000)
        >>> 'n02124075' in dense201.infer_single_image(get_image_from_google_drive('1SwablQsZO8mBuB84xnr1IoOisE3pm03l'),1).key_list[0]
        True

    """
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)

    densenet201 = DenseNet([6, 12, 48, 32],
                           32,
                           64,
                           include_top=include_top,
                           pretrained=True,
                           input_shape=input_shape,
                           num_classes=classes,
                           name='densenet201')
    with tf.device(get_device()):
        if pretrained == True:
            download_model_from_google_drive(
                '1dJfgus11jXVoCLWfZqTgZ6jKKtrB70om', dirname,
                'densenet201_tf.pth')
            recovery_model = load(os.path.join(dirname, 'densenet201_tf.pth'))
            recovery_model = fix_layer(recovery_model)
            recovery_model._name = 'densenet201'

            recovery_model = _make_recovery_model_include_top(
                recovery_model,
                include_top=include_top,
                classes=classes,
                freeze_features=freeze_features)
            densenet201.model = recovery_model

        else:
            densenet201.model = _make_recovery_model_include_top(
                densenet201.model,
                include_top=include_top,
                classes=classes,
                freeze_features=False)
            densenet201.model.input_shape = input_shape
        return densenet201