def create_timm_body(arch:str, pretrained=True, cut=None, n_in=3): "Creates a body from any model in the `timm` library." model = create_model(arch, pretrained=pretrained, num_classes=0, global_pool='') _update_first_layer(model, n_in, pretrained) if cut is None: ll = list(enumerate(model.children())) cut = next(i for i,o in reversed(ll) if has_pool_type(o)) if isinstance(cut, int): return nn.Sequential(*list(model.children())[:cut]) elif callable(cut): return cut(model) else: raise NamedError("cut must be either integer or function")
def create_body(arch, n_in=3, pretrained=True, cut=None): "Cut off the body of a typically pretrained `arch` as determined by `cut`" model = arch(pretrained=pretrained) _update_first_layer(model, n_in, pretrained) if cut is None: ll = list(enumerate(model.children())) cut = next(i for i,o in reversed(ll) if has_pool_type(o)) if isinstance(cut, int): return nn.Sequential(*list(model.children())[:cut]) elif callable(cut): return cut(model) else: raise NamedError("cut must be either integer or a function")