def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # loss and optimizer
        self.optimizer = optim.Adam(self.model.parameters(),
                                    lr=self.config["lr"])
        self.cuda = True if self.config["cuda"] and torch.cuda.is_available(
        ) else False
        self.device = "cuda" if self.cuda else "cpu"
        self.mseloss = torch.nn.MSELoss()
        self.L1loss = torch.nn.L1Loss()
        self.sigmoid = torch.nn.Sigmoid()
        # vgg loss
        if self.config["losses"]["vgg"]:
            self.vggL1 = VGGLossWithL1(
                gpu_ids=[0],
                l1_alpha=self.config["losses"]["vgg_l1_alpha"],
                vgg_alpha=self.config["losses"]["vgg_alpha"]).to(self.device)

        # initalize perceptual loss if possible
        if self.config["losses"]["perceptual"]:
            net = self.config["losses"]["perceptual_network"]
            assert net in [
                "alex", "squeeze", "vgg"
            ], f"Perceptual network needs to be 'alex', 'squeeze' or 'vgg', got {net}"
            self.perceptual_loss = PerceptualLoss(model='net-lin',
                                                  net=net,
                                                  use_gpu=self.cuda,
                                                  spatial=True).to(self.device)
        if self.cuda:
            self.model.cuda()
Exemple #2
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.logger = get_logger(self)
        # loss and optimizer
        self.optimizer = optim.Adam(self.model.parameters(), lr=self.config["lr"])
        # Initialize Loss functions
        self.mse_loss = torch.nn.MSELoss()
        # self.mse_instance = MSELossInstances()
        # self.l1_instance = L1LossInstances()
        self.cuda = True if self.config["cuda"] and torch.cuda.is_available() else False
        self.device = "cuda" if self.cuda else "cpu"
        # Imagenet Mean
        self.mean = [0.485, 0.456, 0.406]
        self.std = [0.229, 0.224, 0.225]
        self.normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

        # self.normalize = torchvision.transforms.Normalize(mean=self.mean, std=self.std)
        if self.cuda:
            self.model.cuda()

        self.freeze_encoder()

        # vgg loss
        if self.config["losses"]["vgg"]:
            self.vggL1 = VGGLossWithL1(gpu_ids=[0],
                                       l1_alpha=self.config["losses"]["vgg_l1_alpha"],
                                       vgg_alpha=self.config["losses"]["vgg_alpha"]).to(self.device)

        # initalize perceptual loss if possible
        if self.config["losses"]["perceptual"]:
            net = self.config["losses"]["perceptual_network"]
            assert net in ["alex", "squeeze",
                           "vgg"], f"Perceptual network needs to be 'alex', 'squeeze' or 'vgg', got {net}"
            self.perceptual_loss = PerceptualLoss(model='net-lin', net=net, use_gpu=self.cuda, spatial=False).to(
                self.device)
 def __init__(self, config):
     super().__init__()
     self.config = config
     self.lambda_init = retrieve(config, "LossConstrained/lambda_init", default=100000.0)
     self.register_buffer("lambda_", torch.ones(size=()) * self.lambda_init)
     self.mu = retrieve(config, "LossConstrained/mu", default=0.01)  # anpassung loss
     self.eps = retrieve(config, "LossConstrained/eps", default=0.4)  # max rec loss # 30.72)  # mean of 0.01
     net = self.config["losses"]["perceptual_network"]
     self.perceptual_loss = PerceptualLoss(model='net-lin', net=net, use_gpu=True, spatial=False).to("cuda")
     self.device = "cuda"
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # loss and optimizer
        self.optimizer = optim.Adam(self.model.parameters(), lr=self.config["lr"])
        self.cuda = True if self.config["cuda"] and torch.cuda.is_available() else False
        self.device = "cuda" if self.cuda else "cpu"
        self.variational = False
        self.encoder_2 = False
        self.sigmoid = torch.nn.Sigmoid()

        # # initalize perceptual loss if possible
        if self.config["losses"]["perceptual"]:
            net = self.config["losses"]["perceptual_network"]
            assert net in ["alex", "squeeze",
                           "vgg"], f"Perceptual network needs to be 'alex', 'squeeze' or 'vgg', got {net}"
            self.perceptual_loss = PerceptualLoss(model='net-lin', net=net, use_gpu=self.cuda, spatial=False).to(
                self.device)
        if self.cuda:
            self.model.cuda()
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # loss and optimizer
        self.optimizer = optim.Adam(self.model.parameters(), lr=self.config["lr"])
        self.cuda = True if self.config["cuda"] and torch.cuda.is_available() else False
        self.device = "cuda" if self.cuda else "cpu"
        self.variational = self.config["variational"]["active"]
        self.encoder_2 = True if self.config["encoder_2"] else False
        self.kl_weight = self.config["variational"]["kl_weight"]
        if retrieve(self.config, "classifier/active", default=False):
            self.classifier = ResnetTorchVisionClass(self.config).to(self.device)
            # Load pretrained classifier into memory
            self.logger.info(f"Loading weights for classifier from {self.config['classifier']['weights']}")
            state_dict = torch.load(self.config["classifier"]["weights"], map_location=self.device)["model"]
            self.classifier.load_state_dict(state_dict)

        try:
            self.start_step, self.stop_step, self.start_weight, self.stop_weight = self.config["variational"][
                                                                                       "start_step"], \
                                                                                   self.config[
                                                                                       "num_steps"], self.kl_weight, \
                                                                                   self.config["variational"][
                                                                                       "stop_weight"]
        except:
            print("Some infos not found")
        self.loss_constrained = LossConstrained(self.config)
        self.sigmoid = torch.nn.Sigmoid()
        # # initalize perceptual loss if possible
        if self.config["losses"]["perceptual"]:
            net = self.config["losses"]["perceptual_network"]
            assert net in ["alex", "squeeze",
                           "vgg"], f"Perceptual network needs to be 'alex', 'squeeze' or 'vgg', got {net}"
            self.perceptual_loss = PerceptualLoss(model='net-lin', net=net, use_gpu=self.cuda, spatial=False).to(
                self.device)
        if self.cuda:
            self.model.cuda()