File size: 4,563 Bytes
1bd13a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
Few-shot split utilities and support set selection.

Provides:
- Reproducible train/val/test split creation
- Stratified few-shot subset selection (1-shot, 5-shot, 10-shot per class)
"""

import os
import glob
import random
import numpy as np
from pathlib import Path
from typing import Dict, List, Tuple, Optional
from collections import defaultdict

from src.data.yolo_to_masks import parse_yolo_label


def get_class_to_samples(labels_dir: str, images_dir: str) -> Dict[int, List[str]]:
    """
    Map each class to the list of image stems containing that class.
    
    Returns:
        Dict mapping class_id → list of stems
    """
    class_to_stems = defaultdict(list)
    
    label_files = sorted(glob.glob(os.path.join(labels_dir, '*.txt')))
    
    for label_path in label_files:
        stem = Path(label_path).stem
        
        # Verify image exists
        img_exists = False
        for ext in ['.jpeg', '.jpg', '.png']:
            if os.path.exists(os.path.join(images_dir, stem + ext)):
                img_exists = True
                break
        
        if not img_exists:
            continue
        
        annotations = parse_yolo_label(label_path)
        classes_in_image = set(a[0] for a in annotations)
        
        for cls_id in classes_in_image:
            class_to_stems[cls_id].append(stem)
    
    return dict(class_to_stems)


def select_few_shot_support(labels_dir: str,
                             images_dir: str,
                             n_shots: int,
                             seed: int = 42,
                             strategy: str = 'random') -> Tuple[List[str], List[str]]:
    """
    Select a few-shot support set, stratified by class.
    
    Args:
        labels_dir: Path to labels
        images_dir: Path to images
        n_shots: Number of examples per class
        seed: Random seed
        strategy: 'random' or 'diverse'
        
    Returns:
        support_stems: List of stems selected as support
        query_stems: Remaining stems for evaluation
    """
    rng = random.Random(seed)
    
    class_to_stems = get_class_to_samples(labels_dir, images_dir)
    
    support_stems = set()
    
    for cls_id, stems in sorted(class_to_stems.items()):
        available = [s for s in stems if s not in support_stems]
        
        if strategy == 'random':
            selected = rng.sample(available, min(n_shots, len(available)))
        elif strategy == 'diverse':
            # Select diverse examples (spread across the stem ID range)
            indices = np.linspace(0, len(available) - 1, min(n_shots, len(available)))
            selected = [available[int(i)] for i in indices]
        else:
            raise ValueError(f"Unknown strategy: {strategy}")
        
        support_stems.update(selected)
    
    # All stems
    all_stems = set()
    for stems in class_to_stems.values():
        all_stems.update(stems)
    
    query_stems = sorted(all_stems - support_stems)
    support_stems = sorted(support_stems)
    
    return support_stems, query_stems


def print_split_statistics(labels_dir: str, images_dir: str, stems: List[str], name: str):
    """Print class distribution for a set of stems."""
    CLASS_NAMES = [
        'giant cell tumor', 'multiple osteochondromas', 'osteochondroma',
        'osteofibroma', 'osteosarcoma', 'other bt', 'other mt',
        'simple bone cyst', 'synovial osteochondroma'
    ]
    
    class_counts = defaultdict(int)
    
    for stem in stems:
        label_path = os.path.join(labels_dir, stem + '.txt')
        if os.path.exists(label_path):
            annotations = parse_yolo_label(label_path)
            for cls_id, _, _, _, _ in annotations:
                class_counts[cls_id] += 1
    
    print(f"\n{name} ({len(stems)} images):")
    for cls_id in range(9):
        count = class_counts.get(cls_id, 0)
        print(f"  {CLASS_NAMES[cls_id]:30s}: {count}")


if __name__ == '__main__':
    root = '.'
    labels_dir = os.path.join(root, 'BTXRD dataset', 'labels', 'train')
    images_dir = os.path.join(root, 'BTXRD dataset', 'images', 'train')
    
    for n_shots in [1, 5, 10]:
        support, query = select_few_shot_support(
            labels_dir, images_dir, n_shots=n_shots
        )
        print(f"\n{'='*60}")
        print(f"{n_shots}-shot selection")
        print(f"{'='*60}")
        print(f"Support set: {len(support)} images")
        print(f"Query set: {len(query)} images")
        
        print_split_statistics(labels_dir, images_dir, support, f"{n_shots}-shot support")