def download_model(saving_path='.'): # inception net # model = models.Inception3() # model.load_state_dict(model_zoo.load_url(model_urls['inception_v3_google'], model_dir=saving_path, progress=True)) # resnet model = models.ResNet(_Bottleneck, [3, 8, 36, 3]) model.load_state_dict(model_zoo.load_url(model_urls['resnet152'], model_dir=saving_path, progress=True)) # save_model(model, 'resnet152.pkl', saving_path) # alex net model = models.AlexNet() model.load_state_dict(model_zoo.load_url(model_urls['alexnet'], model_dir=saving_path, progress=True)) # save_model(model, 'alexnet.pkl', saving_path) # vgg model = models.VGG(_vgg_make_layers(_vgg_cfg['E'], batch_norm=True), init_weights=False) model.load_state_dict(model_zoo.load_url(model_urls['vgg19_bn'], model_dir=saving_path, progress=True)) # save_model(model, 'vgg19.pkl', saving_path) # squeeze net model = models.SqueezeNet(version=1.1) model.load_state_dict(model_zoo.load_url(model_urls['squeezenet1_1'], model_dir=saving_path, progress=True)) # save_model(model, 'squeezenet1_1.pkl', saving_path) # dense net model = models.DenseNet(num_init_features=64, growth_rate=32, block_config=(6, 12, 48, 32)) pattern = re.compile( r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$') state_dict = model_zoo.load_url(model_urls['densenet201'], model_dir=saving_path, progress=True) for key in list(state_dict.keys()): res = pattern.match(key) if res: new_key = res.group(1) + res.group(2) state_dict[new_key] = state_dict[key] del state_dict[key] model.load_state_dict(state_dict) # save_model(model, 'densenet201.pkl', saving_path) # googlenet kwargs = dict() kwargs['transform_input'] = True kwargs['aux_logits'] = False # if kwargs['aux_logits']: # warnings.warn('auxiliary heads in the pretrained googlenet model are NOT pretrained, ' # 'so make sure to train them') original_aux_logits = kwargs['aux_logits'] kwargs['aux_logits'] = True kwargs['init_weights'] = False model = models.GoogLeNet(**kwargs) model.load_state_dict(model_zoo.load_url(model_urls['googlenet'])) if not original_aux_logits: model.aux_logits = False del model.aux1, model.aux2 # save_model(model, 'googlenet.pkl', saving_path) # resnext model = models.resnext101_32x8d(pretrained=False) model.load_state_dict(model_zoo.load_url(model_urls['resnext101_32x8d'], model_dir=saving_path, progress=True))
def test_googlenet_eval(self): # replacement for models.googlenet(pretrained=True) that does not download weights kwargs = {} kwargs['transform_input'] = True kwargs['aux_logits'] = True kwargs['init_weights'] = False model = models.GoogLeNet(**kwargs) model.aux_logits = False model.aux1 = None model.aux2 = None m = torch.jit.script(model.eval()) self.checkModule(m, "googlenet", torch.rand(1, 3, 224, 224))
def test_googlenet_eval(): # replacement for models.googlenet(pretrained=True) that does not download weights kwargs = {} kwargs['transform_input'] = True kwargs['aux_logits'] = True kwargs['init_weights'] = False name = "googlenet" model = models.GoogLeNet(**kwargs) model.aux_logits = False model.aux1 = None model.aux2 = None model = model.eval() x = torch.rand(1, 3, 224, 224) _check_jit_scriptable(model, (x,), unwrapper=script_model_unwrapper.get(name, None))
def test_googlenet_eval(): kwargs = {} kwargs["transform_input"] = True kwargs["aux_logits"] = True kwargs["init_weights"] = False name = "googlenet" model = models.GoogLeNet(**kwargs) model.aux_logits = False model.aux1 = None model.aux2 = None model = model.eval() x = torch.rand(1, 3, 224, 224) _check_jit_scriptable(model, (x,), unwrapper=script_model_unwrapper.get(name, None)) _check_input_backprop(model, x)
def load_model(model_name): global MODEL_NAME # Detect if we have a GPU available device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") if model_name == 'ResNet': model = models.resnet152(pretrained=False) model.load_state_dict(torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device)) elif model_name == 'AlexNet': model = models.AlexNet() model.load_state_dict(torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device)) elif model_name == 'VGG': model = models.VGG(_vgg_make_layers(_vgg_cfg['E'], batch_norm=True), init_weights=False) model.load_state_dict(torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device)) elif model_name == 'DenseNet': model = models.DenseNet(num_init_features=64, growth_rate=32, block_config=(6, 12, 48, 32)) pattern = re.compile( r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$') state_dict = torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device) for key in list(state_dict.keys()): res = pattern.match(key) if res: new_key = res.group(1) + res.group(2) state_dict[new_key] = state_dict[key] del state_dict[key] model.load_state_dict(state_dict) elif model_name == 'GoogleNet': # googlenet kwargs = dict() kwargs['transform_input'] = True kwargs['aux_logits'] = False # if kwargs['aux_logits']: # warnings.warn('auxiliary heads in the pretrained googlenet model are NOT pretrained, ' # 'so make sure to train them') original_aux_logits = kwargs['aux_logits'] kwargs['aux_logits'] = True kwargs['init_weights'] = False model = models.GoogLeNet(**kwargs) model.load_state_dict(torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device)) if not original_aux_logits: model.aux_logits = False del model.aux1, model.aux2 elif model_name == 'ResNext101': model = models.resnext101_32x8d(pretrained=False) model.load_state_dict(torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device)) else: raise ValueError("Model name must be one of ['VGG', 'ResNet', 'DenseNet', 'AlexNet', 'GoogleNet', 'ResNext101']") return model
def load_model(name, num_classes, input_height=64, input_width=64, num_of_channels=3): if name == 'COAPModNet': net = COAPModNet(num_classes=num_classes) elif name == 'COAPNet': net = COAPNet(num_classes=num_classes) elif name == 'SimpleNet': net = SimpleNet(num_classes=num_classes) elif name == 'AlexNet': net = models.AlexNet(num_classes=num_classes) elif name == 'PlanktonNet': net = PlanktonNet(num_classes=num_classes) elif name == 'ResNet18': net = models.resnet18(num_of_channels, num_classes) elif name == 'ResNet34': net = models.resnet34(num_of_channels, num_classes) elif name == 'ResNet50': net = models.resnet50(num_of_channels, num_classes) elif name == 'ResNet101': net = models.resnet101(num_of_channels, num_classes) elif name == 'ResNet152': net = models.resnet152(num_of_channels, num_classes) elif name == 'VGGNet11': net = models.vgg11(num_classes=num_classes) elif name == 'VGGNet13': net = models.vgg13(num_classes=num_classes) elif name == 'VGGNet16': net = models.vgg16(num_classes=num_classes) elif name == 'VGGNet19': net = models.vgg19(num_classes=num_classes) elif name == 'ResNext50': net = models.resnext50_32x4d(num_classes=num_classes) elif name == 'ResNext101': net = models.resnext101_32x8d(num_classes=num_classes) elif name == 'GoogLeNet': net = models.GoogLeNet(num_classes=num_classes) return net
def __init__(self): super(Facenet_train, self).__init__() self.layer = models.GoogLeNet(128)
def __init__(self, num_classes=3, num_level=3, pool_type='max_pool', use_spp=False): super(InceptionV1, self).__init__() self.inception = models.GoogLeNet(num_classes=3, aux_logits=False) # print(self.inception._modules.keys()) # del self.inception.aux1 # del self.inception.aux2 del self.inception.avgpool del self.inception.dropout del self.inception.fc # print(self.inception) origin_dicts = torch.load('pth/googlenet-1378be20.pth') # 预训练模型中googlenet网络的参数 model_dicts = self.inception.state_dict() # 自定义的去掉后面几层的网络的参数列表 pretrained_dicts = {k: v for k, v in origin_dicts.items() if k in model_dicts} # 预训练模型参数在自定义模型中有的参数列表 model_dicts.update(pretrained_dicts) # 更新自定义的模型参数 self.inception.load_state_dict(model_dicts) # print(self.inception._modules.keys()) # print(self.inception.conv1.conv) self.inception_1 = models.GoogLeNet(num_classes=3, aux_logits=False) del self.inception_1.avgpool del self.inception_1.dropout del self.inception_1.fc self.inception_1.conv1.conv = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False) model_dicts = self.inception_1.state_dict() # 自定义的去掉后面几层的网络的参数列表 pretrained_dicts = {k: v for k, v in origin_dicts.items() if k in model_dicts} # 预训练模型参数在自定义模型中有的参数列表 layer1 = pretrained_dicts['conv1.conv.weight'] new = torch.zeros(64, 1, 7, 7) for i, output_channel in enumerate(layer1): new[i] = 0.299 * output_channel[0] + 0.587 * output_channel[1] + 0.114 * output_channel[2] pretrained_dicts['conv1.conv.weight'] = new model_dicts.update(pretrained_dicts) # 更新自定义的模型参数 self.inception_1.load_state_dict(model_dicts) self.conv1_fusion = nn.Conv2d(3072, 256, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(256) self.relu1_fusion = nn.ReLU(inplace=True) self.conv2_fusion = nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(128) self.relu2_fusion = nn.ReLU(inplace=True) self.downsample = nn.Sequential( nn.Conv2d(3072, 128, kernel_size=1, stride=1, padding=0, bias=False), nn.BatchNorm2d(128), ) self.conv_fusion = nn.Sequential( nn.Conv2d(128, 64, kernel_size=1, stride=1, padding=0, bias=False), nn.ReLU(inplace=True), ) self.classifier = nn.Sequential( nn.Dropout(), nn.Linear(64 * 4 * 4, 512), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(512, 256), nn.ReLU(inplace=True), nn.Linear(256, num_classes), ) self.use_spp = use_spp if use_spp: self.num_level = num_level self.pool_type = pool_type self.num_grid = self._cal_num_grids(num_level) self.spp_layer = SpatialPyramidPooling2d(num_level) self.classifier_spp = nn.Sequential( nn.Dropout(), nn.Linear(64 * self.num_grid, 512), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(512, 256), nn.ReLU(inplace=True), nn.Linear(256, num_classes), )
pytest.param( "models.mnasnet", "MNASNet", {"alpha": 1.0}, [], {}, models.MNASNet(alpha=1.0), id="MNASNetConf", ), pytest.param( "models.googlenet", "GoogLeNet", {}, [], {}, models.GoogLeNet(), id="GoogleNetConf", ), ], ) def test_instantiate_classes( modulepath: str, classname: str, cfg: Any, passthrough_args: Any, passthrough_kwargs: Any, expected: Any, ) -> None: full_class = f"hydra_configs.torchvision.{modulepath}.{classname}Conf" schema = OmegaConf.structured(get_class(full_class)) cfg = OmegaConf.merge(schema, cfg)
def get_googLenet_model(weights_path=None, num_classes=1000): googLenet = models.GoogLeNet(num_classes=num_classes, init_weights=True, aux_logits=False) try_load_weights(googLenet, weights_path) return googLenet
# run smoothing smoothing(helperEpoch, helper, model, dataMetadata, modelMetadata, metadata) def __test__(self, helperEpoch: 'EpochDataContainer', helper, model: 'Model', dataMetadata: 'Data_Metadata', modelMetadata: 'Model_Metadata', metadata: 'Metadata', smoothing: 'Smoothing'): helper.pred = model.getNNModelModule()(helper.inputs) helper.test_loss = model.loss_fn(helper.pred.logits, helper.labels).item() if (__name__ == '__main__'): torch.backends.cudnn.benchmark = True obj = models.GoogLeNet(init_weights=True) #sf.useDeterministic() #sf.modelDetermTest(sf.Metadata, DefaultData_Metadata, DefaultModel_Metadata, DefaultData, VGG16Model, DefaultSmoothing) stat = sf.modelRun(sf.Metadata, dc.DefaultData_Metadata, dc.DefaultModel_Metadata, dc.DefaultDataMNIST, dc.DefaultModelSimpleConv, dc.DefaultSmoothingOscilationGeneralizedMean, obj, load=False) #plt.plot(stat.trainLossArray) #plt.xlabel('Train index') #plt.ylabel('Loss')