def test_model(folderpath, model, output):
    scores = {"f1_breast": [], "f1_brain": [],
              "aji_breast": [], "aji_brain": []}
    files = glob(os.path.join(folderpath, "Slide_*", "*.png"))
    check_or_create(output)
    num = 0
    for f in tqdm(files):
        rgb, label = resize(load_data(f))
        dic_res = model.predict(rgb, label=label)
        label = meas.label(label)

        f1 = dic_res['f1_score']
        label_int = PostProcessOut(dic_res['probability'][:,:,0])
        aji = AJI_fast(label, label_int)

        n_pat = f.split('/')[-1].split('_')[0]
        if n_pat in ["02", "06"]:
            f1_name = "f1_breast"
            aji_name = "aji_breast"
        else:
            f1_name = "f1_brain"
            aji_name = "aji_brain"
        scores[f1_name].append(f1)
        scores[aji_name].append(aji)
        colors = random_colors(255)
        output_gt = apply_mask_with_highlighted_borders(rgb[92:-92, 92:-92], label, colors, alpha=0.5)
        output_pred = apply_mask_with_highlighted_borders(rgb[92:-92, 92:-92], label_int, colors, alpha=0.5)
        num += 1
        imsave(os.path.join(output, "test_{}_gt.png".format(num)), output_gt)
        imsave(os.path.join(output, "test_{}_pred.png".format(num)), output_pred)
    print("mean f1 brain: {}".format(np.mean(scores["f1_brain"])))
    print("mean aji brain: {}".format(np.mean(scores["aji_brain"])))
    print("mean f1 breast: {}".format(np.mean(scores["f1_breast"])))
    print("mean aji breast: {}".format(np.mean(scores["aji_breast"])))
Beispiel #2
0
 def apply_box(self, image, ann):
     if image.dim() == 4:
         for i in range(image.shape[0]):
             img = image[i]
             ann_sigle = ann[i]
             img = self.denormalize(img)
             N = len(ann_sigle)
             colors = torch.tensor(random_colors(N))
             colors = colors.unsqueeze(-1).unsqueeze(-1)
             print(ann_sigle)
             boxs = convert_annotations_to_boxs(ann_sigle, self.label2num)
             img_with_boxs = draw_boxs(img, boxs, colors)
             image[i] = img_with_boxs
         return image
     else:
         image = self.denormalize(image)
         N = len(ann)
         colors = torch.tensor(random_colors(N))
         colors = colors.unsqueeze(-1).unsqueeze(-1)
         boxs = convert_annotations_to_boxs(ann, self.label2num)
         image_with_boxs = draw_boxs(image, boxs, colors)
         return image_with_boxs
Beispiel #3
0
def get_apply_final_model():
    """
    Final endpit
    """
    edge_image = request.args.get('data')
    # 1.- convert base64 to png image and save
    print("IMAGE-->", edge_image, file=sys.stderr)
    edge_name = convert_and_save4(edge_image)
    print("EDGE-name-->", edge_name, file=sys.stderr)
    # 2.- Load image as numpy
    np_img = imread(edge_name).reshape(H, W, 1)
    print("EDGE_MAX-->", np.max(np_img), np_img.shape, file=sys.stderr)
    img = np_img / 255
    # 3.- set random colors for colorize image
    color_mask = random_colors() / 255
    # 4.-pred model
    lista = []
    lista.append(img)
    lista.append(img)

    lista_mask = []
    lista_mask.append(color_mask)
    lista_mask.append(color_mask)

    with graph.as_default():
        ret_img = draw_unet_model.predict([
            np.array(lista).reshape(2, H, W, 1),
            np.array(lista_mask).reshape(2, H, W, 3)
        ])

    endimage = np.uint8((ret_img[1][0]) * 255)
    #    debug_np_to_file(endimage, '/tmp/end.jpg')
    ret = save_np_array_to_image(endimage)
    #

    return ret
Beispiel #4
0
def render_image(args: Namespace, msg_send: bool = False) -> Tuple[Union[None, str], dict, list, utils.Vector]:
    size = utils.Vector(args.width, args.height)

    starting_point = args.starting_point
    if starting_point is not None:
        # For the user to use the coordinate indexing starting at one
        starting_point = utils.Vector(*[x - 1 for x in args.starting_point])

    color_background = tuple(args.color_background)

    nebula = Nebula(
        size,
        args.max_count,
        args.reproduce_chance,
        quadratic=args.quadratic,
        starting_point=starting_point
    )
    nebula.develop(min_percent=args.min_percent, max_percent=args.max_percent)

    colors = config_colors()

    if args.random_colors:
        colors = utils.random_colors(args.colors_number)
        gradient = utils.gradient(nebula.current_generation, colors)
    elif len(colors) > 1:
        gradient = utils.gradient(nebula.current_generation, colors)

    if args.opaque:
        for color in colors:
            color[3] = 255

    print(c.NOTIFICATION_MSG_BEFORE_RENDERING)
    sleep(1)

    image = Image.new('RGBA', (size.x, size.y))
    draw = ImageDraw.Draw(image)

    for x in range(size.x + 1):
        print(f'[{datetime.now().time()}]', 'Image drawing:', '{:.5f}'.format(x / size.x * 100) + ' %', sep='\t')
        for y in range(size.y + 1):
            if nebula.squares[x][y]:
                if len(colors) == 1:
                    max_gen = nebula.current_generation
                    gen = nebula.squares[x][y].gen

                    alpha = round((1 - gen / max_gen) * 255)
                    if args.fade_in:
                        alpha = round(gen / max_gen * 255)

                    colors[0][3] = alpha

                    draw.point([x, y], fill=tuple(colors[0]))
                else:
                    gen = nebula.squares[x][y].gen - 1
                    draw.point([x, y], fill=gradient[gen])
            else:
                draw.point([x, y], fill=color_background)

    image_name = f'{size.x}x{size.y}_{args.reproduce_chance}_{utils.generate_filename()}.png'
    image_path = None
    if args.save or msg_send:
        if args.path:
            image.save(args.path + image_name, format='PNG', optimize=True, quality=1)
            image_path = args.path + image_name
        elif msg_send:
            image.save(c.TELEGRAM_IMAGES_SAVE_PATH + c.TELERGAM_IMAGE_PREFIX + image_name, 'PNG')
            image_path = c.TELEGRAM_IMAGES_SAVE_PATH + c.TELERGAM_IMAGE_PREFIX + image_name
        else:
            image.save(image_name, 'PNG')
            image_path = image_name
    if args.dont_show_image:
        image.show()

    return image_path, vars(args), colors, nebula.starting_point
import os, numpy as np, sys, cv2
from PIL import Image
from utils import is_path_exists, mkdir_if_missing, load_list_from_folder, fileparts, random_colors
from kitti_utils import read_label, compute_box_3d, draw_projected_box3d, Calibration

max_color = 30
colors = random_colors(max_color)       # Generate random colors
type_whitelist = ['Car', 'Pedestrian', 'Cyclist']
score_threshold = -10000
width = 1242
height = 374
seq_list = ['0000', '0003']

def vis(result_sha, data_root, result_root):
	def show_image_with_boxes(img, objects_res, object_gt, calib, save_path, height_threshold=0):
		img2 = np.copy(img) 

		for obj in objects_res:
			box3d_pts_2d, _ = compute_box_3d(obj, calib.P)
			color_tmp = tuple([int(tmp * 255) for tmp in colors[obj.id % max_color]])
			img2 = draw_projected_box3d(img2, box3d_pts_2d, color=color_tmp)
			text = 'ID: %d' % obj.id
			if box3d_pts_2d is not None:
				img2 = cv2.putText(img2, text, (int(box3d_pts_2d[4, 0]), int(box3d_pts_2d[4, 1]) - 8), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color=color_tmp) 

		img = Image.fromarray(img2)
		img = img.resize((width, height))
		img.save(save_path)
	
	for seq in seq_list:
		image_dir = os.path.join(data_root, 'image_02/%s' % seq)
Beispiel #6
0
import matplotlib.pyplot as plt
import numpy as np
import copy
import os
import cv2
import runthis

from utils import apply_mask, random_colors

if __name__ == '__main__':
    del_bg_dir=runthis.get_del_bg_dir()
    label_dir=runthis.get_label_dir()
    rois_dir = runthis.get_rois_dir()

    img_paths = next(os.walk(label_dir))[2]
    colors=random_colors(10)
    for img_path in img_paths:
        img = cv2.imread(del_bg_dir + img_path.split('.')[0]+'_delbg.png')     # 反常
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # 正常
        rois = np.loadtxt(rois_dir + img_path.split('.')[0] + '.rois', dtype=np.int, delimiter=',').tolist()
        mask_inone=np.loadtxt(label_dir+img_path,dtype=int,delimiter=',')
        minrow = rois[0]
        mincol = rois[1]
        maxrow = rois[2]
        maxcol = rois[3]

        temp1=mask_inone.flatten().tolist()
        labels = list(set(temp1))
        labels.sort()
        labels=labels[1:]
        mask_part=[]
Beispiel #7
0
    def build_targets(self, frame, debug=False):
        """
        assign anchors to corresponding ground truth targets
        :param frame: frame index
        :param debug: debug mode
        :return:
        """
        gt_data = self.get_gt_data(frame)
        gt_boxes = gt_data[:, 2:6]

        IoU = np_vec_iou(self.anchors, gt_boxes)
        # Initialize target index as -1's (assign index=-1 for negative anchors)
        target_index = np.zeros(self.anchors.shape[0], dtype=np.int) - 1
        # Assign positive anchor box to the gt target with whom the anchor box has the maxIoU >= self.iou_pos_threshold
        pos_mask = np.max(
            IoU,
            axis=1) >= self.iou_pos_threshold  # mask of the positive anchors
        target_index[pos_mask] = np.argmax(IoU, axis=1)[pos_mask]

        # todo: deal with thresholded negative indices
        neg_mask = np.max(IoU, axis=1) < self.iou_neg_threshold

        # Assign the gt target's class label to the anchor
        anchor_class = target_index.copy()
        # background_mask = anchor_class == -1
        anchor_class[~pos_mask] = 0  # Backgroud
        anchor_class[pos_mask] = 1  # Pedestrian

        # Calculate the bbox offsets from POSITIVE anchors to their assigned target gt
        # according to SSD equation (2). https://arxiv.org/pdf/1512.02325.pdf
        targets = gt_boxes[target_index]
        ll_norm = (targets[:, 0] - self.anchors[:, 0]
                   ) / self.anchors[:, 2]  # g_hat_x = (g_x - d_x) / d_w
        rr_norm = (targets[:, 1] - self.anchors[:, 1]
                   ) / self.anchors[:, 3]  # g_hat_y = (g_y - d_y) / d_h
        w_norm = np.log(targets[:, 2] /
                        self.anchors[:, 2])  # g_hat_w = log(g_w / d_w)
        h_norm = np.log(targets[:, 3] /
                        self.anchors[:, 3])  # g_hat_h = log(g_h / d_h)
        # concatenate and mask only the positive anchors
        offsets = np.concatenate((ll_norm.reshape(-1, 1), rr_norm.reshape(
            -1, 1), w_norm.reshape(-1, 1), h_norm.reshape(-1, 1)),
                                 axis=1)
        offsets *= pos_mask.astype(np.int).reshape(-1, 1)

        if debug:
            image = self.img[frame]
            gt_ids = gt_data[:, 1]
            max_color = 5
            colors = random_colors(max_color, bright=True)
            image = visualize_boxes(image,
                                    self.anchors,
                                    width=1,
                                    outline="white")

            # Visualize gt boxes
            for i, gt_box in enumerate(gt_boxes):
                id = gt_ids[i]
                color_tmp = tuple(
                    [int(tmp * 255) for tmp in colors[int(id % max_color)]])
                image = visualize_boxes(image, [gt_box],
                                        width=5,
                                        outline=color_tmp)

            colors = random_colors(max_color, bright=False)

            # Visualize anchors and targets
            for j, anchor_box in enumerate(self.anchors):
                target_j = int(target_index[j])
                if target_j != -1:
                    id = gt_ids[target_j]
                    color_tmp = tuple([
                        int(tmp * 255) for tmp in colors[int(id % max_color)]
                    ])
                    width = 2
                    # offset_j = offsets[j]
                    # gw, gh = np.exp(offset_j[2:]) * anchor_box[2:]
                    # gleft, gtop = offset_j[:2] * anchor_box[2:] + anchor_box[:2]
                    # anchor_box_aligned = [gleft, gtop, gw, gh]
                    # image = visualize_boxes(image, [anchor_box_aligned], width=width, outline=color_tmp)
                    image = visualize_boxes(image, [anchor_box],
                                            width=width,
                                            outline=color_tmp)
            # save images for debug
            os.makedirs("/hdd/yongxinw/Det/debug", exist_ok=True)
            image.save(
                osp.join("/hdd/yongxinw/Det/debug",
                         "anchors_{:03d}.jpg".format(frame + 1)))

        return pos_mask, neg_mask, gt_data, target_index, anchor_class, offsets, self.anchors
Beispiel #8
0
def labelit(img, mask_part, labelbase, rois):
    """
    人工给每部分重新打标签
    :param img: 图片
    :param mask_part: 标签
    :return: 重打过后的mask
    """
    n_part = len(mask_part)
    colors = random_colors(n_part)
    minrow = rois[0]
    mincol = rois[1]
    maxrow = rois[2]
    maxcol = rois[3]
    newmask = np.zeros(mask_part[0].shape)
    newmask = newmask.astype(np.int32)
    print("按从左到右顺序输入标签:")

    for i in range(n_part):
        mask = mask_part[i]
        temp = copy.copy(img)
        onepart = apply_mask(temp, mask, colors[i])
        onepart = onepart[minrow:maxrow, mincol:maxcol, ]

        # print(i,onepart.shape)
        ax = plt.subplot(1, n_part, i + 1)
        # print(ax)
        plt.imshow(onepart)
        ax.set_title('part')
        plt.xticks([]), plt.yticks([])
        plt.tight_layout()
    plt.gca().xaxis.set_major_locator(plt.NullLocator())
    plt.gca().yaxis.set_major_locator(plt.NullLocator())
    plt.margins(0, 0)
    plt.show()

    labels = str(input())
    if labels[0] == '.':
        return []
    # if labels[0]=='r' or labels[0]=='R':
    #     for i in range(n_part):
    #         mask = mask_part[i]
    #         temp = copy.copy(img)
    #         onepart = apply_mask(temp, mask, colors[i])
    #         onepart = onepart[minrow:maxrow, mincol:maxcol, ]
    #         cv2.imwrite(str(i)+'.png',onepart)
    #     for i in range(n_part):
    #         onepart=cv2.imread(str(i)+'.png')
    #         ax = plt.subplot(1, n_part, i + 1)
    #         plt.imshow(onepart)
    #         ax.set_title('part')
    #         plt.xticks([]), plt.yticks([])
    #         plt.tight_layout()
    #     plt.gca().xaxis.set_major_locator(plt.NullLocator())
    #     plt.gca().yaxis.set_major_locator(plt.NullLocator())
    #     plt.margins(0, 0)
    #     plt.show()
    for i in range(n_part):
        if int(labels[i]) == 0:
            newmask = np.where(mask_part[i] > 0, np.zeros(newmask.shape),
                               newmask)  # 忽然发现这句好像没用?
        else:
            newmask = np.where(
                mask_part[i] > 0,
                np.ones(newmask.shape, dtype=int) *
                (labelbase * 10 + int(labels[i])), newmask)

    # 一部分一部分标
    # for mask in mask_part:
    #     plt.figure(figsize=(10, 10))
    #     print("输入这部分的标签:")
    #     temp=copy.copy(img)
    #     onepart=apply_mask(temp,mask,colors[0])
    #     plt.subplot(1,1,1)
    #     plt.imshow(onepart)
    #     plt.title("输入这部分的标签")
    #     plt.xticks([]), plt.yticks([])
    #     # plt.show()
    #     plt.ion()
    #     plt.pause(2)
    #     plt.close()
    #     newlabel=int(input())
    #     newmask=np.where(mask>0,newlabel,newmask)
    return newmask