Ejemplo n.º 1
0
    def search_local_extremum_points(self, history, x_letftop, y_lefttop,
                                     x_rightbottom, y_rightbottom):
        '''
        实现Google Brain的算法,用来提取关键点
        :param history: 预测结果
        :param x_letftop:
        :param y_lefttop:
        :param x_rightbottom:
        :param y_rightbottom:
        :return: 坐标集合,与应用的概率
        '''
        c_thresh = 50
        cmb = CancerMapBuilder(self._params, extract_scale=40, patch_size=256)
        cancer_map = cmb.generating_probability_map(history, x_letftop,
                                                    y_lefttop, x_rightbottom,
                                                    y_rightbottom,
                                                    self._params.GLOBAL_SCALE)
        Y, X = cancer_map.shape
        level = 5
        prob_thred = 0.5
        radius = 24

        result = []
        while np.max(cancer_map) > prob_thred:
            prob_max = cancer_map.max()
            max_idx = np.where(cancer_map == prob_max)
            y_mask, x_mask = max_idx[0][0], max_idx[1][0]
            result.append((prob_max, x_mask, y_mask))

            x_min = x_mask - radius if x_mask - radius > 0 else 0
            x_max = x_mask + radius if x_mask + radius <= X else X
            y_min = y_mask - radius if y_mask - radius > 0 else 0
            y_max = y_mask + radius if y_mask + radius <= Y else Y

            for x in range(x_min, x_max):
                for y in range(y_min, y_max):
                    cancer_map[y, x] = 0

        candidated = []
        count = 0
        for prob, x, y in result:
            if count < c_thresh:
                x = x + x_letftop
                y = y + y_lefttop
                candidated.append({"x": 32 * x, "y": 32 * y, "prob": prob})
                count += 1
        return candidated
    def calculate_slide_features(self, history, x1, y1, x2, y2, DIM=5):
        '''
        提取特征向量
        :param history: 预测结果(坐标,概率)
        :param x1: 左上角x
        :param y1: 左上角y
        :param x2: 右上角x
        :param y2: 右上角主
        :param DIM: 特征的维数
        :return:
        '''
        mode = 2
        if mode == 1:  # by cancer map
            cmb = CancerMapBuilder(self._params,
                                   extract_scale=40,
                                   patch_size=256)
            cancer_map = cmb.generating_probability_map(
                history, x1, y1, x2, y2, 1.25)

            selem_size = 8
            cancer_map = morphology.erosion(cancer_map, square(selem_size))

            data = cancer_map.ravel()
        elif mode == 2:  # by history
            value = np.array(list(history.values()))
            data = 1 / (1 + np.exp(-value))

        data = data[data >= 0.5]

        # DIM = 18
        L = len(data)
        if L > 0:
            max_prob = np.max(data)
            hist = np.histogram(data,
                                bins=DIM - 2,
                                range=(0.5, 1),
                                density=False)
            feature = np.array(hist[0], dtype=np.float) / L
            feature = np.append(feature, [L, max_prob])
            return feature
        else:
            return np.zeros((DIM, ))
    def test_adaptive_detect_region_test_slice_normal_part(self):
        # test set
        # test_list = [3, 5, 6, 7, 9, 12, 14, 15, 17, 18, 19, 20, 22, 23, 24, 25, 28, 31, 32, 34, 35, 36, 37, 39, 41, 42, 43, 44, 45,
        #  47, 49, 50, 53, 54, 55, 56, 57, 58, 59, 60, 62, 63, 67, 70, 72, 76, 77, 78, 80, 81, 83, 85, 86, 87, 88, 89, 91,
        #  92, 93, 95, 96, 98, 100, 101, 103, 106, 107, 109, 111, 112, 114, 115, 118, 119, 120, 123, 124, 125, 126, 127,
        #  128, 129, 130]
        test_list = [111, 112, 114, 115, 118, 119, 120, 123, 124, 125, 126, 127,
                 128, 129, 130]
        # test_list = [3,5,6,7,9,12,14]
        result = {}
        # 如果输出癌变概率图,并进行评估
        enable_evaluate = False

        for id in test_list:

            x1, y1, x2, y2 = 0, 0, 0, 0

            slice_id = "Test_{:0>3d}".format(id)

            c = Params()
            c.load_config_file(JSON_PATH)
            imgCone = ImageCone(c, Open_Slide())

            # 读取数字全扫描切片图像
            tag = imgCone.open_slide("Testing/images/%s.tif" % slice_id,
                                     None, slice_id)

            detector = AdaptiveDetector(c, imgCone)

            if x2 * y2 == 0:
                x1, y1, x2, y2 = detector.get_detection_rectangle()
                print("x1, y1, x2, y2: ", x1, y1, x2, y2)

            history = detector.adaptive_detect(x1, y1, x2, y2, 1.25, extract_scale=40, patch_size=256,
                                               max_iter_nums=10, batch_size=100,
                                               limit_sampling_density=1, enhanced=False,
                                               superpixel_area=1000, superpixels_boundaries_spacing=120)

            detector.save_result_history(x1, y1, x2, y2, 1.25, history)

            if enable_evaluate:
                cmb = CancerMapBuilder(c, extract_scale=40, patch_size=256)
                cancer_map = cmb.generating_probability_map(history, x1, y1, x2, y2, 1.25)

                src_img = detector.get_img_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)
                if imgCone.ano_filename is None:
                    mask_img = np.zeros(src_img.shape, dtype=np.bool)

                levels = [0.3, 0.5, 0.6, 0.8]
                false_positive_rate, true_positive_rate, roc_auc, dice = Evaluation.evaluate_slice_map(cancer_map,
                                                                                                       mask_img,
                                                                                                       levels)
                result[slice_id] = (roc_auc, dice)

                # 存盘输出部分
                # self.show_results(cancer_map, dice, false_positive_rate, history, levels, mask_img, roc_auc, slice_id,
                #                   src_img, true_positive_rate)
                save_path = "{}/results/cancer_pic".format(c.PROJECT_ROOT)
                Evaluation.save_result_picture(slice_id, src_img, mask_img, cancer_map, history, roc_auc, levels,
                                               save_path)
                # detector.save_result_xml(x1, y1, 1.25, cancer_map, levels)

        for slice, (auc, dices) in result.items():
            print("#################{}###################".format(slice))
            for t, value in dices:
                print("threshold = {:.3f}, dice coef = {:.6f}".format(t, value))
            print("ROC auc: {:.6f}".format(auc))
            print("#################{}###################".format(slice))
    def test_adaptive_detect_region_test_slice(self):
        # test set
        # test_list = [1,2,4,8,10,11,13,16,21,26,27,29,30,33,38,40,46,48,51,52,61,64,65,66,68,69,71,73,74,75,79,
        # 82,84,90,94,97,99,102,104,105,108,110,113,116,117,121,122]
        # test_list = [4,10,29,30,33,38,48,66,79,84,99,102,116,117,122]
        test_list = [117]
        # test_list = [16,21,26,27,29,30,33,38,40,46,48,51,52,61,64,65,66,68,69,71,73,74,75,79,
        #              82,84,90,94,97,99,102,104,105,108,110,113,116,117,121,122]
        result = {}
        # 如果输出癌变概率图,并进行评估
        enable_evaluate = True

        for id in test_list:
            x1, y1, x2, y2 = 0, 0, 0, 0

            slice_id = "Test_{:0>3d}".format(id)

            c = Params()
            c.load_config_file(JSON_PATH)
            imgCone = ImageCone(c, Open_Slide())

            # 读取数字全扫描切片图像
            tag = imgCone.open_slide("Testing/images/%s.tif" % slice_id,
                                     'Testing/images/%s.xml' % slice_id, slice_id)

            detector = AdaptiveDetector(c, imgCone)

            if x2 * y2 == 0:
                x1, y1, x2, y2 = detector.get_detection_rectangle()
                print("x1, y1, x2, y2: ", x1, y1, x2, y2)

            history = detector.process(x1, y1, x2, y2, 1.25, extract_scale=40, patch_size=256,
                                       max_iter_nums=20, batch_size=100,
                                       limit_sampling_density=2, enhanced=True)

            detector.save_result_history(x1, y1, x2, y2, 1.25, history)

            if enable_evaluate:
                cmb = CancerMapBuilder(c, extract_scale=40, patch_size=256)
                cancer_map = cmb.generating_probability_map(history, x1, y1, x2, y2, 1.25)

                src_img = detector.get_img_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)
                mask_img = detector.get_true_mask_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)

                levels = [0.3, 0.5, 0.6, 0.8]
                false_positive_rate, true_positive_rate, roc_auc, dice = Evaluation.evaluate_slice_map(cancer_map,
                                                                                                       mask_img,
                                                                                                       levels)
                result[slice_id] = (roc_auc, dice)

                # 存盘输出部分
                # self.show_results(cancer_map, dice, false_positive_rate, history, levels, mask_img, roc_auc, slice_id,
                #                   src_img, true_positive_rate)
                save_path = "{}/results/cancer_pic".format(c.PROJECT_ROOT)
                Evaluation.save_result_picture(slice_id, src_img, mask_img, cancer_map, history, roc_auc, levels,
                                               save_path)
                # detector.save_result_xml(x1, y1, 1.25, cancer_map, levels)

        for slice, (auc, dices) in result.items():
            print("#################{}###################".format(slice))
            for t, value in dices:
                print("threshold = {:.3f}, dice coef = {:.6f}".format(t, value))
            print("ROC auc: {:.6f}".format(auc))
            print("#################{}###################".format(slice))
    def test_adaptive_detect_region_train_slice(self):
        # train set
        # train_list = [9, 11, 16, 26, 39, 47, 58, 68, 72, 76]
        # train_list = [11, 16, 26, 39, 47, 58, 68, 72, 76]
        train_list = range(1,112) # [29,33,61,89,95]range(27,39)
        result = {}
        # 如果输出癌变概率图,并进行评估
        enable_evaluate = False

        for id in train_list:
            x1, y1, x2, y2 = 0, 0, 0, 0

            slice_id = "Tumor_{:0>3d}".format(id)

            c = Params()
            c.load_config_file(JSON_PATH)
            imgCone = ImageCone(c, Open_Slide())

            # 读取数字全扫描切片图像
            tag = imgCone.open_slide("Train_Tumor/%s.tif" % slice_id,
                                     'Train_Tumor/%s.xml' % slice_id, slice_id)

            detector = AdaptiveDetector(c, imgCone)

            if x2 * y2 == 0:
                x1, y1, x2, y2 = detector.get_detection_rectangle()
                print("x1, y1, x2, y2: ", x1, y1, x2, y2)

            history = detector.adaptive_detect(x1, y1, x2, y2, 1.25, extract_scale=40, patch_size=256,
                                               max_iter_nums=20, batch_size=100,
                                               limit_sampling_density=1, enhanced=True,
                                               superpixel_area=1000, superpixels_boundaries_spacing=120)

            detector.save_result_history(x1, y1, x2, y2, 1.25, history)

            if enable_evaluate:
                cmb = CancerMapBuilder(c, extract_scale=40, patch_size=256)
                cancer_map = cmb.generating_probability_map(history, x1, y1, x2, y2, 1.25)

                src_img = detector.get_img_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)
                mask_img = detector.get_true_mask_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)

                levels = [0.3, 0.5, 0.6, 0.8]
                false_positive_rate, true_positive_rate, roc_auc, dice = Evaluation.evaluate_slice_map(cancer_map,
                                                                                                       mask_img,
                                                                                                       levels)
                result[slice_id] = (roc_auc, dice)

                # 存盘输出部分
                # self.show_results(cancer_map, dice, false_positive_rate, history, levels, mask_img, roc_auc, slice_id,
                #                   src_img, true_positive_rate)
                # save_path = "{}/results/cancer_pic".format(c.PROJECT_ROOT)
                # Evaluation.save_result_picture(slice_id, src_img, mask_img, cancer_map, history, roc_auc, levels,
                #                                save_path)
                # detector.save_result_xml(x1, y1, 1.25, cancer_map, levels)

        for slice, (auc, dices) in result.items():
            print("#################{}###################".format(slice))
            for t, value in dices:
                print("threshold = {:.3f}, dice coef = {:.6f}".format(t, value))
            print("ROC auc: {:.6f}".format(auc))
            print("#################{}###################".format(slice))
Ejemplo n.º 6
0
    def search_local_extremum_points_max3(self, history, x_letftop, y_lefttop,
                                          x_rightbottom, y_rightbottom):
        '''
        提取关键点
        :param history: 预测的结果
        :param x_letftop:
        :param y_lefttop:
        :param x_rightbottom:
        :param y_rightbottom:
        :return: 坐标集合,与应用的概率
        '''

        # for train set: t,c,h = -0.5, 50, 0.02
        t_thresh = 0  # -0.5
        c_thresh = 300  # 50
        h_param = 0.01  # 0.02
        low_prob_thresh, high_prob_thresh = CancerMapBuilder.calc_probability_threshold(
            history, t=t_thresh)
        print("low_prob_thresh = {:.4f}, high_prob_thresh = {:.4f}".format(
            low_prob_thresh, high_prob_thresh))

        cmb = CancerMapBuilder(self._params, extract_scale=40, patch_size=256)
        cancer_map = cmb.generating_probability_map(history, x_letftop,
                                                    y_lefttop, x_rightbottom,
                                                    y_rightbottom,
                                                    self._params.GLOBAL_SCALE)

        # cancer_map = filters.gaussian(cancer_map, sigma=0.2)

        h = h_param
        h_maxima = extrema.h_maxima(cancer_map, h, selem=square(7))
        xy = np.nonzero(h_maxima)

        sorted_points = []
        for y, x in zip(xy[0], xy[1]):
            prob = cancer_map[y, x]
            if prob > low_prob_thresh:
                sorted_points.append((prob, x, y))
        sorted_points.sort(key=lambda x: (x[0]), reverse=True)

        resolution = 0.243
        level = 5
        Threshold = 3 * 75 / (resolution * pow(2, level) * 2)  # 3
        result = []
        while len(sorted_points) > 0:
            m = sorted_points.pop(0)
            mx = m[1]
            my = m[2]
            mprob = m[0]
            tx = [mx]
            ty = [my]
            temp = []
            for prob, x, y in sorted_points:
                dist = distance.euclidean([mx, my], [x, y])
                if dist > Threshold:
                    temp.append((prob, x, y))
                else:
                    tx.append(x)
                    ty.append(y)

            mx = np.rint(np.mean(np.array(tx))).astype(np.int)
            my = np.rint(np.mean(np.array(ty))).astype(np.int)

            result.append((mprob, mx, my))
            sorted_points = temp

        result.sort(key=lambda x: (x[0]), reverse=True)

        candidated = []
        count = 0
        for prob, x, y in result:
            if prob > high_prob_thresh or (prob > low_prob_thresh
                                           and count < c_thresh):
                x = x + x_letftop
                y = y + y_lefttop
                candidated.append({"x": 32 * x, "y": 32 * y, "prob": prob})
                count += 1

        return candidated
Ejemplo n.º 7
0
    def save_result_pictures(self, slice_dirname, tag, chosen):
        '''

        :param slice_dirname: slide id
        :param tag: 内部标志,对应于Slide filter的input size
        :param chosen: 被选择处理的slide id列表
        :return:
        '''
        project_root = self._params.PROJECT_ROOT
        save_path = "{}/results".format(project_root)
        pic_path = "{}/results/cancer_pic".format(project_root)
        levels = [0.3, 0.5, 0.6, 0.8]

        imgCone = ImageCone(self._params, Open_Slide())
        if tag == 0:
            code = "_history.npz"
        else:
            code = "_history_v{}.npz".format(tag)

        K = len(code)

        for result_file in os.listdir(save_path):
            ext_name = os.path.splitext(result_file)[1]
            slice_id = result_file[:-K]
            if chosen is not None and slice_id not in chosen:
                continue

            if ext_name == ".npz" and code in result_file:
                print("loading data : {}, {}".format(slice_id, result_file))
                result = np.load("{}/{}".format(save_path, result_file),
                                 allow_pickle=True)
                x1 = result["x1"]
                y1 = result["y1"]
                x2 = result["x2"]
                y2 = result["y2"]
                coordinate_scale = result["scale"]
                assert coordinate_scale == 1.25, "Scale is Error!"

                history = result["history"].item()

                cmb = CancerMapBuilder(self._params,
                                       extract_scale=40,
                                       patch_size=256)
                cancer_map = cmb.generating_probability_map(
                    history, x1, y1, x2, y2, 1.25)

                imgCone.open_slide("{}/{}.tif".format(slice_dirname, slice_id),
                                   '{}/{}.xml'.format(slice_dirname,
                                                      slice_id), slice_id)

                mask_img = imgCone.create_mask_image(self._params.GLOBAL_SCALE,
                                                     0)
                mask_img = mask_img['C']

                h, w = cancer_map.shape
                mask_img = mask_img[y1:y1 + h, x1:x1 + w]

                fullImage = np.array(
                    imgCone.get_fullimage_byScale(self._params.GLOBAL_SCALE))
                src_img = fullImage[y1:y1 + h, x1:x1 + w, :]

                false_positive_rate, true_positive_rate, thresholds = metrics.roc_curve(
                    mask_img.ravel(), cancer_map.ravel())
                roc_auc = metrics.auc(false_positive_rate, true_positive_rate)
                Evaluation.save_result_picture(slice_id, src_img, mask_img,
                                               cancer_map, history, roc_auc,
                                               levels, pic_path, tag)
Ejemplo n.º 8
0
    def calculate_ROC(self, slice_dirname, tag, chosen, p_thresh=0.5):
        '''
        计算每张切片的pixel级的ROC
        :param slice_dirname: slice的路径,现在不需要了,直接读取Mask的存盘文件
        :param tag: input size of Slide Filter
        :param chosen: list of slide ID
        :param p_thresh: tumor probability threshold
        :return:
        '''

        project_root = self._params.PROJECT_ROOT
        save_path = "{}/results".format(project_root)
        mask_path = "{}/data/true_masks".format(self._params.PROJECT_ROOT)

        result_auc = []
        if tag == 0:
            code = "_history.npz"
        else:
            code = "_history_v{}.npz".format(tag)

        K = len(code)
        print(
            "slice_id, area, count, p_thresh, dice, accu, recall, f1, roc_auc")
        for result_file in os.listdir(save_path):
            ext_name = os.path.splitext(result_file)[1]
            slice_id = result_file[:-K]
            if chosen is not None and slice_id not in chosen:
                continue

            if ext_name == ".npz" and code in result_file:
                print("loading data : {}, {}".format(slice_id, result_file))
                result = np.load("{}/{}".format(save_path, result_file),
                                 allow_pickle=True)
                x1 = result["x1"]
                y1 = result["y1"]
                x2 = result["x2"]
                y2 = result["y2"]
                coordinate_scale = result["scale"]
                assert coordinate_scale == 1.25, "Scale is Error!"

                history = result["history"].item()

                cmb = CancerMapBuilder(self._params,
                                       extract_scale=40,
                                       patch_size=256)
                cancer_map = cmb.generating_probability_map(
                    history, x1, y1, x2, y2, 1.25)
                h, w = cancer_map.shape

                mask_filename = "{}/{}_true_mask.npz".format(
                    mask_path, slice_id)
                if os.path.exists(mask_filename):
                    result = np.load(mask_filename, allow_pickle=True)
                    mask_img = result["mask"]
                    mask_img = mask_img[y1:y1 + h, x1:x1 + w]
                    area = np.sum(mask_img)
                    _, count = morphology.label(mask_img,
                                                neighbors=8,
                                                connectivity=2,
                                                return_num=True)
                else:
                    mask_img = np.zeros((h, w), dtype=np.bool)
                    area = 0
                    count = 0

                false_positive_rate, true_positive_rate, thresholds = metrics.roc_curve(
                    mask_img.ravel(), cancer_map.ravel())
                roc_auc = metrics.auc(false_positive_rate, true_positive_rate)

                pred = np.array(cancer_map > p_thresh).astype(np.int)
                mask_img = np.array(mask_img).astype(np.int)
                dice = Evaluation.calculate_dice_coef(mask_img, pred)
                accu = metrics.accuracy_score(mask_img, pred)
                recall = metrics.recall_score(mask_img, pred, average='micro')
                # print(set(np.unique(mask_img)) - set(np.unique(pred)))
                f1 = metrics.f1_score(
                    mask_img, pred,
                    average='weighted')  # Here, f1 = dice,average='micro'

                temp = "{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}".format(
                    slice_id, area, count, p_thresh, dice, accu, recall, f1,
                    roc_auc)
                result_auc.append(temp)
                print(temp)

        print("############################################")
        for item in result_auc:
            print(item)

        return
Ejemplo n.º 9
0
    def test_detect(self):

        slide_list = [1]
        result = {}
        Train_Tag = True

        # 如果输出癌变概率图,并进行评估
        enable_evaluate = False

        for id in slide_list:
            x1, y1, x2, y2 = 0, 0, 0, 0

            if Train_Tag:
                slice_id = "Tumor_{:0>3d}".format(id)
            else:
                slice_id = "Test_{:0>3d}".format(id)

            imgCone = ImageCone(self._params, Open_Slide())

            # 读取数字全扫描切片图像
            if Train_Tag:
                tag = imgCone.open_slide("Train_Tumor/%s.tif" % slice_id,
                                         'Train_Tumor/%s.xml' % slice_id,
                                         slice_id)
            else:
                tag = imgCone.open_slide("Testing/images/%s.tif" % slice_id,
                                         'Testing/images/%s.xml' % slice_id,
                                         slice_id)

            detector = AdaptiveDetector(self._params, imgCone)

            if x2 * y2 == 0:
                x1, y1, x2, y2 = detector.get_detection_rectangle()
                print("x1, y1, x2, y2: ", x1, y1, x2, y2)

            history = detector.adaptive_detect(
                x1,
                y1,
                x2,
                y2,
                1.25,
                extract_scale=40,
                patch_size=256,
                max_iter_nums=20,
                batch_size=10,
                limit_sampling_density=1,
                enhanced=True,
                superpixel_area=1000,
                superpixels_boundaries_spacing=120)

            detector.save_result_history(x1, y1, x2, y2, 1.25, history)

            if enable_evaluate:
                cmb = CancerMapBuilder(self._params,
                                       extract_scale=40,
                                       patch_size=256)
                cancer_map = cmb.generating_probability_map(
                    history, x1, y1, x2, y2, 1.25)

                src_img = detector.get_img_in_detect_area(
                    x1, y1, x2, y2, 1.25, 1.25)
                mask_img = detector.get_true_mask_in_detect_area(
                    x1, y1, x2, y2, 1.25, 1.25)

                levels = [0.3, 0.5, 0.8]
                false_positive_rate, true_positive_rate, roc_auc, dice = Evaluation.evaluate_slice_map(
                    cancer_map, mask_img, levels)
                result[slice_id] = (roc_auc, dice)

                # save results
                # self.show_results(cancer_map, dice, false_positive_rate, history, levels, mask_img, roc_auc, slice_id,
                #                   src_img, true_positive_rate)
                save_path = "{}/results/cancer_pic".format(
                    self._params.PROJECT_ROOT)
                Evaluation.save_result_picture(slice_id, src_img, mask_img,
                                               cancer_map, history, roc_auc,
                                               levels, save_path)
                # detector.save_result_xml(x1, y1, 1.25, cancer_map, levels)

        for slice, (auc, dices) in result.items():
            print("#################{}###################".format(slice))
            for t, value in dices:
                print("threshold = {:.3f}, dice coef = {:.6f}".format(
                    t, value))
            print("ROC auc: {:.6f}".format(auc))
            print("#################{}###################".format(slice))