(self, x_start, cond, t, origin_prompt=None, image_input=None, mask=None, noise=None, mask_background_image=None)
| 345 | return opt |
| 346 | |
| 347 | def p_losses(self, x_start, cond, t, origin_prompt=None, image_input=None, mask=None, noise=None, mask_background_image=None): |
| 348 | |
| 349 | noise = default(noise, lambda: torch.randn_like(x_start)) |
| 350 | x_noisy = self.q_sample(x_start=x_start, t=t, noise=noise) |
| 351 | model_output = self.apply_model(x_noisy, t, cond) |
| 352 | |
| 353 | loss_dict = {} |
| 354 | prefix = 'train' if self.training else 'val' |
| 355 | |
| 356 | if self.parameterization == "x0": |
| 357 | target = x_start |
| 358 | elif self.parameterization == "eps": |
| 359 | target = noise |
| 360 | else: |
| 361 | raise NotImplementedError() |
| 362 | |
| 363 | loss_simple = self.get_loss(model_output, target, mean=False) |
| 364 | |
| 365 | if mask is not None: |
| 366 | loss_simple = (loss_simple*mask).sum([1, 2, 3])/mask.sum([1, 2, 3]) |
| 367 | else: |
| 368 | loss_simple = loss_simple.mean([1, 2, 3]) |
| 369 | loss_dict.update({f'{prefix}/loss_simple': loss_simple.mean()}) |
| 370 | |
| 371 | logvar_t = (self.logvar.to(self.device))[t] |
| 372 | loss = loss_simple / torch.exp(logvar_t) + logvar_t |
| 373 | # loss = loss_simple / torch.exp(self.logvar) + self.logvar |
| 374 | if self.learn_logvar: |
| 375 | loss_dict.update({f'{prefix}/loss_gamma': loss.mean()}) |
| 376 | loss_dict.update({'logvar': self.logvar.data.mean()}) |
| 377 | |
| 378 | loss = self.l_simple_weight * loss.mean() |
| 379 | |
| 380 | if self.controller is not None and ('<new1>' in origin_prompt[0] or '<new1>' in origin_prompt[1]): |
| 381 | |
| 382 | batch_encoding = self.cond_stage_model.tokenizer(origin_prompt, truncation=True, max_length=self.cond_stage_model.max_length, return_length=True, |
| 383 | return_overflowing_tokens=False, padding="max_length", return_tensors="pt") |
| 384 | tokens = batch_encoding["input_ids"].to(self.device) |
| 385 | len_add_token = len(self.cond_stage_model.modifier_token_id) |
| 386 | |
| 387 | def get_token_indices(tokens, token_id): |
| 388 | indices = tokens == token_id |
| 389 | indices = (indices*1).unsqueeze(-1) |
| 390 | index_token = (indices == 1).nonzero() |
| 391 | index_token = index_token.tolist() |
| 392 | return index_token |
| 393 | |
| 394 | index_token = get_token_indices(tokens, self.cond_stage_model.modifier_token_id[-len_add_token]) |
| 395 | index_token_new2 = get_token_indices(tokens, self.cond_stage_model.modifier_token_id[-1]) |
| 396 | |
| 397 | if self.new3_subject: |
| 398 | index_token_new3 = get_token_indices(tokens, self.cond_stage_model.modifier_token_id[-2]) |
| 399 | |
| 400 | del batch_encoding |
| 401 | |
| 402 | out_new1, out_foreground, out_new2, out_subject2 = {0:[], 1:[]}, {0:[], 1:[]}, {0:[], 1:[]}, {0:[], 1:[]} |
| 403 | if self.new3_subject: |
| 404 | out_new3, out_subject3 = {0:[], 1:[]}, {0:[], 1:[]} |
nothing calls this directly
no test coverage detected