def vgg_face(pretrained=False, **kwargs): if pretrained: kwargs['init_weights'] = False model = vgg.VGG(vgg.make_layers(vgg.cfg['D'], batch_norm=False), num_classes=2622, **kwargs) if pretrained: model.load_state_dict(vgg_face_state_dict()) return model
def vgg19_bn(pretrained=False, model_dir=None, **kwargs): """VGG 19-layer model (configuration 'E') with batch normalization Args: pretrained (bool): If True, returns a model pre-trained on ImageNet model_dir (None, str): If not None, specifies the directory for the model pickle. """ if pretrained: kwargs['init_weights'] = False model = vgg.VGG(vgg.make_layers(vgg.cfg['E'], batch_norm=True), **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(vgg.model_urls['vgg19_bn'], model_dir=model_dir)) return model
def vgg19(vgg_path, pretrained=False, **kwargs): """VGG 19-layer model (configuration "E") Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ cfg_E = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'] model = vgg.VGG(vgg.make_layers(cfg_E), **kwargs) if pretrained: # model.load_state_dict(model_zoo.load_url(model_urls['vgg19'])) vgg_model = torch.load(vgg_path, map_location='cpu') model.load_state_dict(vgg_model) return model
def __init__(self): super(TinyImagenet_VGG, self).__init__() # Based on the imagenet normalization params. self.offset = 0.44900 self.multiplier = 4.42477 self.cfg = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'] self.model = VGG.VGG(VGG.make_layers(self.cfg, batch_norm=True), num_classes=200) # TinyImagenet would have a different sized feature map. self.model.classifier = nn.Sequential( nn.Linear(512 * 2 * 2, 4096), nn.ReLU(True), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(True), nn.Dropout(), nn.Linear(4096, 200), ) self.model._initialize_weights()
def load_vgg_from_local(arch='vgg19', cfg='E', batch_norm=False, pretrained=True, vgg_dir=None, parallel=True, **kwargs): vgg = vgglib.VGG(vgglib.make_layers(cfgs[cfg], batch_norm=batch_norm), **kwargs) vgg.load_state_dict( model_zoo.load_url(url=VGG_URL, model_dir='/gpub/temp/imagenet2012/hdf5')) vgg = (vgg.eval()).cuda() if parallel: print("Parallel VGG model...") vgg = torch.nn.DataParallel(vgg) return vgg
def __init__(self): super(MNIST_VGG, self).__init__() # Based on the imagenet normalization params. self.offset = 0.44900 self.multiplier = 4.42477 # Reduced VGG16. self.cfg = [64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M'] self.model = VGG.VGG(self.make_layers(self.cfg, batch_norm=True), num_classes=10) # MNIST would have a different sized feature map. self.model.classifier = nn.Sequential( nn.Linear(512 * 1 * 1, 256), nn.ReLU(True), nn.Dropout(), nn.Linear(256, 256), nn.ReLU(True), nn.Dropout(), nn.Linear(256, 10), ) self.model._initialize_weights()
def vggface(weight_path): network = vgg.VGG(vgg.make_layers(vgg.cfgs['D'], batch_norm=False), num_classes=2622) default = torch.load(weight_path) state_dict = OrderedDict({ 'features.0.weight': default['conv1_1.weight'], 'features.0.bias': default['conv1_1.bias'], 'features.2.weight': default['conv1_2.weight'], 'features.2.bias': default['conv1_2.bias'], 'features.5.weight': default['conv2_1.weight'], 'features.5.bias': default['conv2_1.bias'], 'features.7.weight': default['conv2_2.weight'], 'features.7.bias': default['conv2_2.bias'], 'features.10.weight': default['conv3_1.weight'], 'features.10.bias': default['conv3_1.bias'], 'features.12.weight': default['conv3_2.weight'], 'features.12.bias': default['conv3_2.bias'], 'features.14.weight': default['conv3_3.weight'], 'features.14.bias': default['conv3_3.bias'], 'features.17.weight': default['conv4_1.weight'], 'features.17.bias': default['conv4_1.bias'], 'features.19.weight': default['conv4_2.weight'], 'features.19.bias': default['conv4_2.bias'], 'features.21.weight': default['conv4_3.weight'], 'features.21.bias': default['conv4_3.bias'], 'features.24.weight': default['conv5_1.weight'], 'features.24.bias': default['conv5_1.bias'], 'features.26.weight': default['conv5_2.weight'], 'features.26.bias': default['conv5_2.bias'], 'features.28.weight': default['conv5_3.weight'], 'features.28.bias': default['conv5_3.bias'], 'classifier.0.weight': default['fc6.weight'], 'classifier.0.bias': default['fc6.bias'], 'classifier.3.weight': default['fc7.weight'], 'classifier.3.bias': default['fc7.bias'], 'classifier.6.weight': default['fc8.weight'], 'classifier.6.bias': default['fc8.bias'] }) network.load_state_dict(state_dict) return network
def __init__(self, config='D', convs_per_block=[2, 2, 3, 3, 3], batch_norm=True, vgg_state_dict=None): super().__init__() source_vgg = vgg.VGG(vgg.make_layers(vgg.cfgs[config], batch_norm=batch_norm), init_weights=vgg_state_dict is None) if vgg_state_dict is not None: source_vgg.load_state_dict(vgg_state_dict) layers_per_conv = 3 if batch_norm else 2 # conv, batch norm (conditionally), relu layers_per_block = [ convs * layers_per_conv + 1 for convs in convs_per_block ] # +1 for max pool cutoff_1 = sum( layers_per_block[:-1]) - 1 # last relu of second-to-last block cutoff_2 = sum(layers_per_block) - 1 # last relu of last block self.block1 = source_vgg.features[:cutoff_1] self.block2 = source_vgg.features[cutoff_1:cutoff_2]
def __init__(self, scale, classes, epochs, split_size=0, init_weights=True): super(Scaled_VGG, self).__init__() self.dev1 = torch.device('cuda:0') self.dev2 = torch.device('cuda:0') self.split_size = split_size # Based on the imagenet normalization params. self.offset = 0.44900 self.multiplier = 4.42477 small_cfg = [64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M'] middle_cfg = [ 64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512 ] large_cfg = [ 64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M' ] classifier_width = 4096 self.scale = scale if scale[1] < 32: self.cfg = small_cfg classifier_width = 256 if scale[1] == 32: self.cfg = middle_cfg if scale[1] > 32: self.cfg = large_cfg maxpool_count = self.cfg.count('M') scale_factor = 2**maxpool_count channels = scale[0] self.model = VGG.VGG(self.make_layers(self.cfg, channels, batch_norm=True), num_classes=classes) # would have a different sized feature map. poolscale = ((int)(scale[0] / scale_factor), (int)(scale[1] / scale_factor), (int)(scale[2] / scale_factor)) self.model.avgpool = nn.AdaptiveAvgPool2d((poolscale[1], poolscale[2])) self.model.classifier = nn.Sequential( nn.Linear(512 * poolscale[1] * poolscale[2], classifier_width), nn.ReLU(True), nn.Dropout(), nn.Linear(classifier_width, classifier_width), nn.ReLU(True), nn.Dropout(), nn.Linear(classifier_width, classes), ) self.model = self.model.to(self.dev1) torchinfo.summary(self.model, col_names=[ "kernel_size", "input_size", "output_size", "num_params" ], input_size=(32, self.scale[0], self.scale[1], self.scale[2])) if (init_weights): self.model._initialize_weights() self.epochs = epochs
def __init__(self, num_classes=100, model_type="vgg_without_maxpool", pooling="average", poolingshape=7, middleshape=4096, rotational="false", dropout_prob=0.5, deepness=2, cnn_bn_flag=True, fc_bn_flag=True, fc_do_flag=True): super(BatchnormDropoutWithVGG16, self).__init__() cfgs = { 'vgg_with_maxpool': [ 64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M' ], 'vgg_without_maxpool': [ 64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512 ], } """ vgg16 """ vgg = vggmodel.VGG( vggmodel.make_layers(cfgs[model_type], batch_norm=cnn_bn_flag)) self.vgg = nn.Sequential(*list(vgg.children())[:-2]) """ global average pooling or Max Pooling """ if pooling == "average": self.pool = nn.AdaptiveAvgPool2d((poolingshape, poolingshape)) elif pooling == "max": self.pool = nn.AdaptiveMaxPool2d((poolingshape, poolingshape)) else: raise ValueError("poolingの値が不正です") """ FCを定義""" in_shape = 512 * poolingshape * poolingshape if rotational == "false": fc = [nn.Linear(in_shape, middleshape)] if fc_bn_flag: fc.append(nn.BatchNorm1d(middleshape)) fc.append(nn.ReLU(middleshape)) if fc_do_flag: fc.append(nn.Dropout(p=dropout_prob)) if deepness > 1: for _ in range(deepness - 1): fc.append(nn.Linear(middleshape, middleshape)) if fc_bn_flag: fc.append(nn.BatchNorm1d(middleshape)) fc.append(nn.ReLU(middleshape)) if fc_do_flag: fc.append(nn.Dropout(p=dropout_prob)) fc.append(nn.Linear(middleshape, num_classes)) elif rotational == "true": fc = [RotationalLinear(nn.Linear(in_shape, middleshape))] if fc_bn_flag: fc.append(nn.BatchNorm1d(middleshape)) fc.append(nn.ReLU(middleshape)) if fc_do_flag: fc.append(nn.Dropout(p=dropout_prob)) if deepness > 1: for _ in range(deepness - 1): fc.append( RotationalLinear(nn.Linear(middleshape, middleshape))) if fc_bn_flag: fc.append(nn.BatchNorm1d(middleshape)) fc.append(nn.ReLU(middleshape)) if fc_do_flag: fc.append(nn.Dropout(p=dropout_prob)) fc.append(nn.Linear(middleshape, num_classes)) elif rotational == "none": fc = nn.Sequential(nn.Linear(in_shape, num_classes), ) else: raise ValueError("引数fcの値が不正です") self.fc = nn.Sequential() for i, layer in enumerate(fc): self.fc.add_module(str(i), layer)