def FW_within_catergory(data_, cid_, all_pid_, all_did_): W_class_ = np.asarray([2, 6, 7, 8, 9, 10]) + offset_s2s_category_ FW_clustering_within_category_F_ = { 0: [8, 9, 11], 1: [20], 2: [1, 15], 3: [3, 21], 4: [10, 12], 5: [13, 14, 16, 17, 18, 19] } FW_ = {} for ii_ in range(len(W_class_)): # S2S temp_W_category = W_class_[ii_] temp_W_idx = np.where(cid_ == temp_W_category)[0] temp_W_feat = data_[temp_W_idx, :] # DF temp_F_categories = FW_clustering_within_category_F_[ii_] temp_F_idx = [] for jj_ in temp_F_categories: temp_idx_ = np.where(cid_ == jj_)[0] temp_F_idx = np.concatenate((temp_F_idx, temp_idx_)).astype(int) temp_F_feat = data_[temp_F_idx, :] temp_F_W_idx = np.concatenate((temp_F_idx, temp_W_idx)).astype(int) temp_F_W_pid_ = all_pid_[temp_F_W_idx] temp_F_W_did_ = all_did_[temp_F_W_idx] feat_ = np.concatenate((temp_F_feat, temp_W_feat)) c, num_clust, req_c = FINCH(feat_, initial_rank=None, req_clust=None, distance='cosine', verbose=False) #verbose=True FW_['{}'.format(W_class_[ii_])] = [] FW_['{}'.format(W_class_[ii_])].append({ 'idx': temp_F_W_idx, 'c': c, 'nc': num_clust, 'pid_c': temp_F_W_pid_, 'did_c': temp_F_W_did_ }) return FW_
def W_within_catergory(data_, cid_): W_clustering_within_category = np.asarray([0, 1, 3, 4, 5 ]) + offset_s2s_category_ W_ = {} for ii_ in W_clustering_within_category: temp_idx = np.where(cid_ == ii_)[0] feat_ = data_[temp_idx, :] c, num_clust, req_c = FINCH(feat_, initial_rank=None, req_clust=None, distance='cosine', verbose=False) #verbose=True W_['{}'.format(ii_)] = [] W_['{}'.format(ii_)].append({'idx': temp_idx, 'c': c, 'nc': num_clust}) return W_
t6 = time.time() print("classification time = ", t6 - t5) nu = 0 features_dict_incremental = OrderedDict() class_to_process = [] p_max, i_max = torch.max(normalized_tensor, axis=1) for k in range(normalized_tensor.shape[0]): if i_max[k] == 0: # predicted unnkwon unknown residual_dict[image_names[k]] = FV[k, :].numpy() print(f"len(residual_dict) = {len(residual_dict)}") if len(residual_dict) > 0: image_names_residual, FVs_residual = zip(*residual_dict.items()) data = np.vstack(list(FVs_residual)) c_all, num_clust, req_c = FINCH(data) modified_index_partition_selected = min(len(num_clust) - 1, index_partition_selected) cluster_labels = c_all[:, modified_index_partition_selected] number_of_clusters = num_clust[modified_index_partition_selected] # number of clusters after clustering. for cluster_number in range(number_of_clusters): # number of clusters after clustering. index = [iii for iii in range(len(cluster_labels)) if cluster_labels[iii] == cluster_number] nu = nu + 1 class_number = int(nu + number_of_discovered_classes + number_of_known_classes) features_dict_incremental[class_number] = torch.from_numpy( np.array([FVs_residual[jjj] for jjj in index]) ) # .double()#.cuda() class_to_process.append(class_number) if ((len(class_to_process) > 0) and (number_of_discovered_classes > 0)) or (len(class_to_process) > 2): SCAIL.fit(features_dict_incremental)
FV_supervised, _ = cnn_model_supervised(x_supervised) FV_moco_places, _ = cnn_model_moco_places(x_moco) FV = torch.cat((FV_supervised, FV_moco_places), 1) feature_storage[n : (n + FV.size(0)), 0] = y feature_storage[n : (n + FV.size(0)), 1:] = FV.cpu() image_list = image_list + list(A) n = n + FV.size(0) assert len(image_list) == n data_ = feature_storage.detach().clone().cpu().numpy() data_ = data_[:n, :] feature = data_[:, 1:] true_label = data_[:, 0] c_all, num_clust, req_c = FINCH(feature) cluster_labels = c_all[:, 1] number_of_clusters = num_clust[1] # number of clusters after clustering. storage_dict = dict() for L, K, A in zip(true_label, cluster_labels, image_list): if K in storage_dict: storage_dict[K].append((L, A)) else: storage_dict[K] = [(L, A)] torch.save(storage_dict, f"/scratch/mjafarzadeh/clusters_all_b3_SP_{data_name}.pth") del feature_storage, data_, storage_dict t5 = time.time() print(f"extracting {data_name} time = ", t5 - t4)