-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
231 lines (193 loc) · 7.65 KB
/
settings.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import os
from dataclasses import dataclass, field
from typing import List, Optional, Dict, Any
from hydra.utils import to_absolute_path
from omegaconf.omegaconf import MISSING
ROOT_DIR = os.path.realpath(os.path.dirname(__file__))
# path to the CUB 200-2011 folder (e.g., /path/to/CUB_200_2011)
PATH_TO_RAW_CUB_200 = 'datasets/CUB_200_2011'
@dataclass
class BaseDataset:
"""
data_path: str
path to the root folder of the dataset (e.g., datasets/cub200_cropped)
train_dir: str
directory containing the augmented training set (<data_path>/train_cropped_augmented)
test_dir: str
the directory containing the test set (<data_path>/test_cropped")
train_push_dir: str
the directory containing the original (unaugmented) training set (<data_path>/train_cropped)
"""
name: str = MISSING
img_size: int = MISSING
num_classes: int = MISSING
data_path: str = MISSING
relative_data_path: str = MISSING
train_directory: str = 'train_cropped_augmented'
train_push_directory: str = 'train_cropped'
test_directory: str = 'test_cropped'
remembering_protos_directory: str = 'remembering_prototypes'
forbidden_protos_directory: str = 'forbidden_prototypes'
train_batch_size: int = MISSING
test_batch_size: int = MISSING
train_push_batch_size: int = MISSING
test_on_segmented_image: bool = False
train_on_segmented_image: bool = False
@dataclass
class Cub200DatasetConfig(BaseDataset):
img_size: int = 224
train_batch_size: int = 20
test_batch_size: int = 30
train_push_batch_size: int = 25
@dataclass
class Cub200ArtificialConfound(Cub200DatasetConfig):
name: str = 'cub200_artificial'
num_classes: int = 5
relative_data_path: str = 'datasets/cub200_cropped/confound_artificial'
data_path: str = os.path.join(os.path.realpath(os.path.dirname(__file__)),
relative_data_path)
@dataclass
class Cub200Clean5(Cub200DatasetConfig):
name: str = 'cub200_clean_5'
num_classes: int = 5
relative_data_path: str = 'datasets/cub200_cropped/clean_5_classes'
data_path: str = os.path.join(os.path.realpath(os.path.dirname(__file__)),
relative_data_path)
@dataclass
class Cub200CleanTop20(Cub200DatasetConfig):
name: str = 'cub200_clean_top20'
num_classes: int = 20
train_batch_size: int = 128
relative_data_path: str = 'datasets/cub200_cropped/clean_top_20'
test_directory = 'test_cropped_shuffled'
data_path: str = os.path.join(os.path.realpath(os.path.dirname(__file__)),
relative_data_path)
@dataclass
class Cub200CleanAll(Cub200DatasetConfig):
name: str = 'cub200_clean_all'
num_classes: int = 200
train_batch_size = 80
test_batch_size = 100
train_push_batch_size = 75
relative_data_path: str = 'datasets/cub200_cropped/clean_all'
data_path: str = os.path.join(os.path.realpath(os.path.dirname(__file__)),
relative_data_path)
@dataclass
class Cub200BackgroundConfound(Cub200DatasetConfig):
name: str = 'cub200_bgconf'
relative_data_path: str = 'datasets/cub200_cropped/confound_background'
data_path: str = os.path.join(os.path.realpath(os.path.dirname(__file__)),
relative_data_path)
@dataclass
class SyntheticDatasetConfig(BaseDataset):
name: str = 'synthetic'
data_path = os.path.join(os.path.realpath(os.path.dirname(__file__)),
'datasets/synthetic')
img_size = 80
num_classes = 3
train_batch_size = 10
test_batch_size = 25
train_push_batch_size = 30
@dataclass
class CovidDatasetConfig(BaseDataset):
name: str = 'covid'
relative_data_path: str = 'datasets/covid'
data_path = os.path.join(os.path.realpath(os.path.dirname(__file__)),
relative_data_path)
img_size: int = 224
num_classes = 2
train_batch_size = 20
test_batch_size = 30
train_push_batch_size = 35
DATASET_CONFIGS = {
Cub200ArtificialConfound.name: Cub200ArtificialConfound,
Cub200Clean5.name: Cub200Clean5,
Cub200CleanTop20.name: Cub200CleanTop20,
Cub200CleanAll.name: Cub200CleanAll,
Cub200BackgroundConfound.name: Cub200BackgroundConfound,
SyntheticDatasetConfig.name: SyntheticDatasetConfig,
CovidDatasetConfig.name: CovidDatasetConfig
}
@dataclass
class ModelConfig:
base_architecture: str = 'vgg11'
pretrained_model_path: str = to_absolute_path('pretrained_models')
num_prototypes_per_class: int = 2
prototype_shape: tuple = (128, 1, 1)
prototype_activation_function: str = 'log'
add_on_layers_type: str = 'regular'
joint_optimizer_lrs: Dict[str, float] = field(default_factory=
lambda: {'features': 1e-4,
'add_on_layers': 3e-3,
'prototype_vectors': 3e-3})
joint_lr_step_size: int = 5
warm_optimizer_lrs: Dict[str, float] = field(default_factory=
lambda: {'add_on_layers': 3e-3,
'prototype_vectors': 3e-3})
last_layer_optimizer_lr: float = 1e-4
gamma: float = 0.15 # original 0.1 Gamma of learning rate scheduler of the joint optimizer
@dataclass
class Lambdas:
# coefs: weighting of different training losses
crs_ent: float = 1
clst: float = 0.5
sep: float = -0.08
l1: float = 1e-4
debug: float = 0
rem: float = 0
# clst_step_size: int = 1
# clst_gamma: float = 1.001
coefs: Lambdas = Lambdas()
# 1 as in original PNet paper, 5% in IAIA-BL paper
# if value in (0,1), relative number of patches
# If >= 1, absolute number of patches
topk_k: float = 1
@dataclass
class Debug:
# type of loss used to fix wrong prototype
# ['attribute', 'aggregation', 'prototypes', 'kernel', 'iaiabl']
loss: Optional[str] = None
fine_annotation: Optional[float] = None # % of images that are fine annotated
path_to_model: Optional[str] = None # load existing model
load_optimizer: bool = False # load optimizer and scheduler from path_to_model
auto_path_to_model: bool = False # take model from previous iteration
hard_constraint: bool = False
# list of prototype number to remove [0, number of prototypes)
protos: List[int] = field(default_factory=list)
# list of classes idx [0,number of classes)
classes: List[int] = field(default_factory=list)
# only for aggregation loss
class_specific_penalization: bool = True
act_place: str = 'center' # ['center', 'all']
epsilon: float = 1e-8
# defaults = [
# {"data": "cub200_clean_top20"}
# ]
@dataclass
class ExperimentConfig:
"""
gpuid: list
GPU device ID(s)
wandb: bool
warm_epochs: int
number of epochs (passes through the dataset)
last_layer_iterations: int
number of last layer optimization iterations, before pushing the prototypes
push_start: int
push start epoch
"""
# defaults: List[Any] = field(default_factory=lambda: defaults)
experiment_name: str = MISSING
dry_run: bool = False
seed: int = 0
cpu: bool = False
gpuid: List[str] = field(default_factory=lambda: [0, 1])
wandb: bool = False
epochs: int = 15
warm_epochs: int = 5
last_layer_iterations: int = 0
push_start: int = 0
push_epochs: Optional[List[int]] = field(default_factory=list)
data: BaseDataset = MISSING
model: ModelConfig = ModelConfig()
debug: Debug = Debug()