Example #1
0
 def __init__(self, opt):
     BaseModel.__init__(self, opt)
Example #2
0
 def __init__(self, *args, **kwargs):
     """Initiating the Review class"""
     if (args and type(args) is dict):
         BaseModel.__init__(self, args[0])
     else:
         BaseModel.__init__(self)
Example #3
0
 def __init__(self, **kwargs):
     """ constructor sends the values for the object to be created from BaseModel """
     BaseModel.__init__(self, **kwargs)
Example #4
0
    def __init__(self, model_params):
        BaseModel.__init__(self, model_params)
        self.data_iter = DataIter(self.model_params.batch_size)

        self.tar_video = tf.placeholder(
            tf.float32, [None, 64, self.model_params.visual_feat_dim])  # 64
        self.tar_img = tf.placeholder(tf.float32,
                                      [None, self.model_params.word_vec_dim])
        self.neg_img = tf.placeholder(tf.int32, [
            self.model_params.batch_size,
        ])
        self.y = tf.placeholder(tf.int32,
                                [self.model_params.batch_size, 18])  # 10
        self.y_single = tf.placeholder(tf.int32,
                                       [self.model_params.batch_size, 1])
        self.l = tf.placeholder(tf.float32, [])
        self.trans = tf.placeholder(tf.float32, [None, 64, 64])

        self.emb_matrix = self.matrix_embed(
            self.tar_video)  # batch_size*(100,64)
        self.emb_w = self.label_embed(self.tar_img)
        self.emb_v = self.visual_embed(self.emb_matrix[0])
        for i in range(1, self.model_params.batch_size):
            M = self.visual_embed(self.emb_matrix[i], reuse=True)
            self.emb_v = tf.concat([self.emb_v, M], axis=0)

        # triplet loss
        idata = []
        for i in range(self.model_params.batch_size):
            em_w = self.emb_w[i] / tf.norm(self.emb_w[i])
            x = tf.matmul(tf.reshape(em_w, [64, 1]), tf.reshape(em_w, [1, 64]))
            y = tf.concat([x[k] for k in range(64)], axis=0)
            idata.append(y)

        self.trans, tran2 = self.transpose(self.emb_matrix)
        tran2 = tf.stop_gradient(tran2)
        margin = self.model_params.margin
        self.triplet_loss = self.tripletloss(idata,
                                             self.trans / tf.norm(self.trans),
                                             margin)
        self.logits_w = self.label_classifier(self.emb_w)
        self.logits_v = self.label_classifier(self.emb_v, reuse=True)
        self.label_loss1 = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=self.y,
                                                    logits=self.logits_v))
        self.label_loss2 = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=self.y,
                                                    logits=self.logits_w))
        self.trans_loss = tf.reduce_mean(tran2 - self.trans)
        self.emb_loss = 0.01 * self.label_loss1 + self.label_loss2 + 10 * self.triplet_loss + self.model_params.alpha * self.trans_loss
        self.emb_v_class = self.domain_classifier(self.emb_v, self.l)
        self.emb_w_class = self.domain_classifier(self.emb_w,
                                                  self.l,
                                                  reuse=True)

        all_emb_v = tf.concat([
            tf.ones([self.model_params.batch_size, 1]),
            tf.zeros([self.model_params.batch_size, 1])
        ], 1)
        all_emb_w = tf.concat([
            tf.zeros([self.model_params.batch_size, 1]),
            tf.ones([self.model_params.batch_size, 1])
        ], 1)
        self.domain_class_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.emb_v_class, labels=all_emb_w) + \
                                 tf.nn.softmax_cross_entropy_with_logits(logits=self.emb_w_class, labels=all_emb_v)

        self.domain_class_loss = tf.reduce_mean(self.domain_class_loss)

        self.t_vars = tf.trainable_variables()
        self.vf1_vars = [v for v in self.t_vars if 'vf1_' in v.name]
        self.vf_vars = [v for v in self.t_vars if 'vf_' in v.name]
        self.le_vars = [v for v in self.t_vars if 'le_' in v.name]
        self.dc_vars = [v for v in self.t_vars if 'dc_' in v.name]
        self.lc_vars = [v for v in self.t_vars if 'lc_' in v.name]
        self.rf_vars = [v for v in self.t_vars if 'rf_' in v.name]
Example #5
0
 def __init__(self, config):
     BaseModel.__init__(self, config)
Example #6
0
    def __init__(self, opt):
        """Initialize the pix2pix class.

        Parameters:
            opt (Option class)-- stores all the experiment flags; needs to be a subclass of BaseOptions
        """
        assert opt.isTrain
        BaseModel.__init__(self, opt)
        # specify the training losses you want to print out. The training/test scripts will call <BaseModel.get_current_losses>
        self.loss_names = ['G_gan', 'G_recon', 'D_real', 'D_fake']
        # specify the images you want to save/display. The training/test scripts will call <BaseModel.get_current_visuals>
        self.visual_names = ['real_A', 'fake_B', 'real_B']
        # specify the models you want to save to the disk. The training/test scripts will call <BaseModel.save_networks> and <BaseModel.load_networks>
        self.model_names = ['G', 'D']

        # define networks (both generator and discriminator)
        self.netG = networks.define_G(opt.netG,
                                      input_nc=opt.input_nc,
                                      output_nc=opt.output_nc,
                                      ngf=opt.ngf,
                                      norm=opt.norm,
                                      dropout_rate=opt.dropout_rate,
                                      init_type=opt.init_type,
                                      init_gain=opt.init_gain,
                                      gpu_ids=self.gpu_ids,
                                      opt=opt)
        self.netD = networks.define_D(opt.netD,
                                      input_nc=opt.input_nc + opt.output_nc,
                                      ndf=opt.ndf,
                                      n_layers_D=opt.n_layers_D,
                                      norm=opt.norm,
                                      init_type=opt.init_type,
                                      init_gain=opt.init_gain,
                                      gpu_ids=self.gpu_ids,
                                      opt=opt)

        # define loss functions
        self.criterionGAN = GANLoss(opt.gan_mode).to(self.device)
        if opt.recon_loss_type == 'l1':
            self.criterionRecon = torch.nn.L1Loss()
        elif opt.recon_loss_type == 'l2':
            self.criterionRecon = torch.nn.MSELoss()
        elif opt.recon_loss_type == 'smooth_l1':
            self.criterionRecon = torch.nn.SmoothL1Loss()
        else:
            raise NotImplementedError(
                'Unknown reconstruction loss type [%s]!' % opt.loss_type)
        # initialize optimizers; schedulers will be automatically created by function <BaseModel.setup>.
        self.optimizer_G = torch.optim.Adam(self.netG.parameters(),
                                            lr=opt.lr,
                                            betas=(opt.beta1, 0.999))
        self.optimizer_D = torch.optim.Adam(self.netD.parameters(),
                                            lr=opt.lr,
                                            betas=(opt.beta1, 0.999))
        self.optimizers = []
        self.optimizers.append(self.optimizer_G)
        self.optimizers.append(self.optimizer_D)

        self.eval_dataloader = create_eval_dataloader(self.opt)

        block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[2048]
        self.inception_model = InceptionV3([block_idx])
        self.inception_model.to(self.device)
        self.inception_model.eval()

        if 'cityscapes' in opt.dataroot:
            self.drn_model = DRNSeg('drn_d_105', 19, pretrained=False)
            util.load_network(self.drn_model, opt.drn_path, verbose=False)
            if len(opt.gpu_ids) > 0:
                self.drn_model.to(self.device)
                self.drn_model = nn.DataParallel(self.drn_model, opt.gpu_ids)
            self.drn_model.eval()

        self.best_fid = 1e9
        self.best_mIoU = -1e9
        self.fids, self.mIoUs = [], []
        self.is_best = False
        self.Tacts, self.Sacts = {}, {}
        self.npz = np.load(opt.real_stat_path)
Example #7
0
    def __init__(self, model_params):
        BaseModel.__init__(self, model_params)
        self.data_iter = DataIter(self.model_params.batch_size)

        self.tar_img = tf.placeholder(
            tf.float32, [None, self.model_params.visual_feat_dim])
        self.tar_txt = tf.placeholder(tf.float32,
                                      [None, self.model_params.word_vec_dim])
        self.pos_img = tf.placeholder(
            tf.float32, [None, self.model_params.visual_feat_dim])
        self.neg_img = tf.placeholder(
            tf.float32, [None, self.model_params.visual_feat_dim])
        self.pos_txt = tf.placeholder(tf.float32,
                                      [None, self.model_params.word_vec_dim])
        self.neg_txt = tf.placeholder(tf.float32,
                                      [None, self.model_params.word_vec_dim])
        self.y = tf.placeholder(tf.int32, [self.model_params.batch_size, 10])
        self.y_single = tf.placeholder(tf.int32,
                                       [self.model_params.batch_size, 1])
        self.l = tf.placeholder(tf.float32, [])
        self.emb_v = self.visual_feature_embed(self.tar_img)
        self.emb_w = self.label_embed(self.tar_txt)
        self.emb_v_pos = self.visual_feature_embed(self.pos_img, reuse=True)
        self.emb_v_neg = self.visual_feature_embed(self.neg_img, reuse=True)
        self.emb_w_pos = self.label_embed(self.pos_txt, reuse=True)
        self.emb_w_neg = self.label_embed(self.neg_txt, reuse=True)

        # triplet loss
        margin = self.model_params.margin
        alpha = self.model_params.alpha
        v_loss_pos = tf.reduce_sum(tf.nn.l2_loss(self.emb_v - self.emb_w_pos))
        v_loss_neg = tf.reduce_sum(tf.nn.l2_loss(self.emb_v - self.emb_w_neg))
        w_loss_pos = tf.reduce_sum(tf.nn.l2_loss(self.emb_w - self.emb_v_pos))
        w_loss_neg = tf.reduce_sum(tf.nn.l2_loss(self.emb_w - self.emb_v_neg))
        self.triplet_loss = tf.maximum(
            0., margin + alpha * v_loss_pos - v_loss_neg) + tf.maximum(
                0., margin + alpha * w_loss_pos - w_loss_neg)

        logits_v = self.label_classifier(self.emb_v)
        logits_w = self.label_classifier(self.emb_w, reuse=True)
        self.label_loss = tf.nn.softmax_cross_entropy_with_logits(labels=self.y, logits=logits_v) + \
            tf.nn.softmax_cross_entropy_with_logits(labels=self.y, logits=logits_w)
        self.label_loss = tf.reduce_mean(self.label_loss)
        self.emb_loss = 100 * self.label_loss + self.triplet_loss
        self.emb_v_class = self.domain_classifier(self.emb_v, self.l)
        self.emb_w_class = self.domain_classifier(self.emb_w,
                                                  self.l,
                                                  reuse=True)

        all_emb_v = tf.concat([
            tf.ones([self.model_params.batch_size, 1]),
            tf.zeros([self.model_params.batch_size, 1])
        ], 1)
        all_emb_w = tf.concat([
            tf.zeros([self.model_params.batch_size, 1]),
            tf.ones([self.model_params.batch_size, 1])
        ], 1)
        self.domain_class_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.emb_v_class, labels=all_emb_w) + \
            tf.nn.softmax_cross_entropy_with_logits(logits=self.emb_w_class, labels=all_emb_v)
        self.domain_class_loss = tf.reduce_mean(self.domain_class_loss)

        self.t_vars = tf.trainable_variables()
        self.vf_vars = [v for v in self.t_vars if 'vf_' in v.name]
        self.le_vars = [v for v in self.t_vars if 'le_' in v.name]
        self.dc_vars = [v for v in self.t_vars if 'dc_' in v.name]
        self.lc_vars = [v for v in self.t_vars if 'lc_' in v.name]
 def testArgs(self):
     '''No arguments'''
     with self.assertRaises(TypeError) as e:
         BaseModel.__init__()
     msg = "__init__() missing 1 required positional argument: 'self'"
     self.assertEqual(str(e.exception), msg)
	def __init__(self, opt):
		BaseModel.__init__(self, opt)  # call the initialization method of BaseModel

		# specify the images you want to save and display. The program will call base_model.get_current_visuals to save and display these images.
		self.visual_names = []
Example #10
0
 def __init__(self, *args, **kwargs):
     if args and type(args[0]) is dict:
         BaseModel.__init__(self, args[0])
     else:
         BaseModel.__init__(self)
Example #11
0
 def __init__(self, *args, **kwargs):
     """ initializes parent class """
     if args and type(args[0]) is dict:
         BaseModel.__init__(self, args[0])
     else:
         BaseModel.__init__(self)
Example #12
0
 def test_init_no_args(self):
     """Tests __init__ with no arguments."""
     with self.assertRaises(TypeError) as e:
         BaseModel.__init__()
     msj = "__init__() missing 1 required positional argument: 'self'"
     self.assertEqual(str(e.exception), msj)
Example #13
0
 def __init__(self, *args, **kwargs):
     """ init method
     """
     BaseModel.__init__(self, *args, **kwargs)
Example #14
0
 def __init__(self, data_layer):
     BaseModel.__init__(self, 'archive', data_layer, {}, ItemValidator())
    def __init__(self, opt):
        """Initialize the CycleGAN class.

        Parameters:
            opt (Option class)-- stores all the experiment flags; needs to be a subclass of BaseOptions
        """
        assert opt.isTrain
        assert opt.direction == 'AtoB'
        assert opt.dataset_mode == 'unaligned'
        BaseModel.__init__(self, opt)
        # specify the training losses you want to print out. The training/test scripts will call <BaseModel.get_current_losses>
        self.loss_names = [
            'D_A', 'G_A', 'G_cycle_A', 'G_idt_A', 'D_B', 'G_B', 'G_cycle_B',
            'G_idt_B'
        ]
        # specify the images you want to save/display. The training/test scripts will call <BaseModel.get_current_visuals>
        visual_names_A = ['real_A', 'fake_B', 'rec_A']
        visual_names_B = ['real_B', 'fake_A', 'rec_B']
        if self.opt.lambda_identity > 0.0:  # if identity loss is used, we also visualize idt_B=G_A(B) ad idt_A=G_A(B)
            visual_names_A.append('idt_B')
            visual_names_B.append('idt_A')

        self.visual_names = visual_names_A + visual_names_B  # combine visualizations for A and B
        # specify the models you want to save to the disk. The training/test scripts will call <BaseModel.save_networks> and <BaseModel.load_networks>.
        self.model_names = ['G_A', 'G_B', 'D_A', 'D_B']

        # define networks (both Generators and discriminators)
        # The naming is different from those used in the paper.
        # Code (vs. paper): G_A (G), G_B (F), D_A (D_Y), D_B (D_X)
        self.netG_A = networks.define_G(opt.input_nc, opt.output_nc, opt.ngf,
                                        opt.netG, opt.norm, opt.dropout_rate,
                                        opt.init_type, opt.init_gain,
                                        self.gpu_ids)
        self.netG_B = networks.define_G(opt.output_nc, opt.input_nc, opt.ngf,
                                        opt.netG, opt.norm, opt.dropout_rate,
                                        opt.init_type, opt.init_gain,
                                        self.gpu_ids)

        self.netD_A = networks.define_D(opt.output_nc, opt.ndf, opt.netD,
                                        opt.n_layers_D, opt.norm,
                                        opt.init_type, opt.init_gain,
                                        self.gpu_ids)
        self.netD_B = networks.define_D(opt.input_nc, opt.ndf, opt.netD,
                                        opt.n_layers_D, opt.norm,
                                        opt.init_type, opt.init_gain,
                                        self.gpu_ids)

        if opt.lambda_identity > 0.0:  # only works when input and output images have the same number of channels
            assert (opt.input_nc == opt.output_nc)
        self.fake_A_pool = ImagePool(
            opt.pool_size
        )  # create image buffer to store previously generated images
        self.fake_B_pool = ImagePool(
            opt.pool_size
        )  # create image buffer to store previously generated images

        # define loss functions
        self.criterionGAN = models.modules.loss.GANLoss(opt.gan_mode).to(
            self.device)  # define GAN loss.
        self.criterionCycle = torch.nn.L1Loss()
        self.criterionIdt = torch.nn.L1Loss()

        # initialize optimizers; schedulers will be automatically created by function <BaseModel.setup>.
        self.optimizer_G = torch.optim.Adam(itertools.chain(
            self.netG_A.parameters(), self.netG_B.parameters()),
                                            lr=opt.lr,
                                            betas=(opt.beta1, 0.999))
        self.optimizer_D = torch.optim.Adam(itertools.chain(
            self.netD_A.parameters(), self.netD_B.parameters()),
                                            lr=opt.lr,
                                            betas=(opt.beta1, 0.999))

        self.optimizers = []
        self.optimizers.append(self.optimizer_G)
        self.optimizers.append(self.optimizer_D)

        self.eval_dataloader_AtoB = create_eval_dataloader(self.opt,
                                                           direction='AtoB')
        self.eval_dataloader_BtoA = create_eval_dataloader(self.opt,
                                                           direction='BtoA')

        block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[2048]
        self.inception_model = InceptionV3([block_idx])
        self.inception_model.to(self.device)
        self.inception_model.eval()

        if 'cityscapes' in opt.dataroot:
            self.drn_model = DRNSeg('drn_d_105', 19, pretrained=False)
            util.load_network(self.drn_model, opt.drn_path, verbose=False)
            if len(opt.gpu_ids) > 0:
                self.drn_model = nn.DataParallel(self.drn_model, opt.gpu_ids)
            self.drn_model.eval()

        self.best_fid_A, self.best_fid_B = 1e9, 1e9
        self.best_mAP = -1e9
        self.fids_A, self.fids_B = [], []
        self.mAPs = []
        self.is_best = False
        self.npz_A = np.load(opt.real_stat_A_path)
        self.npz_B = np.load(opt.real_stat_B_path)