This class implements CUT and FastCUT model, described in the paper Contrastive Learning for Unpaired Image-to-Image Translation Taesung Park, Alexei A. Efros, Richard Zhang, Jun-Yan Zhu ECCV, 2020 The code borrows heavily from the PyTorch implementation of CycleGAN https://git
| 7 | |
| 8 | |
| 9 | class CUTModel(BaseModel): |
| 10 | """ This class implements CUT and FastCUT model, described in the paper |
| 11 | Contrastive Learning for Unpaired Image-to-Image Translation |
| 12 | Taesung Park, Alexei A. Efros, Richard Zhang, Jun-Yan Zhu |
| 13 | ECCV, 2020 |
| 14 | |
| 15 | The code borrows heavily from the PyTorch implementation of CycleGAN |
| 16 | https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix |
| 17 | """ |
| 18 | @staticmethod |
| 19 | def modify_commandline_options(parser, is_train=True): |
| 20 | """ Configures options specific for CUT model |
| 21 | """ |
| 22 | parser.add_argument('--lambda_GAN', type=float, default=1.0, help='weight for GAN loss:GAN(G(X))') |
| 23 | parser.add_argument('--lambda_NCE', type=float, default=1.0, help='weight for NCE loss: NCE(G(X), X)') |
| 24 | parser.add_argument('--lambda_IDT', type=float, default=10.0, help='weight for NCE loss: NCE(G(X), X)') |
| 25 | parser.add_argument('--nce_idt', type=util.str2bool, nargs='?', const=True, default=False, help='use NCE loss for identity mapping: NCE(G(Y), Y))') |
| 26 | parser.add_argument('--nce_layers', type=str, default='0,4,8,12,16', help='compute NCE loss on which layers') |
| 27 | parser.add_argument('--nce_includes_all_negatives_from_minibatch', |
| 28 | type=util.str2bool, nargs='?', const=True, default=False, |
| 29 | help='(used for single image translation) If True, include the negatives from the other samples of the minibatch when computing the contrastive loss. Please see models/patchnce.py for more details.') |
| 30 | parser.add_argument('--netF', type=str, default='mlp_sample', choices=['sample', 'reshape', 'mlp_sample'], help='how to downsample the feature map') |
| 31 | parser.add_argument('--netF_nc', type=int, default=256) |
| 32 | parser.add_argument('--nce_T', type=float, default=0.07, help='temperature for NCE loss') |
| 33 | parser.add_argument('--num_patches', type=int, default=256, help='number of patches per layer') |
| 34 | parser.add_argument('--flip_equivariance', |
| 35 | type=util.str2bool, nargs='?', const=True, default=False, |
| 36 | help="Enforce flip-equivariance as additional regularization. It's used by FastCUT, but not CUT") |
| 37 | |
| 38 | parser.set_defaults(pool_size=0) # no image pooling |
| 39 | |
| 40 | opt, _ = parser.parse_known_args() |
| 41 | parser.set_defaults(nce_idt=True, lambda_NCE=1.0) |
| 42 | |
| 43 | return parser |
| 44 | |
| 45 | |
| 46 | def __init__(self, opt): |
| 47 | BaseModel.__init__(self, opt) |
| 48 | |
| 49 | # specify the training losses you want to print out. |
| 50 | # The training/test scripts will call <BaseModel.get_current_losses> |
| 51 | self.loss_names = ['G_GAN', 'D_real', 'D_fake', 'G', 'NCE'] |
| 52 | self.visual_names = ['real_A', 'fake_B', 'real_B'] |
| 53 | self.nce_layers = [int(i) for i in self.opt.nce_layers.split(',')] |
| 54 | |
| 55 | if opt.nce_idt and self.isTrain: |
| 56 | self.loss_names += ['NCE_Y'] |
| 57 | self.visual_names += ['idt_B'] |
| 58 | |
| 59 | if self.isTrain: |
| 60 | self.model_names = ['G', 'F', 'D'] |
| 61 | else: # during test time, only load G |
| 62 | self.model_names = ['G'] |
| 63 | |
| 64 | # define networks (both generator and discriminator) |
| 65 | self.netG = networks.define_G(opt.input_nc, opt.output_nc, opt.ngf, opt.netG, opt.normG, not opt.no_dropout, opt.init_type, opt.init_gain, opt.no_antialias, opt.no_antialias_up, self.gpu_ids, opt) |
| 66 | self.netF = networks.define_F(opt.input_nc, opt.netF, opt.normG, not opt.no_dropout, opt.init_type, opt.init_gain, opt.no_antialias, self.gpu_ids, opt) |
nothing calls this directly
no outgoing calls
no test coverage detected