Exemplo n.º 1
0
 def __init__(self,
              data_name,
              img_dir,
              bit,
              visdom=True,
              batch_size=128,
              cuda=True,
              **kwargs):
     super(PRDH, self).__init__("PRDH", data_name, bit, batch_size, visdom,
                                cuda)
     self.train_data, self.valid_data = single_data(data_name,
                                                    img_dir,
                                                    batch_size=batch_size,
                                                    **kwargs)
     self.loss_store = [
         "inter loss", 'intra loss', 'decorrelation loss',
         'regularization loss', 'loss'
     ]
     self.parameters = {'gamma': 1, 'lambda': 1}
     self.max_epoch = 500
     self.lr = {'img': 10**(-1.5), 'txt': 10**(-1.5)}
     self.lr_decay_freq = 1
     self.lr_decay = (10**(-1.5) - 1e-6) / self.max_epoch
     self.num_train = len(self.train_data)
     self.img_model = cnnf.get_cnnf(bit)
     self.txt_model = MLP.MLP(self.train_data.get_tag_length(),
                              bit,
                              leakRelu=False)
     self.F_buffer = torch.randn(self.num_train, bit)
     self.G_buffer = torch.randn(self.num_train, bit)
     self.train_L = self.train_data.get_all_label()
     self.ones = torch.ones(batch_size, 1)
     self.ones_ = torch.ones(self.num_train - batch_size, 1)
     if cuda:
         self.img_model = self.img_model.cuda()
         self.txt_model = self.txt_model.cuda()
         self.train_L = self.train_L.cuda()
         self.F_buffer = self.F_buffer.cuda()
         self.G_buffer = self.G_buffer.cuda()
         self.ones = self.ones.cuda()
         self.ones_ = self.ones_.cuda()
     self.Sim = calc_neighbor(self.train_L, self.train_L)
     self.B = torch.sign(self.F_buffer + self.G_buffer)
     optimizer_img = SGD(self.img_model.parameters(), lr=self.lr['img'])
     optimizer_txt = SGD(self.txt_model.parameters(), lr=self.lr['txt'])
     self.optimizers = [optimizer_img, optimizer_txt]
     self._init()
Exemplo n.º 2
0
    def __init__(self,
                 data_name: str,
                 val_freq: int,
                 img_dir: str,
                 bit: int,
                 visdom=True,
                 batch_size=128,
                 cuda=True,
                 **kwargs):
        super(CMHH, self).__init__("CMHH", data_name, bit, batch_size, visdom,
                                   cuda)
        self.train_data, self.valid_data = single_data(data_name,
                                                       img_dir,
                                                       batch_size=batch_size,
                                                       **kwargs)
        self.loss_store = ['focal loss', 'quantization loss', 'loss']
        self.parameters = {'gamma': 2, 'alpha': 0.5, 'beta': 0.5, 'lambda': 0}
        self.lr = {'img': 0.02, 'txt': 0.2}
        self.lr_decay_freq = 15
        self.lr_decay = 0.5
        self.val_freq = val_freq

        self.num_train = len(self.train_data)
        self.img_model = alexnet.get_alexnet(bit=bit)
        self.txt_model = MLP.MLP(self.train_data.get_tag_length(), bit)
        self.train_label = self.train_data.get_all_label()
        self.F_buffer = torch.randn(self.num_train, bit)
        self.G_buffer = torch.randn(self.num_train, bit)
        if cuda:
            self.img_model = self.img_model.cuda()
            self.txt_model = self.txt_model.cuda()
            self.train_label = self.train_label.cuda()
            self.F_buffer = self.F_buffer.cuda()
            self.G_buffer = self.G_buffer.cuda()
        self.B = torch.sign(self.F_buffer + self.G_buffer)

        optimizer_img = SGD(self.img_model.parameters(),
                            lr=self.lr['img'],
                            momentum=0.9)
        optimizer_txt = SGD(self.txt_model.parameters(),
                            lr=self.lr['txt'],
                            momentum=0.9)
        self.optimizers = [optimizer_img, optimizer_txt]
        self._init()
Exemplo n.º 3
0
 def __init__(self,
              data_name: str,
              img_dir: str,
              bit: int,
              visdom=True,
              batch_size=128,
              cuda=True,
              **kwargs):
     super(CHN, self).__init__("CHN", data_name, bit, batch_size, visdom,
                               cuda)
     self.train_data, self.valid_data = single_data(data_name,
                                                    img_dir,
                                                    batch_size=batch_size,
                                                    **kwargs)
     self.loss_store = ['cosine loss', 'quantization loss', 'loss']
     self.parameters = {'beta': 0.8, 'lambda': 0.01}
     self.lr = {'img': 0.01, 'txt': 1}
     self.lr_decay_freq = 10
     self.lr_decay = 0.5
     self.num_train = len(self.train_data)
     self.img_model = cnnf.get_cnnf(bit)
     self.txt_model = MLP.MLP(self.train_data.get_tag_length(), bit)
     self.F_buffer = torch.randn(self.num_train, bit)
     self.G_buffer = torch.randn(self.num_train, bit)
     self.train_L = self.train_data.get_all_label()
     if cuda:
         self.img_model = self.img_model.cuda()
         self.txt_model = self.txt_model.cuda()
         self.train_L = self.train_L.cuda()
         self.F_buffer = self.F_buffer.cuda()
         self.G_buffer = self.G_buffer.cuda()
     optimizer_img = SGD(self.img_model.parameters(),
                         lr=self.lr['img'],
                         momentum=0.9)
     optimizer_txt = SGD(self.txt_model.parameters(),
                         lr=self.lr['txt'],
                         momentum=0.9)
     self.optimizers = [optimizer_img, optimizer_txt]
     self._init()
Exemplo n.º 4
0
    def __init__(self,
                 data_name: str,
                 img_dir: str,
                 bit: int,
                 img_net,
                 visdom=True,
                 batch_size=64,
                 cuda=True,
                 **kwargs):
        super(SAGAN, self).__init__("SAGAN", data_name, bit, batch_size,
                                    visdom, cuda)
        self.train_data, self.valid_data = single_data(data_name,
                                                       img_dir,
                                                       batch_size=batch_size,
                                                       **kwargs)
        self.loss_store = [
            'cosine_margin_loss', 'cosine_quantization_loss', 'graph_loss',
            'loss_adver', 'loss'
        ]
        self.parameters = {
            'fusion num': 4,
            'lambda': 1,
            'margin': 0.3,
            'alpha': 2**np.log2(bit / 32),
            'margin_t': 2.0
        }
        self.lr = {'img': 10**(-1.5), 'txt': 10**(-1.5), 'gan': 0.001}
        self.max_epoch = 500
        self.lr_decay_freq = 1
        self.lr_decay = (10**(-1.5) - 1e-6) / self.max_epoch

        self.num_train = len(self.train_data)
        self.img_model = img_net(bit, self.parameters['fusion num'])
        self.txt_model = get_MS_Text(self.train_data.get_tag_length(), bit,
                                     self.parameters['fusion num'])
        ##GAN Module
        self.emd_dim = self.bit
        self.img_discriminator = nn.Sequential(
            SpectralNorm(
                nn.Conv2d(1, self.emd_dim, kernel_size=(self.emd_dim, 1))),
            nn.ReLU(True), SpectralNorm(nn.Conv2d(self.emd_dim, 256, 1)),
            nn.ReLU(True), SpectralNorm(nn.Conv2d(256, 1, 1)))

        self.txt_discriminator = nn.Sequential(
            SpectralNorm(
                nn.Conv2d(1, self.emd_dim, kernel_size=(self.emd_dim, 1))),
            nn.ReLU(True), SpectralNorm(nn.Conv2d(self.emd_dim, 256, 1)),
            nn.ReLU(True), SpectralNorm(nn.Conv2d(256, 1, 1)))

        self.train_L = self.train_data.get_all_label()
        self.F_buffer = torch.randn(self.num_train, bit)
        self.G_buffer = torch.randn(self.num_train, bit)
        if cuda:
            self.img_model = self.img_model.cuda()
            self.txt_model = self.txt_model.cuda()
            self.img_discriminator = self.img_discriminator.cuda()
            self.txt_discriminator = self.txt_discriminator.cuda()
            self.train_L = self.train_L.cuda()
            self.F_buffer = self.F_buffer.cuda()
            self.G_buffer = self.G_buffer.cuda()
        self.B = torch.sign(self.F_buffer + self.G_buffer)

        #GAN_optimizer
        self.optimizer_dis = {
            'img':
            Adamax(self.img_discriminator.parameters(),
                   lr=self.lr['img'],
                   betas=(0.5, 0.9),
                   weight_decay=0.0001),
            'txt':
            Adamax(self.txt_discriminator.parameters(),
                   lr=self.lr['img'],
                   betas=(0.5, 0.9),
                   weight_decay=0.0001)
        }
        self.optimizer = torch.optim.SGD([{
            'params': self.img_model.parameters(),
            'lr': self.lr['img']
        }, {
            'params': self.txt_model.parameters(),
            'lr': self.lr['txt']
        }])
        self._init()