Exemplo n.º 1
0
    def __init__(self, encoder=None, decoder=None):
        super(AutoEncoder, self).__init__()
        if encoder is None:
            encoder = {
                "name": "VGG16",
                "module": "deeplodocus.app.models.vgg",
                "kwargs": {
                    "batch_norm": True,
                    "pretrained": True,
                    "num_channels": 3,
                    "include_top": False
                }
            }
        else:
            if "include_top" not in encoder["kwargs"]:
                Notification(
                    DEEP_NOTIF_WARNING,
                    "include_top not specified for the encoder, should be set to False"
                )
        if decoder is None:
            decoder = {
                "name": "VGG16Decode",
                "module": "deeplodocus.app.models.vgg",
                "kwargs": {
                    "batch_norm": True,
                    "num_classes": 10
                }
            }

        self.encoder = get_specific_module(
            name=encoder["name"],
            module=encoder["module"])(**encoder["kwargs"])
        self.decoder = get_specific_module(
            name=decoder["name"],
            module=decoder["module"],
        )(**decoder["kwargs"])
Exemplo n.º 2
0
    def __init__(self,
                 backbone=None,
                 num_classes=10,
                 skip_layers=None,
                 anchors=(((10, 13), (16, 30), (22, 23)), ((30, 61), (62, 45),
                                                           (59, 119)),
                          ((116, 90), (156, 198), (373, 326)))):
        super(YOLO, self).__init__()
        self.num_classes = num_classes
        self.anchors = anchors

        # If no backbone specified, set default backbone arguments
        if backbone is None:
            backbone = {
                "name": "Darknet53",
                "module": "deeplodocus.app.models.darknet",
                "kwargs": {
                    "num_channels": 3,
                    "include_top": False
                }
            }
            skip_layers = (36, 61)

        # If skip layers not specified, select the appropriate indices in accordance with skip_dict
        if skip_layers is None:
            try:
                self.skip_layers = skip_dict[backbone["name"]]
                Notification(
                    DEEP_NOTIF_INFO,
                    MSG_AUTO_SKIP % (backbone["name"], str(self.skip_layers)))
            except KeyError:
                Notification(DEEP_NOTIF_FATAL, MSG_NO_SKIP % backbone["name"])
        else:
            self.skip_layers = skip_layers

        # Get and initialise the backbone module
        backbone_module = get_specific_module(name=backbone["name"],
                                              module=backbone["module"],
                                              fatal=True)
        self.backbone = backbone_module(**backbone["kwargs"])

        # CONVOLUTIONAL LAYERS/BLOCKS (ConvBlocks contain 4 conv layers)
        self.conv_1_1 = ConvLayer(in_channels=1024,
                                  out_channels=512,
                                  kernel_size=1,
                                  negative_slope=0.1)
        self.conv_1_2 = ConvBlock(1024)
        self.conv_1_3 = ConvLayer(in_channels=512,
                                  out_channels=1024,
                                  kernel_size=3,
                                  padding=1,
                                  negative_slope=0.1)
        self.conv_1_4 = nn.Conv2d(in_channels=1024,
                                  out_channels=len(anchors) *
                                  (num_classes + 5),
                                  kernel_size=1)

        self.conv_2_1 = ConvLayer(in_channels=512,
                                  out_channels=256,
                                  kernel_size=1,
                                  negative_slope=0.1)
        self.conv_2_2 = ConvLayer(in_channels=768,
                                  out_channels=256,
                                  kernel_size=1,
                                  negative_slope=0.1)
        self.conv_2_3 = ConvBlock(512)
        self.conv_2_4 = ConvLayer(in_channels=256,
                                  out_channels=512,
                                  kernel_size=3,
                                  padding=1,
                                  negative_slope=0.1)
        self.conv_2_5 = nn.Conv2d(in_channels=512,
                                  out_channels=len(anchors) *
                                  (num_classes + 5),
                                  kernel_size=1)

        self.conv_3_1 = ConvLayer(in_channels=256,
                                  out_channels=128,
                                  kernel_size=1,
                                  negative_slope=0.1)
        self.conv_3_2 = ConvLayer(in_channels=384,
                                  out_channels=128,
                                  kernel_size=1,
                                  negative_slope=0.1)
        self.conv_3_3 = ConvBlock(256)
        self.conv_3_4 = ConvLayer(in_channels=128,
                                  out_channels=256,
                                  kernel_size=3,
                                  padding=1,
                                  negative_slope=0.1)
        self.conv_3_5 = nn.Conv2d(in_channels=256,
                                  out_channels=len(anchors) *
                                  (num_classes + 5),
                                  kernel_size=1)

        # UPSAMPLE LAYER
        self.upsample = nn.Upsample(scale_factor=2, mode="nearest")

        # Classify after backbone
        self.classifier = nn.Linear(in_features=1024, out_features=num_classes)
Exemplo n.º 3
0
    def __init__(
        self,
        backbone=None,
        num_classes=80,
        skip_layers=(36, 61),
        input_shape=(256, 256),
        anchors=(((116, 90), (156, 198), (373, 326)),
                 ((30, 61), (62, 45), (59, 119)), ((10, 13), (16, 30), (22,
                                                                        23))),
        normalized_anchors=False,
    ):
        super(YOLOv3, self).__init__()
        # Default backbone arguments
        if backbone is None:
            backbone = {
                "name": "Daknet53",
                "module": "deeplodocus.app.models.darknet",
                "kwargs": {
                    "num_channels": 3,
                    "include_top": False
                }
            }

        # Scale anchors by image dimensions if the anchors are normalized
        if normalized_anchors:
            anchors = [[(a[0] * input_shape[1], a[1] * input_shape[0])
                        for a in anchor] for anchor in anchors]

        # Get the number of anchor boxes
        num_anchors = len(anchors)

        # Get and initialise the backbone module
        backbone_module = get_specific_module(name=backbone["name"],
                                              module=backbone["module"],
                                              fatal=True)
        self.backbone = backbone_module(**backbone["kwargs"])

        # The keys to extract from the list
        self.skip_layers = skip_layers

        # CONVOLUTIONAL LAYERS/BLOCKS (ConvBlocks consist of 4 conv layers)
        self.conv_1_1 = ConvLayer(in_channels=1024,
                                  out_channels=512,
                                  kernel_size=1,
                                  negative_slope=0.1)
        self.conv_1_2 = ConvBlock(1024)
        self.conv_1_3 = ConvLayer(in_channels=512,
                                  out_channels=1024,
                                  kernel_size=3,
                                  padding=1,
                                  negative_slope=0.1)
        self.conv_1_4 = nn.Conv2d(in_channels=1024,
                                  out_channels=num_anchors * (num_classes + 5),
                                  kernel_size=1)

        self.conv_2_1 = ConvLayer(in_channels=512,
                                  out_channels=256,
                                  kernel_size=1,
                                  negative_slope=0.1)
        self.conv_2_2 = ConvLayer(in_channels=768,
                                  out_channels=256,
                                  kernel_size=1,
                                  negative_slope=0.1)
        self.conv_2_3 = ConvBlock(512)
        self.conv_2_4 = ConvLayer(in_channels=256,
                                  out_channels=512,
                                  kernel_size=3,
                                  padding=1,
                                  negative_slope=0.1)
        self.conv_2_5 = nn.Conv2d(in_channels=512,
                                  out_channels=num_anchors * (num_classes + 5),
                                  kernel_size=1)

        self.conv_3_1 = ConvLayer(in_channels=256,
                                  out_channels=128,
                                  kernel_size=1,
                                  negative_slope=0.1)
        self.conv_3_2 = ConvLayer(in_channels=384,
                                  out_channels=128,
                                  kernel_size=1,
                                  negative_slope=0.1)
        self.conv_3_3 = ConvBlock(256)
        self.conv_3_4 = ConvLayer(in_channels=128,
                                  out_channels=256,
                                  kernel_size=3,
                                  padding=1,
                                  negative_slope=0.1)
        self.conv_3_5 = nn.Conv2d(in_channels=256,
                                  out_channels=num_anchors * (num_classes + 5),
                                  kernel_size=1)

        # UPSAMPLE LAYER
        self.upsample = nn.Upsample(scale_factor=2, mode="nearest")

        # YOLO LAYERS
        self.yolo_layer_1 = YoloLayer(num_classes=num_classes,
                                      image_shape=input_shape,
                                      anchors=anchors[0])
        self.yolo_layer_2 = YoloLayer(num_classes=num_classes,
                                      image_shape=input_shape,
                                      anchors=anchors[1])
        self.yolo_layer_3 = YoloLayer(num_classes=num_classes,
                                      image_shape=input_shape,
                                      anchors=anchors[2])