-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathal_comp.py
79 lines (64 loc) · 2.43 KB
/
al_comp.py
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
from .al_types import Comp
from .al_round import ALRound
import numpy as np
class ALComp:
"""A Component defines an active learning run.
Specifically, it defines:
- Inital data.
- A list of rounds performed in the active learning run.
"""
type: Comp
rounds: list[ALRound] = []
seed: int = 0
def append_round(self, round: ALRound):
self.rounds.append(round)
def set_rounds(self, rounds: list[ALRound]):
self.rounds = rounds
def set_seed(self, seed: int):
self.seed = seed
def get_seed(self):
return self.seed
def get_comp_desc(self):
return "+".join(list(dict.fromkeys([r.get_rounds_desc() for r in self.rounds])))
def get_init_idx(self, X_training, y_training=None):
"""Get initial data."""
raise NotImplementedError(f"{self} has not implemented 'get_init_data'.")
class RandomComp(ALComp):
"""Component with random selected initial data."""
init_size: int
def __init__(self, init_size: int, rounds: list[ALRound]=[], seed=0):
super().__init__()
self.type = Comp.RANDOM
self.init_size = init_size
self.rounds = rounds
self.seed = seed
def set_init_size(self, init_size: int):
self.init_size = init_size
def get_init_idx(self, X_training, y_training):
#TODO: add init foil error handling
valid = False
idx_lst = []
while not valid:
idx_lst = np.random.choice(len(y_training), size=self.init_size, replace=False)
if len(set(y_training[idx_lst])) >= 2:
valid = True
return idx_lst
class ManualComp(ALComp):
"""Component with manually selected initial data."""
init_imgIds: list[str] = []
def __init__(self, init_imgIds: list[str]=[], rounds: list[ALRound]=[], seed=0):
super().__init__()
self.type = Comp.MANUAL
self.init_imgIds = init_imgIds
self.rounds = rounds
self.seed = seed
def set_init_imgIds(self, init_imgIds: list[str]):
self.init_imgIds = init_imgIds
def get_init_idx(self, X_training, y_training=None):
init_idx = []
for idx, x in enumerate(X_training):
if x['imageId'] in self.init_imgIds:
init_idx.append(idx)
if len(init_idx) != len(self.init_imgIds):
raise Exception(f"Cannot find all imageIds in training_data.")
return np.array(init_idx)