def object_function(self, hash_layers1, hash_layer2, final_hash1, final_hash2, label1, label2, F, G, ind1, ind2): # get similarity matrix S_inter1 = calc_neighbor(label1, self.train_L) S_inter2 = calc_neighbor(label2, self.train_L) S_intra = calc_neighbor(label1, label2) inter_loss1, inter_loss2 = calc_inter_loss(hash_layers1, hash_layer2, S_inter1, S_inter2, G, self.parameters['alpha']) inter_loss = 0.5 * (inter_loss1 + inter_loss2) intra_loss1, intra_loss2 = calc_intra_loss(hash_layers1, hash_layer2, S_inter1, S_inter2, F, self.parameters['alpha']) intra_loss = 0.5 * (intra_loss1 + intra_loss2) * self.parameters['lambda'] intra_pair_loss = calc_intra_pairwise_loss(hash_layers1, hash_layer2, S_intra, self.parameters['alpha']) intra_pair_loss = intra_pair_loss * self.parameters['gamma'] quantization_loss1 = torch.mean( torch.sum(torch.pow(self.B[ind1, :] - final_hash1, 2), dim=1)) quantization_loss2 = torch.mean( torch.sum(torch.pow(self.B[ind2, :] - final_hash2, 2), dim=1)) quantization_loss = 0.5 * (quantization_loss1 + quantization_loss2 ) / self.bit * self.parameters['beta'] return inter_loss, intra_loss, intra_pair_loss, quantization_loss
def object_function(self, cur_h: torch.Tensor, sample_label: torch.Tensor, A: torch.Tensor, C: torch.Tensor, ind): unupdated_ind = np.setdiff1d(range(self.num_train), ind) S = calc_neighbor(sample_label, self.train_L) theta = 1.0 / 2 * torch.matmul(cur_h, A.t()) logloss = -torch.sum(S * theta - torch.log(1.0 + torch.exp(theta))) quantization = torch.sum(torch.pow(self.B[ind, :] - cur_h, 2)) balance = torch.sum( torch.pow( cur_h.t().mm(self.ones) + C[unupdated_ind].t().mm(self.ones_), 2)) return logloss, quantization, balance
def object_function(self, cur_h, O, label, ind): hamming_dist = euclidean_dist_matrix(cur_h, O) logit = torch.exp(-hamming_dist * self.parameters['beta']) sim = calc_neighbor(label, label) focal_pos = sim * focal_loss(logit, gamma=self.parameters['gamma'], alpha=self.parameters['alpha']) focal_neg = (1 - sim) * focal_loss(1 - logit, gamma=self.parameters['gamma'], alpha=1 - self.parameters['alpha']) fl_loss = torch.mean(focal_pos + focal_neg) quantization_loss = torch.mean( torch.sqrt( torch.sum(torch.pow(torch.sign(cur_h) - cur_h, 2), dim=1))) * self.parameters['lambda'] return fl_loss, quantization_loss
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()
def object_function(self, f_img, f_txt, hash_layers1, hash_layers2, final_hash1, final_hash2, label, G, F, ind): S_inter = calc_neighbor(label, self.train_L) inter_loss_img = calc_inter_loss(hash_layers1, S_inter, G, self.parameters['alpha']) inter_loss_txt = calc_inter_loss(hash_layers2, S_inter, F, self.parameters['alpha']) inter_loss = 0.5 * (inter_loss_img + inter_loss_txt) # intra_loss of img and txt intra_loss_1 = calc_inter_loss(hash_layers1, S_inter, F, self.parameters['alpha']) intra_loss_2 = calc_inter_loss(hash_layers2, S_inter, G, self.parameters['alpha']) intra_loss = (intra_loss_1 + intra_loss_2) * self.parameters['lambda'] intra = intra_loss + inter_loss # S_cos=calc_neighbor_new(label,self.train_L,f_img,f_txt) #NOTE:margin is the hyperparameters criterion_tri_cos = triplet_loss.TripletHardLoss(dis_metric='cos', reduction='sum') loss1 = criterion_tri_cos(final_hash1, label, target=final_hash2, margin=self.parameters['margin']) loss2 = criterion_tri_cos(final_hash2, label, target=final_hash1, margin=self.parameters['margin']) cosine_margin_loss = loss1 + loss2 theta1 = functional.cosine_similarity(torch.abs(F), torch.ones_like(F).cuda()) theta2 = functional.cosine_similarity(torch.abs(G), torch.ones_like(G).cuda()) cosine_quantization_loss = torch.sum( 1 / (1 + torch.exp(theta1))) + torch.sum(1 / (1 + torch.exp(theta2))) quantization_loss1 = torch.mean( torch.sum(torch.pow(final_hash2 - final_hash1, 2), dim=1)) graph_loss = quantization_loss1 / self.bit return cosine_margin_loss, cosine_quantization_loss, intra, graph_loss
def object_function(self, cur_h: torch.Tensor, sample_label: torch.Tensor, A: torch.Tensor, C: torch.Tensor, ind): unupdated_ind = np.setdiff1d(range(self.num_train), ind) S = calc_neighbor(sample_label, self.train_L) theta = 1.0 / 2 * torch.matmul(cur_h, A.t()) inter_loss = -torch.mean(S * theta - torch.log(1.0 + torch.exp(theta))) theta = 1.0 / 2 * torch.matmul(cur_h, C.t()) intra_loss = -torch.mean(S * theta - torch.log(1.0 + torch.exp(theta))) decorrelation_loss = calc_decorrelation_loss(cur_h) decorrelation_loss *= self.parameters['lambda'] quantization = torch.sum(torch.pow(self.B - C, 2)) quantization /= (self.num_train * self.batch_size) balance = torch.sum( torch.pow( cur_h.t().mm(self.ones) + C[unupdated_ind].t().mm(self.ones_), 2)) balance /= (self.num_train * self.batch_size) regularization_loss = quantization + balance regularization_loss *= self.parameters['gamma'] return inter_loss, intra_loss, decorrelation_loss, regularization_loss