in sampling.py [0:0]
def get_slides(database: Database,
test_slide_ids: List[int],
base_path: str = 'WSI',
size: int = 256,
positive_class: int = 2,
negative_class: int = 7):
lbl_bbox = []
training_slides = []
test_slides = []
files = []
slides = tqdm(
database.execute("SELECT uid, filename FROM Slides").fetchall(),
desc='Loading slides...',
)
for idx, (cur_slide, filename) in enumerate(slides):
database.loadIntoMemory(cur_slide)
slide_path = os.path.join(base_path, filename)
slide = openslide.open_slide(slide_path)
level = 0
level_dimension = slide.level_dimensions[level]
down_factor = slide.level_downsamples[level]
# Dictionary of classes found so far, initialized with the positive class, which
# we are sure it exists.
classes = {
positive_class: 1
}
annotations = {}
labels, bboxes = [], []
for id, annotation in database.annotations.items():
# Ignore deleted annotations, or any that are not of type SPOT.
if annotation.deleted or annotation.annotationType != AnnotationType.SPOT:
continue
annotation.r = 25
d = 2 * annotation.r / down_factor
x_min = (annotation.x1 - annotation.r) / down_factor
y_min = (annotation.y1 - annotation.r) / down_factor
x_max = x_min + d
y_max = y_min + d
# If the annotation class is not yet in the classes dictionary, create
# an entry and initialize the structures within.
if annotation.agreedClass not in annotations:
annotations[annotation.agreedClass] = {}
annotations[annotation.agreedClass]['bboxes'] = []
annotations[annotation.agreedClass]['label'] = []
# Store bounding box and label into the annotation dictionary.
annotations[annotation.agreedClass]['bboxes'].append([int(x_min), int(y_min), int(x_max), int(y_max)])
annotations[annotation.agreedClass]['label'].append(annotation.agreedClass)
# Store the bounding box and its label separately in the bboxes and labels
# lists.
if annotation.agreedClass in classes:
label = classes[annotation.agreedClass]
# bboxes.append([int(x_min), int(y_min), int(x_max), int(y_max)])
bboxes.append(BoundingBox(int(x_min), int(y_min), int(x_max), int(y_max)))
labels.append(label)
if len(bboxes) > 0:
lbl_bbox.append([bboxes, labels])
is_test = str(cur_slide) in test_slide_ids
if is_test:
files.append(SlideContainer(
file=slide_path,
annotations=annotations,
level=level,
width=size,
height=size,
y=[bboxes, labels],
sample_func=partial(sampling_func, negative_class=negative_class)))
test_slides.append(len(files) - 1)
else:
files.append(SlideContainer(
file=slide_path,
annotations=annotations,
level=level,
width=size,
height=size,
y=[bboxes, labels],
sample_func=partial(sampling_func, negative_class=negative_class)))
training_slides.append(len(files) - 1)
return lbl_bbox, training_slides, test_slides, files