def pair_distance(x, y, rng, flag = True, after_weights=None): total = 0 for i in xrange(0, rng, 2): if flag: total += cosine_distance(pre_weights[0][x[i]], pre_weights[0][y[i]]) else: total += cosine_distance(after_weights[x[i]], after_weights[y[i]]) return total * 2 / rng
def rank_features(self, input, num_rank=5): """Uses abstraction features.""" ref_features, rank_features = [self.base_forward(x) for x in input] ref_features = ref_features[:, -1:] rank_features = rank_features[:, :num_rank] dist = cosine_distance(ref_features, rank_features, dim=-1) return dist
def rank(self, input, num_rank=5): """Uses learned semantic embeddings.""" ref_features, rank_features = [self.base_forward(x) for x in input] ref_embed = self.embedding_linear(ref_features)[:, -1:] rank_embed = self.embedding_linear(rank_features)[:, :num_rank] dist = cosine_distance(ref_embed, rank_embed, dim=-1) return dist
def detect_face(self, frame_list, gt_feature): exist_face = 0 for frame in frame_list: info_list = self.detector.detect_faces(frame) for info_box in info_list: if info_box['confidence'] < 0.9: continue bbox = info_box['box'] face_img = frame[bbox[1]:bbox[1] + bbox[3], bbox[0]:bbox[0] + bbox[2]] self.image_cnt += 1 face_img = FaceDetector.pre_process(face_img) face_feature = self.face_encoder(face_img) face_feature = face_feature.squeeze() if TRAIN_PARAMETER['GPU']: face_feature = face_feature.cpu() face_feature = face_feature.detach().numpy() face_dist = cosine_distance(face_feature, gt_feature) if face_dist < 1e-4: exist_face += 1 break elif face_dist < 1.5e-4: exist_face += 0.5 break return exist_face
def n_pair_loss(anchors, positives, negatives, num_class, batch_size): """ Calculates N-Pair loss :param anchors: A torch.Tensor, (n, embedding_size) :param positives: A torch.Tensor, (n, embedding_size) :param negatives: A torch.Tensor, (n, n-1, embedding_size) :return: A scalar """ pos_dis = utils.cosine_distance(anchors, positives, cross=False) # neg_dis = utils.cosine_distance(anchors,negatives,cross = True) anchors = torch.unsqueeze(Func.normalize(anchors, dim=1), dim=1) # (n, 1, embedding_size) positives = torch.unsqueeze(Func.normalize(positives, dim=1), dim=1) # (n, 1, embedding_size) negatives = Func.normalize(negatives, dim=2) neg_dis = 0.5 * (1.0 - torch.matmul(anchors, negatives.transpose(1, 2))) sub = Func.normalize((negatives - positives), dim=2) sub = torch.where( negatives.type(torch.cuda.FloatTensor) == torch.zeros(1, 256).cuda(), torch.zeros(1, 256).cuda(), sub) x_l = torch.matmul(anchors, sub.transpose( 1, 2)) #/(batch_size*num_class)#*() # (n, 1, n-1) # x_l = -(neg_dis.squeeze(1).transpose(0,1).unsqueeze(2) - pos_dis.unsqueeze(1)) # # print("npair :::::: ",x_l.mean()) # x_l = x_l.squeeze(2) # x_l = x_l.transpose(0,1) # x_l = x_l.unsqueeze(1) x = torch.sum(torch.exp(x_l), 2) # (n, 1) # print("npair exp :::::: ", x.mean()) loss = torch.mean(torch.log(1 + x)) #/(num_class) # print("npair loss :::::: ", loss) # print("np loss ", loss) return loss
def _get(self, index: int): if isinstance(self.record_set, RankingSets): record = self.record_set[index] # Outlier is last element in the set. set_names = record.reference + [record.outlier] features = torch.stack([ self.model.named_features( rec.path.replace('multi_test', 'test_multi')) for rec in set_names ]) return features, torch.LongTensor([len(set_names) - 1]) elif isinstance(self.record_set, AbstractionSets): record = self.record_set[index] outliers = [] features = torch.stack( [self.model.named_features(name) for name in record.videos]) class_embeddings = torch.stack( [self.model.named_embedding(name) for name in record.videos]) inds = [ind for ind in record.output_indices() if len(ind) > 2] for ind in inds: label = record.labels_dict[ind] nodes = self.graph.category_to_node(*label) ce = torch.stack([class_embeddings[i] for i in ind]) abstr_embedding = self.graph.node_to_embedding(*nodes) dists = utils.cosine_distance(abstr_embedding, ce, dim=-1) outlier_idx = ind[torch.argmax(dists)] outliers.append(outlier_idx) return features, (torch.LongTensor(outliers), )
def clustering_stop(self, centers): if centers is None: self.stop = False else: dist = utils.cosine_distance(centers, self.target_ctr, cross=False) dist = torch.mean(dist, dim=0) print('dist %.4f' % dist.item()) self.stop = dist.item() < self.eps
def find_outlier_rn(self, input): num_inputs = input.size(1) set_features = self.base_forward(input) abstr_feats = set_features[:, -1:] dist = cosine_distance(abstr_feats, input, dim=-1) return dist, torch.argsort(dist) dists = cosine_distance(abstr_feats, set_features, dim=-1) pdists = defaultdict(list) for i, rset in enumerate( self.relation_sets(num_inputs, min(self.scales))): d = dists[:, i] for r in rset: pdists[r].append(d) dist = torch.stack( [torch.stack(pdists[n]).sum(dim=0) for n in range(num_inputs)], 1) return dist, torch.argsort(dist)
def find_outlier_features(self, input): """Uses abstraction features.""" num_inputs = input.size(1) set_features = self.base_forward(input) abstr_feats = set_features[:, -1:] class_feats = set_features[:, :num_inputs] dist = cosine_distance(abstr_feats, class_feats, dim=-1) return dist, torch.argsort(dist)
def predict(self, x: Tensor, z_mu: Optional[Tensor] = None, z_logvar: Optional[Tensor] = None): """Predict classes. Args: x (Tensor): Tensor input. z_mu (Optional[Tensor], optional): Already computed mu from encoder. Defaults to None. z_logvar (Optional[Tensor], optional): Already computed logvar from encoder. Defaults to None. Returns: List: List of predicted labels. """ # Allows us to pass already computed z_mu and z_logvar if z_mu is None or z_logvar is None: output = self(x) z_mu, z_logvar = output['z_mu'], output['z_logvar'] z_var = calculate_var(z_logvar) device = self.device() batch_size = x.size(0) prediction = [None] * batch_size min_distances = [math.inf] * batch_size if empty(self.class_idents): print(f"⚠ No registered class identifiers") for label, prototype in self.class_idents.items(): proto_mu, proto_var = prototype['mu'], prototype['var'] proto_mu = proto_mu.repeat(batch_size, 1) proto_var = proto_var.repeat(batch_size, 1) mu_distances = cosine_distance(z_mu, proto_mu, dim=1) var_distances = cosine_distance(z_var, proto_var, dim=1) distances = self.rho * mu_distances + \ (1.0 - self.rho) * var_distances for i in range(0, batch_size): distance = distances[i].cpu().item() if distance < min_distances[i]: min_distances[i] = distance prediction[i] = label return prediction
def cos_nearest(self, word_index, k): """Sorts words according to their Cosinus distance.""" e = self.embeddings[word_index] distances = [] for line in self.embeddings: distances.append(1 - cosine_distance(e, line)) sorted_distances = sorted(enumerate(distances), key=itemgetter(1)) return zip(*sorted_distances[:k])
def get_human_readable(self, x: torch.Tensor) -> Tuple[List[str], List[str]]: dataset: IntentEmbedBertDataset = self.loaders[Splits.train].ds_orig embeds = torch.from_numpy(dataset.embeds).to(self.device) distances = cosine_distance(x, embeds) ranking = distances.argsort(dim=-1, descending=False) indices = ranking[:, 0].cpu().numpy() texts = list(np.array(dataset.texts)[indices]) labels = list(np.array(dataset.labels)[indices]) return texts, labels
def find_outlier(self, input): """Uses learned semantic embeddings.""" num_inputs = input.size(1) set_features = self.base_forward(input) set_embed = self.embedding_linear(set_features) abstr_embed = set_embed[:, num_inputs:].mean(dim=1, keepdim=True) class_embed = set_embed[:, :num_inputs] dist = cosine_distance(abstr_embed, class_embed, dim=-1) return dist, torch.argsort(dist)
def pair_distance(pairs): total = 0.0 for pair in pairs: a = pair[0] b = pair[1] a_id = vocab['get_index'][a] b_id = vocab['get_index'][b] total += cosine_distance(afterr[a_id+1], afterr[b_id+1]) total /= len(pairs) return total
def rank_rn(self, input, num_rank=5): num_inputs = input[-1].size(1) ref_features, rank_features = [self.base_forward(x) for x in input] ref_features = ref_features[:, -1:] dists = cosine_distance(ref_features, rank_features, dim=-1) pdists = defaultdict(list) for i, rset in enumerate( self.relation_sets(num_inputs, min(self.scales))): for r in rset: pdists[r].append(dists[:, i]) dist = torch.stack( [torch.stack(pdists[n]).sum(dim=0) for n in range(num_inputs)], 1) return dist
def similarity_score(pose1, pose2): p1 = [] p2 = [] pose_1 = np.array(pose1, dtype=np.float) pose_2 = np.array(pose2, dtype=np.float) # Normalize coordinates pose_1[:, 0] = pose_1[:, 0] / max(pose_1[:, 0]) pose_1[:, 1] = pose_1[:, 1] / max(pose_1[:, 1]) pose_2[:, 0] = pose_2[:, 0] / max(pose_2[:, 0]) pose_2[:, 1] = pose_2[:, 1] / max(pose_2[:, 1]) # L2 Normalization # for joint in range(pose_1.shape[0]): # mag1 = float(math.sqrt(pose_1[joint][0]**2 + pose_1[joint][1]**2)) # mag2 = float(math.sqrt(pose_2[joint][0]**2 + pose_2[joint][1]**2)) # pose_1[joint][0] = pose_1[joint][0] / mag1 # pose_1[joint][1] = pose_1[joint][1] / mag2 # pose_2[joint][0] = pose_2[joint][0] / mag2 # pose_2[joint][1] = pose_2[joint][1] / mag2 # Turn (16x2) into (32x1) for joint in range(pose_1.shape[0]): x1 = pose_1[joint][0] y1 = pose_1[joint][1] x2 = pose_2[joint][0] y2 = pose_2[joint][1] p1.append(x1) p1.append(y1) p2.append(x2) p2.append(y2) p1 = np.array(p1) p2 = np.array(p2) # Looking to minimize the distance if there is a match # Computing two different distance metrics cosine_distance = utils.cosine_distance(p1, p2) # weighted_distance = utils.weight_distance(p1, p2, conf1) # print("Cosine Distance:", cosine_distance) # print("Weighted Distance:", weighted_distance) return cosine_distance, 0
def assign_labels(self, feats): dists = utils.cosine_distance(feats, self.target_ctr, cross=True) _, labels = torch.min(dists, dim=1) return dists, labels
afterr = np.load("after_class.npy")[0] xiao_p = pre[92] xiao_a = after[92] xiao_c = afterr[93] da_p = pre[23] da_a = after[23] da_c = afterr[24] xi_p = pre[922] xi_a = after[922] xi_c = after[923] print "xi-xiao" print cosine_distance(xi_p, xiao_p) print cosine_distance(xi_a, xiao_a) print cosine_distance(xi_c, xiao_c) print "xiao-da" print cosine_distance(xiao_p, da_p) print cosine_distance(xiao_a, da_a) print cosine_distance(xiao_c, da_c) print "xi-da" print cosine_distance(xi_p, da_p) print cosine_distance(xi_a, da_a) print cosine_distance(xi_c, da_c) g = GetWordVector() g.load_all()
def skmeans_clustering(self, cluster_iter): self.target_ctr = self.source_ctr self.max_len = 1000 centers = None self.stop = False batch_features = [] batch_path = [] for data, labels, path in iter(cluster_iter): data, labels = data.cuda(), labels.cuda() features, _ = self.base_network(data) batch_features += [features] batch_path += path self.target_features = torch.cat(batch_features, dim=0) self.clustered_targets["features"] = self.target_features self.clustered_targets["path"] = batch_path refs = utils.to_cuda( torch.LongTensor(range(self.num_classes)).unsqueeze(1)) num_samples = self.target_features.size(0) num_split = ceil(1.0 * num_samples / self.max_len) # while True: # print(self.stop) # self.clustering_stop(centers) for _ in range(0, 1000): if centers is not None: self.target_ctr = centers # if self.stop: break centers = 0 count = 0 start = 0 ps_label = [] dis2c = [] for N in range(num_split): cur_len = min(self.max_len, num_samples - start) cur_feature = self.target_features.narrow(0, start, cur_len) dist2center = utils.cosine_distance(cur_feature, self.target_ctr, cross=True) # dis2c +=[dist2center] dis, pseudo_labels = torch.min(dist2center, dim=1) dis2c += [dis] ps_label += [pseudo_labels] # dist2center, pseudo_labels = self.assign_labels(cur_feature) labels_onehot = utils.to_onehot(pseudo_labels, self.num_classes) count += torch.sum(labels_onehot, dim=0) pseudo_labels = pseudo_labels.unsqueeze( 0) #.expand(self.num_classes, -1) mask = (pseudo_labels == refs).unsqueeze(2).type( torch.cuda.FloatTensor) reshaped_feature = cur_feature.unsqueeze(0) # update centers centers += torch.sum(reshaped_feature * mask, dim=1) start += cur_len self.clustered_targets["ps_label"] = torch.cat(ps_label, dim=0) self.clustered_targets["Dis2C"] = torch.cat(dis2c, dim=0) mask = (count.unsqueeze(1) > 0).type(torch.cuda.FloatTensor) centers = mask * centers + (1 - mask) * self.source_ctr del self.clustered_targets[ "features"], mask, pseudo_labels, labels_onehot, ps_label, dis2c, dis, dist2center, cur_feature, reshaped_feature
def main(): get_console_args() cur_dataset = TRAIN_PARAMETER['cur_dataset'] face_dataset = DATASET_PARAMETER[cur_dataset]['dataset']( **DATASET_PARAMETER[cur_dataset]['d_params']) face_loader = DataLoader(face_dataset, batch_size=TRAIN_PARAMETER['batch_size'], num_workers=TRAIN_PARAMETER['num_workers']) face_encoder, _ = get_network('FaceEncoder') face_feature_dict = dict() for face_img, face_label in face_loader: if TRAIN_PARAMETER['GPU']: face_img, face_label = face_img.cuda(), face_label.cuda() face_feature = face_encoder(face_img) face_label = face_label.item() if face_label not in face_feature_dict.keys(): face_feature_dict[face_label] = dict() face_feature_dict[face_label]['list'] = [] if TRAIN_PARAMETER['GPU']: face_feature = face_feature.cpu() face_feature = face_feature.detach().numpy()[0] # magnitude = np.sqrt((face_feature**2).sum()) magnitude = np.linalg.norm(face_feature) face_feature = face_feature / magnitude face_feature_dict[face_label]['list'].append(face_feature) for face_label in face_feature_dict.keys(): if len(face_feature_dict[face_label]['list']) == 0: face_feature_dict.pop(face_label) print(face_label) continue face_feature_dict[face_label]['avg'] = np.average( face_feature_dict[face_label]['list'], axis=0) face_feature_dict[face_label]['var'] = np.var( face_feature_dict[face_label]['list']) total_face = 0 correct_face = 0 for face_1 in face_feature_dict.keys(): for feature_1 in face_feature_dict[face_1]['list']: min_dist = None min_arg = None for face_2 in face_feature_dict.keys(): # tmp_dist = np.linalg.norm(feature_1-face_feature_dict[face_2]['avg']) tmp_dist = cosine_distance(feature_1, face_feature_dict[face_2]['avg']) if min_dist is None or min_dist > tmp_dist: min_dist = tmp_dist min_arg = face_2 if min_arg == face_1 and min_dist < 1.5e-4: # print(min_dist) correct_face += 1 total_face += 1 # label2AvgFeature = dict() # for key in face_feature_dict.keys(): # label2AvgFeature[key] = face_feature_dict[key]['avg'] # np.save('data/MOOC1/label2AvgFeature.npy', label2AvgFeature) # save_model(face_encoder, 'FaceEncoder') print("match ratio:%.2f" % (correct_face / total_face))
def rank_baseline(self, input, num_rank=5): """Uses base model features.""" ref_features, rank_features = input ref_embed = ref_features.mean(dim=1, keepdim=True) dist = cosine_distance(ref_embed, rank_features, dim=-1) return dist
def find_outlier_baseline(self, input): """Uses base model features.""" set_embed = input.mean(dim=1, keepdim=True) dist = cosine_distance(set_embed, input, dim=-1) return dist, torch.argsort(dist)
def train(config): ## set pre-process prep_dict = {} prep_config = config["prep"] prep_dict["source"] = prep.image_train(**config["prep"]['params']) prep_dict["target"] = prep.image_train(**config["prep"]['params']) if prep_config["test_10crop"]: prep_dict["test"] = prep.image_test_10crop(**config["prep"]['params']) else: prep_dict["test"] = prep.image_test(**config["prep"]['params']) ## prepare data dsets = {} dset_loaders = {} data_config = config["data"] train_bs = data_config["source"]["batch_size"] test_bs = data_config["test"]["batch_size"] dsets["source"] = ImageList(open(data_config["source"]["list_path"]).readlines(), \ transform=prep_dict["source"]) dset_loaders["source"] = DataLoader(dsets["source"], batch_size=train_bs, \ shuffle=True, num_workers=4, drop_last=True) dsets["target"] = ImageList(open(data_config["target"]["list_path"]).readlines(), \ transform=prep_dict["target"]) dset_loaders["target"] = DataLoader(dsets["target"], batch_size=train_bs, \ shuffle=True, num_workers=4, drop_last=True) if prep_config["test_10crop"]: for i in range(10): dsets["test"] = [ImageList(open(data_config["test"]["list_path"]).readlines(), \ transform=prep_dict["test"][i]) for i in range(10)] dset_loaders["test"] = [DataLoader(dset, batch_size=test_bs, \ shuffle=False, num_workers=4) for dset in dsets['test']] else: dsets["test"] = ImageList(open(data_config["test"]["list_path"]).readlines(), \ transform=prep_dict["test"]) dset_loaders["test"] = DataLoader(dsets["test"], batch_size=test_bs, \ shuffle=False, num_workers=4) class_num = config["network"]["params"]["class_num"] ## set base network net_config = config["network"] base_network = net_config["name"](**net_config["params"]) base_network = base_network.cuda() with torch.no_grad(): cluster_data_loader = {} cluster_data_loader["source"] = DataLoader(dsets["source"], batch_size=100, \ shuffle=True, num_workers=0, drop_last=True) cluster_data_loader["target"] = DataLoader(dsets["source"], batch_size=100, \ shuffle=True, num_workers=0, drop_last=True) ## add additional network for some methods if config["loss"]["random"]: random_layer = network.RandomLayer([base_network.output_num(), class_num], config["loss"]["random_dim"]) ad_net = network.AdversarialNetwork(config["loss"]["random_dim"], 1024) else: random_layer = None ad_net = network.AdversarialNetwork(base_network.output_num() * class_num, 1024) if config["loss"]["random"]: random_layer.cuda() ad_net = ad_net.cuda() parameter_list = base_network.get_parameters() + ad_net.get_parameters() ## set optimizer optimizer_config = config["optimizer"] optimizer = optimizer_config["type"](parameter_list, \ **(optimizer_config["optim_params"])) param_lr = [] for param_group in optimizer.param_groups: param_lr.append(param_group["lr"]) schedule_param = optimizer_config["lr_param"] lr_scheduler = lr_schedule.schedule_dict[optimizer_config["lr_type"]] gpus = config['gpu'].split(',') if len(gpus) > 1: ad_net = nn.DataParallel(ad_net, device_ids=[int(i) for i in gpus]) base_network = nn.DataParallel(base_network, device_ids=[int(i) for i in gpus]) # dset_loaders["ps_target"]=[] ## train len_train_source = len(dset_loaders["source"]) # len_train_target = len(dset_loaders["ps_target"]) transfer_loss_value = classifier_loss_value = total_loss_value = 0.0 best_acc = 0.0 for i in range(config["num_iterations"]): lamb = adaptation_factor((i+1)/10000) cls_lamb = adaptation_factor(5*(i+1)/10000) epoch = int(i / len_train_source) if i% len_train_source ==0: testing = True pl_update=True print_loss =True # print("epoch: {} ".format(int(i / len_train_source))) if epoch % 5 ==0 and pl_update: pl_update= False # del dset_loaders["ps_target"] pseudo_labeled_targets,target_g_ctr, source_g_ctr = pseudo_labeling(base_network, cluster_data_loader, class_num) global_source_ctr = source_g_ctr.detach_() global_target_ctr = target_g_ctr.detach_() if len(pseudo_labeled_targets["label_list"]) !=0: print("new pl at epoch {}".format(epoch)) pseudo_dataset = PS_ImageList(pseudo_labeled_targets, transform=prep_dict["target"]) dset_loaders["ps_target"] = DataLoader(pseudo_dataset, batch_size=train_bs, \ shuffle=False, num_workers=0, drop_last=True) len_train_target = len(dset_loaders["ps_target"]) else: print("no pl at epoch {}".format(epoch)) # print("pseudo labeling done") # print(i) # if i % config["test_interval"] == config["test_interval"] - 1: if epoch % 5 ==0 and testing and i>0: base_network.train(False) temp_acc,v_loss = image_classification_test(dset_loaders, \ base_network, test_10crop=prep_config["test_10crop"]) temp_model = nn.Sequential(base_network) if temp_acc > best_acc: best_acc = temp_acc best_model = temp_model log_str = "iter: {:05d}, precision: {:.5f}".format(i, temp_acc) config["out_file"].write(log_str + "\n") config["out_file"].flush() print(log_str) testing=False now = datetime.now() current_time = now.strftime("%H:%M:%S") print("epoch: {} ".format(int(i / len_train_source))) print("time: {} ".format(current_time)) print("best acc: {} ".format(best_acc)) print("loss: {} ".format(v_loss)) print("adaptation rate : {}".format(lamb)) print("learning rare : {} {} {} {}".format(optimizer.param_groups[0]["lr"],optimizer.param_groups[1]["lr"],optimizer.param_groups[2]["lr"],optimizer.param_groups[3]["lr"])) print("------------") if i % config["snapshot_interval"] == 0: torch.save(nn.Sequential(base_network), osp.join(config["output_path"], \ "iter_{:05d}_model.pth.tar".format(i))) loss_params = config["loss"] ## train one iter base_network.train(True) ad_net.train(True) optimizer = lr_scheduler(optimizer, i, **schedule_param) optimizer.zero_grad() ### if i % len_train_source == 0: iter_source = iter(dset_loaders["source"]) if i % len_train_target == 0: # print(i,len_train_target) iter_target = iter(dset_loaders["ps_target"]) try: inputs_source, labels_source, _ = iter_source.next() inputs_target, labels_target = iter_target.next() except StopIteration: iter_target = iter(dset_loaders["ps_target"]) inputs_target, labels_target = iter_target.next() inputs_source, inputs_target, labels_source, labels_target = inputs_source.cuda(), inputs_target.cuda(), labels_source.cuda(), labels_target.cuda() features_source, outputs_source = base_network(inputs_source) features_target, outputs_target = base_network(inputs_target) ##class_aware batch_source_centroids = utils.get_batch_centers(features_source, labels_source, class_num) batch_target_centroids = utils.get_batch_centers(features_target,labels_target, class_num) # if i==0: # global_source_ctr = batch_source_centroids # global_target_ctr = batch_target_centroids # if i>0: batch_source_centroids = ctr_adapt_factor* global_source_ctr + (1- ctr_adapt_factor) * batch_source_centroids batch_target_centroids = ctr_adapt_factor * global_target_ctr + (1 - ctr_adapt_factor) * batch_target_centroids global_source_ctr = batch_source_centroids.clone().detach_() global_target_ctr = batch_target_centroids.clone().detach_() # # global_source_ctr = global_source_ctr.cpu().data.numpy() # global_target_ctr.detach_() # ctr_alignment_loss = utils.cosine_distance(global_source_ctr,global_target_ctr,cross=False) # source_p2c_Distances = 0 - utils.cosine_distance(features_source, global_source_ctr, cross=True) # # target_p2c_Distances = 0 - utils.cosine_distance(features_target, global_target_ctr, cross=True) # # # # zero_ctrs_s = torch.unique(torch.where(global_source_ctr==0)[0]) # zero_ctrs_t = torch.unique(torch.where(global_target_ctr == 0)[0]) alignment_index = [] identity = np.eye(class_num) ctr_alignment_count =0 pos = [] post = [] neg =[] negt =[] index_s = np.empty([0,1]) index_t = np.empty([0,1]) itt=0 triplets ={} # with torch.no_grad(): labels = labels_source.cpu().data.numpy() labelt = labels_target.cpu().data.numpy() # zero_ctrs_s = zero_ctrs_s.cpu().data.numpy() # zero_ctrs_t = zero_ctrs_t.cpu().data.numpy() #####npair # labels = labels.cpu().data.numpy() n_pairs = [] for label in set(labels): label_mask = (labels == label) label_indices = np.where(label_mask)[0] if len(label_indices) < 1: continue anchor = np.random.choice(label_indices, 1, replace=False) n_pairs.append([anchor, np.array([label])]) n_pairs = np.array(n_pairs) n_negatives = [] for i in range(len(n_pairs)): negative = np.concatenate([n_pairs[:i, 1], n_pairs[i + 1:, 1]]) n_negatives.append(negative) n_negatives = np.array(n_negatives) n_pairs_s = torch.LongTensor(n_pairs) n_neg_s = torch.LongTensor(n_negatives) n_pairs = [] for label in set(labelt): label_mask = (labelt == label) label_indices = np.where(label_mask)[0] if len(label_indices) < 1: continue anchor = np.random.choice(label_indices, 1, replace=False) n_pairs.append([anchor, np.array([label])]) n_pairs = np.array(n_pairs) n_negatives = [] for i in range(len(n_pairs)): negative = np.concatenate([n_pairs[:i, 1], n_pairs[i + 1:, 1]]) n_negatives.append(negative) n_negatives = np.array(n_negatives) n_pairs_t = torch.LongTensor(n_pairs) n_neg_t = torch.LongTensor(n_negatives) # return torch.LongTensor(n_pairs), torch.LongTensor(n_negatives) ##### for it in range(class_num): label_mask = (labels == it) label_maskt = (labelt == it) idx = np.where(label_mask)[0] idxt = np.where(label_maskt)[0] # idx = torch.flatten(torch.nonzero(labels_source== torch.tensor(it).cuda())) if len(idx) !=0: index_s =np.append(index_s,idx) pos += [it for cc in range(len(idx))] mask = 1- identity[it,:] neg_id = np.nonzero(mask.flatten())[0].flatten() # neg_idx = np.where(np.in1d(neg_id,zero_ctrs_s)!=True)[0] neg += [[neg_id] for cc in range(len(idx))] if len(idxt) !=0: index_t = np.append(index_t, idxt) post += [it for cc in range(len(idxt))] maskt = 1- identity[it,:] neg_idt = np.nonzero(maskt.flatten())[0].flatten() # neg_idxt = np.where(np.in1d(neg_idt, zero_ctrs_t))[0] negt += [[neg_idt] for cc in range(len(idxt))] # negt += [[neg_idt] for cc in range(len(idxt))] # alignment_ctr_idx =idx[torch.nonzero(torch.where(idx ==idxt, idx,0))] if len(idx) != 0 and len(idxt) !=0: ctr_alignment_count +=1 alignment_index +=[it] # alignment_loss +=[utils.cosine_distance(batch_source_centroids[it], batch_source_centroids[it], cross=False)] # tempp = torch.cat(source_loss,0) # posetives_s = torch.cat(pos, dim=0) # negatives_s = torch.cat(neg, dim=0) # posetives_t = torch.cat(post, dim=0) # negatives_t = torch.cat(negt, dim=0) # a_i = torch.LongTensor(index_s.flatten()).cuda() # a_p = torch.LongTensor(pos).cuda() # a_n = torch.LongTensor(neg).cuda() ctr_alignment_loss =0 anchors_s = features_source[index_s.flatten(),:] positive_s = global_source_ctr[pos,:] negative_s = global_source_ctr[neg].squeeze(1) # n_pairs_s = n_pairs_s.cuda().squeeze(2) # n_neg_s = n_neg_s.cuda().squeeze(2) # anchors_s = features_source[n_pairs_s[:, 0]] # positive_s = global_source_ctr[n_pairs_s[:, 1]] # negative_s = global_source_ctr[n_neg_s] # # n_pairs_t = n_pairs_t.cuda().squeeze(2) # # n_neg_t = n_neg_t.cuda().squeeze(2) # anchors_t = features_source[n_pairs_t[:, 0]] # positive_t = global_source_ctr[n_pairs_t[:, 1]] # negative_t = global_source_ctr[n_neg_t] # anchors_s.retain_graph=True # positive_s.retain_graph=True # negative_s.retain_graph=True anchors_t = features_target[index_t.flatten(), :] positive_t = global_target_ctr[post, :] negative_t = global_target_ctr[negt].squeeze(1) # FAT_loss = torch.empty([],requires_grad=True) # FAT_loss.requires_grad = True # FAT_loss.retain_grad() # nfat_s = Variable(n_pair_loss(anchors_s,positive_s, negative_s,class_num,train_bs)) # nfat_t = Variable(n_pair_loss(anchors_t,positive_t, negative_t,class_num,train_bs)) # FAT_loss.requires_grad = True # FAT_loss.retain_grad() FAT_loss = n_pair_loss(anchors_s,positive_s, negative_s,class_num,train_bs) + n_pair_loss(anchors_t,positive_t, negative_t,class_num,train_bs)/2 if len(alignment_index) != 0: ctr_alignment_loss = torch.sum(utils.cosine_distance(batch_source_centroids[alignment_index], batch_target_centroids[alignment_index], cross=False))#/ctr_alignment_count # source_batch_FAT_Loss = torch.mean(torch.cat(source_loss,0), 0)/class_num # target_batch_FAT_Loss = torch.mean(torch.cat(target_loss,0),0)/class_num # # FAT_loss = source_batch_FAT_Loss.add(target_batch_FAT_Loss) ## # print("train loss: ", FAT_loss) # ctr_alignment_loss.grad_required =True # ctr_alignment_loss.retain_grad() features = torch.cat((features_source, features_target), dim=0) outputs = torch.cat((outputs_source, outputs_target), dim=0) softmax_out = nn.Softmax(dim=1)(outputs) if config['method'] == 'CDAN+E': entropy = loss.Entropy(softmax_out) transfer_loss = loss.CDAN([features, softmax_out], ad_net, entropy, network.calc_coeff(i), random_layer) elif config['method'] == 'CDAN': transfer_loss = loss.CDAN([features, softmax_out], ad_net, None, None, random_layer) elif config['method'] == 'DANN': transfer_loss = loss.DANN(features, ad_net) else: raise ValueError('Method cannot be recognized.') classifier_loss = nn.CrossEntropyLoss()(outputs_source/(2), labels_source) total_loss = loss_params["trade_off"] * (transfer_loss) + classifier_loss if lamb >.1: cls_lamb = 1.0 else: cls_lamb = 10*lamb # total_loss = lamb * ( FAT_loss + 10*ctr_alignment_loss) + (transfer_loss) + cls_lamb*classifier_loss # total_loss =transfer_loss + lamb * (FAT_loss + ctr_alignment_loss) + classifier_loss # FAT_loss.backward(retain_graph=True) # optimizer.zero_grad() total_loss.backward() optimizer.step() # my_lr_scheduler.step() if epoch % 5 ==0 and print_loss: print("fat loss ", FAT_loss)#.grad_fn, FAT_loss.requires_grad) print("ctr align: ", ctr_alignment_loss) print("tot: ", total_loss) print("clss: ",classifier_loss) print("trs: ", transfer_loss) print("++++++++++++++++++++++++end of epoch++++++++++++++++++++") print_loss =False