def m_metric(mask, predict, thr): batchsize = mask.shape[0] mask = mask.squeeze(1) predict = predict.squeeze(1) predict[predict < thr] = 0 predict[predict >= thr] = 1 mask[mask < thr] = 0 mask[mask >= thr] = 1 predict = predict.numpy().astype(np.int16) mask = mask.numpy().astype(np.int16) m_dsc = [] m_acc = [] m_ppv = [] m_sen = [] m_hausdorff_distance = [] for i in range(batchsize): m_dsc.append(dice_coef(predict[i], mask[i])) m_acc.append(accuracy(predict[i], mask[i])) m_ppv.append(ppv(predict[i], mask[i])) m_sen.append(sensitivity(predict[i], mask[i])) m_hausdorff_distance.append(hausdorff_distance(predict[i], mask[i])) return np.nanmean(m_dsc), np.nanmean(m_acc), np.nanmean(m_ppv), np.nanmean( m_sen), np.nanmean(m_hausdorff_distance)
def best_fit_scale(lo_points_pos, hi_points_pos): best_scale, best_dist = 0.15, float('inf') min_scale, max_scale = 0, 1 tries, max_tries = 0, 10 sample_size = 50 better_scale_findable = True while tries < max_tries and better_scale_findable: scales, dists = [], [] for sample in range(sample_size): if sample == 0: scale = best_scale else: is_scale = False scale_tries, max_scale_tries = 0, 10 while not is_scale and scale_tries < max_scale_tries: scale = np.random.normal(best_scale, (max_scale - min_scale) * 0.5/(tries+1)) scale_tries += 1 if scale >= min_scale and scale < max_scale: is_scale = True if not is_scale: scale = np.random.uniform(min_scale, max_scale) scales.append(scale) dists.append(hausdorff_distance(lo_points_pos, hi_points_pos * scale, 'manhattan')) min_dist, i = min((dist, i) for (i, dist) in enumerate(dists)) if best_dist > min_dist: best_scale, best_dist = scales[i], min_dist else: better_scale_findable = False return best_scale, best_dist
def cal_hausdorff_distance(pred,target): pred = np.array(pred.contiguous()) target = np.array(target.contiguous()) result = hausdorff_distance(pred,target,distance="euclidean") return result
def distance(x, y, dist): if 'euclidean' in dist: tmp = np.sum((x[0] - y[0])**2 + (x[1] - y[1])**2) if 'cityblock' in dist: tmp = np.sum(abs(x[0] - y[0]) + abs(x[1] - y[1])) if 'hausdorff' in dist: tmp = hausdorff_distance(x.T, y.T) return tmp
def Limeng(n,b): a = readData() labels = np.zeros(shape=(958,20)) labels.dtype = 'int64' cluster_centers = np.zeros(shape=(n,40)) for i in range(20): #print(i) clf = KMeans(n_clusters=n, random_state=9) y_pred = clf.fit_predict(a[:, i, 2:4]) labels[:, i] = clf.labels_ cluster_centers[:, 2*i:(2*i+2)] = clf.cluster_centers_ newpaths = [] for i in range(958): newpath = "" for j in range(20): string = "L"+ str(labels[i, j]) newpath += string newpaths.append(newpath) result = Counter(newpaths) newpathsdict = dict(result) #随机生成轨迹补足数量 while(len(newpathsdict)<958): key = "" for i in range(20): string = "L" + str(random.randint(0,n-1)) key += string newpathsdict.setdefault(key, 0) # 取出truecount truecount = list(newpathsdict.values()) cA, cD = pywt.dwt(truecount, 'haar') # 加噪重构 lapnoiseca = [] b = 2 for i in range(479): ln = math.sqrt(2) * np.random.laplace(0, b) lapnoiseca.append(ln) lapnoisecd = [] for i in range(479): ln = math.sqrt(2) * np.random.laplace(0, b) lapnoisecd.append(ln) cAnoise = np.array(lapnoiseca) + cA cDnoise = np.array(lapnoisecd) + cD noisecount = pywt.idwt(cAnoise, cDnoise, 'haar') # # 作图 # plt.plot(x,a,"b.-",markersize=8) # plt.plot(x,y,"r.",markersize=8) # plt.plot(x,y_,"g.-",markersize=8) # plt.show() NMI=metrics.normalized_mutual_info_score(truecount, noisecount) # print(NMI) truecount.resize(1,958) noisecount.resize(1,958) hau_dis = hausdorff_distance(truecount, noisecount, distance="euclidean") return NMI, hau_dis
def Limeng(n, b): a = readData() labels = np.zeros(shape=(351, 20)) labels.dtype = 'int64' cluster_centers = np.zeros(shape=(n, 40)) for i in range(20): #print(i) clf = KMeans(n_clusters=n, random_state=9) y_pred = clf.fit_predict(a[:, i, 2:4]) labels[:, i] = clf.labels_ cluster_centers[:, 2 * i:(2 * i + 2)] = clf.cluster_centers_ newpaths = [] for i in range(351): newpath = "" for j in range(20): string = "L" + str(labels[i, j]) newpath += string newpaths.append(newpath) result = Counter(newpaths) newpathsdict = dict(result) #计算u u = mean(list(newpathsdict.values())) #随机生成轨迹补足数量 while (len(newpathsdict) < 351): key = "" for i in range(20): string = "L" + str(random.randint(0, n - 1)) key += string newpathsdict.setdefault(key, 0) valueslist = sorted(list(newpathsdict.values()), reverse=True) newpathslist = list_sort_by_value(newpathsdict) #生成噪声 lapnoise = [] for i in range(351): ln = np.random.laplace(u, b) while ln > 2 * u or ln < 0: ln = np.random.laplace(u, b) lapnoise.append(ln) #添加噪声 vcarr = np.array(valueslist) lcarr = np.array(lapnoise) noisecount = vcarr + lcarr #保序回归 x = np.arange(351) y = noisecount y_ = IsotonicRegression(increasing=False).fit_transform(x, y) # # 作图 # plt.plot(x,a,"b.-",markersize=8) # plt.plot(x,y,"r.",markersize=8) # plt.plot(x,y_,"g.-",markersize=8) # plt.show() NMI = metrics.normalized_mutual_info_score(vcarr, y_) # print(NMI) vcarr.resize(351, 1) y_.resize(351, 1) hau_dis = hausdorff_distance(vcarr, y_, distance="euclidean") return NMI, hau_dis
def hausdorff_wrapper(A, B): # this is some weird thing to prevent a C_Contiguous error # https://stackoverflow.com/questions/26778079/valueerror-ndarray-is-not-c-contiguous-in-cython #A = A.copy(order='C') #B = B.copy(order='C') dist = hausdorff_distance(A.astype(float), B.astype(float)) return (dist)
def hausdorff_distance_channel_wise(output, target): outputs = [] # 一般 C 代表 GT 的维度 for i in range(output.shape[1]): one_batch = [] for b in range(output.shape[0]): hd = hausdorff_distance(output[b, i, :, :], target[b, i, :, :]) one_batch.append(hd) # 标量 outputs.append(sum(one_batch)) d = np.stack(outputs) # 向量 return d
def hausdorff_index(output, target, name="euclidean"): if torch.is_tensor(output): output = output.data.cpu().numpy() if torch.is_tensor(target): target = target.data.cpu().numpy() assert output.shape == target.shape # assert len(output.shape) == 4 size = output.shape[0] sum = 0 for i in range(size): sum += hausdorff_distance(output[i, 0], target[i, 0], distance=name) return sum / size
def Hausdorff(pred_edge, true_edge): pred_edge = np.asarray(pred_edge > 0) true_edge = np.asarray(true_edge > 0) _, h, w = true_edge.shape pred_coords = get_coords(pred_edge[0]) true_coords = get_coords(true_edge[0]) if len(pred_coords) == 0 or len(true_coords) == 0: hsdf = float("inf") else: hsdf = hausdorff_distance(pred_coords, true_coords) / w return hsdf
def evaluate_metric(data_output, data_target): scores = {'mse': [], 'rmse': [], 'hd': []} for j in range(data_output.shape[0]): X = data_output[j, :, :] Y = data_target[j, :, :] mse = (((np.sum( (X - Y)**2)) / (data_output.shape[1] * data_output.shape[2]))) rmse = np.sqrt( np.sum((X - Y)**2) / (data_output.shape[1] * data_output.shape[2])) scores['mse'].append(mse) scores['rmse'].append(rmse) tmp_hausdorff = hausdorff_distance(X, Y) scores['hd'].append(tmp_hausdorff) scores_final = {'mse': [], 'rmse': [], 'hd': []} scores_final['mse'].append(np.mean(scores['mse'])) scores_final['rmse'].append(np.mean(scores['rmse'])) scores_final['hd'].append(np.mean(scores['hd'])) return scores_final
def virtual_test(): output, gt = torch.zeros(3, 2, 5, 5), torch.zeros(3, 5, 5).long() # print(classwise_iou(output, gt)) pred = torch.Tensor([[[[0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[1, 0, 0, 1], [0, 1, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1]]]]) gt = torch.Tensor([[[[0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[1, 0, 0, 1], [0, 1, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1]]]]) pred2 = torch.Tensor([[[[0, 0.6, 0.4, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]]]]) pred2[pred2 >= 0.5] = 1 pred2[pred2 < 0.5] = 0 gt2 = torch.Tensor([[[[0, 1, 0, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]]]]) print("dice " + str(dice_index(pred, gt))) # print("iouReal? " + str(iou_score(pred, gt))) print("dice " + str(dice_index(pred2, gt2))) print("dice0 " + str(dice_index(pred2[:, 0:1, :], gt2[:, 0:1, :]))) print("dice1 " + str(dice_index(pred2[:, 1:2, :], gt2[:, 1:2, :]))) print("dice2 " + str(dice_index(pred2[:, 2:3, :], gt2[:, 2:3, :]))) # print("iouReal? " + str(iou_score(pred2, gt2))) theP = pred2[0, 0, :].detach().cpu().numpy() theT = gt2[0, 0, :].detach().cpu().numpy() print(hausdorff_distance(theP, theT, distance="haversine")) test_metrics = [ 'dice', 'jaccard', 'precision', 'recall', 'fpr', 'fnr', 'vs', 'hd', 'hd95', 'msd', 'mdsd', 'stdsd' ] quality = sg.computeQualityMeasures(theP, theT, (1, 1), test_metrics) print(quality)
def Extrenal_metric(gt,pred): result1 = [] # dice result2 = [] # jaccard result3 = [] # hausdorff for i in range(gt.shape[0]): x1 = dice2_coef(gt[i:i+1], pred[i:i+1]) result1.append(x1) x2 = jaccard(gt[i:i+1], pred[i:i+1]) result2.append(x2) with tf.Session() as sess: y1 = sess.run(result1) y2 = sess.run(result2) print("Dice,Jaccard,Hausdorff") print('%.2f±%.2f[%.2f,%.2f]'%(sum(y1)/len(y1)*100, np.std(y1)*100, min(y1)*100, max(y1)*100), end='\t') print('%.2f±%.2f[%.2f,%.2f]'%(sum(y2)/len(y2)*100, np.std(y2)*100, min(y2)*100, max(y2)*100), end='\t') for i in range(gt.shape[0]): a = gt[i, :, :, 0] b = pred[i, :, :, 0] x3 = hausdorff_distance(a,b,distance='manhattan')#distance="euclidean",distance="chebyshev",distance="cosine" result3.append(x3) y3 = result3 print('%.2f±%.2f[%.2f,%.2f]'%(sum(y3)/len(y3), np.std(y3), min(y3), max(y3)), end='\t') '''
print(str(max(y1)), end='\n') print("Jaccard:mean std min max", end='\t') mean = sum(y2) / len(y2) print(str(mean), end='\t') std = np.std(y2) print(str(std), end='\t') print(str(min(y2)), end='\t') print(str(max(y2)), end='\n') result3 = [] for i in range(gt.shape[0]): a = gt[i, :, :, 0] b = pred[i, :, :, 0] x3 = hausdorff_distance( a, b, distance='manhattan' ) #distance="euclidean",distance="chebyshev",distance="cosine" result3.append(x3) y3 = result3 np.savetxt( 'E:\\D1_Paper_Cardiac_Segmentation\\Test_results\\UNet_pp\\lge\\500\\hausdorff.csv', y3) print("Hausdorff:mean std min max", end='\t') mean = sum(y3) / len(y3) print(str(mean), end='\t') std = np.std(y3) print(str(std), end='\t') print(str(min(y3)), end='\t') print(str(max(y3)), end='\n')
def clustering(adata, sequence_coordinates, cluster_chains, cluster_strength, cluster_labels_1, n_clusters=None, method=None, smoothing=False, distance='cosine'): average_cl_d = np.zeros((len(cluster_chains), len(cluster_chains))) for i in tqdm(range(len(average_cl_d))): for j in range(len(average_cl_d)): average_cl_d[i, j] = hausdorff_distance(cluster_chains[i], cluster_chains[j], distance=distance) average_cl_d_affinity = -average_cl_d average_cl_d_affinity = sparse.csr_matrix( minmax_scale(average_cl_d_affinity, axis=1)) print('Clustering using hausdorff distances') if n_clusters is None: #cluster_labels = OPTICS(metric="precomputed").fit_predict(average_cl_d) #cluster_labels = AffinityPropagation(affinity="precomputed", convergence_iter=100).fit_predict(average_cl_d_affinity) cluster_labels = Louvain( resolution=0.75).fit_transform(average_cl_d_affinity) clusters = np.unique(cluster_labels) else: if method == 'kmeans': cluster_labels = KMeans( n_clusters=n_clusters, precompute_distances=True).fit_predict(average_cl_d_affinity) clusters = np.unique(cluster_labels) all_trajectories_labels = np.zeros((len(cluster_labels_1))) for i in range(len(cluster_labels)): all_trajectories_labels[np.where( cluster_labels_1 == i)] = cluster_labels[i] cluster_strength = np.asarray(cluster_strength, dtype=int) final_cluster_strength = np.empty([len(clusters)], dtype=int) for i in range(len(clusters)): final_cluster_strength[i] = len( np.where(all_trajectories_labels == i)[0].astype(int)) print('Forming trajectory by aligning clusters') # pairwise allignment until only 1 avg. trajectory remains per cluster final_cluster = [] all_chains = sequence_coordinates for i in tqdm(range(len(clusters))): index_cluster = np.where(all_trajectories_labels == clusters[i]) aver_tr_cluster = all_chains[index_cluster] if len(aver_tr_cluster) > 1: average_trajectory = aver_tr_cluster while len(average_trajectory) > 1: pair_range = range(len(average_trajectory)) pairs = [] for j in range( int((len(average_trajectory) - (len(average_trajectory) % 2)) / 2)): pairs.append( [pair_range[j * 2], pair_range[((j) * 2) + 1]]) if (len(average_trajectory) % 2) != 0: pairs.append([pair_range[-2], pair_range[-1]]) average_traj_while = [] for l in range(len(pairs)): alligned_trajectory = fastdtw( average_trajectory[pairs[l][0]], average_trajectory[pairs[l][1]])[1] alligned_trajectory = np.asarray(alligned_trajectory) alligned_tr = np.zeros((2, len(average_trajectory[0][0]))) alligned_av = np.zeros((len(alligned_trajectory), len(average_trajectory[0][0]))) for n in range(len(alligned_trajectory)): alligned_tr[0, :] = average_trajectory[pairs[l][0]][ alligned_trajectory[n, 0]] alligned_tr[1, :] = average_trajectory[pairs[l][1]][ alligned_trajectory[n, 1]] alligned_av[n, :] = np.mean(alligned_tr, axis=0) average_traj_while.append(alligned_av) average_trajectory = average_traj_while average_alligned_tr = average_trajectory[0] else: average_alligned_tr = aver_tr_cluster[0] final_cluster.append(average_alligned_tr) # TODO: Moving average concept needs development if smoothing: for i in range(len(final_cluster)): window_width = 4 cumsum_vec = np.cumsum(np.insert(final_cluster[i][:, 0], 0, 0)) ma_vec_Y = (cumsum_vec[window_width:] - cumsum_vec[:-window_width]) / window_width cumsum_vec = np.cumsum(np.insert(final_cluster[i][:, 1], 0, 0)) ma_vec_X = (cumsum_vec[window_width:] - cumsum_vec[:-window_width]) / window_width final_cluster[i] = np.zeros((len(ma_vec_X), 2)) final_cluster[i][:, 0] = ma_vec_Y final_cluster[i][:, 1] = ma_vec_X return final_cluster, final_cluster_strength
gt_mask = (cv2.imread(gt_mask_files[jj]) > 0).astype( np.uint8)[:, :, 0] pred_mask = (cv2.imread(pred_mask_files[jj]) > 0).astype(np.uint8) # make same size as GT pred_mask = cv2.resize(pred_mask, (gt_mask.shape[1], gt_mask.shape[0]), interpolation=cv2.INTER_AREA)[:, :, 0] # computation tn, fp, fn, tp = get_confusion_matrix_elements( gt_mask.flatten().tolist(), pred_mask.flatten().tolist()) overall_acc = (tp + tn) / (tp + tn + fp + fn) Hf = hausdorff_distance(gt_mask, pred_mask, distance='euclidean') jac_set = np.hstack([jac_score(gt_mask, pred_mask)]) dice_set = np.hstack([dice_score(gt_mask, pred_mask)]) f2_set = np.hstack([F2(gt_mask, pred_mask)]) PPV_set = np.hstack([precision(gt_mask, pred_mask)]) Rec_set = np.hstack([recall(gt_mask, pred_mask)]) acc = np.hstack([overall_acc]) jac_scores.append(jac_set) dice_scores.append(dice_set) f2_scores.append(f2_set) PPV_scores.append(PPV_set) Rec_scores.append(Rec_set) acc_scores.append(acc) Hfd_score.append(Hf)
weights=counts / data.shape[0], label=lab, histtype='bar') print(bins) plt.legend() plt.show() if __name__ == "__main__": rootp = r"F:\OneDrive\Projects_ongoing\13_HANDMRI_Muscle\3_CODE_learn_muscle_skinning\debug_folder\compare_mano" a = trimesh.load(os.path.join(rootp, "37.obj"), process=False) b = trimesh.load(os.path.join(rootp, "1636647278_306936\\3\\mano_test_37.obj"), process=False) c = trimesh.load(os.path.join(rootp, "piano\\1636647559_4610462\\3\\skin_0.obj"), process=False) d1 = hausdorff_distance(b.vertices, a.vertices, distance='euclidean') d2 = hausdorff_distance(c.vertices, a.vertices, distance='euclidean') print(d1, d2) d1, cb, dba, dab = hausdorff_distance_vismesh(b, a) d2, cc, dca, dac = hausdorff_distance_vismesh(c, a) print(d1, d2) histogram_vis(dba) histogram_vis(dab) histogram_vis(dca) histogram_vis(dac)
def myMechanism(n, e): a = readData() labels = np.zeros(shape=(958, 20)) labels.dtype = 'int64' cluster_centers = np.zeros(shape=(n, 40)) for i in range(20): # print(i) clf = KMeans(n_clusters=n, random_state=9) y_pred = clf.fit_predict(a[:, i, 2:4]) labels[:, i] = clf.labels_ cluster_centers[:, 2 * i:(2 * i + 2)] = clf.cluster_centers_ newpaths = [] for i in range(958): newpath = "" for j in range(20): string = "L" + str(labels[i, j]) newpath += string newpaths.append(newpath) result = Counter(newpaths) newpathsdict = dict(result) # 随机生成轨迹补足数量 while (len(newpathsdict) < 958): key = "" for i in range(20): string = "L" + str(random.randint(0, n - 1)) key += string newpathsdict.setdefault(key, 0) # 补齐2的n次方haar变换和重构 values = list(newpathsdict.values()) a = int(math.log(len(values), 2)) for index in range(int(math.pow(2, a + 1)) - len(values)): values.append(0) temp = buildHaarTreeList(values) # print(len(temp)) noise = addNoise(len(temp), e) c = [temp[i] + noise[i] for i in range(len(temp))] noisecounts = rebuildHaarTreeList(c) i = 0 newpathsdict2 = copy.deepcopy(newpathsdict); for key, value in newpathsdict.items(): newpathsdict[key] = noisecounts[i] i = i + 1 valueslist = sorted(list(newpathsdict.values()), reverse=True) newpathslist = list_sort_by_value(newpathsdict) truecounts = [] for item in newpathslist: truecounts.append(newpathsdict2.get(item)) # 根据一致性约束 trueconts 保序回归 x = np.arange(958) y = np.array(truecounts) y_ = IsotonicRegression(increasing=False).fit_transform(x, y) # print(y.shape) NMI = metrics.normalized_mutual_info_score(y_, y) mapeyy = mape(y_, y) maeyy = metrics.mean_absolute_error(y, y_) # hausdorff_distance y.resize(1,958) y_.resize(1,958) # print(y.shape) hau_dis = hausdorff_distance(y, y_, distance="euclidean") return NMI, hau_dis,mapeyy,maeyy
def preclustering(adata, all_seq_cluster, sequence_coordinates, basis="umap", distance='cosine'): if basis is None: map_state = adata.layers['Ms'] elif type(basis) == list: map_state = adata[:, basis].layers['Ms'] else: map_state = adata.obsm['X_' + basis] all_chains = sequence_coordinates # Calculate hausdorff distance print('Calculating hausdorff distances') distances = np.zeros((len(all_chains), len(all_chains))) for i in tqdm(range(len(distances))): for j in range(len(distances) - i): haus = hausdorff_distance(all_chains[j + i], all_chains[i], distance=distance) distances[i, j + i] = haus distances[j + i, i] = haus affinity = -distances affinity = sparse.csr_matrix(minmax_scale(affinity, axis=1)) # Perform clustering using hausdorff distance print('Clustering using hausdorff distances') #cluster_labels = AffinityPropagation(affinity="precomputed").fit_predict(affinity) cluster_labels = Louvain(resolution=1.25).fit_transform(affinity) clusters = np.unique(cluster_labels) # Pairwise alignment of chains within stage 1 clusters using DTW cluster_chains = [] cluster_strength = [] print('Forming trajectory by aligning clusters') # Pairwise allignment until only 1 avg. trajectory remains per cluster for i in tqdm(range(len(clusters))): index_cluster = np.where(cluster_labels == clusters[i])[0] aver_tr_cluster = all_chains[index_cluster] cluster_strength.append(len(index_cluster)) if len(aver_tr_cluster) > 1: average_trajectory = aver_tr_cluster while len(average_trajectory) > 1: pair_range = range(len(average_trajectory)) pairs = [] for j in range( int((len(average_trajectory) - (len(average_trajectory) % 2)) / 2)): pairs.append( [pair_range[j * 2], pair_range[((j) * 2) + 1]]) if (len(average_trajectory) % 2) != 0: pairs.append([pair_range[-2], pair_range[-1]]) average_traj_while = [] for l in range(len(pairs)): alligned_trajectory = fastdtw( average_trajectory[pairs[l][0]], average_trajectory[pairs[l][1]])[1] alligned_trajectory = np.asarray(alligned_trajectory) alligned_tr = np.zeros((2, len(average_trajectory[0][0]))) alligned_av = np.zeros((len(alligned_trajectory), len(average_trajectory[0][0]))) for n in range(len(alligned_trajectory)): alligned_tr[0, :] = average_trajectory[pairs[l][0]][ alligned_trajectory[n, 0]] alligned_tr[1, :] = average_trajectory[pairs[l][1]][ alligned_trajectory[n, 1]] alligned_av[n, :] = np.mean(alligned_tr, axis=0) average_traj_while.append(alligned_av) average_trajectory = average_traj_while average_alligned_tr = average_trajectory[0] else: average_alligned_tr = aver_tr_cluster[0] cluster_chains.append(average_alligned_tr) return cluster_chains, cluster_strength, cluster_labels
def dist_haus(u, v, pdist='euclidean'): """Hausdorff distance of two 2D images.""" u3 = three_d(u) v3 = three_d(v) return hausdorff_distance(u3, v3, pdist)
def Limeng(n, b): a = readData() labels = np.zeros(shape=(10000, 20)) labels.dtype = 'int64' cluster_centers = np.zeros(shape=(n, 40)) for i in range(20): #print(i) clf = KMeans(n_clusters=n, random_state=9) y_pred = clf.fit_predict(a[:, i, 2:4]) labels[:, i] = clf.labels_ cluster_centers[:, 2 * i:(2 * i + 2)] = clf.cluster_centers_ newpaths = [] for i in range(10000): newpath = "" for j in range(20): string = "L" + str(labels[i, j]) newpath += string newpaths.append(newpath) result = Counter(newpaths) newpathsdict = dict(result) #计算u u = mean(list(newpathsdict.values())) #随机生成轨迹补足数量 while (len(newpathsdict) < 10000): key = "" for i in range(20): string = "L" + str(random.randint(0, n - 1)) key += string newpathsdict.setdefault(key, 0) #生成噪声 lapnoise = [] for i in range(10000): ln = np.random.laplace(u, b) while ln > 2 * u or ln < 0: ln = np.random.laplace(u, b) lapnoise.append(ln) #添加噪声 newpathsdict备份至newpathsdict2,之后newpathsdict包含噪声 i = 0 newpathsdict2 = copy.deepcopy(newpathsdict) for key, value in newpathsdict.items(): newpathsdict[key] = value + lapnoise[i] i = i + 1 # dict按value排序得到两个list valueslist = sorted(list(newpathsdict.values()), reverse=True) newpathslist = list_sort_by_value(newpathsdict) # 按照newpathslist里顺序,取出newpathsdict2里truecount的值 truecountlist = [] for item in newpathslist: truecountlist.append(newpathsdict2.get(item)) #保序回归 x = np.arange(10000) y = np.array(truecountlist) y_ = IsotonicRegression(increasing=False).fit_transform(x, y) # # 作图 # plt.plot(x,a,"b.-",markersize=8) # plt.plot(x,y,"r.",markersize=8) # plt.plot(x,y_,"g.-",markersize=8) # plt.show() maeyy = metrics.mean_absolute_error(y_, y) NMI = metrics.normalized_mutual_info_score(y, y_) # print(NMI) y.resize(1, 10000) y_.resize(1, 10000) hau_dis = hausdorff_distance(y, y_, distance="euclidean") return NMI, hau_dis, maeyy
def main(): val_args = parse_args() args = joblib.load('models/%s/args.pkl' % val_args.name) if not os.path.exists('output/%s' % args.name): os.makedirs('output/%s' % args.name) print('Config -----') for arg in vars(args): print('%s: %s' % (arg, getattr(args, arg))) print('------------') joblib.dump(args, 'models/%s/args.pkl' % args.name) # create model print("=> creating model %s" % args.arch) model = mymodel.__dict__[args.arch](args) model = model.cuda() # Data loading code img_paths = glob( r'D:\Project\CollegeDesign\dataset\Brats2018FoulModel2D\testImage\*') mask_paths = glob( r'D:\Project\CollegeDesign\dataset\Brats2018FoulModel2D\testMask\*') val_img_paths = img_paths val_mask_paths = mask_paths #train_img_paths, val_img_paths, train_mask_paths, val_mask_paths = \ # train_test_split(img_paths, mask_paths, test_size=0.2, random_state=41) model.load_state_dict(torch.load('models/%s/model.pth' % args.name)) model.eval() val_dataset = Dataset(args, val_img_paths, val_mask_paths) val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=args.batch_size, shuffle=False, pin_memory=True, drop_last=False) if val_args.mode == "GetPicture": """ 获取并保存模型生成的标签图 """ with warnings.catch_warnings(): warnings.simplefilter('ignore') with torch.no_grad(): for i, (input, target) in tqdm(enumerate(val_loader), total=len(val_loader)): input = input.cuda() #target = target.cuda() # compute output if args.deepsupervision: output = model(input)[-1] else: output = model(input) #print("img_paths[i]:%s" % img_paths[i]) output = torch.sigmoid(output).data.cpu().numpy() img_paths = val_img_paths[args.batch_size * i:args.batch_size * (i + 1)] #print("output_shape:%s"%str(output.shape)) for i in range(output.shape[0]): """ 生成灰色圖片 wtName = os.path.basename(img_paths[i]) overNum = wtName.find(".npy") wtName = wtName[0:overNum] wtName = wtName + "_WT" + ".png" imsave('output/%s/'%args.name + wtName, (output[i,0,:,:]*255).astype('uint8')) tcName = os.path.basename(img_paths[i]) overNum = tcName.find(".npy") tcName = tcName[0:overNum] tcName = tcName + "_TC" + ".png" imsave('output/%s/'%args.name + tcName, (output[i,1,:,:]*255).astype('uint8')) etName = os.path.basename(img_paths[i]) overNum = etName.find(".npy") etName = etName[0:overNum] etName = etName + "_ET" + ".png" imsave('output/%s/'%args.name + etName, (output[i,2,:,:]*255).astype('uint8')) """ npName = os.path.basename(img_paths[i]) overNum = npName.find(".npy") rgbName = npName[0:overNum] rgbName = rgbName + ".png" rgbPic = np.zeros([160, 160, 3], dtype=np.uint8) for idx in range(output.shape[2]): for idy in range(output.shape[3]): if output[i, 0, idx, idy] > 0.5: rgbPic[idx, idy, 0] = 0 rgbPic[idx, idy, 1] = 128 rgbPic[idx, idy, 2] = 0 if output[i, 1, idx, idy] > 0.5: rgbPic[idx, idy, 0] = 255 rgbPic[idx, idy, 1] = 0 rgbPic[idx, idy, 2] = 0 if output[i, 2, idx, idy] > 0.5: rgbPic[idx, idy, 0] = 255 rgbPic[idx, idy, 1] = 255 rgbPic[idx, idy, 2] = 0 imsave('output/%s/' % args.name + rgbName, rgbPic) torch.cuda.empty_cache() """ 将验证集中的GT numpy格式转换成图片格式并保存 """ print("Saving GT,numpy to picture") val_gt_path = 'output/%s/' % args.name + "GT/" if not os.path.exists(val_gt_path): os.mkdir(val_gt_path) for idx in tqdm(range(len(val_mask_paths))): mask_path = val_mask_paths[idx] name = os.path.basename(mask_path) overNum = name.find(".npy") name = name[0:overNum] rgbName = name + ".png" npmask = np.load(mask_path) GtColor = np.zeros([npmask.shape[0], npmask.shape[1], 3], dtype=np.uint8) for idx in range(npmask.shape[0]): for idy in range(npmask.shape[1]): #坏疽(NET,non-enhancing tumor)(标签1) 红色 if npmask[idx, idy] == 1: GtColor[idx, idy, 0] = 255 GtColor[idx, idy, 1] = 0 GtColor[idx, idy, 2] = 0 #浮肿区域(ED,peritumoral edema) (标签2) 绿色 elif npmask[idx, idy] == 2: GtColor[idx, idy, 0] = 0 GtColor[idx, idy, 1] = 128 GtColor[idx, idy, 2] = 0 #增强肿瘤区域(ET,enhancing tumor)(标签4) 黄色 elif npmask[idx, idy] == 4: GtColor[idx, idy, 0] = 255 GtColor[idx, idy, 1] = 255 GtColor[idx, idy, 2] = 0 #imsave(val_gt_path + rgbName, GtColor) imageio.imwrite(val_gt_path + rgbName, GtColor) """ mask_path = val_mask_paths[idx] name = os.path.basename(mask_path) overNum = name.find(".npy") name = name[0:overNum] wtName = name + "_WT" + ".png" tcName = name + "_TC" + ".png" etName = name + "_ET" + ".png" npmask = np.load(mask_path) WT_Label = npmask.copy() WT_Label[npmask == 1] = 1. WT_Label[npmask == 2] = 1. WT_Label[npmask == 4] = 1. TC_Label = npmask.copy() TC_Label[npmask == 1] = 1. TC_Label[npmask == 2] = 0. TC_Label[npmask == 4] = 1. ET_Label = npmask.copy() ET_Label[npmask == 1] = 0. ET_Label[npmask == 2] = 0. ET_Label[npmask == 4] = 1. imsave(val_gt_path + wtName, (WT_Label * 255).astype('uint8')) imsave(val_gt_path + tcName, (TC_Label * 255).astype('uint8')) imsave(val_gt_path + etName, (ET_Label * 255).astype('uint8')) """ print("Done!") if val_args.mode == "Calculate": """ 计算各种指标:Dice、Sensitivity、PPV """ wt_dices = [] tc_dices = [] et_dices = [] wt_sensitivities = [] tc_sensitivities = [] et_sensitivities = [] wt_ppvs = [] tc_ppvs = [] et_ppvs = [] wt_Hausdorf = [] tc_Hausdorf = [] et_Hausdorf = [] wtMaskList = [] tcMaskList = [] etMaskList = [] wtPbList = [] tcPbList = [] etPbList = [] maskPath = glob("output/%s/" % args.name + "GT\*.png") pbPath = glob("output/%s/" % args.name + "*.png") if len(maskPath) == 0: print("请先生成图片!") return for myi in tqdm(range(len(maskPath))): mask = imread(maskPath[myi]) pb = imread(pbPath[myi]) wtmaskregion = np.zeros([mask.shape[0], mask.shape[1]], dtype=np.float32) wtpbregion = np.zeros([mask.shape[0], mask.shape[1]], dtype=np.float32) tcmaskregion = np.zeros([mask.shape[0], mask.shape[1]], dtype=np.float32) tcpbregion = np.zeros([mask.shape[0], mask.shape[1]], dtype=np.float32) etmaskregion = np.zeros([mask.shape[0], mask.shape[1]], dtype=np.float32) etpbregion = np.zeros([mask.shape[0], mask.shape[1]], dtype=np.float32) for idx in range(mask.shape[0]): for idy in range(mask.shape[1]): # 只要这个像素的任何一个通道有值,就代表这个像素不属于前景,即属于WT区域 if mask[idx, idy, :].any() != 0: wtmaskregion[idx, idy] = 1 if pb[idx, idy, :].any() != 0: wtpbregion[idx, idy] = 1 # 只要第一个通道是255,即可判断是TC区域,因为红色和黄色的第一个通道都是255,区别于绿色 if mask[idx, idy, 0] == 255: tcmaskregion[idx, idy] = 1 if pb[idx, idy, 0] == 255: tcpbregion[idx, idy] = 1 # 只要第二个通道是128,即可判断是ET区域 if mask[idx, idy, 1] == 128: etmaskregion[idx, idy] = 1 if pb[idx, idy, 1] == 128: etpbregion[idx, idy] = 1 #开始计算WT dice = dice_coef(wtpbregion, wtmaskregion) wt_dices.append(dice) ppv_n = ppv(wtpbregion, wtmaskregion) wt_ppvs.append(ppv_n) Hausdorff = hausdorff_distance(wtmaskregion, wtpbregion) wt_Hausdorf.append(Hausdorff) sensitivity_n = sensitivity(wtpbregion, wtmaskregion) wt_sensitivities.append(sensitivity_n) # 开始计算TC dice = dice_coef(tcpbregion, tcmaskregion) tc_dices.append(dice) ppv_n = ppv(tcpbregion, tcmaskregion) tc_ppvs.append(ppv_n) Hausdorff = hausdorff_distance(tcmaskregion, tcpbregion) tc_Hausdorf.append(Hausdorff) sensitivity_n = sensitivity(tcpbregion, tcmaskregion) tc_sensitivities.append(sensitivity_n) # 开始计算ET dice = dice_coef(etpbregion, etmaskregion) et_dices.append(dice) ppv_n = ppv(etpbregion, etmaskregion) et_ppvs.append(ppv_n) Hausdorff = hausdorff_distance(etmaskregion, etpbregion) et_Hausdorf.append(Hausdorff) sensitivity_n = sensitivity(etpbregion, etmaskregion) et_sensitivities.append(sensitivity_n) print('WT Dice: %.4f' % np.mean(wt_dices)) print('TC Dice: %.4f' % np.mean(tc_dices)) print('ET Dice: %.4f' % np.mean(et_dices)) print("=============") print('WT PPV: %.4f' % np.mean(wt_ppvs)) print('TC PPV: %.4f' % np.mean(tc_ppvs)) print('ET PPV: %.4f' % np.mean(et_ppvs)) print("=============") print('WT sensitivity: %.4f' % np.mean(wt_sensitivities)) print('TC sensitivity: %.4f' % np.mean(tc_sensitivities)) print('ET sensitivity: %.4f' % np.mean(et_sensitivities)) print("=============") print('WT Hausdorff: %.4f' % np.mean(wt_Hausdorf)) print('TC Hausdorff: %.4f' % np.mean(tc_Hausdorf)) print('ET Hausdorff: %.4f' % np.mean(et_Hausdorf)) print("=============")
import numpy as np from hausdorff import hausdorff_distance # two random 2D arrays (second dimension must match) np.random.seed(0) X = np.random.random((1000, 100)) Y = np.random.random((5000, 100)) # Test computation of Hausdorff distance with different base distances print("Hausdorff distance test: {0}".format( hausdorff_distance(X, Y, distance="manhattan"))) print("Hausdorff distance test: {0}".format( hausdorff_distance(X, Y, distance="euclidean"))) print("Hausdorff distance test: {0}".format( hausdorff_distance(X, Y, distance="chebyshev"))) print("Hausdorff distance test: {0}".format( hausdorff_distance(X, Y, distance="cosine"))) # For haversine, use 2D lat, lng coordinates def rand_lat_lng(N): lats = np.random.uniform(-90, 90, N) lngs = np.random.uniform(-180, 180, N) return np.stack([lats, lngs], axis=-1) X = rand_lat_lng(100) Y = rand_lat_lng(250) print("Hausdorff haversine test: {0}".format( hausdorff_distance(X, Y, distance="haversine")))
def get_hausdorff(team1, team2): team1 = np.array(team1).reshape(10, -1) team2 = np.array(team2).reshape(10, -1) hd = hausdorff_distance(team1, team2, distance="euclidean") return hd