Ejemplo n.º 1
0
    def __init__(self, n_downsample, input_dim, dim, style_dim, norm, activ, pad_type):
        super(VggStyleEncoder, self).__init__()
        # self.vgg = models.vgg19(pretrained=True).features
        # VggStyleEncoder(3, input_dim=3, dim=64, int(style_dim/SP_input_nc)=512/8, norm='none', activ=relu, pad_type=reflect)
        vgg19 = models.vgg19(pretrained=False)
        vgg19.load_state_dict(torch.load('/home/ssl/PycharmProjects/ADGAN/data/deepfashion/vgg19-dcbb9e9d.pth'))
        self.vgg = vgg19.features

        for param in self.vgg.parameters():
            param.requires_grad_(False)

        self.conv1 = Conv2dBlock(input_dim, dim, 7, 1, 3, norm=norm, activation=activ, pad_type=pad_type) # 3->64,concat,维度不变
        dim = dim*2
        self.conv2 = Conv2dBlock(dim , dim, 4, 2, 1, norm=norm, activation=activ, pad_type=pad_type) # 128->128,维度减半
        dim = dim*2
        self.conv3 = Conv2dBlock(dim , dim, 4, 2, 1, norm=norm, activation=activ, pad_type=pad_type) # 256->256,维度减半
        dim = dim * 2
        self.conv4 = Conv2dBlock(dim, dim, 4, 2, 1, norm=norm, activation=activ, pad_type=pad_type)  # 512->512,维度减半
        dim = dim * 2#512

        self.model = []
        self.model += [nn.AdaptiveAvgPool2d(1)] # global average pooling
        self.model += [nn.Conv2d(dim, style_dim, 1, 1, 0)]
        self.model = nn.Sequential(*self.model)#(1024,64,1,1)
        self.output_dim = dim
Ejemplo n.º 2
0
 def __init__(self, device='cpu'):
     super(VGG19, self).__init__()
     features = list(vgg19(pretrained=True).features)
     if device == "cuda":
         self.features = nn.ModuleList(features).cuda().eval()
     else:
         self.features = nn.ModuleList(features).eval()
 def __init__(self):
     super(VggFeatures, self).__init__()
     self.vgg_layers = vgg.vgg19(pretrained=True).features
     self.layer_name_mapping = {
         '26': "relu4",
         '35': "relu5",
     }
Ejemplo n.º 4
0
    def __init__(self, i=5, include_max_pool=False):
        super(FeatureExtractor, self).__init__()

        model = vgg19(pretrained=True)

        # Break layers and normalization by Tak-Wai Hui and Wai-Ho Kwok https://github.com/twhui/SRGAN-PyTorch
        children = list(model.features.children())
        max_pool_indices = [
            index for index, m in enumerate(children)
            if isinstance(m, tr.nn.MaxPool2d)
        ]
        target_features = children[:max_pool_indices[
            i - 1] + 1] if include_max_pool else children[:max_pool_indices[i -
                                                                            1]]

        self.features = tr.nn.Sequential(*target_features)

        mean = tr.autograd.Variable(
            tr.Tensor([0.485, 0.456, 0.406]).view(
                1, 3, 1,
                1))  # [0.485-1, 0.456-1, 0.406-1] if input in range [-1,1]
        std = tr.autograd.Variable(
            tr.Tensor([0.229, 0.224, 0.225]).view(
                1, 3, 1,
                1))  # [0.229*2, 0.224*2, 0.225*2] if input in range [-1,1]
        self.register_buffer('mean', mean)
        self.register_buffer('std', std)
Ejemplo n.º 5
0
def get_network(device):
    net = vgg.vgg19(pretrained=True)

    # get features from last layer
    new_classifier = nn.Sequential(*list(net.classifier.children())[:-1])
    net.classifier = new_classifier
    return net.to(device)
Ejemplo n.º 6
0
def _fuse(inputs, model=None, with_exp=False):
    with torch.no_grad():
        if model is None:
            model = vgg19(True)
        model.cuda().eval()

        tc_inputs = []
        relus_acts = []
        for input_img in inputs:
            tc_input = to_pytorch(input_img)

            relus_act, out = get_activation(model, tc_input)

            tc_inputs.append(tc_input)
            relus_acts.append(relus_act)

        saliency_max = None
        idx = 0
        for relus_list in zip(*relus_acts):
            saliency_current, D = fusion_strategy(relus_list, tc_inputs,
                                                  with_exp)
            idx += 1
            if saliency_max is None:
                saliency_max = saliency_current
            else:
                saliency_max = torch.max(saliency_max, saliency_current)

        output = np.squeeze(saliency_max.cpu().numpy())
        if output.ndim == 3:
            output = np.transpose(output, (1, 2, 0))
        return output
Ejemplo n.º 7
0
 def __init__(self,
              feature_layer=34,
              use_bn=False,
              use_input_norm=True,
              device=torch.device('cpu'),
              z_norm=False): #Note: PPON uses cuda instead of CPU
     super(VGGFeatureExtractor, self).__init__()
     if use_bn:
         model = vgg.vgg19_bn(pretrained=True)
     else:
         model = vgg.vgg19(pretrained=True)
     self.use_input_norm = use_input_norm
     if self.use_input_norm:
         if z_norm: # if input in range [-1,1]
             mean = torch.Tensor([0.485-1, 0.456-1, 0.406-1]).view(1, 3, 1, 1).to(device) 
             std = torch.Tensor([0.229*2, 0.224*2, 0.225*2]).view(1, 3, 1, 1).to(device)
         else: # input in range [0,1]
             mean = torch.Tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1).to(device)                 
             std = torch.Tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1).to(device)
         self.register_buffer('mean', mean)
         self.register_buffer('std', std)
     self.features = nn.Sequential(*list(model.features.children())[:(feature_layer + 1)])
     # No need to BP to variable
     for k, v in self.features.named_parameters():
         v.requires_grad = False
Ejemplo n.º 8
0
def vgg(**config):
    dataset = config.pop('dataset', 'imagenet')
    depth = config.pop('depth', 16)
    bn = config.pop('bn', True)

    if dataset == 'imagenet':
        config.setdefault('num_classes', 1000)
        if depth == 11:
            if bn is False:
                return vgg11(pretrained=False, **config)
            else:
                return vgg11_bn(pretrained=False, **config)
        if depth == 13:
            if bn is False:
                return vgg13(pretrained=False, **config)
            else:
                return vgg13_bn(pretrained=False, **config)
        if depth == 16:
            if bn is False:
                return vgg16(pretrained=False, **config)
            else:
                return vgg16_bn(pretrained=False, **config)
        if depth == 19:
            if bn is False:
                return vgg19(pretrained=False, **config)
            else:
                return vgg19_bn(pretrained=False, **config)
    elif dataset == 'cifar10':
        config.setdefault('num_classes', 10)
    elif dataset == 'cifar100':
        config.setdefault('num_classes', 100)
    config.setdefault('batch_norm', bn)
    return VGG(model_name[depth], **config)
Ejemplo n.º 9
0
 def __init__(self):
     super(PerceptualLoss, self).__init__()
     vgg = vgg19(pretrained=True)
     loss_network = nn.Sequential(*list(vgg.features)[:34]).eval()
     for param in loss_network.parameters():
         param.requires_grad = False
     self.loss_network = loss_network
     self.mse_loss = nn.MSELoss()
Ejemplo n.º 10
0
 def __init__(self):
     super().__init__()
     vgg = vgg19(pretrained=True)
     features_slice = vgg.features[:35]
     perceptual_network = nn.Sequential(*features_slice).eval()
     for param in perceptual_network.parameters():
         param.requires_grad = False
     self.perceptual_network = perceptual_network
Ejemplo n.º 11
0
 def __init__(self):
     super(VGGFeatureExtractor, self).__init__()
     vgg = vgg19(pretrained=True)
     loss_network = nn.Sequential(*list(vgg.features)[:18]).eval()
     for param in loss_network.parameters():
         param.requires_grad = False
     self.loss_network = loss_network
     self.perception_loss = nn.MSELoss()
 def __init__(self):
     super(VGG19,self).__init__()
     self.vgg = vgg19(pretrained=True)
     self.encoder_1 = self.vgg.features[0:4]
     self.encoder_2 = self.vgg.features[4:9]
     self.encoder_3 = self.vgg.features[9:18]
     self.encoder_4 = self.vgg.features[18:27]
     self.encoder_5 = self.vgg.features[27:36]
Ejemplo n.º 13
0
    def __init__(self):
        super().__init__()

        vgg = vgg19(pretrained=True)
        loss_network = nn.Sequential(*list(vgg.features)[:35]).eval()
        for param in loss_network.parameters():
            param.requires_grad = False
        self.loss_network = loss_network
        self.l1_loss = nn.L1Loss()
Ejemplo n.º 14
0
 def __init__(self, device):
     super(VGGLoss, self).__init__()
     vgg19_model = vgg19(pretrained=True, progress=False)
     feature_extractor = nn.Sequential(
         *list(vgg19_model.features)[:31]).eval()
     for param in feature_extractor.parameters():
         param.requires_grad = False
     self.feature_extractor = feature_extractor.to(device)
     self.mse = nn.MSELoss()
Ejemplo n.º 15
0
 def __init__(self):
     super(LossNetwork, self).__init__()
     self.vgg_layers = vgg.vgg19(pretrained=True).features
     self.layer_name_mapping = {
         '3': "relu1",
         '8': "relu2",
         '13': "relu3",
         '22': "relu4",
         '31': "relu5",  #1_2 to 5_2
     }
Ejemplo n.º 16
0
 def __init__(self):
     super(PerceptualLossVGG19, self).__init__()
     vgg = vgg19(pretrained=True)
     loss_network = nn.Sequential(*list(vgg.features)[:36]).eval()
     if torch.cuda.is_available():
         loss_network = loss_network.cuda()
     for param in loss_network.parameters():
         param.requires_grad = False
     self.loss_network = loss_network
     self.mse_loss = nn.MSELoss()
Ejemplo n.º 17
0
 def __init__(self):
     super(LossNetwork, self).__init__()
     self.vgg_layers = vgg.vgg19(pretrained=True).features
     self.detach_layers = {
         '3': "relu1",
         '8': "relu2",
         '17': "relu3",
         '26': "relu4",
         '35': "relu5",
     }
 def __init__(self):
     super(VggNetwork, self).__init__()
     self.vgg_layers = vgg.vgg19(pretrained=True).features
     self.layer_name_mapping = {
         '3': "relu1",
         '8': "relu2",
         '17': "relu3",
         '26': "relu4",
         '35': "relu5",
     }
 def __init__(self):
     super(VggNetwork, self).__init__()
     self.vgg_layers = vgg.vgg19(pretrained=True).features
     self.layer_name_mapping = {
         '2': "conv1",
         '7': "conv2",
         '16': "conv3",
         '25': "conv4",
         '34': "conv5",
     }
Ejemplo n.º 20
0
 def __init__(self):
     super(SourceNetwork, self).__init__()
     vgg = vgg19(True)
     layers = vgg.features
     # print(layers)
     self.layer1 = layers[:5]
     self.layer2 = layers[5:10]
     self.layer3 = layers[10:19]
     self.layer4 = layers[19:28]
     self.layer5 = layers[28:]
Ejemplo n.º 21
0
    def __init__(self, n_downsample, input_dim, dim, style_dim, norm, activ,
                 pad_type):
        super(VggStyleEncoder, self).__init__()
        # self.vgg = models.vgg19(pretrained=True).features
        vgg19 = models.vgg19(pretrained=False)
        vgg19.load_state_dict(
            torch.load('/nitthilan/data/ADGAN/data/vgg19-dcbb9e9d.pth'))
        self.vgg = vgg19.features

        for param in self.vgg.parameters():
            param.requires_grad_(False)

        self.conv1 = Conv2dBlock(input_dim,
                                 dim,
                                 7,
                                 1,
                                 3,
                                 norm=norm,
                                 activation=activ,
                                 pad_type=pad_type)  # 3->64,concat
        dim = dim * 2
        self.conv2 = Conv2dBlock(dim,
                                 dim,
                                 4,
                                 2,
                                 1,
                                 norm=norm,
                                 activation=activ,
                                 pad_type=pad_type)  # 128->128
        dim = dim * 2
        self.conv3 = Conv2dBlock(dim,
                                 dim,
                                 4,
                                 2,
                                 1,
                                 norm=norm,
                                 activation=activ,
                                 pad_type=pad_type)  # 256->256
        dim = dim * 2
        self.conv4 = Conv2dBlock(dim,
                                 dim,
                                 4,
                                 2,
                                 1,
                                 norm=norm,
                                 activation=activ,
                                 pad_type=pad_type)  # 512->512
        dim = dim * 2

        self.model = []
        self.model += [nn.AdaptiveAvgPool2d(1)]  # global average pooling
        self.model += [nn.Conv2d(dim, style_dim, 1, 1, 0)]
        self.model = nn.Sequential(*self.model)
        self.output_dim = dim
Ejemplo n.º 22
0
 def __init__(self):
     super(GeneratorLoss, self).__init__()
     vgg = vgg19(pretrained=True)
     loss_network = nn.Sequential(*list(vgg.features)[:35]).eval()
     for param in loss_network.parameters():
         param.requires_grad = False
     self.loss_network = loss_network
     self.mse_loss = nn.MSELoss()
     self.tv_loss = TVLoss()
     self.content_loss = nn.L1Loss()
     self.text_loss = TextureLoss()
Ejemplo n.º 23
0
 def __init__(self, lambda1, lambda2, lambda3):
     super(GeneratorLoss, self).__init__()
     vgg = vgg19(pretrained=True)
     self.lambda1 = lambda1
     self.lambda2 = lambda2
     self.lambda3 = lambda3
     loss_network = nn.Sequential(*list(vgg.features)[:37]).eval()
     for param in loss_network.parameters():
         param.requires_grad = False
     self.loss_network = loss_network
     self.mse_loss = nn.MSELoss()
Ejemplo n.º 24
0
def vgg19_bn_planet(pretrained=True):
    net = vgg19(num_classes=17)
    if pretrained:
        state_dict = net.state_dict()
        # load pretrained dictionary
        pretrained_dict = model_zoo.load_url(model_urls['vgg19'])
        state_dict.update({
            key: pretrained_dict[key]
            for key in state_dict if 'classifier' not in key
        })
        net.load_state_dict(state_dict)
    return net
Ejemplo n.º 25
0
 def __init__(self):
     super(PerceptualLoss, self).__init__()
     self.model = (vgg19(pretrained=True).to(device))
     trainable_(self.model, False)
     self.loss = nn.MSELoss().to(device)
     self.vgg_layers = self.model.features
     self.layer_names = {
         '0': 'conv1_1',
         '3': 'relu1_2',
         '6': 'relu2_1',
         '8': 'relu2_2',
         '11': 'relu3_1'
     }
Ejemplo n.º 26
0
    def __init__(self):
        super(LossNetwork, self).__init__()
        vggn = vgg.vgg19(pretrained=True)
        vggn.features[0] = nn.Conv2d(1, 64, kernel_size=3, padding=1)

        self.vgg_layers = vggn.features
        self.layer_name_mapping = {
            '3': "relu1",
            '8': "relu2",
            '17': "relu3",
            '26': "relu4",
            '35': "relu5",
        }
Ejemplo n.º 27
0
	def __init__(self, args):
		super(GeneratorLoss, self).__init__()
		# note that vgg is for RGB image, hence not compatible with gray-scale
		self.vgg = vgg19(pretrained=True)
		self.loss_network = nn.Sequential(*list(self.vgg.features)[:35]).eval()
		for param in self.loss_network.parameters():
			param.requires_grad = False
		# average over all the pixels in the batch
		self.mse_loss = nn.MSELoss(reduction='mean')
		self.tv_loss = TVLoss()
		self.bce_loss = nn.BCELoss(reduction='mean')

		self.adversarial_weight = args.gamma
		self.perception_weight = args.sigma
Ejemplo n.º 28
0
    def __init__(self, layers = [8, 17, 26, 35], replace_pooling = False):
        super(TextureLoss, self).__init__()
        self.layers = layers
        self.model = vgg19(pretrained=True).features
        
        if replace_pooling:
            self.model._modules['4'] = nn.AvgPool2d((2,2), (2,2), (1,1))
            self.model._modules['9'] = nn.AvgPool2d((2,2), (2,2), (1,1))
            self.model._modules['18'] =nn.AvgPool2d((2,2), (2,2), (1,1))
            self.model._modules['27'] =nn.AvgPool2d((2,2), (2,2), (1,1))
            self.model._modules['36'] = nn.AvgPool2d((2,2), (2,2), (1,1))

        for param in self.model.parameters():
            param.requires_grad = False
Ejemplo n.º 29
0
    def __init__(self,
                 weight_perception=0.006,
                 weight_adversarial=0.001,
                 weight_image=1,
                 network="vgg16"):
        super(GeneratorLoss, self).__init__()
        self.network = network
        self.weight_image = weight_image
        self.weight_adversarial = weight_adversarial
        self.weight_perception = weight_perception
        if network == "vgg16":
            pretrained_net = vgg16(pretrained=True)
            loss_network = nn.Sequential(
                *list(pretrained_net.features)[:31]).eval()
        elif network == "vgg19":
            pretrained_net = vgg19(pretrained=True)
            loss_network = nn.Sequential(
                *list(pretrained_net.features)[:37]).eval()
        elif network == "resnet101":
            pretrained_net = resnet101(pretrained=True)
            loss_network = nn.Sequential(
                *list(pretrained_net.classifier.children())[:-1]).eval()
        elif network == "vgg16vgg19":
            pretrained_net_vgg16 = vgg16(pretrained=True)
            loss_network = nn.Sequential(
                *list(pretrained_net_vgg16.features)[:31]).eval()
            pretrained_net_vgg19 = vgg19(pretrained=True)
            loss_network_vgg19 = nn.Sequential(
                *list(pretrained_net_vgg19.features)[:27]).eval()
            for param in loss_network_vgg19.parameters():
                param.requires_grad = False
            self.loss_network_vgg19 = loss_network_vgg19

        for param in loss_network.parameters():
            param.requires_grad = False
        self.loss_network = loss_network
        self.mse_loss = nn.MSELoss()
Ejemplo n.º 30
0
    def __init__(self):
        super().__init__()
        features = vgg19(pretrained=True).features[:30]
        self.features = nn.ModuleList(list(features)).eval().to(device)

        self.mean = torch.tensor([0.485, 0.456, 0.406]).view(-1, 1,
                                                             1).to(device)
        self.std = torch.tensor([0.229, 0.224, 0.225]).view(-1, 1,
                                                            1).to(device)

        for param in self.features.parameters():
            param.requires_grad = False

        self.mean.requires_grad = False
        self.std.requires_grad = False
Ejemplo n.º 31
0
 def test_vgg19(self):
     # VGG 19-layer model (configuration "E")
     x = Variable(torch.randn(BATCH_SIZE, 3, 224, 224).fill_(1.0))
     self.exportTest(toC(vgg19()), toC(x))
Ejemplo n.º 32
0
 def test_vgg19(self):
     state_dict = model_zoo.load_url(model_urls['vgg19'], progress=False)
     self.run_model_test(vgg19(), train=False, batch_size=BATCH_SIZE,
                         state_dict=state_dict)