def _resnet(arch, block, layers, pretrained, progress, **kwargs): model = ResNet(block, layers, **kwargs) if pretrained: state_dict = load_state_dict_from_url(model_urls[arch], progress=progress) model.load_state_dict(state_dict) return model
def _resnet(arch: str, in_channels, block: Type[Union[BasicBlock, Bottleneck]], layers: List[int], layers_channel: List[int], pretrained: bool, progress: bool, **kwargs: Any) -> ResNetM: model = ResNetM(block, in_channels, layers, layers_channel, **kwargs) if pretrained: state_dict = load_state_dict_from_url(model_urls[arch], progress=progress) model.load_state_dict(state_dict) return model
def _resnet_global_local_pred(codeword_multiplier, out_dims, arch, block, layers, pretrained, progress, **kwargs): model = GlobalLocalPredResNet(codeword_multiplier, out_dims, block, layers, **kwargs) if pretrained: state_dict = ResNetUtils.load_state_dict_from_url(ResNetUtils.model_urls[arch], progress=progress) model.load_state_dict(state_dict) return model
def resnet152(pretrained=True, is_lower=True, num_classes=12, **kwargs): model = ResNetLower(Bottleneck, [3, 8, 36, 2], ** kwargs) if is_lower else ResNetUpper( num_classes=num_classes) if pretrained: model.load_imagenet_state_dict( resnet.load_state_dict_from_url(model_urls['resnet152'])) print("loaded imagenet pretrained resnet152") return model
def ResNet18(pretrained=False, fine_tune=False, classes=(10, 10), pool=True): model = MultiTaskResNet(fine_tune, classes, pool) if pretrained: state_dict = resnet.load_state_dict_from_url( resnet.model_urls['resnet18']) model.load_state_dict(state_dict) return model
def _resnetlstm(arch, pretrained, block, layers, **kwargs): model = ResNetLSTM(**kwargs) # use weights pretrained on the 1000-class ImageNet dataset if pretrained: state_dict = load_state_dict_from_url(model_urls[arch]) for key in model.state_dict(): if 'lstm' in key or 'fc' in key: state_dict[key] = model.state_dict()[key] model.load_state_dict(state_dict) return model
def _resnet(codeword_multiplier, sparsity_multiplier, arch, block, layers, pretrained, progress, **kwargs): model = SparseRepResNet(codeword_multiplier, sparsity_multiplier, block, layers, **kwargs) if pretrained: state_dict = ResNetUtils.load_state_dict_from_url( ResNetUtils.model_urls[arch], progress=progress) model.load_state_dict(state_dict) return model
def load_pretrained_resnet(arch, progress=True): if arch == 'resnet34': model = ResNet(BasicBlock, [3, 4, 6, 3]) if arch == 'resnet18': model = ResNet(BasicBlock, [2, 2, 2, 2]) state_dict = load_state_dict_from_url(model_urls[arch], progress=progress) model.load_state_dict(state_dict, strict=False) model.fc_channel = model.fc.in_features delattr(model, 'fc') return model
def _resnet(arch, block, layers, branches, pretrained, progress, **kwargs): model = MTLResNet(block, layers, branches, **kwargs) if pretrained: state_dict = load_state_dict_from_url(model_urls[arch], progress=progress) own_state = model.state_dict() pretrained_dict = { k: v for k, v in state_dict.items() if k in own_state } # 2. overwrite entries in the existing state dict own_state.update(pretrained_dict) # 3. load the new state dict model.load_state_dict(own_state) return model
def __init__(self, landmark_num: int = 5, pretrained=True, pretrained_path: str = None): super().__init__() self.res = torchvision.models.resnet18(pretrained=False, num_classes=landmark_num * 2) if pretrained: if pretrained_path: state_dict = torch.load(pretrained_path) self.load_state_dict(state_dict, strict=True) else: state_dict = load_state_dict_from_url( res_model_urls['resnet18'], progress=True) state_dict.pop('fc.weight') state_dict.pop('fc.bias') self.res.load_state_dict(state_dict, strict=False)