【问题标题】:Fuzzy C means in python for brain tumor detection用于脑肿瘤检测的python中的模糊C意味着
【发布时间】:2020-10-11 11:21:56
【问题描述】:

我在 GitHub 上找到了这段代码,我试图理解函数的行为。我试图将这段代码与公式进行比较(来自this paper 的第 6 页):

我找不到这些公式在代码中实现的位置。谁能帮我解释一下公式和代码之间的相似之处?

class FCM() :
    def __init__(self, n_clusters=17, max_iter=100, m=2, error=1e-6):
        super().__init__()
        self.u, self.centers = None, None
        self.n_clusters = n_clusters
        self.max_iter = max_iter
        self.m = m
        self.error = error

def fit(self, X):
    N = X.shape[0]
    C = self.n_clusters
    centers = []

    u = np.random.dirichlet(np.ones(C), size=N)

    iteration = 0
    while iteration < self.max_iter:
        u2 = u.copy()

        centers = self.next_centers(X, u)
        u = self.next_u(X, centers)
        iteration += 1

        # Stopping rule
        if norm(u - u2) < self.error:
            break

    self.u = u
    self.centers = centers
    return centers

def next_centers(self, X, u):
    um = u ** self.m
    return (X.T @ um / np.sum(um, axis=0)).transpose()  #Vi

def next_u(self, X, centers):
    return self._predict(X, centers)

def _predict(self, X, centers):
    power = float(2 / (self.m - 1))
    temp = cdist(X, centers) ** power
    denominator_ = temp.reshape((X.shape[0], 1, -1)).repeat(temp.shape[-1], axis=1)
    denominator_ = temp[:, :, np.newaxis] / denominator_

    return 1 / denominator_.sum(2)

def predict(self, X):
    if len(X.shape) == 1:
        X = np.expand_dims(X, axis=0)

    u = self._predict(X, self.centers)
    return np.argmax(u, axis=-1)

img2 = ret.reshape(x * y, z)
    algorithm = FCM()
    cluster_centers = algorithm.fit(img2)
    output = algorithm.predict(img2)
    img = cluster_centers[output].astype(np.int16).reshape(x, y, 3)

【问题讨论】:

    标签: python numpy cluster-analysis image-segmentation fuzzy-c-means


    【解决方案1】:

    我认为最相关的部分如下:

    • 函数fit描述了算法的一般步骤:
      • 一些初始化
      • 中心和标签的更新(委托给其他功能)
      • 在每次迭代后验证停止标准 (norm(u - u2) &lt; self.error)。
    • 方程 (4) 中的模糊隶属度在函数 _predict 内实现:
      • 指数存储在power
      • 差异规范由cdist from scipy.spatial.distance 处理。对该函数的一次调用用于计算数据点 xj 和聚类中心 vi 的所有组合之间的距离。每个结果都被提升到power,结果存储在temp 数组中。
      • _predict 的最后 3 行围绕 temp 数组的条目进行处理,在没有循环的情况下,它计算每个“距离”和“距离”的适当总和之间的除法(实际上, “power of distance(s)”,但首先忽略指数更容易)。为此,代码使用了 numpy 中可用的一些技巧,例如 reshaperepeatindexing
    • 方程 (5) 中的模糊中心由 next_centers 计算:
      • 预计算一个矩阵um,其中包含每个uijm次幂(供以后用于分子和分母)
      • 使用np.sum(um, axis=0) 创建一个数组,其i-th 条目是计算i-th 集群时在分母中使用的总和
      • 对于i-th 集群,(5) 的分子将在X.T @ umi-th 列中计算(一旦我们.transpose()结果矩阵)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-12
      • 2012-03-19
      • 1970-01-01
      • 2015-09-25
      • 2019-09-13
      • 2018-09-24
      • 2020-04-27
      • 2015-07-30
      相关资源
      最近更新 更多