Exemplo n.º 1
0
                pre.append(int(argmax(pred, 0)))
            else:
                pre += argmax(pred, 1).tolist()
            pbar.update(img.shape[0])
    return np.array(pre)


df = pd.DataFrame(columns=[
    'filename', 'samples', 'class0', 'class1', 'class2', 'class3', 'gt'
])
tifs = os.listdir('/mnt/hd1/antoine/TissueNet/data/Train_Metadata/tif')
with tqdm(total=len(tifs), desc=f'evaluating', unit='img') as pbar2:
    for tif_name in tifs:
        assert (tif_name.endswith('.tif'))
        tif_dir = '/mnt/hd1/antoine/TissueNet/data/Train_Metadata/tif/' + tif_name
        img = np_from_tif(tif_dir, 6)
        #print(img.shape)
        gt = []
        for i in range(4):
            gt.append(int(csv[csv['filename'] == tif_name][str(i)]))
        mask = seg_hsv_threshold(img,
                                 disp=False,
                                 denoise_f=(denoise_erode, denoise_bilatera))
        #print(mask.shape)
        boxes, contours, _ = get_boxes_contours(img, mask, disp=False)
        R = ROI()
        R.set_boxes(name=tif_name,
                    H=img.shape[0],
                    W=img.shape[1],
                    boxes=boxes,
                    coordinate='lu')
            if len(list(pred.size())) == 1:
                pre.append(int(argmax(pred, 0)))
            else:
                pre += argmax(pred, 1).tolist()
            pbar.update(img.shape[0])
    return np.array(pre)


with tqdm(total=len(tifs), desc=f'evaluating', unit='img') as pbar2:
    for tif_name in tifs:
        tif_dir = 'data/' + tif_name
        layer = 8  # the layer for segmentation
        img = None
        while (img is None):
            try:
                img = np_from_tif(tif_dir, layer)
            except pyvips.error.Error:
                layer -= 1
            else:
                if img.shape[0] < 500 or img.shape[
                        1] < 500:  # too small segmentation layer means bad resolution
                    img = None
                    layer -= 1
        print('image for segmentation with size: ', img.shape)
        mask = seg_hsv_threshold(img,
                                 disp=False,
                                 denoise_f=(denoise_erode, denoise_bilatera))
        boxes, contours, _ = get_boxes_contours(img, mask, disp=False)
        if len(boxes) == 0:
            df = df.append([{
                'filename': tif_name,
def get_segmented_patches(tif_path):

    # choose propriate layer
    layer = 8  # the layer for segmentation
    img = None
    while (img is None and layer > 0):
        try:
            img = np_from_tif(tif_path, layer)
        except pyvips.error.Error:
            layer -= 1
        else:
            if img.shape[0] < 500 or img.shape[
                    1] < 500:  # too small segmentation layer means bad resolution
                img = None
                layer -= 1

    print('image for segmentation with size: ', img.shape)

    # get boxes
    mask = seg_hsv_threshold(img,
                             disp=False,
                             denoise_f=(denoise_erode, denoise_bilatera))
    boxes, contours, _ = get_boxes_contours(img, mask, disp=False)
    if len(boxes) == 0:
        return None, None, None, None
    R = ROI()
    R.set_boxes(name=tif_name,
                H=img.shape[0],
                W=img.shape[1],
                boxes=boxes,
                coordinate='lu')
    boxes_normalized = R.get_boxes_normalized(name=tif_name, coordinate='lu')

    p = 0
    ind = 0
    num_patches = 0

    # if there are too many patches (here more than 2000), downsample the tif image
    while ind == 0:
        # build dataset from tif and detected ROI
        if p == 0:
            patches = TissueTestAnno(tif_path,
                                     rois=boxes_normalized,
                                     mask=mask,
                                     stride=2048,
                                     grid_size=2048,
                                     page=p,
                                     mask_page=layer,
                                     transform=transform)
        elif p == 1:
            patches = TissueTestAnno(tif_path,
                                     rois=boxes_normalized,
                                     mask=mask,
                                     stride=1536,
                                     grid_size=1536,
                                     page=p,
                                     mask_page=layer,
                                     transform=transform)
        elif p == 2:
            patches = TissueTestAnno(tif_path,
                                     rois=boxes_normalized,
                                     mask=mask,
                                     stride=1024,
                                     grid_size=1024,
                                     page=p,
                                     mask_page=layer,
                                     transform=transform)
            ind = 1
        num_patches = len(patches)
        if (num_patches < 500):
            ind = 1
        p += 1

    if num_patches == 0:
        return None, None, None, None

    # initialize dataloader
    val_loader = DataLoader(patches, batch_size=12, shuffle=False)
    anchors_normalized = patches.get_anchors_normalized()
    anchor_group_info = patches.get_anchor_group()

    return val_loader, boxes_normalized, anchors_normalized, anchor_group_info