Exemplo n.º 1
0
def create_cnn_model(base_arch:Callable, nc:int, cut:Union[int,Callable]=None, pretrained:bool=True,
                     lin_ftrs:Optional[Collection[int]]=None, ps:Floats=0.5, custom_head:Optional[nn.Module]=None,
                     bn_final:bool=False, concat_pool:bool=True):
    "Create custom convnet architecture"
    body = create_body(base_arch, pretrained, cut)
    if custom_head is None:
        nf = num_features_model(nn.Sequential(*body.children())) * (2 if concat_pool else 1)
        head = create_head(nf, nc, lin_ftrs, ps=ps, concat_pool=concat_pool, bn_final=bn_final)
    else: head = custom_head
    return nn.Sequential(body, head)
Exemplo n.º 2
0
 def __init__(self):
     super().__init__()
     concat_pool = True
     self.encoder = create_body(models.resnet18, True)
     nf = num_features_model(nn.Sequential(
         *(self.encoder).children())) * (2 if concat_pool else 1)
     self.domain_head_1, self.domain_head_2 = create_domain_head(
         nf, 2, lin_ftrs=[256])
     self.class_head_1, self.class_head_2 = create_classification_head(
         nf, 2, lin_ftrs=[256])
     self.alpha = 0
Exemplo n.º 3
0
    def create_head(self):
        # num_features_model is 512 for resnet18, is 1280 for mobilenet
        modules = [
            nn.ReLU(inplace=True),
            nn.Dropout(p=0.75),
            nn.Linear(num_features_model(self.body), self.n_output)
        ]

        if self.n_output != 1:
            modules.append(nn.Softmax(dim=1))

        return nn.Sequential(*modules)  # 1280 for mobilenet: 224x224 --> 7x7
Exemplo n.º 4
0
def create_model(num_classes=None):
    """Create a fastai compatible torch model.

    This model is a sequential model with body and head children.

    If num_classes is not provided, it will use the torch models
    default head (ie. for 1k imagenet classes)
    """

    model = new_model()

    if num_classes:
        body = list(model.children())[0]
        nf = num_features_model(body) * 2
        head = create_head(nf, num_classes, None, ps=0.5, bn_final=False)
        model = nn.Sequential(body, head)

    return model
Exemplo n.º 5
0
def DR_learner(data: DataBunch,
               base_arch: Callable,
               cut: Union[int, Callable] = None,
               pretrained: bool = True,
               lin_ftrs: Optional[Collection[int]] = None,
               ps: Floats = 0.5,
               custom_head: Optional[nn.Module] = None,
               split_on: Optional[SplitFuncOrIdxList] = None,
               bn_final: bool = False,
               init=nn.init.kaiming_normal_,
               concat_pool: bool = True,
               attention=False,
               **kwargs: Any) -> Learner:
    "Build convnet style learner."
    meta = cnn_config(base_arch)

    "Create custom convnet architecture"
    body = create_body(base_arch, pretrained, cut)
    if custom_head is None:  # quadnet head
        nf = num_features_model(
            nn.Sequential(*body.children())) * (2 if concat_pool else 1)
        if attention:
            pass
        head = create_DR_head(nf,
                              data.c + 2,
                              lin_ftrs,
                              ps=ps,
                              concat_pool=concat_pool,
                              bn_final=bn_final)
    else:
        head = custom_head

    model = nn.Sequential(body, head)

    learn = Learner(data, model, **kwargs)
    learn.split(split_on or meta['split'])
    if pretrained:
        learn.freeze()
    if init:
        apply_init(model[1], init)
    return learn
Exemplo n.º 6
0
 def create_head(self):
     nf = num_features_model(self.body) * 2
     return create_head(nf, self.n_output)