def recordDense(info): global SKIP import torchvision.models.densenet as denGen if not (SKIP and 'densenet121' in info['name_list']): INFO("proceeding for DenseNet121") net = denGen.densenet121(pretrained=True).cuda() sum = __summary(net, [3, 224, 224], verbose=True) __writeInfoJSON(sum, 'densenet121') else: INFO("Skip DenseNet121") if not (SKIP and 'densenet161' in info['name_list']): INFO("proceeding for DenseNet161") net = denGen.densenet161(pretrained=True).cuda() sum = __summary(net, [3, 224, 224], verbose=True) __writeInfoJSON(sum, 'densenet161') else: INFO("Skip DenseNet161") if not (SKIP and 'densenet169' in info['name_list']): INFO("proceeding for DenseNet169") net = denGen.densenet169(pretrained=True).cuda() sum = __summary(net, [3, 224, 224], verbose=True) __writeInfoJSON(sum, 'densenet169') else: INFO("Skip DenseNet169") if not (SKIP and 'densenet201' in info['name_list']): INFO("proceeding for DenseNet201") net = denGen.densenet201(pretrained=True).cuda() sum = __summary(net, [3, 224, 224], verbose=True) __writeInfoJSON(sum, 'densenet201') else: INFO("Skip DenseNet201")
def __init__(self, subtype='densenet121', out_stages=[2, 3, 4], backbone_path=None): super(Densenet, self).__init__() self.out_stages = out_stages self.backbone_path = backbone_path self.out_channels = [64, 128, 256, 512, 1024] if subtype == 'densenet121': features = densenet121(pretrained=not backbone_path).features elif subtype == 'densenet161': features = densenet161(pretrained=not backbone_path).features elif subtype == 'densenet169': features = densenet169(pretrained=not backbone_path).features elif subtype == 'densenet201': features = densenet201(pretrained=not backbone_path).features self.out_channels = self.out_channels[self.out_stages[0]:self. out_stages[-1] + 1] self.conv1 = nn.Sequential(features.conv0, features.norm0, features.relu0, features.pool0) self.layer1 = nn.Sequential(features.denseblock1, features.transition1) self.layer2 = nn.Sequential(features.denseblock2, features.transition2) self.layer3 = nn.Sequential(features.denseblock3, features.transition3) self.layer4 = features.denseblock4 if self.backbone_path: self.features.load_state_dict(torch.load(self.backbone_path)) else: self.init_weights()
def __init__(self, cf, num_classes=21, pretrained=False, net_name='densenet161'): super(DenseNet161, self).__init__(cf) self.url = '' self.pretrained = pretrained self.net_name = net_name if pretrained: self.model = models.densenet161(pretrained=True) self.model.classifier = nn.Linear(1024, num_classes) else: self.model = models.densenet161(pretrained=False, num_classes=num_classes)
def __init__(self, config): super().__init__(config) self.freeze_num = 2 feats = densenet.densenet161( num_classes=1000, pretrained="imagenet").children().__next__() self.model = nn.Sequential(*list(feats.children())[:-2]) self.dropout = nn.Dropout2d(p=0.2) self.get_last_linear(in_features=1056)
def __init__(self, cf, num_classes=21, pretrained=False, net_name='densenet161'): super(DenseNet161, self).__init__(cf) self.pretrained = pretrained self.net_name = net_name if self.pretrained: self.model = models.densenet161(pretrained=True) num_ftrs = self.model.classifier.in_features self.model.classifier = nn.Linear(num_ftrs, num_classes) self.model = models.densenet161(pretrained=False, num_classes=num_classes) '''if pretrained:
def create_model(model_name, num_classes=1000, pretrained=False, **kwargs): if 'test_time_pool' in kwargs: test_time_pool = kwargs.pop('test_time_pool') else: test_time_pool = True if 'extra' in kwargs: extra = kwargs.pop('extra') else: extra = True if model_name == 'dpn68': model = dpn68( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn68b': model = dpn68b( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn92': model = dpn92( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool, extra=extra) elif model_name == 'dpn98': model = dpn98( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn131': model = dpn131( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn107': model = dpn107( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'resnet18': model = resnet18(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet34': model = resnet34(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet50': model = resnet50(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet101': model = resnet101(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet152': model = resnet152(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet121': model = densenet121(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet161': model = densenet161(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet169': model = densenet169(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet201': model = densenet201(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'inception_v3': model = inception_v3( num_classes=num_classes, pretrained=pretrained, transform_input=False, **kwargs) else: assert False, "Unknown model architecture (%s)" % model_name return model
def create_model(model_name, num_classes=1000, pretrained=False, **kwargs): if 'test_time_pool' in kwargs: test_time_pool = kwargs.pop('test_time_pool') else: test_time_pool = True if 'extra' in kwargs: extra = kwargs.pop('extra') else: extra = True if model_name == 'dpn68': model = dpn68( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn68b': model = dpn68b( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn92': model = dpn92( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool, extra=extra) elif model_name == 'dpn98': model = dpn98( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn131': model = dpn131( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn107': model = dpn107( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'resnet18': model = resnet18(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet34': model = resnet34(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet50': model = resnet50(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet101': model = resnet101(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet152': model = resnet152(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet121': model = densenet121(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet161': model = densenet161(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet169': model = densenet169(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet201': model = densenet201(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'inception_v3': model = inception_v3( num_classes=num_classes, pretrained=pretrained, transform_input=False, **kwargs) else: assert False, "Unknown model architecture (%s)" % model_name return model
def get_densenet_model(densenet, num_classes=-1): if densenet == "densenet121": model = densenet121(pretrained=True) elif densenet == "densenet169": model = densenet169(pretrained=True) elif densenet == "densenet201": model = densenet201(pretrained=True) elif densenet == "densenet161": model = densenet161(pretrained=True) else: raise RuntimeError("Unsupported model, %s" % densenet) if num_classes > 0: in_features = model.classifier.in_features classifier = nn.Linear(in_features, num_classes, bias=True) model.classifier = classifier return model
def __init__(self, num_class=1000, in_channels=3, pretrained=False, **kargs): super(Densenet161, self).__init__() self.model = densenet161(pretrained=pretrained) self.model.features[0] = torch.nn.Conv2d(in_channels=in_channels, out_channels=96, kernel_size=7, stride=2, padding=3, bias=False) self.model.classifier = torch.nn.Linear(in_features=2208, out_features=num_class, bias=True)
def __init__(self, pretrained=True): super(FurnitureDenseNet161_350, self).__init__() self.model = densenet161(num_classes=1000, pretrained=pretrained) num_features = self.model.classifier.in_features self.model.classifier = Linear(num_features, 128) for m in self.model.classifier.modules(): if isinstance(m, Linear): m.weight.data.normal_(0, 0.01) m.bias.data.zero_() # create aliases: self.stem = ModuleList([ self.model.features[0], self.model.features[1], ]) self.features = ModuleList([ self.model.features[i] for i in range(2, len(self.model.features)) ]) self.classifier = self.model.classifier
def __init__(self, pretrained=True, progress=True, **kwargs): super(densenet161, self).__init__() backbone = densenet.densenet161(pretrained, progress, **kwargs) self.backbone = backbone.features
from collections import OrderedDict # Project imports from util.util import print # print with a header for this project # Leveraged Torchvision implementation of DenseNet at: # https://github.com/pytorch/vision/blob/master/torchvision/models/densenet.py _use_pretrained = True name_to_model_fns = { "densenet121": lambda: densenet.densenet121(pretrained=_use_pretrained), "densenet169": lambda: densenet.densenet169(pretrained=_use_pretrained), "densenet201": lambda: densenet.densenet201( pretrained=_use_pretrained) # We're just going to use this one , "densenet161": lambda: densenet.densenet161(pretrained=_use_pretrained) } def get_densenet(name): return name_to_model_fns[name]() # Call the function """ old_model.modules is more accurate of all indiv. layers old_model.features decomposes structure more, but doesn't include final FC classifier (instead under old_model.classifier) DenseNet201's model.features [nn.Sequential] 0 = conv2d 1 = batchnorm